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 0d40acea7c2c69003c54cf3ae1e1d15adcb6deee..ba28c8f315b4b89a08659b45eaff485dcb65fc39 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; +} + void BrowsingInstance::GetSiteAndLockForURL(const GURL& url, bool allow_default_instance, GURL* site_url, diff --git a/content/browser/browsing_instance.h b/content/browser/browsing_instance.h index acbf074f6e408cdc3817580b9601ee40c036c4dd..e56456c5b6024b92dbd025a8f25d4bde5f8afe27 100644 --- a/content/browser/browsing_instance.h +++ b/content/browser/browsing_instance.h @@ -136,6 +136,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/render_frame_host_manager.cc b/content/browser/frame_host/render_frame_host_manager.cc index 6ed970857351aed119dc95a8d0f3828c4278a2c9..0111785bcafd0ba83781edac2fc8b74b5f960e0e 100644 --- a/content/browser/frame_host/render_frame_host_manager.cc +++ b/content/browser/frame_host/render_frame_host_manager.cc @@ -2176,6 +2176,21 @@ bool RenderFrameHostManager::InitRenderView( scoped_refptr RenderFrameHostManager::GetSiteInstanceForNavigationRequest( const NavigationRequest& request) { + BrowserContext* browser_context = nullptr; + scoped_refptr candidate_site_instance; + if (!GetContentClient()->browser()->CanUseCustomSiteInstance()) { + browser_context = + delegate_->GetControllerForRenderManager().GetBrowserContext(); + // If the navigation can swap SiteInstances, compute the SiteInstance it + // should use. + // TODO(clamy): We should also consider as a candidate SiteInstance the + // speculative SiteInstance that was computed on redirects. + 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 @@ -2213,6 +2228,53 @@ RenderFrameHostManager::GetSiteInstanceForNavigationRequest( request.common_params().url); no_renderer_swap_allowed |= request.from_begin_navigation() && !can_renderer_initiate_transfer; + + if (!GetContentClient()->browser()->CanUseCustomSiteInstance()) { + bool has_response_started = + (request.state() == NavigationRequest::RESPONSE_STARTED || + request.state() == NavigationRequest::FAILED) && + !speculative_render_frame_host_; + // Gives user a chance to choose a custom site instance. + SiteInstance* affinity_site_instance = nullptr; + scoped_refptr overriden_site_instance; + ContentBrowserClient::SiteInstanceForNavigationType siteInstanceType = + GetContentClient()->browser()->ShouldOverrideSiteInstanceForNavigation( + current_frame_host(), speculative_frame_host(), browser_context, + request.common_params().url, 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); + 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 (siteInstanceType == + ContentBrowserClient::SiteInstanceForNavigationType:: + FORCE_CANDIDATE_OR_NEW) { + GetContentClient()->browser()->RegisterPendingSiteInstance( + render_frame_host_.get(), overriden_site_instance.get()); + } + return overriden_site_instance; + } + } } else { // Subframe navigations will use the current renderer, unless specifically // allowed to swap processes. @@ -2224,23 +2286,28 @@ RenderFrameHostManager::GetSiteInstanceForNavigationRequest( if (no_renderer_swap_allowed && !should_swap_for_error_isolation) return scoped_refptr(current_site_instance); + if (GetContentClient()->browser()->CanUseCustomSiteInstance()) { // If the navigation can swap SiteInstances, compute the SiteInstance it // should use. // TODO(clamy): We should also consider as a candidate SiteInstance the // speculative SiteInstance that was computed on redirects. - SiteInstanceImpl* candidate_site_instance = + candidate_site_instance = speculative_render_frame_host_ ? speculative_render_frame_host_->GetSiteInstance() : nullptr; + } scoped_refptr dest_site_instance = GetSiteInstanceForNavigation( request.common_params().url, request.source_site_instance(), - request.dest_site_instance(), candidate_site_instance, + request.dest_site_instance(), candidate_site_instance.get(), request.common_params().transition, request.state() == NavigationRequest::FAILED, request.restore_type() != RestoreType::NONE, request.is_view_source(), was_server_redirect); + 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 c6bc3bf3ea5fd936c8d14f07efbf2453da881342..08d0fa5c6dfe3ff46f206f7983d47e9c445c63da 100644 --- a/content/browser/site_instance_impl.cc +++ b/content/browser/site_instance_impl.cc @@ -356,6 +356,10 @@ bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) { return browsing_instance_->HasSiteInstance(url); } +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 5f57cd47108b85c0e884c1855af2c74b7fd02b9b..6b755ec0f9f76fa91bec975fdcf7094b4d43cea6 100644 --- a/content/browser/site_instance_impl.h +++ b/content/browser/site_instance_impl.h @@ -83,6 +83,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 b7a903ae979f56e79603af1a6edf395d67e0e982..090eaa070e56d84255f570af3d9f0ea56d02cb69 100644 --- a/content/public/browser/content_browser_client.cc +++ b/content/public/browser/content_browser_client.cc @@ -52,6 +52,20 @@ void OverrideOnBindInterface(const service_manager::BindSourceInfo& remote_info, handle); } +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_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 86755824ccbaef2d5eeea63166d2243228bd5a4a..e67ab701d608f87cfaf259d44b17605467181fea 100644 --- a/content/public/browser/content_browser_client.h +++ b/content/public/browser/content_browser_client.h @@ -211,8 +211,41 @@ CONTENT_EXPORT void OverrideOnBindInterface( // the observer interfaces.) class CONTENT_EXPORT ContentBrowserClient { public: + // 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 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_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 a3e880e20e51d988175f0e8e2c42e7f5c1740104..faadd39d01530092f4f31a896ecb60f235e7f63f 100644 --- a/content/public/browser/site_instance.h +++ b/content/public/browser/site_instance.h @@ -121,6 +121,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