Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / browser / ppapi_plugin_process_host.cc
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.
4
5 #include "content/browser/ppapi_plugin_process_host.h"
6
7 #include <string>
8
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"
29
30 #if defined(OS_WIN)
31 #include "content/common/sandbox_win.h"
32 #include "sandbox/win/src/sandbox_policy.h"
33 #endif
34
35 namespace content {
36
37 // NOTE: changes to this class need to be reviewed by the security team.
38 class PpapiPluginSandboxedProcessLauncherDelegate
39     : public content::SandboxedProcessLauncherDelegate {
40  public:
41   PpapiPluginSandboxedProcessLauncherDelegate(bool is_broker,
42                                               const PepperPluginInfo& info,
43                                               ChildProcessHost* host)
44       :
45 #if defined(OS_POSIX)
46         info_(info),
47         ipc_fd_(host->TakeClientFileDescriptor()),
48 #endif  // OS_POSIX
49         is_broker_(is_broker) {}
50
51   virtual ~PpapiPluginSandboxedProcessLauncherDelegate() {}
52
53 #if defined(OS_WIN)
54   virtual bool ShouldSandbox() OVERRIDE {
55     return !is_broker_;
56   }
57
58   virtual void PreSpawnTarget(sandbox::TargetPolicy* policy,
59                               bool* success) {
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);
69   }
70
71 #elif defined(OS_POSIX)
72   virtual bool ShouldUseZygote() OVERRIDE {
73     const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
74     CommandLine::StringType plugin_launcher = browser_command_line
75         .GetSwitchValueNative(switches::kPpapiPluginLauncher);
76     return !is_broker_ && plugin_launcher.empty() && info_.is_sandboxed;
77   }
78   virtual int GetIpcFd() OVERRIDE {
79     return ipc_fd_;
80   }
81 #endif  // OS_WIN
82
83  private:
84 #if defined(OS_POSIX)
85   const PepperPluginInfo& info_;
86   int ipc_fd_;
87 #endif  // OS_POSIX
88   bool is_broker_;
89
90   DISALLOW_COPY_AND_ASSIGN(PpapiPluginSandboxedProcessLauncherDelegate);
91 };
92
93 class PpapiPluginProcessHost::PluginNetworkObserver
94     : public net::NetworkChangeNotifier::IPAddressObserver,
95       public net::NetworkChangeNotifier::ConnectionTypeObserver {
96  public:
97   explicit PluginNetworkObserver(PpapiPluginProcessHost* process_host)
98       : process_host_(process_host) {
99     net::NetworkChangeNotifier::AddIPAddressObserver(this);
100     net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
101   }
102
103   virtual ~PluginNetworkObserver() {
104     net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
105     net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
106   }
107
108   // IPAddressObserver implementation.
109   virtual void OnIPAddressChanged() OVERRIDE {
110     // TODO(brettw) bug 90246: This doesn't seem correct. The online/offline
111     // notification seems like it should be sufficient, but I don't see that
112     // when I unplug and replug my network cable. Sending this notification when
113     // "something" changes seems to make Flash reasonably happy, but seems
114     // wrong. We should really be able to provide the real online state in
115     // OnConnectionTypeChanged().
116     process_host_->Send(new PpapiMsg_SetNetworkState(true));
117   }
118
119   // ConnectionTypeObserver implementation.
120   virtual void OnConnectionTypeChanged(
121       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
122     process_host_->Send(new PpapiMsg_SetNetworkState(
123         type != net::NetworkChangeNotifier::CONNECTION_NONE));
124   }
125
126  private:
127   PpapiPluginProcessHost* const process_host_;
128 };
129
130 PpapiPluginProcessHost::~PpapiPluginProcessHost() {
131   DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
132            << "~PpapiPluginProcessHost()";
133   CancelRequests();
134 }
135
136 // static
137 PpapiPluginProcessHost* PpapiPluginProcessHost::CreatePluginHost(
138     const PepperPluginInfo& info,
139     const base::FilePath& profile_data_directory) {
140   PpapiPluginProcessHost* plugin_host = new PpapiPluginProcessHost(
141       info, profile_data_directory);
142   DCHECK(plugin_host);
143   if (plugin_host->Init(info))
144     return plugin_host;
145
146   NOTREACHED();  // Init is not expected to fail.
147   return NULL;
148 }
149
150 // static
151 PpapiPluginProcessHost* PpapiPluginProcessHost::CreateBrokerHost(
152     const PepperPluginInfo& info) {
153   PpapiPluginProcessHost* plugin_host =
154       new PpapiPluginProcessHost();
155   if (plugin_host->Init(info))
156     return plugin_host;
157
158   NOTREACHED();  // Init is not expected to fail.
159   return NULL;
160 }
161
162 // static
163 void PpapiPluginProcessHost::DidCreateOutOfProcessInstance(
164     int plugin_process_id,
165     int32 pp_instance,
166     const PepperRendererInstanceData& instance_data) {
167   for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
168     if (iter->process_.get() &&
169         iter->process_->GetData().id == plugin_process_id) {
170       // Found the plugin.
171       iter->host_impl_->AddInstance(pp_instance, instance_data);
172       return;
173     }
174   }
175   // We'll see this passed with a 0 process ID for the browser tag stuff that
176   // is currently in the process of being removed.
177   //
178   // TODO(brettw) When old browser tag impl is removed
179   // (PepperPluginDelegateImpl::CreateBrowserPluginModule passes a 0 plugin
180   // process ID) this should be converted to a NOTREACHED().
181   DCHECK(plugin_process_id == 0)
182       << "Renderer sent a bad plugin process host ID";
183 }
184
185 // static
186 void PpapiPluginProcessHost::DidDeleteOutOfProcessInstance(
187     int plugin_process_id,
188     int32 pp_instance) {
189   for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
190     if (iter->process_.get() &&
191         iter->process_->GetData().id == plugin_process_id) {
192       // Found the plugin.
193       iter->host_impl_->DeleteInstance(pp_instance);
194       return;
195     }
196   }
197   // Note: It's possible that the plugin process has already been deleted by
198   // the time this message is received. For example, it could have crashed.
199   // That's OK, we can just ignore this message.
200 }
201
202 // static
203 void PpapiPluginProcessHost::FindByName(
204     const base::string16& name,
205     std::vector<PpapiPluginProcessHost*>* hosts) {
206   for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
207     if (iter->process_.get() && iter->process_->GetData().name == name)
208       hosts->push_back(*iter);
209   }
210 }
211
212 bool PpapiPluginProcessHost::Send(IPC::Message* message) {
213   return process_->Send(message);
214 }
215
216 void PpapiPluginProcessHost::OpenChannelToPlugin(Client* client) {
217   if (process_->GetHost()->IsChannelOpening()) {
218     // The channel is already in the process of being opened.  Put
219     // this "open channel" request into a queue of requests that will
220     // be run once the channel is open.
221     pending_requests_.push_back(client);
222     return;
223   }
224
225   // We already have an open channel, send a request right away to plugin.
226   RequestPluginChannel(client);
227 }
228
229 PpapiPluginProcessHost::PpapiPluginProcessHost(
230     const PepperPluginInfo& info,
231     const base::FilePath& profile_data_directory)
232     : profile_data_directory_(profile_data_directory),
233       is_broker_(false) {
234   uint32 base_permissions = info.permissions;
235   if (GetContentClient()->browser()->IsPluginAllowedToUseDevChannelAPIs())
236     base_permissions |= ppapi::PERMISSION_DEV_CHANNEL;
237   permissions_ = ppapi::PpapiPermissions::GetForCommandLine(base_permissions);
238
239   process_.reset(new BrowserChildProcessHostImpl(
240       PROCESS_TYPE_PPAPI_PLUGIN, this));
241
242   host_impl_.reset(new BrowserPpapiHostImpl(this, permissions_, info.name,
243                                             info.path, profile_data_directory,
244                                             false /* in_process */,
245                                             false /* external_plugin */));
246
247   filter_ = new PepperMessageFilter();
248   process_->AddFilter(filter_.get());
249   process_->GetHost()->AddFilter(host_impl_->message_filter().get());
250
251   GetContentClient()->browser()->DidCreatePpapiPlugin(host_impl_.get());
252
253   // Only request network status updates if the plugin has dev permissions.
254   if (permissions_.HasPermission(ppapi::PERMISSION_DEV))
255     network_observer_.reset(new PluginNetworkObserver(this));
256 }
257
258 PpapiPluginProcessHost::PpapiPluginProcessHost()
259     : is_broker_(true) {
260   process_.reset(new BrowserChildProcessHostImpl(
261       PROCESS_TYPE_PPAPI_BROKER, this));
262
263   ppapi::PpapiPermissions permissions;  // No permissions.
264   // The plugin name, path and profile data directory shouldn't be needed for
265   // the broker.
266   host_impl_.reset(new BrowserPpapiHostImpl(this, permissions,
267                                             std::string(), base::FilePath(),
268                                             base::FilePath(),
269                                             false /* in_process */,
270                                             false /* external_plugin */));
271 }
272
273 bool PpapiPluginProcessHost::Init(const PepperPluginInfo& info) {
274   plugin_path_ = info.path;
275   if (info.name.empty()) {
276     process_->SetName(plugin_path_.BaseName().LossyDisplayName());
277   } else {
278     process_->SetName(base::UTF8ToUTF16(info.name));
279   }
280
281   std::string channel_id = process_->GetHost()->CreateChannel();
282   if (channel_id.empty()) {
283     VLOG(1) << "Could not create pepper host channel.";
284     return false;
285   }
286
287   const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
288   CommandLine::StringType plugin_launcher =
289       browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher);
290
291 #if defined(OS_LINUX)
292   int flags = plugin_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
293                                         ChildProcessHost::CHILD_NORMAL;
294 #else
295   int flags = ChildProcessHost::CHILD_NORMAL;
296 #endif
297   base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
298   if (exe_path.empty()) {
299     VLOG(1) << "Pepper plugin exe path is empty.";
300     return false;
301   }
302
303   CommandLine* cmd_line = new CommandLine(exe_path);
304   cmd_line->AppendSwitchASCII(switches::kProcessType,
305                               is_broker_ ? switches::kPpapiBrokerProcess
306                                          : switches::kPpapiPluginProcess);
307   cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
308
309   // These switches are forwarded to both plugin and broker pocesses.
310   static const char* kCommonForwardSwitches[] = {
311     switches::kVModule
312   };
313   cmd_line->CopySwitchesFrom(browser_command_line, kCommonForwardSwitches,
314                              arraysize(kCommonForwardSwitches));
315
316   if (!is_broker_) {
317     static const char* kPluginForwardSwitches[] = {
318       switches::kDisableSeccompFilterSandbox,
319 #if defined(OS_MACOSX)
320       switches::kEnableSandboxLogging,
321 #endif
322       switches::kNoSandbox,
323       switches::kPpapiStartupDialog,
324     };
325     cmd_line->CopySwitchesFrom(browser_command_line, kPluginForwardSwitches,
326                                arraysize(kPluginForwardSwitches));
327
328     // Copy any flash args over and introduce field trials if necessary.
329     // TODO(vtl): Stop passing flash args in the command line, or windows is
330     // going to explode.
331     std::string field_trial =
332         base::FieldTrialList::FindFullName(kFlashHwVideoDecodeFieldTrialName);
333     std::string existing_args =
334         browser_command_line.GetSwitchValueASCII(switches::kPpapiFlashArgs);
335     if (field_trial == kFlashHwVideoDecodeFieldTrialEnabledName) {
336       // Arguments passed to Flash are comma delimited.
337       if (!existing_args.empty())
338         existing_args.append(",");
339       existing_args.append("enable_hw_video_decode=1");
340     }
341     cmd_line->AppendSwitchASCII(switches::kPpapiFlashArgs, existing_args);
342   }
343
344   std::string locale = GetContentClient()->browser()->GetApplicationLocale();
345   if (!locale.empty()) {
346     // Pass on the locale so the plugin will know what language we're using.
347     cmd_line->AppendSwitchASCII(switches::kLang, locale);
348   }
349
350   if (!plugin_launcher.empty())
351     cmd_line->PrependWrapper(plugin_launcher);
352
353   // On posix, never use the zygote for the broker. Also, only use the zygote if
354   // the plugin is sandboxed, and we are not using a plugin launcher - having a
355   // plugin launcher means we need to use another process instead of just
356   // forking the zygote.
357 #if defined(OS_POSIX)
358   if (!info.is_sandboxed)
359     cmd_line->AppendSwitchASCII(switches::kNoSandbox, std::string());
360 #endif  // OS_POSIX
361   process_->Launch(
362       new PpapiPluginSandboxedProcessLauncherDelegate(is_broker_,
363                                                       info,
364                                                       process_->GetHost()),
365       cmd_line);
366   return true;
367 }
368
369 void PpapiPluginProcessHost::RequestPluginChannel(Client* client) {
370   base::ProcessHandle process_handle;
371   int renderer_child_id;
372   client->GetPpapiChannelInfo(&process_handle, &renderer_child_id);
373
374   base::ProcessId process_id = (process_handle == base::kNullProcessHandle) ?
375       0 : base::GetProcId(process_handle);
376
377   // We can't send any sync messages from the browser because it might lead to
378   // a hang. See the similar code in PluginProcessHost for more description.
379   PpapiMsg_CreateChannel* msg = new PpapiMsg_CreateChannel(
380       process_id, renderer_child_id, client->OffTheRecord());
381   msg->set_unblock(true);
382   if (Send(msg)) {
383     sent_requests_.push(client);
384   } else {
385     client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
386   }
387 }
388
389 void PpapiPluginProcessHost::OnProcessLaunched() {
390   VLOG(2) << "ppapi plugin process launched.";
391   host_impl_->set_plugin_process_handle(process_->GetHandle());
392 }
393
394 void PpapiPluginProcessHost::OnProcessCrashed(int exit_code) {
395   VLOG(1) << "ppapi plugin process crashed.";
396   PluginServiceImpl::GetInstance()->RegisterPluginCrash(plugin_path_);
397 }
398
399 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) {
400   bool handled = true;
401   IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg)
402     IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
403                         OnRendererPluginChannelCreated)
404     IPC_MESSAGE_UNHANDLED(handled = false)
405   IPC_END_MESSAGE_MAP()
406   DCHECK(handled);
407   return handled;
408 }
409
410 // Called when the browser <--> plugin channel has been established.
411 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) {
412   // This will actually load the plugin. Errors will actually not be reported
413   // back at this point. Instead, the plugin will fail to establish the
414   // connections when we request them on behalf of the renderer(s).
415   Send(new PpapiMsg_LoadPlugin(plugin_path_, permissions_));
416
417   // Process all pending channel requests from the renderers.
418   for (size_t i = 0; i < pending_requests_.size(); i++)
419     RequestPluginChannel(pending_requests_[i]);
420   pending_requests_.clear();
421 }
422
423 // Called when the browser <--> plugin channel has an error. This normally
424 // means the plugin has crashed.
425 void PpapiPluginProcessHost::OnChannelError() {
426   VLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
427           << "::OnChannelError()";
428   // We don't need to notify the renderers that were communicating with the
429   // plugin since they have their own channels which will go into the error
430   // state at the same time. Instead, we just need to notify any renderers
431   // that have requested a connection but have not yet received one.
432   CancelRequests();
433 }
434
435 void PpapiPluginProcessHost::CancelRequests() {
436   DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
437            << "CancelRequests()";
438   for (size_t i = 0; i < pending_requests_.size(); i++) {
439     pending_requests_[i]->OnPpapiChannelOpened(IPC::ChannelHandle(),
440                                                base::kNullProcessId, 0);
441   }
442   pending_requests_.clear();
443
444   while (!sent_requests_.empty()) {
445     sent_requests_.front()->OnPpapiChannelOpened(IPC::ChannelHandle(),
446                                                  base::kNullProcessId, 0);
447     sent_requests_.pop();
448   }
449 }
450
451 // Called when a new plugin <--> renderer channel has been created.
452 void PpapiPluginProcessHost::OnRendererPluginChannelCreated(
453     const IPC::ChannelHandle& channel_handle) {
454   if (sent_requests_.empty())
455     return;
456
457   // All requests should be processed FIFO, so the next item in the
458   // sent_requests_ queue should be the one that the plugin just created.
459   Client* client = sent_requests_.front();
460   sent_requests_.pop();
461
462   const ChildProcessData& data = process_->GetData();
463   client->OnPpapiChannelOpened(channel_handle, base::GetProcId(data.handle),
464                                data.id);
465 }
466
467 }  // namespace content