From acb5b75057c1576f1f0ba9b7553d3155702448c1 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Tue, 21 Jan 2020 09:42:55 -0800 Subject: [PATCH] chore: [extensions] support old APIs when enable_electron_extensions = true (#21812) --- filenames.auto.gni | 1 + lib/browser/chrome-extension-shim.js | 37 +++++++++++++++++++ lib/browser/init.ts | 4 +- .../gin_converters/extension_converter.cc | 2 + spec-main/extensions-spec.ts | 33 +++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 lib/browser/chrome-extension-shim.js diff --git a/filenames.auto.gni b/filenames.auto.gni index 27af484c8c02..5894803ccfb1 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -247,6 +247,7 @@ auto_filenames = { "lib/browser/api/views/text-field.js", "lib/browser/api/web-contents-view.js", "lib/browser/api/web-contents.js", + "lib/browser/chrome-extension-shim.js", "lib/browser/chrome-extension.js", "lib/browser/crash-reporter-init.js", "lib/browser/default-menu.ts", diff --git a/lib/browser/chrome-extension-shim.js b/lib/browser/chrome-extension-shim.js new file mode 100644 index 000000000000..c9e003517813 --- /dev/null +++ b/lib/browser/chrome-extension-shim.js @@ -0,0 +1,37 @@ +'use strict' + +// This is a temporary shim to aid in transition from the old +// BrowserWindow-based extensions stuff to the new native-backed extensions +// API. + +if (!process.electronBinding('features').isExtensionsEnabled()) { + throw new Error('Attempted to load JS chrome-extension shim without //extensions support enabled') +} + +const { app, session, BrowserWindow, deprecate } = require('electron') + +app.once('ready', function () { + const addExtension = function (srcDirectory) { + return session.defaultSession.loadExtension(srcDirectory) + } + + const removeExtension = function (name) { + const extension = session.defaultSession.getAllExtensions().find(e => e.name === name) + if (extension) { session.defaultSession.removeExtension(extension.id) } + } + + const getExtensions = function () { + const extensions = {} + 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 73418ac3d3e8..9d35e5b8953b 100644 --- a/lib/browser/init.ts +++ b/lib/browser/init.ts @@ -154,7 +154,9 @@ require('@electron/internal/browser/devtools') const features = process.electronBinding('features') // Load the chrome extension support. -if (!features.isExtensionsEnabled()) { +if (features.isExtensionsEnabled()) { + require('@electron/internal/browser/chrome-extension-shim') +} else { require('@electron/internal/browser/chrome-extension') } diff --git a/shell/common/gin_converters/extension_converter.cc b/shell/common/gin_converters/extension_converter.cc index e113c27b5fa6..a4c18339daff 100644 --- a/shell/common/gin_converters/extension_converter.cc +++ b/shell/common/gin_converters/extension_converter.cc @@ -15,6 +15,8 @@ v8::Local Converter::ToV8( const extensions::Extension* extension) { auto dict = gin::Dictionary::CreateEmpty(isolate); dict.Set("id", extension->id()); + dict.Set("name", extension->name()); + dict.Set("version", extension->VersionString()); return gin::ConvertToV8(isolate, dict); } diff --git a/spec-main/extensions-spec.ts b/spec-main/extensions-spec.ts index ae831f998a90..638df0c26e6f 100644 --- a/spec-main/extensions-spec.ts +++ b/spec-main/extensions-spec.ts @@ -210,6 +210,39 @@ ifdescribe(process.electronBinding('features').isExtensionsEnabled())('chrome ex await emittedOnce(ipcMain, 'winning') }) }) + + describe('deprecation shims', () => { + afterEach(() => { + (session.defaultSession as any).getAllExtensions().forEach((e: any) => { + (session.defaultSession as any).removeExtension(e.id) + }) + }) + + 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('') + }) + }) }) ifdescribe(!process.electronBinding('features').isExtensionsEnabled())('chrome extensions', () => {