Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extensions): expose ExtensionRegistryObserver events in Session #25385

Merged
merged 4 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,40 @@ session.defaultSession.on('will-download', (event, item, webContents) => {
})
```

#### Event: 'extension-loaded'

Returns:

* `event` Event
* `extension` [Extension](structures/extension.md)

Emitted after an extension is loaded. This occurs whenever an extension is
added to the "enabled" set of extensions. This includes:
- Extensions being loaded from `Session.loadExtension`.
- Extensions being reloaded:
* from a crash.
* if the extension requested it ([`chrome.runtime.reload()`](https://developer.chrome.com/extensions/runtime#method-reload)).

#### Event: 'extension-unloaded'

Returns:

* `event` Event
* `extension` [Extension](structures/extension.md)

Emitted after an extension is unloaded. This occurs when
`Session.removeExtension` is called.

#### Event: 'extension-ready'

Returns:

* `event` Event
* `extension` [Extension](structures/extension.md)

Emitted after an extension is loaded and all necessary browser state is
initialized to support the start of the extension's background page.

#### Event: 'preconnect'

Returns:
Expand Down
24 changes: 24 additions & 0 deletions shell/browser/api/electron_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ 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() {
Expand All @@ -294,6 +298,10 @@ Session::~Session() {
service->SetHunspellObserver(nullptr);
}
#endif

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
extensions::ExtensionRegistry::Get(browser_context())->RemoveObserver(this);
samuelmaddock marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

void Session::OnDownloadCreated(content::DownloadManager* manager,
Expand Down Expand Up @@ -749,6 +757,22 @@ v8::Local<v8::Value> Session::GetAllExtensions() {
}
return gin::ConvertToV8(v8::Isolate::GetCurrent(), 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) {
Expand Down
17 changes: 17 additions & 0 deletions shell/browser/api/electron_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#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 {
Expand Down Expand Up @@ -55,6 +60,9 @@ class Session : public gin::Wrappable<Session>,
public gin_helper::CleanedUpAtExit,
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
public SpellcheckHunspellDictionary::Observer,
#endif
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
public extensions::ExtensionRegistryObserver,
#endif
public content::DownloadManager::Observer {
public:
Expand Down Expand Up @@ -126,6 +134,15 @@ class Session : public gin::Wrappable<Session>,
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

protected:
Expand Down
17 changes: 17 additions & 0 deletions spec-main/extensions-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ describe('chrome extensions', () => {
}
});

it('emits extension lifecycle events', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);

const loadedPromise = emittedOnce(customSession, 'extension-loaded');
const extension = await customSession.loadExtension(path.join(fixtures, 'extensions', 'red-bg'));
const [, loadedExtension] = await loadedPromise;
const [, readyExtension] = await emittedOnce(customSession, 'extension-ready');

expect(loadedExtension.id).to.equal(extension.id);
expect(readyExtension.id).to.equal(extension.id);

const unloadedPromise = emittedOnce(customSession, 'extension-unloaded');
await customSession.removeExtension(extension.id);
const [, unloadedExtension] = await unloadedPromise;
expect(unloadedExtension.id).to.equal(extension.id);
});

it('lists loaded extensions in getAllExtensions', async () => {
const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
const e = await customSession.loadExtension(path.join(fixtures, 'extensions', 'red-bg'));
Expand Down