From 19954126e08c67022b89a886cadb10471ac853ae Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 30 Nov 2020 23:40:56 +0100 Subject: [PATCH] chore: remove deprecated extension APIs (#26696) --- docs/api/browser-window.md | 88 ---------------------------- docs/breaking-changes.md | 39 ++++++++++++ filenames.auto.gni | 1 - lib/browser/chrome-extension-shim.ts | 31 ---------- lib/browser/init.ts | 3 - spec-main/extensions-spec.ts | 31 +--------- spec-main/webview-spec.ts | 4 +- spec/ts-smoke/electron/main.ts | 2 - 8 files changed, 43 insertions(+), 156 deletions(-) delete mode 100644 lib/browser/chrome-extension-shim.ts diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e175fefee103..aa37bd2fdf59 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -723,94 +723,6 @@ Returns `BrowserWindow | null` - The window that owns the given `browserView`. I Returns `BrowserWindow | null` - The window with the given `id`. -#### `BrowserWindow.addExtension(path)` _Deprecated_ - -* `path` String - -Adds Chrome extension located at `path`, and returns extension's name. - -The method will also not return if the extension's manifest is missing or incomplete. - -**Note:** This API cannot be called before the `ready` event of the `app` module -is emitted. - -**Note:** This method is deprecated. Instead, use -[`ses.loadExtension(path)`](session.md#sesloadextensionpath). - -#### `BrowserWindow.removeExtension(name)` _Deprecated_ - -* `name` String - -Remove a Chrome extension by name. - -**Note:** This API cannot be called before the `ready` event of the `app` module -is emitted. - -**Note:** This method is deprecated. Instead, use -[`ses.removeExtension(extension_id)`](session.md#sesremoveextensionextensionid). - -#### `BrowserWindow.getExtensions()` _Deprecated_ - -Returns `Record` - The keys are the extension names and each value is -an Object containing `name` and `version` properties. - -**Note:** This API cannot be called before the `ready` event of the `app` module -is emitted. - -**Note:** This method is deprecated. Instead, use -[`ses.getAllExtensions()`](session.md#sesgetallextensions). - -#### `BrowserWindow.addDevToolsExtension(path)` _Deprecated_ - -* `path` String - -Adds DevTools extension located at `path`, and returns extension's name. - -The extension will be remembered so you only need to call this API once, this -API is not for programming use. If you try to add an extension that has already -been loaded, this method will not return and instead log a warning to the -console. - -The method will also not return if the extension's manifest is missing or incomplete. - -**Note:** This API cannot be called before the `ready` event of the `app` module -is emitted. - -**Note:** This method is deprecated. Instead, use -[`ses.loadExtension(path)`](session.md#sesloadextensionpath). - -#### `BrowserWindow.removeDevToolsExtension(name)` _Deprecated_ - -* `name` String - -Remove a DevTools extension by name. - -**Note:** This API cannot be called before the `ready` event of the `app` module -is emitted. - -**Note:** This method is deprecated. Instead, use -[`ses.removeExtension(extension_id)`](session.md#sesremoveextensionextensionid). - -#### `BrowserWindow.getDevToolsExtensions()` _Deprecated_ - -Returns `Record` - The keys are the extension names and each value is -an Object containing `name` and `version` properties. - -To check if a DevTools extension is installed you can run the following: - -```javascript -const { BrowserWindow } = require('electron') - -const installed = 'devtron' in BrowserWindow.getDevToolsExtensions() -console.log(installed) -``` - -**Note:** This API cannot be called before the `ready` event of the `app` module -is emitted. - -**Note:** This method is deprecated. Instead, use -[`ses.getAllExtensions()`](session.md#sesgetallextensions). - ### Instance Properties Objects created with `new BrowserWindow` have the following properties: diff --git a/docs/breaking-changes.md b/docs/breaking-changes.md index 166be0241cce..93ac2b2f3721 100644 --- a/docs/breaking-changes.md +++ b/docs/breaking-changes.md @@ -26,6 +26,45 @@ shell.moveItemToTrash(path) shell.trashItem(path).then(/* ... */) ``` +### Removed: `BrowserWindow` extension APIs + +The deprecated extension APIs have been removed: +* `BrowserWindow.addExtension(path)` +* `BrowserWindow.addDevToolsExtension(path)` +* `BrowserWindow.removeExtension(name)` +* `BrowserWindow.removeDevToolsExtension(name)` +* `BrowserWindow.getExtensions()` +* `BrowserWindow.getDevToolsExtensions()` + +Use the session APIs instead: +* `ses.loadExtension(path)` +* `ses.removeExtension(extension_id)` +* `ses.getAllExtensions()` + +```js +// Removed in Electron 13 +BrowserWindow.addExtension(path) +BrowserWindow.addDevToolsExtension(path) +// Replace with +session.defaultSession.loadExtension(path) +``` + +```js +// Removed in Electron 13 +BrowserWindow.removeExtension(name) +BrowserWindow.removeDevToolsExtension(name) +// Replace with +session.defaultSession.removeExtension(extension_id) +``` + +```js +// Removed in Electron 13 +BrowserWindow.getExtensions() +BrowserWindow.getDevToolsExtensions() +// Replace with +session.defaultSession.getAllExtensions() +``` + ## Planned Breaking API Changes (12.0) ### Removed: Pepper Flash support diff --git a/filenames.auto.gni b/filenames.auto.gni index 68825e65ef55..c9b219d38bbd 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -231,7 +231,6 @@ auto_filenames = { "lib/browser/api/web-contents-view.ts", "lib/browser/api/web-contents.ts", "lib/browser/api/web-frame-main.ts", - "lib/browser/chrome-extension-shim.ts", "lib/browser/default-menu.ts", "lib/browser/desktop-capturer.ts", "lib/browser/devtools.ts", diff --git a/lib/browser/chrome-extension-shim.ts b/lib/browser/chrome-extension-shim.ts deleted file mode 100644 index 9bf673293901..000000000000 --- a/lib/browser/chrome-extension-shim.ts +++ /dev/null @@ -1,31 +0,0 @@ -// This is a temporary shim to aid in transition from the old -// BrowserWindow-based extensions stuff to the new native-backed extensions -// API. - -import { app, session, BrowserWindow, deprecate } from 'electron/main'; - -app.whenReady().then(function () { - const addExtension = function (srcDirectory: string) { - return session.defaultSession.loadExtension(srcDirectory); - }; - - const removeExtension = function (name: string) { - const extension = session.defaultSession.getAllExtensions().find(e => e.name === name); - if (extension) { session.defaultSession.removeExtension(extension.id); } - }; - - const getExtensions = function () { - const extensions: Record = {}; - session.defaultSession.getAllExtensions().forEach(e => { - extensions[e.name] = e; - }); - return extensions; - }; - - BrowserWindow.addExtension = deprecate.moveAPI(addExtension, 'BrowserWindow.addExtension', 'session.loadExtension'); - BrowserWindow.removeExtension = deprecate.moveAPI(removeExtension, 'BrowserWindow.removeExtension', 'session.removeExtension'); - BrowserWindow.getExtensions = deprecate.moveAPI(getExtensions, 'BrowserWindow.getExtensions', 'session.getAllExtensions'); - BrowserWindow.addDevToolsExtension = deprecate.moveAPI(addExtension, 'BrowserWindow.addDevToolsExtension', 'session.loadExtension'); - BrowserWindow.removeDevToolsExtension = deprecate.moveAPI(removeExtension, 'BrowserWindow.removeDevToolsExtension', 'session.removeExtension'); - BrowserWindow.getDevToolsExtensions = deprecate.moveAPI(getExtensions, 'BrowserWindow.getDevToolsExtensions', 'session.getAllExtensions'); -}); diff --git a/lib/browser/init.ts b/lib/browser/init.ts index 8360dc65e677..58414b751be6 100644 --- a/lib/browser/init.ts +++ b/lib/browser/init.ts @@ -132,9 +132,6 @@ app._setDefaultAppPaths(packagePath); // Load the chrome devtools support. require('@electron/internal/browser/devtools'); -// Load the chrome extension support. -require('@electron/internal/browser/chrome-extension-shim'); - if (BUILDFLAG(ENABLE_REMOTE_MODULE)) { require('@electron/internal/browser/remote/server'); } diff --git a/spec-main/extensions-spec.ts b/spec-main/extensions-spec.ts index bc4624db93e3..5d5e6c08c9dc 100644 --- a/spec-main/extensions-spec.ts +++ b/spec-main/extensions-spec.ts @@ -401,33 +401,6 @@ describe('chrome extensions', () => { }); }); - describe('deprecation shims', () => { - it('loads an extension through BrowserWindow.addExtension', async () => { - BrowserWindow.addExtension(path.join(fixtures, 'extensions', 'red-bg')); - const w = new BrowserWindow({ show: false }); - await w.loadURL(url); - const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor'); - expect(bg).to.equal('red'); - }); - - it('loads an extension through BrowserWindow.addDevToolsExtension', async () => { - BrowserWindow.addDevToolsExtension(path.join(fixtures, 'extensions', 'red-bg')); - const w = new BrowserWindow({ show: false }); - await w.loadURL(url); - const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor'); - expect(bg).to.equal('red'); - }); - - it('removes an extension through BrowserWindow.removeExtension', async () => { - await (BrowserWindow.addExtension(path.join(fixtures, 'extensions', 'red-bg')) as any); - BrowserWindow.removeExtension('red-bg'); - const w = new BrowserWindow({ show: false }); - await w.loadURL(url); - const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor'); - expect(bg).to.equal(''); - }); - }); - describe('chrome extension content scripts', () => { const fixtures = path.resolve(__dirname, 'fixtures'); const extensionPath = path.resolve(fixtures, 'extensions'); @@ -510,11 +483,11 @@ describe('chrome extensions', () => { const COLOR_TRANSPARENT = 'rgba(0, 0, 0, 0)'; before(() => { - BrowserWindow.addExtension(contentScript); + session.defaultSession.loadExtension(contentScript); }); after(() => { - BrowserWindow.removeExtension('content-script-test'); + session.defaultSession.removeExtension('content-script-test'); }); beforeEach(() => { diff --git a/spec-main/webview-spec.ts b/spec-main/webview-spec.ts index d72d5085047d..217310bf5b93 100644 --- a/spec-main/webview-spec.ts +++ b/spec-main/webview-spec.ts @@ -181,10 +181,10 @@ describe(' tag', function () { nodeIntegration: true } }); - BrowserWindow.removeDevToolsExtension('foo'); + w.webContents.session.removeExtension('foo'); const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo'); - await BrowserWindow.addDevToolsExtension(extensionPath); + await w.webContents.session.loadExtension(extensionPath); w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'webview-devtools.html')); loadWebView(w.webContents, { diff --git a/spec/ts-smoke/electron/main.ts b/spec/ts-smoke/electron/main.ts index a728cd5ec979..2063cb52707d 100644 --- a/spec/ts-smoke/electron/main.ts +++ b/spec/ts-smoke/electron/main.ts @@ -462,8 +462,6 @@ window.setVibrancy('selection') window.setVibrancy('popover') window.setIcon('/path/to/icon') -const installed = BrowserWindow.getDevToolsExtensions().hasOwnProperty('devtron') - // content-tracing // https://github.com/electron/electron/blob/master/docs/api/content-tracing.md