blob: d3ba06e9f4380dccc43ad274d1aafea87ea6f705 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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// Represents the browser side of the browser <--> renderer communication
6// channel. There will be one RenderProcessHost per renderer process.
7
8#include "content/browser/renderer_host/render_process_host_impl.h"
9
10#include <algorithm>
11#include <limits>
12#include <vector>
13
14#if defined(OS_POSIX)
15#include <utility> // for pair<>
16#endif
17
18#include "base/base_switches.h"
19#include "base/bind.h"
20#include "base/bind_helpers.h"
21#include "base/callback.h"
22#include "base/command_line.h"
23#include "base/debug/trace_event.h"
24#include "base/lazy_instance.h"
25#include "base/logging.h"
26#include "base/metrics/field_trial.h"
27#include "base/metrics/histogram.h"
28#include "base/path_service.h"
29#include "base/platform_file.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000030#include "base/rand_util.h"
31#include "base/stl_util.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010032#include "base/strings/string_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000033#include "base/supports_user_data.h"
34#include "base/sys_info.h"
35#include "base/threading/thread.h"
36#include "base/threading/thread_restrictions.h"
37#include "base/tracked_objects.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000038#include "cc/base/switches.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000039#include "content/browser/appcache/appcache_dispatcher_host.h"
40#include "content/browser/appcache/chrome_appcache_service.h"
41#include "content/browser/browser_main.h"
42#include "content/browser/browser_main_loop.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000043#include "content/browser/browser_plugin/browser_plugin_geolocation_permission_context.h"
44#include "content/browser/browser_plugin/browser_plugin_message_filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000045#include "content/browser/child_process_security_policy_impl.h"
Ben Murdoch2385ea32013-08-06 11:01:04 +010046#include "content/browser/device_orientation/device_motion_message_filter.h"
47#include "content/browser/device_orientation/device_orientation_message_filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048#include "content/browser/device_orientation/orientation_message_filter.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010049#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050#include "content/browser/dom_storage/dom_storage_message_filter.h"
51#include "content/browser/download/mhtml_generation_manager.h"
52#include "content/browser/fileapi/chrome_blob_storage_context.h"
53#include "content/browser/fileapi/fileapi_message_filter.h"
54#include "content/browser/geolocation/geolocation_dispatcher_host.h"
55#include "content/browser/gpu/gpu_data_manager_impl.h"
56#include "content/browser/gpu/gpu_process_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000057#include "content/browser/gpu/shader_disk_cache.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000058#include "content/browser/histogram_message_filter.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010059#include "content/browser/indexed_db/indexed_db_context_impl.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010060#include "content/browser/indexed_db/indexed_db_dispatcher_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000061#include "content/browser/loader/resource_message_filter.h"
62#include "content/browser/loader/resource_scheduler_filter.h"
63#include "content/browser/media/media_internals.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000064#include "content/browser/mime_registry_message_filter.h"
65#include "content/browser/plugin_service_impl.h"
66#include "content/browser/profiler_message_filter.h"
Ben Murdochbbcdd452013-07-25 10:06:34 +010067#include "content/browser/quota_dispatcher_host.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068#include "content/browser/renderer_host/clipboard_message_filter.h"
69#include "content/browser/renderer_host/database_message_filter.h"
70#include "content/browser/renderer_host/file_utilities_message_filter.h"
71#include "content/browser/renderer_host/gamepad_browser_message_filter.h"
72#include "content/browser/renderer_host/gpu_message_filter.h"
73#include "content/browser/renderer_host/media/audio_input_renderer_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000074#include "content/browser/renderer_host/media/audio_mirroring_manager.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000075#include "content/browser/renderer_host/media/audio_renderer_host.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010076#include "content/browser/renderer_host/media/device_request_message_filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077#include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010078#include "content/browser/renderer_host/media/midi_dispatcher_host.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010079#include "content/browser/renderer_host/media/midi_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080#include "content/browser/renderer_host/media/peer_connection_tracker_host.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000081#include "content/browser/renderer_host/media/video_capture_host.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010082#include "content/browser/renderer_host/memory_benchmark_message_filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000083#include "content/browser/renderer_host/p2p/socket_dispatcher_host.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000084#include "content/browser/renderer_host/pepper/pepper_message_filter.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010085#include "content/browser/renderer_host/pepper/pepper_renderer_connection.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000086#include "content/browser/renderer_host/render_message_filter.h"
87#include "content/browser/renderer_host/render_view_host_delegate.h"
88#include "content/browser/renderer_host/render_view_host_impl.h"
89#include "content/browser/renderer_host/render_widget_helper.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010090#include "content/browser/renderer_host/render_widget_host_impl.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091#include "content/browser/renderer_host/socket_stream_dispatcher_host.h"
92#include "content/browser/renderer_host/text_input_client_message_filter.h"
93#include "content/browser/resolve_proxy_msg_helper.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000094#include "content/browser/speech/input_tag_speech_dispatcher_host.h"
95#include "content/browser/speech/speech_recognition_dispatcher_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000096#include "content/browser/storage_partition_impl.h"
Ben Murdoch2385ea32013-08-06 11:01:04 +010097#include "content/browser/streams/stream_context.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000098#include "content/browser/tracing/trace_message_filter.h"
99#include "content/browser/webui/web_ui_controller_factory_registry.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100#include "content/browser/worker_host/worker_message_filter.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000101#include "content/browser/worker_host/worker_storage_partition.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000102#include "content/common/child_process_host_impl.h"
103#include "content/common/child_process_messages.h"
104#include "content/common/gpu/gpu_messages.h"
105#include "content/common/resource_messages.h"
106#include "content/common/view_messages.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107#include "content/port/browser/render_widget_host_view_frame_subscriber.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000108#include "content/public/browser/browser_context.h"
109#include "content/public/browser/content_browser_client.h"
110#include "content/public/browser/notification_service.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000111#include "content/public/browser/notification_types.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000112#include "content/public/browser/render_process_host_factory.h"
Ben Murdocheb525c52013-07-10 11:40:50 +0100113#include "content/public/browser/render_widget_host.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000114#include "content/public/browser/resource_context.h"
115#include "content/public/browser/user_metrics.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000116#include "content/public/common/content_constants.h"
117#include "content/public/common/content_switches.h"
118#include "content/public/common/process_type.h"
119#include "content/public/common/result_codes.h"
120#include "content/public/common/url_constants.h"
121#include "content/renderer/render_process_impl.h"
122#include "content/renderer/render_thread_impl.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100123#include "gpu/command_buffer/service/gpu_switches.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000124#include "ipc/ipc_channel.h"
125#include "ipc/ipc_logging.h"
126#include "ipc/ipc_platform_file.h"
127#include "ipc/ipc_switches.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000128#include "media/base/media_switches.h"
129#include "net/url_request/url_request_context_getter.h"
130#include "ppapi/shared_impl/ppapi_switches.h"
131#include "ui/base/ui_base_switches.h"
132#include "ui/gl/gl_switches.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100133#include "webkit/browser/fileapi/sandbox_file_system_backend.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +0100134#include "webkit/common/resource_type.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000135#include "webkit/plugins/plugin_switches.h"
136
Ben Murdochca12bfa2013-07-23 11:17:05 +0100137#if defined(OS_ANDROID)
138#include "content/browser/android/vibration_message_filter.h"
139#endif
140
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000141#if defined(OS_WIN)
142#include "base/win/scoped_com_initializer.h"
143#include "content/common/font_cache_dispatcher_win.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000144#include "content/common/sandbox_win.h"
145#include "content/public/common/sandboxed_process_launcher_delegate.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000146#endif
147
Ben Murdocheb525c52013-07-10 11:40:50 +0100148#if defined(ENABLE_WEBRTC)
149#include "content/browser/renderer_host/media/webrtc_identity_service_host.h"
150#endif
151
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000152#include "third_party/skia/include/core/SkBitmap.h"
153
154extern bool g_exited_main_message_loop;
155
156static const char* kSiteProcessMapKeyName = "content_site_process_map";
157
158namespace content {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000159namespace {
160
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100161base::MessageLoop* g_in_process_thread;
162
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000163void CacheShaderInfo(int32 id, base::FilePath path) {
164 ShaderCacheFactory::GetInstance()->SetCacheInfo(id, path);
165}
166
167void RemoveShaderInfo(int32 id) {
168 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(id);
169}
170
171} // namespace
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000172
Ben Murdochca12bfa2013-07-23 11:17:05 +0100173#if !defined(CHROME_MULTIPLE_DLL)
174
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000175// This class creates the IO thread for the renderer when running in
176// single-process mode. It's not used in multi-process mode.
177class RendererMainThread : public base::Thread {
178 public:
179 explicit RendererMainThread(const std::string& channel_id)
180 : Thread("Chrome_InProcRendererThread"),
181 channel_id_(channel_id) {
182 }
183
184 virtual ~RendererMainThread() {
185 Stop();
186 }
187
188 protected:
189 virtual void Init() OVERRIDE {
190 render_process_.reset(new RenderProcessImpl());
191 new RenderThreadImpl(channel_id_);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100192 g_in_process_thread = message_loop();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000193 }
194
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000195 virtual void CleanUp() OVERRIDE {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100196 g_in_process_thread = NULL;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000197 render_process_.reset();
198
199 // It's a little lame to manually set this flag. But the single process
200 // RendererThread will receive the WM_QUIT. We don't need to assert on
201 // this thread, so just force the flag manually.
202 // If we want to avoid this, we could create the InProcRendererThread
203 // directly with _beginthreadex() rather than using the Thread class.
204 // We used to set this flag in the Init function above. However there
205 // other threads like WebThread which are created by this thread
206 // which resets this flag. Please see Thread::StartWithOptions. Setting
207 // this flag to true in Cleanup works around these problems.
208 SetThreadWasQuitProperly(true);
209 }
210
211 private:
212 std::string channel_id_;
213 scoped_ptr<RenderProcess> render_process_;
214
215 DISALLOW_COPY_AND_ASSIGN(RendererMainThread);
216};
217
Ben Murdochca12bfa2013-07-23 11:17:05 +0100218#endif
219
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000220namespace {
221
222// Helper class that we pass to ResourceMessageFilter so that it can find the
223// right net::URLRequestContext for a request.
224class RendererURLRequestContextSelector
225 : public ResourceMessageFilter::URLRequestContextSelector {
226 public:
227 RendererURLRequestContextSelector(BrowserContext* browser_context,
228 int render_child_id)
229 : request_context_(browser_context->GetRequestContextForRenderProcess(
230 render_child_id)),
231 media_request_context_(
232 browser_context->GetMediaRequestContextForRenderProcess(
233 render_child_id)) {
234 }
235
236 virtual net::URLRequestContext* GetRequestContext(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000237 ResourceType::Type resource_type) OVERRIDE {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100238 net::URLRequestContextGetter* request_context = request_context_.get();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000239 // If the request has resource type of ResourceType::MEDIA, we use a request
240 // context specific to media for handling it because these resources have
241 // specific needs for caching.
242 if (resource_type == ResourceType::MEDIA)
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100243 request_context = media_request_context_.get();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000244 return request_context->GetURLRequestContext();
245 }
246
247 private:
248 virtual ~RendererURLRequestContextSelector() {}
249
250 scoped_refptr<net::URLRequestContextGetter> request_context_;
251 scoped_refptr<net::URLRequestContextGetter> media_request_context_;
252};
253
254// the global list of all renderer processes
255base::LazyInstance<IDMap<RenderProcessHost> >::Leaky
256 g_all_hosts = LAZY_INSTANCE_INITIALIZER;
257
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100258base::LazyInstance<scoped_refptr<BrowserPluginGeolocationPermissionContext> >
259 g_browser_plugin_geolocation_context = LAZY_INSTANCE_INITIALIZER;
260
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000261// Map of site to process, to ensure we only have one RenderProcessHost per
262// site in process-per-site mode. Each map is specific to a BrowserContext.
263class SiteProcessMap : public base::SupportsUserData::Data {
264 public:
265 typedef base::hash_map<std::string, RenderProcessHost*> SiteToProcessMap;
266 SiteProcessMap() {}
267
268 void RegisterProcess(const std::string& site, RenderProcessHost* process) {
269 map_[site] = process;
270 }
271
272 RenderProcessHost* FindProcess(const std::string& site) {
273 SiteToProcessMap::iterator i = map_.find(site);
274 if (i != map_.end())
275 return i->second;
276 return NULL;
277 }
278
279 void RemoveProcess(RenderProcessHost* host) {
280 // Find all instances of this process in the map, then separately remove
281 // them.
282 std::set<std::string> sites;
283 for (SiteToProcessMap::const_iterator i = map_.begin();
284 i != map_.end();
285 i++) {
286 if (i->second == host)
287 sites.insert(i->first);
288 }
289 for (std::set<std::string>::iterator i = sites.begin();
290 i != sites.end();
291 i++) {
292 SiteToProcessMap::iterator iter = map_.find(*i);
293 if (iter != map_.end()) {
294 DCHECK_EQ(iter->second, host);
295 map_.erase(iter);
296 }
297 }
298 }
299
300 private:
301 SiteToProcessMap map_;
302};
303
304// Find the SiteProcessMap specific to the given context.
305SiteProcessMap* GetSiteProcessMapForBrowserContext(BrowserContext* context) {
306 DCHECK(context);
307 SiteProcessMap* map = static_cast<SiteProcessMap*>(
308 context->GetUserData(kSiteProcessMapKeyName));
309 if (!map) {
310 map = new SiteProcessMap();
311 context->SetUserData(kSiteProcessMapKeyName, map);
312 }
313 return map;
314}
315
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000316#if defined(OS_WIN)
317// NOTE: changes to this class need to be reviewed by the security team.
318class RendererSandboxedProcessLauncherDelegate
319 : public content::SandboxedProcessLauncherDelegate {
320 public:
321 RendererSandboxedProcessLauncherDelegate() {}
322 virtual ~RendererSandboxedProcessLauncherDelegate() {}
323
324 virtual void ShouldSandbox(bool* in_sandbox) OVERRIDE {
325#if !defined (GOOGLE_CHROME_BUILD)
326 if (CommandLine::ForCurrentProcess()->HasSwitch(
327 switches::kInProcessPlugins)) {
328 *in_sandbox = false;
329 }
330#endif
331 }
332
333 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy,
334 bool* success) {
335 AddBaseHandleClosePolicy(policy);
336 GetContentClient()->browser()->PreSpawnRenderer(policy, success);
337 }
338};
339#endif // OS_WIN
340
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000341} // namespace
342
343// Stores the maximum number of renderer processes the content module can
344// create.
345static size_t g_max_renderer_count_override = 0;
346
347// static
348size_t RenderProcessHost::GetMaxRendererProcessCount() {
349 if (g_max_renderer_count_override)
350 return g_max_renderer_count_override;
351
352 // Defines the maximum number of renderer processes according to the
353 // amount of installed memory as reported by the OS. The calculation
354 // assumes that you want the renderers to use half of the installed
355 // RAM and assuming that each WebContents uses ~40MB.
356 // If you modify this assumption, you need to adjust the
357 // ThirtyFourTabs test to match the expected number of processes.
358 //
359 // With the given amounts of installed memory below on a 32-bit CPU,
360 // the maximum renderer count will roughly be as follows:
361 //
362 // 128 MB -> 3
363 // 512 MB -> 6
364 // 1024 MB -> 12
365 // 4096 MB -> 51
366 // 16384 MB -> 82 (kMaxRendererProcessCount)
367
368 static size_t max_count = 0;
369 if (!max_count) {
370 const size_t kEstimatedWebContentsMemoryUsage =
371#if defined(ARCH_CPU_64_BITS)
372 60; // In MB
373#else
374 40; // In MB
375#endif
376 max_count = base::SysInfo::AmountOfPhysicalMemoryMB() / 2;
377 max_count /= kEstimatedWebContentsMemoryUsage;
378
379 const size_t kMinRendererProcessCount = 3;
380 max_count = std::max(max_count, kMinRendererProcessCount);
381 max_count = std::min(max_count, kMaxRendererProcessCount);
382 }
383 return max_count;
384}
385
386// static
387bool g_run_renderer_in_process_ = false;
388
389// static
390void RenderProcessHost::SetMaxRendererProcessCount(size_t count) {
391 g_max_renderer_count_override = count;
392}
393
394RenderProcessHostImpl::RenderProcessHostImpl(
395 BrowserContext* browser_context,
396 StoragePartitionImpl* storage_partition_impl,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000397 bool supports_browser_plugin,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000398 bool is_guest)
399 : fast_shutdown_started_(false),
400 deleting_soon_(false),
401 pending_views_(0),
402 visible_widgets_(0),
403 backgrounded_(true),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100404 cached_dibs_cleaner_(
405 FROM_HERE, base::TimeDelta::FromSeconds(5),
406 this, &RenderProcessHostImpl::ClearTransportDIBCache),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000407 is_initialized_(false),
408 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
409 browser_context_(browser_context),
410 storage_partition_impl_(storage_partition_impl),
411 sudden_termination_allowed_(true),
412 ignore_input_events_(false),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000413 supports_browser_plugin_(supports_browser_plugin),
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100414 is_guest_(is_guest),
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100415 gpu_observer_registered_(false),
416 power_monitor_broadcaster_(this) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000417 widget_helper_ = new RenderWidgetHelper();
418
419 ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID());
420
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000421 CHECK(!g_exited_main_message_loop);
422 RegisterHost(GetID(), this);
423 g_all_hosts.Get().set_check_on_null_data(true);
424 // Initialize |child_process_activity_time_| to a reasonable value.
425 mark_child_process_activity_time();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000426
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100427 if (!GetBrowserContext()->IsOffTheRecord() &&
428 !CommandLine::ForCurrentProcess()->HasSwitch(
429 switches::kDisableGpuShaderDiskCache)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000430 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
431 base::Bind(&CacheShaderInfo, GetID(),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100432 storage_partition_impl_->GetPath()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000433 }
434
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000435 // Note: When we create the RenderProcessHostImpl, it's technically
436 // backgrounded, because it has no visible listeners. But the process
437 // doesn't actually exist yet, so we'll Background it later, after
438 // creation.
439}
440
441RenderProcessHostImpl::~RenderProcessHostImpl() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000442 DCHECK(!run_renderer_in_process());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000443 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID());
444
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100445 if (gpu_observer_registered_) {
446 GpuDataManagerImpl::GetInstance()->RemoveObserver(this);
447 gpu_observer_registered_ = false;
448 }
449
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000450 // We may have some unsent messages at this point, but that's OK.
451 channel_.reset();
452 while (!queued_messages_.empty()) {
453 delete queued_messages_.front();
454 queued_messages_.pop();
455 }
456
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000457 ClearTransportDIBCache();
458 UnregisterHost(GetID());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000459
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100460 if (!CommandLine::ForCurrentProcess()->HasSwitch(
461 switches::kDisableGpuShaderDiskCache)) {
462 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
463 base::Bind(&RemoveShaderInfo, GetID()));
464 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000465}
466
467void RenderProcessHostImpl::EnableSendQueue() {
468 is_initialized_ = false;
469}
470
471bool RenderProcessHostImpl::Init() {
472 // calling Init() more than once does nothing, this makes it more convenient
473 // for the view host which may not be sure in some cases
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100474 if (channel_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000475 return true;
476
477 CommandLine::StringType renderer_prefix;
478#if defined(OS_POSIX)
479 // A command prefix is something prepended to the command line of the spawned
480 // process. It is supported only on POSIX systems.
481 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
482 renderer_prefix =
483 browser_command_line.GetSwitchValueNative(switches::kRendererCmdPrefix);
484#endif // defined(OS_POSIX)
485
486#if defined(OS_LINUX)
487 int flags = renderer_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
488 ChildProcessHost::CHILD_NORMAL;
489#else
490 int flags = ChildProcessHost::CHILD_NORMAL;
491#endif
492
493 // Find the renderer before creating the channel so if this fails early we
494 // return without creating the channel.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000495 base::FilePath renderer_path = ChildProcessHost::GetChildPath(flags);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000496 if (renderer_path.empty())
497 return false;
498
499 // Setup the IPC channel.
500 const std::string channel_id =
501 IPC::Channel::GenerateVerifiedChannelID(std::string());
502 channel_.reset(
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100503 new IPC::ChannelProxy(channel_id,
504 IPC::Channel::MODE_SERVER,
505 this,
506 BrowserThread::GetMessageLoopProxyForThread(
507 BrowserThread::IO).get()));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000508
509 // Call the embedder first so that their IPC filters have priority.
510 GetContentClient()->browser()->RenderProcessHostCreated(this);
511
512 CreateMessageFilters();
513
Ben Murdochca12bfa2013-07-23 11:17:05 +0100514 // Single-process mode not supported in multiple-dll mode currently.
515#if !defined(CHROME_MULTIPLE_DLL)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000516 if (run_renderer_in_process()) {
517 // Crank up a thread and run the initialization there. With the way that
518 // messages flow between the browser and renderer, this thread is required
519 // to prevent a deadlock in single-process mode. Since the primordial
520 // thread in the renderer process runs the WebKit code and can sometimes
521 // make blocking calls to the UI thread (i.e. this thread), they need to run
522 // on separate threads.
523 in_process_renderer_.reset(new RendererMainThread(channel_id));
524
525 base::Thread::Options options;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100526#if defined(OS_WIN) && !defined(OS_MACOSX)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000527 // In-process plugins require this to be a UI message loop.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100528 options.message_loop_type = base::MessageLoop::TYPE_UI;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000529#else
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100530 // We can't have multiple UI loops on Linux and Android, so we don't support
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000531 // in-process plugins.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100532 options.message_loop_type = base::MessageLoop::TYPE_DEFAULT;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000533#endif
534 in_process_renderer_->StartWithOptions(options);
535
536 OnProcessLaunched(); // Fake a callback that the process is ready.
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100537 } else
Ben Murdochca12bfa2013-07-23 11:17:05 +0100538#endif // !CHROME_MULTIPLE_DLL
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100539 {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000540 // Build command line for renderer. We call AppendRendererCommandLine()
541 // first so the process type argument will appear first.
542 CommandLine* cmd_line = new CommandLine(renderer_path);
543 if (!renderer_prefix.empty())
544 cmd_line->PrependWrapper(renderer_prefix);
545 AppendRendererCommandLine(cmd_line);
546 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
547
548 // Spawn the child process asynchronously to avoid blocking the UI thread.
549 // As long as there's no renderer prefix, we can use the zygote process
550 // at this stage.
551 child_process_launcher_.reset(new ChildProcessLauncher(
552#if defined(OS_WIN)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000553 new RendererSandboxedProcessLauncherDelegate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000554#elif defined(OS_POSIX)
555 renderer_prefix.empty(),
556 base::EnvironmentVector(),
557 channel_->TakeClientFileDescriptor(),
558#endif
559 cmd_line,
560 GetID(),
561 this));
562
563 fast_shutdown_started_ = false;
564 }
565
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100566 if (!gpu_observer_registered_) {
567 gpu_observer_registered_ = true;
568 GpuDataManagerImpl::GetInstance()->AddObserver(this);
569 }
570
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000571 is_initialized_ = true;
572 return true;
573}
574
575void RenderProcessHostImpl::CreateMessageFilters() {
576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000577 channel_->AddFilter(new ResourceSchedulerFilter(GetID()));
578 MediaInternals* media_internals = MediaInternals::GetInstance();;
Ben Murdocheb525c52013-07-10 11:40:50 +0100579 media::AudioManager* audio_manager =
580 BrowserMainLoop::GetInstance()->audio_manager();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000581 // Add BrowserPluginMessageFilter to ensure it gets the first stab at messages
582 // from guests.
583 if (supports_browser_plugin_) {
584 scoped_refptr<BrowserPluginMessageFilter> bp_message_filter(
585 new BrowserPluginMessageFilter(GetID(), IsGuest()));
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100586 channel_->AddFilter(bp_message_filter.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000587 }
588
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000589 scoped_refptr<RenderMessageFilter> render_message_filter(
590 new RenderMessageFilter(
591 GetID(),
Ben Murdoch558790d2013-07-30 15:19:42 +0100592 IsGuest(),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000593#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000594 PluginServiceImpl::GetInstance(),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000595#else
596 NULL,
597#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000598 GetBrowserContext(),
599 GetBrowserContext()->GetRequestContextForRenderProcess(GetID()),
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100600 widget_helper_.get(),
Ben Murdocheb525c52013-07-10 11:40:50 +0100601 audio_manager,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000602 media_internals,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000603 storage_partition_impl_->GetDOMStorageContext()));
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100604 channel_->AddFilter(render_message_filter.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000605 BrowserContext* browser_context = GetBrowserContext();
606 ResourceContext* resource_context = browser_context->GetResourceContext();
607
608 ResourceMessageFilter* resource_message_filter = new ResourceMessageFilter(
609 GetID(), PROCESS_TYPE_RENDERER, resource_context,
610 storage_partition_impl_->GetAppCacheService(),
611 ChromeBlobStorageContext::GetFor(browser_context),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000612 storage_partition_impl_->GetFileSystemContext(),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000613 new RendererURLRequestContextSelector(browser_context, GetID()));
614
615 channel_->AddFilter(resource_message_filter);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000616 MediaStreamManager* media_stream_manager =
Ben Murdocheb525c52013-07-10 11:40:50 +0100617 BrowserMainLoop::GetInstance()->media_stream_manager();
618 channel_->AddFilter(new AudioInputRendererHost(
619 audio_manager,
620 media_stream_manager,
621 BrowserMainLoop::GetInstance()->audio_mirroring_manager()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000622 channel_->AddFilter(new AudioRendererHost(
Ben Murdocheb525c52013-07-10 11:40:50 +0100623 GetID(), audio_manager,
624 BrowserMainLoop::GetInstance()->audio_mirroring_manager(),
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100625 media_internals, media_stream_manager));
Ben Murdocheb525c52013-07-10 11:40:50 +0100626 channel_->AddFilter(
627 new MIDIHost(BrowserMainLoop::GetInstance()->midi_manager()));
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100628 channel_->AddFilter(new MIDIDispatcherHost(GetID(), browser_context));
Ben Murdocheb525c52013-07-10 11:40:50 +0100629 channel_->AddFilter(new VideoCaptureHost(media_stream_manager));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000630 channel_->AddFilter(new AppCacheDispatcherHost(
631 storage_partition_impl_->GetAppCacheService(),
632 GetID()));
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100633 channel_->AddFilter(new ClipboardMessageFilter);
Ben Murdocheb525c52013-07-10 11:40:50 +0100634 channel_->AddFilter(new DOMStorageMessageFilter(
635 GetID(),
636 storage_partition_impl_->GetDOMStorageContext()));
637 channel_->AddFilter(new IndexedDBDispatcherHost(
638 GetID(),
639 storage_partition_impl_->GetIndexedDBContext()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000640 if (IsGuest()) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100641 if (!g_browser_plugin_geolocation_context.Get().get()) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100642 g_browser_plugin_geolocation_context.Get() =
643 new BrowserPluginGeolocationPermissionContext();
644 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000645 channel_->AddFilter(GeolocationDispatcherHost::New(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100646 GetID(), g_browser_plugin_geolocation_context.Get().get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000647 } else {
648 channel_->AddFilter(GeolocationDispatcherHost::New(
649 GetID(), browser_context->GetGeolocationPermissionContext()));
650 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000651 gpu_message_filter_ = new GpuMessageFilter(GetID(), widget_helper_.get());
652 channel_->AddFilter(gpu_message_filter_);
653#if defined(ENABLE_WEBRTC)
Ben Murdocheb525c52013-07-10 11:40:50 +0100654 channel_->AddFilter(new WebRTCIdentityServiceHost(
655 storage_partition_impl_->GetWebRTCIdentityStore()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000656 peer_connection_tracker_host_ = new PeerConnectionTrackerHost(GetID());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100657 channel_->AddFilter(peer_connection_tracker_host_.get());
Ben Murdocheb525c52013-07-10 11:40:50 +0100658 channel_->AddFilter(new MediaStreamDispatcherHost(
659 GetID(), media_stream_manager));
Ben Murdoch32409262013-08-07 11:04:47 +0100660 channel_->AddFilter(
661 new DeviceRequestMessageFilter(resource_context, media_stream_manager));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000662#endif
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000663#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100664 // TODO(raymes): PepperMessageFilter should be removed from here.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000665 channel_->AddFilter(new PepperMessageFilter(GetID(), browser_context));
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100666 channel_->AddFilter(new PepperRendererConnection(GetID()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000667#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000668#if defined(ENABLE_INPUT_SPEECH)
669 channel_->AddFilter(new InputTagSpeechDispatcherHost(
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100670 IsGuest(), GetID(), storage_partition_impl_->GetURLRequestContext()));
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100671#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000672 channel_->AddFilter(new SpeechRecognitionDispatcherHost(
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100673 GetID(), storage_partition_impl_->GetURLRequestContext()));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000674 channel_->AddFilter(new FileAPIMessageFilter(
675 GetID(),
676 storage_partition_impl_->GetURLRequestContext(),
677 storage_partition_impl_->GetFileSystemContext(),
Ben Murdoch2385ea32013-08-06 11:01:04 +0100678 ChromeBlobStorageContext::GetFor(browser_context),
679 StreamContext::GetFor(browser_context)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000680 channel_->AddFilter(new OrientationMessageFilter());
681 channel_->AddFilter(new FileUtilitiesMessageFilter(GetID()));
682 channel_->AddFilter(new MimeRegistryMessageFilter());
683 channel_->AddFilter(new DatabaseMessageFilter(
684 storage_partition_impl_->GetDatabaseTracker()));
685#if defined(OS_MACOSX)
686 channel_->AddFilter(new TextInputClientMessageFilter(GetID()));
687#elif defined(OS_WIN)
688 channel_->AddFilter(new FontCacheDispatcher());
689#endif
690
691 SocketStreamDispatcherHost* socket_stream_dispatcher_host =
692 new SocketStreamDispatcherHost(GetID(),
693 new RendererURLRequestContextSelector(browser_context, GetID()),
694 resource_context);
695 channel_->AddFilter(socket_stream_dispatcher_host);
696
Ben Murdocheb525c52013-07-10 11:40:50 +0100697 channel_->AddFilter(new WorkerMessageFilter(
698 GetID(),
699 resource_context,
700 WorkerStoragePartition(
701 storage_partition_impl_->GetURLRequestContext(),
702 storage_partition_impl_->GetMediaURLRequestContext(),
703 storage_partition_impl_->GetAppCacheService(),
704 storage_partition_impl_->GetQuotaManager(),
705 storage_partition_impl_->GetFileSystemContext(),
706 storage_partition_impl_->GetDatabaseTracker(),
707 storage_partition_impl_->GetIndexedDBContext()),
708 base::Bind(&RenderWidgetHelper::GetNextRoutingID,
709 base::Unretained(widget_helper_.get()))));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000710
711#if defined(ENABLE_WEBRTC)
Ben Murdocheb525c52013-07-10 11:40:50 +0100712 channel_->AddFilter(new P2PSocketDispatcherHost(
713 resource_context,
714 browser_context->GetRequestContextForRenderProcess(GetID())));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000715#endif
716
717 channel_->AddFilter(new TraceMessageFilter());
718 channel_->AddFilter(new ResolveProxyMsgHelper(
719 browser_context->GetRequestContextForRenderProcess(GetID())));
720 channel_->AddFilter(new QuotaDispatcherHost(
721 GetID(),
722 storage_partition_impl_->GetQuotaManager(),
723 GetContentClient()->browser()->CreateQuotaPermissionContext()));
724 channel_->AddFilter(new GamepadBrowserMessageFilter());
Ben Murdoch2385ea32013-08-06 11:01:04 +0100725 channel_->AddFilter(new DeviceMotionMessageFilter());
726 channel_->AddFilter(new DeviceOrientationMessageFilter());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000727 channel_->AddFilter(new ProfilerMessageFilter(PROCESS_TYPE_RENDERER));
728 channel_->AddFilter(new HistogramMessageFilter());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100729#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
730 if (CommandLine::ForCurrentProcess()->HasSwitch(
731 switches::kEnableMemoryBenchmarking))
732 channel_->AddFilter(new MemoryBenchmarkMessageFilter());
733#endif
Ben Murdochca12bfa2013-07-23 11:17:05 +0100734#if defined(OS_ANDROID)
735 channel_->AddFilter(new VibrationMessageFilter());
736#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000737}
738
739int RenderProcessHostImpl::GetNextRoutingID() {
740 return widget_helper_->GetNextRoutingID();
741}
742
Ben Murdocheb525c52013-07-10 11:40:50 +0100743
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100744void RenderProcessHostImpl::ResumeDeferredNavigation(
745 const GlobalRequestID& request_id) {
746 widget_helper_->ResumeDeferredNavigation(request_id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000747}
748
Ben Murdocheb525c52013-07-10 11:40:50 +0100749void RenderProcessHostImpl::AddRoute(
750 int32 routing_id,
751 IPC::Listener* listener) {
752 listeners_.AddWithID(listener, routing_id);
753}
754
755void RenderProcessHostImpl::RemoveRoute(int32 routing_id) {
756 DCHECK(listeners_.Lookup(routing_id) != NULL);
757 listeners_.Remove(routing_id);
758
759#if defined(OS_WIN)
760 // Dump the handle table if handle auditing is enabled.
761 const CommandLine& browser_command_line =
762 *CommandLine::ForCurrentProcess();
763 if (browser_command_line.HasSwitch(switches::kAuditHandles) ||
764 browser_command_line.HasSwitch(switches::kAuditAllHandles)) {
765 DumpHandles();
766
767 // We wait to close the channels until the child process has finished
768 // dumping handles and sends us ChildProcessHostMsg_DumpHandlesDone.
769 return;
770 }
771#endif
772 // Keep the one renderer thread around forever in single process mode.
773 if (!run_renderer_in_process())
774 Cleanup();
775}
776
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000777bool RenderProcessHostImpl::WaitForBackingStoreMsg(
778 int render_widget_id,
779 const base::TimeDelta& max_delay,
780 IPC::Message* msg) {
781 // The post task to this thread with the process id could be in queue, and we
782 // don't want to dispatch a message before then since it will need the handle.
783 if (child_process_launcher_.get() && child_process_launcher_->IsStarting())
784 return false;
785
786 return widget_helper_->WaitForBackingStoreMsg(render_widget_id,
787 max_delay, msg);
788}
789
790void RenderProcessHostImpl::ReceivedBadMessage() {
791 if (run_renderer_in_process()) {
792 // In single process mode it is better if we don't suicide but just
793 // crash.
794 CHECK(false);
795 }
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100796 // We kill the renderer but don't include a NOTREACHED, because we want the
797 // browser to try to survive when it gets illegal messages from the renderer.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000798 base::KillProcess(GetHandle(), RESULT_CODE_KILLED_BAD_MESSAGE,
799 false);
800}
801
802void RenderProcessHostImpl::WidgetRestored() {
803 // Verify we were properly backgrounded.
804 DCHECK_EQ(backgrounded_, (visible_widgets_ == 0));
805 visible_widgets_++;
806 SetBackgrounded(false);
807}
808
809void RenderProcessHostImpl::WidgetHidden() {
810 // On startup, the browser will call Hide
811 if (backgrounded_)
812 return;
813
814 DCHECK_EQ(backgrounded_, (visible_widgets_ == 0));
815 visible_widgets_--;
816 DCHECK_GE(visible_widgets_, 0);
817 if (visible_widgets_ == 0) {
818 DCHECK(!backgrounded_);
819 SetBackgrounded(true);
820 }
821}
822
823int RenderProcessHostImpl::VisibleWidgetCount() const {
824 return visible_widgets_;
825}
826
827bool RenderProcessHostImpl::IsGuest() const {
828 return is_guest_;
829}
830
831StoragePartition* RenderProcessHostImpl::GetStoragePartition() const {
832 return storage_partition_impl_;
833}
834
835void RenderProcessHostImpl::AppendRendererCommandLine(
836 CommandLine* command_line) const {
837 // Pass the process type first, so it shows first in process listings.
838 command_line->AppendSwitchASCII(switches::kProcessType,
839 switches::kRendererProcess);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000840
841 // Now send any options from our own command line we want to propagate.
842 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
843 PropagateBrowserCommandLineToRenderer(browser_command_line, command_line);
844
845 // Pass on the browser locale.
846 const std::string locale =
847 GetContentClient()->browser()->GetApplicationLocale();
848 command_line->AppendSwitchASCII(switches::kLang, locale);
849
850 // If we run base::FieldTrials, we want to pass to their state to the
851 // renderer so that it can act in accordance with each state, or record
852 // histograms relating to the base::FieldTrial states.
853 std::string field_trial_states;
854 base::FieldTrialList::StatesToString(&field_trial_states);
855 if (!field_trial_states.empty()) {
856 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
857 field_trial_states);
858 }
859
860 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
861 command_line, GetID());
862
863 // Appending disable-gpu-feature switches due to software rendering list.
864 GpuDataManagerImpl* gpu_data_manager = GpuDataManagerImpl::GetInstance();
865 DCHECK(gpu_data_manager);
866 gpu_data_manager->AppendRendererCommandLine(command_line);
867}
868
869void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
870 const CommandLine& browser_cmd,
871 CommandLine* renderer_cmd) const {
872 // Propagate the following switches to the renderer command line (along
873 // with any associated values) if present in the browser command line.
874 static const char* const kSwitchNames[] = {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000875 switches::kAudioBufferSize,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000876 switches::kAuditAllHandles,
877 switches::kAuditHandles,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000878 switches::kDisable3DAPIs,
879 switches::kDisableAcceleratedCompositing,
880 switches::kDisableAcceleratedVideoDecode,
881 switches::kDisableApplicationCache,
882 switches::kDisableAudio,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000883 switches::kDisableBreakpad,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000884 switches::kDisableDatabases,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100885 switches::kDisableDelegatedRenderer,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000886 switches::kDisableDesktopNotifications,
887 switches::kDisableDeviceOrientation,
888 switches::kDisableFileSystem,
889 switches::kDisableGeolocation,
890 switches::kDisableGLMultisampling,
891 switches::kDisableGpuVsync,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100892 switches::kDisableGpu,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100893 switches::kDisableGpuCompositing,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000894 switches::kDisableHistogramCustomizer,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000895 switches::kDisableLocalStorage,
896 switches::kDisableLogging,
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100897 switches::kDisableNewDialogStyle,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000898 switches::kDisableSeccompFilterSandbox,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000899 switches::kDisableSessionStorage,
900 switches::kDisableSharedWorkers,
901 switches::kDisableSpeechInput,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100902 switches::kDisableTouchDragDrop,
903 switches::kDisableTouchEditing,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000904#if defined(OS_ANDROID)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000905 switches::kDisableWebRTC,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100906 switches::kEnableSpeechRecognition,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000907#endif
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100908 switches::kDisableWebAudio,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100909#if defined(ENABLE_WEBRTC)
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100910 switches::kDisableDeviceEnumeration,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100911 switches::kEnableSCTPDataChannels,
912#endif
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100913 switches::kEnableWebAnimationsCSS,
914 switches::kEnableWebAnimationsSVG,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100915 switches::kEnableWebMIDI,
916 switches::kEnableExperimentalCanvasFeatures,
917 switches::kEnableExperimentalWebSocket,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000918 switches::kDomAutomationController,
919 switches::kEnableAccessibilityLogging,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100920 switches::kEnableBeginFrameScheduling,
921 switches::kEnableBrowserInputController,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000922 switches::kEnableBrowserPluginForAllViewTypes,
923 switches::kEnableDCHECK,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000924 switches::kEnableDelegatedRenderer,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100925 switches::kEnableEncryptedMedia,
926 switches::kDisableLegacyEncryptedMedia,
Ben Murdocheb525c52013-07-10 11:40:50 +0100927 switches::kOverrideEncryptedMediaCanPlayType,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100928 switches::kEnableExperimentalWebPlatformFeatures,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000929 switches::kEnableFixedLayout,
930 switches::kEnableDeferredImageDecoding,
931 switches::kEnableGPUServiceLogging,
932 switches::kEnableGPUClientLogging,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000933 switches::kEnableGpuClientTracing,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000934 switches::kEnableGpuBenchmarking,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000935 switches::kEnableMemoryBenchmarking,
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100936 switches::kEnableSkiaBenchmarking,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000937 switches::kEnableLogging,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100938 switches::kEnableSpeechSynthesis,
939 switches::kEnableTouchDragDrop,
940 switches::kEnableTouchEditing,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100941#if defined(ENABLE_WEBRTC)
942 switches::kEnableWebRtcAecRecordings,
Ben Murdocheb525c52013-07-10 11:40:50 +0100943 switches::kEnableWebRtcTcpServerSocket,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100944 switches::kEnableWebRtcHWDecoding,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100945#endif
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100946 switches::kDisableWebKitMediaSource,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100947 switches::kEnableOverscrollNotifications,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000948 switches::kEnableStrictSiteIsolation,
949 switches::kDisableFullScreen,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000950 switches::kEnableNewDialogStyle,
951#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000952 switches::kEnablePepperTesting,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100953 switches::kDisablePepper3d,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000954#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000955 switches::kEnablePreparsedJsCaching,
956 switches::kEnablePruneGpuCommandBuffers,
957 switches::kEnablePinch,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100958 switches::kDisablePinch,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000959#if defined(OS_MACOSX)
960 // Allow this to be set when invoking the browser and relayed along.
961 switches::kEnableSandboxLogging,
962#endif
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100963 switches::kEnableSoftwareCompositing,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000964 switches::kEnableStatsTable,
965 switches::kEnableThreadedCompositing,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000966 switches::kEnableCompositingForFixedPosition,
967 switches::kEnableHighDpiCompositingForFixedPosition,
968 switches::kDisableCompositingForFixedPosition,
Ben Murdocheb525c52013-07-10 11:40:50 +0100969 switches::kEnableAcceleratedOverflowScroll,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100970 switches::kEnableCompositingForTransition,
971 switches::kDisableCompositingForTransition,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100972 switches::kEnableAcceleratedFixedRootBackground,
973 switches::kDisableAcceleratedFixedRootBackground,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000974 switches::kDisableThreadedCompositing,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000975 switches::kDisableTouchAdjustment,
976 switches::kDefaultTileWidth,
977 switches::kDefaultTileHeight,
978 switches::kMaxUntiledLayerWidth,
979 switches::kMaxUntiledLayerHeight,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000980 switches::kEnableViewport,
Ben Murdocheb525c52013-07-10 11:40:50 +0100981 switches::kEnableInbandTextTracks,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000982 switches::kEnableOpusPlayback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100983 switches::kEnableVp8AlphaPlayback,
984 switches::kEnableEac3Playback,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000985 switches::kForceDeviceScaleFactor,
986 switches::kFullMemoryCrashReport,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100987#if defined(OS_ANDROID)
988 switches::kHideScrollbars,
989#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000990#if !defined (GOOGLE_CHROME_BUILD)
991 // These are unsupported and not fully tested modes, so don't enable them
992 // for official Google Chrome builds.
993 switches::kInProcessPlugins,
994#endif // GOOGLE_CHROME_BUILD
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000995 switches::kJavaScriptFlags,
996 switches::kLoggingLevel,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000997 switches::kMemoryMetrics,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000998#if defined(OS_ANDROID)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000999 switches::kNetworkCountryIso,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001000 switches::kDisableGestureRequirementForMediaPlayback,
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +01001001#endif
1002#if defined(GOOGLE_TV)
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001003 switches::kUseExternalVideoSurfaceThresholdInPixels,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001004#endif
1005 switches::kNoReferrers,
1006 switches::kNoSandbox,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001007 switches::kEnableVtune,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001008 switches::kPpapiInProcess,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001009 switches::kRegisterPepperPlugins,
1010 switches::kRendererAssertTest,
1011#if defined(OS_POSIX)
1012 switches::kChildCleanExit,
1013#endif
1014 switches::kRendererStartupDialog,
1015 switches::kShowPaintRects,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001016 switches::kSitePerProcess,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001017 switches::kStatsCollectionController,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001018 switches::kTestSandbox,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001019 switches::kTouchEvents,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001020 switches::kTraceStartup,
1021 // This flag needs to be propagated to the renderer process for
1022 // --in-process-webgl.
1023 switches::kUseGL,
1024 switches::kUseMobileUserAgent,
1025 switches::kUserAgent,
1026 switches::kV,
1027 switches::kVideoThreads,
1028 switches::kVModule,
1029 switches::kWebCoreLogChannels,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001030 switches::kEnableWebGLDraftExtensions,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001031 switches::kTraceToConsole,
1032 switches::kEnableDeviceMotion,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +01001033#if defined(OS_ANDROID)
1034 switches::kDisableDeviceMotion,
1035#endif
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001036 // Please keep these in alphabetical order. Compositor switches here should
1037 // also be added to chrome/browser/chromeos/login/chrome_restart_request.cc.
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001038 cc::switches::kBackgroundColorInsteadOfCheckerboard,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001039 cc::switches::kCompositeToMailbox,
Ben Murdochca12bfa2013-07-23 11:17:05 +01001040 cc::switches::kDisableCompositedAntialiasing,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001041 cc::switches::kDisableImplSidePainting,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001042 cc::switches::kDisableThreadedAnimation,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001043 cc::switches::kEnableImplSidePainting,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001044 cc::switches::kEnablePartialSwap,
1045 cc::switches::kEnablePerTilePainting,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001046 cc::switches::kEnablePinchVirtualViewport,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001047 cc::switches::kEnableTopControlsPositionCalculation,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001048 cc::switches::kForceDirectLayerDrawing,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001049 cc::switches::kLowResolutionContentsScaleFactor,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001050 cc::switches::kMaxTilesForInterestArea,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001051 cc::switches::kMaxUnusedResourceMemoryUsagePercentage,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001052 cc::switches::kNumRasterThreads,
1053 cc::switches::kShowCompositedLayerBorders,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001054 cc::switches::kShowFPSCounter,
1055 cc::switches::kShowNonOccludingRects,
1056 cc::switches::kShowOccludingRects,
1057 cc::switches::kShowPropertyChangedRects,
1058 cc::switches::kShowReplicaScreenSpaceRects,
1059 cc::switches::kShowScreenSpaceRects,
1060 cc::switches::kShowSurfaceDamageRects,
1061 cc::switches::kSlowDownRasterScaleFactor,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001062 cc::switches::kStrictLayerPropertyChangeChecking,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001063 cc::switches::kTopControlsHeight,
1064 cc::switches::kTopControlsHideThreshold,
1065 cc::switches::kTopControlsShowThreshold,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001066 cc::switches::kTraceOverdraw,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001067 cc::switches::kUseMapImage,
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001068 };
1069 renderer_cmd->CopySwitchesFrom(browser_cmd, kSwitchNames,
1070 arraysize(kSwitchNames));
1071
1072 // Disable databases in incognito mode.
1073 if (GetBrowserContext()->IsOffTheRecord() &&
1074 !browser_cmd.HasSwitch(switches::kDisableDatabases)) {
1075 renderer_cmd->AppendSwitch(switches::kDisableDatabases);
1076#if defined(OS_ANDROID)
1077 renderer_cmd->AppendSwitch(switches::kDisableMediaHistoryLogging);
1078#endif
1079 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001080
1081 // Enforce the extra command line flags for impl-side painting.
1082 if (cc::switches::IsImplSidePaintingEnabled() &&
1083 !browser_cmd.HasSwitch(switches::kEnableDeferredImageDecoding))
1084 renderer_cmd->AppendSwitch(switches::kEnableDeferredImageDecoding);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001085}
1086
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001087base::ProcessHandle RenderProcessHostImpl::GetHandle() const {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001088 if (run_renderer_in_process())
1089 return base::Process::Current().handle();
1090
1091 if (!child_process_launcher_.get() || child_process_launcher_->IsStarting())
1092 return base::kNullProcessHandle;
1093
1094 return child_process_launcher_->GetHandle();
1095}
1096
1097bool RenderProcessHostImpl::FastShutdownIfPossible() {
1098 if (run_renderer_in_process())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001099 return false; // Single process mode never shutdown the renderer.
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001100
1101 if (!GetContentClient()->browser()->IsFastShutdownPossible())
1102 return false;
1103
1104 if (!child_process_launcher_.get() ||
1105 child_process_launcher_->IsStarting() ||
1106 !GetHandle())
1107 return false; // Render process hasn't started or is probably crashed.
1108
1109 // Test if there's an unload listener.
1110 // NOTE: It's possible that an onunload listener may be installed
1111 // while we're shutting down, so there's a small race here. Given that
1112 // the window is small, it's unlikely that the web page has much
1113 // state that will be lost by not calling its unload handlers properly.
1114 if (!SuddenTerminationAllowed())
1115 return false;
1116
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001117 ProcessDied(false /* already_dead */);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001118 fast_shutdown_started_ = true;
1119 return true;
1120}
1121
1122void RenderProcessHostImpl::DumpHandles() {
1123#if defined(OS_WIN)
1124 Send(new ChildProcessMsg_DumpHandles());
1125 return;
1126#endif
1127
1128 NOTIMPLEMENTED();
1129}
1130
1131// This is a platform specific function for mapping a transport DIB given its id
1132TransportDIB* RenderProcessHostImpl::MapTransportDIB(
1133 TransportDIB::Id dib_id) {
1134#if defined(OS_WIN)
1135 // On Windows we need to duplicate the handle from the remote process
1136 HANDLE section;
1137 DuplicateHandle(GetHandle(), dib_id.handle, GetCurrentProcess(), &section,
1138 STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ | FILE_MAP_WRITE,
1139 FALSE, 0);
1140 return TransportDIB::Map(section);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001141#elif defined(TOOLKIT_GTK)
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001142 return TransportDIB::Map(dib_id.shmkey);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001143#elif defined(OS_ANDROID)
1144 return TransportDIB::Map(dib_id);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001145#else
1146 // On POSIX, the browser allocates all DIBs and keeps a file descriptor around
1147 // for each.
1148 return widget_helper_->MapTransportDIB(dib_id);
1149#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001150}
1151
1152TransportDIB* RenderProcessHostImpl::GetTransportDIB(
1153 TransportDIB::Id dib_id) {
1154 if (!TransportDIB::is_valid_id(dib_id))
1155 return NULL;
1156
1157 const std::map<TransportDIB::Id, TransportDIB*>::iterator
1158 i = cached_dibs_.find(dib_id);
1159 if (i != cached_dibs_.end()) {
1160 cached_dibs_cleaner_.Reset();
1161 return i->second;
1162 }
1163
1164 TransportDIB* dib = MapTransportDIB(dib_id);
1165 if (!dib)
1166 return NULL;
1167
1168 if (cached_dibs_.size() >= MAX_MAPPED_TRANSPORT_DIBS) {
1169 // Clean a single entry from the cache
1170 std::map<TransportDIB::Id, TransportDIB*>::iterator smallest_iterator;
1171 size_t smallest_size = std::numeric_limits<size_t>::max();
1172
1173 for (std::map<TransportDIB::Id, TransportDIB*>::iterator
1174 i = cached_dibs_.begin(); i != cached_dibs_.end(); ++i) {
1175 if (i->second->size() <= smallest_size) {
1176 smallest_iterator = i;
1177 smallest_size = i->second->size();
1178 }
1179 }
1180
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001181#if defined(TOOLKIT_GTK)
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001182 smallest_iterator->second->Detach();
1183#else
1184 delete smallest_iterator->second;
1185#endif
1186 cached_dibs_.erase(smallest_iterator);
1187 }
1188
1189 cached_dibs_[dib_id] = dib;
1190 cached_dibs_cleaner_.Reset();
1191 return dib;
1192}
1193
1194void RenderProcessHostImpl::ClearTransportDIBCache() {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001195#if defined(TOOLKIT_GTK)
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001196 std::map<TransportDIB::Id, TransportDIB*>::const_iterator dib =
1197 cached_dibs_.begin();
1198 for (; dib != cached_dibs_.end(); ++dib)
1199 dib->second->Detach();
1200#else
1201 STLDeleteContainerPairSecondPointers(
1202 cached_dibs_.begin(), cached_dibs_.end());
1203#endif
1204 cached_dibs_.clear();
1205}
1206
1207bool RenderProcessHostImpl::Send(IPC::Message* msg) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001208 if (!channel_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001209 if (!is_initialized_) {
1210 queued_messages_.push(msg);
1211 return true;
1212 } else {
1213 delete msg;
1214 return false;
1215 }
1216 }
1217
1218 if (child_process_launcher_.get() && child_process_launcher_->IsStarting()) {
1219 queued_messages_.push(msg);
1220 return true;
1221 }
1222
1223 return channel_->Send(msg);
1224}
1225
1226bool RenderProcessHostImpl::OnMessageReceived(const IPC::Message& msg) {
1227 // If we're about to be deleted, or have initiated the fast shutdown sequence,
1228 // we ignore incoming messages.
1229
1230 if (deleting_soon_ || fast_shutdown_started_)
1231 return false;
1232
1233 mark_child_process_activity_time();
1234 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
1235 // Dispatch control messages.
1236 bool msg_is_ok = true;
1237 IPC_BEGIN_MESSAGE_MAP_EX(RenderProcessHostImpl, msg, msg_is_ok)
1238 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ShutdownRequest,
1239 OnShutdownRequest)
1240 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_DumpHandlesDone,
1241 OnDumpHandlesDone)
1242 IPC_MESSAGE_HANDLER(ViewHostMsg_SuddenTerminationChanged,
1243 SuddenTerminationChanged)
1244 IPC_MESSAGE_HANDLER(ViewHostMsg_UserMetricsRecordAction,
1245 OnUserMetricsRecordAction)
1246 IPC_MESSAGE_HANDLER(ViewHostMsg_SavedPageAsMHTML, OnSavedPageAsMHTML)
1247 // Adding single handlers for your service here is fine, but once your
1248 // service needs more than one handler, please extract them into a new
1249 // message filter and add that filter to CreateMessageFilters().
1250 IPC_MESSAGE_UNHANDLED_ERROR()
1251 IPC_END_MESSAGE_MAP_EX()
1252
1253 if (!msg_is_ok) {
1254 // The message had a handler, but its de-serialization failed.
1255 // We consider this a capital crime. Kill the renderer if we have one.
1256 LOG(ERROR) << "bad message " << msg.type() << " terminating renderer.";
1257 RecordAction(UserMetricsAction("BadMessageTerminate_BRPH"));
1258 ReceivedBadMessage();
1259 }
1260 return true;
1261 }
1262
Ben Murdocheb525c52013-07-10 11:40:50 +01001263 // Dispatch incoming messages to the appropriate IPC::Listener.
1264 IPC::Listener* listener = listeners_.Lookup(msg.routing_id());
1265 if (!listener) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001266 if (msg.is_sync()) {
1267 // The listener has gone away, so we must respond or else the caller will
1268 // hang waiting for a reply.
1269 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg);
1270 reply->set_reply_error();
1271 Send(reply);
1272 }
1273
1274 // If this is a SwapBuffers, we need to ack it if we're not going to handle
1275 // it so that the GPU process doesn't get stuck in unscheduled state.
1276 bool msg_is_ok = true;
1277 IPC_BEGIN_MESSAGE_MAP_EX(RenderProcessHostImpl, msg, msg_is_ok)
1278 IPC_MESSAGE_HANDLER(ViewHostMsg_CompositorSurfaceBuffersSwapped,
1279 OnCompositorSurfaceBuffersSwappedNoHost)
1280 IPC_END_MESSAGE_MAP_EX()
1281 return true;
1282 }
Ben Murdocheb525c52013-07-10 11:40:50 +01001283 return listener->OnMessageReceived(msg);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001284}
1285
1286void RenderProcessHostImpl::OnChannelConnected(int32 peer_pid) {
1287#if defined(IPC_MESSAGE_LOG_ENABLED)
1288 Send(new ChildProcessMsg_SetIPCLoggingEnabled(
1289 IPC::Logging::GetInstance()->Enabled()));
1290#endif
1291
1292 tracked_objects::ThreadData::Status status =
1293 tracked_objects::ThreadData::status();
1294 Send(new ChildProcessMsg_SetProfilerStatus(status));
1295}
1296
1297void RenderProcessHostImpl::OnChannelError() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001298 ProcessDied(true /* already_dead */);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001299}
1300
1301BrowserContext* RenderProcessHostImpl::GetBrowserContext() const {
1302 return browser_context_;
1303}
1304
1305bool RenderProcessHostImpl::InSameStoragePartition(
1306 StoragePartition* partition) const {
1307 return storage_partition_impl_ == partition;
1308}
1309
1310int RenderProcessHostImpl::GetID() const {
1311 return id_;
1312}
1313
1314bool RenderProcessHostImpl::HasConnection() const {
1315 return channel_.get() != NULL;
1316}
1317
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001318void RenderProcessHostImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1319 ignore_input_events_ = ignore_input_events;
1320}
1321
1322bool RenderProcessHostImpl::IgnoreInputEvents() const {
1323 return ignore_input_events_;
1324}
1325
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001326void RenderProcessHostImpl::Cleanup() {
1327 // When no other owners of this object, we can delete ourselves
Ben Murdocheb525c52013-07-10 11:40:50 +01001328 if (listeners_.IsEmpty()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001329 DCHECK_EQ(0, pending_views_);
1330 NotificationService::current()->Notify(
1331 NOTIFICATION_RENDERER_PROCESS_TERMINATED,
1332 Source<RenderProcessHost>(this),
1333 NotificationService::NoDetails());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001334
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001335 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001336 deleting_soon_ = true;
1337 // It's important not to wait for the DeleteTask to delete the channel
1338 // proxy. Kill it off now. That way, in case the profile is going away, the
1339 // rest of the objects attached to this RenderProcessHost start going
1340 // away first, since deleting the channel proxy will post a
1341 // OnChannelClosed() to IPC::ChannelProxy::Context on the IO thread.
1342 channel_.reset();
1343 gpu_message_filter_ = NULL;
1344
1345 // Remove ourself from the list of renderer processes so that we can't be
1346 // reused in between now and when the Delete task runs.
1347 UnregisterHost(GetID());
1348 }
1349}
1350
1351void RenderProcessHostImpl::AddPendingView() {
1352 pending_views_++;
1353}
1354
1355void RenderProcessHostImpl::RemovePendingView() {
1356 DCHECK(pending_views_);
1357 pending_views_--;
1358}
1359
1360void RenderProcessHostImpl::SetSuddenTerminationAllowed(bool enabled) {
1361 sudden_termination_allowed_ = enabled;
1362}
1363
1364bool RenderProcessHostImpl::SuddenTerminationAllowed() const {
1365 return sudden_termination_allowed_;
1366}
1367
1368base::TimeDelta RenderProcessHostImpl::GetChildProcessIdleTime() const {
1369 return base::TimeTicks::Now() - child_process_activity_time_;
1370}
1371
1372void RenderProcessHostImpl::SurfaceUpdated(int32 surface_id) {
1373 if (!gpu_message_filter_)
1374 return;
1375 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
1376 &GpuMessageFilter::SurfaceUpdated,
1377 gpu_message_filter_,
1378 surface_id));
1379}
1380
1381void RenderProcessHostImpl::ResumeRequestsForView(int route_id) {
1382 widget_helper_->ResumeRequestsForView(route_id);
1383}
1384
1385IPC::ChannelProxy* RenderProcessHostImpl::GetChannel() {
1386 return channel_.get();
1387}
1388
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001389bool RenderProcessHostImpl::FastShutdownForPageCount(size_t count) {
Ben Murdocheb525c52013-07-10 11:40:50 +01001390 if (static_cast<size_t>(GetActiveViewCount()) == count)
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001391 return FastShutdownIfPossible();
1392 return false;
1393}
1394
1395bool RenderProcessHostImpl::FastShutdownStarted() const {
1396 return fast_shutdown_started_;
1397}
1398
1399// static
1400void RenderProcessHostImpl::RegisterHost(int host_id, RenderProcessHost* host) {
1401 g_all_hosts.Get().AddWithID(host, host_id);
1402}
1403
1404// static
1405void RenderProcessHostImpl::UnregisterHost(int host_id) {
1406 RenderProcessHost* host = g_all_hosts.Get().Lookup(host_id);
1407 if (!host)
1408 return;
1409
1410 g_all_hosts.Get().Remove(host_id);
1411
1412 // Look up the map of site to process for the given browser_context,
1413 // in case we need to remove this process from it. It will be registered
1414 // under any sites it rendered that use process-per-site mode.
1415 SiteProcessMap* map =
1416 GetSiteProcessMapForBrowserContext(host->GetBrowserContext());
1417 map->RemoveProcess(host);
1418}
1419
1420// static
1421bool RenderProcessHostImpl::IsSuitableHost(
1422 RenderProcessHost* host,
1423 BrowserContext* browser_context,
1424 const GURL& site_url) {
1425 if (run_renderer_in_process())
1426 return true;
1427
1428 if (host->GetBrowserContext() != browser_context)
1429 return false;
1430
1431 // Check whether the given host and the intended site_url will be using the
1432 // same StoragePartition, since a RenderProcessHost can only support a single
1433 // StoragePartition. This is relevant for packaged apps, browser tags, and
1434 // isolated sites.
1435 StoragePartition* dest_partition =
1436 BrowserContext::GetStoragePartitionForSite(browser_context, site_url);
1437 if (!host->InSameStoragePartition(dest_partition))
1438 return false;
1439
1440 // All URLs are suitable if this is associated with a guest renderer process.
1441 // TODO(fsamuel, creis): Further validation is needed to ensure that only
1442 // normal web URLs are permitted in guest processes. We need to investigate
1443 // where this validation should happen.
1444 if (host->IsGuest())
1445 return true;
1446
1447 if (!host->IsGuest() && site_url.SchemeIs(chrome::kGuestScheme))
1448 return false;
1449
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001450 if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001451 host->GetID()) !=
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001452 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIBindingsForURL(
1453 browser_context, site_url)) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001454 return false;
1455 }
1456
1457 return GetContentClient()->browser()->IsSuitableHost(host, site_url);
1458}
1459
1460// static
1461bool RenderProcessHost::run_renderer_in_process() {
1462 return g_run_renderer_in_process_;
1463}
1464
1465// static
1466void RenderProcessHost::SetRunRendererInProcess(bool value) {
1467 g_run_renderer_in_process_ = value;
1468
1469 CommandLine* command_line = CommandLine::ForCurrentProcess();
1470 if (value && !command_line->HasSwitch(switches::kLang)) {
1471 // Modify the current process' command line to include the browser locale,
1472 // as the renderer expects this flag to be set.
1473 const std::string locale =
1474 GetContentClient()->browser()->GetApplicationLocale();
1475 command_line->AppendSwitchASCII(switches::kLang, locale);
1476 }
1477}
1478
1479RenderProcessHost::iterator RenderProcessHost::AllHostsIterator() {
1480 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1481 return iterator(g_all_hosts.Pointer());
1482}
1483
1484// static
1485RenderProcessHost* RenderProcessHost::FromID(int render_process_id) {
1486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1487 return g_all_hosts.Get().Lookup(render_process_id);
1488}
1489
1490// static
1491bool RenderProcessHost::ShouldTryToUseExistingProcessHost(
1492 BrowserContext* browser_context, const GURL& url) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001493 // Experimental:
1494 // If --enable-strict-site-isolation or --site-per-process is enabled, do not
1495 // try to reuse renderer processes when over the limit. (We could allow pages
1496 // from the same site to share, if we knew what the given process was
1497 // dedicated to. Allowing no sharing is simpler for now.) This may cause
1498 // resource exhaustion issues if too many sites are open at once.
1499 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1500 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation) ||
1501 command_line.HasSwitch(switches::kSitePerProcess))
1502 return false;
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001503
1504 if (run_renderer_in_process())
1505 return true;
1506
1507 // NOTE: Sometimes it's necessary to create more render processes than
1508 // GetMaxRendererProcessCount(), for instance when we want to create
1509 // a renderer process for a browser context that has no existing
1510 // renderers. This is OK in moderation, since the
1511 // GetMaxRendererProcessCount() is conservative.
1512 if (g_all_hosts.Get().size() >= GetMaxRendererProcessCount())
1513 return true;
1514
1515 return GetContentClient()->browser()->
1516 ShouldTryToUseExistingProcessHost(browser_context, url);
1517}
1518
1519// static
1520RenderProcessHost* RenderProcessHost::GetExistingProcessHost(
1521 BrowserContext* browser_context,
1522 const GURL& site_url) {
1523 // First figure out which existing renderers we can use.
1524 std::vector<RenderProcessHost*> suitable_renderers;
1525 suitable_renderers.reserve(g_all_hosts.Get().size());
1526
1527 iterator iter(AllHostsIterator());
1528 while (!iter.IsAtEnd()) {
1529 if (RenderProcessHostImpl::IsSuitableHost(
1530 iter.GetCurrentValue(),
1531 browser_context, site_url))
1532 suitable_renderers.push_back(iter.GetCurrentValue());
1533
1534 iter.Advance();
1535 }
1536
1537 // Now pick a random suitable renderer, if we have any.
1538 if (!suitable_renderers.empty()) {
1539 int suitable_count = static_cast<int>(suitable_renderers.size());
1540 int random_index = base::RandInt(0, suitable_count - 1);
1541 return suitable_renderers[random_index];
1542 }
1543
1544 return NULL;
1545}
1546
1547// static
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001548bool RenderProcessHost::ShouldUseProcessPerSite(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001549 BrowserContext* browser_context,
1550 const GURL& url) {
1551 // Returns true if we should use the process-per-site model. This will be
1552 // the case if the --process-per-site switch is specified, or in
1553 // process-per-site-instance for particular sites (e.g., WebUI).
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001554 // Note that --single-process is handled in ShouldTryToUseExistingProcessHost.
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001555 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1556 if (command_line.HasSwitch(switches::kProcessPerSite))
1557 return true;
1558
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001559 // We want to consolidate particular sites like WebUI even when we are using
1560 // the process-per-tab or process-per-site-instance models.
1561 // Note: DevTools pages have WebUI type but should not reuse the same host.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001562 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1563 browser_context, url) &&
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001564 !url.SchemeIs(chrome::kChromeDevToolsScheme)) {
1565 return true;
1566 }
1567
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001568 // Otherwise let the content client decide, defaulting to false.
1569 return GetContentClient()->browser()->ShouldUseProcessPerSite(browser_context,
1570 url);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001571}
1572
1573// static
1574RenderProcessHost* RenderProcessHostImpl::GetProcessHostForSite(
1575 BrowserContext* browser_context,
1576 const GURL& url) {
1577 // Look up the map of site to process for the given browser_context.
1578 SiteProcessMap* map =
1579 GetSiteProcessMapForBrowserContext(browser_context);
1580
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001581 // See if we have an existing process with appropriate bindings for this site.
1582 // If not, the caller should create a new process and register it.
1583 std::string site = SiteInstance::GetSiteForURL(browser_context, url)
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001584 .possibly_invalid_spec();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001585 RenderProcessHost* host = map->FindProcess(site);
1586 if (host && !IsSuitableHost(host, browser_context, url)) {
1587 // The registered process does not have an appropriate set of bindings for
1588 // the url. Remove it from the map so we can register a better one.
1589 RecordAction(UserMetricsAction("BindingsMismatch_GetProcessHostPerSite"));
1590 map->RemoveProcess(host);
1591 host = NULL;
1592 }
1593
1594 return host;
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001595}
1596
1597void RenderProcessHostImpl::RegisterProcessHostForSite(
1598 BrowserContext* browser_context,
1599 RenderProcessHost* process,
1600 const GURL& url) {
1601 // Look up the map of site to process for the given browser_context.
1602 SiteProcessMap* map =
1603 GetSiteProcessMapForBrowserContext(browser_context);
1604
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001605 // Only register valid, non-empty sites. Empty or invalid sites will not
1606 // use process-per-site mode. We cannot check whether the process has
1607 // appropriate bindings here, because the bindings have not yet been granted.
1608 std::string site = SiteInstance::GetSiteForURL(browser_context, url)
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001609 .possibly_invalid_spec();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001610 if (!site.empty())
1611 map->RegisterProcess(site, process);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001612}
1613
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001614base::MessageLoop*
1615 RenderProcessHostImpl::GetInProcessRendererThreadForTesting() {
1616 return g_in_process_thread;
1617}
1618
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001619void RenderProcessHostImpl::ProcessDied(bool already_dead) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001620 // Our child process has died. If we didn't expect it, it's a crash.
1621 // In any case, we need to let everyone know it's gone.
1622 // The OnChannelError notification can fire multiple times due to nested sync
1623 // calls to a renderer. If we don't have a valid channel here it means we
1624 // already handled the error.
1625
1626 // child_process_launcher_ can be NULL in single process mode or if fast
1627 // termination happened.
1628 int exit_code = 0;
1629 base::TerminationStatus status =
1630 child_process_launcher_.get() ?
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001631 child_process_launcher_->GetChildTerminationStatus(already_dead,
1632 &exit_code) :
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001633 base::TERMINATION_STATUS_NORMAL_TERMINATION;
1634
1635 RendererClosedDetails details(GetHandle(), status, exit_code);
1636 NotificationService::current()->Notify(
1637 NOTIFICATION_RENDERER_PROCESS_CLOSED,
1638 Source<RenderProcessHost>(this),
1639 Details<RendererClosedDetails>(&details));
1640
1641 child_process_launcher_.reset();
1642 channel_.reset();
1643 gpu_message_filter_ = NULL;
1644
Ben Murdocheb525c52013-07-10 11:40:50 +01001645 IDMap<IPC::Listener>::iterator iter(&listeners_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001646 while (!iter.IsAtEnd()) {
Ben Murdocheb525c52013-07-10 11:40:50 +01001647 iter.GetCurrentValue()->OnMessageReceived(
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001648 ViewHostMsg_RenderProcessGone(iter.GetCurrentKey(),
1649 static_cast<int>(status),
1650 exit_code));
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001651 iter.Advance();
1652 }
1653
1654 ClearTransportDIBCache();
1655
1656 // this object is not deleted at this point and may be reused later.
1657 // TODO(darin): clean this up
1658}
1659
1660int RenderProcessHostImpl::GetActiveViewCount() {
1661 int num_active_views = 0;
Ben Murdocheb525c52013-07-10 11:40:50 +01001662 RenderWidgetHost::List widgets = RenderWidgetHost::GetRenderWidgetHosts();
1663 for (size_t i = 0; i < widgets.size(); ++i) {
1664 // Count only RenderWidgetHosts in this process.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001665 if (widgets[i]->GetProcess()->GetID() == GetID())
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001666 num_active_views++;
1667 }
1668 return num_active_views;
1669}
1670
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001671// Frame subscription API for this class is for accelerated composited path
1672// only. These calls are redirected to GpuMessageFilter.
1673void RenderProcessHostImpl::BeginFrameSubscription(
1674 int route_id,
1675 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) {
1676 if (!gpu_message_filter_)
1677 return;
1678 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
1679 &GpuMessageFilter::BeginFrameSubscription,
1680 gpu_message_filter_,
1681 route_id, base::Passed(&subscriber)));
1682}
1683
1684void RenderProcessHostImpl::EndFrameSubscription(int route_id) {
1685 if (!gpu_message_filter_)
1686 return;
1687 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
1688 &GpuMessageFilter::EndFrameSubscription,
1689 gpu_message_filter_,
1690 route_id));
1691}
1692
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001693void RenderProcessHostImpl::OnShutdownRequest() {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001694 // Don't shut down if there are active RenderViews, or if there are pending
1695 // RenderViews being swapped back in.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001696 // In single process mode, we never shutdown the renderer.
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001697 int num_active_views = GetActiveViewCount();
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001698 if (pending_views_ || num_active_views > 0 || run_renderer_in_process())
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001699 return;
1700
1701 // Notify any contents that might have swapped out renderers from this
1702 // process. They should not attempt to swap them back in.
1703 NotificationService::current()->Notify(
1704 NOTIFICATION_RENDERER_PROCESS_CLOSING,
1705 Source<RenderProcessHost>(this),
1706 NotificationService::NoDetails());
1707
1708 Send(new ChildProcessMsg_Shutdown());
1709}
1710
1711void RenderProcessHostImpl::SuddenTerminationChanged(bool enabled) {
1712 SetSuddenTerminationAllowed(enabled);
1713}
1714
1715void RenderProcessHostImpl::OnDumpHandlesDone() {
1716 Cleanup();
1717}
1718
1719void RenderProcessHostImpl::SetBackgrounded(bool backgrounded) {
1720 // Note: we always set the backgrounded_ value. If the process is NULL
1721 // (and hence hasn't been created yet), we will set the process priority
1722 // later when we create the process.
1723 backgrounded_ = backgrounded;
1724 if (!child_process_launcher_.get() || child_process_launcher_->IsStarting())
1725 return;
1726
1727#if defined(OS_WIN)
1728 // The cbstext.dll loads as a global GetMessage hook in the browser process
1729 // and intercepts/unintercepts the kernel32 API SetPriorityClass in a
1730 // background thread. If the UI thread invokes this API just when it is
1731 // intercepted the stack is messed up on return from the interceptor
1732 // which causes random crashes in the browser process. Our hack for now
1733 // is to not invoke the SetPriorityClass API if the dll is loaded.
1734 if (GetModuleHandle(L"cbstext.dll"))
1735 return;
1736#endif // OS_WIN
1737
1738 child_process_launcher_->SetProcessBackgrounded(backgrounded);
1739}
1740
1741void RenderProcessHostImpl::OnProcessLaunched() {
1742 // No point doing anything, since this object will be destructed soon. We
1743 // especially don't want to send the RENDERER_PROCESS_CREATED notification,
1744 // since some clients might expect a RENDERER_PROCESS_TERMINATED afterwards to
1745 // properly cleanup.
1746 if (deleting_soon_)
1747 return;
1748
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001749 if (child_process_launcher_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001750 if (!child_process_launcher_->GetHandle()) {
1751 OnChannelError();
1752 return;
1753 }
1754
1755 child_process_launcher_->SetProcessBackgrounded(backgrounded_);
1756 }
1757
1758 // NOTE: This needs to be before sending queued messages because
1759 // ExtensionService uses this notification to initialize the renderer process
1760 // with state that must be there before any JavaScript executes.
1761 //
1762 // The queued messages contain such things as "navigate". If this notification
1763 // was after, we can end up executing JavaScript before the initialization
1764 // happens.
1765 NotificationService::current()->Notify(
1766 NOTIFICATION_RENDERER_PROCESS_CREATED,
1767 Source<RenderProcessHost>(this),
1768 NotificationService::NoDetails());
1769
1770 while (!queued_messages_.empty()) {
1771 Send(queued_messages_.front());
1772 queued_messages_.pop();
1773 }
1774}
1775
1776void RenderProcessHostImpl::OnUserMetricsRecordAction(
1777 const std::string& action) {
1778 RecordComputedAction(action);
1779}
1780
1781void RenderProcessHostImpl::OnSavedPageAsMHTML(int job_id, int64 data_size) {
1782 MHTMLGenerationManager::GetInstance()->MHTMLGenerated(job_id, data_size);
1783}
1784
1785void RenderProcessHostImpl::OnCompositorSurfaceBuffersSwappedNoHost(
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001786 const ViewHostMsg_CompositorSurfaceBuffersSwapped_Params& params) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001787 TRACE_EVENT0("renderer_host",
1788 "RenderWidgetHostImpl::OnCompositorSurfaceBuffersSwappedNoHost");
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001789 AcceleratedSurfaceMsg_BufferPresented_Params ack_params;
1790 ack_params.sync_point = 0;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001791 RenderWidgetHostImpl::AcknowledgeBufferPresent(params.route_id,
1792 params.gpu_process_host_id,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001793 ack_params);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001794}
1795
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001796void RenderProcessHostImpl::OnGpuSwitching() {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001797 // We are updating all widgets including swapped out ones.
1798 RenderWidgetHost::List widgets =
1799 RenderWidgetHostImpl::GetAllRenderWidgetHosts();
Ben Murdocheb525c52013-07-10 11:40:50 +01001800 for (size_t i = 0; i < widgets.size(); ++i) {
1801 if (!widgets[i]->IsRenderView())
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001802 continue;
1803
Ben Murdocheb525c52013-07-10 11:40:50 +01001804 // Skip widgets in other processes.
1805 if (widgets[i]->GetProcess()->GetID() != GetID())
1806 continue;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001807
Ben Murdocheb525c52013-07-10 11:40:50 +01001808 RenderViewHost* rvh = RenderViewHost::From(widgets[i]);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001809 rvh->UpdateWebkitPreferences(rvh->GetWebkitPreferences());
1810 }
1811}
1812
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001813} // namespace content