[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / ppapi_plugin_process_host.cc
blob10aef1569598e391c507798896fc22e270a23cc2
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 namespace content {
31 class PpapiPluginProcessHost::PluginNetworkObserver
32 : public net::NetworkChangeNotifier::IPAddressObserver,
33 public net::NetworkChangeNotifier::ConnectionTypeObserver {
34 public:
35 explicit PluginNetworkObserver(PpapiPluginProcessHost* process_host)
36 : process_host_(process_host) {
37 net::NetworkChangeNotifier::AddIPAddressObserver(this);
38 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
41 ~PluginNetworkObserver() {
42 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
43 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
46 // IPAddressObserver implementation.
47 virtual void OnIPAddressChanged() OVERRIDE {
48 // TODO(brettw) bug 90246: This doesn't seem correct. The online/offline
49 // notification seems like it should be sufficient, but I don't see that
50 // when I unplug and replug my network cable. Sending this notification when
51 // "something" changes seems to make Flash reasonably happy, but seems
52 // wrong. We should really be able to provide the real online state in
53 // OnConnectionTypeChanged().
54 process_host_->Send(new PpapiMsg_SetNetworkState(true));
57 // ConnectionTypeObserver implementation.
58 virtual void OnConnectionTypeChanged(
59 net::NetworkChangeNotifier::ConnectionType type) {
60 process_host_->Send(new PpapiMsg_SetNetworkState(
61 type != net::NetworkChangeNotifier::CONNECTION_NONE));
64 private:
65 PpapiPluginProcessHost* const process_host_;
68 PpapiPluginProcessHost::~PpapiPluginProcessHost() {
69 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
70 << "~PpapiPluginProcessHost()";
71 CancelRequests();
74 // static
75 PpapiPluginProcessHost* PpapiPluginProcessHost::CreatePluginHost(
76 const PepperPluginInfo& info,
77 const FilePath& profile_data_directory,
78 net::HostResolver* host_resolver) {
79 PpapiPluginProcessHost* plugin_host = new PpapiPluginProcessHost(
80 info, profile_data_directory, host_resolver);
81 if (plugin_host->Init(info))
82 return plugin_host;
84 NOTREACHED(); // Init is not expected to fail.
85 return NULL;
88 // static
89 PpapiPluginProcessHost* PpapiPluginProcessHost::CreateBrokerHost(
90 const PepperPluginInfo& info) {
91 PpapiPluginProcessHost* plugin_host =
92 new PpapiPluginProcessHost();
93 if (plugin_host->Init(info))
94 return plugin_host;
96 NOTREACHED(); // Init is not expected to fail.
97 return NULL;
100 // static
101 void PpapiPluginProcessHost::DidCreateOutOfProcessInstance(
102 int plugin_process_id,
103 int32 pp_instance,
104 const PepperRendererInstanceData& instance_data) {
105 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
106 if (iter->process_.get() &&
107 iter->process_->GetData().id == plugin_process_id) {
108 // Found the plugin.
109 iter->host_impl_->AddInstance(pp_instance, instance_data);
110 return;
113 // We'll see this passed with a 0 process ID for the browser tag stuff that
114 // is currently in the process of being removed.
116 // TODO(brettw) When old browser tag impl is removed
117 // (PepperPluginDelegateImpl::CreateBrowserPluginModule passes a 0 plugin
118 // process ID) this should be converted to a NOTREACHED().
119 DCHECK(plugin_process_id == 0)
120 << "Renderer sent a bad plugin process host ID";
123 // static
124 void PpapiPluginProcessHost::DidDeleteOutOfProcessInstance(
125 int plugin_process_id,
126 int32 pp_instance) {
127 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
128 if (iter->process_.get() &&
129 iter->process_->GetData().id == plugin_process_id) {
130 // Found the plugin.
131 iter->host_impl_->DeleteInstance(pp_instance);
132 return;
135 // Note: It's possible that the plugin process has already been deleted by
136 // the time this message is received. For example, it could have crashed.
137 // That's OK, we can just ignore this message.
140 bool PpapiPluginProcessHost::Send(IPC::Message* message) {
141 return process_->Send(message);
144 void PpapiPluginProcessHost::OpenChannelToPlugin(Client* client) {
145 if (process_->GetHost()->IsChannelOpening()) {
146 // The channel is already in the process of being opened. Put
147 // this "open channel" request into a queue of requests that will
148 // be run once the channel is open.
149 pending_requests_.push_back(client);
150 return;
153 // We already have an open channel, send a request right away to plugin.
154 RequestPluginChannel(client);
157 PpapiPluginProcessHost::PpapiPluginProcessHost(
158 const PepperPluginInfo& info,
159 const FilePath& profile_data_directory,
160 net::HostResolver* host_resolver)
161 : permissions_(
162 ppapi::PpapiPermissions::GetForCommandLine(info.permissions)),
163 profile_data_directory_(profile_data_directory),
164 is_broker_(false) {
165 process_.reset(new BrowserChildProcessHostImpl(
166 PROCESS_TYPE_PPAPI_PLUGIN, this));
168 filter_ = new PepperMessageFilter(PepperMessageFilter::PLUGIN,
169 permissions_,
170 host_resolver);
172 host_impl_.reset(new BrowserPpapiHostImpl(this, permissions_, info.name,
173 profile_data_directory));
175 process_->GetHost()->AddFilter(filter_.get());
176 process_->GetHost()->AddFilter(host_impl_->message_filter());
178 GetContentClient()->browser()->DidCreatePpapiPlugin(host_impl_.get());
180 // Only request network status updates if the plugin has dev permissions.
181 if (permissions_.HasPermission(ppapi::PERMISSION_DEV))
182 network_observer_.reset(new PluginNetworkObserver(this));
185 PpapiPluginProcessHost::PpapiPluginProcessHost()
186 : is_broker_(true) {
187 process_.reset(new BrowserChildProcessHostImpl(
188 PROCESS_TYPE_PPAPI_BROKER, this));
190 ppapi::PpapiPermissions permissions; // No permissions.
191 // The plugin name and profile data directory shouldn't be needed for the
192 // broker.
193 std::string plugin_name;
194 FilePath profile_data_directory;
195 host_impl_.reset(new BrowserPpapiHostImpl(this, permissions, plugin_name,
196 profile_data_directory));
199 bool PpapiPluginProcessHost::Init(const PepperPluginInfo& info) {
200 plugin_path_ = info.path;
201 if (info.name.empty()) {
202 process_->SetName(plugin_path_.BaseName().LossyDisplayName());
203 } else {
204 process_->SetName(UTF8ToUTF16(info.name));
207 std::string channel_id = process_->GetHost()->CreateChannel();
208 if (channel_id.empty())
209 return false;
211 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
212 CommandLine::StringType plugin_launcher =
213 browser_command_line.GetSwitchValueNative(switches::kPpapiPluginLauncher);
215 #if defined(OS_LINUX)
216 int flags = plugin_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
217 ChildProcessHost::CHILD_NORMAL;
218 #else
219 int flags = ChildProcessHost::CHILD_NORMAL;
220 #endif
221 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
222 if (exe_path.empty())
223 return false;
225 CommandLine* cmd_line = new CommandLine(exe_path);
226 cmd_line->AppendSwitchASCII(switches::kProcessType,
227 is_broker_ ? switches::kPpapiBrokerProcess
228 : switches::kPpapiPluginProcess);
229 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
231 // These switches are forwarded to both plugin and broker pocesses.
232 static const char* kCommonForwardSwitches[] = {
233 switches::kVModule
235 cmd_line->CopySwitchesFrom(browser_command_line, kCommonForwardSwitches,
236 arraysize(kCommonForwardSwitches));
238 if (!is_broker_) {
239 // TODO(vtl): Stop passing flash args in the command line, on windows is
240 // going to explode.
241 static const char* kPluginForwardSwitches[] = {
242 switches::kDisablePepperThreading,
243 switches::kDisableSeccompFilterSandbox,
244 switches::kEnablePepperThreading,
245 #if defined(OS_MACOSX)
246 switches::kEnableSandboxLogging,
247 #endif
248 switches::kNoSandbox,
249 switches::kPpapiFlashArgs,
250 switches::kPpapiStartupDialog,
252 cmd_line->CopySwitchesFrom(browser_command_line, kPluginForwardSwitches,
253 arraysize(kPluginForwardSwitches));
256 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
257 if (!locale.empty()) {
258 // Pass on the locale so the plugin will know what language we're using.
259 cmd_line->AppendSwitchASCII(switches::kLang, locale);
262 if (!plugin_launcher.empty())
263 cmd_line->PrependWrapper(plugin_launcher);
265 // On posix, never use the zygote for the broker. Also, only use the zygote if
266 // the plugin is sandboxed, and we are not using a plugin launcher - having a
267 // plugin launcher means we need to use another process instead of just
268 // forking the zygote.
269 #if defined(OS_POSIX)
270 bool use_zygote = !is_broker_ && plugin_launcher.empty() && info.is_sandboxed;
271 if (!info.is_sandboxed)
272 cmd_line->AppendSwitchASCII(switches::kNoSandbox, "");
273 #endif // OS_POSIX
274 process_->Launch(
275 #if defined(OS_WIN)
276 FilePath(),
277 #elif defined(OS_POSIX)
278 use_zygote,
279 base::EnvironmentVector(),
280 #endif
281 cmd_line);
282 return true;
285 void PpapiPluginProcessHost::RequestPluginChannel(Client* client) {
286 base::ProcessHandle process_handle;
287 int renderer_id;
288 client->GetPpapiChannelInfo(&process_handle, &renderer_id);
290 // We can't send any sync messages from the browser because it might lead to
291 // a hang. See the similar code in PluginProcessHost for more description.
292 PpapiMsg_CreateChannel* msg = new PpapiMsg_CreateChannel(
293 renderer_id, client->OffTheRecord());
294 msg->set_unblock(true);
295 if (Send(msg)) {
296 sent_requests_.push(client);
297 } else {
298 client->OnPpapiChannelOpened(IPC::ChannelHandle(), 0);
302 void PpapiPluginProcessHost::OnProcessLaunched() {
303 host_impl_->set_plugin_process_handle(process_->GetHandle());
306 void PpapiPluginProcessHost::OnProcessCrashed(int exit_code) {
307 PluginServiceImpl::GetInstance()->RegisterPluginCrash(plugin_path_);
310 bool PpapiPluginProcessHost::OnMessageReceived(const IPC::Message& msg) {
311 bool handled = true;
312 IPC_BEGIN_MESSAGE_MAP(PpapiPluginProcessHost, msg)
313 IPC_MESSAGE_HANDLER(PpapiHostMsg_ChannelCreated,
314 OnRendererPluginChannelCreated)
315 IPC_MESSAGE_UNHANDLED(handled = false)
316 IPC_END_MESSAGE_MAP()
317 DCHECK(handled);
318 return handled;
321 // Called when the browser <--> plugin channel has been established.
322 void PpapiPluginProcessHost::OnChannelConnected(int32 peer_pid) {
323 // This will actually load the plugin. Errors will actually not be reported
324 // back at this point. Instead, the plugin will fail to establish the
325 // connections when we request them on behalf of the renderer(s).
326 Send(new PpapiMsg_LoadPlugin(plugin_path_, permissions_));
328 // Process all pending channel requests from the renderers.
329 for (size_t i = 0; i < pending_requests_.size(); i++)
330 RequestPluginChannel(pending_requests_[i]);
331 pending_requests_.clear();
334 // Called when the browser <--> plugin channel has an error. This normally
335 // means the plugin has crashed.
336 void PpapiPluginProcessHost::OnChannelError() {
337 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
338 << "::OnChannelError()";
339 // We don't need to notify the renderers that were communicating with the
340 // plugin since they have their own channels which will go into the error
341 // state at the same time. Instead, we just need to notify any renderers
342 // that have requested a connection but have not yet received one.
343 CancelRequests();
346 void PpapiPluginProcessHost::CancelRequests() {
347 DVLOG(1) << "PpapiPluginProcessHost" << (is_broker_ ? "[broker]" : "")
348 << "CancelRequests()";
349 for (size_t i = 0; i < pending_requests_.size(); i++) {
350 pending_requests_[i]->OnPpapiChannelOpened(IPC::ChannelHandle(), 0);
352 pending_requests_.clear();
354 while (!sent_requests_.empty()) {
355 sent_requests_.front()->OnPpapiChannelOpened(IPC::ChannelHandle(), 0);
356 sent_requests_.pop();
360 // Called when a new plugin <--> renderer channel has been created.
361 void PpapiPluginProcessHost::OnRendererPluginChannelCreated(
362 const IPC::ChannelHandle& channel_handle) {
363 if (sent_requests_.empty())
364 return;
366 // All requests should be processed FIFO, so the next item in the
367 // sent_requests_ queue should be the one that the plugin just created.
368 Client* client = sent_requests_.front();
369 sent_requests_.pop();
371 client->OnPpapiChannelOpened(channel_handle, process_->GetData().id);
374 } // namespace content