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

Keep track of remote object reference count. #9389

Merged
merged 2 commits into from May 17, 2017
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
5 changes: 5 additions & 0 deletions lib/browser/rpc-server.js
Expand Up @@ -394,6 +394,11 @@ ipcMain.on('ELECTRON_BROWSER_DEREFERENCE', function (event, id) {
objectsRegistry.remove(event.sender.getId(), id)
})

ipcMain.on('ELECTRON_BROWSER_CONTEXT_RELEASE', (e) => {
objectsRegistry.clear(e.sender.getId())
e.returnValue = null
})

ipcMain.on('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, guestInstanceId) {
try {
let guestViewManager = require('./guest-view-manager')
Expand Down
4 changes: 4 additions & 0 deletions lib/renderer/api/remote.js
Expand Up @@ -304,6 +304,10 @@ ipcRenderer.on('ELECTRON_RENDERER_RELEASE_CALLBACK', function (event, id) {
callbacksRegistry.remove(id)
})

process.on('exit', () => {
ipcRenderer.sendSync('ELECTRON_BROWSER_CONTEXT_RELEASE')
})

// Get remote module.
exports.require = function (module) {
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_REQUIRE', module))
Expand Down
68 changes: 67 additions & 1 deletion spec/api-browser-window-spec.js
Expand Up @@ -1161,7 +1161,6 @@ describe('BrowserWindow module', function () {
}
})
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?allocate-memory'))
w.webContents.openDevTools({mode: 'detach'})
ipcMain.once('answer', function (event, {bytesBeforeOpen, bytesAfterOpen, bytesAfterClose}) {
const memoryIncreaseByOpen = bytesAfterOpen - bytesBeforeOpen
const memoryDecreaseByClose = bytesAfterOpen - bytesAfterClose
Expand All @@ -1173,6 +1172,73 @@ describe('BrowserWindow module', function () {
done()
})
})

// see #9387
it('properly manages remote object references after page reload', (done) => {
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
sandbox: true
}
})
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?reload-remote'))

ipcMain.on('get-remote-module-path', (event) => {
event.returnValue = path.join(fixtures, 'module', 'hello.js')
})

let reload = false
ipcMain.on('reloaded', (event) => {
event.returnValue = reload
reload = !reload
})

ipcMain.once('reload', (event) => {
event.sender.reload()
})

ipcMain.once('answer', (event, arg) => {
ipcMain.removeAllListeners('reloaded')
ipcMain.removeAllListeners('get-remote-module-path')
assert.equal(arg, 'hi')
done()
})
})

it('properly manages remote object references after page reload in child window', (done) => {
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
sandbox: true
}
})
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?reload-remote-child'))

ipcMain.on('get-remote-module-path', (event) => {
event.returnValue = path.join(fixtures, 'module', 'hello-child.js')
})

let reload = false
ipcMain.on('reloaded', (event) => {
event.returnValue = reload
reload = !reload
})

ipcMain.once('reload', (event) => {
event.sender.reload()
})

ipcMain.once('answer', (event, arg) => {
ipcMain.removeAllListeners('reloaded')
ipcMain.removeAllListeners('get-remote-module-path')
assert.equal(arg, 'hi child window')
done()
})
})
})

describe('nativeWindowOpen option', () => {
Expand Down
20 changes: 17 additions & 3 deletions spec/fixtures/api/sandbox.html
Expand Up @@ -13,13 +13,28 @@
await timeout(100)
}
}
if (window.opener) {

const [,test] = window.location.href.split('?')
if (window.opener && test !== 'reload-remote') {
window.callback = () => {
opener.require('electron').ipcRenderer.send('answer', document.body.innerHTML)
}
} else {
const {ipcRenderer} = require('electron')
const {ipcRenderer, remote} = require('electron')
const tests = {
'reload-remote-child': () => {
open(`${location.protocol}//${location.pathname}?reload-remote`)
},
'reload-remote': async () => {
const p = ipcRenderer.sendSync('get-remote-module-path')
const Hello = remote.require(p)
if (!ipcRenderer.sendSync('reloaded')) {
ipcRenderer.send('reload')
return
}
await invokeGc()
ipcRenderer.send('answer', new Hello().say())
},
'allocate-memory': async () => {
await invokeGc()
const {privateBytes: bytesBeforeOpen} = process.getProcessMemoryInfo()
Expand Down Expand Up @@ -95,7 +110,6 @@
popup.close()
}, false)

let [,test] = window.location.href.split('?')
if (tests.hasOwnProperty(test))
tests[test]()
}
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/module/hello-child.js
@@ -0,0 +1,6 @@
class Hello {
say () {
return 'hi child window'
}
}
module.exports = Hello
6 changes: 6 additions & 0 deletions spec/fixtures/module/hello.js
@@ -0,0 +1,6 @@
class Hello {
say () {
return 'hi'
}
}
module.exports = Hello
2 changes: 1 addition & 1 deletion spec/fixtures/module/preload-sandbox.js
Expand Up @@ -3,9 +3,9 @@
const {ipcRenderer} = require('electron')
window.ipcRenderer = ipcRenderer
window.setImmediate = setImmediate
window.require = require
if (location.protocol === 'file:') {
window.test = 'preload'
window.require = require
window.process = process
} else if (location.href !== 'about:blank') {
addEventListener('DOMContentLoaded', () => {
Expand Down