From 77d59e99b6d366cffb29b75ca5d4f9a2b02cf02b Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 4 Apr 2019 20:35:06 +0200 Subject: [PATCH] feat: emit process 'loaded' event in sandboxed renderers (#17680) --- atom/renderer/atom_sandboxed_renderer_client.cc | 4 ++++ lib/sandboxed_renderer/init.js | 4 ++++ spec/api-browser-window-spec.js | 17 +++++++++++++---- spec/fixtures/module/preload-sandbox.js | 4 ++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/atom/renderer/atom_sandboxed_renderer_client.cc b/atom/renderer/atom_sandboxed_renderer_client.cc index 20bd915aea8f..d87beab4bbc0 100644 --- a/atom/renderer/atom_sandboxed_renderer_client.cc +++ b/atom/renderer/atom_sandboxed_renderer_client.cc @@ -226,6 +226,10 @@ void AtomSandboxedRendererClient::DidCreateScriptContext( node::per_process::native_module_loader.CompileAndCall( isolate->GetCurrentContext(), "electron/js2c/preload_bundle", &preload_bundle_params, &preload_bundle_args, nullptr); + + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + InvokeHiddenCallback(context, kLifecycleKey, "onLoaded"); } void AtomSandboxedRendererClient::SetupMainWorldOverrides( diff --git a/lib/sandboxed_renderer/init.js b/lib/sandboxed_renderer/init.js index 62c50a385853..a770027ab27b 100644 --- a/lib/sandboxed_renderer/init.js +++ b/lib/sandboxed_renderer/init.js @@ -56,6 +56,9 @@ v8Util.setHiddenValue(global, 'ipcNative', { // AtomSandboxedRendererClient will look for the "lifecycle" hidden object when v8Util.setHiddenValue(global, 'lifecycle', { + onLoaded () { + process.emit('loaded') + }, onExit () { process.emit('exit') }, @@ -89,6 +92,7 @@ Object.defineProperty(preloadProcess, 'noDeprecation', { } }) +process.on('loaded', () => preloadProcess.emit('loaded')) process.on('exit', () => preloadProcess.emit('exit')) // This is the `require` function that will be visible to the preload script diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 27497e471ceb..6d32e831c3a3 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1568,7 +1568,6 @@ describe('BrowserWindow module', () => { w = new BrowserWindow({ show: false, webPreferences: { - nodeIntegration: true, sandbox: true, preload } @@ -1586,7 +1585,6 @@ describe('BrowserWindow module', () => { w = new BrowserWindow({ show: false, webPreferences: { - nodeIntegration: true, sandbox: true, preload: preloadSpecialChars } @@ -1594,12 +1592,24 @@ describe('BrowserWindow module', () => { w.loadFile(path.join(fixtures, 'api', 'preload.html')) }) + it('exposes "loaded" event to preload script', function (done) { + w.destroy() + w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true, + preload + } + }) + ipcMain.once('process-loaded', () => done()) + w.loadURL('about:blank') + }) + it('exposes "exit" event to preload script', function (done) { w.destroy() w = new BrowserWindow({ show: false, webPreferences: { - nodeIntegration: true, sandbox: true, preload } @@ -1622,7 +1632,6 @@ describe('BrowserWindow module', () => { w = new BrowserWindow({ show: false, webPreferences: { - nodeIntegration: true, sandbox: true, preload } diff --git a/spec/fixtures/module/preload-sandbox.js b/spec/fixtures/module/preload-sandbox.js index ae73d25bab67..02da90f8c560 100644 --- a/spec/fixtures/module/preload-sandbox.js +++ b/spec/fixtures/module/preload-sandbox.js @@ -13,6 +13,10 @@ } } + process.once('loaded', () => { + ipcRenderer.send('process-loaded') + }) + if (location.protocol === 'file:') { window.test = 'preload' window.process = process