From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Mon, 3 Jun 2019 14:20:05 -0700 Subject: frame_host_manager.patch Allows embedder to intercept site instances chosen by chromium and respond with custom instance. Also allows for us to at-runtime enable or disable this patch. diff --git a/content/browser/browsing_instance.cc b/content/browser/browsing_instance.cc index 54546d15ba4006481ca8844e26b86359d3eb8a79..7712c681170ddf6739b2825b4b2fb9284a23f4b1 100644 --- a/content/browser/browsing_instance.cc +++ b/content/browser/browsing_instance.cc @@ -83,6 +83,13 @@ scoped_refptr BrowsingInstance::GetSiteInstanceForURL( return instance; } +scoped_refptr BrowsingInstance::CreateSiteInstanceForURL( + const GURL& url) { + scoped_refptr instance = new SiteInstanceImpl(this); + instance->SetSite(url); + return instance; +} + SiteInfo BrowsingInstance::GetSiteInfoForURL(const GURL& url, bool allow_default_instance) { scoped_refptr site_instance = diff --git a/content/browser/browsing_instance.h b/content/browser/browsing_instance.h index 7f588450c441ed2b17db06afb38075bbfadfc633..e56178401374f3c6a9536056511fb63a20195e56 100644 --- a/content/browser/browsing_instance.h +++ b/content/browser/browsing_instance.h @@ -134,6 +134,11 @@ class CONTENT_EXPORT BrowsingInstance final const GURL& url, bool allow_default_instance); + // Create a new SiteInstance for the given URL bound the current + // BrowsingInstance. + scoped_refptr CreateSiteInstanceForURL( + const GURL& url); + // Adds the given SiteInstance to our map, to ensure that we do not create // another SiteInstance for the same site. void RegisterSiteInstance(SiteInstanceImpl* site_instance); diff --git a/content/browser/frame_host/navigation_request.cc b/content/browser/frame_host/navigation_request.cc index a6d3bafd08336a07012e46aba6d611c62e61c7cf..f7b03517380b653071384a9686924b584efadde7 100644 --- a/content/browser/frame_host/navigation_request.cc +++ b/content/browser/frame_host/navigation_request.cc @@ -1475,6 +1475,21 @@ void NavigationRequest::BeginNavigation() { // it immediately. EnterChildTraceEvent("ResponseStarted", this); + // In Electron, a new process is started for every navigation when node is + // integrated in the renderer. Since GetFrameHostForNavigation is called more + // than once per navigation, we want to return the same frame host once + // the response has started, otherwise if the frame host that started the response + // and the frame host that is available for commit don't match then request will + // be cancelled. We rely on the NavigationRequest::state_ to determine this + // factor + // + // bool has_response_started = + // (request->state() >= NavigationRequest::WILL_PROCESS_RESPONSE && + // !speculative_render_frame_host_); + // + // Hence this call is necesary before the call to GetFrameHostForNavigation. + SetState(REUSE_SITE_INSTANCE); + // Select an appropriate RenderFrameHost. render_frame_host_ = frame_tree_node_->render_manager()->GetFrameHostForNavigation(this); @@ -5043,6 +5058,7 @@ void NavigationRequest::CheckStateTransition(NavigationState state) const { {WILL_START_REQUEST, { WILL_REDIRECT_REQUEST, WILL_PROCESS_RESPONSE, + REUSE_SITE_INSTANCE, READY_TO_COMMIT, DID_COMMIT, CANCELING, @@ -5056,10 +5072,14 @@ void NavigationRequest::CheckStateTransition(NavigationState state) const { WILL_FAIL_REQUEST, }}, {WILL_PROCESS_RESPONSE, { + REUSE_SITE_INSTANCE, READY_TO_COMMIT, CANCELING, WILL_FAIL_REQUEST, }}, + {REUSE_SITE_INSTANCE, { + READY_TO_COMMIT, + }}, {READY_TO_COMMIT, { NOT_STARTED, DID_COMMIT, diff --git a/content/browser/frame_host/navigation_request.h b/content/browser/frame_host/navigation_request.h index cfcc6cd6cbc2723156a2bce1cd8a98a979952a04..a6736ae58641501041a676505ea8a3680f90cdf7 100644 --- a/content/browser/frame_host/navigation_request.h +++ b/content/browser/frame_host/navigation_request.h @@ -162,6 +162,10 @@ class CONTENT_EXPORT NavigationRequest // asynchronous. WILL_PROCESS_RESPONSE, + // Electron: state that is between than WILL_PROCESS_RESPONSE and + // READY_TO_COMMIT to force reuse the same site instance. + REUSE_SITE_INSTANCE, + // The response started on the IO thread and is ready to be committed. READY_TO_COMMIT, diff --git a/content/browser/frame_host/render_frame_host_manager.cc b/content/browser/frame_host/render_frame_host_manager.cc index 3223378b661e6a53e62d7981c90beb67a51dc900..f83175d673e280e81ae3175d98172763f9c016ae 100644 --- a/content/browser/frame_host/render_frame_host_manager.cc +++ b/content/browser/frame_host/render_frame_host_manager.cc @@ -2383,6 +2383,16 @@ bool RenderFrameHostManager::InitRenderView( scoped_refptr RenderFrameHostManager::GetSiteInstanceForNavigationRequest( NavigationRequest* request) { + // Compute the SiteInstance that the navigation should use, which will be + // either the current SiteInstance or a new one. + // + // TODO(clamy): We should also consider as a candidate SiteInstance the + // speculative SiteInstance that was computed on redirects. + SiteInstanceImpl* candidate_site_instance = + speculative_render_frame_host_ + ? speculative_render_frame_host_->GetSiteInstance() + : nullptr; + SiteInstance* current_site_instance = render_frame_host_->GetSiteInstance(); // All children of MHTML documents must be MHTML documents. They all live in @@ -2402,10 +2412,60 @@ RenderFrameHostManager::GetSiteInstanceForNavigationRequest( // // TODO(clamy): We should also consider as a candidate SiteInstance the // speculative SiteInstance that was computed on redirects. - SiteInstanceImpl* candidate_site_instance = - speculative_render_frame_host_ - ? speculative_render_frame_host_->GetSiteInstance() - : nullptr; + if (!GetContentClient()->browser()->CanUseCustomSiteInstance() && + frame_tree_node_->IsMainFrame()) { + BrowserContext* browser_context = + delegate_->GetControllerForRenderManager().GetBrowserContext(); + bool has_navigation_started = request->state() != NavigationRequest::NOT_STARTED; + bool has_response_started = + (request->state() >= NavigationRequest::WILL_PROCESS_RESPONSE && + !speculative_render_frame_host_); + // Gives user a chance to choose a custom site instance. + SiteInstance* affinity_site_instance = nullptr; + scoped_refptr overriden_site_instance; + bool should_register_site_instance = false; + ContentBrowserClient::SiteInstanceForNavigationType siteInstanceType = + GetContentClient()->browser()->ShouldOverrideSiteInstanceForNavigation( + current_frame_host(), speculative_frame_host(), browser_context, + request->common_params().url, has_navigation_started, + has_response_started, &affinity_site_instance); + switch (siteInstanceType) { + case ContentBrowserClient::SiteInstanceForNavigationType:: + FORCE_CANDIDATE_OR_NEW: + overriden_site_instance = + candidate_site_instance + ? candidate_site_instance + : current_site_instance->CreateRelatedSiteInstance( + request->common_params().url); + should_register_site_instance = true; + break; + case ContentBrowserClient::SiteInstanceForNavigationType::FORCE_NEW: + overriden_site_instance = current_site_instance->CreateRelatedSiteInstance( + request->common_params().url); + should_register_site_instance = true; + break; + case ContentBrowserClient::SiteInstanceForNavigationType::FORCE_CURRENT: + overriden_site_instance = render_frame_host_->GetSiteInstance(); + break; + case ContentBrowserClient::SiteInstanceForNavigationType::FORCE_AFFINITY: + DCHECK(affinity_site_instance); + overriden_site_instance = + scoped_refptr(affinity_site_instance); + break; + case ContentBrowserClient::SiteInstanceForNavigationType::ASK_CHROMIUM: + DCHECK(!affinity_site_instance); + break; + default: + break; + } + if (overriden_site_instance) { + if (should_register_site_instance) { + GetContentClient()->browser()->RegisterPendingSiteInstance( + render_frame_host_.get(), overriden_site_instance.get()); + } + return overriden_site_instance; + } + } // Account for renderer-initiated reload as well. // Needed as a workaround for https://crbug.com/1045524, remove it when it is @@ -2431,6 +2491,9 @@ RenderFrameHostManager::GetSiteInstanceForNavigationRequest( request->ResetStateForSiteInstanceChange(); } + GetContentClient()->browser()->RegisterPendingSiteInstance( + render_frame_host_.get(), dest_site_instance.get()); + return dest_site_instance; } diff --git a/content/browser/site_instance_impl.cc b/content/browser/site_instance_impl.cc index b167aaf428df5feeac16c68beec3e947d44cd61f..c2ad9320b242c228b295a7fe20cc258282d95a09 100644 --- a/content/browser/site_instance_impl.cc +++ b/content/browser/site_instance_impl.cc @@ -505,6 +505,10 @@ bool SiteInstanceImpl::HasRelatedSiteInstance(const SiteInfo& site_info) { return browsing_instance_->HasSiteInstance(site_info); } +scoped_refptr SiteInstanceImpl::CreateRelatedSiteInstance(const GURL& url) { + return browsing_instance_->CreateSiteInstanceForURL(url); +} + scoped_refptr SiteInstanceImpl::GetRelatedSiteInstance( const GURL& url) { return browsing_instance_->GetSiteInstanceForURL( diff --git a/content/browser/site_instance_impl.h b/content/browser/site_instance_impl.h index 9fffa8c2c602aa373400dfb0d1c817f34b75bc8f..f898ab9291f66ac0cfbf0317f47befc8d832b364 100644 --- a/content/browser/site_instance_impl.h +++ b/content/browser/site_instance_impl.h @@ -184,6 +184,7 @@ class CONTENT_EXPORT SiteInstanceImpl final : public SiteInstance, BrowserContext* GetBrowserContext() override; const GURL& GetSiteURL() override; scoped_refptr GetRelatedSiteInstance(const GURL& url) override; + scoped_refptr CreateRelatedSiteInstance(const GURL& url) override; bool IsRelatedSiteInstance(const SiteInstance* instance) override; size_t GetRelatedActiveContentsCount() override; bool RequiresDedicatedProcess() override; diff --git a/content/public/browser/content_browser_client.cc b/content/public/browser/content_browser_client.cc index 0e150a89b451e7ae464f1bd5e38c412f58aa8568..187e8bc17e2dfb662c283bacaeda9764c2fdc11e 100644 --- a/content/public/browser/content_browser_client.cc +++ b/content/public/browser/content_browser_client.cc @@ -62,6 +62,21 @@ namespace content { +bool ContentBrowserClient::CanUseCustomSiteInstance() { + return false; +} + +ContentBrowserClient::SiteInstanceForNavigationType ContentBrowserClient::ShouldOverrideSiteInstanceForNavigation( + content::RenderFrameHost* current_rfh, + content::RenderFrameHost* speculative_rfh, + content::BrowserContext* browser_context, + const GURL& url, + bool has_navigation_started, + bool has_request_started, + content::SiteInstance** affinity_site_instance) const { + return SiteInstanceForNavigationType::ASK_CHROMIUM; +} + std::unique_ptr ContentBrowserClient::CreateBrowserMainParts( const MainFunctionParams& parameters) { return nullptr; diff --git a/content/public/browser/content_browser_client.h b/content/public/browser/content_browser_client.h index 170beaff3f579d3fcf9c461f6a41350e8c9439cc..a29cde8ecd97077c4e60e40e2c91083275781602 100644 --- a/content/public/browser/content_browser_client.h +++ b/content/public/browser/content_browser_client.h @@ -249,8 +249,45 @@ class CONTENT_EXPORT ContentBrowserClient { using IsClipboardPasteAllowedCallback = base::OnceCallback; + // Identifies the type of site instance to use for a navigation. + enum SiteInstanceForNavigationType { + // Use either the candidate site instance or, if it doesn't exist + // a new, unrelated site instance for the navigation. + FORCE_CANDIDATE_OR_NEW = 0, + + // Use the current site instance for the navigation. + FORCE_CURRENT, + + // Use a new, unrelated site instance. + FORCE_NEW, + + // Use the provided affinity site instance for the navigation. + FORCE_AFFINITY, + + // Delegate the site instance creation to Chromium. + ASK_CHROMIUM + }; + virtual ~ContentBrowserClient() {} + // Electron: Allows disabling the below ShouldOverride patch + virtual bool CanUseCustomSiteInstance(); + + // Electron: Allows overriding the SiteInstance when navigating. + virtual SiteInstanceForNavigationType ShouldOverrideSiteInstanceForNavigation( + content::RenderFrameHost* current_rfh, + content::RenderFrameHost* speculative_rfh, + content::BrowserContext* browser_context, + const GURL& url, + bool has_navigation_started, + bool has_request_started, + content::SiteInstance** affinity_site_instance) const; + + // Electron: Registers a pending site instance during a navigation. + virtual void RegisterPendingSiteInstance( + content::RenderFrameHost* rfh, + content::SiteInstance* pending_site_instance) {} + // Allows the embedder to set any number of custom BrowserMainParts // implementations for the browser startup code. See comments in // browser_main_parts.h. diff --git a/content/public/browser/site_instance.h b/content/public/browser/site_instance.h index 0e6c3d076a02cd1b356745e514b9de9d32e4b00c..76647f1c5e9ee401fbf3def6ccfe73372f622548 100644 --- a/content/public/browser/site_instance.h +++ b/content/public/browser/site_instance.h @@ -134,6 +134,11 @@ class CONTENT_EXPORT SiteInstance : public base::RefCounted { // corresponds to a site URL with the host "example.com". virtual const GURL& GetSiteURL() = 0; + // Create a SiteInstance for the given URL that shares the current + // BrowsingInstance. + virtual scoped_refptr CreateRelatedSiteInstance( + const GURL& url) = 0; + // Gets a SiteInstance for the given URL that shares the current // BrowsingInstance, creating a new SiteInstance if necessary. This ensures // that a BrowsingInstance only has one SiteInstance per site, so that pages