15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/metrics/sparse_histogram.h"
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/browser/renderer_host/pepper/pepper_message_filter.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/browser/tracing/trace_message_filter.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/common/pepper_renderer_instance_data.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/process_type.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ipc/ipc_message_macros.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BrowserPpapiHost* BrowserPpapiHost::CreateExternalPluginProcess(
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IPC::Sender* sender,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ppapi::PpapiPermissions permissions,
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::ProcessHandle plugin_child_process,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IPC::ChannelProxy* channel,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int render_process_id,
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int render_view_id,
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& profile_directory) {
262385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // The plugin name and path shouldn't be needed for external plugins.
272385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  BrowserPpapiHostImpl* browser_ppapi_host =
28a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new BrowserPpapiHostImpl(sender,
29a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                               permissions,
30a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                               std::string(),
31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                               base::FilePath(),
32a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                               profile_directory,
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                               false /* in_process */,
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                               true /* external_plugin */);
352385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  browser_ppapi_host->set_plugin_process_handle(plugin_child_process);
362385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<PepperMessageFilter> pepper_message_filter(
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      new PepperMessageFilter());
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  channel->AddFilter(pepper_message_filter->GetFilter());
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  channel->AddFilter(browser_ppapi_host->message_filter().get());
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  channel->AddFilter((new TraceMessageFilter())->GetFilter());
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser_ppapi_host;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BrowserPpapiHostImpl::BrowserPpapiHostImpl(
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IPC::Sender* sender,
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const ppapi::PpapiPermissions& permissions,
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& plugin_name,
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const base::FilePath& plugin_path,
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_data_directory,
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool in_process,
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    bool external_plugin)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : ppapi_host_(new ppapi::host::PpapiHost(sender, permissions)),
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      plugin_process_handle_(base::kNullProcessHandle),
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      plugin_name_(plugin_name),
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      plugin_path_(plugin_path),
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_data_directory_(profile_data_directory),
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      in_process_(in_process),
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      external_plugin_(external_plugin),
61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      ssl_context_helper_(new SSLContextHelper()) {
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  message_filter_ = new HostMessageFilter(ppapi_host_.get(), this);
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ppapi_host_->AddHostFactoryFilter(scoped_ptr<ppapi::host::HostFactory>(
64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new ContentBrowserPepperHostFactory(this)));
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BrowserPpapiHostImpl::~BrowserPpapiHostImpl() {
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify the filter so it won't foward messages to us.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message_filter_->OnHostDestroyed();
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Delete the host explicitly first. This shutdown will destroy the
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // resources, which may want to do cleanup in their destructors and expect
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // their pointers to us to be valid.
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ppapi_host_.reset();
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ppapi::host::PpapiHost* BrowserPpapiHostImpl::GetPpapiHost() {
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return ppapi_host_.get();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::ProcessHandle BrowserPpapiHostImpl::GetPluginProcessHandle() const {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handle should previously have been set before use.
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(in_process_ || plugin_process_handle_ != base::kNullProcessHandle);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return plugin_process_handle_;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BrowserPpapiHostImpl::IsValidInstance(PP_Instance instance) const {
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return instance_map_.find(instance) != instance_map_.end();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool BrowserPpapiHostImpl::GetRenderFrameIDsForInstance(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_Instance instance,
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int* render_process_id,
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int* render_frame_id) const {
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstanceMap::const_iterator found = instance_map_.find(instance);
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (found == instance_map_.end()) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *render_process_id = 0;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *render_frame_id = 0;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *render_process_id = found->second.render_process_id;
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *render_frame_id = found->second.render_frame_id;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const std::string& BrowserPpapiHostImpl::GetPluginName() {
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return plugin_name_;
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const base::FilePath& BrowserPpapiHostImpl::GetPluginPath() {
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return plugin_path_;
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath& BrowserPpapiHostImpl::GetProfileDataDirectory() {
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return profile_data_directory_;
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GURL BrowserPpapiHostImpl::GetDocumentURLForInstance(PP_Instance instance) {
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstanceMap::const_iterator found = instance_map_.find(instance);
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (found == instance_map_.end())
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return GURL();
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return found->second.document_url;
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GURL BrowserPpapiHostImpl::GetPluginURLForInstance(PP_Instance instance) {
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstanceMap::const_iterator found = instance_map_.find(instance);
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (found == instance_map_.end())
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return GURL();
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return found->second.plugin_url;
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void BrowserPpapiHostImpl::SetOnKeepaliveCallback(
134a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const BrowserPpapiHost::OnKeepaliveCallback& callback) {
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  on_keepalive_callback_ = callback;
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BrowserPpapiHostImpl::AddInstance(
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PP_Instance instance,
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const PepperRendererInstanceData& instance_data) {
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(instance_map_.find(instance) == instance_map_.end());
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  instance_map_[instance] = instance_data;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void BrowserPpapiHostImpl::DeleteInstance(PP_Instance instance) {
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstanceMap::iterator found = instance_map_.find(instance);
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (found == instance_map_.end()) {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  instance_map_.erase(found);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BrowserPpapiHostImpl::HostMessageFilter::HostMessageFilter(
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ppapi::host::PpapiHost* ppapi_host,
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    BrowserPpapiHostImpl* browser_ppapi_host_impl)
157a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    : ppapi_host_(ppapi_host),
158a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      browser_ppapi_host_impl_(browser_ppapi_host_impl) {}
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BrowserPpapiHostImpl::HostMessageFilter::OnMessageReceived(
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const IPC::Message& msg) {
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't forward messages if our owner object has been destroyed.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ppapi_host_)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool handled = true;
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IPC_BEGIN_MESSAGE_MAP(BrowserPpapiHostImpl::HostMessageFilter, msg)
168a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Add necessary message handlers here.
169a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  IPC_MESSAGE_HANDLER(PpapiHostMsg_Keepalive, OnKeepalive)
170a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  IPC_MESSAGE_HANDLER(PpapiHostMsg_LogInterfaceUsage,
171a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                      OnHostMsgLogInterfaceUsage)
172a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  IPC_MESSAGE_UNHANDLED(handled = ppapi_host_->OnMessageReceived(msg))
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC_END_MESSAGE_MAP();
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return handled;
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BrowserPpapiHostImpl::HostMessageFilter::OnHostDestroyed() {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ppapi_host_);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ppapi_host_ = NULL;
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  browser_ppapi_host_impl_ = NULL;
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
183a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochBrowserPpapiHostImpl::HostMessageFilter::~HostMessageFilter() {}
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void BrowserPpapiHostImpl::HostMessageFilter::OnKeepalive() {
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (browser_ppapi_host_impl_)
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    browser_ppapi_host_impl_->OnKeepalive();
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void BrowserPpapiHostImpl::HostMessageFilter::OnHostMsgLogInterfaceUsage(
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int hash) const {
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UMA_HISTOGRAM_SPARSE_SLOWLY("Pepper.InterfaceUsed", hash);
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void BrowserPpapiHostImpl::OnKeepalive() {
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // An instance has been active. The on_keepalive_callback_ will be
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // used to permit the content embedder to handle this, e.g. by tracking
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // activity and shutting down processes that go idle.
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Currently embedders do not need to distinguish between instances having
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // different idle state, and thus this implementation handles all instances
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for this module together.
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (on_keepalive_callback_.is_null())
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
207a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  BrowserPpapiHost::OnKeepaliveInstanceData instance_data(instance_map_.size());
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InstanceMap::iterator instance = instance_map_.begin();
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int i = 0;
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  while (instance != instance_map_.end()) {
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    instance_data[i].render_process_id = instance->second.render_process_id;
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    instance_data[i].render_frame_id = instance->second.render_frame_id;
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    instance_data[i].document_url = instance->second.document_url;
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ++instance;
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ++i;
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  on_keepalive_callback_.Run(instance_data, profile_data_directory_);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
222