OLD | NEW |
---|---|
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 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 void Release(int old_route_id) { | 456 void Release(int old_route_id) { |
457 session_storage_namespaces_awaiting_close_->erase(old_route_id); | 457 session_storage_namespaces_awaiting_close_->erase(old_route_id); |
458 } | 458 } |
459 | 459 |
460 private: | 460 private: |
461 std::unique_ptr<std::map<int, SessionStorageNamespaceMap>> | 461 std::unique_ptr<std::map<int, SessionStorageNamespaceMap>> |
462 session_storage_namespaces_awaiting_close_; | 462 session_storage_namespaces_awaiting_close_; |
463 DISALLOW_COPY_AND_ASSIGN(SessionStorageHolder); | 463 DISALLOW_COPY_AND_ASSIGN(SessionStorageHolder); |
464 }; | 464 }; |
465 | 465 |
466 // This class manages spare RenderProcessHosts. | |
467 // | |
468 // There is a singleton instance of this class which manages a single spare | |
469 // renderer (g_spare_render_process_host_manager, below). This class | |
470 // encapsulates the implementation of | |
471 // RenderProcessHost::WarmupSpareRenderProcessHost() | |
472 // | |
473 // RenderProcessHostImpl should call | |
474 // SpareRenderProcessHostManager::MaybeTakeSpareRenderProcessHost when creating | |
475 // a new RPH. In this implementation, the spare renderer is bound to a | |
476 // BrowserContext and its default StoragePartition. If | |
477 // MaybeTakeSpareRenderProcessHost is called with a BrowserContext and | |
478 // StoragePartition that does not match, the spare renderer is discarded. In | |
479 // particular, only the default StoragePartition will be able to use a spare | |
480 // renderer. The spare renderer will also not be used as a guest renderer | |
481 // (is_for_guests_ == true). | |
482 // | |
483 // It is safe to call WarmupSpareRenderProcessHost multiple times, although if | |
484 // called in a context where the spare renderer is not likely to be used | |
485 // performance may suffer due to the unnecessary RPH creation. | |
486 class SpareRenderProcessHostManager : public RenderProcessHostObserver { | |
487 public: | |
488 SpareRenderProcessHostManager() {} | |
489 | |
490 void WarmupSpareRenderProcessHost(BrowserContext* browser_context) { | |
491 StoragePartitionImpl* current_partition = | |
492 static_cast<StoragePartitionImpl*>( | |
493 BrowserContext::GetStoragePartition(browser_context, nullptr)); | |
494 | |
495 if (spare_render_process_host_ && | |
496 matching_browser_context_ == browser_context && | |
497 matching_storage_partition_ == current_partition) | |
498 return; // Nothing to warm up. | |
499 | |
500 CleanupSpareRenderProcessHost(); | |
501 | |
502 // Don't create a spare renderer if we've got too many processes. See also | |
503 // ShouldTryToUseExistingProcessHost in this file. | |
Charlie Reis
2017/06/26 21:22:50
Yeah, we'd kind of like to just return early if Sh
mattcary
2017/06/28 13:14:38
Good point, done.
| |
504 if (g_all_hosts.Get().size() >= | |
505 RenderProcessHostImpl::GetMaxRendererProcessCount()) | |
506 return; | |
507 | |
508 matching_browser_context_ = browser_context; | |
509 matching_storage_partition_ = current_partition; | |
510 | |
511 spare_render_process_host_ = RenderProcessHostImpl::CreateRenderProcessHost( | |
512 browser_context, current_partition, false /* is_for_guests_only */); | |
513 spare_render_process_host_->AddObserver(this); | |
514 spare_render_process_host_->Init(); | |
515 } | |
516 | |
517 RenderProcessHost* MaybeTakeSpareRenderProcessHost( | |
518 const BrowserContext* browser_context, | |
519 const StoragePartition* partition, | |
520 bool is_for_guests_only) { | |
521 if (!spare_render_process_host_ || | |
522 browser_context != matching_browser_context_ || | |
523 partition != matching_storage_partition_ || is_for_guests_only) { | |
524 // As a new RenderProcessHost will almost certainly be created, we cleanup | |
525 // the non-matching one so as not to waste resources. | |
526 CleanupSpareRenderProcessHost(); | |
527 return nullptr; | |
528 } | |
529 | |
530 RenderProcessHost* rph = spare_render_process_host_; | |
531 DropSpareRenderProcessHost(spare_render_process_host_); | |
532 return rph; | |
533 } | |
534 | |
535 void OnStoragePartitionShutdown(StoragePartition* partition) { | |
536 if (partition == matching_storage_partition_) | |
537 CleanupSpareRenderProcessHost(); | |
538 } | |
539 | |
540 RenderProcessHost* spare_render_process_host() { | |
541 return spare_render_process_host_; | |
542 } | |
543 | |
544 private: | |
545 // RenderProcessHostObserver | |
546 void RenderProcessWillExit(RenderProcessHost* host) override { | |
547 DropSpareRenderProcessHost(host); | |
548 } | |
549 | |
550 void RenderProcessExited(RenderProcessHost* host, | |
551 base::TerminationStatus unused_status, | |
552 int unused_exit_code) override { | |
553 DropSpareRenderProcessHost(host); | |
554 } | |
555 | |
556 void RenderProcessHostDestroyed(RenderProcessHost* host) override { | |
557 DropSpareRenderProcessHost(host); | |
558 } | |
559 | |
560 void CleanupSpareRenderProcessHost() { | |
561 if (spare_render_process_host_) { | |
562 spare_render_process_host_->Cleanup(); | |
563 DropSpareRenderProcessHost(spare_render_process_host_); | |
564 } | |
565 } | |
566 | |
567 // Remove |host| as a possible spare renderer. Does not shut it down cleanly; | |
568 // the assumption is that the host was shutdown somewhere else and has | |
569 // notifying the SpareRenderProcessHostManager. | |
570 void DropSpareRenderProcessHost(RenderProcessHost* host) { | |
571 if (host && host == spare_render_process_host_) { | |
572 spare_render_process_host_->RemoveObserver(this); | |
573 spare_render_process_host_ = nullptr; | |
574 } | |
575 } | |
576 | |
577 // This is a bare pointer, because RenderProcessHost manages the lifetime of | |
578 // all its instances; see g_all_hosts, above. | |
579 RenderProcessHost* spare_render_process_host_; | |
580 | |
581 // Used only to check if a creation request matches the spare, and not | |
582 // accessed. | |
583 const BrowserContext* matching_browser_context_ = nullptr; | |
584 const StoragePartition* matching_storage_partition_ = nullptr; | |
585 | |
586 DISALLOW_COPY_AND_ASSIGN(SpareRenderProcessHostManager); | |
587 }; | |
588 | |
589 base::LazyInstance<SpareRenderProcessHostManager>::Leaky | |
590 g_spare_render_process_host_manager = LAZY_INSTANCE_INITIALIZER; | |
591 | |
466 const void* const kDefaultSubframeProcessHostHolderKey = | 592 const void* const kDefaultSubframeProcessHostHolderKey = |
467 &kDefaultSubframeProcessHostHolderKey; | 593 &kDefaultSubframeProcessHostHolderKey; |
468 | 594 |
469 class DefaultSubframeProcessHostHolder : public base::SupportsUserData::Data, | 595 class DefaultSubframeProcessHostHolder : public base::SupportsUserData::Data, |
470 public RenderProcessHostObserver { | 596 public RenderProcessHostObserver { |
471 public: | 597 public: |
472 explicit DefaultSubframeProcessHostHolder(BrowserContext* browser_context) | 598 explicit DefaultSubframeProcessHostHolder(BrowserContext* browser_context) |
473 : browser_context_(browser_context) {} | 599 : browser_context_(browser_context) {} |
474 ~DefaultSubframeProcessHostHolder() override {} | 600 ~DefaultSubframeProcessHostHolder() override {} |
475 | 601 |
476 // Gets the correct render process to use for this SiteInstance. | 602 // Gets the correct render process to use for this SiteInstance. |
477 RenderProcessHost* GetProcessHost(SiteInstance* site_instance, | 603 RenderProcessHost* GetProcessHost(SiteInstance* site_instance, |
478 bool is_for_guests_only) { | 604 bool is_for_guests_only) { |
479 StoragePartition* default_partition = | 605 StoragePartition* default_partition = |
480 BrowserContext::GetDefaultStoragePartition(browser_context_); | 606 BrowserContext::GetDefaultStoragePartition(browser_context_); |
481 StoragePartition* partition = | 607 StoragePartition* partition = |
482 BrowserContext::GetStoragePartition(browser_context_, site_instance); | 608 BrowserContext::GetStoragePartition(browser_context_, site_instance); |
483 | 609 |
484 // Is this the default storage partition? If it isn't, then just give it its | 610 // Is this the default storage partition? If it isn't, then just give it its |
485 // own non-shared process. | 611 // own non-shared process. |
486 if (partition != default_partition || is_for_guests_only) { | 612 if (partition != default_partition || is_for_guests_only) { |
487 RenderProcessHostImpl* host = new RenderProcessHostImpl( | 613 RenderProcessHost* host = RenderProcessHostImpl::CreateRenderProcessHost( |
488 browser_context_, static_cast<StoragePartitionImpl*>(partition), | 614 browser_context_, static_cast<StoragePartitionImpl*>(partition), |
489 is_for_guests_only); | 615 is_for_guests_only); |
490 host->SetIsNeverSuitableForReuse(); | 616 host->SetIsNeverSuitableForReuse(); |
491 return host; | 617 return host; |
492 } | 618 } |
493 | 619 |
494 // If we already have a shared host for the default storage partition, use | 620 // If we already have a shared host for the default storage partition, use |
495 // it. | 621 // it. |
496 if (host_) | 622 if (host_) |
497 return host_; | 623 return host_; |
498 | 624 |
499 host_ = new RenderProcessHostImpl( | 625 // Before creating a new process, try to take a spare renderer. |
626 host_ = | |
627 g_spare_render_process_host_manager.Get() | |
628 .MaybeTakeSpareRenderProcessHost(browser_context_, partition, | |
629 false /* is for guests only */); | |
630 | |
631 host_ = RenderProcessHostImpl::CreateRenderProcessHost( | |
500 browser_context_, static_cast<StoragePartitionImpl*>(partition), | 632 browser_context_, static_cast<StoragePartitionImpl*>(partition), |
501 false /* for guests only */); | 633 false /* for guests only */); |
502 host_->SetIsNeverSuitableForReuse(); | 634 host_->SetIsNeverSuitableForReuse(); |
503 host_->AddObserver(this); | 635 host_->AddObserver(this); |
504 | 636 |
505 return host_; | 637 return host_; |
506 } | 638 } |
507 | 639 |
508 // Implementation of RenderProcessHostObserver. | 640 // Implementation of RenderProcessHostObserver. |
509 void RenderProcessHostDestroyed(RenderProcessHost* host) override { | 641 void RenderProcessHostDestroyed(RenderProcessHost* host) override { |
510 DCHECK_EQ(host_, host); | 642 DCHECK_EQ(host_, host); |
511 host_->RemoveObserver(this); | 643 host_->RemoveObserver(this); |
512 host_ = nullptr; | 644 host_ = nullptr; |
513 } | 645 } |
514 | 646 |
515 private: | 647 private: |
516 BrowserContext* browser_context_; | 648 BrowserContext* browser_context_; |
517 | 649 |
518 // The default subframe render process used for the default storage partition | 650 // The default subframe render process used for the default storage partition |
519 // of this BrowserContext. | 651 // of this BrowserContext. |
520 RenderProcessHostImpl* host_ = nullptr; | 652 RenderProcessHost* host_ = nullptr; |
521 }; | 653 }; |
522 | 654 |
523 void CreateMemoryCoordinatorHandle( | 655 void CreateMemoryCoordinatorHandle( |
524 int render_process_id, | 656 int render_process_id, |
525 const service_manager::BindSourceInfo& source_info, | 657 const service_manager::BindSourceInfo& source_info, |
526 mojom::MemoryCoordinatorHandleRequest request) { | 658 mojom::MemoryCoordinatorHandleRequest request) { |
527 MemoryCoordinatorImpl::GetInstance()->CreateHandle(render_process_id, | 659 MemoryCoordinatorImpl::GetInstance()->CreateHandle(render_process_id, |
528 std::move(request)); | 660 std::move(request)); |
529 } | 661 } |
530 | 662 |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
908 max_count = std::min(max_count, kMaxRendererProcessCount); | 1040 max_count = std::min(max_count, kMaxRendererProcessCount); |
909 } | 1041 } |
910 return max_count; | 1042 return max_count; |
911 } | 1043 } |
912 | 1044 |
913 // static | 1045 // static |
914 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { | 1046 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { |
915 g_max_renderer_count_override = count; | 1047 g_max_renderer_count_override = count; |
916 } | 1048 } |
917 | 1049 |
1050 // static | |
1051 RenderProcessHost* RenderProcessHostImpl::CreateRenderProcessHost( | |
1052 BrowserContext* browser_context, | |
1053 StoragePartitionImpl* storage_partition_impl, | |
1054 bool is_for_guests_only) { | |
1055 if (g_render_process_host_factory_) { | |
1056 return g_render_process_host_factory_->CreateRenderProcessHost( | |
1057 browser_context); | |
1058 } | |
1059 | |
1060 return new RenderProcessHostImpl(browser_context, storage_partition_impl, | |
1061 is_for_guests_only); | |
1062 } | |
1063 | |
918 RenderProcessHostImpl::RenderProcessHostImpl( | 1064 RenderProcessHostImpl::RenderProcessHostImpl( |
919 BrowserContext* browser_context, | 1065 BrowserContext* browser_context, |
920 StoragePartitionImpl* storage_partition_impl, | 1066 StoragePartitionImpl* storage_partition_impl, |
921 bool is_for_guests_only) | 1067 bool is_for_guests_only) |
922 : fast_shutdown_started_(false), | 1068 : fast_shutdown_started_(false), |
923 deleting_soon_(false), | 1069 deleting_soon_(false), |
924 #ifndef NDEBUG | 1070 #ifndef NDEBUG |
925 is_self_deleted_(false), | 1071 is_self_deleted_(false), |
926 #endif | 1072 #endif |
927 pending_views_(0), | 1073 pending_views_(0), |
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1979 SiteProcessCountTracker* tracker = static_cast<SiteProcessCountTracker*>( | 2125 SiteProcessCountTracker* tracker = static_cast<SiteProcessCountTracker*>( |
1980 browser_context->GetUserData(kPendingSiteProcessCountTrackerKey)); | 2126 browser_context->GetUserData(kPendingSiteProcessCountTrackerKey)); |
1981 if (!tracker) { | 2127 if (!tracker) { |
1982 tracker = new SiteProcessCountTracker(); | 2128 tracker = new SiteProcessCountTracker(); |
1983 browser_context->SetUserData(kPendingSiteProcessCountTrackerKey, | 2129 browser_context->SetUserData(kPendingSiteProcessCountTrackerKey, |
1984 base::WrapUnique(tracker)); | 2130 base::WrapUnique(tracker)); |
1985 } | 2131 } |
1986 tracker->DecrementSiteProcessCount(site_url, render_process_host->GetID()); | 2132 tracker->DecrementSiteProcessCount(site_url, render_process_host->GetID()); |
1987 } | 2133 } |
1988 | 2134 |
2135 // static | |
2136 void RenderProcessHostImpl::OnStoragePartitionShutdown( | |
2137 StoragePartition* partition) { | |
2138 g_spare_render_process_host_manager.Get().OnStoragePartitionShutdown( | |
2139 partition); | |
2140 } | |
2141 | |
2142 // static | |
2143 RenderProcessHost* | |
2144 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting() { | |
2145 return g_spare_render_process_host_manager.Get().spare_render_process_host(); | |
2146 } | |
2147 | |
1989 bool RenderProcessHostImpl::IsForGuestsOnly() const { | 2148 bool RenderProcessHostImpl::IsForGuestsOnly() const { |
1990 return is_for_guests_only_; | 2149 return is_for_guests_only_; |
1991 } | 2150 } |
1992 | 2151 |
1993 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const { | 2152 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const { |
1994 return storage_partition_impl_; | 2153 return storage_partition_impl_; |
1995 } | 2154 } |
1996 | 2155 |
1997 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) { | 2156 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) { |
1998 command_line->AppendSwitchASCII( | 2157 command_line->AppendSwitchASCII( |
(...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2918 host->GetID()) != | 3077 host->GetID()) != |
2919 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIBindingsForURL( | 3078 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIBindingsForURL( |
2920 browser_context, site_url)) { | 3079 browser_context, site_url)) { |
2921 return false; | 3080 return false; |
2922 } | 3081 } |
2923 | 3082 |
2924 return GetContentClient()->browser()->IsSuitableHost(host, site_url); | 3083 return GetContentClient()->browser()->IsSuitableHost(host, site_url); |
2925 } | 3084 } |
2926 | 3085 |
2927 // static | 3086 // static |
3087 void RenderProcessHost::WarmupSpareRenderProcessHost( | |
3088 content::BrowserContext* browser_context) { | |
3089 g_spare_render_process_host_manager.Get().WarmupSpareRenderProcessHost( | |
3090 browser_context); | |
3091 } | |
3092 | |
3093 // static | |
2928 bool RenderProcessHost::run_renderer_in_process() { | 3094 bool RenderProcessHost::run_renderer_in_process() { |
2929 return g_run_renderer_in_process_; | 3095 return g_run_renderer_in_process_; |
2930 } | 3096 } |
2931 | 3097 |
2932 // static | 3098 // static |
2933 void RenderProcessHost::SetRunRendererInProcess(bool value) { | 3099 void RenderProcessHost::SetRunRendererInProcess(bool value) { |
2934 g_run_renderer_in_process_ = value; | 3100 g_run_renderer_in_process_ = value; |
2935 | 3101 |
2936 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 3102 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
2937 if (value) { | 3103 if (value) { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3116 default: | 3282 default: |
3117 break; | 3283 break; |
3118 } | 3284 } |
3119 | 3285 |
3120 // If not (or if none found), see if we should reuse an existing process. | 3286 // If not (or if none found), see if we should reuse an existing process. |
3121 if (!render_process_host && | 3287 if (!render_process_host && |
3122 ShouldTryToUseExistingProcessHost(browser_context, site_url)) { | 3288 ShouldTryToUseExistingProcessHost(browser_context, site_url)) { |
3123 render_process_host = GetExistingProcessHost(browser_context, site_url); | 3289 render_process_host = GetExistingProcessHost(browser_context, site_url); |
3124 } | 3290 } |
3125 | 3291 |
3292 // Before creating a new process, try to use a spare process if one is found. | |
3293 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( | |
3294 BrowserContext::GetStoragePartition(browser_context, site_instance)); | |
3295 if (!render_process_host) { | |
3296 render_process_host = | |
3297 g_spare_render_process_host_manager.Get() | |
3298 .MaybeTakeSpareRenderProcessHost(browser_context, partition, | |
3299 is_for_guests_only); | |
3300 } | |
3301 | |
3126 // Otherwise (or if that fails), create a new one. | 3302 // Otherwise (or if that fails), create a new one. |
3127 if (!render_process_host) { | 3303 if (!render_process_host) { |
3128 if (g_render_process_host_factory_) { | 3304 render_process_host = |
3129 render_process_host = | 3305 CreateRenderProcessHost(browser_context, partition, is_for_guests_only); |
3130 g_render_process_host_factory_->CreateRenderProcessHost( | |
3131 browser_context); | |
3132 } else { | |
3133 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( | |
3134 BrowserContext::GetStoragePartition(browser_context, site_instance)); | |
3135 render_process_host = new RenderProcessHostImpl( | |
3136 browser_context, partition, is_for_guests_only); | |
3137 } | |
3138 } | 3306 } |
3307 | |
3308 DCHECK(render_process_host); | |
3139 return render_process_host; | 3309 return render_process_host; |
3140 } | 3310 } |
3141 | 3311 |
3142 void RenderProcessHostImpl::CreateSharedRendererHistogramAllocator() { | 3312 void RenderProcessHostImpl::CreateSharedRendererHistogramAllocator() { |
3143 // Create a persistent memory segment for renderer histograms only if | 3313 // Create a persistent memory segment for renderer histograms only if |
3144 // they're active in the browser. | 3314 // they're active in the browser. |
3145 if (!base::GlobalHistogramAllocator::Get()) | 3315 if (!base::GlobalHistogramAllocator::Get()) |
3146 return; | 3316 return; |
3147 | 3317 |
3148 // Get handle to the renderer process. Stop if there is none. | 3318 // Get handle to the renderer process. Stop if there is none. |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3635 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error; | 3805 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error; |
3636 | 3806 |
3637 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing. | 3807 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing. |
3638 // Capture the error message in a crash key value. | 3808 // Capture the error message in a crash key value. |
3639 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error); | 3809 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error); |
3640 bad_message::ReceivedBadMessage(render_process_id, | 3810 bad_message::ReceivedBadMessage(render_process_id, |
3641 bad_message::RPH_MOJO_PROCESS_ERROR); | 3811 bad_message::RPH_MOJO_PROCESS_ERROR); |
3642 } | 3812 } |
3643 | 3813 |
3644 } // namespace content | 3814 } // namespace content |
OLD | NEW |