Add a FrameHostMsg_BeginNavigation IPC
[chromium-blink-merge.git] / content / browser / plugin_process_host.cc
blob2a68eaf1cb2bd11545557578ddba2d65192f935c
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/strings/string_number_conversions.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "content/browser/browser_child_process_host_impl.h"
26 #include "content/browser/loader/resource_message_filter.h"
27 #include "content/browser/gpu/gpu_data_manager_impl.h"
28 #include "content/browser/plugin_service_impl.h"
29 #include "content/common/child_process_host_impl.h"
30 #include "content/common/plugin_process_messages.h"
31 #include "content/common/resource_messages.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/content_browser_client.h"
34 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/plugin_service.h"
36 #include "content/public/browser/resource_context.h"
37 #include "content/public/common/content_switches.h"
38 #include "content/public/common/process_type.h"
39 #include "content/public/common/sandboxed_process_launcher_delegate.h"
40 #include "ipc/ipc_switches.h"
41 #include "net/url_request/url_request_context_getter.h"
42 #include "ui/base/ui_base_switches.h"
43 #include "ui/gfx/native_widget_types.h"
44 #include "ui/gfx/switches.h"
45 #include "ui/gl/gl_switches.h"
47 #if defined(OS_MACOSX)
48 #include "base/mac/mac_util.h"
49 #include "ui/gfx/rect.h"
50 #endif
52 #if defined(OS_WIN)
53 #include "base/win/windows_version.h"
54 #include "content/common/plugin_constants_win.h"
55 #endif
57 namespace content {
59 #if defined(OS_WIN)
60 void PluginProcessHost::OnPluginWindowDestroyed(HWND window, HWND parent) {
61 // The window is destroyed at this point, we just care about its parent, which
62 // is the intermediate window we created.
63 std::set<HWND>::iterator window_index =
64 plugin_parent_windows_set_.find(parent);
65 if (window_index == plugin_parent_windows_set_.end())
66 return;
68 plugin_parent_windows_set_.erase(window_index);
69 PostMessage(parent, WM_CLOSE, 0, 0);
72 void PluginProcessHost::AddWindow(HWND window) {
73 plugin_parent_windows_set_.insert(window);
75 #endif // defined(OS_WIN)
77 // NOTE: changes to this class need to be reviewed by the security team.
78 class PluginSandboxedProcessLauncherDelegate
79 : public SandboxedProcessLauncherDelegate {
80 public:
81 explicit PluginSandboxedProcessLauncherDelegate(ChildProcessHost* host)
82 #if defined(OS_POSIX)
83 : ipc_fd_(host->TakeClientFileDescriptor())
84 #endif // OS_POSIX
87 virtual ~PluginSandboxedProcessLauncherDelegate() {}
89 #if defined(OS_WIN)
90 virtual bool ShouldSandbox() OVERRIDE {
91 return false;
94 #elif defined(OS_POSIX)
95 virtual int GetIpcFd() OVERRIDE {
96 return ipc_fd_;
98 #endif // OS_WIN
100 private:
101 #if defined(OS_POSIX)
102 int ipc_fd_;
103 #endif // OS_POSIX
105 DISALLOW_COPY_AND_ASSIGN(PluginSandboxedProcessLauncherDelegate);
108 PluginProcessHost::PluginProcessHost()
109 #if defined(OS_MACOSX)
110 : plugin_cursor_visible_(true)
111 #endif
113 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_PLUGIN, this));
116 PluginProcessHost::~PluginProcessHost() {
117 #if defined(OS_WIN)
118 // We erase HWNDs from the plugin_parent_windows_set_ when we receive a
119 // notification that the window is being destroyed. If we don't receive this
120 // notification and the PluginProcessHost instance is being destroyed, it
121 // means that the plugin process crashed. We paint a sad face in this case in
122 // the renderer process. To ensure that the sad face shows up, and we don't
123 // leak HWNDs, we should destroy existing plugin parent windows.
124 std::set<HWND>::iterator window_index;
125 for (window_index = plugin_parent_windows_set_.begin();
126 window_index != plugin_parent_windows_set_.end();
127 ++window_index) {
128 PostMessage(*window_index, WM_CLOSE, 0, 0);
130 #elif defined(OS_MACOSX)
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
132 // If the plugin process crashed but had fullscreen windows open at the time,
133 // make sure that the menu bar is visible.
134 for (size_t i = 0; i < plugin_fullscreen_windows_set_.size(); ++i) {
135 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
136 base::Bind(base::mac::ReleaseFullScreen,
137 base::mac::kFullScreenModeHideAll));
139 // If the plugin hid the cursor, reset that.
140 if (!plugin_cursor_visible_) {
141 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
142 base::Bind(base::mac::SetCursorVisibility, true));
144 #endif
145 // Cancel all pending and sent requests.
146 CancelRequests();
149 bool PluginProcessHost::Send(IPC::Message* message) {
150 return process_->Send(message);
153 bool PluginProcessHost::Init(const WebPluginInfo& info) {
154 info_ = info;
155 process_->SetName(info_.name);
157 std::string channel_id = process_->GetHost()->CreateChannel();
158 if (channel_id.empty())
159 return false;
161 // Build command line for plugin. When we have a plugin launcher, we can't
162 // allow "self" on linux and we need the real file path.
163 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
164 CommandLine::StringType plugin_launcher =
165 browser_command_line.GetSwitchValueNative(switches::kPluginLauncher);
167 #if defined(OS_MACOSX)
168 // Run the plug-in process in a mode tolerant of heap execution without
169 // explicit mprotect calls. Some plug-ins still rely on this quaint and
170 // archaic "feature." See http://crbug.com/93551.
171 int flags = ChildProcessHost::CHILD_ALLOW_HEAP_EXECUTION;
172 #elif defined(OS_LINUX)
173 int flags = plugin_launcher.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
174 ChildProcessHost::CHILD_NORMAL;
175 #else
176 int flags = ChildProcessHost::CHILD_NORMAL;
177 #endif
179 base::FilePath exe_path = ChildProcessHost::GetChildPath(flags);
180 if (exe_path.empty())
181 return false;
183 CommandLine* cmd_line = new CommandLine(exe_path);
184 // Put the process type and plugin path first so they're easier to see
185 // in process listings using native process management tools.
186 cmd_line->AppendSwitchASCII(switches::kProcessType, switches::kPluginProcess);
187 cmd_line->AppendSwitchPath(switches::kPluginPath, info.path);
189 // Propagate the following switches to the plugin command line (along with
190 // any associated values) if present in the browser command line
191 static const char* const kSwitchNames[] = {
192 switches::kDisableBreakpad,
193 switches::kDisableDirectNPAPIRequests,
194 switches::kEnableStatsTable,
195 switches::kFullMemoryCrashReport,
196 switches::kLoggingLevel,
197 switches::kLogPluginMessages,
198 switches::kNoSandbox,
199 switches::kPluginStartupDialog,
200 switches::kTraceStartup,
201 switches::kUseGL,
202 switches::kForceDeviceScaleFactor,
203 #if defined(OS_MACOSX)
204 switches::kDisableCoreAnimationPlugins,
205 switches::kEnableSandboxLogging,
206 #endif
209 cmd_line->CopySwitchesFrom(browser_command_line, kSwitchNames,
210 arraysize(kSwitchNames));
212 GpuDataManagerImpl::GetInstance()->AppendPluginCommandLine(cmd_line);
214 // If specified, prepend a launcher program to the command line.
215 if (!plugin_launcher.empty())
216 cmd_line->PrependWrapper(plugin_launcher);
218 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
219 if (!locale.empty()) {
220 // Pass on the locale so the null plugin will use the right language in the
221 // prompt to install the desired plugin.
222 cmd_line->AppendSwitchASCII(switches::kLang, locale);
225 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
227 process_->Launch(
228 new PluginSandboxedProcessLauncherDelegate(process_->GetHost()),
229 cmd_line);
231 // The plugin needs to be shutdown gracefully, i.e. NP_Shutdown needs to be
232 // called on the plugin. The plugin process exits when it receives the
233 // OnChannelError notification indicating that the browser plugin channel has
234 // been destroyed.
235 process_->SetTerminateChildOnShutdown(false);
237 ResourceMessageFilter::GetContextsCallback get_contexts_callback(
238 base::Bind(&PluginProcessHost::GetContexts,
239 base::Unretained(this)));
241 // TODO(jam): right now we're passing NULL for appcache, blob storage, and
242 // file system. If NPAPI plugins actually use this, we'll have to plumb them.
243 ResourceMessageFilter* resource_message_filter = new ResourceMessageFilter(
244 process_->GetData().id, PROCESS_TYPE_PLUGIN, NULL, NULL, NULL, NULL,
245 get_contexts_callback);
246 process_->AddFilter(resource_message_filter);
247 return true;
250 void PluginProcessHost::ForceShutdown() {
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
252 Send(new PluginProcessMsg_NotifyRenderersOfPendingShutdown());
253 process_->ForceShutdown();
256 bool PluginProcessHost::OnMessageReceived(const IPC::Message& msg) {
257 bool handled = true;
258 IPC_BEGIN_MESSAGE_MAP(PluginProcessHost, msg)
259 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_ChannelCreated, OnChannelCreated)
260 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_ChannelDestroyed,
261 OnChannelDestroyed)
262 #if defined(OS_WIN)
263 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginWindowDestroyed,
264 OnPluginWindowDestroyed)
265 #endif
266 #if defined(OS_MACOSX)
267 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginShowWindow,
268 OnPluginShowWindow)
269 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginHideWindow,
270 OnPluginHideWindow)
271 IPC_MESSAGE_HANDLER(PluginProcessHostMsg_PluginSetCursorVisibility,
272 OnPluginSetCursorVisibility)
273 #endif
274 IPC_MESSAGE_UNHANDLED(handled = false)
275 IPC_END_MESSAGE_MAP()
277 return handled;
280 void PluginProcessHost::OnChannelConnected(int32 peer_pid) {
281 for (size_t i = 0; i < pending_requests_.size(); ++i) {
282 RequestPluginChannel(pending_requests_[i]);
285 pending_requests_.clear();
288 void PluginProcessHost::OnChannelError() {
289 CancelRequests();
292 bool PluginProcessHost::CanShutdown() {
293 return sent_requests_.empty();
296 void PluginProcessHost::OnProcessCrashed(int exit_code) {
297 PluginServiceImpl::GetInstance()->RegisterPluginCrash(info_.path);
300 void PluginProcessHost::CancelRequests() {
301 for (size_t i = 0; i < pending_requests_.size(); ++i)
302 pending_requests_[i]->OnError();
303 pending_requests_.clear();
305 while (!sent_requests_.empty()) {
306 Client* client = sent_requests_.front();
307 if (client)
308 client->OnError();
309 sent_requests_.pop_front();
313 void PluginProcessHost::OpenChannelToPlugin(Client* client) {
314 BrowserThread::PostTask(
315 BrowserThread::UI, FROM_HERE,
316 base::Bind(&BrowserChildProcessHostImpl::NotifyProcessInstanceCreated,
317 process_->GetData()));
318 client->SetPluginInfo(info_);
319 if (process_->GetHost()->IsChannelOpening()) {
320 // The channel is already in the process of being opened. Put
321 // this "open channel" request into a queue of requests that will
322 // be run once the channel is open.
323 pending_requests_.push_back(client);
324 return;
327 // We already have an open channel, send a request right away to plugin.
328 RequestPluginChannel(client);
331 void PluginProcessHost::CancelPendingRequest(Client* client) {
332 std::vector<Client*>::iterator it = pending_requests_.begin();
333 while (it != pending_requests_.end()) {
334 if (client == *it) {
335 pending_requests_.erase(it);
336 return;
338 ++it;
340 DCHECK(it != pending_requests_.end());
343 void PluginProcessHost::CancelSentRequest(Client* client) {
344 std::list<Client*>::iterator it = sent_requests_.begin();
345 while (it != sent_requests_.end()) {
346 if (client == *it) {
347 *it = NULL;
348 return;
350 ++it;
352 DCHECK(it != sent_requests_.end());
355 void PluginProcessHost::RequestPluginChannel(Client* client) {
356 // We can't send any sync messages from the browser because it might lead to
357 // a hang. However this async messages must be answered right away by the
358 // plugin process (i.e. unblocks a Send() call like a sync message) otherwise
359 // a deadlock can occur if the plugin creation request from the renderer is
360 // a result of a sync message by the plugin process.
361 PluginProcessMsg_CreateChannel* msg =
362 new PluginProcessMsg_CreateChannel(
363 client->ID(),
364 client->OffTheRecord());
365 msg->set_unblock(true);
366 if (Send(msg)) {
367 sent_requests_.push_back(client);
368 client->OnSentPluginChannelRequest();
369 } else {
370 client->OnError();
374 void PluginProcessHost::OnChannelCreated(
375 const IPC::ChannelHandle& channel_handle) {
376 Client* client = sent_requests_.front();
378 if (client) {
379 if (!resource_context_map_.count(client->ID())) {
380 ResourceContextEntry entry;
381 entry.ref_count = 0;
382 entry.resource_context = client->GetResourceContext();
383 resource_context_map_[client->ID()] = entry;
385 resource_context_map_[client->ID()].ref_count++;
386 client->OnChannelOpened(channel_handle);
388 sent_requests_.pop_front();
391 void PluginProcessHost::OnChannelDestroyed(int renderer_id) {
392 resource_context_map_[renderer_id].ref_count--;
393 if (!resource_context_map_[renderer_id].ref_count)
394 resource_context_map_.erase(renderer_id);
397 void PluginProcessHost::GetContexts(const ResourceHostMsg_Request& request,
398 ResourceContext** resource_context,
399 net::URLRequestContext** request_context) {
400 *resource_context =
401 resource_context_map_[request.origin_pid].resource_context;
402 *request_context = (*resource_context)->GetRequestContext();
405 } // namespace content