Skip to content

Commit 9597729

Browse files
samuelmaddocknornagon
authored andcommitted
feat: preliminary support for //extensions (electron#17440)
1 parent bd526f9 commit 9597729

54 files changed

Lines changed: 2483 additions & 36 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BUILD.gn

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,20 @@ source_set("electron_lib") {
627627
if (enable_pepper_flash) {
628628
deps += [ "components/pepper_flash" ]
629629
}
630+
631+
public_deps += [ "shell/common/extensions/api:extensions_features" ]
632+
deps += [
633+
"//components/pref_registry",
634+
"//components/user_prefs",
635+
"//extensions/browser",
636+
"//extensions/browser:core_api_provider",
637+
"//extensions/common",
638+
"//extensions/common:core_api_provider",
639+
"//extensions/renderer",
640+
]
641+
if (enable_electron_extensions) {
642+
sources += filenames.lib_sources_extensions
643+
}
630644
}
631645

632646
electron_paks("packed_resources") {

buildflags/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ buildflag_header("buildflags") {
1717
"ENABLE_PDF_VIEWER=$enable_pdf_viewer",
1818
"ENABLE_TTS=$enable_tts",
1919
"ENABLE_COLOR_CHOOSER=$enable_color_chooser",
20+
"ENABLE_ELECTRON_EXTENSIONS=$enable_electron_extensions",
2021
"OVERRIDE_LOCATION_PROVIDER=$enable_fake_location_provider",
2122
]
2223
}

buildflags/buildflags.gni

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ declare_args() {
2626

2727
# Enable flash plugin support.
2828
enable_pepper_flash = true
29+
30+
# Enable Chrome extensions support.
31+
enable_electron_extensions = false
2932
}

filenames.gni

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,35 @@ filenames = {
604604
"chromium_src/chrome/browser/certificate_manager_model.h",
605605
]
606606

