Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: content/browser/renderer_host/render_process_host_impl.cc

Issue 2861433002: Implement ProcessReusePolicy for SiteInstances (Closed)
Patch Set: Fixed ChromeOS issue Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Represents the browser side of the browser <--> renderer communication 5 // Represents the browser side of the browser <--> renderer communication
6 // channel. There will be one RenderProcessHost per renderer process. 6 // channel. There will be one RenderProcessHost per renderer process.
7 7
8 #include "content/browser/renderer_host/render_process_host_impl.h" 8 #include "content/browser/renderer_host/render_process_host_impl.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 #include "content/browser/renderer_host/render_view_host_impl.h" 117 #include "content/browser/renderer_host/render_view_host_impl.h"
118 #include "content/browser/renderer_host/render_widget_helper.h" 118 #include "content/browser/renderer_host/render_widget_helper.h"
119 #include "content/browser/renderer_host/render_widget_host_impl.h" 119 #include "content/browser/renderer_host/render_widget_host_impl.h"
120 #include "content/browser/renderer_host/render_widget_host_view_frame_subscriber .h" 120 #include "content/browser/renderer_host/render_widget_host_view_frame_subscriber .h"
121 #include "content/browser/renderer_host/text_input_client_message_filter.h" 121 #include "content/browser/renderer_host/text_input_client_message_filter.h"
122 #include "content/browser/resolve_proxy_msg_helper.h" 122 #include "content/browser/resolve_proxy_msg_helper.h"
123 #include "content/browser/service_worker/service_worker_context_wrapper.h" 123 #include "content/browser/service_worker/service_worker_context_wrapper.h"
124 #include "content/browser/service_worker/service_worker_dispatcher_host.h" 124 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
125 #include "content/browser/shared_worker/shared_worker_message_filter.h" 125 #include "content/browser/shared_worker/shared_worker_message_filter.h"
126 #include "content/browser/shared_worker/worker_storage_partition.h" 126 #include "content/browser/shared_worker/worker_storage_partition.h"
127 #include "content/browser/site_instance_impl.h"
127 #include "content/browser/speech/speech_recognition_dispatcher_host.h" 128 #include "content/browser/speech/speech_recognition_dispatcher_host.h"
128 #include "content/browser/storage_partition_impl.h" 129 #include "content/browser/storage_partition_impl.h"
129 #include "content/browser/streams/stream_context.h" 130 #include "content/browser/streams/stream_context.h"
130 #include "content/browser/tracing/trace_message_filter.h" 131 #include "content/browser/tracing/trace_message_filter.h"
131 #include "content/browser/websockets/websocket_manager.h" 132 #include "content/browser/websockets/websocket_manager.h"
132 #include "content/browser/webui/web_ui_controller_factory_registry.h" 133 #include "content/browser/webui/web_ui_controller_factory_registry.h"
133 #include "content/common/child_process_host_impl.h" 134 #include "content/common/child_process_host_impl.h"
134 #include "content/common/child_process_messages.h" 135 #include "content/common/child_process_messages.h"
135 #include "content/common/content_switches_internal.h" 136 #include "content/common/content_switches_internal.h"
136 #include "content/common/frame_messages.h" 137 #include "content/common/frame_messages.h"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 251
251 #if defined(OS_WIN) 252 #if defined(OS_WIN)
252 #define IntToStringType base::IntToString16 253 #define IntToStringType base::IntToString16
253 #else 254 #else
254 #define IntToStringType base::IntToString 255 #define IntToStringType base::IntToString
255 #endif 256 #endif
256 257
257 namespace content { 258 namespace content {
258 namespace { 259 namespace {
259 260
261 const RenderProcessHostFactory* g_render_process_host_factory_ = nullptr;
260 const char kSiteProcessMapKeyName[] = "content_site_process_map"; 262 const char kSiteProcessMapKeyName[] = "content_site_process_map";
261 263
262 #if BUILDFLAG(ENABLE_WEBRTC) 264 #if BUILDFLAG(ENABLE_WEBRTC)
263 const base::FilePath::CharType kAecDumpFileNameAddition[] = 265 const base::FilePath::CharType kAecDumpFileNameAddition[] =
264 FILE_PATH_LITERAL("aec_dump"); 266 FILE_PATH_LITERAL("aec_dump");
265 #endif 267 #endif
266 268
267 void CacheShaderInfo(int32_t id, base::FilePath path) { 269 void CacheShaderInfo(int32_t id, base::FilePath path) {
268 if (GetShaderCacheFactorySingleton()) 270 if (GetShaderCacheFactorySingleton())
269 GetShaderCacheFactorySingleton()->SetCacheInfo(id, path); 271 GetShaderCacheFactorySingleton()->SetCacheInfo(id, path);
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 void Release(int old_route_id) { 443 void Release(int old_route_id) {
442 session_storage_namespaces_awaiting_close_->erase(old_route_id); 444 session_storage_namespaces_awaiting_close_->erase(old_route_id);
443 } 445 }
444 446
445 private: 447 private:
446 std::unique_ptr<std::map<int, SessionStorageNamespaceMap>> 448 std::unique_ptr<std::map<int, SessionStorageNamespaceMap>>
447 session_storage_namespaces_awaiting_close_; 449 session_storage_namespaces_awaiting_close_;
448 DISALLOW_COPY_AND_ASSIGN(SessionStorageHolder); 450 DISALLOW_COPY_AND_ASSIGN(SessionStorageHolder);
449 }; 451 };
450 452
453 const void* const kDefaultSubframeProcessHostHolderKey =
454 &kDefaultSubframeProcessHostHolderKey;
455
456 class DefaultSubframeProcessHostHolder : public base::SupportsUserData::Data,
457 public RenderProcessHostObserver {
458 public:
459 explicit DefaultSubframeProcessHostHolder(BrowserContext* browser_context)
460 : browser_context_(browser_context) {}
461 ~DefaultSubframeProcessHostHolder() override {}
462
463 // Gets the correct render process to use for this SiteInstance.
464 RenderProcessHost* GetProcessHost(SiteInstance* site_instance,
465 bool is_for_guests_only) {
466 StoragePartition* default_partition =
467 BrowserContext::GetDefaultStoragePartition(browser_context_);
468 StoragePartition* partition =
469 BrowserContext::GetStoragePartition(browser_context_, site_instance);
470
471 // Is this the default storage partition? If it isn't, then just give it its
472 // own non-shared process.
473 if (partition != default_partition || is_for_guests_only) {
474 RenderProcessHostImpl* host = new RenderProcessHostImpl(
475 browser_context_, static_cast<StoragePartitionImpl*>(partition),
476 is_for_guests_only);
477 host->SetIsNeverSuitableForReuse();
478 return host;
479 }
480
481 // If we already have a shared host for the default storage partition, use
482 // it.
483 if (host_)
484 return host_;
485
486 host_ = new RenderProcessHostImpl(
487 browser_context_, static_cast<StoragePartitionImpl*>(partition),
488 false /* for guests only */);
489 host_->SetIsNeverSuitableForReuse();
490 host_->AddObserver(this);
491
492 return host_;
493 }
494
495 // Implementation of RenderProcessHostObserver.
496 void RenderProcessHostDestroyed(RenderProcessHost* host) override {
497 DCHECK_EQ(host_, host);
498 host_->RemoveObserver(this);
499 host_ = nullptr;
500 }
501
502 private:
503 BrowserContext* browser_context_;
504
505 // The default subframe render process used for the default storage partition
506 // of this BrowserContext.
507 RenderProcessHostImpl* host_ = nullptr;
508 };
509
451 void CreateMemoryCoordinatorHandle( 510 void CreateMemoryCoordinatorHandle(
452 int render_process_id, 511 int render_process_id,
453 const service_manager::BindSourceInfo& source_info, 512 const service_manager::BindSourceInfo& source_info,
454 mojom::MemoryCoordinatorHandleRequest request) { 513 mojom::MemoryCoordinatorHandleRequest request) {
455 MemoryCoordinatorImpl::GetInstance()->CreateHandle(render_process_id, 514 MemoryCoordinatorImpl::GetInstance()->CreateHandle(render_process_id,
456 std::move(request)); 515 std::move(request));
457 } 516 }
458 517
459 // Forwards service requests to Service Manager since the renderer cannot launch 518 // Forwards service requests to Service Manager since the renderer cannot launch
460 // out-of-process services on is own. 519 // out-of-process services on is own.
(...skipping 1146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1607 ++audio_stream_count_; 1666 ++audio_stream_count_;
1608 UpdateProcessPriority(); 1667 UpdateProcessPriority();
1609 } 1668 }
1610 1669
1611 void RenderProcessHostImpl::OnAudioStreamRemoved() { 1670 void RenderProcessHostImpl::OnAudioStreamRemoved() {
1612 DCHECK_GT(audio_stream_count_, 0); 1671 DCHECK_GT(audio_stream_count_, 0);
1613 --audio_stream_count_; 1672 --audio_stream_count_;
1614 UpdateProcessPriority(); 1673 UpdateProcessPriority();
1615 } 1674 }
1616 1675
1676 void RenderProcessHostImpl::set_render_process_host_factory(
1677 const RenderProcessHostFactory* rph_factory) {
1678 g_render_process_host_factory_ = rph_factory;
1679 }
1680
1617 bool RenderProcessHostImpl::IsForGuestsOnly() const { 1681 bool RenderProcessHostImpl::IsForGuestsOnly() const {
1618 return is_for_guests_only_; 1682 return is_for_guests_only_;
1619 } 1683 }
1620 1684
1621 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const { 1685 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const {
1622 return storage_partition_impl_; 1686 return storage_partition_impl_;
1623 } 1687 }
1624 1688
1625 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) { 1689 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) {
1626 command_line->AppendSwitchASCII( 1690 command_line->AppendSwitchASCII(
(...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after
2701 2765
2702 // Only register valid, non-empty sites. Empty or invalid sites will not 2766 // Only register valid, non-empty sites. Empty or invalid sites will not
2703 // use process-per-site mode. We cannot check whether the process has 2767 // use process-per-site mode. We cannot check whether the process has
2704 // appropriate bindings here, because the bindings have not yet been granted. 2768 // appropriate bindings here, because the bindings have not yet been granted.
2705 std::string site = 2769 std::string site =
2706 SiteInstance::GetSiteForURL(browser_context, url).possibly_invalid_spec(); 2770 SiteInstance::GetSiteForURL(browser_context, url).possibly_invalid_spec();
2707 if (!site.empty()) 2771 if (!site.empty())
2708 map->RegisterProcess(site, process); 2772 map->RegisterProcess(site, process);
2709 } 2773 }
2710 2774
2775 // static
2776 RenderProcessHost* RenderProcessHostImpl::GetProcessHostForSiteInstance(
2777 BrowserContext* browser_context,
2778 SiteInstanceImpl* site_instance) {
2779 const GURL site_url = site_instance->GetSiteURL();
2780 SiteInstanceImpl::ProcessReusePolicy process_reuse_policy =
2781 site_instance->process_reuse_policy();
2782 bool is_for_guests_only = site_url.SchemeIs(kGuestScheme);
2783 RenderProcessHost* render_process_host = nullptr;
2784
2785 // First, attempt to reuse an existing RenderProcessHost if necessary.
2786 switch (process_reuse_policy) {
2787 case SiteInstanceImpl::ProcessReusePolicy::PROCESS_PER_SITE:
2788 render_process_host = GetProcessHostForSite(browser_context, site_url);
2789 break;
2790 case SiteInstanceImpl::ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS:
2791 DCHECK(SiteIsolationPolicy::IsTopDocumentIsolationEnabled());
2792 render_process_host = GetDefaultSubframeProcessHost(
2793 browser_context, site_instance, is_for_guests_only);
2794 break;
2795 default:
2796 break;
2797 }
2798
2799 // If not (or if none found), see if we should reuse an existing process.
2800 if (!render_process_host &&
2801 ShouldTryToUseExistingProcessHost(browser_context, site_url)) {
2802 render_process_host = GetExistingProcessHost(browser_context, site_url);
2803 }
2804
2805 // Otherwise (or if that fails), create a new one.
2806 if (!render_process_host) {
2807 if (g_render_process_host_factory_) {
2808 render_process_host =
2809 g_render_process_host_factory_->CreateRenderProcessHost(
2810 browser_context, site_instance);
2811 } else {
2812 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
2813 BrowserContext::GetStoragePartition(browser_context, site_instance));
2814 render_process_host = new RenderProcessHostImpl(
2815 browser_context, partition, is_for_guests_only);
2816 }
2817 }
2818 return render_process_host;
2819 }
2820
2711 void RenderProcessHostImpl::CreateSharedRendererHistogramAllocator() { 2821 void RenderProcessHostImpl::CreateSharedRendererHistogramAllocator() {
2712 // Create a persistent memory segment for renderer histograms only if 2822 // Create a persistent memory segment for renderer histograms only if
2713 // they're active in the browser. 2823 // they're active in the browser.
2714 if (!base::GlobalHistogramAllocator::Get()) 2824 if (!base::GlobalHistogramAllocator::Get())
2715 return; 2825 return;
2716 2826
2717 // Get handle to the renderer process. Stop if there is none. 2827 // Get handle to the renderer process. Stop if there is none.
2718 base::ProcessHandle destination = GetHandle(); 2828 base::ProcessHandle destination = GetHandle();
2719 if (destination == base::kNullProcessHandle) 2829 if (destination == base::kNullProcessHandle)
2720 return; 2830 return;
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
3029 static_cast<SessionStorageHolder*>(GetUserData(kSessionStorageHolderKey)); 3139 static_cast<SessionStorageHolder*>(GetUserData(kSessionStorageHolderKey));
3030 if (!holder) 3140 if (!holder)
3031 return; 3141 return;
3032 holder->Release(old_route_id); 3142 holder->Release(old_route_id);
3033 } 3143 }
3034 3144
3035 void RenderProcessHostImpl::OnGpuSwitched() { 3145 void RenderProcessHostImpl::OnGpuSwitched() {
3036 RecomputeAndUpdateWebKitPreferences(); 3146 RecomputeAndUpdateWebKitPreferences();
3037 } 3147 }
3038 3148
3149 // static
3150 RenderProcessHost* RenderProcessHostImpl::GetDefaultSubframeProcessHost(
3151 BrowserContext* browser_context,
3152 SiteInstanceImpl* site_instance,
3153 bool is_for_guests_only) {
3154 DefaultSubframeProcessHostHolder* holder =
3155 static_cast<DefaultSubframeProcessHostHolder*>(
3156 browser_context->GetUserData(&kDefaultSubframeProcessHostHolderKey));
3157 if (!holder) {
3158 holder = new DefaultSubframeProcessHostHolder(browser_context);
3159 browser_context->SetUserData(kDefaultSubframeProcessHostHolderKey,
3160 base::WrapUnique(holder));
3161 }
3162
3163 return holder->GetProcessHost(site_instance, is_for_guests_only);
3164 }
3165
3039 #if BUILDFLAG(ENABLE_WEBRTC) 3166 #if BUILDFLAG(ENABLE_WEBRTC)
3040 void RenderProcessHostImpl::OnRegisterAecDumpConsumer(int id) { 3167 void RenderProcessHostImpl::OnRegisterAecDumpConsumer(int id) {
3041 BrowserThread::PostTask( 3168 BrowserThread::PostTask(
3042 BrowserThread::UI, FROM_HERE, 3169 BrowserThread::UI, FROM_HERE,
3043 base::Bind(&RenderProcessHostImpl::RegisterAecDumpConsumerOnUIThread, 3170 base::Bind(&RenderProcessHostImpl::RegisterAecDumpConsumerOnUIThread,
3044 weak_factory_.GetWeakPtr(), id)); 3171 weak_factory_.GetWeakPtr(), id));
3045 } 3172 }
3046 3173
3047 void RenderProcessHostImpl::OnUnregisterAecDumpConsumer(int id) { 3174 void RenderProcessHostImpl::OnUnregisterAecDumpConsumer(int id) {
3048 BrowserThread::PostTask( 3175 BrowserThread::PostTask(
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
3119 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error; 3246 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error;
3120 3247
3121 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing. 3248 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing.
3122 // Capture the error message in a crash key value. 3249 // Capture the error message in a crash key value.
3123 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error); 3250 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error);
3124 bad_message::ReceivedBadMessage(render_process_id, 3251 bad_message::ReceivedBadMessage(render_process_id,
3125 bad_message::RPH_MOJO_PROCESS_ERROR); 3252 bad_message::RPH_MOJO_PROCESS_ERROR);
3126 } 3253 }
3127 3254
3128 } // namespace content 3255 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/render_process_host_impl.h ('k') | content/browser/site_instance_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698