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 "ipc/ipc_switches.h"
25 #include "net/base/network_change_notifier.h"
26 #include "ppapi/proxy/ppapi_messages.h"
27 #include "ui/base/ui_base_switches.h"
30 #include "content/common/sandbox_win.h"
31 #include "content/public/common/sandboxed_process_launcher_delegate.h"
32 #include "sandbox/win/src/sandbox_policy.h"
38 // NOTE: changes to this class need to be reviewed by the security team.
39 class PpapiPluginSandboxedProcessLauncherDelegate
40 : public content::SandboxedProcessLauncherDelegate
{
42 explicit PpapiPluginSandboxedProcessLauncherDelegate(bool is_broker
)
43 : is_broker_(is_broker
) {}
44 virtual ~PpapiPluginSandboxedProcessLauncherDelegate() {}
46 virtual void ShouldSandbox(bool* in_sandbox
) OVERRIDE
{
51 virtual void PreSpawnTarget(sandbox::TargetPolicy
* policy
,
55 // The Pepper process as locked-down as a renderer execpt that it can
56 // create the server side of chrome pipes.
57 sandbox::ResultCode result
;
58 result
= policy
->AddRule(sandbox::TargetPolicy::SUBSYS_NAMED_PIPES
,
59 sandbox::TargetPolicy::NAMEDPIPES_ALLOW_ANY
,
60 L
"\\\\.\\pipe\\chrome.*");
61 *success
= (result
== sandbox::SBOX_ALL_OK
);
67 DISALLOW_COPY_AND_ASSIGN(PpapiPluginSandboxedProcessLauncherDelegate
);
71 class PpapiPluginProcessHost::PluginNetworkObserver
72 : public net::NetworkChangeNotifier::IPAddressObserver
,
73 public net::NetworkChangeNotifier::ConnectionTypeObserver
{
75 explicit PluginNetworkObserver(PpapiPluginProcessHost
* process_host
)
76 : process_host_(process_host
) {
77 net::NetworkChangeNotifier::AddIPAddressObserver(this);
78 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
81 virtual ~PluginNetworkObserver() {
82 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
83 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
86 // IPAddressObserver implementation.
87 virtual void OnIPAddressChanged() OVERRIDE
{
88 // TODO(brettw) bug 90246: This doesn't seem correct. The online/offline
89 // notification seems like it should be sufficient, but I don't see that
90 // when I unplug and replug my network cable. Sending this notification when
91 // "something" changes seems to make Flash reasonably happy, but seems
92 // wrong. We should really be able to provide the real online state in
93 // OnConnectionTypeChanged().
94 process_host_
->Send(new PpapiMsg_SetNetworkState(true));
97 // ConnectionTypeObserver implementation.
98 virtual void OnConnectionTypeChanged(
99 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
{
100 process_host_
->Send(new PpapiMsg_SetNetworkState(
101 type
!= net::NetworkChangeNotifier::CONNECTION_NONE
));
105 PpapiPluginProcessHost
* const process_host_
;
108 PpapiPluginProcessHost::~PpapiPluginProcessHost() {
109 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_
? "[broker]" : "")
110 << "~PpapiPluginProcessHost()";
115 PpapiPluginProcessHost
* PpapiPluginProcessHost::CreatePluginHost(
116 const PepperPluginInfo
& info
,
117 const base::FilePath
& profile_data_directory
) {
118 PpapiPluginProcessHost
* plugin_host
= new PpapiPluginProcessHost(
119 info
, profile_data_directory
);
121 if (plugin_host
->Init(info
))
124 NOTREACHED(); // Init is not expected to fail.
129 PpapiPluginProcessHost
* PpapiPluginProcessHost::CreateBrokerHost(
130 const PepperPluginInfo
& info
) {
131 PpapiPluginProcessHost
* plugin_host
=
132 new PpapiPluginProcessHost();
133 if (plugin_host
->Init(info
))
136 NOTREACHED(); // Init is not expected to fail.
141 void PpapiPluginProcessHost::DidCreateOutOfProcessInstance(
142 int plugin_process_id
,
144 const PepperRendererInstanceData
& instance_data
) {
145 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
146 if (iter
->process_
.get() &&
147 iter
->process_
->GetData().id
== plugin_process_id
) {
149 iter
->host_impl_
->AddInstance(pp_instance
, instance_data
);
153 // We'll see this passed with a 0 process ID for the browser tag stuff that
154 // is currently in the process of being removed.
156 // TODO(brettw) When old browser tag impl is removed
157 // (PepperPluginDelegateImpl::CreateBrowserPluginModule passes a 0 plugin
158 // process ID) this should be converted to a NOTREACHED().
159 DCHECK(plugin_process_id
== 0)
160 << "Renderer sent a bad plugin process host ID";
164 void PpapiPluginProcessHost::DidDeleteOutOfProcessInstance(
165 int plugin_process_id
,
167 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
168 if (iter
->process_
.get() &&
169 iter
->process_
->GetData().id
== plugin_process_id
) {
171 iter
->host_impl_
->DeleteInstance(pp_instance
);
175 // Note: It's possible that the plugin process has already been deleted by
176 // the time this message is received. For example, it could have crashed.
177 // That's OK, we can just ignore this message.
181 void PpapiPluginProcessHost::FindByName(
182 const base::string16
& name
,
183 std::vector
<PpapiPluginProcessHost
*>* hosts
) {
184 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
185 if (iter
->process_
.get() && iter
->process_
->GetData().name
== name
)
186 hosts
->push_back(*iter
);
190 bool PpapiPluginProcessHost::Send(IPC::Message
* message
) {
191 return process_
->Send(message
);
194 void PpapiPluginProcessHost::OpenChannelToPlugin(Client
* client
) {
195 if (process_
->GetHost()->IsChannelOpening()) {
196 // The channel is already in the process of being opened. Put
197 // this "open channel" request into a queue of requests that will
198 // be run once the channel is open.
199 pending_requests_
.push_back(client
);
203 // We already have an open channel, send a request right away to plugin.
204 RequestPluginChannel(client
);
207 PpapiPluginProcessHost::PpapiPluginProcessHost(
208 const PepperPluginInfo
& info
,
209 const base::FilePath
& profile_data_directory
)
210 : profile_data_directory_(profile_data_directory
),
212 uint32 base_permissions
= info
.permissions
;
213 if (GetContentClient()->browser()->IsPluginAllowedToUseDevChannelAPIs())
214 base_permissions
|= ppapi::PERMISSION_DEV_CHANNEL
;
215 permissions_
= ppapi::PpapiPermissions::GetForCommandLine(base_permissions
);
217 process_
.reset(new BrowserChildProcessHostImpl(
218 PROCESS_TYPE_PPAPI_PLUGIN
, this));
220 host_impl_
.reset(new BrowserPpapiHostImpl(this, permissions_
, info
.name
,
221 info
.path
, profile_data_directory
,
222 false /* in_process */,
223 false /* external_plugin */));
225 filter_
= new PepperMessageFilter();
226 process_
->AddFilter(filter_
.get());
227 process_
->GetHost()->AddFilter(host_impl_
->message_filter().get());
229 GetContentClient()->browser()->DidCreatePpapiPlugin(host_impl_
.get());
231 // Only request network status updates if the plugin has dev permissions.
232 if (permissions_
.HasPermission(ppapi::PERMISSION_DEV
))
233 network_observer_
.reset(new PluginNetworkObserver(this));
236 PpapiPluginProcessHost::PpapiPluginProcessHost()
238 process_
.reset(new BrowserChildProcessHostImpl(
239 PROCESS_TYPE_PPAPI_BROKER
, this));
241 ppapi::PpapiPermissions permissions
; // No permissions.
242 // The plugin name, path and profile data directory shouldn't be needed for
244 host_impl_
.reset(new BrowserPpapiHostImpl(this, permissions
,
245 std::string(), base::FilePath(),
247 false /* in_process */,
248 false /* external_plugin */));
251 bool PpapiPluginProcessHost::Init(const PepperPluginInfo
& info
) {
252 plugin_path_
= info
.path
;
253 if (info
.name
.empty()) {
254 process_
->SetName(plugin_path_
.BaseName().LossyDisplayName());
256 process_
->SetName(base::UTF8ToUTF16(info
.name
));
259 std::string channel_id
= process_
->GetHost()->CreateChannel();
260 if (channel_id
.empty()) {
261 VLOG(1) << "Could not create pepper host channel.";
265 const CommandLine
& browser_command_line
= *CommandLine::ForCurrentProcess();
266 CommandLine::StringType plugin_launcher
=
267 browser_command_line
.GetSwitchValueNative(switches::kPpapiPluginLauncher
);
269 #if defined(OS_LINUX)
270 int flags
= plugin_launcher
.empty() ? ChildProcessHost::CHILD_ALLOW_SELF
:
271 ChildProcessHost::CHILD_NORMAL
;
273 int flags
= ChildProcessHost::CHILD_NORMAL
;
275 base::FilePath exe_path
= ChildProcessHost::GetChildPath(flags
);
276 if (exe_path
.empty()) {
277 VLOG(1) << "Pepper plugin exe path is empty.";
281 CommandLine
* cmd_line
= new CommandLine(exe_path
);
282 cmd_line
->AppendSwitchASCII(switches::kProcessType
,
283 is_broker_
? switches::kPpapiBrokerProcess
284 : switches::kPpapiPluginProcess
);
285 cmd_line
->AppendSwitchASCII(switches::kProcessChannelID
, channel_id
);
287 // These switches are forwarded to both plugin and broker pocesses.
288 static const char* kCommonForwardSwitches
[] = {
291 cmd_line
->CopySwitchesFrom(browser_command_line
, kCommonForwardSwitches
,
292 arraysize(kCommonForwardSwitches
));
295 static const char* kPluginForwardSwitches
[] = {
296 switches::kDisableSeccompFilterSandbox
,
297 #if defined(OS_MACOSX)
298 switches::kEnableSandboxLogging
,
300 switches::kNoSandbox
,
301 switches::kPpapiStartupDialog
,
303 cmd_line
->CopySwitchesFrom(browser_command_line
, kPluginForwardSwitches
,
304 arraysize(kPluginForwardSwitches
));
306 // Copy any flash args over and introduce field trials if necessary.
307 // TODO(vtl): Stop passing flash args in the command line, or windows is
309 std::string field_trial
=
310 base::FieldTrialList::FindFullName(kLowLatencyFlashAudioFieldTrialName
);
311 std::string existing_args
=
312 browser_command_line
.GetSwitchValueASCII(switches::kPpapiFlashArgs
);
313 if (field_trial
== kLowLatencyFlashAudioFieldTrialEnabledName
)
314 existing_args
.append(" enable_low_latency_audio=1");
315 cmd_line
->AppendSwitchASCII(switches::kPpapiFlashArgs
, existing_args
);
318 std::string locale
= GetContentClient()->browser()->GetApplicationLocale();
319 if (!locale
.empty()) {
320 // Pass on the locale so the plugin will know what language we're using.
321 cmd_line
->AppendSwitchASCII(switches::kLang
, locale
);
324 if (!plugin_launcher
.empty())
325 cmd_line
->PrependWrapper(plugin_launcher
);
327 // On posix, never use the zygote for the broker. Also, only use the zygote if
328 // the plugin is sandboxed, and we are not using a plugin launcher - having a
329 // plugin launcher means we need to use another process instead of just
330 // forking the zygote.
331 #if defined(OS_POSIX)
332 bool use_zygote
= !is_broker_
&& plugin_launcher
.empty() && info
.is_sandboxed
;
333 if (!info
.is_sandboxed
)
334 cmd_line
->AppendSwitchASCII(switches::kNoSandbox
, std::string());
338 new PpapiPluginSandboxedProcessLauncherDelegate(is_broker_
),
339 #elif defined(OS_POSIX)
341 base::EnvironmentMap(),
347 void PpapiPluginProcessHost::RequestPluginChannel(Client
* client
) {
348 base::ProcessHandle process_handle
;
349 int renderer_child_id
;
350 client
->GetPpapiChannelInfo(&process_handle
, &renderer_child_id
);
352 base::ProcessId process_id
= (process_handle
== base::kNullProcessHandle
) ?
353 0 : base::GetProcId(process_handle
);
355 // We can't send any sync messages from the browser because it might lead to
356 // a hang. See the similar code in PluginProcessHost for more description.
357 PpapiMsg_CreateChannel
* msg
= new PpapiMsg_CreateChannel(
358 process_id
, renderer_child_id
, client
->OffTheRecord());
359 msg
->set_unblock(true);
361 sent_requests_
.push(client
);
363 client
->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId
, 0);
367 void PpapiPluginProcessHost::OnProcessLaunched() {
368 VLOG(2) << "ppapi plugin process launched.";
369 host_impl_
->set_plugin_process_handle(process_
->GetHandle());
372 void PpapiPluginProcessHost::OnProcessCrashed(int exit_code
) {
373 VLOG(1) << "ppapi plugin process crashed.";
374 PluginServiceImpl::GetInstance()->RegisterPluginCrash(plugin_path_
);
377 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message
& msg
) {
379 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost
, msg
)
380 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated
,
381 OnRendererPluginChannelCreated
)
382 IPC_MESSAGE_UNHANDLED(handled
= false)
383 IPC_END_MESSAGE_MAP()
388 // Called when the browser <--> plugin channel has been established.
389 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid
) {
390 // This will actually load the plugin. Errors will actually not be reported
391 // back at this point. Instead, the plugin will fail to establish the
392 // connections when we request them on behalf of the renderer(s).
393 Send(new PpapiMsg_LoadPlugin(plugin_path_
, permissions_
));
395 // Process all pending channel requests from the renderers.
396 for (size_t i
= 0; i
< pending_requests_
.size(); i
++)
397 RequestPluginChannel(pending_requests_
[i
]);
398 pending_requests_
.clear();
401 // Called when the browser <--> plugin channel has an error. This normally
402 // means the plugin has crashed.
403 void PpapiPluginProcessHost::OnChannelError() {
404 VLOG(1) << "PpapiPluginProcessHost" << (is_broker_
? "[broker]" : "")
405 << "::OnChannelError()";
406 // We don't need to notify the renderers that were communicating with the
407 // plugin since they have their own channels which will go into the error
408 // state at the same time. Instead, we just need to notify any renderers
409 // that have requested a connection but have not yet received one.
413 void PpapiPluginProcessHost::CancelRequests() {
414 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_
? "[broker]" : "")
415 << "CancelRequests()";
416 for (size_t i
= 0; i
< pending_requests_
.size(); i
++) {
417 pending_requests_
[i
]->OnPpapiChannelOpened(IPC::ChannelHandle(),
418 base::kNullProcessId
, 0);
420 pending_requests_
.clear();
422 while (!sent_requests_
.empty()) {
423 sent_requests_
.front()->OnPpapiChannelOpened(IPC::ChannelHandle(),
424 base::kNullProcessId
, 0);
425 sent_requests_
.pop();
429 // Called when a new plugin <--> renderer channel has been created.
430 void PpapiPluginProcessHost::OnRendererPluginChannelCreated(
431 const IPC::ChannelHandle
& channel_handle
) {
432 if (sent_requests_
.empty())
435 // All requests should be processed FIFO, so the next item in the
436 // sent_requests_ queue should be the one that the plugin just created.
437 Client
* client
= sent_requests_
.front();
438 sent_requests_
.pop();
440 const ChildProcessData
& data
= process_
->GetData();
441 client
->OnPpapiChannelOpened(channel_handle
, base::GetProcId(data
.handle
),
445 } // namespace content