2019-10-23 04:44:21 +00:00
|
|
|
import * as path from 'path'
|
2019-06-07 17:43:32 +00:00
|
|
|
import { expect } from 'chai'
|
2019-10-29 04:16:42 +00:00
|
|
|
import { closeWindow, closeAllWindows } from './window-helpers'
|
2019-11-01 20:37:02 +00:00
|
|
|
import { ifdescribe } from './spec-helpers'
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-23 04:44:21 +00:00
|
|
|
import { ipcMain, BrowserWindow } from 'electron'
|
2019-11-01 20:37:02 +00:00
|
|
|
import { emittedOnce } from './events-helpers'
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
const features = process.electronBinding('features')
|
|
|
|
|
|
|
|
ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
|
2019-10-23 04:44:21 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('', () => {
|
|
|
|
let w = null as unknown as BrowserWindow
|
|
|
|
beforeEach(async () => {
|
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
|
|
|
afterEach(async () => {
|
|
|
|
await closeWindow(w)
|
|
|
|
})
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
async function remotely (script: string) {
|
2019-10-29 04:16:42 +00:00
|
|
|
// executeJavaScript never returns if the script throws an error, so catch
|
|
|
|
// any errors manually.
|
|
|
|
const assembledScript = `(function() {
|
2019-06-07 17:43:32 +00:00
|
|
|
try {
|
|
|
|
return { result: ${script} }
|
|
|
|
} catch (e) {
|
|
|
|
return { error: e.message }
|
|
|
|
}
|
|
|
|
})()`
|
2019-10-29 04:16:42 +00:00
|
|
|
const { result, error } = await w.webContents.executeJavaScript(assembledScript)
|
|
|
|
if (error) {
|
|
|
|
throw new Error(error)
|
|
|
|
}
|
|
|
|
return result
|
2019-06-07 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getGlobal filtering', () => {
|
|
|
|
it('can return custom values', async () => {
|
|
|
|
w.webContents.once('remote-get-global', (event, name) => {
|
|
|
|
event.returnValue = name
|
|
|
|
})
|
|
|
|
expect(await remotely(`require('electron').remote.getGlobal('test')`)).to.equal('test')
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w.webContents.once('remote-get-global', (event) => {
|
2019-10-29 04:16:42 +00:00
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
await expect(remotely(`require('electron').remote.getGlobal('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getBuiltin filtering', () => {
|
|
|
|
it('can return custom values', async () => {
|
|
|
|
w.webContents.once('remote-get-builtin', (event, name) => {
|
|
|
|
event.returnValue = name
|
|
|
|
})
|
|
|
|
expect(await remotely(`require('electron').remote.getBuiltin('test')`)).to.equal('test')
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w.webContents.once('remote-get-builtin', (event) => {
|
2019-10-29 04:16:42 +00:00
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
await expect(remotely(`require('electron').remote.getBuiltin('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.require filtering', () => {
|
|
|
|
it('can return custom values', async () => {
|
|
|
|
w.webContents.once('remote-require', (event, name) => {
|
|
|
|
event.returnValue = name
|
|
|
|
})
|
|
|
|
expect(await remotely(`require('electron').remote.require('test')`)).to.equal('test')
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w.webContents.once('remote-require', (event) => {
|
2019-10-29 04:16:42 +00:00
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
await expect(remotely(`require('electron').remote.require('test')`)).to.eventually.be.rejected(`Blocked remote.require('test')`)
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getCurrentWindow filtering', () => {
|
|
|
|
it('can return custom value', async () => {
|
|
|
|
w.webContents.once('remote-get-current-window', (e) => {
|
|
|
|
e.returnValue = 'some window'
|
|
|
|
})
|
|
|
|
expect(await remotely(`require('electron').remote.getCurrentWindow()`)).to.equal('some window')
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
|
|
|
w.webContents.once('remote-get-current-window', (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
await expect(remotely(`require('electron').remote.getCurrentWindow()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWindow()`)
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getCurrentWebContents filtering', () => {
|
|
|
|
it('can return custom value', async () => {
|
|
|
|
w.webContents.once('remote-get-current-web-contents', (event) => {
|
|
|
|
event.returnValue = 'some web contents'
|
|
|
|
})
|
|
|
|
expect(await remotely(`require('electron').remote.getCurrentWebContents()`)).to.equal('some web contents')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when no returnValue set', async () => {
|
|
|
|
w.webContents.once('remote-get-current-web-contents', (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
await expect(remotely(`require('electron').remote.getCurrentWebContents()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWebContents()`)
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
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', async () => {
|
|
|
|
after(() => { ipcMain.removeAllListeners('SENT_ON_EXIT') })
|
|
|
|
await emittedOnce(w.webContents, 'did-finish-load')
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
w.webContents.loadURL('about:blank')
|
|
|
|
})
|
|
|
|
w.loadFile(path.join(fixtures, 'api', 'send-on-exit.html'))
|
|
|
|
await emittedOnce(ipcMain, 'SENT_ON_EXIT')
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-10-23 04:44:21 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote function in renderer', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
ipcMain.removeAllListeners('done')
|
|
|
|
})
|
|
|
|
afterEach(closeAllWindows)
|
|
|
|
|
|
|
|
it('works when created in preload script', async () => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'preload-remote-function.js')
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload
|
|
|
|
}
|
2019-10-23 04:44:21 +00:00
|
|
|
})
|
2019-10-29 04:16:42 +00:00
|
|
|
w.loadURL('about:blank')
|
|
|
|
await emittedOnce(ipcMain, 'done')
|
2019-10-23 04:44:21 +00:00
|
|
|
})
|
2019-10-29 04:16:42 +00:00
|
|
|
})
|
2019-10-23 04:44:21 +00:00
|
|
|
|
2019-11-14 22:09:03 +00:00
|
|
|
describe('remote objects registry', () => {
|
|
|
|
it('does not dereference until the render view is deleted (regression)', (done) => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.once('error-message', (event, message) => {
|
|
|
|
expect(message).to.match(/^Cannot call method 'getURL' on missing remote object/)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote listeners', () => {
|
|
|
|
afterEach(closeAllWindows)
|
|
|
|
|
|
|
|
it('detaches listeners subscribed to destroyed renderers, and shows a warning', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2019-10-23 04:44:21 +00:00
|
|
|
})
|
2019-11-14 22:09:03 +00:00
|
|
|
await w.loadFile(path.join(fixtures, 'api', 'remote-event-handler.html'))
|
2019-10-29 04:16:42 +00:00
|
|
|
w.webContents.reload()
|
|
|
|
await emittedOnce(w.webContents, 'did-finish-load')
|
|
|
|
|
|
|
|
const expectedMessage = [
|
|
|
|
'Attempting to call a function in a renderer window that has been closed or released.',
|
|
|
|
'Function provided here: remote-event-handler.html:11:33',
|
|
|
|
'Remote event names: remote-handler, other-remote-handler'
|
|
|
|
].join('\n')
|
|
|
|
|
|
|
|
expect(w.webContents.listenerCount('remote-handler')).to.equal(2)
|
|
|
|
let warnMessage: string | null = null
|
2019-11-01 20:37:02 +00:00
|
|
|
const originalWarn = console.warn
|
2019-10-29 04:16:42 +00:00
|
|
|
try {
|
2019-11-01 20:37:02 +00:00
|
|
|
console.warn = (message: string) => { warnMessage = message }
|
2019-10-29 04:16:42 +00:00
|
|
|
w.webContents.emit('remote-handler', { sender: w.webContents })
|
|
|
|
} finally {
|
|
|
|
console.warn = originalWarn
|
|
|
|
}
|
|
|
|
expect(w.webContents.listenerCount('remote-handler')).to.equal(1)
|
|
|
|
expect(warnMessage).to.equal(expectedMessage)
|
2019-10-23 04:44:21 +00:00
|
|
|
})
|
|
|
|
})
|
2019-06-07 17:43:32 +00:00
|
|
|
})
|