From c71cdce0b7d450ca49505a226a6738c4f0e01a8e Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 31 Jul 2019 14:25:41 -0700 Subject: [PATCH] chore: disable JS impl when //extensions support is on (#19512) --- lib/browser/chrome-extension.js | 4 ++++ lib/browser/init.ts | 4 +++- lib/browser/rpc-server.js | 16 +++++++++++++--- lib/renderer/chrome-api.ts | 4 ++++ lib/renderer/init.ts | 10 +++++++--- lib/sandboxed_renderer/init.js | 8 ++++++-- shell/renderer/atom_render_frame_observer.cc | 3 +++ shell/renderer/atom_renderer_client.cc | 5 +++++ shell/renderer/atom_sandboxed_renderer_client.cc | 5 +++++ 9 files changed, 50 insertions(+), 9 deletions(-) diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index 797c2df6105e..1c945d194c96 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -1,5 +1,9 @@ 'use strict' +if (process.electronBinding('features').isExtensionsEnabled()) { + throw new Error('Attempted to load JS chrome-extension polyfill with //extensions support enabled') +} + const { app, webContents, BrowserWindow } = require('electron') const { getAllWebContents } = process.electronBinding('web_contents') const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils') diff --git a/lib/browser/init.ts b/lib/browser/init.ts index 96ffe88851b3..f75698b3da36 100644 --- a/lib/browser/init.ts +++ b/lib/browser/init.ts @@ -152,7 +152,9 @@ app._setDefaultAppPaths(packagePath) require('@electron/internal/browser/devtools') // Load the chrome extension support. -require('@electron/internal/browser/chrome-extension') +if (!process.electronBinding('features').isExtensionsEnabled()) { + require('@electron/internal/browser/chrome-extension') +} // Load protocol module to ensure it is populated on app ready require('@electron/internal/browser/api/protocol') diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index fd511c9439e9..aa66388610ee 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -9,7 +9,6 @@ const eventBinding = process.electronBinding('event') const clipboard = process.electronBinding('clipboard') const features = process.electronBinding('features') -const { getContentScripts } = require('@electron/internal/browser/chrome-extension') const { crashReporterInit } = require('@electron/internal/browser/crash-reporter-init') const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal') const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils') @@ -526,13 +525,24 @@ const getPreloadScript = async function (preloadPath) { return { preloadPath, preloadSrc, preloadError } } -ipcMainUtils.handle('ELECTRON_GET_CONTENT_SCRIPTS', () => getContentScripts()) +if (process.electronBinding('features').isExtensionsEnabled()) { + ipcMainUtils.handle('ELECTRON_GET_CONTENT_SCRIPTS', () => []) +} else { + const { getContentScripts } = require('@electron/internal/browser/chrome-extension') + ipcMainUtils.handle('ELECTRON_GET_CONTENT_SCRIPTS', () => getContentScripts()) +} ipcMainUtils.handle('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) { const preloadPaths = event.sender._getPreloadPaths() + let contentScripts = [] + if (!process.electronBinding('features').isExtensionsEnabled()) { + const { getContentScripts } = require('@electron/internal/browser/chrome-extension') + contentScripts = getContentScripts() + } + return { - contentScripts: getContentScripts(), + contentScripts, preloadScripts: await Promise.all(preloadPaths.map(path => getPreloadScript(path))), isRemoteModuleEnabled: isRemoteModuleEnabled(event.sender), isWebViewTagEnabled: guestViewManager.isWebViewTagEnabled(event.sender), diff --git a/lib/renderer/chrome-api.ts b/lib/renderer/chrome-api.ts index ad07f7c2c6d4..19b5576569e1 100644 --- a/lib/renderer/chrome-api.ts +++ b/lib/renderer/chrome-api.ts @@ -67,6 +67,10 @@ class Port { // Inject chrome API to the |context| export function injectTo (extensionId: string, context: any) { + if (process.electronBinding('features').isExtensionsEnabled()) { + throw new Error('Attempted to load JS chrome-extension polyfill with //extensions support enabled') + } + const chrome = context.chrome = context.chrome || {} ipcRendererInternal.on(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, ( diff --git a/lib/renderer/init.ts b/lib/renderer/init.ts index 8e4fbf760081..24b2b979a8db 100644 --- a/lib/renderer/init.ts +++ b/lib/renderer/init.ts @@ -101,7 +101,9 @@ switch (window.location.protocol) { } case 'chrome-extension:': { // Inject the chrome.* APIs that chrome extensions require - require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window) + if (!process.electronBinding('features').isExtensionsEnabled()) { + require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window) + } break } case 'chrome:': @@ -112,8 +114,10 @@ switch (window.location.protocol) { windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen) // Inject content scripts. - const contentScripts = ipcRendererUtils.invokeSync('ELECTRON_GET_CONTENT_SCRIPTS') as Electron.ContentScriptEntry[] - require('@electron/internal/renderer/content-scripts-injector')(contentScripts) + if (!process.electronBinding('features').isExtensionsEnabled()) { + const contentScripts = ipcRendererUtils.invokeSync('ELECTRON_GET_CONTENT_SCRIPTS') as Electron.ContentScriptEntry[] + require('@electron/internal/renderer/content-scripts-injector')(contentScripts) + } } } diff --git a/lib/sandboxed_renderer/init.js b/lib/sandboxed_renderer/init.js index b8d55ce5e723..424cd8002fa2 100644 --- a/lib/sandboxed_renderer/init.js +++ b/lib/sandboxed_renderer/init.js @@ -116,7 +116,9 @@ switch (window.location.protocol) { } case 'chrome-extension:': { // Inject the chrome.* APIs that chrome extensions require - require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window) + if (!process.electronBinding('features').isExtensionsEnabled()) { + require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window) + } break } case 'chrome': { @@ -124,7 +126,9 @@ switch (window.location.protocol) { } default: { // Inject content scripts. - require('@electron/internal/renderer/content-scripts-injector')(contentScripts) + if (!process.electronBinding('features').isExtensionsEnabled()) { + require('@electron/internal/renderer/content-scripts-injector')(contentScripts) + } } } diff --git a/shell/renderer/atom_render_frame_observer.cc b/shell/renderer/atom_render_frame_observer.cc index a22667f16ea5..b0e06d17c7d9 100644 --- a/shell/renderer/atom_render_frame_observer.cc +++ b/shell/renderer/atom_render_frame_observer.cc @@ -13,6 +13,7 @@ #include "base/trace_event/trace_event.h" #include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_view.h" +#include "electron/buildflags/buildflags.h" #include "electron/shell/common/api/api.mojom.h" #include "ipc/ipc_message_macros.h" #include "native_mate/dictionary.h" @@ -85,11 +86,13 @@ void AtomRenderFrameObserver::DidCreateScriptContext( renderer_client_->SetupMainWorldOverrides(context, render_frame_); } +#if !BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) if (world_id >= World::ISOLATED_WORLD_EXTENSIONS && world_id <= World::ISOLATED_WORLD_EXTENSIONS_END) { renderer_client_->SetupExtensionWorldOverrides(context, render_frame_, world_id); } +#endif } void AtomRenderFrameObserver::DraggableRegionsChanged() { diff --git a/shell/renderer/atom_renderer_client.cc b/shell/renderer/atom_renderer_client.cc index c19baec31a91..ffe535795289 100644 --- a/shell/renderer/atom_renderer_client.cc +++ b/shell/renderer/atom_renderer_client.cc @@ -9,6 +9,7 @@ #include "base/command_line.h" #include "content/public/renderer/render_frame.h" +#include "electron/buildflags/buildflags.h" #include "native_mate/dictionary.h" #include "shell/common/api/electron_bindings.h" #include "shell/common/api/event_emitter_caller.h" @@ -225,6 +226,9 @@ void AtomRendererClient::SetupExtensionWorldOverrides( v8::Handle context, content::RenderFrame* render_frame, int world_id) { +#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) + NOTREACHED(); +#else auto* isolate = context->GetIsolate(); std::vector> isolated_bundle_params = { @@ -244,6 +248,7 @@ void AtomRendererClient::SetupExtensionWorldOverrides( node::native_module::NativeModuleEnv::CompileAndCall( context, "electron/js2c/content_script_bundle", &isolated_bundle_params, &isolated_bundle_args, nullptr); +#endif } node::Environment* AtomRendererClient::GetEnvironment( diff --git a/shell/renderer/atom_sandboxed_renderer_client.cc b/shell/renderer/atom_sandboxed_renderer_client.cc index 12e0dc4e2897..d009bbe8a7d7 100644 --- a/shell/renderer/atom_sandboxed_renderer_client.cc +++ b/shell/renderer/atom_sandboxed_renderer_client.cc @@ -10,6 +10,7 @@ #include "base/path_service.h" #include "base/process/process_handle.h" #include "content/public/renderer/render_frame.h" +#include "electron/buildflags/buildflags.h" #include "gin/converter.h" #include "native_mate/dictionary.h" #include "shell/common/api/electron_bindings.h" @@ -266,6 +267,9 @@ void AtomSandboxedRendererClient::SetupExtensionWorldOverrides( v8::Handle context, content::RenderFrame* render_frame, int world_id) { +#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) + NOTREACHED(); +#else auto* isolate = context->GetIsolate(); mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate); @@ -284,6 +288,7 @@ void AtomSandboxedRendererClient::SetupExtensionWorldOverrides( node::native_module::NativeModuleEnv::CompileAndCall( context, "electron/js2c/content_script_bundle", &isolated_bundle_params, &isolated_bundle_args, nullptr); +#endif } void AtomSandboxedRendererClient::WillReleaseScriptContext(