Skip to content

Commit 8bc0c92

Browse files
authored
feat: [extensions] background pages (electron#21591)
1 parent cf497ea commit 8bc0c92

23 files changed

+477
-11
lines changed

electron_paks.gni

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import("//build/config/locales.gni")
2+
import("//electron/buildflags/buildflags.gni")
23
import("//printing/buildflags/buildflags.gni")
34
import("//tools/grit/repack.gni")
45
import("//ui/base/ui_features.gni")
@@ -87,6 +88,13 @@ template("electron_extra_paks") {
8788
sources += [ "$root_gen_dir/chrome/print_preview_resources.pak" ]
8889
deps += [ "//chrome/browser/resources:print_preview_resources" ]
8990
}
91+
if (enable_electron_extensions) {
92+
sources += [
93+
"$root_gen_dir/extensions/extensions_renderer_resources.pak",
94+
"$root_gen_dir/extensions/extensions_resources.pak",
95+
]
96+
deps += [ "//extensions:extensions_resources" ]
97+
}
9098
}
9199
}
92100

filenames.gni

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,12 @@ filenames = {
607607
"shell/browser/extensions/atom_extension_web_contents_observer.h",
608608
"shell/browser/extensions/atom_navigation_ui_data.cc",
609609
"shell/browser/extensions/atom_navigation_ui_data.h",
610+
"shell/browser/extensions/electron_process_manager_delegate.cc",
611+
"shell/browser/extensions/electron_process_manager_delegate.h",
612+
"shell/browser/extensions/electron_extensions_api_client.cc",
613+
"shell/browser/extensions/electron_extensions_api_client.h",
614+
"shell/browser/extensions/electron_messaging_delegate.cc",
615+
"shell/browser/extensions/electron_messaging_delegate.h",
610616
"shell/common/extensions/atom_extensions_api_provider.cc",
611617
"shell/common/extensions/atom_extensions_api_provider.h",
612618
"shell/common/extensions/atom_extensions_client.cc",

shell/app/atom_content_client.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "base/strings/utf_string_conversions.h"
1717
#include "content/public/common/content_constants.h"
1818
#include "electron/buildflags/buildflags.h"
19+
#include "extensions/common/constants.h"
1920
#include "ppapi/buildflags/buildflags.h"
2021
#include "shell/browser/atom_paths.h"
2122
#include "shell/common/options_switches.h"
@@ -225,7 +226,7 @@ void AtomContentClient::AddAdditionalSchemes(Schemes* schemes) {
225226
&schemes->cors_enabled_schemes);
226227

227228
schemes->service_worker_schemes.emplace_back(url::kFileScheme);
228-
schemes->standard_schemes.emplace_back("chrome-extension");
229+
schemes->standard_schemes.emplace_back(extensions::kExtensionScheme);
229230
}
230231

231232
void AtomContentClient::AddPepperPlugins(

shell/app/atom_main_delegate.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#include "base/mac/bundle_locations.h"
2020
#include "base/path_service.h"
2121
#include "chrome/common/chrome_paths.h"
22+
#include "components/content_settings/core/common/content_settings_pattern.h"
2223
#include "content/public/common/content_switches.h"
2324
#include "electron/buildflags/buildflags.h"
25+
#include "extensions/common/constants.h"
2426
#include "ipc/ipc_buildflags.h"
2527
#include "services/service_manager/embedder/switches.h"
2628
#include "services/service_manager/sandbox/switches.h"
@@ -131,6 +133,11 @@ AtomMainDelegate::AtomMainDelegate() = default;
131133

132134
AtomMainDelegate::~AtomMainDelegate() = default;
133135

136+
const char* const AtomMainDelegate::kNonWildcardDomainNonPortSchemes[] = {
137+
extensions::kExtensionScheme};
138+
const size_t AtomMainDelegate::kNonWildcardDomainNonPortSchemesSize =
139+
base::size(kNonWildcardDomainNonPortSchemes);
140+
134141
bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
135142
auto* command_line = base::CommandLine::ForCurrentProcess();
136143

@@ -187,6 +194,10 @@ bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
187194
tracing::TracingSamplerProfiler::CreateOnMainThread();
188195

189196
chrome::RegisterPathProvider();
197+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
198+
ContentSettingsPattern::SetNonWildcardDomainNonPortSchemes(
199+
kNonWildcardDomainNonPortSchemes, kNonWildcardDomainNonPortSchemesSize);
200+
#endif
190201

191202
#if defined(OS_MACOSX)
192203
OverrideChildProcessPath();

shell/app/atom_main_delegate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void LoadResourceBundle(const std::string& locale);
2121

2222
class AtomMainDelegate : public content::ContentMainDelegate {
2323
public:
24+
static const char* const kNonWildcardDomainNonPortSchemes[];
25+
static const size_t kNonWildcardDomainNonPortSchemesSize;
2426
AtomMainDelegate();
2527
~AtomMainDelegate() override;
2628

shell/browser/api/atom_api_web_contents.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ WebContents::WebContents(v8::Isolate* isolate,
358358
Init(isolate);
359359
AttachAsUserData(web_contents);
360360
InitZoomController(web_contents, gin::Dictionary::CreateEmpty(isolate));
361+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
362+
extensions::AtomExtensionWebContentsObserver::CreateForWebContents(
363+
web_contents);
364+
#endif
361365
registry_.AddInterface(base::BindRepeating(&WebContents::BindElectronBrowser,
362366
base::Unretained(this)));
363367
bindings_.set_connection_error_handler(base::BindRepeating(

shell/browser/atom_browser_client.cc

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
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"
@@ -43,6 +44,8 @@
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"
@@ -123,6 +126,16 @@
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+
711759
void 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

724800
std::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

shell/browser/atom_browser_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class AtomBrowserClient : public content::ContentBrowserClient,
219219
scoped_refptr<net::HttpResponseHeaders> response_headers,
220220
bool first_auth_attempt,
221221
LoginAuthRequiredCallback auth_required_callback) override;
222+
void SiteInstanceGotProcess(content::SiteInstance* site_instance) override;
222223

223224
// content::RenderProcessHostObserver:
224225
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

shell/browser/atom_browser_context.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,13 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
146146
AtomBrowserContext::~AtomBrowserContext() {
147147
DCHECK_CURRENTLY_ON(BrowserThread::UI);
148148
NotifyWillBeDestroyed(this);
149+
// Notify any keyed services of browser context destruction.
150+
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
151+
this);
149152
ShutdownStoragePartitions();
150153

151154
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
152155
std::move(resource_context_));
153-
154-
// Notify any keyed services of browser context destruction.
155-
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
156-
this);
157156
}
158157

159158
void AtomBrowserContext::InitPrefs() {

shell/browser/atom_browser_context.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ class AtomBrowserContext
141141
return weak_factory_.GetWeakPtr();
142142
}
143143

144+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
145+
extensions::AtomExtensionSystem* extension_system() {
146+
return extension_system_;
147+
}
148+
#endif
149+
144150
protected:
145151
AtomBrowserContext(const std::string& partition,
146152
bool in_memory,

0 commit comments

Comments
 (0)