Skip to content

Commit

Permalink
fix: send ELECTRON_BROWSER_CONTEXT_RELEASE asynchronously (#20632)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Oct 23, 2019
1 parent 9901700 commit 0bff913
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
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
21 changes: 21 additions & 0 deletions spec/api-remote-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,25 @@ describe('remote module', () => {
w.loadURL('about:blank')
})
})

describe('remote references', () => {
it('render-view-deleted is sent when page is destroyed', (done) => {
w.webContents.once('render-view-deleted', () => {
done()
})
w.destroy()
})

// 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.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>

0 comments on commit 0bff913

Please sign in to comment.