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 // Represents the browser side of the browser <--> renderer communication
6 // channel. There will be one RenderProcessHost per renderer process.
8 #include "content/browser/renderer_host/render_process_host_impl.h"
15 #include <utility> // for pair<>
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"
30 #include "base/process_util.h"
31 #include "base/rand_util.h"
32 #include "base/stl_util.h"
33 #include "base/string_util.h"
34 #include "base/supports_user_data.h"
35 #include "base/sys_info.h"
36 #include "base/threading/thread.h"
37 #include "base/threading/thread_restrictions.h"
38 #include "base/tracked_objects.h"
39 #include "cc/switches.h"
40 #include "content/browser/appcache/appcache_dispatcher_host.h"
41 #include "content/browser/appcache/chrome_appcache_service.h"
42 #include "content/browser/browser_main.h"
43 #include "content/browser/browser_main_loop.h"
44 #include "content/browser/browser_plugin/browser_plugin_message_filter.h"
45 #include "content/browser/child_process_security_policy_impl.h"
46 #include "content/browser/device_orientation/orientation_message_filter.h"
47 #include "content/browser/dom_storage/dom_storage_context_impl.h"
48 #include "content/browser/dom_storage/dom_storage_message_filter.h"
49 #include "content/browser/download/mhtml_generation_manager.h"
50 #include "content/browser/fileapi/chrome_blob_storage_context.h"
51 #include "content/browser/fileapi/fileapi_message_filter.h"
52 #include "content/browser/geolocation/geolocation_dispatcher_host.h"
53 #include "content/browser/gpu/gpu_data_manager_impl.h"
54 #include "content/browser/gpu/gpu_process_host.h"
55 #include "content/browser/histogram_message_filter.h"
56 #include "content/browser/hyphenator/hyphenator_message_filter.h"
57 #include "content/browser/in_process_webkit/indexed_db_context_impl.h"
58 #include "content/browser/in_process_webkit/indexed_db_dispatcher_host.h"
59 #include "content/browser/loader/resource_message_filter.h"
60 #include "content/browser/loader/resource_scheduler_filter.h"
61 #include "content/browser/media/media_internals.h"
62 #include "content/browser/mime_registry_message_filter.h"
63 #include "content/browser/plugin_service_impl.h"
64 #include "content/browser/profiler_message_filter.h"
65 #include "content/browser/renderer_host/clipboard_message_filter.h"
66 #include "content/browser/renderer_host/database_message_filter.h"
67 #include "content/browser/renderer_host/file_utilities_message_filter.h"
68 #include "content/browser/renderer_host/gamepad_browser_message_filter.h"
69 #include "content/browser/renderer_host/gpu_message_filter.h"
70 #include "content/browser/renderer_host/media/audio_input_renderer_host.h"
71 #include "content/browser/renderer_host/media/audio_mirroring_manager.h"
72 #include "content/browser/renderer_host/media/audio_renderer_host.h"
73 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
74 #include "content/browser/renderer_host/media/peer_connection_tracker_host.h"
75 #include "content/browser/renderer_host/media/video_capture_host.h"
76 #include "content/browser/renderer_host/p2p/socket_dispatcher_host.h"
77 #include "content/browser/renderer_host/pepper/pepper_message_filter.h"
78 #include "content/browser/renderer_host/quota_dispatcher_host.h"
79 #include "content/browser/renderer_host/render_message_filter.h"
80 #include "content/browser/renderer_host/render_view_host_delegate.h"
81 #include "content/browser/renderer_host/render_view_host_impl.h"
82 #include "content/browser/renderer_host/render_widget_helper.h"
83 #include "content/browser/renderer_host/socket_stream_dispatcher_host.h"
84 #include "content/browser/renderer_host/text_input_client_message_filter.h"
85 #include "content/browser/resolve_proxy_msg_helper.h"
86 #include "content/browser/storage_partition_impl.h"
87 #include "content/browser/speech/input_tag_speech_dispatcher_host.h"
88 #include "content/browser/speech/speech_recognition_dispatcher_host.h"
89 #include "content/browser/tracing/trace_message_filter.h"
90 #include "content/browser/webui/web_ui_controller_factory_registry.h"
91 #include "content/browser/worker_host/worker_storage_partition.h"
92 #include "content/browser/worker_host/worker_message_filter.h"
93 #include "content/common/child_process_host_impl.h"
94 #include "content/common/child_process_messages.h"
95 #include "content/common/gpu/gpu_messages.h"
96 #include "content/common/resource_messages.h"
97 #include "content/common/view_messages.h"
98 #include "content/public/browser/browser_context.h"
99 #include "content/public/browser/content_browser_client.h"
100 #include "content/public/browser/notification_service.h"
101 #include "content/public/browser/notification_types.h"
102 #include "content/public/browser/render_process_host_factory.h"
103 #include "content/public/browser/resource_context.h"
104 #include "content/public/browser/user_metrics.h"
105 #include "content/public/common/content_constants.h"
106 #include "content/public/common/content_switches.h"
107 #include "content/public/common/process_type.h"
108 #include "content/public/common/result_codes.h"
109 #include "content/public/common/url_constants.h"
110 #include "content/renderer/render_process_impl.h"
111 #include "content/renderer/render_thread_impl.h"
112 #include "ipc/ipc_channel.h"
113 #include "ipc/ipc_logging.h"
114 #include "ipc/ipc_platform_file.h"
115 #include "ipc/ipc_switches.h"
116 #include "ipc/ipc_sync_channel.h"
117 #include "media/base/media_switches.h"
118 #include "net/url_request/url_request_context_getter.h"
119 #include "ppapi/shared_impl/ppapi_switches.h"
120 #include "ui/base/ui_base_switches.h"
121 #include "ui/gl/gl_switches.h"
122 #include "webkit/fileapi/sandbox_mount_point_provider.h"
123 #include "webkit/glue/resource_type.h"
124 #include "webkit/plugins/plugin_switches.h"
127 #include "base/win/scoped_com_initializer.h"
128 #include "content/common/font_cache_dispatcher_win.h"
131 #include "third_party/skia/include/core/SkBitmap.h"
133 extern bool g_exited_main_message_loop
;
135 static const char* kSiteProcessMapKeyName
= "content_site_process_map";
139 // This class creates the IO thread for the renderer when running in
140 // single-process mode. It's not used in multi-process mode.
141 class RendererMainThread
: public base::Thread
{
143 explicit RendererMainThread(const std::string
& channel_id
)
144 : Thread("Chrome_InProcRendererThread"),
145 channel_id_(channel_id
) {
148 virtual ~RendererMainThread() {
153 virtual void Init() OVERRIDE
{
154 render_process_
.reset(new RenderProcessImpl());
155 new RenderThreadImpl(channel_id_
);
158 virtual void CleanUp() OVERRIDE
{
159 render_process_
.reset();
161 // It's a little lame to manually set this flag. But the single process
162 // RendererThread will receive the WM_QUIT. We don't need to assert on
163 // this thread, so just force the flag manually.
164 // If we want to avoid this, we could create the InProcRendererThread
165 // directly with _beginthreadex() rather than using the Thread class.
166 // We used to set this flag in the Init function above. However there
167 // other threads like WebThread which are created by this thread
168 // which resets this flag. Please see Thread::StartWithOptions. Setting
169 // this flag to true in Cleanup works around these problems.
170 SetThreadWasQuitProperly(true);
174 std::string channel_id_
;
175 scoped_ptr
<RenderProcess
> render_process_
;
177 DISALLOW_COPY_AND_ASSIGN(RendererMainThread
);
182 // Helper class that we pass to ResourceMessageFilter so that it can find the
183 // right net::URLRequestContext for a request.
184 class RendererURLRequestContextSelector
185 : public ResourceMessageFilter::URLRequestContextSelector
{
187 RendererURLRequestContextSelector(BrowserContext
* browser_context
,
189 : request_context_(browser_context
->GetRequestContextForRenderProcess(
191 media_request_context_(
192 browser_context
->GetMediaRequestContextForRenderProcess(
196 virtual net::URLRequestContext
* GetRequestContext(
197 ResourceType::Type resource_type
) OVERRIDE
{
198 net::URLRequestContextGetter
* request_context
= request_context_
;
199 // If the request has resource type of ResourceType::MEDIA, we use a request
200 // context specific to media for handling it because these resources have
201 // specific needs for caching.
202 if (resource_type
== ResourceType::MEDIA
)
203 request_context
= media_request_context_
;
204 return request_context
->GetURLRequestContext();
208 virtual ~RendererURLRequestContextSelector() {}
210 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
211 scoped_refptr
<net::URLRequestContextGetter
> media_request_context_
;
214 // the global list of all renderer processes
215 base::LazyInstance
<IDMap
<RenderProcessHost
> >::Leaky
216 g_all_hosts
= LAZY_INSTANCE_INITIALIZER
;
218 // Map of site to process, to ensure we only have one RenderProcessHost per
219 // site in process-per-site mode. Each map is specific to a BrowserContext.
220 class SiteProcessMap
: public base::SupportsUserData::Data
{
222 typedef base::hash_map
<std::string
, RenderProcessHost
*> SiteToProcessMap
;
225 void RegisterProcess(const std::string
& site
, RenderProcessHost
* process
) {
226 map_
[site
] = process
;
229 RenderProcessHost
* FindProcess(const std::string
& site
) {
230 SiteToProcessMap::iterator i
= map_
.find(site
);
236 void RemoveProcess(RenderProcessHost
* host
) {
237 // Find all instances of this process in the map, then separately remove
239 std::set
<std::string
> sites
;
240 for (SiteToProcessMap::const_iterator i
= map_
.begin();
243 if (i
->second
== host
)
244 sites
.insert(i
->first
);
246 for (std::set
<std::string
>::iterator i
= sites
.begin();
249 SiteToProcessMap::iterator iter
= map_
.find(*i
);
250 if (iter
!= map_
.end()) {
251 DCHECK_EQ(iter
->second
, host
);
258 SiteToProcessMap map_
;
261 // Find the SiteProcessMap specific to the given context.
262 SiteProcessMap
* GetSiteProcessMapForBrowserContext(BrowserContext
* context
) {
264 SiteProcessMap
* map
= static_cast<SiteProcessMap
*>(
265 context
->GetUserData(kSiteProcessMapKeyName
));
267 map
= new SiteProcessMap();
268 context
->SetUserData(kSiteProcessMapKeyName
, map
);
275 // Stores the maximum number of renderer processes the content module can
277 static size_t g_max_renderer_count_override
= 0;
280 size_t RenderProcessHost::GetMaxRendererProcessCount() {
281 if (g_max_renderer_count_override
)
282 return g_max_renderer_count_override
;
284 // Defines the maximum number of renderer processes according to the
285 // amount of installed memory as reported by the OS. The calculation
286 // assumes that you want the renderers to use half of the installed
287 // RAM and assuming that each WebContents uses ~40MB.
288 // If you modify this assumption, you need to adjust the
289 // ThirtyFourTabs test to match the expected number of processes.
291 // With the given amounts of installed memory below on a 32-bit CPU,
292 // the maximum renderer count will roughly be as follows:
298 // 16384 MB -> 82 (kMaxRendererProcessCount)
300 static size_t max_count
= 0;
302 const size_t kEstimatedWebContentsMemoryUsage
=
303 #if defined(ARCH_CPU_64_BITS)
308 max_count
= base::SysInfo::AmountOfPhysicalMemoryMB() / 2;
309 max_count
/= kEstimatedWebContentsMemoryUsage
;
311 const size_t kMinRendererProcessCount
= 3;
312 max_count
= std::max(max_count
, kMinRendererProcessCount
);
313 max_count
= std::min(max_count
, kMaxRendererProcessCount
);
319 bool g_run_renderer_in_process_
= false;
322 void RenderProcessHost::SetMaxRendererProcessCount(size_t count
) {
323 g_max_renderer_count_override
= count
;
326 RenderProcessHostImpl::RenderProcessHostImpl(
327 BrowserContext
* browser_context
,
328 StoragePartitionImpl
* storage_partition_impl
,
330 : fast_shutdown_started_(false),
331 deleting_soon_(false),
335 ALLOW_THIS_IN_INITIALIZER_LIST(cached_dibs_cleaner_(
336 FROM_HERE
, base::TimeDelta::FromSeconds(5),
337 this, &RenderProcessHostImpl::ClearTransportDIBCache
)),
338 is_initialized_(false),
339 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
340 browser_context_(browser_context
),
341 storage_partition_impl_(storage_partition_impl
),
342 sudden_termination_allowed_(true),
343 ignore_input_events_(false),
344 #if defined(OS_ANDROID)
345 dummy_shutdown_event_(false, false),
347 is_guest_(is_guest
) {
348 widget_helper_
= new RenderWidgetHelper();
350 ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID());
352 CHECK(!g_exited_main_message_loop
);
353 RegisterHost(GetID(), this);
354 g_all_hosts
.Get().set_check_on_null_data(true);
355 // Initialize |child_process_activity_time_| to a reasonable value.
356 mark_child_process_activity_time();
357 // Note: When we create the RenderProcessHostImpl, it's technically
358 // backgrounded, because it has no visible listeners. But the process
359 // doesn't actually exist yet, so we'll Background it later, after
363 RenderProcessHostImpl::~RenderProcessHostImpl() {
364 DCHECK(!run_renderer_in_process());
365 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID());
367 // We may have some unsent messages at this point, but that's OK.
369 while (!queued_messages_
.empty()) {
370 delete queued_messages_
.front();
371 queued_messages_
.pop();
374 ClearTransportDIBCache();
375 UnregisterHost(GetID());
378 void RenderProcessHostImpl::EnableSendQueue() {
379 is_initialized_
= false;
382 bool RenderProcessHostImpl::Init() {
383 // calling Init() more than once does nothing, this makes it more convenient
384 // for the view host which may not be sure in some cases
388 CommandLine::StringType renderer_prefix
;
389 #if defined(OS_POSIX)
390 // A command prefix is something prepended to the command line of the spawned
391 // process. It is supported only on POSIX systems.
392 const CommandLine
& browser_command_line
= *CommandLine::ForCurrentProcess();
394 browser_command_line
.GetSwitchValueNative(switches::kRendererCmdPrefix
);
395 #endif // defined(OS_POSIX)
397 #if defined(OS_LINUX)
398 int flags
= renderer_prefix
.empty() ? ChildProcessHost::CHILD_ALLOW_SELF
:
399 ChildProcessHost::CHILD_NORMAL
;
401 int flags
= ChildProcessHost::CHILD_NORMAL
;
404 // Find the renderer before creating the channel so if this fails early we
405 // return without creating the channel.
406 base::FilePath renderer_path
= ChildProcessHost::GetChildPath(flags
);
407 if (renderer_path
.empty())
410 // Setup the IPC channel.
411 const std::string channel_id
=
412 IPC::Channel::GenerateVerifiedChannelID(std::string());
414 #if defined(OS_ANDROID)
415 // Android WebView needs to be able to wait from the UI thread to support
416 // the synchronous legacy APIs.
417 browser_command_line
.HasSwitch(switches::kEnableWebViewSynchronousAPIs
) ?
418 new IPC::SyncChannel(
419 channel_id
, IPC::Channel::MODE_SERVER
, this,
420 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
421 true, &dummy_shutdown_event_
) :
423 new IPC::ChannelProxy(
424 channel_id
, IPC::Channel::MODE_SERVER
, this,
425 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
427 // Call the embedder first so that their IPC filters have priority.
428 GetContentClient()->browser()->RenderProcessHostCreated(this);
430 CreateMessageFilters();
432 if (run_renderer_in_process()) {
433 // Crank up a thread and run the initialization there. With the way that
434 // messages flow between the browser and renderer, this thread is required
435 // to prevent a deadlock in single-process mode. Since the primordial
436 // thread in the renderer process runs the WebKit code and can sometimes
437 // make blocking calls to the UI thread (i.e. this thread), they need to run
438 // on separate threads.
439 in_process_renderer_
.reset(new RendererMainThread(channel_id
));
441 base::Thread::Options options
;
442 #if !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
443 // In-process plugins require this to be a UI message loop.
444 options
.message_loop_type
= MessageLoop::TYPE_UI
;
446 // We can't have multiple UI loops on GTK and Android, so we don't support
447 // in-process plugins.
448 options
.message_loop_type
= MessageLoop::TYPE_DEFAULT
;
450 in_process_renderer_
->StartWithOptions(options
);
452 OnProcessLaunched(); // Fake a callback that the process is ready.
454 // Build command line for renderer. We call AppendRendererCommandLine()
455 // first so the process type argument will appear first.
456 CommandLine
* cmd_line
= new CommandLine(renderer_path
);
457 if (!renderer_prefix
.empty())
458 cmd_line
->PrependWrapper(renderer_prefix
);
459 AppendRendererCommandLine(cmd_line
);
460 cmd_line
->AppendSwitchASCII(switches::kProcessChannelID
, channel_id
);
462 // Spawn the child process asynchronously to avoid blocking the UI thread.
463 // As long as there's no renderer prefix, we can use the zygote process
465 child_process_launcher_
.reset(new ChildProcessLauncher(
468 #elif defined(OS_POSIX)
469 renderer_prefix
.empty(),
470 base::EnvironmentVector(),
471 channel_
->TakeClientFileDescriptor(),
477 fast_shutdown_started_
= false;
480 is_initialized_
= true;
484 void RenderProcessHostImpl::CreateMessageFilters() {
485 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
486 channel_
->AddFilter(new ResourceSchedulerFilter(GetID()));
487 MediaInternals
* media_internals
= MediaInternals::GetInstance();;
488 // Add BrowserPluginMessageFilter to ensure it gets the first stab at messages
490 scoped_refptr
<BrowserPluginMessageFilter
> bp_message_filter(
491 new BrowserPluginMessageFilter(GetID(), IsGuest()));
492 channel_
->AddFilter(bp_message_filter
);
494 scoped_refptr
<RenderMessageFilter
> render_message_filter(
495 new RenderMessageFilter(
497 #if defined(ENABLE_PLUGINS)
498 PluginServiceImpl::GetInstance(),
503 GetBrowserContext()->GetRequestContextForRenderProcess(GetID()),
506 storage_partition_impl_
->GetDOMStorageContext()));
507 channel_
->AddFilter(render_message_filter
);
508 BrowserContext
* browser_context
= GetBrowserContext();
509 ResourceContext
* resource_context
= browser_context
->GetResourceContext();
511 ResourceMessageFilter
* resource_message_filter
= new ResourceMessageFilter(
512 GetID(), PROCESS_TYPE_RENDERER
, resource_context
,
513 storage_partition_impl_
->GetAppCacheService(),
514 ChromeBlobStorageContext::GetFor(browser_context
),
515 storage_partition_impl_
->GetFileSystemContext(),
516 new RendererURLRequestContextSelector(browser_context
, GetID()));
518 channel_
->AddFilter(resource_message_filter
);
519 media::AudioManager
* audio_manager
= BrowserMainLoop::GetAudioManager();
520 MediaStreamManager
* media_stream_manager
=
521 BrowserMainLoop::GetMediaStreamManager();
522 channel_
->AddFilter(new AudioInputRendererHost(audio_manager
,
523 media_stream_manager
));
524 channel_
->AddFilter(new AudioRendererHost(
525 GetID(), audio_manager
, BrowserMainLoop::GetAudioMirroringManager(),
527 channel_
->AddFilter(new VideoCaptureHost());
528 channel_
->AddFilter(new AppCacheDispatcherHost(
529 storage_partition_impl_
->GetAppCacheService(),
531 channel_
->AddFilter(new ClipboardMessageFilter(browser_context
));
533 new DOMStorageMessageFilter(
535 storage_partition_impl_
->GetDOMStorageContext()));
537 new IndexedDBDispatcherHost(
539 storage_partition_impl_
->GetIndexedDBContext()));
540 channel_
->AddFilter(GeolocationDispatcherHost::New(
541 GetID(), browser_context
->GetGeolocationPermissionContext()));
542 gpu_message_filter_
= new GpuMessageFilter(GetID(), widget_helper_
.get());
543 channel_
->AddFilter(gpu_message_filter_
);
544 #if defined(ENABLE_WEBRTC)
545 peer_connection_tracker_host_
= new PeerConnectionTrackerHost(GetID());
546 channel_
->AddFilter(peer_connection_tracker_host_
);
547 channel_
->AddFilter(new MediaStreamDispatcherHost(GetID()));
549 #if defined(ENABLE_PLUGINS)
550 channel_
->AddFilter(new PepperMessageFilter(PROCESS_TYPE_RENDERER
,
551 GetID(), browser_context
));
553 #if defined(ENABLE_INPUT_SPEECH)
554 channel_
->AddFilter(new InputTagSpeechDispatcherHost(
555 IsGuest(), GetID(), storage_partition_impl_
->GetURLRequestContext(),
556 browser_context
->GetSpeechRecognitionPreferences()));
557 channel_
->AddFilter(new SpeechRecognitionDispatcherHost(
558 GetID(), storage_partition_impl_
->GetURLRequestContext(),
559 browser_context
->GetSpeechRecognitionPreferences()));
561 channel_
->AddFilter(new FileAPIMessageFilter(
563 storage_partition_impl_
->GetURLRequestContext(),
564 storage_partition_impl_
->GetFileSystemContext(),
565 ChromeBlobStorageContext::GetFor(browser_context
)));
566 channel_
->AddFilter(new OrientationMessageFilter());
567 channel_
->AddFilter(new FileUtilitiesMessageFilter(GetID()));
568 channel_
->AddFilter(new MimeRegistryMessageFilter());
569 channel_
->AddFilter(new DatabaseMessageFilter(
570 storage_partition_impl_
->GetDatabaseTracker()));
571 #if defined(OS_MACOSX)
572 channel_
->AddFilter(new TextInputClientMessageFilter(GetID()));
573 #elif defined(OS_WIN)
574 channel_
->AddFilter(new FontCacheDispatcher());
577 SocketStreamDispatcherHost
* socket_stream_dispatcher_host
=
578 new SocketStreamDispatcherHost(GetID(),
579 new RendererURLRequestContextSelector(browser_context
, GetID()),
581 channel_
->AddFilter(socket_stream_dispatcher_host
);
584 new WorkerMessageFilter(
587 WorkerStoragePartition(
588 storage_partition_impl_
->GetURLRequestContext(),
589 storage_partition_impl_
->GetMediaURLRequestContext(),
590 storage_partition_impl_
->GetAppCacheService(),
591 storage_partition_impl_
->GetFileSystemContext(),
592 storage_partition_impl_
->GetDatabaseTracker(),
593 storage_partition_impl_
->GetIndexedDBContext()),
594 base::Bind(&RenderWidgetHelper::GetNextRoutingID
,
595 base::Unretained(widget_helper_
.get()))));
597 #if defined(ENABLE_WEBRTC)
598 channel_
->AddFilter(new P2PSocketDispatcherHost(resource_context
));
601 channel_
->AddFilter(new TraceMessageFilter());
602 channel_
->AddFilter(new ResolveProxyMsgHelper(
603 browser_context
->GetRequestContextForRenderProcess(GetID())));
604 channel_
->AddFilter(new QuotaDispatcherHost(
606 storage_partition_impl_
->GetQuotaManager(),
607 GetContentClient()->browser()->CreateQuotaPermissionContext()));
608 channel_
->AddFilter(new GamepadBrowserMessageFilter());
609 channel_
->AddFilter(new ProfilerMessageFilter(PROCESS_TYPE_RENDERER
));
610 channel_
->AddFilter(new HistogramMessageFilter());
611 channel_
->AddFilter(new HyphenatorMessageFilter(this));
614 int RenderProcessHostImpl::GetNextRoutingID() {
615 return widget_helper_
->GetNextRoutingID();
618 void RenderProcessHostImpl::CancelResourceRequests(int render_widget_id
) {
619 widget_helper_
->CancelResourceRequests(render_widget_id
);
622 void RenderProcessHostImpl::SimulateSwapOutACK(
623 const ViewMsg_SwapOut_Params
& params
) {
624 widget_helper_
->SimulateSwapOutACK(params
);
627 bool RenderProcessHostImpl::WaitForBackingStoreMsg(
628 int render_widget_id
,
629 const base::TimeDelta
& max_delay
,
631 // The post task to this thread with the process id could be in queue, and we
632 // don't want to dispatch a message before then since it will need the handle.
633 if (child_process_launcher_
.get() && child_process_launcher_
->IsStarting())
636 return widget_helper_
->WaitForBackingStoreMsg(render_widget_id
,
640 void RenderProcessHostImpl::ReceivedBadMessage() {
641 if (run_renderer_in_process()) {
642 // In single process mode it is better if we don't suicide but just
647 base::KillProcess(GetHandle(), RESULT_CODE_KILLED_BAD_MESSAGE
,
651 void RenderProcessHostImpl::WidgetRestored() {
652 // Verify we were properly backgrounded.
653 DCHECK_EQ(backgrounded_
, (visible_widgets_
== 0));
655 SetBackgrounded(false);
658 void RenderProcessHostImpl::WidgetHidden() {
659 // On startup, the browser will call Hide
663 DCHECK_EQ(backgrounded_
, (visible_widgets_
== 0));
665 DCHECK_GE(visible_widgets_
, 0);
666 if (visible_widgets_
== 0) {
667 DCHECK(!backgrounded_
);
668 SetBackgrounded(true);
672 int RenderProcessHostImpl::VisibleWidgetCount() const {
673 return visible_widgets_
;
676 bool RenderProcessHostImpl::IsGuest() const {
680 StoragePartition
* RenderProcessHostImpl::GetStoragePartition() const {
681 return storage_partition_impl_
;
684 void RenderProcessHostImpl::AppendRendererCommandLine(
685 CommandLine
* command_line
) const {
686 // Pass the process type first, so it shows first in process listings.
687 command_line
->AppendSwitchASCII(switches::kProcessType
,
688 switches::kRendererProcess
);
690 // Now send any options from our own command line we want to propagate.
691 const CommandLine
& browser_command_line
= *CommandLine::ForCurrentProcess();
692 PropagateBrowserCommandLineToRenderer(browser_command_line
, command_line
);
694 // Pass on the browser locale.
695 const std::string locale
=
696 GetContentClient()->browser()->GetApplicationLocale();
697 command_line
->AppendSwitchASCII(switches::kLang
, locale
);
699 // If we run base::FieldTrials, we want to pass to their state to the
700 // renderer so that it can act in accordance with each state, or record
701 // histograms relating to the base::FieldTrial states.
702 std::string field_trial_states
;
703 base::FieldTrialList::StatesToString(&field_trial_states
);
704 if (!field_trial_states
.empty()) {
705 command_line
->AppendSwitchASCII(switches::kForceFieldTrials
,
709 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
710 command_line
, GetID());
712 // Appending disable-gpu-feature switches due to software rendering list.
713 GpuDataManagerImpl
* gpu_data_manager
= GpuDataManagerImpl::GetInstance();
714 DCHECK(gpu_data_manager
);
715 gpu_data_manager
->AppendRendererCommandLine(command_line
);
718 void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
719 const CommandLine
& browser_cmd
,
720 CommandLine
* renderer_cmd
) const {
721 // Propagate the following switches to the renderer command line (along
722 // with any associated values) if present in the browser command line.
723 static const char* const kSwitchNames
[] = {
724 // We propagate the Chrome Frame command line here as well in case the
725 // renderer is not run in the sandbox.
726 switches::kAudioBufferSize
,
727 switches::kAuditAllHandles
,
728 switches::kAuditHandles
,
729 switches::kChromeFrame
,
730 switches::kDisable3DAPIs
,
731 switches::kDisableAcceleratedCompositing
,
732 switches::kDisableAcceleratedVideoDecode
,
733 switches::kDisableApplicationCache
,
734 switches::kDisableAudio
,
735 switches::kDisableAudioOutputResampler
,
736 switches::kDisableBreakpad
,
737 #if defined(OS_MACOSX)
738 switches::kDisableCompositedCoreAnimationPlugins
,
740 switches::kDisableDataTransferItems
,
741 switches::kDisableDatabases
,
742 switches::kDisableDesktopNotifications
,
743 switches::kDisableDeviceOrientation
,
744 switches::kDisableFileSystem
,
745 switches::kDisableGeolocation
,
746 switches::kDisableGLMultisampling
,
747 switches::kDisableGpuVsync
,
748 switches::kDisableHistogramCustomizer
,
749 switches::kDisableJavaScriptI18NAPI
,
750 switches::kDisableLocalStorage
,
751 switches::kDisableLogging
,
752 switches::kDisableSeccompFilterSandbox
,
753 switches::kDisableSeccompSandbox
,
754 switches::kDisableSessionStorage
,
755 switches::kDisableSharedWorkers
,
756 switches::kDisableSpeechInput
,
757 #if defined(OS_ANDROID)
758 switches::kEnableWebAudio
,
759 switches::kEnableWebRTC
,
761 switches::kDisableWebAudio
,
763 switches::kEnableWebAudioInput
,
764 switches::kDisableWebSockets
,
765 switches::kDomAutomationController
,
766 switches::kEnableAccessibilityLogging
,
767 switches::kEnableBrowserPluginForAllViewTypes
,
768 switches::kEnableBrowserPluginPointerLock
,
769 switches::kEnableDCHECK
,
770 switches::kEnableDelegatedRenderer
,
771 switches::kDisableEncryptedMedia
,
772 switches::kEnableExperimentalWebKitFeatures
,
773 switches::kEnableFixedLayout
,
774 switches::kEnableDeferredImageDecoding
,
775 switches::kEnableGPUServiceLogging
,
776 switches::kEnableGPUClientLogging
,
777 switches::kEnableGpuClientTracing
,
778 switches::kEnableGpuBenchmarking
,
779 switches::kEnableMemoryBenchmarking
,
780 switches::kEnableLogging
,
781 switches::kDisableMediaSource
,
782 switches::kDisableRendererSideMixing
,
783 switches::kEnableStrictSiteIsolation
,
784 switches::kDisableFullScreen
,
785 switches::kEnableNewDialogStyle
,
786 #if defined(ENABLE_PLUGINS)
787 switches::kEnablePepperTesting
,
789 switches::kEnablePreparsedJsCaching
,
790 switches::kEnablePruneGpuCommandBuffers
,
791 switches::kEnablePinch
,
792 #if defined(OS_MACOSX)
793 // Allow this to be set when invoking the browser and relayed along.
794 switches::kEnableSandboxLogging
,
796 switches::kEnableSeccompSandbox
,
797 switches::kEnableSoftwareCompositingGLAdapter
,
798 switches::kEnableStatsTable
,
799 switches::kEnableThreadedCompositing
,
800 switches::kEnableCompositingForFixedPosition
,
801 switches::kEnableHighDpiCompositingForFixedPosition
,
802 switches::kDisableCompositingForFixedPosition
,
803 switches::kEnableAcceleratedPainting
,
804 switches::kEnableTouchDragDrop
,
805 switches::kDisableThreadedCompositing
,
806 switches::kDisableTouchAdjustment
,
807 switches::kDefaultTileWidth
,
808 switches::kDefaultTileHeight
,
809 switches::kMaxUntiledLayerWidth
,
810 switches::kMaxUntiledLayerHeight
,
811 switches::kShowFPSCounter
,
812 switches::kEnableViewport
,
813 switches::kEnableOpusPlayback
,
814 switches::kEnableVp9Playback
,
815 switches::kForceDeviceScaleFactor
,
816 switches::kFullMemoryCrashReport
,
817 #if !defined (GOOGLE_CHROME_BUILD)
818 // These are unsupported and not fully tested modes, so don't enable them
819 // for official Google Chrome builds.
820 switches::kInProcessPlugins
,
821 #endif // GOOGLE_CHROME_BUILD
822 switches::kInProcessWebGL
,
823 switches::kJavaScriptFlags
,
824 switches::kLoggingLevel
,
825 switches::kMemoryMetrics
,
826 #if defined(OS_ANDROID)
827 switches::kNetworkCountryIso
,
828 switches::kDisableGestureRequirementForMediaPlayback
,
830 switches::kNoReferrers
,
831 switches::kNoSandbox
,
832 switches::kOldCheckboxStyle
,
833 switches::kPpapiInProcess
,
834 switches::kRegisterPepperPlugins
,
835 switches::kRendererAssertTest
,
836 #if defined(OS_POSIX)
837 switches::kChildCleanExit
,
839 switches::kRendererStartupDialog
,
840 switches::kShowPaintRects
,
841 switches::kShowCompositedLayerBorders
,
842 switches::kShowCompositedLayerTree
,
843 switches::kSitePerProcess
,
844 switches::kTestSandbox
,
845 switches::kTouchEvents
,
846 switches::kTraceStartup
,
847 // This flag needs to be propagated to the renderer process for
848 // --in-process-webgl.
850 switches::kUseMobileUserAgent
,
851 switches::kUserAgent
,
853 switches::kVideoThreads
,
855 switches::kWebCoreLogChannels
,
856 cc::switches::kBackgroundColorInsteadOfCheckerboard
,
857 cc::switches::kDisableImplSidePainting
,
858 cc::switches::kDisableThreadedAnimation
,
859 cc::switches::kEnableCompositorFrameMessage
,
860 cc::switches::kEnableImplSidePainting
,
861 cc::switches::kEnablePartialSwap
,
862 cc::switches::kEnablePerTilePainting
,
863 cc::switches::kEnableRightAlignedScheduling
,
864 cc::switches::kEnableTopControlsPositionCalculation
,
865 cc::switches::kLowResolutionContentsScaleFactor
,
866 cc::switches::kNumRasterThreads
,
867 cc::switches::kShowNonOccludingRects
,
868 cc::switches::kShowOccludingRects
,
869 cc::switches::kShowPropertyChangedRects
,
870 cc::switches::kShowReplicaScreenSpaceRects
,
871 cc::switches::kShowScreenSpaceRects
,
872 cc::switches::kShowSurfaceDamageRects
,
873 cc::switches::kSlowDownRasterScaleFactor
,
874 cc::switches::kTopControlsHeight
,
875 cc::switches::kTopControlsHideThreshold
,
876 cc::switches::kTopControlsShowThreshold
,
877 cc::switches::kTraceAllRenderedFrames
,
878 cc::switches::kTraceOverdraw
,
879 cc::switches::kUseCheapnessEstimator
,
880 cc::switches::kCompositeToMailbox
,
882 renderer_cmd
->CopySwitchesFrom(browser_cmd
, kSwitchNames
,
883 arraysize(kSwitchNames
));
885 // Disable databases in incognito mode.
886 if (GetBrowserContext()->IsOffTheRecord() &&
887 !browser_cmd
.HasSwitch(switches::kDisableDatabases
)) {
888 renderer_cmd
->AppendSwitch(switches::kDisableDatabases
);
889 #if defined(OS_ANDROID)
890 renderer_cmd
->AppendSwitch(switches::kDisableMediaHistoryLogging
);
894 // Enforce the extra command line flags for impl-side painting.
895 if (cc::switches::IsImplSidePaintingEnabled() &&
896 !browser_cmd
.HasSwitch(switches::kEnableDeferredImageDecoding
))
897 renderer_cmd
->AppendSwitch(switches::kEnableDeferredImageDecoding
);
900 base::ProcessHandle
RenderProcessHostImpl::GetHandle() const {
901 if (run_renderer_in_process())
902 return base::Process::Current().handle();
904 if (!child_process_launcher_
.get() || child_process_launcher_
->IsStarting())
905 return base::kNullProcessHandle
;
907 return child_process_launcher_
->GetHandle();
910 bool RenderProcessHostImpl::FastShutdownIfPossible() {
911 if (run_renderer_in_process())
912 return false; // Single process mode never shutdown the renderer.
914 if (!GetContentClient()->browser()->IsFastShutdownPossible())
917 if (!child_process_launcher_
.get() ||
918 child_process_launcher_
->IsStarting() ||
920 return false; // Render process hasn't started or is probably crashed.
922 // Test if there's an unload listener.
923 // NOTE: It's possible that an onunload listener may be installed
924 // while we're shutting down, so there's a small race here. Given that
925 // the window is small, it's unlikely that the web page has much
926 // state that will be lost by not calling its unload handlers properly.
927 if (!SuddenTerminationAllowed())
930 ProcessDied(false /* already_dead */);
931 fast_shutdown_started_
= true;
935 void RenderProcessHostImpl::DumpHandles() {
937 Send(new ChildProcessMsg_DumpHandles());
944 // This is a platform specific function for mapping a transport DIB given its id
945 TransportDIB
* RenderProcessHostImpl::MapTransportDIB(
946 TransportDIB::Id dib_id
) {
948 // On Windows we need to duplicate the handle from the remote process
950 DuplicateHandle(GetHandle(), dib_id
.handle
, GetCurrentProcess(), §ion
,
951 STANDARD_RIGHTS_REQUIRED
| FILE_MAP_READ
| FILE_MAP_WRITE
,
953 return TransportDIB::Map(section
);
954 #elif defined(OS_MACOSX)
955 // On OSX, the browser allocates all DIBs and keeps a file descriptor around
957 return widget_helper_
->MapTransportDIB(dib_id
);
958 #elif defined(OS_ANDROID)
959 return TransportDIB::Map(dib_id
);
960 #elif defined(OS_POSIX)
961 return TransportDIB::Map(dib_id
.shmkey
);
962 #endif // defined(OS_POSIX)
965 TransportDIB
* RenderProcessHostImpl::GetTransportDIB(
966 TransportDIB::Id dib_id
) {
967 if (!TransportDIB::is_valid_id(dib_id
))
970 const std::map
<TransportDIB::Id
, TransportDIB
*>::iterator
971 i
= cached_dibs_
.find(dib_id
);
972 if (i
!= cached_dibs_
.end()) {
973 cached_dibs_cleaner_
.Reset();
977 TransportDIB
* dib
= MapTransportDIB(dib_id
);
981 if (cached_dibs_
.size() >= MAX_MAPPED_TRANSPORT_DIBS
) {
982 // Clean a single entry from the cache
983 std::map
<TransportDIB::Id
, TransportDIB
*>::iterator smallest_iterator
;
984 size_t smallest_size
= std::numeric_limits
<size_t>::max();
986 for (std::map
<TransportDIB::Id
, TransportDIB
*>::iterator
987 i
= cached_dibs_
.begin(); i
!= cached_dibs_
.end(); ++i
) {
988 if (i
->second
->size() <= smallest_size
) {
989 smallest_iterator
= i
;
990 smallest_size
= i
->second
->size();
995 smallest_iterator
->second
->Detach();
997 delete smallest_iterator
->second
;
999 cached_dibs_
.erase(smallest_iterator
);
1002 cached_dibs_
[dib_id
] = dib
;
1003 cached_dibs_cleaner_
.Reset();
1007 void RenderProcessHostImpl::ClearTransportDIBCache() {
1008 #if defined(USE_X11)
1009 std::map
<TransportDIB::Id
, TransportDIB
*>::const_iterator dib
=
1010 cached_dibs_
.begin();
1011 for (; dib
!= cached_dibs_
.end(); ++dib
)
1012 dib
->second
->Detach();
1014 STLDeleteContainerPairSecondPointers(
1015 cached_dibs_
.begin(), cached_dibs_
.end());
1017 cached_dibs_
.clear();
1020 bool RenderProcessHostImpl::Send(IPC::Message
* msg
) {
1021 if (!channel_
.get()) {
1022 if (!is_initialized_
) {
1023 queued_messages_
.push(msg
);
1031 if (child_process_launcher_
.get() && child_process_launcher_
->IsStarting()) {
1032 queued_messages_
.push(msg
);
1036 return channel_
->Send(msg
);
1039 bool RenderProcessHostImpl::OnMessageReceived(const IPC::Message
& msg
) {
1040 // If we're about to be deleted, or have initiated the fast shutdown sequence,
1041 // we ignore incoming messages.
1043 if (deleting_soon_
|| fast_shutdown_started_
)
1046 mark_child_process_activity_time();
1047 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
1048 // Dispatch control messages.
1049 bool msg_is_ok
= true;
1050 IPC_BEGIN_MESSAGE_MAP_EX(RenderProcessHostImpl
, msg
, msg_is_ok
)
1051 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ShutdownRequest
,
1053 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_DumpHandlesDone
,
1055 IPC_MESSAGE_HANDLER(ViewHostMsg_SuddenTerminationChanged
,
1056 SuddenTerminationChanged
)
1057 IPC_MESSAGE_HANDLER(ViewHostMsg_UserMetricsRecordAction
,
1058 OnUserMetricsRecordAction
)
1059 IPC_MESSAGE_HANDLER(ViewHostMsg_SavedPageAsMHTML
, OnSavedPageAsMHTML
)
1060 // Adding single handlers for your service here is fine, but once your
1061 // service needs more than one handler, please extract them into a new
1062 // message filter and add that filter to CreateMessageFilters().
1063 IPC_MESSAGE_UNHANDLED_ERROR()
1064 IPC_END_MESSAGE_MAP_EX()
1067 // The message had a handler, but its de-serialization failed.
1068 // We consider this a capital crime. Kill the renderer if we have one.
1069 LOG(ERROR
) << "bad message " << msg
.type() << " terminating renderer.";
1070 RecordAction(UserMetricsAction("BadMessageTerminate_BRPH"));
1071 ReceivedBadMessage();
1076 // Dispatch incoming messages to the appropriate RenderView/WidgetHost.
1077 RenderWidgetHost
* rwh
= render_widget_hosts_
.Lookup(msg
.routing_id());
1079 if (msg
.is_sync()) {
1080 // The listener has gone away, so we must respond or else the caller will
1081 // hang waiting for a reply.
1082 IPC::Message
* reply
= IPC::SyncMessage::GenerateReply(&msg
);
1083 reply
->set_reply_error();
1087 // If this is a SwapBuffers, we need to ack it if we're not going to handle
1088 // it so that the GPU process doesn't get stuck in unscheduled state.
1089 bool msg_is_ok
= true;
1090 IPC_BEGIN_MESSAGE_MAP_EX(RenderProcessHostImpl
, msg
, msg_is_ok
)
1091 IPC_MESSAGE_HANDLER(ViewHostMsg_CompositorSurfaceBuffersSwapped
,
1092 OnCompositorSurfaceBuffersSwappedNoHost
)
1093 IPC_END_MESSAGE_MAP_EX()
1096 return RenderWidgetHostImpl::From(rwh
)->OnMessageReceived(msg
);
1099 void RenderProcessHostImpl::OnChannelConnected(int32 peer_pid
) {
1100 #if defined(IPC_MESSAGE_LOG_ENABLED)
1101 Send(new ChildProcessMsg_SetIPCLoggingEnabled(
1102 IPC::Logging::GetInstance()->Enabled()));
1105 tracked_objects::ThreadData::Status status
=
1106 tracked_objects::ThreadData::status();
1107 Send(new ChildProcessMsg_SetProfilerStatus(status
));
1110 void RenderProcessHostImpl::OnChannelError() {
1111 ProcessDied(true /* already_dead */);
1114 BrowserContext
* RenderProcessHostImpl::GetBrowserContext() const {
1115 return browser_context_
;
1118 bool RenderProcessHostImpl::InSameStoragePartition(
1119 StoragePartition
* partition
) const {
1120 return storage_partition_impl_
== partition
;
1123 int RenderProcessHostImpl::GetID() const {
1127 bool RenderProcessHostImpl::HasConnection() const {
1128 return channel_
.get() != NULL
;
1131 RenderWidgetHost
* RenderProcessHostImpl::GetRenderWidgetHostByID(
1133 return render_widget_hosts_
.Lookup(routing_id
);
1136 void RenderProcessHostImpl::SetIgnoreInputEvents(bool ignore_input_events
) {
1137 ignore_input_events_
= ignore_input_events
;
1140 bool RenderProcessHostImpl::IgnoreInputEvents() const {
1141 return ignore_input_events_
;
1144 void RenderProcessHostImpl::Attach(RenderWidgetHost
* host
,
1146 render_widget_hosts_
.AddWithID(host
, routing_id
);
1149 void RenderProcessHostImpl::Release(int routing_id
) {
1150 DCHECK(render_widget_hosts_
.Lookup(routing_id
) != NULL
);
1151 render_widget_hosts_
.Remove(routing_id
);
1153 // Make sure that all associated resource requests are stopped.
1154 CancelResourceRequests(routing_id
);
1157 // Dump the handle table if handle auditing is enabled.
1158 const CommandLine
& browser_command_line
=
1159 *CommandLine::ForCurrentProcess();
1160 if (browser_command_line
.HasSwitch(switches::kAuditHandles
) ||
1161 browser_command_line
.HasSwitch(switches::kAuditAllHandles
)) {
1164 // We wait to close the channels until the child process has finished
1165 // dumping handles and sends us ChildProcessHostMsg_DumpHandlesDone.
1169 // Keep the one renderer thread around forever in single process mode.
1170 if (!run_renderer_in_process())
1174 void RenderProcessHostImpl::Cleanup() {
1175 // When no other owners of this object, we can delete ourselves
1176 if (render_widget_hosts_
.IsEmpty()) {
1177 DCHECK_EQ(0, pending_views_
);
1178 NotificationService::current()->Notify(
1179 NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
1180 Source
<RenderProcessHost
>(this),
1181 NotificationService::NoDetails());
1183 MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
1184 deleting_soon_
= true;
1185 // It's important not to wait for the DeleteTask to delete the channel
1186 // proxy. Kill it off now. That way, in case the profile is going away, the
1187 // rest of the objects attached to this RenderProcessHost start going
1188 // away first, since deleting the channel proxy will post a
1189 // OnChannelClosed() to IPC::ChannelProxy::Context on the IO thread.
1191 gpu_message_filter_
= NULL
;
1193 // Remove ourself from the list of renderer processes so that we can't be
1194 // reused in between now and when the Delete task runs.
1195 UnregisterHost(GetID());
1199 void RenderProcessHostImpl::AddPendingView() {
1203 void RenderProcessHostImpl::RemovePendingView() {
1204 DCHECK(pending_views_
);
1208 void RenderProcessHostImpl::SetSuddenTerminationAllowed(bool enabled
) {
1209 sudden_termination_allowed_
= enabled
;
1212 bool RenderProcessHostImpl::SuddenTerminationAllowed() const {
1213 return sudden_termination_allowed_
;
1216 base::TimeDelta
RenderProcessHostImpl::GetChildProcessIdleTime() const {
1217 return base::TimeTicks::Now() - child_process_activity_time_
;
1220 void RenderProcessHostImpl::SurfaceUpdated(int32 surface_id
) {
1221 if (!gpu_message_filter_
)
1223 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
, base::Bind(
1224 &GpuMessageFilter::SurfaceUpdated
,
1225 gpu_message_filter_
,
1229 void RenderProcessHostImpl::ResumeRequestsForView(int route_id
) {
1230 widget_helper_
->ResumeRequestsForView(route_id
);
1233 IPC::ChannelProxy
* RenderProcessHostImpl::GetChannel() {
1234 return channel_
.get();
1237 RenderProcessHost::RenderWidgetHostsIterator
1238 RenderProcessHostImpl::GetRenderWidgetHostsIterator() {
1239 return RenderWidgetHostsIterator(&render_widget_hosts_
);
1242 bool RenderProcessHostImpl::FastShutdownForPageCount(size_t count
) {
1243 if (render_widget_hosts_
.size() == count
)
1244 return FastShutdownIfPossible();
1248 bool RenderProcessHostImpl::FastShutdownStarted() const {
1249 return fast_shutdown_started_
;
1253 void RenderProcessHostImpl::RegisterHost(int host_id
, RenderProcessHost
* host
) {
1254 g_all_hosts
.Get().AddWithID(host
, host_id
);
1258 void RenderProcessHostImpl::UnregisterHost(int host_id
) {
1259 RenderProcessHost
* host
= g_all_hosts
.Get().Lookup(host_id
);
1263 g_all_hosts
.Get().Remove(host_id
);
1265 // Look up the map of site to process for the given browser_context,
1266 // in case we need to remove this process from it. It will be registered
1267 // under any sites it rendered that use process-per-site mode.
1268 SiteProcessMap
* map
=
1269 GetSiteProcessMapForBrowserContext(host
->GetBrowserContext());
1270 map
->RemoveProcess(host
);
1274 bool RenderProcessHostImpl::IsSuitableHost(
1275 RenderProcessHost
* host
,
1276 BrowserContext
* browser_context
,
1277 const GURL
& site_url
) {
1278 if (run_renderer_in_process())
1281 if (host
->GetBrowserContext() != browser_context
)
1284 // Check whether the given host and the intended site_url will be using the
1285 // same StoragePartition, since a RenderProcessHost can only support a single
1286 // StoragePartition. This is relevant for packaged apps, browser tags, and
1288 StoragePartition
* dest_partition
=
1289 BrowserContext::GetStoragePartitionForSite(browser_context
, site_url
);
1290 if (!host
->InSameStoragePartition(dest_partition
))
1293 // All URLs are suitable if this is associated with a guest renderer process.
1294 // TODO(fsamuel, creis): Further validation is needed to ensure that only
1295 // normal web URLs are permitted in guest processes. We need to investigate
1296 // where this validation should happen.
1297 if (host
->IsGuest())
1300 if (!host
->IsGuest() && site_url
.SchemeIs(chrome::kGuestScheme
))
1303 if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1305 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIBindingsForURL(
1306 browser_context
, site_url
)) {
1310 return GetContentClient()->browser()->IsSuitableHost(host
, site_url
);
1314 bool RenderProcessHost::run_renderer_in_process() {
1315 return g_run_renderer_in_process_
;
1319 void RenderProcessHost::SetRunRendererInProcess(bool value
) {
1320 g_run_renderer_in_process_
= value
;
1322 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
1323 if (value
&& !command_line
->HasSwitch(switches::kLang
)) {
1324 // Modify the current process' command line to include the browser locale,
1325 // as the renderer expects this flag to be set.
1326 const std::string locale
=
1327 GetContentClient()->browser()->GetApplicationLocale();
1328 command_line
->AppendSwitchASCII(switches::kLang
, locale
);
1332 RenderProcessHost::iterator
RenderProcessHost::AllHostsIterator() {
1333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1334 return iterator(g_all_hosts
.Pointer());
1338 RenderProcessHost
* RenderProcessHost::FromID(int render_process_id
) {
1339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1340 return g_all_hosts
.Get().Lookup(render_process_id
);
1344 bool RenderProcessHost::ShouldTryToUseExistingProcessHost(
1345 BrowserContext
* browser_context
, const GURL
& url
) {
1347 // If --enable-strict-site-isolation or --site-per-process is enabled, do not
1348 // try to reuse renderer processes when over the limit. (We could allow pages
1349 // from the same site to share, if we knew what the given process was
1350 // dedicated to. Allowing no sharing is simpler for now.) This may cause
1351 // resource exhaustion issues if too many sites are open at once.
1352 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
1353 if (command_line
.HasSwitch(switches::kEnableStrictSiteIsolation
) ||
1354 command_line
.HasSwitch(switches::kSitePerProcess
))
1357 if (run_renderer_in_process())
1360 // NOTE: Sometimes it's necessary to create more render processes than
1361 // GetMaxRendererProcessCount(), for instance when we want to create
1362 // a renderer process for a browser context that has no existing
1363 // renderers. This is OK in moderation, since the
1364 // GetMaxRendererProcessCount() is conservative.
1365 if (g_all_hosts
.Get().size() >= GetMaxRendererProcessCount())
1368 return GetContentClient()->browser()->
1369 ShouldTryToUseExistingProcessHost(browser_context
, url
);
1373 RenderProcessHost
* RenderProcessHost::GetExistingProcessHost(
1374 BrowserContext
* browser_context
,
1375 const GURL
& site_url
) {
1376 // First figure out which existing renderers we can use.
1377 std::vector
<RenderProcessHost
*> suitable_renderers
;
1378 suitable_renderers
.reserve(g_all_hosts
.Get().size());
1380 iterator
iter(AllHostsIterator());
1381 while (!iter
.IsAtEnd()) {
1382 if (RenderProcessHostImpl::IsSuitableHost(
1383 iter
.GetCurrentValue(),
1384 browser_context
, site_url
))
1385 suitable_renderers
.push_back(iter
.GetCurrentValue());
1390 // Now pick a random suitable renderer, if we have any.
1391 if (!suitable_renderers
.empty()) {
1392 int suitable_count
= static_cast<int>(suitable_renderers
.size());
1393 int random_index
= base::RandInt(0, suitable_count
- 1);
1394 return suitable_renderers
[random_index
];
1401 bool RenderProcessHostImpl::ShouldUseProcessPerSite(
1402 BrowserContext
* browser_context
,
1404 // Returns true if we should use the process-per-site model. This will be
1405 // the case if the --process-per-site switch is specified, or in
1406 // process-per-site-instance for particular sites (e.g., WebUI).
1408 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
1409 if (command_line
.HasSwitch(switches::kProcessPerSite
))
1412 // We want to consolidate particular sites like WebUI when we are using
1413 // process-per-tab or process-per-site-instance models.
1414 // Note that --single-process is handled in ShouldTryToUseExistingProcessHost.
1416 if (GetContentClient()->browser()->
1417 ShouldUseProcessPerSite(browser_context
, url
)) {
1421 // DevTools pages have WebUI type but should not reuse the same host.
1422 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1423 browser_context
, url
) &&
1424 !url
.SchemeIs(chrome::kChromeDevToolsScheme
)) {
1428 // In all other cases, don't use process-per-site logic.
1433 RenderProcessHost
* RenderProcessHostImpl::GetProcessHostForSite(
1434 BrowserContext
* browser_context
,
1436 // Look up the map of site to process for the given browser_context.
1437 SiteProcessMap
* map
=
1438 GetSiteProcessMapForBrowserContext(browser_context
);
1440 // See if we have an existing process with appropriate bindings for this site.
1441 // If not, the caller should create a new process and register it.
1442 std::string site
= SiteInstance::GetSiteForURL(browser_context
, url
)
1443 .possibly_invalid_spec();
1444 RenderProcessHost
* host
= map
->FindProcess(site
);
1445 if (host
&& !IsSuitableHost(host
, browser_context
, url
)) {
1446 // The registered process does not have an appropriate set of bindings for
1447 // the url. Remove it from the map so we can register a better one.
1448 RecordAction(UserMetricsAction("BindingsMismatch_GetProcessHostPerSite"));
1449 map
->RemoveProcess(host
);
1456 void RenderProcessHostImpl::RegisterProcessHostForSite(
1457 BrowserContext
* browser_context
,
1458 RenderProcessHost
* process
,
1460 // Look up the map of site to process for the given browser_context.
1461 SiteProcessMap
* map
=
1462 GetSiteProcessMapForBrowserContext(browser_context
);
1464 // Only register valid, non-empty sites. Empty or invalid sites will not
1465 // use process-per-site mode. We cannot check whether the process has
1466 // appropriate bindings here, because the bindings have not yet been granted.
1467 std::string site
= SiteInstance::GetSiteForURL(browser_context
, url
)
1468 .possibly_invalid_spec();
1470 map
->RegisterProcess(site
, process
);
1473 void RenderProcessHostImpl::ProcessDied(bool already_dead
) {
1474 // Our child process has died. If we didn't expect it, it's a crash.
1475 // In any case, we need to let everyone know it's gone.
1476 // The OnChannelError notification can fire multiple times due to nested sync
1477 // calls to a renderer. If we don't have a valid channel here it means we
1478 // already handled the error.
1480 // child_process_launcher_ can be NULL in single process mode or if fast
1481 // termination happened.
1483 base::TerminationStatus status
=
1484 child_process_launcher_
.get() ?
1485 child_process_launcher_
->GetChildTerminationStatus(already_dead
,
1487 base::TERMINATION_STATUS_NORMAL_TERMINATION
;
1489 RendererClosedDetails
details(GetHandle(), status
, exit_code
);
1490 NotificationService::current()->Notify(
1491 NOTIFICATION_RENDERER_PROCESS_CLOSED
,
1492 Source
<RenderProcessHost
>(this),
1493 Details
<RendererClosedDetails
>(&details
));
1495 child_process_launcher_
.reset();
1497 gpu_message_filter_
= NULL
;
1499 IDMap
<RenderWidgetHost
>::iterator
iter(&render_widget_hosts_
);
1500 while (!iter
.IsAtEnd()) {
1501 RenderWidgetHostImpl::From(iter
.GetCurrentValue())->OnMessageReceived(
1502 ViewHostMsg_RenderViewGone(iter
.GetCurrentKey(),
1503 static_cast<int>(status
),
1508 ClearTransportDIBCache();
1510 // this object is not deleted at this point and may be reused later.
1511 // TODO(darin): clean this up
1514 int RenderProcessHostImpl::GetActiveViewCount() {
1515 int num_active_views
= 0;
1516 for (RenderWidgetHostsIterator iter
= GetRenderWidgetHostsIterator();
1519 const RenderWidgetHost
* widget
= iter
.GetCurrentValue();
1524 // All RenderWidgetHosts are swapped in.
1525 if (!widget
->IsRenderView()) {
1530 // Don't count swapped out views.
1531 RenderViewHost
* rvh
=
1532 RenderViewHost::From(const_cast<RenderWidgetHost
*>(widget
));
1533 if (!static_cast<RenderViewHostImpl
*>(rvh
)->is_swapped_out())
1536 return num_active_views
;
1539 void RenderProcessHostImpl::OnShutdownRequest() {
1540 // Don't shut down if there are more active RenderViews than the one asking
1541 // to close, or if there are pending RenderViews being swapped back in.
1542 // In single process mode, we never shutdown the renderer.
1543 int num_active_views
= GetActiveViewCount();
1544 if (pending_views_
|| num_active_views
> 1 || run_renderer_in_process())
1547 // Notify any contents that might have swapped out renderers from this
1548 // process. They should not attempt to swap them back in.
1549 NotificationService::current()->Notify(
1550 NOTIFICATION_RENDERER_PROCESS_CLOSING
,
1551 Source
<RenderProcessHost
>(this),
1552 NotificationService::NoDetails());
1554 Send(new ChildProcessMsg_Shutdown());
1557 void RenderProcessHostImpl::SuddenTerminationChanged(bool enabled
) {
1558 SetSuddenTerminationAllowed(enabled
);
1561 void RenderProcessHostImpl::OnDumpHandlesDone() {
1565 void RenderProcessHostImpl::SetBackgrounded(bool backgrounded
) {
1566 // Note: we always set the backgrounded_ value. If the process is NULL
1567 // (and hence hasn't been created yet), we will set the process priority
1568 // later when we create the process.
1569 backgrounded_
= backgrounded
;
1570 if (!child_process_launcher_
.get() || child_process_launcher_
->IsStarting())
1574 // The cbstext.dll loads as a global GetMessage hook in the browser process
1575 // and intercepts/unintercepts the kernel32 API SetPriorityClass in a
1576 // background thread. If the UI thread invokes this API just when it is
1577 // intercepted the stack is messed up on return from the interceptor
1578 // which causes random crashes in the browser process. Our hack for now
1579 // is to not invoke the SetPriorityClass API if the dll is loaded.
1580 if (GetModuleHandle(L
"cbstext.dll"))
1584 child_process_launcher_
->SetProcessBackgrounded(backgrounded
);
1587 void RenderProcessHostImpl::OnProcessLaunched() {
1588 // No point doing anything, since this object will be destructed soon. We
1589 // especially don't want to send the RENDERER_PROCESS_CREATED notification,
1590 // since some clients might expect a RENDERER_PROCESS_TERMINATED afterwards to
1591 // properly cleanup.
1595 if (child_process_launcher_
.get()) {
1596 if (!child_process_launcher_
->GetHandle()) {
1601 child_process_launcher_
->SetProcessBackgrounded(backgrounded_
);
1604 // NOTE: This needs to be before sending queued messages because
1605 // ExtensionService uses this notification to initialize the renderer process
1606 // with state that must be there before any JavaScript executes.
1608 // The queued messages contain such things as "navigate". If this notification
1609 // was after, we can end up executing JavaScript before the initialization
1611 NotificationService::current()->Notify(
1612 NOTIFICATION_RENDERER_PROCESS_CREATED
,
1613 Source
<RenderProcessHost
>(this),
1614 NotificationService::NoDetails());
1616 while (!queued_messages_
.empty()) {
1617 Send(queued_messages_
.front());
1618 queued_messages_
.pop();
1622 void RenderProcessHostImpl::OnUserMetricsRecordAction(
1623 const std::string
& action
) {
1624 RecordComputedAction(action
);
1627 void RenderProcessHostImpl::OnSavedPageAsMHTML(int job_id
, int64 data_size
) {
1628 MHTMLGenerationManager::GetInstance()->MHTMLGenerated(job_id
, data_size
);
1631 void RenderProcessHostImpl::OnCompositorSurfaceBuffersSwappedNoHost(
1633 uint64 surface_handle
,
1635 const gfx::Size
& size
,
1636 int32 gpu_process_host_id
) {
1637 TRACE_EVENT0("renderer_host",
1638 "RenderWidgetHostImpl::OnCompositorSurfaceBuffersSwappedNoHost");
1639 AcceleratedSurfaceMsg_BufferPresented_Params ack_params
;
1640 ack_params
.sync_point
= 0;
1641 RenderWidgetHostImpl::AcknowledgeBufferPresent(route_id
,
1642 gpu_process_host_id
,
1646 } // namespace content