refactor: move extension APIs to session.extensions (#45597)
refactor: move extensions to session.extensions
This commit is contained in:
parent
a63f6143ea
commit
e3f61b465d
14 changed files with 508 additions and 205 deletions
158
shell/browser/api/electron_api_extensions.cc
Normal file
158
shell/browser/api/electron_api_extensions.cc
Normal file
|
@ -0,0 +1,158 @@
|
|||
// Copyright (c) 2019 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/api/electron_api_extensions.h"
|
||||
|
||||
#include "chrome/browser/browser_process.h"
|
||||
#include "extensions/browser/extension_registry.h"
|
||||
#include "gin/data_object_builder.h"
|
||||
#include "gin/handle.h"
|
||||
#include "gin/object_template_builder.h"
|
||||
#include "shell/browser/api/electron_api_extensions.h"
|
||||
#include "shell/browser/electron_browser_context.h"
|
||||
#include "shell/browser/extensions/electron_extension_system.h"
|
||||
#include "shell/browser/javascript_environment.h"
|
||||
#include "shell/common/gin_converters/extension_converter.h"
|
||||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_converters/gurl_converter.h"
|
||||
#include "shell/common/gin_converters/value_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "shell/common/node_util.h"
|
||||
|
||||
namespace electron::api {
|
||||
|
||||
gin::WrapperInfo Extensions::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||
|
||||
Extensions::Extensions(v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context)
|
||||
: browser_context_(browser_context) {
|
||||
extensions::ExtensionRegistry::Get(browser_context)->AddObserver(this);
|
||||
}
|
||||
|
||||
Extensions::~Extensions() {
|
||||
extensions::ExtensionRegistry::Get(browser_context())->RemoveObserver(this);
|
||||
}
|
||||
|
||||
// static
|
||||
gin::Handle<Extensions> Extensions::Create(
|
||||
v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context) {
|
||||
return gin::CreateHandle(isolate, new Extensions(isolate, browser_context));
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> Extensions::LoadExtension(
|
||||
v8::Isolate* isolate,
|
||||
const base::FilePath& extension_path,
|
||||
gin::Arguments* args) {
|
||||
gin_helper::Promise<const extensions::Extension*> promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
if (!extension_path.IsAbsolute()) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"The path to the extension in 'loadExtension' must be absolute");
|
||||
return handle;
|
||||
}
|
||||
|
||||
if (browser_context()->IsOffTheRecord()) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"Extensions cannot be loaded in a temporary session");
|
||||
return handle;
|
||||
}
|
||||
|
||||
int load_flags = extensions::Extension::FOLLOW_SYMLINKS_ANYWHERE;
|
||||
gin_helper::Dictionary options;
|
||||
if (args->GetNext(&options)) {
|
||||
bool allowFileAccess = false;
|
||||
options.Get("allowFileAccess", &allowFileAccess);
|
||||
if (allowFileAccess)
|
||||
load_flags |= extensions::Extension::ALLOW_FILE_ACCESS;
|
||||
}
|
||||
|
||||
auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
|
||||
extensions::ExtensionSystem::Get(browser_context()));
|
||||
extension_system->LoadExtension(
|
||||
extension_path, load_flags,
|
||||
base::BindOnce(
|
||||
[](gin_helper::Promise<const extensions::Extension*> promise,
|
||||
const extensions::Extension* extension,
|
||||
const std::string& error_msg) {
|
||||
if (extension) {
|
||||
if (!error_msg.empty())
|
||||
util::EmitWarning(promise.isolate(), error_msg,
|
||||
"ExtensionLoadWarning");
|
||||
promise.Resolve(extension);
|
||||
} else {
|
||||
promise.RejectWithErrorMessage(error_msg);
|
||||
}
|
||||
},
|
||||
std::move(promise)));
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
void Extensions::RemoveExtension(const std::string& extension_id) {
|
||||
auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
|
||||
extensions::ExtensionSystem::Get(browser_context()));
|
||||
extension_system->RemoveExtension(extension_id);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Extensions::GetExtension(v8::Isolate* isolate,
|
||||
const std::string& extension_id) {
|
||||
auto* registry = extensions::ExtensionRegistry::Get(browser_context());
|
||||
const extensions::Extension* extension =
|
||||
registry->GetInstalledExtension(extension_id);
|
||||
if (extension) {
|
||||
return gin::ConvertToV8(isolate, extension);
|
||||
} else {
|
||||
return v8::Null(isolate);
|
||||
}
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Extensions::GetAllExtensions(v8::Isolate* isolate) {
|
||||
auto* registry = extensions::ExtensionRegistry::Get(browser_context());
|
||||
const extensions::ExtensionSet extensions =
|
||||
registry->GenerateInstalledExtensionsSet();
|
||||
std::vector<const extensions::Extension*> extensions_vector;
|
||||
for (const auto& extension : extensions) {
|
||||
if (extension->location() !=
|
||||
extensions::mojom::ManifestLocation::kComponent)
|
||||
extensions_vector.emplace_back(extension.get());
|
||||
}
|
||||
return gin::ConvertToV8(isolate, extensions_vector);
|
||||
}
|
||||
|
||||
void Extensions::OnExtensionLoaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) {
|
||||
Emit("extension-loaded", extension);
|
||||
}
|
||||
|
||||
void Extensions::OnExtensionUnloaded(
|
||||
content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension,
|
||||
extensions::UnloadedExtensionReason reason) {
|
||||
Emit("extension-unloaded", extension);
|
||||
}
|
||||
|
||||
void Extensions::OnExtensionReady(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) {
|
||||
Emit("extension-ready", extension);
|
||||
}
|
||||
|
||||
// static
|
||||
gin::ObjectTemplateBuilder Extensions::GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) {
|
||||
return gin_helper::EventEmitterMixin<Extensions>::GetObjectTemplateBuilder(
|
||||
isolate)
|
||||
.SetMethod("loadExtension", &Extensions::LoadExtension)
|
||||
.SetMethod("removeExtension", &Extensions::RemoveExtension)
|
||||
.SetMethod("getExtension", &Extensions::GetExtension)
|
||||
.SetMethod("getAllExtensions", &Extensions::GetAllExtensions);
|
||||
}
|
||||
|
||||
const char* Extensions::GetTypeName() {
|
||||
return "Extensions";
|
||||
}
|
||||
|
||||
} // namespace electron::api
|
79
shell/browser/api/electron_api_extensions.h
Normal file
79
shell/browser/api/electron_api_extensions.h
Normal file
|
@ -0,0 +1,79 @@
|
|||
// Copyright (c) 2019 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_EXTENSIONS_H_
|
||||
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_EXTENSIONS_H_
|
||||
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "extensions/browser/extension_registry.h"
|
||||
#include "extensions/browser/extension_registry_observer.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "shell/browser/event_emitter_mixin.h"
|
||||
|
||||
namespace gin {
|
||||
template <typename T>
|
||||
class Handle;
|
||||
} // namespace gin
|
||||
|
||||
namespace electron {
|
||||
|
||||
class ElectronBrowserContext;
|
||||
|
||||
namespace api {
|
||||
|
||||
class Extensions final : public gin::Wrappable<Extensions>,
|
||||
public gin_helper::EventEmitterMixin<Extensions>,
|
||||
private extensions::ExtensionRegistryObserver {
|
||||
public:
|
||||
static gin::Handle<Extensions> Create(
|
||||
v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context);
|
||||
|
||||
// gin::Wrappable
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) override;
|
||||
const char* GetTypeName() override;
|
||||
|
||||
v8::Local<v8::Promise> LoadExtension(v8::Isolate* isolate,
|
||||
const base::FilePath& extension_path,
|
||||
gin::Arguments* args);
|
||||
void RemoveExtension(const std::string& extension_id);
|
||||
v8::Local<v8::Value> GetExtension(v8::Isolate* isolate,
|
||||
const std::string& extension_id);
|
||||
v8::Local<v8::Value> GetAllExtensions(v8::Isolate* isolate);
|
||||
|
||||
// extensions::ExtensionRegistryObserver:
|
||||
void OnExtensionLoaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) override;
|
||||
void OnExtensionReady(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) override;
|
||||
void OnExtensionUnloaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension,
|
||||
extensions::UnloadedExtensionReason reason) override;
|
||||
|
||||
// disable copy
|
||||
Extensions(const Extensions&) = delete;
|
||||
Extensions& operator=(const Extensions&) = delete;
|
||||
|
||||
protected:
|
||||
explicit Extensions(v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context);
|
||||
~Extensions() override;
|
||||
|
||||
private:
|
||||
content::BrowserContext* browser_context() const {
|
||||
return browser_context_.get();
|
||||
}
|
||||
|
||||
raw_ptr<content::BrowserContext> browser_context_;
|
||||
|
||||
base::WeakPtrFactory<Extensions> weak_ptr_factory_{this};
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_EXTENSIONS_H_
|
|
@ -97,9 +97,7 @@
|
|||
#include "url/origin.h"
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
#include "extensions/browser/extension_registry.h"
|
||||
#include "shell/browser/extensions/electron_extension_system.h"
|
||||
#include "shell/common/gin_converters/extension_converter.h"
|
||||
#include "shell/browser/api/electron_api_extensions.h"
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||
|
@ -569,10 +567,6 @@ Session::Session(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
|
|||
service->SetHunspellObserver(this);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
extensions::ExtensionRegistry::Get(browser_context)->AddObserver(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
Session::~Session() {
|
||||
|
@ -584,10 +578,6 @@ Session::~Session() {
|
|||
service->SetHunspellObserver(nullptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
extensions::ExtensionRegistry::Get(browser_context())->RemoveObserver(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Session::OnDownloadCreated(content::DownloadManager* manager,
|
||||
|
@ -1305,103 +1295,6 @@ v8::Local<v8::Promise> Session::GetSharedDictionaryUsageInfo() {
|
|||
return handle;
|
||||
}
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
v8::Local<v8::Promise> Session::LoadExtension(
|
||||
const base::FilePath& extension_path,
|
||||
gin::Arguments* args) {
|
||||
gin_helper::Promise<const extensions::Extension*> promise(isolate_);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
if (!extension_path.IsAbsolute()) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"The path to the extension in 'loadExtension' must be absolute");
|
||||
return handle;
|
||||
}
|
||||
|
||||
if (browser_context()->IsOffTheRecord()) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"Extensions cannot be loaded in a temporary session");
|
||||
return handle;
|
||||
}
|
||||
|
||||
int load_flags = extensions::Extension::FOLLOW_SYMLINKS_ANYWHERE;
|
||||
gin_helper::Dictionary options;
|
||||
if (args->GetNext(&options)) {
|
||||
bool allowFileAccess = false;
|
||||
options.Get("allowFileAccess", &allowFileAccess);
|
||||
if (allowFileAccess)
|
||||
load_flags |= extensions::Extension::ALLOW_FILE_ACCESS;
|
||||
}
|
||||
|
||||
auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
|
||||
extensions::ExtensionSystem::Get(browser_context()));
|
||||
extension_system->LoadExtension(
|
||||
extension_path, load_flags,
|
||||
base::BindOnce(
|
||||
[](gin_helper::Promise<const extensions::Extension*> promise,
|
||||
const extensions::Extension* extension,
|
||||
const std::string& error_msg) {
|
||||
if (extension) {
|
||||
if (!error_msg.empty())
|
||||
util::EmitWarning(promise.isolate(), error_msg,
|
||||
"ExtensionLoadWarning");
|
||||
promise.Resolve(extension);
|
||||
} else {
|
||||
promise.RejectWithErrorMessage(error_msg);
|
||||
}
|
||||
},
|
||||
std::move(promise)));
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
void Session::RemoveExtension(const std::string& extension_id) {
|
||||
auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
|
||||
extensions::ExtensionSystem::Get(browser_context()));
|
||||
extension_system->RemoveExtension(extension_id);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::GetExtension(const std::string& extension_id) {
|
||||
auto* registry = extensions::ExtensionRegistry::Get(browser_context());
|
||||
const extensions::Extension* extension =
|
||||
registry->GetInstalledExtension(extension_id);
|
||||
if (extension) {
|
||||
return gin::ConvertToV8(isolate_, extension);
|
||||
} else {
|
||||
return v8::Null(isolate_);
|
||||
}
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::GetAllExtensions() {
|
||||
auto* registry = extensions::ExtensionRegistry::Get(browser_context());
|
||||
const extensions::ExtensionSet extensions =
|
||||
registry->GenerateInstalledExtensionsSet();
|
||||
std::vector<const extensions::Extension*> extensions_vector;
|
||||
for (const auto& extension : extensions) {
|
||||
if (extension->location() !=
|
||||
extensions::mojom::ManifestLocation::kComponent)
|
||||
extensions_vector.emplace_back(extension.get());
|
||||
}
|
||||
return gin::ConvertToV8(isolate_, extensions_vector);
|
||||
}
|
||||
|
||||
void Session::OnExtensionLoaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) {
|
||||
Emit("extension-loaded", extension);
|
||||
}
|
||||
|
||||
void Session::OnExtensionUnloaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension,
|
||||
extensions::UnloadedExtensionReason reason) {
|
||||
Emit("extension-unloaded", extension);
|
||||
}
|
||||
|
||||
void Session::OnExtensionReady(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) {
|
||||
Emit("extension-ready", extension);
|
||||
}
|
||||
#endif
|
||||
|
||||
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
||||
if (cookies_.IsEmpty()) {
|
||||
auto handle = Cookies::Create(isolate, browser_context());
|
||||
|
@ -1410,6 +1303,17 @@ v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
|||
return cookies_.Get(isolate);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::Extensions(v8::Isolate* isolate) {
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
if (extensions_.IsEmpty()) {
|
||||
v8::Local<v8::Value> handle;
|
||||
handle = Extensions::Create(isolate, browser_context()).ToV8();
|
||||
extensions_.Reset(isolate, handle);
|
||||
}
|
||||
#endif
|
||||
return extensions_.Get(isolate);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::Protocol(v8::Isolate* isolate) {
|
||||
return protocol_.Get(isolate);
|
||||
}
|
||||
|
@ -1872,12 +1776,6 @@ void Session::FillObjectTemplate(v8::Isolate* isolate,
|
|||
&Session::ClearSharedDictionaryCache)
|
||||
.SetMethod("clearSharedDictionaryCacheForIsolationKey",
|
||||
&Session::ClearSharedDictionaryCacheForIsolationKey)
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
.SetMethod("loadExtension", &Session::LoadExtension)
|
||||
.SetMethod("removeExtension", &Session::RemoveExtension)
|
||||
.SetMethod("getExtension", &Session::GetExtension)
|
||||
.SetMethod("getAllExtensions", &Session::GetAllExtensions)
|
||||
#endif
|
||||
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||
.SetMethod("getSpellCheckerLanguages", &Session::GetSpellCheckerLanguages)
|
||||
.SetMethod("setSpellCheckerLanguages", &Session::SetSpellCheckerLanguages)
|
||||
|
@ -1903,6 +1801,7 @@ void Session::FillObjectTemplate(v8::Isolate* isolate,
|
|||
.SetMethod("clearCodeCaches", &Session::ClearCodeCaches)
|
||||
.SetMethod("clearData", &Session::ClearData)
|
||||
.SetProperty("cookies", &Session::Cookies)
|
||||
.SetProperty("extensions", &Session::Extensions)
|
||||
.SetProperty("netLog", &Session::NetLog)
|
||||
.SetProperty("protocol", &Session::Protocol)
|
||||
.SetProperty("serviceWorkers", &Session::ServiceWorkerContext)
|
||||
|
|
|
@ -29,11 +29,6 @@
|
|||
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" // nogncheck
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
#include "extensions/browser/extension_registry.h"
|
||||
#include "extensions/browser/extension_registry_observer.h"
|
||||
#endif
|
||||
|
||||
class GURL;
|
||||
|
||||
namespace base {
|
||||
|
@ -70,9 +65,6 @@ class Session final : public gin::Wrappable<Session>,
|
|||
public IpcDispatcher<Session>,
|
||||
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||
private SpellcheckHunspellDictionary::Observer,
|
||||
#endif
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
private extensions::ExtensionRegistryObserver,
|
||||
#endif
|
||||
private content::DownloadManager::Observer {
|
||||
public:
|
||||
|
@ -156,6 +148,7 @@ class Session final : public gin::Wrappable<Session>,
|
|||
v8::Local<v8::Promise> ClearSharedDictionaryCacheForIsolationKey(
|
||||
const gin_helper::Dictionary& options);
|
||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> Extensions(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> Protocol(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> ServiceWorkerContext(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
|
||||
|
@ -178,23 +171,6 @@ class Session final : public gin::Wrappable<Session>,
|
|||
bool IsSpellCheckerEnabled() const;
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
v8::Local<v8::Promise> LoadExtension(const base::FilePath& extension_path,
|
||||
gin::Arguments* args);
|
||||
void RemoveExtension(const std::string& extension_id);
|
||||
v8::Local<v8::Value> GetExtension(const std::string& extension_id);
|
||||
v8::Local<v8::Value> GetAllExtensions();
|
||||
|
||||
// extensions::ExtensionRegistryObserver:
|
||||
void OnExtensionLoaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) override;
|
||||
void OnExtensionReady(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension) override;
|
||||
void OnExtensionUnloaded(content::BrowserContext* browser_context,
|
||||
const extensions::Extension* extension,
|
||||
extensions::UnloadedExtensionReason reason) override;
|
||||
#endif
|
||||
|
||||
// disable copy
|
||||
Session(const Session&) = delete;
|
||||
Session& operator=(const Session&) = delete;
|
||||
|
@ -223,6 +199,7 @@ class Session final : public gin::Wrappable<Session>,
|
|||
|
||||
// Cached gin_helper::Wrappable objects.
|
||||
v8::Global<v8::Value> cookies_;
|
||||
v8::Global<v8::Value> extensions_;
|
||||
v8::Global<v8::Value> protocol_;
|
||||
v8::Global<v8::Value> net_log_;
|
||||
v8::Global<v8::Value> service_worker_context_;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue