Android Browser Compositor: Add ScheduleComposite() callback.
[chromium-blink-merge.git] / content / browser / ppapi_plugin_process_host.cc
blob4669da0cc61b4d25e69b404b58a008af01c233bb
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/ppapi_plugin_process_host.h"
7 #include <string>
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/file_path.h"
12 #include "base/process_util.h"
13 #include "base/utf_string_conversions.h"
14 #include "content/browser/browser_child_process_host_impl.h"
15 #include "content/browser/plugin_service_impl.h"
16 #include "content/browser/renderer_host/render_message_filter.h"
17 #include "content/common/child_process_host_impl.h"
18 #include "content/common/child_process_messages.h"
19 #include "content/public/browser/content_browser_client.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/pepper_plugin_info.h"
22 #include "content/public/common/process_type.h"
23 #include "ipc/ipc_switches.h"
24 #include "net/base/network_change_notifier.h"
25 #include "ppapi/proxy/ppapi_messages.h"
26 #include "ui/base/ui_base_switches.h"
27 #include "webkit/plugins/plugin_switches.h"
29 using content::ChildProcessHost;
30 using content::ChildProcessHostImpl;
32 class PpapiPluginProcessHost::PluginNetworkObserver
33 : public net::NetworkChangeNotifier::IPAddressObserver,
34 public net::NetworkChangeNotifier::ConnectionTypeObserver {
35 public:
36 explicit PluginNetworkObserver(PpapiPluginProcessHost* process_host)
37 : process_host_(process_host) {
38 net::NetworkChangeNotifier::AddIPAddressObserver(this);
39 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
42 ~PluginNetworkObserver() {
43 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
44 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
47 // IPAddressObserver implementation.
48 virtual void OnIPAddressChanged() OVERRIDE {
49 // TODO(brettw) bug 90246: This doesn't seem correct. The online/offline
50 // notification seems like it should be sufficient, but I don't see that
51 // when I unplug and replug my network cable. Sending this notification when
52 // "something" changes seems to make Flash reasonably happy, but seems
53 // wrong. We should really be able to provide the real online state in
54 // OnConnectionTypeChanged().
55 process_host_->Send(new PpapiMsg_SetNetworkState(true));
58 // ConnectionTypeObserver implementation.
59 virtual void OnConnectionTypeChanged(
60 net::NetworkChangeNotifier::ConnectionType type) {
61 process_host_->Send(new PpapiMsg_SetNetworkState(
62 type != net::NetworkChangeNotifier::CONNECTION_NONE));
65 private:
66 PpapiPluginProcessHost* const process_host_;
69 PpapiPluginProcessHost::~PpapiPluginProcessHost() {
70 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
71 << "~PpapiPluginProcessHost()";
72 CancelRequests();
75 // static
76 PpapiPluginProcessHost* PpapiPluginProcessHost::CreatePluginHost(
77 const content::PepperPluginInfo& info,
78 const FilePath& profile_data_directory,
79 net::HostResolver* host_resolver) {
80 PpapiPluginProcessHost* plugin_host = new PpapiPluginProcessHost(
81 info, profile_data_directory, host_resolver);
82 if (plugin_host->Init(info))
83 return plugin_host;
85 NOTREACHED(); // Init is not expected to fail.
86 return NULL;
89 // static
90 PpapiPluginProcessHost* PpapiPluginProcessHost::CreateBrokerHost(
91 const content::PepperPluginInfo& info) {
92 PpapiPluginProcessHost* plugin_host =
93 new PpapiPluginProcessHost();
94 if (plugin_host->Init(info))
95 return plugin_host;
97 NOTREACHED(); // Init is not expected to fail.
98 return NULL;
101 // static
102 void PpapiPluginProcessHost::DidCreateOutOfProcessInstance(
103 int plugin_process_id,
104 int32 pp_instance,
105 int render_process_id,
106 int render_view_id) {
107 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
108 if (iter->process_.get() &&
109 iter->process_->GetData().id == plugin_process_id) {
110 // Found the plugin.
111 iter->host_impl_->AddInstanceForView(pp_instance,
112 render_process_id, render_view_id);
113 return;
116 // We'll see this passed with a 0 process ID for the browser tag stuff that
117 // is currently in the process of being removed.
119 // TODO(brettw) When old browser tag impl is removed
120 // (PepperPluginDelegateImpl::CreateBrowserPluginModule passes a 0 plugin
121 // process ID) this should be converted to a NOTREACHED().
122 DCHECK(plugin_process_id == 0)
123 << "Renderer sent a bad plugin process host ID";
126 // static
127 void PpapiPluginProcessHost::DidDeleteOutOfProcessInstance(
128 int plugin_process_id,
129 int32 pp_instance) {
130 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
131 if (iter->process_.get() &&
132 iter->process_->GetData().id == plugin_process_id) {
133 // Found the plugin.
134 iter->host_impl_->DeleteInstanceForView(pp_instance);
135 return;
138 // Note: It's possible that the plugin process has already been deleted by
139 // the time this message is received. For example, it could have crashed.
140 // That's OK, we can just ignore this message.
143 bool PpapiPluginProcessHost::Send(IPC::Message* message) {
144 return process_->Send(message);
147 void PpapiPluginProcessHost::OpenChannelToPlugin(Client* client) {
148 if (process_->GetHost()->IsChannelOpening()) {
149 // The channel is already in the process of being opened. Put
150 // this "open channel" request into a queue of requests that will
151 // be run once the channel is open.
152 pending_requests_.push_back(client);
153 return;
156 // We already have an open channel, send a request right away to plugin.
157 RequestPluginChannel(client);
160 PpapiPluginProcessHost::PpapiPluginProcessHost(
161 const content::PepperPluginInfo& info,
162 const FilePath& profile_data_directory,
163 net::HostResolver* host_resolver)
164 : permissions_(
165 ppapi::PpapiPermissions::GetForCommandLine(info.permissions)),
166 network_observer_(new PluginNetworkObserver(this)),
167 profile_data_directory_(profile_data_directory),
168 is_broker_(false) {
169 process_.reset(new BrowserChildProcessHostImpl(
170 content::PROCESS_TYPE_PPAPI_PLUGIN, this));
172 filter_ = new PepperMessageFilter(PepperMessageFilter::PLUGIN,
173 host_resolver);
175 host_impl_ = new content::BrowserPpapiHostImpl(this, permissions_);
177 file_filter_ = new PepperTrustedFileMessageFilter(
178 process_->GetData().id, info.name, profile_data_directory);
180 process_->GetHost()->AddFilter(filter_.get());
181 process_->GetHost()->AddFilter(file_filter_.get());
182 process_->GetHost()->AddFilter(host_impl_.get());
184 content::GetContentClient()->browser()->DidCreatePpapiPlugin(host_impl_);
187 PpapiPluginProcessHost::PpapiPluginProcessHost()
188 : is_broker_(true) {
189 process_.reset(new BrowserChildProcessHostImpl(
190 content::PROCESS_TYPE_PPAPI_BROKER, this));
192 ppapi::PpapiPermissions permissions; // No permissions.
193 host_impl_ = new content::BrowserPpapiHostImpl(this, permissions);
196 bool PpapiPluginProcessHost::Init(const content::PepperPluginInfo& info) {
197 plugin_path_ = info.path;
198 if (info.name.empty()) {
199 process_->SetName(plugin_path_.BaseName().LossyDisplayName());
200 } else {
201 process_->SetName(UTF8ToUTF16(info.name));
204 std::string channel_id = process_->GetHost()->CreateChannel();
205 if (channel_id.empty())
206 return false;
208 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
209 CommandLine::StringType plugin_launcher =
210 browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher);
212 #if defined(OS_LINUX)
213 int flags = plugin_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
214 ChildProcessHost::CHILD_NORMAL;
215 #else
216 int flags = ChildProcessHost::CHILD_NORMAL;
217 #endif
218 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
219 if (exe_path.empty())
220 return false;
222 CommandLine* cmd_line = new CommandLine(exe_path);
223 cmd_line->AppendSwitchASCII(switches::kProcessType,
224 is_broker_ ? switches::kPpapiBrokerProcess
225 : switches::kPpapiPluginProcess);
226 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
228 // These switches are forwarded to both plugin and broker pocesses.
229 static const char* kCommonForwardSwitches[] = {
230 switches::kVModule
232 cmd_line->CopySwitchesFrom(browser_command_line, kCommonForwardSwitches,
233 arraysize(kCommonForwardSwitches));
235 if (!is_broker_) {
236 // TODO(vtl): Stop passing flash args in the command line, on windows is
237 // going to explode.
238 static const char* kPluginForwardSwitches[] = {
239 switches::kDisableSeccompFilterSandbox,
240 #if defined(OS_MACOSX)
241 switches::kEnableSandboxLogging,
242 #endif
243 switches::kNoSandbox,
244 switches::kPpapiFlashArgs,
245 switches::kPpapiStartupDialog,
247 cmd_line->CopySwitchesFrom(browser_command_line, kPluginForwardSwitches,
248 arraysize(kPluginForwardSwitches));
251 std::string locale =
252 content::GetContentClient()->browser()->GetApplicationLocale();
253 if (!locale.empty()) {
254 // Pass on the locale so the plugin will know what language we're using.
255 cmd_line->AppendSwitchASCII(switches::kLang, locale);
258 if (!plugin_launcher.empty())
259 cmd_line->PrependWrapper(plugin_launcher);
261 // On posix, never use the zygote for the broker. Also, only use the zygote if
262 // the plugin is sandboxed, and we are not using a plugin launcher - having a
263 // plugin launcher means we need to use another process instead of just
264 // forking the zygote.
265 #if defined(OS_POSIX)
266 bool use_zygote = !is_broker_ && plugin_launcher.empty() && info.is_sandboxed;
267 if (!info.is_sandboxed)
268 cmd_line->AppendSwitchASCII(switches::kNoSandbox, "");
269 #endif // OS_POSIX
270 process_->Launch(
271 #if defined(OS_WIN)
272 FilePath(),
273 #elif defined(OS_POSIX)
274 use_zygote,
275 base::EnvironmentVector(),
276 #endif
277 cmd_line);
278 return true;
281 void PpapiPluginProcessHost::RequestPluginChannel(Client* client) {
282 base::ProcessHandle process_handle;
283 int renderer_id;
284 client->GetPpapiChannelInfo(&process_handle, &renderer_id);
286 // We can't send any sync messages from the browser because it might lead to
287 // a hang. See the similar code in PluginProcessHost for more description.
288 PpapiMsg_CreateChannel* msg = new PpapiMsg_CreateChannel(
289 renderer_id, client->OffTheRecord());
290 msg->set_unblock(true);
291 if (Send(msg)) {
292 sent_requests_.push(client);
293 } else {
294 client->OnPpapiChannelOpened(IPC::ChannelHandle(), 0);
298 void PpapiPluginProcessHost::OnProcessLaunched() {
299 host_impl_->set_plugin_process_handle(process_->GetHandle());
302 void PpapiPluginProcessHost::OnProcessCrashed(int exit_code) {
303 PluginServiceImpl::GetInstance()->RegisterPluginCrash(plugin_path_);
306 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) {
307 bool handled = true;
308 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg)
309 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
310 OnRendererPluginChannelCreated)
311 IPC_MESSAGE_UNHANDLED(handled = false)
312 IPC_END_MESSAGE_MAP()
313 DCHECK(handled);
314 return handled;
317 // Called when the browser <--> plugin channel has been established.
318 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) {
319 // This will actually load the plugin. Errors will actually not be reported
320 // back at this point. Instead, the plugin will fail to establish the
321 // connections when we request them on behalf of the renderer(s).
322 Send(new PpapiMsg_LoadPlugin(plugin_path_, permissions_));
324 // Process all pending channel requests from the renderers.
325 for (size_t i = 0; i < pending_requests_.size(); i++)
326 RequestPluginChannel(pending_requests_[i]);
327 pending_requests_.clear();
330 // Called when the browser <--> plugin channel has an error. This normally
331 // means the plugin has crashed.
332 void PpapiPluginProcessHost::OnChannelError() {
333 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
334 << "::OnChannelError()";
335 // We don't need to notify the renderers that were communicating with the
336 // plugin since they have their own channels which will go into the error
337 // state at the same time. Instead, we just need to notify any renderers
338 // that have requested a connection but have not yet received one.
339 CancelRequests();
342 void PpapiPluginProcessHost::CancelRequests() {
343 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
344 << "CancelRequests()";
345 for (size_t i = 0; i < pending_requests_.size(); i++) {
346 pending_requests_[i]->OnPpapiChannelOpened(IPC::ChannelHandle(), 0);
348 pending_requests_.clear();
350 while (!sent_requests_.empty()) {
351 sent_requests_.front()->OnPpapiChannelOpened(IPC::ChannelHandle(), 0);
352 sent_requests_.pop();
356 // Called when a new plugin <--> renderer channel has been created.
357 void PpapiPluginProcessHost::OnRendererPluginChannelCreated(
358 const IPC::ChannelHandle& channel_handle) {
359 if (sent_requests_.empty())
360 return;
362 // All requests should be processed FIFO, so the next item in the
363 // sent_requests_ queue should be the one that the plugin just created.
364 Client* client = sent_requests_.front();
365 sent_requests_.pop();
367 client->OnPpapiChannelOpened(channel_handle, process_->GetData().id);