Skip to content

Commit

Permalink
feat: emit process 'loaded' event in sandboxed renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Apr 3, 2019
1 parent 59e3164 commit e1b074a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions atom/renderer/atom_sandboxed_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions lib/sandboxed_renderer/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
},
Expand Down Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions spec/api-browser-window-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,6 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
sandbox: true,
preload
}
Expand All @@ -1586,20 +1585,32 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
sandbox: true,
preload: preloadSpecialChars
}
})
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')
w.webContents.openDevTools()
})

it('exposes "exit" event to preload script', function (done) {
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
sandbox: true,
preload
}
Expand All @@ -1622,7 +1633,6 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
sandbox: true,
preload
}
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/module/preload-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
}
}

process.once('loaded', () => {
ipcRenderer.send('process-loaded')
})

if (location.protocol === 'file:') {
window.test = 'preload'
window.process = process
Expand Down

0 comments on commit e1b074a

Please sign in to comment.