DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / ppapi_plugin_process_host.cc
blobb58c1d6e4d79f5264246e44ba34ecd54a0955c97
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/files/file_path.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/strings/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_constants.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/pepper_plugin_info.h"
23 #include "content/public/common/process_type.h"
24 #include "content/public/common/sandbox_type.h"
25 #include "content/public/common/sandboxed_process_launcher_delegate.h"
26 #include "ipc/ipc_switches.h"
27 #include "net/base/network_change_notifier.h"
28 #include "ppapi/proxy/ppapi_messages.h"
29 #include "ui/base/ui_base_switches.h"
31 #if defined(OS_WIN)
32 #include "content/common/sandbox_win.h"
33 #include "sandbox/win/src/sandbox_policy.h"
34 #endif
36 namespace content {
38 // NOTE: changes to this class need to be reviewed by the security team.
39 class PpapiPluginSandboxedProcessLauncherDelegate
40 : public content::SandboxedProcessLauncherDelegate {
41 public:
42 PpapiPluginSandboxedProcessLauncherDelegate(bool is_broker,
43 const PepperPluginInfo& info,
44 ChildProcessHost* host)
46 #if defined(OS_POSIX)
47 info_(info),
48 ipc_fd_(host->TakeClientFileDescriptor()),
49 #endif // OS_POSIX
50 is_broker_(is_broker) {}
52 ~PpapiPluginSandboxedProcessLauncherDelegate() override {}
54 #if defined(OS_WIN)
55 bool ShouldSandbox() override {
56 return !is_broker_;
59 void PreSpawnTarget(sandbox::TargetPolicy* policy, bool* success) override {
60 if (is_broker_)
61 return;
62 // The Pepper process as locked-down as a renderer execpt that it can
63 // create the server side of chrome pipes.
64 sandbox::ResultCode result;
65 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_NAMED_PIPES,
66 sandbox::TargetPolicy::NAMEDPIPES_ALLOW_ANY,
67 L"\\\\.\\pipe\\chrome.*");
68 *success = (result == sandbox::SBOX_ALL_OK);
71 #elif defined(OS_POSIX)
72 bool ShouldUseZygote() override {
73 const base::CommandLine& browser_command_line =
74 *base::CommandLine::ForCurrentProcess();
75 base::CommandLine::StringType plugin_launcher = browser_command_line
76 .GetSwitchValueNative(switches::kPpapiPluginLauncher);
77 return !is_broker_ && plugin_launcher.empty();
79 base::ScopedFD TakeIpcFd() override { return ipc_fd_.Pass(); }
80 #endif // OS_WIN
82 SandboxType GetSandboxType() override {
83 return SANDBOX_TYPE_PPAPI;
86 private:
87 #if defined(OS_POSIX)
88 const PepperPluginInfo& info_;
89 base::ScopedFD ipc_fd_;
90 #endif // OS_POSIX
91 bool is_broker_;
93 DISALLOW_COPY_AND_ASSIGN(PpapiPluginSandboxedProcessLauncherDelegate);
96 class PpapiPluginProcessHost::PluginNetworkObserver
97 : public net::NetworkChangeNotifier::IPAddressObserver,
98 public net::NetworkChangeNotifier::ConnectionTypeObserver {
99 public:
100 explicit PluginNetworkObserver(PpapiPluginProcessHost* process_host)
101 : process_host_(process_host) {
102 net::NetworkChangeNotifier::AddIPAddressObserver(this);
103 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
106 ~PluginNetworkObserver() override {
107 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
108 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
111 // IPAddressObserver implementation.
112 void OnIPAddressChanged() override {
113 // TODO(brettw) bug 90246: This doesn't seem correct. The online/offline
114 // notification seems like it should be sufficient, but I don't see that
115 // when I unplug and replug my network cable. Sending this notification when
116 // "something" changes seems to make Flash reasonably happy, but seems
117 // wrong. We should really be able to provide the real online state in
118 // OnConnectionTypeChanged().
119 process_host_->Send(new PpapiMsg_SetNetworkState(true));
122 // ConnectionTypeObserver implementation.
123 void OnConnectionTypeChanged(
124 net::NetworkChangeNotifier::ConnectionType type) override {
125 process_host_->Send(new PpapiMsg_SetNetworkState(
126 type != net::NetworkChangeNotifier::CONNECTION_NONE));
129 private:
130 PpapiPluginProcessHost* const process_host_;
133 PpapiPluginProcessHost::~PpapiPluginProcessHost() {
134 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
135 << "~PpapiPluginProcessHost()";
136 CancelRequests();
139 // static
140 PpapiPluginProcessHost* PpapiPluginProcessHost::CreatePluginHost(
141 const PepperPluginInfo& info,
142 const base::FilePath& profile_data_directory) {
143 PpapiPluginProcessHost* plugin_host = new PpapiPluginProcessHost(
144 info, profile_data_directory);
145 DCHECK(plugin_host);
146 if (plugin_host->Init(info))
147 return plugin_host;
149 NOTREACHED(); // Init is not expected to fail.
150 return NULL;
153 // static
154 PpapiPluginProcessHost* PpapiPluginProcessHost::CreateBrokerHost(
155 const PepperPluginInfo& info) {
156 PpapiPluginProcessHost* plugin_host =
157 new PpapiPluginProcessHost();
158 if (plugin_host->Init(info))
159 return plugin_host;
161 NOTREACHED(); // Init is not expected to fail.
162 return NULL;
165 // static
166 void PpapiPluginProcessHost::DidCreateOutOfProcessInstance(
167 int plugin_process_id,
168 int32 pp_instance,
169 const PepperRendererInstanceData& instance_data) {
170 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
171 if (iter->process_.get() &&
172 iter->process_->GetData().id == plugin_process_id) {
173 // Found the plugin.
174 iter->host_impl_->AddInstance(pp_instance, instance_data);
175 return;
178 // We'll see this passed with a 0 process ID for the browser tag stuff that
179 // is currently in the process of being removed.
181 // TODO(brettw) When old browser tag impl is removed
182 // (PepperPluginDelegateImpl::CreateBrowserPluginModule passes a 0 plugin
183 // process ID) this should be converted to a NOTREACHED().
184 DCHECK(plugin_process_id == 0)
185 << "Renderer sent a bad plugin process host ID";
188 // static
189 void PpapiPluginProcessHost::DidDeleteOutOfProcessInstance(
190 int plugin_process_id,
191 int32 pp_instance) {
192 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
193 if (iter->process_.get() &&
194 iter->process_->GetData().id == plugin_process_id) {
195 // Found the plugin.
196 iter->host_impl_->DeleteInstance(pp_instance);
197 return;
200 // Note: It's possible that the plugin process has already been deleted by
201 // the time this message is received. For example, it could have crashed.
202 // That's OK, we can just ignore this message.
205 // static
206 void PpapiPluginProcessHost::OnPluginInstanceThrottleStateChange(
207 int plugin_process_id,
208 int32 pp_instance,
209 bool is_throttled) {
210 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
211 if (iter->process_.get() &&
212 iter->process_->GetData().id == plugin_process_id) {
213 // Found the plugin.
214 iter->host_impl_->OnThrottleStateChanged(pp_instance, is_throttled);
215 return;
218 // Note: It's possible that the plugin process has already been deleted by
219 // the time this message is received. For example, it could have crashed.
220 // That's OK, we can just ignore this message.
223 // static
224 void PpapiPluginProcessHost::FindByName(
225 const base::string16& name,
226 std::vector<PpapiPluginProcessHost*>* hosts) {
227 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
228 if (iter->process_.get() && iter->process_->GetData().name == name)
229 hosts->push_back(*iter);
233 bool PpapiPluginProcessHost::Send(IPC::Message* message) {
234 return process_->Send(message);
237 void PpapiPluginProcessHost::OpenChannelToPlugin(Client* client) {
238 if (process_->GetHost()->IsChannelOpening()) {
239 // The channel is already in the process of being opened. Put
240 // this "open channel" request into a queue of requests that will
241 // be run once the channel is open.
242 pending_requests_.push_back(client);
243 return;
246 // We already have an open channel, send a request right away to plugin.
247 RequestPluginChannel(client);
250 PpapiPluginProcessHost::PpapiPluginProcessHost(
251 const PepperPluginInfo& info,
252 const base::FilePath& profile_data_directory)
253 : profile_data_directory_(profile_data_directory),
254 is_broker_(false) {
255 uint32 base_permissions = info.permissions;
257 // We don't have to do any whitelisting for APIs in this process host, so
258 // don't bother passing a browser context or document url here.
259 if (GetContentClient()->browser()->IsPluginAllowedToUseDevChannelAPIs(
260 NULL, GURL()))
261 base_permissions |= ppapi::PERMISSION_DEV_CHANNEL;
262 permissions_ = ppapi::PpapiPermissions::GetForCommandLine(base_permissions);
264 process_.reset(new BrowserChildProcessHostImpl(
265 PROCESS_TYPE_PPAPI_PLUGIN, this));
267 host_impl_.reset(new BrowserPpapiHostImpl(this, permissions_, info.name,
268 info.path, profile_data_directory,
269 false /* in_process */,
270 false /* external_plugin */));
272 filter_ = new PepperMessageFilter();
273 process_->AddFilter(filter_.get());
274 process_->GetHost()->AddFilter(host_impl_->message_filter().get());
276 GetContentClient()->browser()->DidCreatePpapiPlugin(host_impl_.get());
278 // Only request network status updates if the plugin has dev permissions.
279 if (permissions_.HasPermission(ppapi::PERMISSION_DEV))
280 network_observer_.reset(new PluginNetworkObserver(this));
283 PpapiPluginProcessHost::PpapiPluginProcessHost()
284 : is_broker_(true) {
285 process_.reset(new BrowserChildProcessHostImpl(
286 PROCESS_TYPE_PPAPI_BROKER, this));
288 ppapi::PpapiPermissions permissions; // No permissions.
289 // The plugin name, path and profile data directory shouldn't be needed for
290 // the broker.
291 host_impl_.reset(new BrowserPpapiHostImpl(this, permissions,
292 std::string(), base::FilePath(),
293 base::FilePath(),
294 false /* in_process */,
295 false /* external_plugin */));
298 bool PpapiPluginProcessHost::Init(const PepperPluginInfo& info) {
299 plugin_path_ = info.path;
300 if (info.name.empty()) {
301 process_->SetName(plugin_path_.BaseName().LossyDisplayName());
302 } else {
303 process_->SetName(base::UTF8ToUTF16(info.name));
306 std::string channel_id = process_->GetHost()->CreateChannel();
307 if (channel_id.empty()) {
308 VLOG(1) << "Could not create pepper host channel.";
309 return false;
312 const base::CommandLine& browser_command_line =
313 *base::CommandLine::ForCurrentProcess();
314 base::CommandLine::StringType plugin_launcher =
315 browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher);
317 #if defined(OS_LINUX)
318 int flags = plugin_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
319 ChildProcessHost::CHILD_NORMAL;
320 #else
321 int flags = ChildProcessHost::CHILD_NORMAL;
322 #endif
323 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
324 if (exe_path.empty()) {
325 VLOG(1) << "Pepper plugin exe path is empty.";
326 return false;
329 base::CommandLine* cmd_line = new base::CommandLine(exe_path);
330 cmd_line->AppendSwitchASCII(switches::kProcessType,
331 is_broker_ ? switches::kPpapiBrokerProcess
332 : switches::kPpapiPluginProcess);
333 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
335 // These switches are forwarded to both plugin and broker pocesses.
336 static const char* kCommonForwardSwitches[] = {
337 switches::kVModule
339 cmd_line->CopySwitchesFrom(browser_command_line, kCommonForwardSwitches,
340 arraysize(kCommonForwardSwitches));
342 if (!is_broker_) {
343 static const char* kPluginForwardSwitches[] = {
344 switches::kDisableSeccompFilterSandbox,
345 #if defined(OS_MACOSX)
346 switches::kEnableSandboxLogging,
347 #endif
348 switches::kNoSandbox,
349 switches::kPpapiStartupDialog,
351 cmd_line->CopySwitchesFrom(browser_command_line, kPluginForwardSwitches,
352 arraysize(kPluginForwardSwitches));
354 // Copy any flash args over and introduce field trials if necessary.
355 // TODO(vtl): Stop passing flash args in the command line, or windows is
356 // going to explode.
357 std::string field_trial =
358 base::FieldTrialList::FindFullName(kFlashHwVideoDecodeFieldTrialName);
359 std::string existing_args =
360 browser_command_line.GetSwitchValueASCII(switches::kPpapiFlashArgs);
361 if (field_trial == kFlashHwVideoDecodeFieldTrialEnabledName) {
362 // Arguments passed to Flash are comma delimited.
363 if (!existing_args.empty())
364 existing_args.append(",");
365 existing_args.append("enable_hw_video_decode=1");
366 #if defined(OS_MACOSX)
367 // TODO(ihf): Remove this once Flash newer than 15.0.0.223 is released.
368 existing_args.append(",enable_hw_video_decode_mac=1");
369 #endif
371 cmd_line->AppendSwitchASCII(switches::kPpapiFlashArgs, existing_args);
374 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
375 if (!locale.empty()) {
376 // Pass on the locale so the plugin will know what language we're using.
377 cmd_line->AppendSwitchASCII(switches::kLang, locale);
380 if (!plugin_launcher.empty())
381 cmd_line->PrependWrapper(plugin_launcher);
383 // On posix, never use the zygote for the broker. Also, only use the zygote if
384 // we are not using a plugin launcher - having a plugin launcher means we need
385 // to use another process instead of just forking the zygote.
386 process_->Launch(
387 new PpapiPluginSandboxedProcessLauncherDelegate(is_broker_,
388 info,
389 process_->GetHost()),
390 cmd_line,
391 true);
392 return true;
395 void PpapiPluginProcessHost::RequestPluginChannel(Client* client) {
396 base::ProcessHandle process_handle;
397 int renderer_child_id;
398 client->GetPpapiChannelInfo(&process_handle, &renderer_child_id);
400 base::ProcessId process_id = (process_handle == base::kNullProcessHandle) ?
401 0 : base::GetProcId(process_handle);
403 // We can't send any sync messages from the browser because it might lead to
404 // a hang. See the similar code in PluginProcessHost for more description.
405 PpapiMsg_CreateChannel* msg = new PpapiMsg_CreateChannel(
406 process_id, renderer_child_id, client->OffTheRecord());
407 msg->set_unblock(true);
408 if (Send(msg)) {
409 sent_requests_.push(client);
410 } else {
411 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
415 void PpapiPluginProcessHost::OnProcessLaunched() {
416 VLOG(2) << "ppapi plugin process launched.";
417 host_impl_->set_plugin_process(process_->GetProcess().Duplicate());
420 void PpapiPluginProcessHost::OnProcessCrashed(int exit_code) {
421 VLOG(1) << "ppapi plugin process crashed.";
422 PluginServiceImpl::GetInstance()->RegisterPluginCrash(plugin_path_);
425 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) {
426 bool handled = true;
427 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg)
428 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
429 OnRendererPluginChannelCreated)
430 IPC_MESSAGE_UNHANDLED(handled = false)
431 IPC_END_MESSAGE_MAP()
432 DCHECK(handled);
433 return handled;
436 // Called when the browser <--> plugin channel has been established.
437 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) {
438 // This will actually load the plugin. Errors will actually not be reported
439 // back at this point. Instead, the plugin will fail to establish the
440 // connections when we request them on behalf of the renderer(s).
441 Send(new PpapiMsg_LoadPlugin(plugin_path_, permissions_));
443 // Process all pending channel requests from the renderers.
444 for (size_t i = 0; i < pending_requests_.size(); i++)
445 RequestPluginChannel(pending_requests_[i]);
446 pending_requests_.clear();
449 // Called when the browser <--> plugin channel has an error. This normally
450 // means the plugin has crashed.
451 void PpapiPluginProcessHost::OnChannelError() {
452 VLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
453 << "::OnChannelError()";
454 // We don't need to notify the renderers that were communicating with the
455 // plugin since they have their own channels which will go into the error
456 // state at the same time. Instead, we just need to notify any renderers
457 // that have requested a connection but have not yet received one.
458 CancelRequests();
461 void PpapiPluginProcessHost::CancelRequests() {
462 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
463 << "CancelRequests()";
464 for (size_t i = 0; i < pending_requests_.size(); i++) {
465 pending_requests_[i]->OnPpapiChannelOpened(IPC::ChannelHandle(),
466 base::kNullProcessId, 0);
468 pending_requests_.clear();
470 while (!sent_requests_.empty()) {
471 sent_requests_.front()->OnPpapiChannelOpened(IPC::ChannelHandle(),
472 base::kNullProcessId, 0);
473 sent_requests_.pop();
477 // Called when a new plugin <--> renderer channel has been created.
478 void PpapiPluginProcessHost::OnRendererPluginChannelCreated(
479 const IPC::ChannelHandle& channel_handle) {
480 if (sent_requests_.empty())
481 return;
483 // All requests should be processed FIFO, so the next item in the
484 // sent_requests_ queue should be the one that the plugin just created.
485 Client* client = sent_requests_.front();
486 sent_requests_.pop();
488 const ChildProcessData& data = process_->GetData();
489 client->OnPpapiChannelOpened(channel_handle, base::GetProcId(data.handle),
490 data.id);
493 } // namespace content