2016-08-21 10:30:14 +00:00
|
|
|
/* eslint no-eval: "off" */
|
2020-07-09 04:19:49 +00:00
|
|
|
/* global binding */
|
2020-05-30 09:56:54 +00:00
|
|
|
import * as events from 'events';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2020-05-30 09:56:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { EventEmitter } = events;
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
process._linkedBinding = binding.get;
|
2017-02-20 13:59:39 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2019-06-02 20:03:03 +00:00
|
|
|
// Expose Buffer shim as a hidden value. This is used by C++ code to
|
2017-03-16 16:20:09 +00:00
|
|
|
// deserialize Buffer instances sent from browser process.
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.setHiddenValue(global, 'Buffer', Buffer);
|
2019-06-02 20:03:03 +00:00
|
|
|
// The process object created by webpack is not an event emitter, fix it so
|
2017-02-27 17:14:36 +00:00
|
|
|
// the API is more compatible with non-sandboxed renderers.
|
2020-05-30 09:56:54 +00:00
|
|
|
for (const prop of Object.keys(EventEmitter.prototype) as (keyof typeof process)[]) {
|
2020-03-20 15:12:18 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(process, prop)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
delete process[prop];
|
2017-02-27 17:14:36 +00:00
|
|
|
}
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
Object.setPrototypeOf(process, EventEmitter.prototype);
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
|
|
|
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils');
|
2018-12-05 18:07:56 +00:00
|
|
|
|
2020-11-23 19:39:08 +00:00
|
|
|
const { preloadScripts, process: processProps } = ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD);
|
2018-12-05 18:07:56 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const electron = require('electron');
|
2018-12-05 18:07:56 +00:00
|
|
|
|
2020-06-02 09:32:32 +00:00
|
|
|
const loadedModules = new Map<string, any>([
|
2017-03-21 13:41:23 +00:00
|
|
|
['electron', electron],
|
2020-06-02 00:37:02 +00:00
|
|
|
['electron/common', electron],
|
|
|
|
['electron/renderer', electron],
|
2020-06-02 09:32:32 +00:00
|
|
|
['events', events]
|
|
|
|
]);
|
|
|
|
|
|
|
|
const loadableModules = new Map<string, Function>([
|
|
|
|
['timers', () => require('timers')],
|
|
|
|
['url', () => require('url')]
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
|
|
|
|
// invoking the 'onMessage' callback.
|
|
|
|
v8Util.setHiddenValue(global, 'ipcNative', {
|
2020-05-30 09:56:54 +00:00
|
|
|
onMessage (internal: boolean, channel: string, ports: MessagePort[], args: any[], senderId: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const sender = internal ? ipcRendererInternal : electron.ipcRenderer;
|
|
|
|
sender.emit(channel, { sender, senderId, ports }, ...args);
|
2019-01-23 16:24:57 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-03-08 00:00:28 +00:00
|
|
|
|
2020-04-13 21:32:29 +00:00
|
|
|
// ElectronSandboxedRendererClient will look for the "lifecycle" hidden object when
|
2019-04-02 22:38:16 +00:00
|
|
|
v8Util.setHiddenValue(global, 'lifecycle', {
|
2019-04-04 18:35:06 +00:00
|
|
|
onLoaded () {
|
2020-05-30 09:56:54 +00:00
|
|
|
(process as events.EventEmitter).emit('loaded');
|
2019-04-04 18:35:06 +00:00
|
|
|
},
|
2019-04-02 22:38:16 +00:00
|
|
|
onExit () {
|
2020-05-30 09:56:54 +00:00
|
|
|
(process as events.EventEmitter).emit('exit');
|
2019-04-02 22:38:16 +00:00
|
|
|
},
|
|
|
|
onDocumentStart () {
|
2020-05-30 09:56:54 +00:00
|
|
|
(process as events.EventEmitter).emit('document-start');
|
2019-04-02 22:38:16 +00:00
|
|
|
},
|
|
|
|
onDocumentEnd () {
|
2020-05-30 09:56:54 +00:00
|
|
|
(process as events.EventEmitter).emit('document-end');
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-03-08 00:00:28 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init');
|
|
|
|
webFrameInit();
|
2018-04-12 01:57:40 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
// Pass different process object to the preload script.
|
2020-05-30 09:56:54 +00:00
|
|
|
const preloadProcess: NodeJS.Process = new EventEmitter() as any;
|
2018-09-26 05:44:55 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
Object.assign(preloadProcess, binding.process);
|
|
|
|
Object.assign(preloadProcess, processProps);
|
2018-09-30 21:24:00 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
Object.assign(process, binding.process);
|
|
|
|
Object.assign(process, processProps);
|
2018-05-21 12:56:05 +00:00
|
|
|
|
2018-12-05 09:34:09 +00:00
|
|
|
Object.defineProperty(preloadProcess, 'noDeprecation', {
|
|
|
|
get () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return process.noDeprecation;
|
2018-12-05 09:34:09 +00:00
|
|
|
},
|
|
|
|
set (value) {
|
2020-03-20 20:28:31 +00:00
|
|
|
process.noDeprecation = value;
|
2018-12-05 09:34:09 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-12-05 09:34:09 +00:00
|
|
|
|
2020-05-30 09:56:54 +00:00
|
|
|
process.on('loaded', () => (preloadProcess as events.EventEmitter).emit('loaded'));
|
|
|
|
process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit'));
|
|
|
|
(process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start'));
|
|
|
|
(process as events.EventEmitter).on('document-end', () => (preloadProcess as events.EventEmitter).emit('document-end'));
|
2017-02-27 17:14:36 +00:00
|
|
|
|
|
|
|
// This is the `require` function that will be visible to the preload script
|
2020-05-30 09:56:54 +00:00
|
|
|
function preloadRequire (module: string) {
|
2018-05-21 12:56:05 +00:00
|
|
|
if (loadedModules.has(module)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return loadedModules.get(module);
|
2018-05-21 12:56:05 +00:00
|
|
|
}
|
2020-06-02 09:32:32 +00:00
|
|
|
if (loadableModules.has(module)) {
|
|
|
|
const loadedModule = loadableModules.get(module)!();
|
|
|
|
loadedModules.set(module, loadedModule);
|
|
|
|
return loadedModule;
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`module not found: ${module}`);
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Process command line arguments.
|
2020-06-23 03:32:45 +00:00
|
|
|
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
|
2020-11-10 17:06:03 +00:00
|
|
|
const { getWebPreference } = process._linkedBinding('electron_renderer_web_frame');
|
2019-01-03 16:22:34 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
// Similar to nodes --expose-internals flag, this exposes _linkedBinding so
|
2020-05-15 18:57:40 +00:00
|
|
|
// that tests can call it to get access to some test only bindings
|
|
|
|
if (hasSwitch('unsafely-expose-electron-internals-for-testing')) {
|
2020-06-23 03:32:45 +00:00
|
|
|
preloadProcess._linkedBinding = process._linkedBinding;
|
2020-05-15 18:57:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
const contextIsolation = getWebPreference(window, 'contextIsolation');
|
2020-11-23 19:39:08 +00:00
|
|
|
const webviewTag = getWebPreference(window, 'webviewTag');
|
2020-11-10 17:06:03 +00:00
|
|
|
const isHiddenPage = getWebPreference(window, 'hiddenPage');
|
|
|
|
const rendererProcessReuseEnabled = getWebPreference(window, 'disableElectronSiteInstanceOverrides');
|
2020-03-20 20:28:31 +00:00
|
|
|
const usesNativeWindowOpen = true;
|
2020-11-23 19:39:08 +00:00
|
|
|
const guestInstanceId = getWebPreference(window, 'guestInstanceId') || null;
|
|
|
|
const openerId = getWebPreference(window, 'openerId') || null;
|
2020-01-06 21:23:03 +00:00
|
|
|
|
2018-11-21 17:56:58 +00:00
|
|
|
switch (window.location.protocol) {
|
2019-05-17 22:37:09 +00:00
|
|
|
case 'devtools:': {
|
2018-11-21 17:56:58 +00:00
|
|
|
// Override some inspector APIs.
|
2020-03-20 20:28:31 +00:00
|
|
|
require('@electron/internal/renderer/inspector');
|
|
|
|
break;
|
2018-11-21 17:56:58 +00:00
|
|
|
}
|
|
|
|
case 'chrome-extension:': {
|
2020-03-20 20:28:31 +00:00
|
|
|
break;
|
2018-11-21 17:56:58 +00:00
|
|
|
}
|
2020-11-23 19:39:08 +00:00
|
|
|
case 'chrome:': {
|
2020-03-20 20:28:31 +00:00
|
|
|
break;
|
2019-03-08 00:00:28 +00:00
|
|
|
}
|
|
|
|
default: {
|
2020-01-06 21:23:03 +00:00
|
|
|
// Override default web functions.
|
2020-03-20 20:28:31 +00:00
|
|
|
const { windowSetup } = require('@electron/internal/renderer/window-setup');
|
|
|
|
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen, rendererProcessReuseEnabled);
|
2019-03-08 00:00:28 +00:00
|
|
|
}
|
2018-03-22 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Load webview tag implementation.
|
2019-01-24 18:53:16 +00:00
|
|
|
if (process.isMainFrame) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init');
|
2020-11-23 19:39:08 +00:00
|
|
|
webViewInit(contextIsolation, webviewTag, guestInstanceId);
|
2019-01-24 18:53:16 +00:00
|
|
|
}
|
2018-07-02 16:06:26 +00:00
|
|
|
|
2017-02-27 17:14:36 +00:00
|
|
|
// 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:
|
|
|
|
//
|
|
|
|
// - `require`: The `preloadRequire` function
|
|
|
|
// - `process`: The `preloadProcess` object
|
2019-06-02 20:03:03 +00:00
|
|
|
// - `Buffer`: Shim of `Buffer` implementation
|
|
|
|
// - `global`: The window object, which is aliased to `global` by webpack.
|
2020-05-30 09:56:54 +00:00
|
|
|
function runPreloadScript (preloadSrc: string) {
|
2019-03-11 23:13:46 +00:00
|
|
|
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports) {
|
2018-03-22 07:15:57 +00:00
|
|
|
${preloadSrc}
|
2020-03-20 20:28:31 +00:00
|
|
|
})`;
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2018-07-13 02:17:11 +00:00
|
|
|
// eval in window scope
|
2020-03-20 20:28:31 +00:00
|
|
|
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
|
|
|
|
const { setImmediate, clearImmediate } = require('timers');
|
2019-01-18 11:03:43 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, {});
|
2019-01-18 11:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 01:16:46 +00:00
|
|
|
for (const { preloadPath, preloadSrc, preloadError } of preloadScripts) {
|
|
|
|
try {
|
|
|
|
if (preloadSrc) {
|
2020-03-20 20:28:31 +00:00
|
|
|
runPreloadScript(preloadSrc);
|
2019-01-29 01:16:46 +00:00
|
|
|
} else if (preloadError) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw preloadError;
|
2019-01-29 01:16:46 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`Unable to load preload script: ${preloadPath}`);
|
|
|
|
console.error(error);
|
2019-01-29 01:16:46 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadPath, error);
|
2019-01-18 11:03:43 +00:00
|
|
|
}
|
2018-03-22 07:15:57 +00:00
|
|
|
}
|
2018-10-03 19:36:12 +00:00
|
|
|
|
|
|
|
// Warn about security issues
|
2019-01-24 18:53:16 +00:00
|
|
|
if (process.isMainFrame) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { securityWarnings } = require('@electron/internal/renderer/security-warnings');
|
|
|
|
securityWarnings();
|
2019-01-24 18:53:16 +00:00
|
|
|
}
|