Re-enable index-basics-workers test to see if still times
[chromium-blink-merge.git] / content / browser / plugin_process_host.cc
blobcce088fc6f6a24e506a4ebf7af620f37c1cfd7a8
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/plugin_process_host.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #elif defined(OS_POSIX)
10 #include <utility> // for pair<>
11 #endif
13 #include <vector>
15 #include "base/base_switches.h"
16 #include "base/bind.h"
17 #include "base/command_line.h"
18 #include "base/files/file_path.h"
19 #include "base/logging.h"
20 #include "base/metrics/histogram.h"
21 #include "base/path_service.h"
22 #include "base/string_util.h"
23 #include "base/utf_string_conversions.h"
24 #include "content/browser/browser_child_process_host_impl.h"
25 #include "content/browser/gpu/gpu_data_manager_impl.h"
26 #include "content/browser/plugin_service_impl.h"
27 #include "content/common/child_process_host_impl.h"
28 #include "content/common/plugin_messages.h"
29 #include "content/common/resource_messages.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/content_browser_client.h"
32 #include "content/public/browser/notification_types.h"
33 #include "content/public/browser/plugin_service.h"
34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/process_type.h"
36 #include "ipc/ipc_switches.h"
37 #include "ui/base/ui_base_switches.h"
38 #include "ui/gfx/native_widget_types.h"
39 #include "ui/gl/gl_switches.h"
41 #if defined(USE_X11)
42 #include "ui/gfx/gtk_native_view_id_manager.h"
43 #endif
45 #if defined(OS_MACOSX)
46 #include "base/mac/mac_util.h"
47 #include "content/common/plugin_carbon_interpose_constants_mac.h"
48 #include "ui/gfx/rect.h"
49 #endif
51 #if defined(OS_WIN)
52 #include "base/win/windows_version.h"
53 #include "content/public/common/sandboxed_process_launcher_delegate.h"
54 #include "webkit/plugins/npapi/plugin_constants_win.h"
55 #include "webkit/plugins/npapi/webplugin_delegate_impl.h"
56 #endif
58 namespace content {
60 #if defined(OS_WIN)
61 void PluginProcessHost::OnPluginWindowDestroyed(HWND window, HWND parent) {
62 // The window is destroyed at this point, we just care about its parent, which
63 // is the intermediate window we created.
64 std::set<HWND>::iterator window_index =
65 plugin_parent_windows_set_.find(parent);
66 if (window_index == plugin_parent_windows_set_.end())
67 return;
69 plugin_parent_windows_set_.erase(window_index);
70 PostMessage(parent, WM_CLOSE, 0, 0);
73 void PluginProcessHost::AddWindow(HWND window) {
74 plugin_parent_windows_set_.insert(window);
77 // NOTE: changes to this class need to be reviewed by the security team.
78 class PluginSandboxedProcessLauncherDelegate
79 : public SandboxedProcessLauncherDelegate {
80 public:
81 PluginSandboxedProcessLauncherDelegate() {}
82 virtual ~PluginSandboxedProcessLauncherDelegate() {}
84 virtual void ShouldSandbox(bool* in_sandbox) OVERRIDE {
85 *in_sandbox = false;
88 private:
89 DISALLOW_COPY_AND_ASSIGN(PluginSandboxedProcessLauncherDelegate);
92 #endif // defined(OS_WIN)
94 #if defined(TOOLKIT_GTK)
95 void PluginProcessHost::OnMapNativeViewId(gfx::NativeViewId id,
96 gfx::PluginWindowHandle* output) {
97 *output = 0;
98 #if !defined(USE_AURA)
99 GtkNativeViewManager::GetInstance()->GetXIDForId(output, id);
100 #endif
102 #endif // defined(TOOLKIT_GTK)
104 PluginProcessHost::PluginProcessHost()
105 #if defined(OS_MACOSX)
106 : plugin_cursor_visible_(true)
107 #endif
109 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_PLUGIN, this));
112 PluginProcessHost::~PluginProcessHost() {
113 #if defined(OS_WIN)
114 // We erase HWNDs from the plugin_parent_windows_set_ when we receive a
115 // notification that the window is being destroyed. If we don't receive this
116 // notification and the PluginProcessHost instance is being destroyed, it
117 // means that the plugin process crashed. We paint a sad face in this case in
118 // the renderer process. To ensure that the sad face shows up, and we don't
119 // leak HWNDs, we should destroy existing plugin parent windows.
120 std::set<HWND>::iterator window_index;
121 for (window_index = plugin_parent_windows_set_.begin();
122 window_index != plugin_parent_windows_set_.end();
123 ++window_index) {
124 PostMessage(*window_index, WM_CLOSE, 0, 0);
126 #elif defined(OS_MACOSX)
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
128 // If the plugin process crashed but had fullscreen windows open at the time,
129 // make sure that the menu bar is visible.
130 for (size_t i = 0; i < plugin_fullscreen_windows_set_.size(); ++i) {
131 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
132 base::Bind(base::mac::ReleaseFullScreen,
133 base::mac::kFullScreenModeHideAll));
135 // If the plugin hid the cursor, reset that.
136 if (!plugin_cursor_visible_) {
137 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
138 base::Bind(base::mac::SetCursorVisibility, true));
140 #endif
141 // Cancel all pending and sent requests.
142 CancelRequests();
145 bool PluginProcessHost::Send(IPC::Message* message) {
146 return process_->Send(message);
149 bool PluginProcessHost::Init(const webkit::WebPluginInfo& info) {
150 info_ = info;
151 process_->SetName(info_.name);
153 std::string channel_id = process_->GetHost()->CreateChannel();
154 if (channel_id.empty())
155 return false;
157 // Build command line for plugin. When we have a plugin launcher, we can't
158 // allow "self" on linux and we need the real file path.
159 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
160 CommandLine::StringType plugin_launcher =
161 browser_command_line.GetSwitchValueNative(switches::kPluginLauncher);
163 #if defined(OS_MACOSX)
164 // Run the plug-in process in a mode tolerant of heap execution without
165 // explicit mprotect calls. Some plug-ins still rely on this quaint and
166 // archaic "feature." See http://crbug.com/93551.
167 int flags = ChildProcessHost::CHILD_ALLOW_HEAP_EXECUTION;
168 #elif defined(OS_LINUX)
169 int flags = plugin_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
170 ChildProcessHost::CHILD_NORMAL;
171 #else
172 int flags = ChildProcessHost::CHILD_NORMAL;
173 #endif
175 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
176 if (exe_path.empty())
177 return false;
179 CommandLine* cmd_line = new CommandLine(exe_path);
180 // Put the process type and plugin path first so they're easier to see
181 // in process listings using native process management tools.
182 cmd_line->AppendSwitchASCII(switches::kProcessType, switches::kPluginProcess);
183 cmd_line->AppendSwitchPath(switches::kPluginPath, info.path);
185 // Propagate the following switches to the plugin command line (along with
186 // any associated values) if present in the browser command line
187 static const char* const kSwitchNames[] = {
188 switches::kDisableBreakpad,
189 #if defined(OS_MACOSX)
190 switches::kDisableCoreAnimationPlugins,
191 switches::kEnableSandboxLogging,
192 #endif
193 switches::kEnableStatsTable,
194 switches::kFullMemoryCrashReport,
195 switches::kLoggingLevel,
196 switches::kLogPluginMessages,
197 switches::kNoSandbox,
198 switches::kPluginStartupDialog,
199 switches::kTestSandbox,
200 switches::kTraceStartup,
201 switches::kUseGL,
202 switches::kUserAgent,
205 cmd_line->CopySwitchesFrom(browser_command_line, kSwitchNames,
206 arraysize(kSwitchNames));
208 GpuDataManagerImpl::GetInstance()->AppendPluginCommandLine(cmd_line);
210 // If specified, prepend a launcher program to the command line.
211 if (!plugin_launcher.empty())
212 cmd_line->PrependWrapper(plugin_launcher);
214 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
215 if (!locale.empty()) {
216 // Pass on the locale so the null plugin will use the right language in the
217 // prompt to install the desired plugin.
218 cmd_line->AppendSwitchASCII(switches::kLang, locale);
221 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
223 #if defined(OS_POSIX)
224 base::EnvironmentVector env;
225 #if defined(OS_MACOSX) && !defined(__LP64__)
226 if (!browser_command_line.HasSwitch(switches::kDisableCarbonInterposing)) {
227 std::string interpose_list = GetContentClient()->GetCarbonInterposePath();
228 if (!interpose_list.empty()) {
229 // Add our interposing library for Carbon. This is stripped back out in
230 // plugin_main.cc, so changes here should be reflected there.
231 const char* existing_list = getenv(kDYLDInsertLibrariesKey);
232 if (existing_list) {
233 interpose_list.insert(0, ":");
234 interpose_list.insert(0, existing_list);
237 env.push_back(std::pair<std::string, std::string>(
238 kDYLDInsertLibrariesKey, interpose_list));
240 #endif
241 #endif
243 process_->Launch(
244 #if defined(OS_WIN)
245 new PluginSandboxedProcessLauncherDelegate,
246 #elif defined(OS_POSIX)
247 false,
248 env,
249 #endif
250 cmd_line);
252 // The plugin needs to be shutdown gracefully, i.e. NP_Shutdown needs to be
253 // called on the plugin. The plugin process exits when it receives the
254 // OnChannelError notification indicating that the browser plugin channel has
255 // been destroyed.
256 process_->SetTerminateChildOnShutdown(false);
258 return true;
261 void PluginProcessHost::ForceShutdown() {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
263 Send(new PluginProcessMsg_NotifyRenderersOfPendingShutdown());
264 process_->ForceShutdown();
267 void PluginProcessHost::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
268 process_->GetHost()->AddFilter(filter);
271 bool PluginProcessHost::OnMessageReceived(const IPC::Message& msg) {
272 bool handled = true;
273 IPC_BEGIN_MESSAGE_MAP(PluginProcessHost, msg)
274 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_ChannelCreated, OnChannelCreated)
275 #if defined(OS_WIN)
276 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginWindowDestroyed,
277 OnPluginWindowDestroyed)
278 #endif
279 #if defined(TOOLKIT_GTK)
280 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_MapNativeViewId,
281 OnMapNativeViewId)
282 #endif
283 #if defined(OS_MACOSX)
284 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginSelectWindow,
285 OnPluginSelectWindow)
286 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginShowWindow,
287 OnPluginShowWindow)
288 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginHideWindow,
289 OnPluginHideWindow)
290 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginSetCursorVisibility,
291 OnPluginSetCursorVisibility)
292 #endif
293 IPC_MESSAGE_UNHANDLED(handled = false)
294 IPC_END_MESSAGE_MAP()
296 DCHECK(handled);
297 return handled;
300 void PluginProcessHost::OnChannelConnected(int32 peer_pid) {
301 for (size_t i = 0; i < pending_requests_.size(); ++i) {
302 RequestPluginChannel(pending_requests_[i]);
305 pending_requests_.clear();
308 void PluginProcessHost::OnChannelError() {
309 CancelRequests();
312 bool PluginProcessHost::CanShutdown() {
313 return sent_requests_.empty();
316 void PluginProcessHost::OnProcessCrashed(int exit_code) {
317 PluginServiceImpl::GetInstance()->RegisterPluginCrash(info_.path);
320 void PluginProcessHost::CancelRequests() {
321 for (size_t i = 0; i < pending_requests_.size(); ++i)
322 pending_requests_[i]->OnError();
323 pending_requests_.clear();
325 while (!sent_requests_.empty()) {
326 Client* client = sent_requests_.front();
327 if (client)
328 client->OnError();
329 sent_requests_.pop_front();
333 // static
334 void PluginProcessHost::CancelPendingRequestsForResourceContext(
335 ResourceContext* context) {
336 for (PluginProcessHostIterator host_it; !host_it.Done(); ++host_it) {
337 PluginProcessHost* host = *host_it;
338 for (size_t i = 0; i < host->pending_requests_.size(); ++i) {
339 if (host->pending_requests_[i]->GetResourceContext() == context) {
340 host->pending_requests_[i]->OnError();
341 host->pending_requests_.erase(host->pending_requests_.begin() + i);
342 --i;
348 void PluginProcessHost::OpenChannelToPlugin(Client* client) {
349 BrowserThread::PostTask(
350 BrowserThread::UI, FROM_HERE,
351 base::Bind(&BrowserChildProcessHostImpl::NotifyProcessInstanceCreated,
352 process_->GetData()));
353 client->SetPluginInfo(info_);
354 if (process_->GetHost()->IsChannelOpening()) {
355 // The channel is already in the process of being opened. Put
356 // this "open channel" request into a queue of requests that will
357 // be run once the channel is open.
358 pending_requests_.push_back(client);
359 return;
362 // We already have an open channel, send a request right away to plugin.
363 RequestPluginChannel(client);
366 void PluginProcessHost::CancelPendingRequest(Client* client) {
367 std::vector<Client*>::iterator it = pending_requests_.begin();
368 while (it != pending_requests_.end()) {
369 if (client == *it) {
370 pending_requests_.erase(it);
371 return;
373 ++it;
375 DCHECK(it != pending_requests_.end());
378 void PluginProcessHost::CancelSentRequest(Client* client) {
379 std::list<Client*>::iterator it = sent_requests_.begin();
380 while (it != sent_requests_.end()) {
381 if (client == *it) {
382 *it = NULL;
383 return;
385 ++it;
387 DCHECK(it != sent_requests_.end());
390 void PluginProcessHost::RequestPluginChannel(Client* client) {
391 // We can't send any sync messages from the browser because it might lead to
392 // a hang. However this async messages must be answered right away by the
393 // plugin process (i.e. unblocks a Send() call like a sync message) otherwise
394 // a deadlock can occur if the plugin creation request from the renderer is
395 // a result of a sync message by the plugin process.
396 PluginProcessMsg_CreateChannel* msg =
397 new PluginProcessMsg_CreateChannel(
398 client->ID(),
399 client->OffTheRecord());
400 msg->set_unblock(true);
401 if (Send(msg)) {
402 sent_requests_.push_back(client);
403 client->OnSentPluginChannelRequest();
404 } else {
405 client->OnError();
409 void PluginProcessHost::OnChannelCreated(
410 const IPC::ChannelHandle& channel_handle) {
411 Client* client = sent_requests_.front();
413 if (client)
414 client->OnChannelOpened(channel_handle);
415 sent_requests_.pop_front();
418 } // namespace content