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: send ELECTRON_BROWSER_CONTEXT_RELEASE asynchronously #20716

Merged
merged 1 commit into from
Oct 29, 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 lib/browser/objects-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class ObjectsRegistry {
this.clear(webContents, contextId)
}
}
// Note that the "render-view-deleted" event may not be emitted on time when
// the renderer process get destroyed because of navigation, we rely on the
// renderer process to send "ELECTRON_BROWSER_CONTEXT_RELEASE" message to
// guard this situation.
webContents.on('render-view-deleted', listener)
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/browser/rpc-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ handleRemoteCommand('ELECTRON_BROWSER_DEREFERENCE', function (event, contextId,

handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
objectsRegistry.clear(event.sender, contextId)
return null
})

handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
Expand Down
2 changes: 1 addition & 1 deletion lib/renderer/api/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const contextId = v8Util.getHiddenValue(global, 'contextId')
// to guard that situation.
process.on('exit', () => {
const command = 'ELECTRON_BROWSER_CONTEXT_RELEASE'
ipcRendererInternal.sendSync(command, contextId)
ipcRendererInternal.send(command, contextId)
})

// Convert the arguments object into an array of meta data.
Expand Down
28 changes: 28 additions & 0 deletions spec/api-remote-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,32 @@ describe('remote module', () => {
w.loadURL('about:blank')
})
})

describe('remote references', () => {
it('render-view-deleted is sent when page is destroyed', (done) => {
w = new BrowserWindow({ show: false })
w.webContents.once('render-view-deleted', () => {
done()
})
w.destroy()
miniak marked this conversation as resolved.
Show resolved Hide resolved
})

// The ELECTRON_BROWSER_CONTEXT_RELEASE message relies on this to work.
it('message can be sent on exit when page is being navigated', (done) => {
after(() => { ipcMain.removeAllListeners('SENT_ON_EXIT') })
ipcMain.once('SENT_ON_EXIT', () => {
done()
})
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.webContents.once('did-finish-load', () => {
w.webContents.loadURL('about:blank')
})
w.loadFile(path.join(fixtures, 'api', 'send-on-exit.html'))
})
})
})
11 changes: 11 additions & 0 deletions spec/fixtures/api/send-on-exit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
const {ipcRenderer} = require('electron')

process.on('exit', () => {
ipcRenderer.send('SENT_ON_EXIT')
})
</script>
</body>
</html>