2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2021-05-06 01:05:01 +00:00
|
|
|
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
|
2024-10-03 02:10:44 +00:00
|
|
|
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
|
|
|
|
|
|
|
import * as events from 'events';
|
|
|
|
import { setImmediate, clearImmediate } from 'timers';
|
2021-05-06 01:05:01 +00:00
|
|
|
|
2023-05-24 18:01:07 +00:00
|
|
|
declare const binding: {
|
|
|
|
get: (name: string) => any;
|
|
|
|
process: NodeJS.Process;
|
|
|
|
createPreloadScript: (src: string) => Function
|
|
|
|
};
|
|
|
|
|
2018-09-30 21:24:00 +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.
|
|
|
|
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)[]) {
|
2023-06-27 20:57:33 +00:00
|
|
|
if (Object.hasOwn(process, prop)) {
|
2017-02-27 17:14:36 +00:00
|
|
|
delete process[prop];
|
|
|
|
}
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
2018-09-30 21:24:00 +00:00
|
|
|
Object.setPrototypeOf(process, EventEmitter.prototype);
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2021-05-06 01:05:01 +00:00
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
|
|
|
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;
|
2018-12-05 18:07:56 +00:00
|
|
|
|
2023-06-14 18:06:46 +00:00
|
|
|
const {
|
|
|
|
preloadScripts,
|
|
|
|
process: processProps
|
|
|
|
} = ipcRendererUtils.invokeSync<{
|
|
|
|
preloadScripts: {
|
|
|
|
preloadPath: string;
|
|
|
|
preloadSrc: string | null;
|
|
|
|
preloadError: null | Error;
|
|
|
|
}[];
|
|
|
|
process: NodeJS.Process;
|
|
|
|
}>(IPC_MESSAGES.BROWSER_SANDBOX_LOAD);
|
2018-12-05 18:07:56 +00:00
|
|
|
|
|
|
|
const electron = require('electron');
|
|
|
|
|
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],
|
2023-06-10 18:36:16 +00:00
|
|
|
['events', events],
|
|
|
|
['node:events', events]
|
2020-06-02 09:32:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
const loadableModules = new Map<string, Function>([
|
|
|
|
['timers', () => require('timers')],
|
2023-06-10 18:36:16 +00:00
|
|
|
['node:timers', () => require('timers')],
|
|
|
|
['url', () => require('url')],
|
|
|
|
['node:url', () => require('url')]
|
2016-08-21 10:30:14 +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
|
|
|
|
2023-02-03 10:59:57 +00:00
|
|
|
// InvokeEmitProcessEvent in ElectronSandboxedRendererClient will look for this
|
|
|
|
v8Util.setHiddenValue(global, 'emit-process-event', (event: string) => {
|
|
|
|
(process as events.EventEmitter).emit(event);
|
|
|
|
(preloadProcess as events.EventEmitter).emit(event);
|
|
|
|
});
|
|
|
|
|
2018-09-30 21:24:00 +00:00
|
|
|
Object.assign(preloadProcess, binding.process);
|
2018-08-21 18:05:45 +00:00
|
|
|
Object.assign(preloadProcess, processProps);
|
2018-09-30 21:24:00 +00:00
|
|
|
|
|
|
|
Object.assign(process, binding.process);
|
2018-08-21 18:05:45 +00:00
|
|
|
Object.assign(process, processProps);
|
2018-05-21 12:56:05 +00:00
|
|
|
|
2021-10-05 22:30:31 +00:00
|
|
|
process.getProcessMemoryInfo = preloadProcess.getProcessMemoryInfo = () => {
|
|
|
|
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
|
|
|
|
};
|
|
|
|
|
2018-12-05 09:34:09 +00:00
|
|
|
Object.defineProperty(preloadProcess, 'noDeprecation', {
|
|
|
|
get () {
|
|
|
|
return process.noDeprecation;
|
|
|
|
},
|
|
|
|
set (value) {
|
|
|
|
process.noDeprecation = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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)) {
|
|
|
|
return loadedModules.get(module);
|
|
|
|
}
|
2020-06-02 09:32:32 +00:00
|
|
|
if (loadableModules.has(module)) {
|
|
|
|
const loadedModule = loadableModules.get(module)!();
|
|
|
|
loadedModules.set(module, loadedModule);
|
|
|
|
return loadedModule;
|
|
|
|
}
|
2019-03-19 19:00:41 +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');
|
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
|
|
|
}
|
|
|
|
|
2021-11-10 00:59:33 +00:00
|
|
|
// Common renderer initialization
|
|
|
|
require('@electron/internal/renderer/common-init');
|
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) {
|
2023-08-22 04:43:08 +00:00
|
|
|
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports, module) {
|
2018-03-22 07:15:57 +00:00
|
|
|
${preloadSrc}
|
|
|
|
})`;
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2018-07-13 02:17:11 +00:00
|
|
|
// eval in window scope
|
|
|
|
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
|
2023-08-22 04:43:08 +00:00
|
|
|
const exports = {};
|
2019-01-18 11:03:43 +00:00
|
|
|
|
2023-08-22 04:43:08 +00:00
|
|
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, exports, { exports });
|
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) {
|
|
|
|
runPreloadScript(preloadSrc);
|
|
|
|
} else if (preloadError) {
|
2019-10-10 13:59:08 +00:00
|
|
|
throw preloadError;
|
2019-01-29 01:16:46 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Unable to load preload script: ${preloadPath}`);
|
2019-07-15 04:54:33 +00:00
|
|
|
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
|
|
|
}
|