fix: send ELECTRON_BROWSER_CONTEXT_RELEASE asynchronously (#20632)

* fix: send ELECTRON_BROWSER_CONTEXT_RELEASE asynchronously

* test: remote references should be able to be cleared for all cases
This commit is contained in:
Milan Burda 2019-10-23 06:44:21 +02:00 коммит произвёл Cheng Zhao
Родитель 2abea22b4b
Коммит ba8f80267c
5 изменённых файлов: 43 добавлений и 5 удалений

Просмотреть файл

@ -127,6 +127,10 @@ class ObjectsRegistry {
this.clear(webContents, contextId) 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' as any, listener) webContents.on('render-view-deleted' as any, listener)
} }
} }

Просмотреть файл

@ -541,7 +541,6 @@ handleRemoteCommand('ELECTRON_BROWSER_DEREFERENCE', function (event, contextId,
handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => { handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
objectsRegistry.clear(event.sender, contextId) objectsRegistry.clear(event.sender, contextId)
return null
}) })
handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId, stack) { handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId, stack) {

Просмотреть файл

@ -19,7 +19,7 @@ const contextId = v8Util.getHiddenValue(global, 'contextId')
// to guard that situation. // to guard that situation.
process.on('exit', () => { process.on('exit', () => {
const command = 'ELECTRON_BROWSER_CONTEXT_RELEASE' const command = 'ELECTRON_BROWSER_CONTEXT_RELEASE'
ipcRendererInternal.sendSync(command, contextId) ipcRendererInternal.send(command, contextId)
}) })
// Convert the arguments object into an array of meta data. // Convert the arguments object into an array of meta data.

Просмотреть файл

@ -1,18 +1,21 @@
import * as path from 'path'
import { expect } from 'chai' import { expect } from 'chai'
import { closeWindow } from './window-helpers' import { closeWindow } from './window-helpers'
import { ifdescribe } from './spec-helpers'; import { ifdescribe } from './spec-helpers';
import { BrowserWindow } from 'electron' import { ipcMain, BrowserWindow } from 'electron'
const features = process.electronBinding('features') const features = process.electronBinding('features')
ifdescribe(features.isRemoteModuleEnabled())('remote module', () => { ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
const fixtures = path.join(__dirname, 'fixtures')
let w = null as unknown as BrowserWindow let w = null as unknown as BrowserWindow
before(async () => { beforeEach(async () => {
w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}}) w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}})
await w.loadURL('about:blank') await w.loadURL('about:blank')
}) })
after(async () => { afterEach(async () => {
await closeWindow(w) await closeWindow(w)
}) })
@ -112,4 +115,25 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
await expect(remotely(`require('electron').remote.getCurrentWebContents()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWebContents()`) await expect(remotely(`require('electron').remote.getCurrentWebContents()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWebContents()`)
}) })
}) })
describe('remote references', () => {
it('render-view-deleted is sent when page is destroyed', (done) => {
w.webContents.once('render-view-deleted' as any, () => {
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'))
})
})
}) })

Просмотреть файл

@ -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>