Chromium Code Reviews| 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 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 622 delete this; | 622 delete this; |
| 623 } | 623 } |
| 624 | 624 |
| 625 RenderProcessHost* render_process_host_; | 625 RenderProcessHost* render_process_host_; |
| 626 base::OnceClosure task_; | 626 base::OnceClosure task_; |
| 627 base::WeakPtrFactory<RenderProcessHostIsReadyObserver> weak_factory_; | 627 base::WeakPtrFactory<RenderProcessHostIsReadyObserver> weak_factory_; |
| 628 | 628 |
| 629 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostIsReadyObserver); | 629 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostIsReadyObserver); |
| 630 }; | 630 }; |
| 631 | 631 |
| 632 // The following object is used to keep track of which sites | |
|
Charlie Reis
2017/05/23 07:29:13
nit: s/object/class/
nit: used to track the sites
clamy
2017/05/23 13:41:15
Done.
| |
| 633 // RenderProcessHosts are hosting frames and expecting navigations to. | |
| 634 // There are two of them per BrowserContext, one for frames and one for | |
|
Charlie Reis
2017/05/23 07:29:13
nit: Colon rather than comma (to make it seem more
clamy
2017/05/23 13:41:15
Done.
| |
| 635 // navigations. | |
|
Charlie Reis
2017/05/23 07:29:13
Can you elaborate a bit further here, explaining h
clamy
2017/05/23 13:41:15
Done.
| |
| 636 const void* const kCommittedSiteCountPerProcessTrackerKey = | |
| 637 "CommittedSiteCountPerProcessTrackerKey"; | |
| 638 const void* const kPendingSiteCountPerProcessTrackerKey = | |
| 639 "PendingSiteCountPerProcessTrackerKey"; | |
| 640 class SiteCountPerProcessTracker : public base::SupportsUserData::Data { | |
|
Charlie Reis
2017/05/23 07:29:13
nit: Maybe SiteProcessCountTracker (now that we've
clamy
2017/05/23 13:41:15
Done.
| |
| 641 public: | |
| 642 SiteCountPerProcessTracker() {} | |
| 643 ~SiteCountPerProcessTracker() override {} | |
| 644 | |
| 645 void AddCountForSiteForProcess(int render_process_host_id, | |
|
Charlie Reis
2017/05/23 07:29:13
nit: IncrementSiteProcessCount
nit: Swap parameter
clamy
2017/05/23 13:41:15
Done.
| |
| 646 const GURL& site_url) { | |
| 647 std::map<ProcessID, Count>& counts_per_process = map_[site_url]; | |
| 648 ++counts_per_process[render_process_host_id]; | |
| 649 } | |
| 650 | |
| 651 void RemoveCountForSiteForProcess(int render_process_host_id, | |
|
Charlie Reis
2017/05/23 07:29:13
nit: DecrementSiteProcessCount and swap parameter
clamy
2017/05/23 13:41:15
Done.
| |
| 652 const GURL& site_url) { | |
| 653 auto result = map_.find(site_url); | |
| 654 DCHECK(result != map_.end()); | |
| 655 std::map<ProcessID, Count>& counts_per_process = result->second; | |
| 656 | |
| 657 --counts_per_process[render_process_host_id]; | |
| 658 DCHECK(counts_per_process[render_process_host_id] >= 0); | |
| 659 | |
| 660 if (counts_per_process[render_process_host_id] == 0) | |
| 661 counts_per_process.erase(render_process_host_id); | |
| 662 | |
| 663 if (counts_per_process.empty()) | |
| 664 map_.erase(site_url); | |
| 665 } | |
| 666 | |
| 667 void FindRenderProcessesForSite( | |
| 668 const GURL& site_url, | |
| 669 std::set<RenderProcessHost*>* foreground_processes, | |
| 670 std::set<RenderProcessHost*>* background_processes) { | |
| 671 auto result = map_.find(site_url); | |
| 672 if (result == map_.end()) | |
| 673 return; | |
| 674 | |
| 675 std::map<ProcessID, Count>& counts_per_process = result->second; | |
| 676 for (auto iter : counts_per_process) { | |
| 677 RenderProcessHost* host = RenderProcessHost::FromID(iter.first); | |
| 678 DCHECK(host); | |
| 679 if (host->VisibleWidgetCount()) | |
| 680 foreground_processes->insert(host); | |
| 681 else | |
| 682 background_processes->insert(host); | |
| 683 } | |
| 684 } | |
| 685 | |
| 686 private: | |
| 687 using ProcessID = int; | |
| 688 using Count = int; | |
| 689 using CountPerProcessPerSiteMap = std::map<GURL, std::map<ProcessID, Count>>; | |
| 690 CountPerProcessPerSiteMap map_; | |
| 691 }; | |
| 692 | |
| 632 } // namespace | 693 } // namespace |
| 633 | 694 |
| 634 RendererMainThreadFactoryFunction g_renderer_main_thread_factory = NULL; | 695 RendererMainThreadFactoryFunction g_renderer_main_thread_factory = NULL; |
| 635 | 696 |
| 636 base::MessageLoop* g_in_process_thread; | 697 base::MessageLoop* g_in_process_thread; |
| 637 | 698 |
| 638 // Stores the maximum number of renderer processes the content module can | 699 // Stores the maximum number of renderer processes the content module can |
| 639 // create. | 700 // create. |
| 640 static size_t g_max_renderer_count_override = 0; | 701 static size_t g_max_renderer_count_override = 0; |
| 641 | 702 |
| (...skipping 1099 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1741 DCHECK_GT(audio_stream_count_, 0); | 1802 DCHECK_GT(audio_stream_count_, 0); |
| 1742 --audio_stream_count_; | 1803 --audio_stream_count_; |
| 1743 UpdateProcessPriority(); | 1804 UpdateProcessPriority(); |
| 1744 } | 1805 } |
| 1745 | 1806 |
| 1746 void RenderProcessHostImpl::set_render_process_host_factory( | 1807 void RenderProcessHostImpl::set_render_process_host_factory( |
| 1747 const RenderProcessHostFactory* rph_factory) { | 1808 const RenderProcessHostFactory* rph_factory) { |
| 1748 g_render_process_host_factory_ = rph_factory; | 1809 g_render_process_host_factory_ = rph_factory; |
| 1749 } | 1810 } |
| 1750 | 1811 |
| 1812 // static | |
| 1813 void RenderProcessHostImpl::AddFrameWithSite( | |
| 1814 BrowserContext* browser_context, | |
| 1815 RenderProcessHost* render_process_host, | |
| 1816 const GURL& site_url) { | |
| 1817 if (site_url.is_empty()) | |
| 1818 return; | |
| 1819 | |
| 1820 SiteCountPerProcessTracker* tracker = | |
| 1821 static_cast<SiteCountPerProcessTracker*>(browser_context->GetUserData( | |
| 1822 kCommittedSiteCountPerProcessTrackerKey)); | |
| 1823 if (!tracker) { | |
| 1824 tracker = new SiteCountPerProcessTracker(); | |
| 1825 browser_context->SetUserData(kCommittedSiteCountPerProcessTrackerKey, | |
| 1826 base::WrapUnique(tracker)); | |
| 1827 } | |
| 1828 tracker->AddCountForSiteForProcess(render_process_host->GetID(), site_url); | |
| 1829 } | |
| 1830 | |
| 1831 // static | |
| 1832 void RenderProcessHostImpl::RemoveFrameWithSite( | |
| 1833 BrowserContext* browser_context, | |
| 1834 RenderProcessHost* render_process_host, | |
| 1835 const GURL& site_url) { | |
| 1836 if (site_url.is_empty()) | |
| 1837 return; | |
| 1838 | |
| 1839 SiteCountPerProcessTracker* tracker = | |
| 1840 static_cast<SiteCountPerProcessTracker*>(browser_context->GetUserData( | |
| 1841 kCommittedSiteCountPerProcessTrackerKey)); | |
| 1842 if (!tracker) { | |
| 1843 tracker = new SiteCountPerProcessTracker(); | |
| 1844 browser_context->SetUserData(kCommittedSiteCountPerProcessTrackerKey, | |
| 1845 base::WrapUnique(tracker)); | |
| 1846 } | |
| 1847 tracker->RemoveCountForSiteForProcess(render_process_host->GetID(), site_url); | |
| 1848 } | |
| 1849 | |
| 1850 // static | |
| 1851 void RenderProcessHostImpl::AddExpectedNavigationToSite( | |
| 1852 BrowserContext* browser_context, | |
| 1853 RenderProcessHost* render_process_host, | |
| 1854 const GURL& site_url) { | |
| 1855 if (site_url.is_empty()) | |
| 1856 return; | |
| 1857 | |
| 1858 SiteCountPerProcessTracker* tracker = | |
| 1859 static_cast<SiteCountPerProcessTracker*>( | |
| 1860 browser_context->GetUserData(kPendingSiteCountPerProcessTrackerKey)); | |
| 1861 if (!tracker) { | |
| 1862 tracker = new SiteCountPerProcessTracker(); | |
| 1863 browser_context->SetUserData(kPendingSiteCountPerProcessTrackerKey, | |
| 1864 base::WrapUnique(tracker)); | |
| 1865 } | |
| 1866 tracker->AddCountForSiteForProcess(render_process_host->GetID(), site_url); | |
| 1867 } | |
| 1868 | |
| 1869 // static | |
| 1870 void RenderProcessHostImpl::RemoveExpectedNavigationToSite( | |
| 1871 BrowserContext* browser_context, | |
| 1872 RenderProcessHost* render_process_host, | |
| 1873 const GURL& site_url) { | |
| 1874 if (site_url.is_empty()) | |
| 1875 return; | |
| 1876 | |
| 1877 SiteCountPerProcessTracker* tracker = | |
| 1878 static_cast<SiteCountPerProcessTracker*>( | |
| 1879 browser_context->GetUserData(kPendingSiteCountPerProcessTrackerKey)); | |
| 1880 if (!tracker) { | |
| 1881 tracker = new SiteCountPerProcessTracker(); | |
| 1882 browser_context->SetUserData(kPendingSiteCountPerProcessTrackerKey, | |
| 1883 base::WrapUnique(tracker)); | |
| 1884 } | |
| 1885 tracker->RemoveCountForSiteForProcess(render_process_host->GetID(), site_url); | |
| 1886 } | |
| 1887 | |
| 1751 bool RenderProcessHostImpl::IsForGuestsOnly() const { | 1888 bool RenderProcessHostImpl::IsForGuestsOnly() const { |
| 1752 return is_for_guests_only_; | 1889 return is_for_guests_only_; |
| 1753 } | 1890 } |
| 1754 | 1891 |
| 1755 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const { | 1892 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const { |
| 1756 return storage_partition_impl_; | 1893 return storage_partition_impl_; |
| 1757 } | 1894 } |
| 1758 | 1895 |
| 1759 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) { | 1896 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) { |
| 1760 command_line->AppendSwitchASCII( | 1897 command_line->AppendSwitchASCII( |
| (...skipping 1105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2866 // First, attempt to reuse an existing RenderProcessHost if necessary. | 3003 // First, attempt to reuse an existing RenderProcessHost if necessary. |
| 2867 switch (process_reuse_policy) { | 3004 switch (process_reuse_policy) { |
| 2868 case SiteInstanceImpl::ProcessReusePolicy::PROCESS_PER_SITE: | 3005 case SiteInstanceImpl::ProcessReusePolicy::PROCESS_PER_SITE: |
| 2869 render_process_host = GetProcessHostForSite(browser_context, site_url); | 3006 render_process_host = GetProcessHostForSite(browser_context, site_url); |
| 2870 break; | 3007 break; |
| 2871 case SiteInstanceImpl::ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS: | 3008 case SiteInstanceImpl::ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS: |
| 2872 DCHECK(SiteIsolationPolicy::IsTopDocumentIsolationEnabled()); | 3009 DCHECK(SiteIsolationPolicy::IsTopDocumentIsolationEnabled()); |
| 2873 render_process_host = GetDefaultSubframeProcessHost( | 3010 render_process_host = GetDefaultSubframeProcessHost( |
| 2874 browser_context, site_instance, is_for_guests_only); | 3011 browser_context, site_instance, is_for_guests_only); |
| 2875 break; | 3012 break; |
| 3013 case SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE: | |
| 3014 render_process_host = | |
| 3015 FindReusableProcessHostForSite(browser_context, site_url); | |
| 3016 break; | |
| 2876 default: | 3017 default: |
| 2877 break; | 3018 break; |
| 2878 } | 3019 } |
| 2879 | 3020 |
| 2880 // If not (or if none found), see if we should reuse an existing process. | 3021 // If not (or if none found), see if we should reuse an existing process. |
| 2881 if (!render_process_host && | 3022 if (!render_process_host && |
| 2882 ShouldTryToUseExistingProcessHost(browser_context, site_url)) { | 3023 ShouldTryToUseExistingProcessHost(browser_context, site_url)) { |
| 2883 render_process_host = GetExistingProcessHost(browser_context, site_url); | 3024 render_process_host = GetExistingProcessHost(browser_context, site_url); |
| 2884 } | 3025 } |
| 2885 | 3026 |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3258 browser_context->GetUserData(&kDefaultSubframeProcessHostHolderKey)); | 3399 browser_context->GetUserData(&kDefaultSubframeProcessHostHolderKey)); |
| 3259 if (!holder) { | 3400 if (!holder) { |
| 3260 holder = new DefaultSubframeProcessHostHolder(browser_context); | 3401 holder = new DefaultSubframeProcessHostHolder(browser_context); |
| 3261 browser_context->SetUserData(kDefaultSubframeProcessHostHolderKey, | 3402 browser_context->SetUserData(kDefaultSubframeProcessHostHolderKey, |
| 3262 base::WrapUnique(holder)); | 3403 base::WrapUnique(holder)); |
| 3263 } | 3404 } |
| 3264 | 3405 |
| 3265 return holder->GetProcessHost(site_instance, is_for_guests_only); | 3406 return holder->GetProcessHost(site_instance, is_for_guests_only); |
| 3266 } | 3407 } |
| 3267 | 3408 |
| 3409 // static | |
| 3410 RenderProcessHost* RenderProcessHostImpl::FindReusableProcessHostForSite( | |
| 3411 BrowserContext* browser_context, | |
| 3412 const GURL& site_url) { | |
| 3413 if (site_url.is_empty()) | |
| 3414 return nullptr; | |
| 3415 | |
| 3416 std::set<RenderProcessHost*> eligible_foreground_hosts; | |
| 3417 std::set<RenderProcessHost*> eligible_background_hosts; | |
| 3418 | |
| 3419 // Add the RenderProcessHosts hosting a frame for |site_url| to the list of | |
| 3420 // eligible RenderProcessHosts. | |
| 3421 SiteCountPerProcessTracker* committed_tracker = | |
| 3422 static_cast<SiteCountPerProcessTracker*>(browser_context->GetUserData( | |
| 3423 kCommittedSiteCountPerProcessTrackerKey)); | |
| 3424 if (committed_tracker) { | |
| 3425 committed_tracker->FindRenderProcessesForSite( | |
| 3426 site_url, &eligible_foreground_hosts, &eligible_background_hosts); | |
| 3427 } | |
| 3428 | |
| 3429 // Add the RenderProcessHosts expecting a navigation to |site_url| to the list | |
| 3430 // of eligible RenderProcessHosts. | |
| 3431 SiteCountPerProcessTracker* pending_tracker = | |
| 3432 static_cast<SiteCountPerProcessTracker*>( | |
| 3433 browser_context->GetUserData(kPendingSiteCountPerProcessTrackerKey)); | |
| 3434 if (pending_tracker) { | |
| 3435 pending_tracker->FindRenderProcessesForSite( | |
| 3436 site_url, &eligible_foreground_hosts, &eligible_background_hosts); | |
|
Charlie Reis
2017/05/23 07:29:14
Should there be any preference between processes t
clamy
2017/05/23 13:41:15
I think it would prioritize the navigating ones to
| |
| 3437 } | |
| 3438 | |
| 3439 if (!eligible_foreground_hosts.empty()) { | |
| 3440 int index = base::RandInt(0, eligible_foreground_hosts.size() - 1); | |
| 3441 auto iterator = eligible_foreground_hosts.begin(); | |
| 3442 for (int i = 0; i < index; ++i) | |
| 3443 ++iterator; | |
| 3444 return (*iterator); | |
| 3445 } | |
| 3446 | |
| 3447 if (!eligible_background_hosts.empty()) { | |
| 3448 int index = base::RandInt(0, eligible_background_hosts.size() - 1); | |
| 3449 auto iterator = eligible_background_hosts.begin(); | |
| 3450 for (int i = 0; i < index; ++i) | |
| 3451 ++iterator; | |
| 3452 return (*iterator); | |
| 3453 } | |
| 3454 | |
| 3455 return nullptr; | |
| 3456 } | |
| 3457 | |
| 3268 #if BUILDFLAG(ENABLE_WEBRTC) | 3458 #if BUILDFLAG(ENABLE_WEBRTC) |
| 3269 void RenderProcessHostImpl::OnRegisterAecDumpConsumer(int id) { | 3459 void RenderProcessHostImpl::OnRegisterAecDumpConsumer(int id) { |
| 3270 BrowserThread::PostTask( | 3460 BrowserThread::PostTask( |
| 3271 BrowserThread::UI, FROM_HERE, | 3461 BrowserThread::UI, FROM_HERE, |
| 3272 base::Bind(&RenderProcessHostImpl::RegisterAecDumpConsumerOnUIThread, | 3462 base::Bind(&RenderProcessHostImpl::RegisterAecDumpConsumerOnUIThread, |
| 3273 weak_factory_.GetWeakPtr(), id)); | 3463 weak_factory_.GetWeakPtr(), id)); |
| 3274 } | 3464 } |
| 3275 | 3465 |
| 3276 void RenderProcessHostImpl::OnUnregisterAecDumpConsumer(int id) { | 3466 void RenderProcessHostImpl::OnUnregisterAecDumpConsumer(int id) { |
| 3277 BrowserThread::PostTask( | 3467 BrowserThread::PostTask( |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3348 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error; | 3538 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error; |
| 3349 | 3539 |
| 3350 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing. | 3540 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing. |
| 3351 // Capture the error message in a crash key value. | 3541 // Capture the error message in a crash key value. |
| 3352 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error); | 3542 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error); |
| 3353 bad_message::ReceivedBadMessage(render_process_id, | 3543 bad_message::ReceivedBadMessage(render_process_id, |
| 3354 bad_message::RPH_MOJO_PROCESS_ERROR); | 3544 bad_message::RPH_MOJO_PROCESS_ERROR); |
| 3355 } | 3545 } |
| 3356 | 3546 |
| 3357 } // namespace content | 3547 } // namespace content |
| OLD | NEW |