2020-03-20 20:28:31 +00:00
|
|
|
import * as path from 'path';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2016-01-13 03:55:49 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const Module = require('module');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-04-26 09:55:12 +00:00
|
|
|
// Make sure globals like "process" and "global" are always available in preload
|
|
|
|
// scripts even after they are deleted in "loaded" script.
|
|
|
|
//
|
|
|
|
// Note 1: We rely on a Node patch to actually pass "process" and "global" and
|
|
|
|
// other arguments to the wrapper.
|
|
|
|
//
|
|
|
|
// Note 2: Node introduced a new code path to use native code to wrap module
|
|
|
|
// code, which does not work with this hack. However by modifying the
|
|
|
|
// "Module.wrapper" we can force Node to use the old code path to wrap module
|
|
|
|
// code with JavaScript.
|
2019-06-02 20:03:03 +00:00
|
|
|
//
|
2019-06-20 15:11:38 +00:00
|
|
|
// Note 3: We provide the equivalent extra variables internally through the
|
2019-06-02 20:03:03 +00:00
|
|
|
// webpack ProvidePlugin in webpack.config.base.js. If you add any extra
|
|
|
|
// variables to this wrapper please ensure to update that plugin as well.
|
2019-04-26 09:55:12 +00:00
|
|
|
Module.wrapper = [
|
|
|
|
'(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
|
|
|
|
// By running the code in a new closure, it would be possible for the module
|
|
|
|
// code to override "process" and "Buffer" with local variables.
|
|
|
|
'return function (exports, require, module, __filename, __dirname) { ',
|
|
|
|
'\n}.call(this, exports, require, module, __filename, __dirname); });'
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2019-04-26 09:55:12 +00:00
|
|
|
|
2016-01-14 19:10:12 +00:00
|
|
|
// We modified the original process.argv to let node.js load the
|
2017-03-08 09:58:54 +00:00
|
|
|
// init.js, we need to restore it here.
|
2020-03-20 20:28:31 +00:00
|
|
|
process.argv.splice(1, 1);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Clear search paths.
|
2019-02-15 01:24:25 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
require('../common/reset-search-paths');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Import common settings.
|
2020-03-20 20:28:31 +00:00
|
|
|
require('@electron/internal/common/init');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// The global variable will be used by ipc for event dispatching
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-06-02 09:33:06 +00:00
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
|
|
|
|
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;
|
2019-04-02 22:38:16 +00:00
|
|
|
|
|
|
|
v8Util.setHiddenValue(global, 'ipcNative', {
|
2020-03-12 01:07:54 +00:00
|
|
|
onMessage (internal: boolean, channel: string, ports: any[], args: any[], senderId: number) {
|
2020-06-02 09:33:06 +00:00
|
|
|
const sender = internal ? ipcRendererInternal : ipcRenderer;
|
2020-03-20 20:28:31 +00:00
|
|
|
sender.emit(channel, { sender, senderId, ports }, ...args);
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-13 03:55:49 +00:00
|
|
|
// Use electron module after everything is ready.
|
2020-03-20 20:28:31 +00:00
|
|
|
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init');
|
|
|
|
webFrameInit();
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Process command line arguments.
|
2020-06-23 03:32:45 +00:00
|
|
|
const { hasSwitch, getSwitchValue } = 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-11-10 17:06:03 +00:00
|
|
|
const parseOption = function<TDefault> (
|
|
|
|
name: string, defaultValue: TDefault, converter?: (value: string) => any
|
2019-02-19 17:05:14 +00:00
|
|
|
) {
|
2020-11-10 17:06:03 +00:00
|
|
|
const value = getWebPreference(window, name);
|
|
|
|
return value
|
2019-02-19 17:05:14 +00:00
|
|
|
? (
|
|
|
|
converter
|
2020-11-10 17:06:03 +00:00
|
|
|
? converter(value)
|
|
|
|
: value
|
2019-02-19 17:05:14 +00:00
|
|
|
)
|
2020-03-20 20:28:31 +00:00
|
|
|
: defaultValue;
|
|
|
|
};
|
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
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');
|
2020-03-20 20:28:31 +00:00
|
|
|
|
|
|
|
const preloadScript = parseOption('preload', null);
|
2020-11-10 17:06:03 +00:00
|
|
|
const preloadScripts = parseOption('preloadScripts', []);
|
|
|
|
const guestInstanceId = parseOption('guestInstanceId', null, value => parseInt(value));
|
|
|
|
const openerId = parseOption('openerId', null, value => parseInt(value));
|
|
|
|
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
|
2018-12-12 21:31:16 +00:00
|
|
|
|
2017-12-05 06:59:15 +00:00
|
|
|
// The webContents preload script is loaded after the session preload scripts.
|
|
|
|
if (preloadScript) {
|
2020-03-20 20:28:31 +00:00
|
|
|
preloadScripts.push(preloadScript);
|
2017-12-05 06:59:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
switch (window.location.protocol) {
|
2019-05-17 22:37:09 +00:00
|
|
|
case 'devtools:': {
|
2019-01-22 01:08:16 +00:00
|
|
|
// Override some inspector APIs.
|
2020-03-20 20:28:31 +00:00
|
|
|
require('@electron/internal/renderer/inspector');
|
|
|
|
break;
|
2019-01-22 01:08:16 +00:00
|
|
|
}
|
|
|
|
case 'chrome-extension:': {
|
2020-03-20 20:28:31 +00:00
|
|
|
break;
|
2019-01-22 01:08:16 +00:00
|
|
|
}
|
2019-01-22 18:32:18 +00:00
|
|
|
case 'chrome:':
|
2020-03-20 20:28:31 +00:00
|
|
|
break;
|
2019-01-22 01:08:16 +00:00
|
|
|
default: {
|
|
|
|
// 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);
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Load webview tag implementation.
|
2019-01-22 19:24:46 +00:00
|
|
|
if (process.isMainFrame) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init');
|
|
|
|
webViewInit(contextIsolation, webviewTag, guestInstanceId);
|
2019-01-22 19:24:46 +00:00
|
|
|
}
|
2019-01-22 01:08:16 +00:00
|
|
|
|
2018-10-08 13:51:31 +00:00
|
|
|
if (nodeIntegration) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Export node bindings to global.
|
2019-06-04 00:03:59 +00:00
|
|
|
const { makeRequireFunction } = __non_webpack_require__('internal/modules/cjs/helpers') // eslint-disable-line
|
2020-03-20 20:28:31 +00:00
|
|
|
global.module = new Module('electron/js2c/renderer_init');
|
|
|
|
global.require = makeRequireFunction(global.module);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Set the __filename to the path of html file if it is file: protocol.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (window.location.protocol === 'file:') {
|
2020-03-20 20:28:31 +00:00
|
|
|
const location = window.location;
|
|
|
|
let pathname = location.pathname;
|
2018-03-15 02:45:13 +00:00
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (pathname[0] === '/') pathname = pathname.substr(1);
|
2018-03-15 02:45:13 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const isWindowsNetworkSharePath = location.hostname.length > 0 && process.resourcesPath.startsWith('\\');
|
2018-03-15 02:45:13 +00:00
|
|
|
if (isWindowsNetworkSharePath) {
|
2020-03-20 20:28:31 +00:00
|
|
|
pathname = `//${location.host}/${pathname}`;
|
2018-03-15 02:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
global.__filename = path.normalize(decodeURIComponent(pathname));
|
|
|
|
global.__dirname = path.dirname(global.__filename);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Set module's filename so relative require can work as expected.
|
2020-03-20 20:28:31 +00:00
|
|
|
global.module.filename = global.__filename;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Also search for module under the html file.
|
2020-03-20 20:28:31 +00:00
|
|
|
global.module.paths = Module._nodeModulePaths(global.__dirname);
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2019-06-04 00:03:59 +00:00
|
|
|
// For backwards compatibility we fake these two paths here
|
2020-03-20 20:28:31 +00:00
|
|
|
global.__filename = path.join(process.resourcesPath, 'electron.asar', 'renderer', 'init.js');
|
|
|
|
global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'renderer');
|
2017-04-03 11:12:02 +00:00
|
|
|
|
2017-04-04 00:36:01 +00:00
|
|
|
if (appPath) {
|
2017-04-03 13:11:29 +00:00
|
|
|
// Search for module under the app directory
|
2020-03-20 20:28:31 +00:00
|
|
|
global.module.paths = Module._nodeModulePaths(appPath);
|
2017-04-03 13:11:29 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Redirect window.onerror to uncaughtException.
|
2019-02-19 17:05:14 +00:00
|
|
|
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
2019-05-01 13:07:57 +00:00
|
|
|
if (global.process.listenerCount('uncaughtException') > 0) {
|
2019-02-19 17:05:14 +00:00
|
|
|
// We do not want to add `uncaughtException` to our definitions
|
|
|
|
// because we don't want anyone else (anywhere) to throw that kind
|
|
|
|
// of error.
|
2020-03-20 20:28:31 +00:00
|
|
|
global.process.emit('uncaughtException' as any, error as any);
|
|
|
|
return true;
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
return false;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2019-06-28 21:37:00 +00:00
|
|
|
// Delete Node's symbols after the Environment has been loaded in a
|
|
|
|
// non context-isolated environment
|
|
|
|
if (!contextIsolation) {
|
|
|
|
process.once('loaded', function () {
|
2020-08-24 18:23:25 +00:00
|
|
|
delete (global as any).process;
|
|
|
|
delete (global as any).Buffer;
|
|
|
|
delete (global as any).setImmediate;
|
|
|
|
delete (global as any).clearImmediate;
|
|
|
|
delete (global as any).global;
|
|
|
|
delete (global as any).root;
|
|
|
|
delete (global as any).GLOBAL;
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-28 21:37:00 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 06:59:15 +00:00
|
|
|
// Load the preload scripts.
|
|
|
|
for (const preloadScript of preloadScripts) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
Module._load(preloadScript);
|
2016-01-14 22:20:06 +00:00
|
|
|
} catch (error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`Unable to load preload script: ${preloadScript}`);
|
|
|
|
console.error(error);
|
2019-01-18 11:03:43 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, preloadScript, error);
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-03 14:50:12 +00:00
|
|
|
|
|
|
|
// Warn about security issues
|
2019-01-22 19:24:46 +00:00
|
|
|
if (process.isMainFrame) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { securityWarnings } = require('@electron/internal/renderer/security-warnings');
|
|
|
|
securityWarnings(nodeIntegration);
|
2019-01-22 19:24:46 +00:00
|
|
|
}
|