From 65028bb42f3fc89569048aa5876805a57b6b2efd Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Fri, 18 Oct 2019 14:53:13 +0200 Subject: [PATCH 1/2] fix: send ELECTRON_BROWSER_CONTEXT_RELEASE asynchronously --- lib/browser/remote/server.ts | 1 - lib/renderer/api/remote.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/browser/remote/server.ts b/lib/browser/remote/server.ts index 970dbf983da9b..4453e5561674c 100644 --- a/lib/browser/remote/server.ts +++ b/lib/browser/remote/server.ts @@ -529,7 +529,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, stack) { diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 07c224b702113..fca80978f6af2 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -19,7 +19,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. From c71763de80917bf9bfa8cddbb85515466bcaa4e6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 19 Oct 2019 10:14:22 +0900 Subject: [PATCH 2/2] test: remote references should be able to be cleared for all cases --- lib/browser/remote/objects-registry.ts | 4 ++++ spec-main/api-remote-spec.ts | 30 +++++++++++++++++++++--- spec-main/fixtures/api/send-on-exit.html | 11 +++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 spec-main/fixtures/api/send-on-exit.html diff --git a/lib/browser/remote/objects-registry.ts b/lib/browser/remote/objects-registry.ts index 97fa39c5eede8..0de48c3960d6f 100644 --- a/lib/browser/remote/objects-registry.ts +++ b/lib/browser/remote/objects-registry.ts @@ -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' as any, listener) } } diff --git a/spec-main/api-remote-spec.ts b/spec-main/api-remote-spec.ts index 130576811c551..13d8e3b9ce0c9 100644 --- a/spec-main/api-remote-spec.ts +++ b/spec-main/api-remote-spec.ts @@ -1,18 +1,21 @@ +import * as path from 'path' import { expect } from 'chai' import { closeWindow } from './window-helpers' import { ifdescribe } from './spec-helpers'; -import { BrowserWindow } from 'electron' +import { ipcMain, BrowserWindow } from 'electron' const features = process.electronBinding('features') ifdescribe(features.isRemoteModuleEnabled())('remote module', () => { + const fixtures = path.join(__dirname, 'fixtures') + let w = null as unknown as BrowserWindow - before(async () => { + beforeEach(async () => { w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}}) await w.loadURL('about:blank') }) - after(async () => { + afterEach(async () => { 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()`) }) }) + + 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')) + }) + }) }) diff --git a/spec-main/fixtures/api/send-on-exit.html b/spec-main/fixtures/api/send-on-exit.html new file mode 100644 index 0000000000000..8be9b4b06a778 --- /dev/null +++ b/spec-main/fixtures/api/send-on-exit.html @@ -0,0 +1,11 @@ + + + + +