From 6932e02eb862080f56d5ae811f3f3efce4309221 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 23 Nov 2020 20:39:08 +0100 Subject: [PATCH] refactor: use getWebPreference() for all options (#26531) --- lib/browser/guest-view-manager.ts | 2 +- lib/browser/rpc-server.ts | 10 --------- lib/renderer/api/module-list.ts | 4 ++-- lib/renderer/init.ts | 25 ++++++----------------- lib/sandboxed_renderer/api/module-list.ts | 6 +++++- lib/sandboxed_renderer/init.ts | 18 ++++++---------- shell/renderer/renderer_client_base.cc | 6 ------ 7 files changed, 20 insertions(+), 51 deletions(-) diff --git a/lib/browser/guest-view-manager.ts b/lib/browser/guest-view-manager.ts index b57fdecad4e..a88305cd2d4 100644 --- a/lib/browser/guest-view-manager.ts +++ b/lib/browser/guest-view-manager.ts @@ -276,7 +276,7 @@ const watchEmbedder = function (embedder: Electron.WebContents) { const isWebViewTagEnabledCache = new WeakMap(); -export const isWebViewTagEnabled = function (contents: Electron.WebContents) { +const isWebViewTagEnabled = function (contents: Electron.WebContents) { if (!isWebViewTagEnabledCache.has(contents)) { const webPreferences = contents.getLastWebPreferences() || {}; isWebViewTagEnabledCache.set(contents, !!webPreferences.webviewTag); diff --git a/lib/browser/rpc-server.ts b/lib/browser/rpc-server.ts index b639ef7028e..4d7a7870143 100644 --- a/lib/browser/rpc-server.ts +++ b/lib/browser/rpc-server.ts @@ -4,7 +4,6 @@ import { clipboard, crashReporter, nativeImage } from 'electron/common'; import * as fs from 'fs'; import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'; import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils'; -import * as guestViewManager from '@electron/internal/browser/guest-view-manager'; import * as typeUtils from '@electron/internal/common/type-utils'; import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages'; @@ -74,10 +73,6 @@ if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) { }); } -const isRemoteModuleEnabled = BUILDFLAG(ENABLE_REMOTE_MODULE) - ? require('@electron/internal/browser/remote/server').isRemoteModuleEnabled - : () => false; - const getPreloadScript = async function (preloadPath: string) { let preloadSrc = null; let preloadError = null; @@ -91,14 +86,9 @@ const getPreloadScript = async function (preloadPath: string) { ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD, async function (event) { const preloadPaths = event.sender._getPreloadPaths(); - const webPreferences = event.sender.getLastWebPreferences() || {}; return { preloadScripts: await Promise.all(preloadPaths.map(path => getPreloadScript(path))), - isRemoteModuleEnabled: isRemoteModuleEnabled(event.sender), - isWebViewTagEnabled: guestViewManager.isWebViewTagEnabled(event.sender), - guestInstanceId: webPreferences.guestInstanceId, - openerId: webPreferences.openerId, process: { arch: process.arch, platform: process.platform, diff --git a/lib/renderer/api/module-list.ts b/lib/renderer/api/module-list.ts index dcc7394eda2..0263284d9a6 100644 --- a/lib/renderer/api/module-list.ts +++ b/lib/renderer/api/module-list.ts @@ -1,6 +1,6 @@ -const v8Util = process._linkedBinding('electron_common_v8_util'); +const { getWebPreference } = process._linkedBinding('electron_renderer_web_frame'); -const enableRemoteModule = v8Util.getHiddenValue(global, 'enableRemoteModule'); +const enableRemoteModule = getWebPreference(window, 'enableRemoteModule'); // Renderer side modules, please sort alphabetically. export const rendererModuleList: ElectronInternal.ModuleEntry[] = [ diff --git a/lib/renderer/init.ts b/lib/renderer/init.ts index d1ab84dd030..d098b41ece1 100644 --- a/lib/renderer/init.ts +++ b/lib/renderer/init.ts @@ -57,30 +57,16 @@ webFrameInit(); const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line'); const { getWebPreference } = process._linkedBinding('electron_renderer_web_frame'); -const parseOption = function ( - name: string, defaultValue: TDefault, converter?: (value: string) => any -) { - const value = getWebPreference(window, name); - return value - ? ( - converter - ? converter(value) - : value - ) - : defaultValue; -}; - const contextIsolation = getWebPreference(window, 'contextIsolation'); const nodeIntegration = getWebPreference(window, 'nodeIntegration'); const webviewTag = getWebPreference(window, 'webviewTag'); const isHiddenPage = getWebPreference(window, 'hiddenPage'); const usesNativeWindowOpen = getWebPreference(window, 'nativeWindowOpen'); const rendererProcessReuseEnabled = getWebPreference(window, 'disableElectronSiteInstanceOverrides'); - -const preloadScript = parseOption('preload', null); -const preloadScripts = parseOption('preloadScripts', []); -const guestInstanceId = parseOption('guestInstanceId', null, value => parseInt(value)); -const openerId = parseOption('openerId', null, value => parseInt(value)); +const preloadScript = getWebPreference(window, 'preload'); +const preloadScripts = getWebPreference(window, 'preloadScripts'); +const guestInstanceId = getWebPreference(window, 'guestInstanceId') || null; +const openerId = getWebPreference(window, 'openerId') || null; const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null; // The webContents preload script is loaded after the session preload scripts. @@ -97,8 +83,9 @@ switch (window.location.protocol) { case 'chrome-extension:': { break; } - case 'chrome:': + case 'chrome:': { break; + } default: { // Override default web functions. const { windowSetup } = require('@electron/internal/renderer/window-setup'); diff --git a/lib/sandboxed_renderer/api/module-list.ts b/lib/sandboxed_renderer/api/module-list.ts index ef957d36104..69f78937ad4 100644 --- a/lib/sandboxed_renderer/api/module-list.ts +++ b/lib/sandboxed_renderer/api/module-list.ts @@ -1,3 +1,7 @@ +const { getWebPreference } = process._linkedBinding('electron_renderer_web_frame'); + +const enableRemoteModule = getWebPreference(window, 'enableRemoteModule'); + export const moduleList: ElectronInternal.ModuleEntry[] = [ { name: 'contextBridge', @@ -34,7 +38,7 @@ if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) { }); } -if (BUILDFLAG(ENABLE_REMOTE_MODULE) && process.isRemoteModuleEnabled) { +if (BUILDFLAG(ENABLE_REMOTE_MODULE) && enableRemoteModule) { moduleList.push({ name: 'remote', loader: () => require('@electron/internal/renderer/api/remote') diff --git a/lib/sandboxed_renderer/init.ts b/lib/sandboxed_renderer/init.ts index ec10f88219f..301072850c5 100644 --- a/lib/sandboxed_renderer/init.ts +++ b/lib/sandboxed_renderer/init.ts @@ -23,16 +23,7 @@ Object.setPrototypeOf(process, EventEmitter.prototype); const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal'); const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils'); -const { - preloadScripts, - isRemoteModuleEnabled, - isWebViewTagEnabled, - guestInstanceId, - openerId, - process: processProps -} = ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD); - -process.isRemoteModuleEnabled = isRemoteModuleEnabled; +const { preloadScripts, process: processProps } = ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD); const electron = require('electron'); @@ -123,9 +114,12 @@ if (hasSwitch('unsafely-expose-electron-internals-for-testing')) { } const contextIsolation = getWebPreference(window, 'contextIsolation'); +const webviewTag = getWebPreference(window, 'webviewTag'); const isHiddenPage = getWebPreference(window, 'hiddenPage'); const rendererProcessReuseEnabled = getWebPreference(window, 'disableElectronSiteInstanceOverrides'); const usesNativeWindowOpen = true; +const guestInstanceId = getWebPreference(window, 'guestInstanceId') || null; +const openerId = getWebPreference(window, 'openerId') || null; switch (window.location.protocol) { case 'devtools:': { @@ -136,7 +130,7 @@ switch (window.location.protocol) { case 'chrome-extension:': { break; } - case 'chrome': { + case 'chrome:': { break; } default: { @@ -149,7 +143,7 @@ switch (window.location.protocol) { // Load webview tag implementation. if (process.isMainFrame) { const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init'); - webViewInit(contextIsolation, isWebViewTagEnabled, guestInstanceId); + webViewInit(contextIsolation, webviewTag, guestInstanceId); } // Wrap the script into a function executed in global scope. It won't have diff --git a/shell/renderer/renderer_client_base.cc b/shell/renderer/renderer_client_base.cc index 46d0941b189..e897f82d15c 100644 --- a/shell/renderer/renderer_client_base.cc +++ b/shell/renderer/renderer_client_base.cc @@ -132,12 +132,6 @@ void RendererClientBase::DidCreateScriptContext( "%s-%" PRId64, renderer_client_id_.c_str(), ++next_context_id_); gin_helper::Dictionary global(context->GetIsolate(), context->Global()); global.SetHidden("contextId", context_id); - -#if BUILDFLAG(ENABLE_REMOTE_MODULE) - bool enableRemoteModule = - render_frame->GetBlinkPreferences().enable_remote_module; - global.SetHidden("enableRemoteModule", enableRemoteModule); -#endif } void RendererClientBase::AddRenderBindings(