refactor: merge duplicate code from sandboxed/non-sandboxed renderer init (#31755)
This commit is contained in:
parent
b40f3d4cfc
commit
dfb3b24de1
5 changed files with 71 additions and 117 deletions
|
@ -143,6 +143,7 @@ auto_filenames = {
|
||||||
"lib/renderer/api/crash-reporter.ts",
|
"lib/renderer/api/crash-reporter.ts",
|
||||||
"lib/renderer/api/ipc-renderer.ts",
|
"lib/renderer/api/ipc-renderer.ts",
|
||||||
"lib/renderer/api/web-frame.ts",
|
"lib/renderer/api/web-frame.ts",
|
||||||
|
"lib/renderer/common-init.ts",
|
||||||
"lib/renderer/inspector.ts",
|
"lib/renderer/inspector.ts",
|
||||||
"lib/renderer/ipc-renderer-internal-utils.ts",
|
"lib/renderer/ipc-renderer-internal-utils.ts",
|
||||||
"lib/renderer/ipc-renderer-internal.ts",
|
"lib/renderer/ipc-renderer-internal.ts",
|
||||||
|
@ -272,6 +273,7 @@ auto_filenames = {
|
||||||
"lib/renderer/api/ipc-renderer.ts",
|
"lib/renderer/api/ipc-renderer.ts",
|
||||||
"lib/renderer/api/module-list.ts",
|
"lib/renderer/api/module-list.ts",
|
||||||
"lib/renderer/api/web-frame.ts",
|
"lib/renderer/api/web-frame.ts",
|
||||||
|
"lib/renderer/common-init.ts",
|
||||||
"lib/renderer/init.ts",
|
"lib/renderer/init.ts",
|
||||||
"lib/renderer/inspector.ts",
|
"lib/renderer/inspector.ts",
|
||||||
"lib/renderer/ipc-renderer-internal-utils.ts",
|
"lib/renderer/ipc-renderer-internal-utils.ts",
|
||||||
|
|
64
lib/renderer/common-init.ts
Normal file
64
lib/renderer/common-init.ts
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
import { ipcRenderer } from 'electron';
|
||||||
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
||||||
|
|
||||||
|
import type * as webViewInitModule from '@electron/internal/renderer/web-view/web-view-init';
|
||||||
|
import type * as windowSetupModule from '@electron/internal/renderer/window-setup';
|
||||||
|
import type * as webFrameInitModule from '@electron/internal/renderer/web-frame-init';
|
||||||
|
import type * as securityWarningsModule from '@electron/internal/renderer/security-warnings';
|
||||||
|
|
||||||
|
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
|
||||||
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
||||||
|
|
||||||
|
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
|
||||||
|
const webviewTag = mainFrame.getWebPreference('webviewTag');
|
||||||
|
const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
|
||||||
|
const nativeWindowOpen = mainFrame.getWebPreference('nativeWindowOpen') || process.sandboxed;
|
||||||
|
const isWebView = mainFrame.getWebPreference('isWebView');
|
||||||
|
const openerId = mainFrame.getWebPreference('openerId');
|
||||||
|
|
||||||
|
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
|
||||||
|
// invoking the 'onMessage' callback.
|
||||||
|
v8Util.setHiddenValue(global, 'ipcNative', {
|
||||||
|
onMessage (internal: boolean, channel: string, ports: MessagePort[], args: any[], senderId: number) {
|
||||||
|
if (internal && senderId !== 0) {
|
||||||
|
console.error(`Message ${channel} sent by unexpected WebContents (${senderId})`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const sender = internal ? ipcRendererInternal : ipcRenderer;
|
||||||
|
sender.emit(channel, { sender, senderId, ports }, ...args);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
switch (window.location.protocol) {
|
||||||
|
case 'devtools:': {
|
||||||
|
// Override some inspector APIs.
|
||||||
|
require('@electron/internal/renderer/inspector');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'chrome-extension:': {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'chrome:': {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
// Override default web functions.
|
||||||
|
const { windowSetup } = require('@electron/internal/renderer/window-setup') as typeof windowSetupModule;
|
||||||
|
windowSetup(isWebView, openerId, isHiddenPage, nativeWindowOpen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load webview tag implementation.
|
||||||
|
if (process.isMainFrame) {
|
||||||
|
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule;
|
||||||
|
webViewInit(webviewTag, isWebView);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init') as typeof webFrameInitModule;
|
||||||
|
webFrameInit();
|
||||||
|
|
||||||
|
// Warn about security issues
|
||||||
|
if (process.isMainFrame) {
|
||||||
|
const { securityWarnings } = require('@electron/internal/renderer/security-warnings') as typeof securityWarningsModule;
|
||||||
|
securityWarnings(nodeIntegration);
|
||||||
|
}
|
|
@ -2,10 +2,6 @@ import * as path from 'path';
|
||||||
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
||||||
|
|
||||||
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
||||||
import type * as webFrameInitModule from '@electron/internal/renderer/web-frame-init';
|
|
||||||
import type * as webViewInitModule from '@electron/internal/renderer/web-view/web-view-init';
|
|
||||||
import type * as windowSetupModule from '@electron/internal/renderer/window-setup';
|
|
||||||
import type * as securityWarningsModule from '@electron/internal/renderer/security-warnings';
|
|
||||||
|
|
||||||
const Module = require('module');
|
const Module = require('module');
|
||||||
|
|
||||||
|
@ -36,49 +32,24 @@ Module.wrapper = [
|
||||||
process.argv.splice(1, 1);
|
process.argv.splice(1, 1);
|
||||||
|
|
||||||
// Clear search paths.
|
// Clear search paths.
|
||||||
|
|
||||||
require('../common/reset-search-paths');
|
require('../common/reset-search-paths');
|
||||||
|
|
||||||
// Import common settings.
|
// Import common settings.
|
||||||
require('@electron/internal/common/init');
|
require('@electron/internal/common/init');
|
||||||
|
|
||||||
// The global variable will be used by ipc for event dispatching
|
|
||||||
const v8Util = process._linkedBinding('electron_common_v8_util');
|
|
||||||
|
|
||||||
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
|
||||||
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;
|
|
||||||
|
|
||||||
v8Util.setHiddenValue(global, 'ipcNative', {
|
|
||||||
onMessage (internal: boolean, channel: string, ports: any[], args: any[], senderId: number) {
|
|
||||||
if (internal && senderId !== 0) {
|
|
||||||
console.error(`Message ${channel} sent by unexpected WebContents (${senderId})`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const sender = internal ? ipcRendererInternal : ipcRenderer;
|
|
||||||
sender.emit(channel, { sender, senderId, ports }, ...args);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
process.getProcessMemoryInfo = () => {
|
process.getProcessMemoryInfo = () => {
|
||||||
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
|
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Use electron module after everything is ready.
|
|
||||||
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init') as typeof webFrameInitModule;
|
|
||||||
webFrameInit();
|
|
||||||
|
|
||||||
// Process command line arguments.
|
// Process command line arguments.
|
||||||
const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
|
const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
|
||||||
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
|
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
|
||||||
|
|
||||||
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
|
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
|
||||||
const webviewTag = mainFrame.getWebPreference('webviewTag');
|
|
||||||
const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
|
|
||||||
const usesNativeWindowOpen = mainFrame.getWebPreference('nativeWindowOpen');
|
|
||||||
const preloadScript = mainFrame.getWebPreference('preload');
|
const preloadScript = mainFrame.getWebPreference('preload');
|
||||||
const preloadScripts = mainFrame.getWebPreference('preloadScripts');
|
const preloadScripts = mainFrame.getWebPreference('preloadScripts');
|
||||||
const isWebView = mainFrame.getWebPreference('isWebView');
|
|
||||||
const openerId = mainFrame.getWebPreference('openerId');
|
|
||||||
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
|
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
|
||||||
|
|
||||||
// The webContents preload script is loaded after the session preload scripts.
|
// The webContents preload script is loaded after the session preload scripts.
|
||||||
|
@ -86,30 +57,8 @@ if (preloadScript) {
|
||||||
preloadScripts.push(preloadScript);
|
preloadScripts.push(preloadScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (window.location.protocol) {
|
// Common renderer initialization
|
||||||
case 'devtools:': {
|
require('@electron/internal/renderer/common-init');
|
||||||
// Override some inspector APIs.
|
|
||||||
require('@electron/internal/renderer/inspector');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'chrome-extension:': {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'chrome:': {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
// Override default web functions.
|
|
||||||
const { windowSetup } = require('@electron/internal/renderer/window-setup') as typeof windowSetupModule;
|
|
||||||
windowSetup(isWebView, openerId, isHiddenPage, usesNativeWindowOpen);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load webview tag implementation.
|
|
||||||
if (process.isMainFrame) {
|
|
||||||
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule;
|
|
||||||
webViewInit(webviewTag, isWebView);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nodeIntegration) {
|
if (nodeIntegration) {
|
||||||
// Export node bindings to global.
|
// Export node bindings to global.
|
||||||
|
@ -189,9 +138,3 @@ for (const preloadScript of preloadScripts) {
|
||||||
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, error);
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn about security issues
|
|
||||||
if (process.isMainFrame) {
|
|
||||||
const { securityWarnings } = require('@electron/internal/renderer/security-warnings') as typeof securityWarningsModule;
|
|
||||||
securityWarnings(nodeIntegration);
|
|
||||||
}
|
|
||||||
|
|
|
@ -281,7 +281,7 @@ const getWebPreferences = async function () {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function securityWarnings (nodeIntegration = false) {
|
export function securityWarnings (nodeIntegration: boolean) {
|
||||||
const loadHandler = async function () {
|
const loadHandler = async function () {
|
||||||
if (shouldLogSecurityWarnings()) {
|
if (shouldLogSecurityWarnings()) {
|
||||||
const webPreferences = await getWebPreferences();
|
const webPreferences = await getWebPreferences();
|
||||||
|
|
|
@ -4,10 +4,6 @@ import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
||||||
|
|
||||||
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
||||||
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
||||||
import type * as webFrameInitModule from '@electron/internal/renderer/web-frame-init';
|
|
||||||
import type * as webViewInitModule from '@electron/internal/renderer/web-view/web-view-init';
|
|
||||||
import type * as windowSetupModule from '@electron/internal/renderer/window-setup';
|
|
||||||
import type * as securityWarningsModule from '@electron/internal/renderer/security-warnings';
|
|
||||||
|
|
||||||
const { EventEmitter } = events;
|
const { EventEmitter } = events;
|
||||||
|
|
||||||
|
@ -45,19 +41,6 @@ const loadableModules = new Map<string, Function>([
|
||||||
['url', () => require('url')]
|
['url', () => require('url')]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
|
|
||||||
// invoking the 'onMessage' callback.
|
|
||||||
v8Util.setHiddenValue(global, 'ipcNative', {
|
|
||||||
onMessage (internal: boolean, channel: string, ports: MessagePort[], args: any[], senderId: number) {
|
|
||||||
if (internal && senderId !== 0) {
|
|
||||||
console.error(`Message ${channel} sent by unexpected WebContents (${senderId})`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const sender = internal ? ipcRendererInternal : electron.ipcRenderer;
|
|
||||||
sender.emit(channel, { sender, senderId, ports }, ...args);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// ElectronSandboxedRendererClient will look for the "lifecycle" hidden object when
|
// ElectronSandboxedRendererClient will look for the "lifecycle" hidden object when
|
||||||
v8Util.setHiddenValue(global, 'lifecycle', {
|
v8Util.setHiddenValue(global, 'lifecycle', {
|
||||||
onLoaded () {
|
onLoaded () {
|
||||||
|
@ -74,9 +57,6 @@ v8Util.setHiddenValue(global, 'lifecycle', {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init') as typeof webFrameInitModule;
|
|
||||||
webFrameInit();
|
|
||||||
|
|
||||||
// Pass different process object to the preload script.
|
// Pass different process object to the preload script.
|
||||||
const preloadProcess: NodeJS.Process = new EventEmitter() as any;
|
const preloadProcess: NodeJS.Process = new EventEmitter() as any;
|
||||||
|
|
||||||
|
@ -119,7 +99,6 @@ function preloadRequire (module: string) {
|
||||||
|
|
||||||
// Process command line arguments.
|
// Process command line arguments.
|
||||||
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
|
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
|
||||||
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
|
|
||||||
|
|
||||||
// Similar to nodes --expose-internals flag, this exposes _linkedBinding so
|
// Similar to nodes --expose-internals flag, this exposes _linkedBinding so
|
||||||
// that tests can call it to get access to some test only bindings
|
// that tests can call it to get access to some test only bindings
|
||||||
|
@ -127,36 +106,8 @@ if (hasSwitch('unsafely-expose-electron-internals-for-testing')) {
|
||||||
preloadProcess._linkedBinding = process._linkedBinding;
|
preloadProcess._linkedBinding = process._linkedBinding;
|
||||||
}
|
}
|
||||||
|
|
||||||
const webviewTag = mainFrame.getWebPreference('webviewTag');
|
// Common renderer initialization
|
||||||
const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
|
require('@electron/internal/renderer/common-init');
|
||||||
const usesNativeWindowOpen = true;
|
|
||||||
const isWebView = mainFrame.getWebPreference('isWebView');
|
|
||||||
const openerId = mainFrame.getWebPreference('openerId');
|
|
||||||
|
|
||||||
switch (window.location.protocol) {
|
|
||||||
case 'devtools:': {
|
|
||||||
// Override some inspector APIs.
|
|
||||||
require('@electron/internal/renderer/inspector');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'chrome-extension:': {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'chrome:': {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
// Override default web functions.
|
|
||||||
const { windowSetup } = require('@electron/internal/renderer/window-setup') as typeof windowSetupModule;
|
|
||||||
windowSetup(isWebView, openerId, isHiddenPage, usesNativeWindowOpen);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load webview tag implementation.
|
|
||||||
if (process.isMainFrame) {
|
|
||||||
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule;
|
|
||||||
webViewInit(webviewTag, isWebView);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wrap the script into a function executed in global scope. It won't have
|
// Wrap the script into a function executed in global scope. It won't have
|
||||||
// access to the current scope, so we'll expose a few objects as arguments:
|
// access to the current scope, so we'll expose a few objects as arguments:
|
||||||
|
@ -191,9 +142,3 @@ for (const { preloadPath, preloadSrc, preloadError } of preloadScripts) {
|
||||||
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadPath, error);
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadPath, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn about security issues
|
|
||||||
if (process.isMainFrame) {
|
|
||||||
const { securityWarnings } = require('@electron/internal/renderer/security-warnings') as typeof securityWarningsModule;
|
|
||||||
securityWarnings();
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue