refactor: process event emitting for sandboxed renderers (#37109)

Co-authored-by: Milan Burda <miburda@microsoft.com>
This commit is contained in:
Milan Burda 2023-02-03 11:59:57 +01:00 committed by GitHub
parent 6e0d63c356
commit 446c7809cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 65 deletions

View file

@ -41,25 +41,15 @@ const loadableModules = new Map<string, Function>([
['url', () => require('url')]
]);
// ElectronSandboxedRendererClient will look for the "lifecycle" hidden object when
v8Util.setHiddenValue(global, 'lifecycle', {
onLoaded () {
(process as events.EventEmitter).emit('loaded');
},
onExit () {
(process as events.EventEmitter).emit('exit');
},
onDocumentStart () {
(process as events.EventEmitter).emit('document-start');
},
onDocumentEnd () {
(process as events.EventEmitter).emit('document-end');
}
});
// Pass different process object to the preload script.
const preloadProcess: NodeJS.Process = new EventEmitter() as any;
// 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);
});
Object.assign(preloadProcess, binding.process);
Object.assign(preloadProcess, processProps);
@ -79,11 +69,6 @@ Object.defineProperty(preloadProcess, 'noDeprecation', {
}
});
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'));
// This is the `require` function that will be visible to the preload script
function preloadRequire (module: string) {
if (loadedModules.has(module)) {