Browse Source

feat: session.enableExtension and session.disableExtension

Samuel Maddock 2 months ago
parent
commit
478b4a3c30

+ 19 - 0
docs/api/session.md

@@ -1541,6 +1541,25 @@ Unloads an extension.
 **Note:** This API cannot be called before the `ready` event of the `app` module
 is emitted.
 
+#### `ses.enableExtension(extensionId)`
+
+* `extensionId` string - ID of extension to enable
+
+Enables the extension and activates it for use by starting any background
+workers. If the extension is disabled, marks it as enabled.
+
+**Note:** This API cannot be called before the `ready` event of the `app` module
+is emitted.
+
+#### `ses.disableExtension(extensionId)`
+
+* `extensionId` string - ID of extension to disable
+
+Disables the extension and deactives it.
+
+**Note:** This API cannot be called before the `ready` event of the `app` module
+is emitted.
+
 #### `ses.getExtension(extensionId)`
 
 * `extensionId` string - ID of extension to query

+ 12 - 0
shell/browser/api/electron_api_session.cc

@@ -1361,6 +1361,18 @@ void Session::RemoveExtension(const std::string& extension_id) {
   extension_system->RemoveExtension(extension_id);
 }
 
+void Session::EnableExtension(const std::string& extension_id) {
+  auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
+      extensions::ExtensionSystem::Get(browser_context()));
+  extension_system->EnableExtension(extension_id);
+}
+
+void Session::DisableExtension(const std::string& extension_id) {
+  auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
+      extensions::ExtensionSystem::Get(browser_context()));
+  extension_system->DisableExtension(extension_id);
+}
+
 v8::Local<v8::Value> Session::GetExtension(const std::string& extension_id) {
   auto* registry = extensions::ExtensionRegistry::Get(browser_context());
   const extensions::Extension* extension =

+ 2 - 0
shell/browser/api/electron_api_session.h

@@ -182,6 +182,8 @@ class Session final : public gin::Wrappable<Session>,
   v8::Local<v8::Promise> LoadExtension(const base::FilePath& extension_path,
                                        gin::Arguments* args);
   void RemoveExtension(const std::string& extension_id);
+  void EnableExtension(const std::string& extension_id);
+  void DisableExtension(const std::string& extension_id);
   v8::Local<v8::Value> GetExtension(const std::string& extension_id);
   v8::Local<v8::Value> GetAllExtensions();