Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / browser_ppapi_host_impl.cc
blob3011603e244cbf703f50c2e22582bdef25a41396
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 "base/metrics/sparse_histogram.h"
8 #include "content/browser/renderer_host/pepper/pepper_message_filter.h"
9 #include "content/browser/tracing/trace_message_filter.h"
10 #include "content/common/pepper_renderer_instance_data.h"
11 #include "content/public/common/process_type.h"
12 #include "ipc/ipc_message_macros.h"
13 #include "ppapi/proxy/ppapi_messages.h"
15 namespace content {
17 // static
18 BrowserPpapiHost* BrowserPpapiHost::CreateExternalPluginProcess(
19 IPC::Sender* sender,
20 ppapi::PpapiPermissions permissions,
21 base::ProcessHandle plugin_child_process,
22 IPC::ChannelProxy* channel,
23 int render_process_id,
24 int render_view_id,
25 const base::FilePath& profile_directory) {
26 // The plugin name and path shouldn't be needed for external plugins.
27 BrowserPpapiHostImpl* browser_ppapi_host =
28 new BrowserPpapiHostImpl(sender,
29 permissions,
30 std::string(),
31 base::FilePath(),
32 profile_directory,
33 false /* in_process */,
34 true /* external_plugin */);
35 browser_ppapi_host->set_plugin_process(
36 base::Process::DeprecatedGetProcessFromHandle(plugin_child_process));
38 scoped_refptr<PepperMessageFilter> pepper_message_filter(
39 new PepperMessageFilter());
40 channel->AddFilter(pepper_message_filter->GetFilter());
41 channel->AddFilter(browser_ppapi_host->message_filter().get());
42 channel->AddFilter((new TraceMessageFilter())->GetFilter());
44 return browser_ppapi_host;
47 BrowserPpapiHostImpl::BrowserPpapiHostImpl(
48 IPC::Sender* sender,
49 const ppapi::PpapiPermissions& permissions,
50 const std::string& plugin_name,
51 const base::FilePath& plugin_path,
52 const base::FilePath& profile_data_directory,
53 bool in_process,
54 bool external_plugin)
55 : ppapi_host_(new ppapi::host::PpapiHost(sender, permissions)),
56 plugin_name_(plugin_name),
57 plugin_path_(plugin_path),
58 profile_data_directory_(profile_data_directory),
59 in_process_(in_process),
60 external_plugin_(external_plugin),
61 ssl_context_helper_(new SSLContextHelper()) {
62 message_filter_ = new HostMessageFilter(ppapi_host_.get(), this);
63 ppapi_host_->AddHostFactoryFilter(scoped_ptr<ppapi::host::HostFactory>(
64 new ContentBrowserPepperHostFactory(this)));
67 BrowserPpapiHostImpl::~BrowserPpapiHostImpl() {
68 // Notify the filter so it won't foward messages to us.
69 message_filter_->OnHostDestroyed();
71 // Delete the host explicitly first. This shutdown will destroy the
72 // resources, which may want to do cleanup in their destructors and expect
73 // their pointers to us to be valid.
74 ppapi_host_.reset();
77 ppapi::host::PpapiHost* BrowserPpapiHostImpl::GetPpapiHost() {
78 return ppapi_host_.get();
81 const base::Process& BrowserPpapiHostImpl::GetPluginProcess() const {
82 // Handle should previously have been set before use.
83 DCHECK(in_process_ || plugin_process_.IsValid());
84 return plugin_process_;
87 bool BrowserPpapiHostImpl::IsValidInstance(PP_Instance instance) const {
88 return instance_map_.contains(instance);
91 bool BrowserPpapiHostImpl::GetRenderFrameIDsForInstance(
92 PP_Instance instance,
93 int* render_process_id,
94 int* render_frame_id) const {
95 auto* data = instance_map_.get(instance);
96 if (data == nullptr) {
97 *render_process_id = 0;
98 *render_frame_id = 0;
99 return false;
102 *render_process_id = data->renderer_data.render_process_id;
103 *render_frame_id = data->renderer_data.render_frame_id;
104 return true;
107 const std::string& BrowserPpapiHostImpl::GetPluginName() {
108 return plugin_name_;
111 const base::FilePath& BrowserPpapiHostImpl::GetPluginPath() {
112 return plugin_path_;
115 const base::FilePath& BrowserPpapiHostImpl::GetProfileDataDirectory() {
116 return profile_data_directory_;
119 GURL BrowserPpapiHostImpl::GetDocumentURLForInstance(PP_Instance instance) {
120 auto* data = instance_map_.get(instance);
121 if (data == nullptr)
122 return GURL();
123 return data->renderer_data.document_url;
126 GURL BrowserPpapiHostImpl::GetPluginURLForInstance(PP_Instance instance) {
127 auto* data = instance_map_.get(instance);
128 if (data == nullptr)
129 return GURL();
130 return data->renderer_data.plugin_url;
133 void BrowserPpapiHostImpl::SetOnKeepaliveCallback(
134 const BrowserPpapiHost::OnKeepaliveCallback& callback) {
135 on_keepalive_callback_ = callback;
138 void BrowserPpapiHostImpl::AddInstance(
139 PP_Instance instance,
140 const PepperRendererInstanceData& renderer_instance_data) {
141 DCHECK(!instance_map_.contains(instance));
142 instance_map_.add(instance,
143 make_scoped_ptr(new InstanceData(renderer_instance_data)));
146 void BrowserPpapiHostImpl::DeleteInstance(PP_Instance instance) {
147 int erased = instance_map_.erase(instance);
148 DCHECK_EQ(1, erased);
151 void BrowserPpapiHostImpl::AddInstanceObserver(PP_Instance instance,
152 InstanceObserver* observer) {
153 instance_map_.get(instance)->observer_list.AddObserver(observer);
156 void BrowserPpapiHostImpl::RemoveInstanceObserver(PP_Instance instance,
157 InstanceObserver* observer) {
158 auto* data = instance_map_.get(instance);
159 if (data)
160 data->observer_list.RemoveObserver(observer);
163 void BrowserPpapiHostImpl::OnThrottleStateChanged(PP_Instance instance,
164 bool is_throttled) {
165 auto* data = instance_map_.get(instance);
166 if (data) {
167 data->is_throttled = is_throttled;
168 FOR_EACH_OBSERVER(InstanceObserver, data->observer_list,
169 OnThrottleStateChanged(is_throttled));
173 bool BrowserPpapiHostImpl::IsThrottled(PP_Instance instance) const {
174 auto* data = instance_map_.get(instance);
175 if (data)
176 return data->is_throttled;
178 return false;
181 BrowserPpapiHostImpl::HostMessageFilter::HostMessageFilter(
182 ppapi::host::PpapiHost* ppapi_host,
183 BrowserPpapiHostImpl* browser_ppapi_host_impl)
184 : ppapi_host_(ppapi_host),
185 browser_ppapi_host_impl_(browser_ppapi_host_impl) {}
187 bool BrowserPpapiHostImpl::HostMessageFilter::OnMessageReceived(
188 const IPC::Message& msg) {
189 // Don't forward messages if our owner object has been destroyed.
190 if (!ppapi_host_)
191 return false;
193 bool handled = true;
194 IPC_BEGIN_MESSAGE_MAP(BrowserPpapiHostImpl::HostMessageFilter, msg)
195 // Add necessary message handlers here.
196 IPC_MESSAGE_HANDLER(PpapiHostMsg_Keepalive, OnKeepalive)
197 IPC_MESSAGE_HANDLER(PpapiHostMsg_LogInterfaceUsage,
198 OnHostMsgLogInterfaceUsage)
199 IPC_MESSAGE_UNHANDLED(handled = ppapi_host_->OnMessageReceived(msg))
200 IPC_END_MESSAGE_MAP();
201 return handled;
204 void BrowserPpapiHostImpl::HostMessageFilter::OnHostDestroyed() {
205 DCHECK(ppapi_host_);
206 ppapi_host_ = NULL;
207 browser_ppapi_host_impl_ = NULL;
210 BrowserPpapiHostImpl::HostMessageFilter::~HostMessageFilter() {}
212 void BrowserPpapiHostImpl::HostMessageFilter::OnKeepalive() {
213 if (browser_ppapi_host_impl_)
214 browser_ppapi_host_impl_->OnKeepalive();
217 void BrowserPpapiHostImpl::HostMessageFilter::OnHostMsgLogInterfaceUsage(
218 int hash) const {
219 UMA_HISTOGRAM_SPARSE_SLOWLY("Pepper.InterfaceUsed", hash);
222 BrowserPpapiHostImpl::InstanceData::InstanceData(
223 const PepperRendererInstanceData& renderer_data)
224 : renderer_data(renderer_data), is_throttled(false) {
227 BrowserPpapiHostImpl::InstanceData::~InstanceData() {
230 void BrowserPpapiHostImpl::OnKeepalive() {
231 // An instance has been active. The on_keepalive_callback_ will be
232 // used to permit the content embedder to handle this, e.g. by tracking
233 // activity and shutting down processes that go idle.
235 // Currently embedders do not need to distinguish between instances having
236 // different idle state, and thus this implementation handles all instances
237 // for this module together.
239 if (on_keepalive_callback_.is_null())
240 return;
242 BrowserPpapiHost::OnKeepaliveInstanceData instance_data(instance_map_.size());
244 auto instance = instance_map_.begin();
245 int i = 0;
246 while (instance != instance_map_.end()) {
247 instance_data[i].render_process_id =
248 instance->second->renderer_data.render_process_id;
249 instance_data[i].render_frame_id =
250 instance->second->renderer_data.render_frame_id;
251 instance_data[i].document_url =
252 instance->second->renderer_data.document_url;
253 ++instance;
254 ++i;
256 on_keepalive_callback_.Run(instance_data, profile_data_directory_);
259 } // namespace content