2727#include " chrome/common/chrome_version.h"
2828#include " components/net_log/chrome_net_log.h"
2929#include " components/network_hints/common/network_hints.mojom.h"
30+ #include " content/public/browser/browser_main_runner.h"
3031#include " content/public/browser/browser_ppapi_host.h"
3132#include " content/public/browser/browser_task_traits.h"
3233#include " content/public/browser/client_certificate_delegate.h"
4344#include " content/public/common/web_preferences.h"
4445#include " electron/buildflags/buildflags.h"
4546#include " electron/grit/electron_resources.h"
47+ #include " extensions/browser/extension_protocols.h"
48+ #include " extensions/common/constants.h"
4649#include " net/base/escape.h"
4750#include " net/ssl/ssl_cert_request_info.h"
4851#include " ppapi/host/ppapi_host.h"
123126#include " chrome/browser/printing/printing_message_filter.h"
124127#endif // BUILDFLAG(ENABLE_PRINTING)
125128
129+ #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
130+ #include " extensions/browser/extension_message_filter.h"
131+ #include " extensions/browser/extension_navigation_throttle.h"
132+ #include " extensions/browser/extension_registry.h"
133+ #include " extensions/browser/info_map.h"
134+ #include " extensions/browser/process_map.h"
135+ #include " extensions/common/extension.h"
136+ #include " shell/browser/extensions/atom_extension_system.h"
137+ #endif
138+
126139#if defined(OS_MACOSX)
127140#include " content/common/mac_helpers.h"
128141#include " content/public/common/child_process_host.h"
@@ -226,6 +239,8 @@ bool AtomBrowserClient::ShouldForceNewSiteInstance(
226239 if (url.SchemeIs (url::kJavaScriptScheme ))
227240 // "javacript:" scheme should always use same SiteInstance
228241 return false ;
242+ if (url.SchemeIs (extensions::kExtensionScheme ))
243+ return false ;
229244
230245 content::SiteInstance* current_instance = current_rfh->GetSiteInstance ();
231246 content::SiteInstance* speculative_instance =
@@ -359,9 +374,16 @@ void AtomBrowserClient::RenderProcessWillLaunch(
359374 if (IsProcessObserved (process_id))
360375 return ;
361376
377+ auto * browser_context = host->GetBrowserContext ();
378+
362379#if BUILDFLAG(ENABLE_PRINTING)
363- host->AddFilter (new printing::PrintingMessageFilter (
364- process_id, host->GetBrowserContext ()));
380+ host->AddFilter (
381+ new printing::PrintingMessageFilter (process_id, browser_context));
382+ #endif
383+
384+ #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
385+ host->AddFilter (
386+ new extensions::ExtensionMessageFilter (process_id, browser_context));
365387#endif
366388
367389 ProcessPreferences prefs;
@@ -708,6 +730,32 @@ void AtomBrowserClient::GetAdditionalWebUISchemes(
708730 additional_schemes->push_back (content::kChromeDevToolsScheme );
709731}
710732
733+ void AtomBrowserClient::SiteInstanceGotProcess (
734+ content::SiteInstance* site_instance) {
735+ #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
736+ auto * browser_context =
737+ static_cast <AtomBrowserContext*>(site_instance->GetBrowserContext ());
738+ extensions::ExtensionRegistry* registry =
739+ extensions::ExtensionRegistry::Get (browser_context);
740+ const extensions::Extension* extension =
741+ registry->enabled_extensions ().GetExtensionOrAppByURL (
742+ site_instance->GetSiteURL ());
743+ if (!extension)
744+ return ;
745+
746+ extensions::ProcessMap::Get (browser_context)
747+ ->Insert (extension->id (), site_instance->GetProcess ()->GetID (),
748+ site_instance->GetId ());
749+
750+ base::PostTask (
751+ FROM_HERE, {BrowserThread::IO},
752+ base::BindOnce (&extensions::InfoMap::RegisterExtensionProcess,
753+ browser_context->extension_system ()->info_map (),
754+ extension->id (), site_instance->GetProcess ()->GetID (),
755+ site_instance->GetId ()));
756+ #endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
757+ }
758+
711759void AtomBrowserClient::SiteInstanceDeleting (
712760 content::SiteInstance* site_instance) {
713761 // We are storing weak_ptr, is it fundamental to maintain the map up-to-date
@@ -719,6 +767,34 @@ void AtomBrowserClient::SiteInstanceDeleting(
719767 break ;
720768 }
721769 }
770+
771+ #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
772+ // Don't do anything if we're shutting down.
773+ if (content::BrowserMainRunner::ExitedMainMessageLoop ())
774+ return ;
775+
776+ auto * browser_context =
777+ static_cast <AtomBrowserContext*>(site_instance->GetBrowserContext ());
778+ // If this isn't an extension renderer there's nothing to do.
779+ extensions::ExtensionRegistry* registry =
780+ extensions::ExtensionRegistry::Get (browser_context);
781+ const extensions::Extension* extension =
782+ registry->enabled_extensions ().GetExtensionOrAppByURL (
783+ site_instance->GetSiteURL ());
784+ if (!extension)
785+ return ;
786+
787+ extensions::ProcessMap::Get (browser_context)
788+ ->Remove (extension->id (), site_instance->GetProcess ()->GetID (),
789+ site_instance->GetId ());
790+
791+ base::PostTask (
792+ FROM_HERE, {BrowserThread::IO},
793+ base::BindOnce (&extensions::InfoMap::UnregisterExtensionProcess,
794+ browser_context->extension_system ()->info_map (),
795+ extension->id (), site_instance->GetProcess ()->GetID (),
796+ site_instance->GetId ()));
797+ #endif
722798}
723799
724800std::unique_ptr<net::ClientCertStore> AtomBrowserClient::CreateClientCertStore (
@@ -869,6 +945,12 @@ AtomBrowserClient::CreateThrottlesForNavigation(
869945 content::NavigationHandle* handle) {
870946 std::vector<std::unique_ptr<content::NavigationThrottle>> throttles;
871947 throttles.push_back (std::make_unique<AtomNavigationThrottle>(handle));
948+
949+ #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
950+ throttles.push_back (
951+ std::make_unique<extensions::ExtensionNavigationThrottle>(handle));
952+ #endif
953+
872954 return throttles;
873955}
874956
0 commit comments