Check that sender is a WebContents instance

This commit is contained in:
Kevin Sawicki 2017-01-24 15:05:34 -08:00
parent ddedcf22d1
commit 56a8eb3a94
3 changed files with 18 additions and 12 deletions

View file

@ -3,6 +3,8 @@
const {Buffer} = require('buffer')
const electron = require('electron')
const v8Util = process.atomBinding('v8_util')
const {WebContents} = process.atomBinding('web_contents')
const {ipcMain, isPromise, webContents} = electron
const fs = require('fs')
@ -150,8 +152,8 @@ const removeRemoteListenersAndLogWarning = (meta, args, callIntoRenderer) => {
let message = `Attempting to call a function in a renderer window that has been closed or released.` +
`\nFunction provided here: ${meta.location}`
if (args.length > 0 && args[0].sender && args[0].sender.eventNames) {
const sender = args[0].sender
if (args.length > 0 && (args[0].sender instanceof WebContents)) {
const {sender} = args[0]
const remoteEvents = sender.eventNames().filter((eventName) => {
return sender.listeners(eventName).includes(callIntoRenderer)
})

View file

@ -506,10 +506,12 @@ describe('ipc module', function () {
'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler'
].join('\n')
const {warningMessage, listenerCountBefore, listenerCountAfter} =
ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.equal(warningMessage, expectedMessage)
assert.equal(listenerCountAfter, listenerCountBefore - 1)
const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.deepEqual(results, {
warningMessage: expectedMessage,
listenerCountBefore: 2,
listenerCountAfter: 1
})
done()
})
w.webContents.reload()

View file

@ -253,13 +253,16 @@ ipcMain.on('prevent-next-new-window', (event, id) => {
ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => {
const consoleWarn = console.warn
let warningMessage = null
console.warn = (message) => {
warningMessage = message
}
const contents = webContents.fromId(id)
const listenerCountBefore = contents.listenerCount(eventName)
contents.emit(eventName, {sender: contents})
try {
console.warn = (message) => warningMessage = message
contents.emit(eventName, {sender: contents})
} finally {
console.warn = consoleWarn
}
const listenerCountAfter = contents.listenerCount(eventName)
event.returnValue = {
@ -267,5 +270,4 @@ ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => {
listenerCountBefore,
listenerCountAfter
}
console.warn = consoleWarn
})