Skip to content

Commit fa42b59

Browse files
authored
feat: flesh out the api for //extensions (electron#21587)
1 parent 8bc0c92 commit fa42b59

File tree

10 files changed

+159
-13
lines changed

10 files changed

+159
-13
lines changed

filenames.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ filenames = {
474474
"shell/common/gin_converters/callback_converter.h",
475475
"shell/common/gin_converters/content_converter.cc",
476476
"shell/common/gin_converters/content_converter.h",
477+
"shell/common/gin_converters/extension_converter.cc",
478+
"shell/common/gin_converters/extension_converter.h",
477479
"shell/common/gin_converters/file_dialog_converter.cc",
478480
"shell/common/gin_converters/file_dialog_converter.h",
479481
"shell/common/gin_converters/file_path_converter.h",

shell/browser/api/atom_api_session.cc

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
#include "ui/base/l10n/l10n_util.h"
6767

6868
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
69+
#include "extensions/browser/extension_registry.h"
6970
#include "shell/browser/extensions/atom_extension_system.h"
71+
#include "shell/common/gin_converters/extension_converter.h"
7072
#endif
7173

7274
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
@@ -606,10 +608,51 @@ std::vector<base::FilePath::StringType> Session::GetPreloads() const {
606608
}
607609

608610
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
609-
void Session::LoadChromeExtension(const base::FilePath extension_path) {
611+
v8::Local<v8::Promise> Session::LoadExtension(
612+
const base::FilePath& extension_path) {
613+
gin_helper::Promise<const extensions::Extension*> promise(isolate());
614+
v8::Local<v8::Promise> handle = promise.GetHandle();
615+
616+
auto* extension_system = static_cast<extensions::AtomExtensionSystem*>(
617+
extensions::ExtensionSystem::Get(browser_context()));
618+
// TODO(nornagon): make LoadExtension() asynchronous.
619+
auto* extension = extension_system->LoadExtension(extension_path);
620+
621+
if (extension) {
622+
promise.Resolve(extension);
623+
} else {
624+
// TODO(nornagon): plumb through error message from extension loader.
625+
promise.RejectWithErrorMessage("Failed to load extension");
626+
}
627+
628+
return handle;
629+
}
630+
631+
void Session::RemoveExtension(const std::string& extension_id) {
610632
auto* extension_system = static_cast<extensions::AtomExtensionSystem*>(
611633
extensions::ExtensionSystem::Get(browser_context()));
612-
extension_system->LoadExtension(extension_path);
634+
extension_system->RemoveExtension(extension_id);
635+
}
636+
637+
v8::Local<v8::Value> Session::GetExtension(const std::string& extension_id) {
638+
auto* registry = extensions::ExtensionRegistry::Get(browser_context());
639+
const extensions::Extension* extension =
640+
registry->GetInstalledExtension(extension_id);
641+
if (extension) {
642+
return gin::ConvertToV8(isolate(), extension);
643+
} else {
644+
return v8::Null(isolate());
645+
}
646+
}
647+
648+
v8::Local<v8::Value> Session::GetAllExtensions() {
649+
auto* registry = extensions::ExtensionRegistry::Get(browser_context());
650+
auto installed_extensions = registry->GenerateInstalledExtensionsSet();
651+
std::vector<const extensions::Extension*> extensions_vector;
652+
for (const auto& extension : *installed_extensions) {
653+
extensions_vector.emplace_back(extension.get());
654+
}
655+
return gin::ConvertToV8(isolate(), extensions_vector);
613656
}
614657
#endif
615658

@@ -797,7 +840,10 @@ void Session::BuildPrototype(v8::Isolate* isolate,
797840
.SetMethod("setPreloads", &Session::SetPreloads)
798841
.SetMethod("getPreloads", &Session::GetPreloads)
799842
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
800-
.SetMethod("loadChromeExtension", &Session::LoadChromeExtension)
843+
.SetMethod("loadExtension", &Session::LoadExtension)
844+
.SetMethod("removeExtension", &Session::RemoveExtension)
845+
.SetMethod("getExtension", &Session::GetExtension)
846+
.SetMethod("getAllExtensions", &Session::GetAllExtensions)
801847
#endif
802848
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
803849
.SetMethod("getSpellCheckerLanguages", &Session::GetSpellCheckerLanguages)

shell/browser/api/atom_api_session.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ class Session : public gin_helper::TrackableObject<Session>,
9696
#endif
9797

9898
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
99-
void LoadChromeExtension(const base::FilePath extension_path);
99+
v8::Local<v8::Promise> LoadExtension(const base::FilePath& extension_path);
100+
void RemoveExtension(const std::string& extension_id);
101+
v8::Local<v8::Value> GetExtension(const std::string& extension_id);
102+
v8::Local<v8::Value> GetAllExtensions();
100103
#endif
101104

102105
protected:

shell/browser/extensions/atom_extension_loader.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const Extension* AtomExtensionLoader::LoadExtension(
7878
return extension.get();
7979
}
8080

81-
void AtomExtensionLoader::ReloadExtension(ExtensionId extension_id) {
81+
void AtomExtensionLoader::ReloadExtension(const ExtensionId& extension_id) {
8282
const Extension* extension = ExtensionRegistry::Get(browser_context_)
8383
->GetInstalledExtension(extension_id);
8484
// We shouldn't be trying to reload extensions that haven't been added.
@@ -94,8 +94,14 @@ void AtomExtensionLoader::ReloadExtension(ExtensionId extension_id) {
9494
return;
9595
}
9696

97+
void AtomExtensionLoader::UnloadExtension(
98+
const ExtensionId& extension_id,
99+
extensions::UnloadedExtensionReason reason) {
100+
extension_registrar_.RemoveExtension(extension_id, reason);
101+
}
102+
97103
void AtomExtensionLoader::FinishExtensionReload(
98-
const ExtensionId old_extension_id,
104+
const ExtensionId& old_extension_id,
99105
scoped_refptr<const Extension> extension) {
100106
if (extension) {
101107
extension_registrar_.AddExtension(extension);

shell/browser/extensions/atom_extension_loader.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ class AtomExtensionLoader : public ExtensionRegistrar::Delegate {
4141
// reloading.
4242
// This may invalidate references to the old Extension object, so it takes the
4343
// ID by value.
44-
void ReloadExtension(ExtensionId extension_id);
44+
void ReloadExtension(const ExtensionId& extension_id);
45+
46+
void UnloadExtension(const ExtensionId& extension_id,
47+
extensions::UnloadedExtensionReason reason);
4548

4649
private:
4750
// If the extension loaded successfully, enables it. If it's an app, launches
4851
// it. If the load failed, updates ShellKeepAliveRequester.
49-
void FinishExtensionReload(const ExtensionId old_extension_id,
52+
void FinishExtensionReload(const ExtensionId& old_extension_id,
5053
scoped_refptr<const Extension> extension);
5154

5255
// ExtensionRegistrar::Delegate:

shell/browser/extensions/atom_extension_system.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const Extension* AtomExtensionSystem::LoadExtension(
4949
}
5050

5151
const Extension* AtomExtensionSystem::LoadApp(const base::FilePath& app_dir) {
52-
CHECK(false); // Should never call LoadApp
52+
NOTIMPLEMENTED() << "Attempted to load platform app in Electron";
5353
return nullptr;
5454
}
5555

@@ -66,6 +66,11 @@ void AtomExtensionSystem::ReloadExtension(const ExtensionId& extension_id) {
6666
extension_loader_->ReloadExtension(extension_id);
6767
}
6868

69+
void AtomExtensionSystem::RemoveExtension(const ExtensionId& extension_id) {
70+
extension_loader_->UnloadExtension(
71+
extension_id, extensions::UnloadedExtensionReason::UNINSTALL);
72+
}
73+
6974
void AtomExtensionSystem::Shutdown() {
7075
extension_loader_.reset();
7176
}

shell/browser/extensions/atom_extension_system.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class AtomExtensionSystem : public ExtensionSystem {
5353
// Reloads the extension with id |extension_id|.
5454
void ReloadExtension(const ExtensionId& extension_id);
5555

56+
void RemoveExtension(const ExtensionId& extension_id);
57+
5658
// KeyedService implementation:
5759
void Shutdown() override;
5860

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2019 Slack Technologies, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "shell/common/gin_converters/extension_converter.h"
6+
7+
#include "extensions/common/extension.h"
8+
#include "gin/dictionary.h"
9+
10+
namespace gin {
11+
12+
// static
13+
v8::Local<v8::Value> Converter<const extensions::Extension*>::ToV8(
14+
v8::Isolate* isolate,
15+
const extensions::Extension* extension) {
16+
auto dict = gin::Dictionary::CreateEmpty(isolate);
17+
dict.Set("id", extension->id());
18+
return gin::ConvertToV8(isolate, dict);
19+
}
20+
21+
} // namespace gin
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2019 Slack Technologies, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_COMMON_GIN_CONVERTERS_EXTENSION_CONVERTER_H_
6+
#define SHELL_COMMON_GIN_CONVERTERS_EXTENSION_CONVERTER_H_
7+
8+
#include <string>
9+
10+
#include "gin/converter.h"
11+
12+
namespace extensions {
13+
class Extension;
14+
} // namespace extensions
15+
16+
namespace gin {
17+
18+
template <>
19+
struct Converter<const extensions::Extension*> {
20+
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
21+
const extensions::Extension* val);
22+
};
23+
24+
} // namespace gin
25+
26+
#endif // SHELL_COMMON_GIN_CONVERTERS_EXTENSION_CONVERTER_H_

spec-main/extensions-spec.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,48 @@ ifdescribe(process.electronBinding('features').isExtensionsEnabled())('chrome ex
3232
// extension in an in-memory session results in it being installed in the
3333
// default session.
3434
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
35-
(customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'red-bg'))
35+
(customSession as any).loadExtension(path.join(fixtures, 'extensions', 'red-bg'))
3636
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } })
3737
await w.loadURL(url)
3838
const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
3939
expect(bg).to.equal('red')
4040
})
4141

42+
it('removes an extension', async () => {
43+
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`)
44+
const { id } = await (customSession as any).loadExtension(path.join(fixtures, 'extensions', 'red-bg'))
45+
{
46+
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } })
47+
await w.loadURL(url)
48+
const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
49+
expect(bg).to.equal('red')
50+
}
51+
(customSession as any).removeExtension(id)
52+
{
53+
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } })
54+
await w.loadURL(url)
55+
const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
56+
expect(bg).to.equal('')
57+
}
58+
})
59+
60+
it('lists loaded extensions in getAllExtensions', async () => {
61+
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`)
62+
const e = await (customSession as any).loadExtension(path.join(fixtures, 'extensions', 'red-bg'))
63+
expect((customSession as any).getAllExtensions()).to.deep.equal([e]);
64+
(customSession as any).removeExtension(e.id)
65+
expect((customSession as any).getAllExtensions()).to.deep.equal([])
66+
})
67+
68+
it('gets an extension by id', async () => {
69+
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`)
70+
const e = await (customSession as any).loadExtension(path.join(fixtures, 'extensions', 'red-bg'))
71+
expect((customSession as any).getExtension(e.id)).to.deep.equal(e)
72+
})
73+
4274
it('confines an extension to the session it was loaded in', async () => {
4375
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
44-
(customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'red-bg'))
76+
(customSession as any).loadExtension(path.join(fixtures, 'extensions', 'red-bg'))
4577
const w = new BrowserWindow({ show: false }) // not in the session
4678
await w.loadURL(url)
4779
const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
@@ -52,7 +84,7 @@ ifdescribe(process.electronBinding('features').isExtensionsEnabled())('chrome ex
5284
let content: any
5385
before(async () => {
5486
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
55-
(customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'chrome-runtime'))
87+
(customSession as any).loadExtension(path.join(fixtures, 'extensions', 'chrome-runtime'))
5688
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } })
5789
try {
5890
await w.loadURL(url)
@@ -76,7 +108,7 @@ ifdescribe(process.electronBinding('features').isExtensionsEnabled())('chrome ex
76108
describe('chrome.storage', () => {
77109
it('stores and retrieves a key', async () => {
78110
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
79-
(customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'chrome-storage'))
111+
(customSession as any).loadExtension(path.join(fixtures, 'extensions', 'chrome-storage'))
80112
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession, nodeIntegration: true } })
81113
try {
82114
const p = emittedOnce(ipcMain, 'storage-success')

0 commit comments

Comments
 (0)