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"
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/sandboxed_process_launcher_delegate.h"
25 #include "ipc/ipc_switches.h"
26 #include "net/base/network_change_notifier.h"
27 #include "ppapi/proxy/ppapi_messages.h"
28 #include "ui/base/ui_base_switches.h"
31 #include "content/common/sandbox_win.h"
32 #include "sandbox/win/src/sandbox_policy.h"
37 // NOTE: changes to this class need to be reviewed by the security team.
38 class PpapiPluginSandboxedProcessLauncherDelegate
39 : public content::SandboxedProcessLauncherDelegate
{
41 PpapiPluginSandboxedProcessLauncherDelegate(bool is_broker
,
42 const PepperPluginInfo
& info
,
43 ChildProcessHost
* host
)
47 ipc_fd_(host
->TakeClientFileDescriptor()),
49 is_broker_(is_broker
) {}
51 virtual ~PpapiPluginSandboxedProcessLauncherDelegate() {}
54 virtual bool ShouldSandbox() OVERRIDE
{
58 virtual void PreSpawnTarget(sandbox::TargetPolicy
* policy
,
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 virtual 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() && info_
.is_sandboxed
;
79 virtual int GetIpcFd() OVERRIDE
{
86 const PepperPluginInfo
& info_
;
91 DISALLOW_COPY_AND_ASSIGN(PpapiPluginSandboxedProcessLauncherDelegate
);
94 class PpapiPluginProcessHost::PluginNetworkObserver
95 : public net::NetworkChangeNotifier::IPAddressObserver
,
96 public net::NetworkChangeNotifier::ConnectionTypeObserver
{
98 explicit PluginNetworkObserver(PpapiPluginProcessHost
* process_host
)
99 : process_host_(process_host
) {
100 net::NetworkChangeNotifier::AddIPAddressObserver(this);
101 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
104 virtual ~PluginNetworkObserver() {
105 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
106 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
109 // IPAddressObserver implementation.
110 virtual void OnIPAddressChanged() OVERRIDE
{
111 // TODO(brettw) bug 90246: This doesn't seem correct. The online/offline
112 // notification seems like it should be sufficient, but I don't see that
113 // when I unplug and replug my network cable. Sending this notification when
114 // "something" changes seems to make Flash reasonably happy, but seems
115 // wrong. We should really be able to provide the real online state in
116 // OnConnectionTypeChanged().
117 process_host_
->Send(new PpapiMsg_SetNetworkState(true));
120 // ConnectionTypeObserver implementation.
121 virtual void OnConnectionTypeChanged(
122 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
{
123 process_host_
->Send(new PpapiMsg_SetNetworkState(
124 type
!= net::NetworkChangeNotifier::CONNECTION_NONE
));
128 PpapiPluginProcessHost
* const process_host_
;
131 PpapiPluginProcessHost::~PpapiPluginProcessHost() {
132 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_
? "[broker]" : "")
133 << "~PpapiPluginProcessHost()";
138 PpapiPluginProcessHost
* PpapiPluginProcessHost::CreatePluginHost(
139 const PepperPluginInfo
& info
,
140 const base::FilePath
& profile_data_directory
) {
141 PpapiPluginProcessHost
* plugin_host
= new PpapiPluginProcessHost(
142 info
, profile_data_directory
);
144 if (plugin_host
->Init(info
))
147 NOTREACHED(); // Init is not expected to fail.
152 PpapiPluginProcessHost
* PpapiPluginProcessHost::CreateBrokerHost(
153 const PepperPluginInfo
& info
) {
154 PpapiPluginProcessHost
* plugin_host
=
155 new PpapiPluginProcessHost();
156 if (plugin_host
->Init(info
))
159 NOTREACHED(); // Init is not expected to fail.
164 void PpapiPluginProcessHost::DidCreateOutOfProcessInstance(
165 int plugin_process_id
,
167 const PepperRendererInstanceData
& instance_data
) {
168 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
169 if (iter
->process_
.get() &&
170 iter
->process_
->GetData().id
== plugin_process_id
) {
172 iter
->host_impl_
->AddInstance(pp_instance
, instance_data
);
176 // We'll see this passed with a 0 process ID for the browser tag stuff that
177 // is currently in the process of being removed.
179 // TODO(brettw) When old browser tag impl is removed
180 // (PepperPluginDelegateImpl::CreateBrowserPluginModule passes a 0 plugin
181 // process ID) this should be converted to a NOTREACHED().
182 DCHECK(plugin_process_id
== 0)
183 << "Renderer sent a bad plugin process host ID";
187 void PpapiPluginProcessHost::DidDeleteOutOfProcessInstance(
188 int plugin_process_id
,
190 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
191 if (iter
->process_
.get() &&
192 iter
->process_
->GetData().id
== plugin_process_id
) {
194 iter
->host_impl_
->DeleteInstance(pp_instance
);
198 // Note: It's possible that the plugin process has already been deleted by
199 // the time this message is received. For example, it could have crashed.
200 // That's OK, we can just ignore this message.
204 void PpapiPluginProcessHost::FindByName(
205 const base::string16
& name
,
206 std::vector
<PpapiPluginProcessHost
*>* hosts
) {
207 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
208 if (iter
->process_
.get() && iter
->process_
->GetData().name
== name
)
209 hosts
->push_back(*iter
);
213 bool PpapiPluginProcessHost::Send(IPC::Message
* message
) {
214 return process_
->Send(message
);
217 void PpapiPluginProcessHost::OpenChannelToPlugin(Client
* client
) {
218 if (process_
->GetHost()->IsChannelOpening()) {
219 // The channel is already in the process of being opened. Put
220 // this "open channel" request into a queue of requests that will
221 // be run once the channel is open.
222 pending_requests_
.push_back(client
);
226 // We already have an open channel, send a request right away to plugin.
227 RequestPluginChannel(client
);
230 PpapiPluginProcessHost::PpapiPluginProcessHost(
231 const PepperPluginInfo
& info
,
232 const base::FilePath
& profile_data_directory
)
233 : profile_data_directory_(profile_data_directory
),
235 uint32 base_permissions
= info
.permissions
;
237 // We don't have to do any whitelisting for APIs in this process host, so
238 // don't bother passing a browser context or document url here.
239 if (GetContentClient()->browser()->IsPluginAllowedToUseDevChannelAPIs(
241 base_permissions
|= ppapi::PERMISSION_DEV_CHANNEL
;
242 permissions_
= ppapi::PpapiPermissions::GetForCommandLine(base_permissions
);
244 process_
.reset(new BrowserChildProcessHostImpl(
245 PROCESS_TYPE_PPAPI_PLUGIN
, this));
247 host_impl_
.reset(new BrowserPpapiHostImpl(this, permissions_
, info
.name
,
248 info
.path
, profile_data_directory
,
249 false /* in_process */,
250 false /* external_plugin */));
252 filter_
= new PepperMessageFilter();
253 process_
->AddFilter(filter_
.get());
254 process_
->GetHost()->AddFilter(host_impl_
->message_filter().get());
256 GetContentClient()->browser()->DidCreatePpapiPlugin(host_impl_
.get());
258 // Only request network status updates if the plugin has dev permissions.
259 if (permissions_
.HasPermission(ppapi::PERMISSION_DEV
))
260 network_observer_
.reset(new PluginNetworkObserver(this));
263 PpapiPluginProcessHost::PpapiPluginProcessHost()
265 process_
.reset(new BrowserChildProcessHostImpl(
266 PROCESS_TYPE_PPAPI_BROKER
, this));
268 ppapi::PpapiPermissions permissions
; // No permissions.
269 // The plugin name, path and profile data directory shouldn't be needed for
271 host_impl_
.reset(new BrowserPpapiHostImpl(this, permissions
,
272 std::string(), base::FilePath(),
274 false /* in_process */,
275 false /* external_plugin */));
278 bool PpapiPluginProcessHost::Init(const PepperPluginInfo
& info
) {
279 plugin_path_
= info
.path
;
280 if (info
.name
.empty()) {
281 process_
->SetName(plugin_path_
.BaseName().LossyDisplayName());
283 process_
->SetName(base::UTF8ToUTF16(info
.name
));
286 std::string channel_id
= process_
->GetHost()->CreateChannel();
287 if (channel_id
.empty()) {
288 VLOG(1) << "Could not create pepper host channel.";
292 const base::CommandLine
& browser_command_line
=
293 *base::CommandLine::ForCurrentProcess();
294 base::CommandLine::StringType plugin_launcher
=
295 browser_command_line
.GetSwitchValueNative(switches::kPpapiPluginLauncher
);
297 #if defined(OS_LINUX)
298 int flags
= plugin_launcher
.empty() ? ChildProcessHost::CHILD_ALLOW_SELF
:
299 ChildProcessHost::CHILD_NORMAL
;
301 int flags
= ChildProcessHost::CHILD_NORMAL
;
303 base::FilePath exe_path
= ChildProcessHost::GetChildPath(flags
);
304 if (exe_path
.empty()) {
305 VLOG(1) << "Pepper plugin exe path is empty.";
309 base::CommandLine
* cmd_line
= new base::CommandLine(exe_path
);
310 cmd_line
->AppendSwitchASCII(switches::kProcessType
,
311 is_broker_
? switches::kPpapiBrokerProcess
312 : switches::kPpapiPluginProcess
);
313 cmd_line
->AppendSwitchASCII(switches::kProcessChannelID
, channel_id
);
315 // These switches are forwarded to both plugin and broker pocesses.
316 static const char* kCommonForwardSwitches
[] = {
319 cmd_line
->CopySwitchesFrom(browser_command_line
, kCommonForwardSwitches
,
320 arraysize(kCommonForwardSwitches
));
323 static const char* kPluginForwardSwitches
[] = {
324 switches::kDisableSeccompFilterSandbox
,
325 #if defined(OS_MACOSX)
326 switches::kEnableSandboxLogging
,
328 switches::kNoSandbox
,
329 switches::kPpapiStartupDialog
,
331 cmd_line
->CopySwitchesFrom(browser_command_line
, kPluginForwardSwitches
,
332 arraysize(kPluginForwardSwitches
));
334 // Copy any flash args over and introduce field trials if necessary.
335 // TODO(vtl): Stop passing flash args in the command line, or windows is
337 std::string field_trial
=
338 base::FieldTrialList::FindFullName(kFlashHwVideoDecodeFieldTrialName
);
339 std::string existing_args
=
340 browser_command_line
.GetSwitchValueASCII(switches::kPpapiFlashArgs
);
341 if (field_trial
== kFlashHwVideoDecodeFieldTrialEnabledName
) {
342 // Arguments passed to Flash are comma delimited.
343 if (!existing_args
.empty())
344 existing_args
.append(",");
345 existing_args
.append("enable_hw_video_decode=1");
347 cmd_line
->AppendSwitchASCII(switches::kPpapiFlashArgs
, existing_args
);
350 std::string locale
= GetContentClient()->browser()->GetApplicationLocale();
351 if (!locale
.empty()) {
352 // Pass on the locale so the plugin will know what language we're using.
353 cmd_line
->AppendSwitchASCII(switches::kLang
, locale
);
356 if (!plugin_launcher
.empty())
357 cmd_line
->PrependWrapper(plugin_launcher
);
359 // On posix, never use the zygote for the broker. Also, only use the zygote if
360 // the plugin is sandboxed, and we are not using a plugin launcher - having a
361 // plugin launcher means we need to use another process instead of just
362 // forking the zygote.
363 #if defined(OS_POSIX)
364 if (!info
.is_sandboxed
)
365 cmd_line
->AppendSwitchASCII(switches::kNoSandbox
, std::string());
368 new PpapiPluginSandboxedProcessLauncherDelegate(is_broker_
,
370 process_
->GetHost()),
375 void PpapiPluginProcessHost::RequestPluginChannel(Client
* client
) {
376 base::ProcessHandle process_handle
;
377 int renderer_child_id
;
378 client
->GetPpapiChannelInfo(&process_handle
, &renderer_child_id
);
380 base::ProcessId process_id
= (process_handle
== base::kNullProcessHandle
) ?
381 0 : base::GetProcId(process_handle
);
383 // We can't send any sync messages from the browser because it might lead to
384 // a hang. See the similar code in PluginProcessHost for more description.
385 PpapiMsg_CreateChannel
* msg
= new PpapiMsg_CreateChannel(
386 process_id
, renderer_child_id
, client
->OffTheRecord());
387 msg
->set_unblock(true);
389 sent_requests_
.push(client
);
391 client
->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId
, 0);
395 void PpapiPluginProcessHost::OnProcessLaunched() {
396 VLOG(2) << "ppapi plugin process launched.";
397 host_impl_
->set_plugin_process_handle(process_
->GetHandle());
400 void PpapiPluginProcessHost::OnProcessCrashed(int exit_code
) {
401 VLOG(1) << "ppapi plugin process crashed.";
402 PluginServiceImpl::GetInstance()->RegisterPluginCrash(plugin_path_
);
405 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message
& msg
) {
407 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost
, msg
)
408 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated
,
409 OnRendererPluginChannelCreated
)
410 IPC_MESSAGE_UNHANDLED(handled
= false)
411 IPC_END_MESSAGE_MAP()
416 // Called when the browser <--> plugin channel has been established.
417 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid
) {
418 // This will actually load the plugin. Errors will actually not be reported
419 // back at this point. Instead, the plugin will fail to establish the
420 // connections when we request them on behalf of the renderer(s).
421 Send(new PpapiMsg_LoadPlugin(plugin_path_
, permissions_
));
423 // Process all pending channel requests from the renderers.
424 for (size_t i
= 0; i
< pending_requests_
.size(); i
++)
425 RequestPluginChannel(pending_requests_
[i
]);
426 pending_requests_
.clear();
429 // Called when the browser <--> plugin channel has an error. This normally
430 // means the plugin has crashed.
431 void PpapiPluginProcessHost::OnChannelError() {
432 VLOG(1) << "PpapiPluginProcessHost" << (is_broker_
? "[broker]" : "")
433 << "::OnChannelError()";
434 // We don't need to notify the renderers that were communicating with the
435 // plugin since they have their own channels which will go into the error
436 // state at the same time. Instead, we just need to notify any renderers
437 // that have requested a connection but have not yet received one.
441 void PpapiPluginProcessHost::CancelRequests() {
442 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_
? "[broker]" : "")
443 << "CancelRequests()";
444 for (size_t i
= 0; i
< pending_requests_
.size(); i
++) {
445 pending_requests_
[i
]->OnPpapiChannelOpened(IPC::ChannelHandle(),
446 base::kNullProcessId
, 0);
448 pending_requests_
.clear();
450 while (!sent_requests_
.empty()) {
451 sent_requests_
.front()->OnPpapiChannelOpened(IPC::ChannelHandle(),
452 base::kNullProcessId
, 0);
453 sent_requests_
.pop();
457 // Called when a new plugin <--> renderer channel has been created.
458 void PpapiPluginProcessHost::OnRendererPluginChannelCreated(
459 const IPC::ChannelHandle
& channel_handle
) {
460 if (sent_requests_
.empty())
463 // All requests should be processed FIFO, so the next item in the
464 // sent_requests_ queue should be the one that the plugin just created.
465 Client
* client
= sent_requests_
.front();
466 sent_requests_
.pop();
468 const ChildProcessData
& data
= process_
->GetData();
469 client
->OnPpapiChannelOpened(channel_handle
, base::GetProcId(data
.handle
),
473 } // namespace content