Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: emit process 'loaded' event in sandboxed renderers #17680

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
17 changes: 13 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,31 @@ 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')
})

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 +1632,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