1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
7 #include "content/browser/renderer_host/pepper/pepper_message_filter.h"
8 #include "content/browser/tracing/trace_message_filter.h"
9 #include "content/common/pepper_renderer_instance_data.h"
10 #include "content/public/common/process_type.h"
11 #include "ipc/ipc_message_macros.h"
12 #include "ppapi/proxy/ppapi_messages.h"
17 BrowserPpapiHost
* BrowserPpapiHost::CreateExternalPluginProcess(
19 ppapi::PpapiPermissions permissions
,
20 base::ProcessHandle plugin_child_process
,
21 IPC::ChannelProxy
* channel
,
22 int render_process_id
,
24 const base::FilePath
& profile_directory
) {
25 // The plugin name and path shouldn't be needed for external plugins.
26 BrowserPpapiHostImpl
* browser_ppapi_host
=
27 new BrowserPpapiHostImpl(sender
, permissions
, std::string(),
28 base::FilePath(), profile_directory
,
29 false /* in_process */,
30 true /* external_plugin */);
31 browser_ppapi_host
->set_plugin_process_handle(plugin_child_process
);
33 scoped_refptr
<PepperMessageFilter
> pepper_message_filter(
34 new PepperMessageFilter());
35 channel
->AddFilter(pepper_message_filter
->GetFilter());
36 channel
->AddFilter(browser_ppapi_host
->message_filter());
37 channel
->AddFilter((new TraceMessageFilter())->GetFilter());
39 return browser_ppapi_host
;
42 BrowserPpapiHostImpl::BrowserPpapiHostImpl(
44 const ppapi::PpapiPermissions
& permissions
,
45 const std::string
& plugin_name
,
46 const base::FilePath
& plugin_path
,
47 const base::FilePath
& profile_data_directory
,
50 : ppapi_host_(new ppapi::host::PpapiHost(sender
, permissions
)),
51 plugin_process_handle_(base::kNullProcessHandle
),
52 plugin_name_(plugin_name
),
53 plugin_path_(plugin_path
),
54 profile_data_directory_(profile_data_directory
),
55 in_process_(in_process
),
56 external_plugin_(external_plugin
),
57 ssl_context_helper_(new SSLContextHelper()) {
58 message_filter_
= new HostMessageFilter(ppapi_host_
.get(), this);
59 ppapi_host_
->AddHostFactoryFilter(scoped_ptr
<ppapi::host::HostFactory
>(
60 new ContentBrowserPepperHostFactory(this)));
63 BrowserPpapiHostImpl::~BrowserPpapiHostImpl() {
64 // Notify the filter so it won't foward messages to us.
65 message_filter_
->OnHostDestroyed();
67 // Delete the host explicitly first. This shutdown will destroy the
68 // resources, which may want to do cleanup in their destructors and expect
69 // their pointers to us to be valid.
73 ppapi::host::PpapiHost
* BrowserPpapiHostImpl::GetPpapiHost() {
74 return ppapi_host_
.get();
77 base::ProcessHandle
BrowserPpapiHostImpl::GetPluginProcessHandle() const {
78 // Handle should previously have been set before use.
79 DCHECK(in_process_
|| plugin_process_handle_
!= base::kNullProcessHandle
);
80 return plugin_process_handle_
;
83 bool BrowserPpapiHostImpl::IsValidInstance(PP_Instance instance
) const {
84 return instance_map_
.find(instance
) != instance_map_
.end();
87 bool BrowserPpapiHostImpl::GetRenderFrameIDsForInstance(
89 int* render_process_id
,
90 int* render_frame_id
) const {
91 InstanceMap::const_iterator found
= instance_map_
.find(instance
);
92 if (found
== instance_map_
.end()) {
93 *render_process_id
= 0;
98 *render_process_id
= found
->second
.render_process_id
;
99 *render_frame_id
= found
->second
.render_frame_id
;
103 const std::string
& BrowserPpapiHostImpl::GetPluginName() {
107 const base::FilePath
& BrowserPpapiHostImpl::GetPluginPath() {
111 const base::FilePath
& BrowserPpapiHostImpl::GetProfileDataDirectory() {
112 return profile_data_directory_
;
115 GURL
BrowserPpapiHostImpl::GetDocumentURLForInstance(PP_Instance instance
) {
116 InstanceMap::const_iterator found
= instance_map_
.find(instance
);
117 if (found
== instance_map_
.end())
119 return found
->second
.document_url
;
122 GURL
BrowserPpapiHostImpl::GetPluginURLForInstance(PP_Instance instance
) {
123 InstanceMap::const_iterator found
= instance_map_
.find(instance
);
124 if (found
== instance_map_
.end())
126 return found
->second
.plugin_url
;
129 void BrowserPpapiHostImpl::SetOnKeepaliveCallback(
130 const BrowserPpapiHost::OnKeepaliveCallback
& callback
) {
131 on_keepalive_callback_
= callback
;
134 void BrowserPpapiHostImpl::AddInstance(
135 PP_Instance instance
,
136 const PepperRendererInstanceData
& instance_data
) {
137 DCHECK(instance_map_
.find(instance
) == instance_map_
.end());
138 instance_map_
[instance
] = instance_data
;
141 void BrowserPpapiHostImpl::DeleteInstance(PP_Instance instance
) {
142 InstanceMap::iterator found
= instance_map_
.find(instance
);
143 if (found
== instance_map_
.end()) {
147 instance_map_
.erase(found
);
150 BrowserPpapiHostImpl::HostMessageFilter::HostMessageFilter(
151 ppapi::host::PpapiHost
* ppapi_host
,
152 BrowserPpapiHostImpl
* browser_ppapi_host_impl
)
153 : ppapi_host_(ppapi_host
),
154 browser_ppapi_host_impl_(browser_ppapi_host_impl
) {
157 bool BrowserPpapiHostImpl::HostMessageFilter::OnMessageReceived(
158 const IPC::Message
& msg
) {
159 // Don't forward messages if our owner object has been destroyed.
164 IPC_BEGIN_MESSAGE_MAP(BrowserPpapiHostImpl::HostMessageFilter
, msg
)
165 // Add necessary message handlers here.
166 IPC_MESSAGE_HANDLER(PpapiHostMsg_Keepalive
, OnKeepalive
)
167 IPC_MESSAGE_UNHANDLED(handled
= ppapi_host_
->OnMessageReceived(msg
))
168 IPC_END_MESSAGE_MAP();
172 void BrowserPpapiHostImpl::HostMessageFilter::OnHostDestroyed() {
175 browser_ppapi_host_impl_
= NULL
;
178 BrowserPpapiHostImpl::HostMessageFilter::~HostMessageFilter() {
181 void BrowserPpapiHostImpl::HostMessageFilter::OnKeepalive() {
182 if (browser_ppapi_host_impl_
)
183 browser_ppapi_host_impl_
->OnKeepalive();
186 void BrowserPpapiHostImpl::OnKeepalive() {
187 // An instance has been active. The on_keepalive_callback_ will be
188 // used to permit the content embedder to handle this, e.g. by tracking
189 // activity and shutting down processes that go idle.
191 // Currently embedders do not need to distinguish between instances having
192 // different idle state, and thus this implementation handles all instances
193 // for this module together.
195 if (on_keepalive_callback_
.is_null())
198 BrowserPpapiHost::OnKeepaliveInstanceData
199 instance_data(instance_map_
.size());
201 InstanceMap::iterator instance
= instance_map_
.begin();
203 while (instance
!= instance_map_
.end()) {
204 instance_data
[i
].render_process_id
= instance
->second
.render_process_id
;
205 instance_data
[i
].render_frame_id
= instance
->second
.render_frame_id
;
206 instance_data
[i
].document_url
= instance
->second
.document_url
;
210 on_keepalive_callback_
.Run(instance_data
, profile_data_directory_
);
213 } // namespace content