607+
lib_sources_extensions = [
608+
"shell/browser/extensions/api/runtime/atom_runtime_api_delegate.cc",
609+
"shell/browser/extensions/api/runtime/atom_runtime_api_delegate.h",
610+
"shell/browser/extensions/atom_extensions_browser_client.cc",
611+
"shell/browser/extensions/atom_extensions_browser_client.h",
612+
"shell/browser/extensions/atom_browser_context_keyed_service_factories.cc",
613+
"shell/browser/extensions/atom_browser_context_keyed_service_factories.h",
614+
"shell/browser/extensions/atom_display_info_provider.cc",
615+
"shell/browser/extensions/atom_display_info_provider.h",
616+
"shell/browser/extensions/atom_extension_host_delegate.cc",
617+
"shell/browser/extensions/atom_extension_host_delegate.h",
618+
"shell/browser/extensions/atom_extension_loader.cc",
619+
"shell/browser/extensions/atom_extension_loader.h",
620+
"shell/browser/extensions/atom_extension_system.cc",
621+
"shell/browser/extensions/atom_extension_system.h",
622+
"shell/browser/extensions/atom_extension_system_factory.cc",
623+
"shell/browser/extensions/atom_extension_system_factory.h",
624+
"shell/browser/extensions/atom_extension_web_contents_observer.cc",
625+
"shell/browser/extensions/atom_extension_web_contents_observer.h",
626+
"shell/browser/extensions/atom_navigation_ui_data.cc",
627+
"shell/browser/extensions/atom_navigation_ui_data.h",
628+
"shell/common/extensions/atom_extensions_api_provider.cc",
629+
"shell/common/extensions/atom_extensions_api_provider.h",
630+
"shell/common/extensions/atom_extensions_client.cc",
631+
"shell/common/extensions/atom_extensions_client.h",
632+
"shell/renderer/extensions/atom_extensions_renderer_client.cc",
633+
"shell/renderer/extensions/atom_extensions_renderer_client.h",
634+
]
635+
607636
app_sources = [
608637
"shell/app/atom_main.cc",
609638
"shell/app/atom_main.h",

shell/browser/api/atom_api_session.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
#include "shell/common/options_switches.h"
6868
#include "ui/base/l10n/l10n_util.h"
6969

70+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
71+
#include "shell/browser/extensions/atom_extension_system.h"
72+
#endif
73+
7074
using content::BrowserThread;
7175
using content::StoragePartition;
7276

@@ -638,6 +642,14 @@ std::vector<base::FilePath::StringType> Session::GetPreloads() const {
638642
return prefs->preloads();
639643
}
640644

645+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
646+
void Session::LoadChromeExtension(const base::FilePath extension_path) {
647+
auto* extension_system = static_cast<extensions::AtomExtensionSystem*>(
648+
extensions::ExtensionSystem::Get(browser_context()));
649+
extension_system->LoadExtension(extension_path);
650+
}
651+
#endif
652+
641653
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
642654
if (cookies_.IsEmpty()) {
643655
auto handle = Cookies::Create(isolate, browser_context());
@@ -745,6 +757,9 @@ void Session::BuildPrototype(v8::Isolate* isolate,
745757
&Session::CreateInterruptedDownload)
746758
.SetMethod("setPreloads", &Session::SetPreloads)
747759
.SetMethod("getPreloads", &Session::GetPreloads)
760+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
761+
.SetMethod("loadChromeExtension", &Session::LoadChromeExtension)
762+
#endif
748763
.SetProperty("cookies", &Session::Cookies)
749764
.SetProperty("netLog", &Session::NetLog)
750765
.SetProperty("protocol", &Session::Protocol)

shell/browser/api/atom_api_session.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "base/values.h"
1212
#include "content/public/browser/download_manager.h"
13+
#include "electron/buildflags/buildflags.h"
1314
#include "native_mate/handle.h"
1415
#include "shell/browser/api/trackable_object.h"
1516
#include "shell/browser/atom_blob_reader.h"
@@ -86,6 +87,10 @@ class Session : public mate::TrackableObject<Session>,
8687
v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
8788
v8::Local<v8::Value> NetLog(v8::Isolate* isolate);
8889

90+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
91+
void LoadChromeExtension(const base::FilePath extension_path);
92+
#endif
93+
8994
protected:
9095
Session(v8::Isolate* isolate, AtomBrowserContext* browser_context);
9196
~Session() override;

shell/browser/api/atom_api_web_contents.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "content/public/browser/storage_partition.h"
4242
#include "content/public/browser/web_contents.h"
4343
#include "content/public/common/context_menu_params.h"
44+
#include "electron/buildflags/buildflags.h"
4445
#include "electron/shell/common/api/api.mojom.h"
4546
#include "mojo/public/cpp/system/platform_handle.h"
4647
#include "native_mate/converter.h"
@@ -112,6 +113,10 @@
112113
#include "components/printing/common/print_messages.h"
113114
#endif
114115

116+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
117+
#include "shell/browser/extensions/atom_extension_web_contents_observer.h"
118+
#endif
119+
115120
namespace mate {
116121

117122
#if BUILDFLAG(ENABLE_PRINTING)
@@ -456,12 +461,13 @@ void WebContents::InitWithSessionAndOptions(
456461
// Save the preferences in C++.
457462
new WebContentsPreferences(web_contents(), options);
458463

459-
// Initialize permission helper.
460464
WebContentsPermissionHelper::CreateForWebContents(web_contents());
461-
// Initialize security state client.
462465
SecurityStateTabHelper::CreateForWebContents(web_contents());
463-
// Initialize zoom controller.
464466
InitZoomController(web_contents(), options);
467+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
468+
extensions::AtomExtensionWebContentsObserver::CreateForWebContents(
469+
web_contents());
470+
#endif
465471

466472
registry_.AddInterface(base::BindRepeating(&WebContents::BindElectronBrowser,
467473
base::Unretained(this)));

shell/browser/atom_browser_context.cc

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "base/files/file_path.h"
1111
#include "base/path_service.h"
1212
#include "base/strings/string_util.h"
13+
#include "base/task/post_task.h"
1314
#include "base/threading/sequenced_task_runner_handle.h"
1415
#include "base/threading/thread_restrictions.h"
1516
#include "chrome/common/chrome_paths.h"
@@ -43,6 +44,22 @@
4344
#include "shell/common/application_info.h"
4445
#include "shell/common/options_switches.h"
4546

47+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
48+
#include "components/pref_registry/pref_registry_syncable.h"
49+
#include "components/user_prefs/user_prefs.h"
50+
#include "extensions/browser/browser_context_keyed_service_factories.h"
51+
#include "extensions/browser/extension_pref_store.h"
52+
#include "extensions/browser/extension_pref_value_map_factory.h"
53+
#include "extensions/browser/extension_prefs.h"
54+
#include "extensions/browser/pref_names.h"
55+
#include "extensions/common/extension_api.h"
56+
#include "shell/browser/extensions/atom_browser_context_keyed_service_factories.h"
57+
#include "shell/browser/extensions/atom_extension_system.h"
58+
#include "shell/browser/extensions/atom_extension_system_factory.h"
59+
#include "shell/browser/extensions/atom_extensions_browser_client.h"
60+
#include "shell/common/extensions/atom_extensions_client.h"
61+
#endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
62+
4663
using content::BrowserThread;
4764

4865
namespace electron {
@@ -91,6 +108,8 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
91108

92109
content::BrowserContext::Initialize(this, path_);
93110

111+
BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
112+
94113
// Initialize Pref Registry.
95114
InitPrefs();
96115

@@ -102,7 +121,15 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
102121

103122
cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);
104123

105-
BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
124+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
125+
BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
126+
this);
127+
128+
extension_system_ = static_cast<extensions::AtomExtensionSystem*>(
129+
extensions::ExtensionSystem::Get(this));
130+
extension_system_->InitForRegularProfile(true /* extensions_enabled */);
131+
extension_system_->FinishInitialization();
132+
#endif
106133
}
107134

108135
AtomBrowserContext::~AtomBrowserContext() {
@@ -131,7 +158,16 @@ void AtomBrowserContext::InitPrefs() {
131158
pref_store->ReadPrefs(); // Synchronous.
132159
prefs_factory.set_user_prefs(pref_store);
133160

161+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
162+
auto* ext_pref_store = new ExtensionPrefStore(
163+
ExtensionPrefValueMapFactory::GetForBrowserContext(this),
164+
IsOffTheRecord());
165+
prefs_factory.set_extension_prefs(ext_pref_store);
166+
167+
auto registry = WrapRefCounted(new user_prefs::PrefRegistrySyncable);
168+
#else
134169
auto registry = WrapRefCounted(new PrefRegistrySimple);
170+
#endif
135171

136172
registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
137173
base::FilePath());
@@ -144,11 +180,17 @@ void AtomBrowserContext::InitPrefs() {
144180
MediaDeviceIDSalt::RegisterPrefs(registry.get());
145181
ZoomLevelDelegate::RegisterPrefs(registry.get());
146182
PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get());
183+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
184+
extensions::ExtensionPrefs::RegisterProfilePrefs(registry.get());
185+
#endif
147186

148187
prefs_ = prefs_factory.Create(
149188
registry.get(),
150189
std::make_unique<PrefStoreDelegate>(weak_factory_.GetWeakPtr()));
151190
prefs_->UpdateCommandLinePrefStore(new ValueMapPrefStore);
191+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
192+
user_prefs::UserPrefs::Set(this, prefs_.get());
193+
#endif
152194
}
153195

154196
void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
@@ -305,6 +347,16 @@ AtomBrowserContext::GetClientHintsControllerDelegate() {
305347
return nullptr;
306348
}
307349

350+
void AtomBrowserContext::SetCorsOriginAccessListForOrigin(
351+
const url::Origin& source_origin,
352+
std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
353+
std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
354+
base::OnceClosure closure) {
355+
// TODO(nornagon): actually set the CORS access lists. This is called from
356+
// extensions/browser/renderer_startup_helper.cc.
357+
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(closure));
358+
}
359+
308360
ResolveProxyHelper* AtomBrowserContext::GetResolveProxyHelper() {
309361
if (!resolve_proxy_helper_) {
310362
resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);

shell/browser/atom_browser_context.h

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "chrome/browser/net/proxy_config_monitor.h"
1616
#include "content/public/browser/browser_context.h"
1717
#include "content/public/browser/resource_context.h"
18+
#include "electron/buildflags/buildflags.h"
1819
#include "shell/browser/media/media_device_id_salt.h"
1920
#include "shell/browser/net/url_request_context_getter.h"
2021

@@ -26,6 +27,12 @@ namespace storage {
2627
class SpecialStoragePolicy;
2728
}
2829

30+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
31+
namespace extensions {
32+
class AtomExtensionSystem;
33+
}
34+
#endif
35+
2936
namespace electron {
3037

3138
class AtomBlobReader;
@@ -41,6 +48,27 @@ class AtomBrowserContext
4148
: public base::RefCountedDeleteOnSequence<AtomBrowserContext>,
4249
public content::BrowserContext {
4350
public:
51+
// partition_id => browser_context
52+
struct PartitionKey {
53+
std::string partition;
54+
bool in_memory;
55+
56+
PartitionKey(const std::string& partition, bool in_memory)
57+
: partition(partition), in_memory(in_memory) {}
58+
59+
bool operator<(const PartitionKey& other) const {
60+
if (partition == other.partition)
61+
return in_memory < other.in_memory;
62+
return partition < other.partition;
63+
}
64+
65+
bool operator==(const PartitionKey& other) const {
66+
return (partition == other.partition) && (in_memory == other.in_memory);
67+
}
68+
};
69+
using BrowserContextMap =
70+
std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;
71+
4472
// Get or create the BrowserContext according to its |partition| and
4573
// |in_memory|. The |options| will be passed to constructor when there is no
4674
// existing BrowserContext.
@@ -49,6 +77,10 @@ class AtomBrowserContext
4977
bool in_memory,
5078
const base::DictionaryValue& options = base::DictionaryValue());
5179

80+
static BrowserContextMap browser_context_map() {
81+
return browser_context_map_;
82+
}
83+
5284
void SetUserAgent(const std::string& user_agent);
5385
std::string GetUserAgent() const;
5486
bool CanUseHttpCache() const;
@@ -84,6 +116,13 @@ class AtomBrowserContext
84116
content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
85117
override;
86118

119+
// extensions deps
120+
void SetCorsOriginAccessListForOrigin(
121+
const url::Origin& source_origin,
122+
std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
123+
std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
124+
base::OnceClosure closure) override;
125+
87126
CookieChangeNotifier* cookie_change_notifier() const {
88127
return cookie_change_notifier_.get();
89128
}
@@ -114,26 +153,6 @@ class AtomBrowserContext
114153
// Initialize pref registry.
115154
void InitPrefs();
116155

117-
// partition_id => browser_context
118-
struct PartitionKey {
119-
std::string partition;
120-
bool in_memory;
121-
122-
PartitionKey(const std::string& partition, bool in_memory)
123-
: partition(partition), in_memory(in_memory) {}
124-
125-
bool operator<(const PartitionKey& other) const {
126-
if (partition == other.partition)
127-
return in_memory < other.in_memory;
128-
return partition < other.partition;
129-
}
130-
131-
bool operator==(const PartitionKey& other) const {
132-
return (partition == other.partition) && (in_memory == other.in_memory);
133-
}
134-
};
135-
using BrowserContextMap =
136-
std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;
137156
static BrowserContextMap browser_context_map_;
138157

139158
// Self-destructing class responsible for creating URLRequestContextGetter
@@ -162,6 +181,11 @@ class AtomBrowserContext
162181
bool use_cache_ = true;
163182
int max_cache_size_ = 0;
164183

184+
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
185+
// Owned by the KeyedService system.
186+
extensions::AtomExtensionSystem* extension_system_;
187+
#endif
188+
165189
base::WeakPtrFactory<AtomBrowserContext> weak_factory_;
166190

167191
DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);

0 commit comments

Comments
 (0)