2023-06-15 14:42:27 +00:00
|
|
|
import { EventEmitter, once } from 'node:events';
|
2019-11-07 00:15:55 +00:00
|
|
|
import { expect } from 'chai';
|
2021-01-15 00:00:37 +00:00
|
|
|
import { BrowserWindow, ipcMain, IpcMainInvokeEvent, MessageChannelMain, WebContents } from 'electron/main';
|
2023-01-25 21:01:25 +00:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2023-02-20 11:30:57 +00:00
|
|
|
import { defer, listen } from './lib/spec-helpers';
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as path from 'node:path';
|
|
|
|
import * as http from 'node:http';
|
2020-03-11 01:16:58 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2022-08-03 23:55:12 +00:00
|
|
|
const fixturesPath = path.resolve(__dirname, 'fixtures');
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
describe('ipc module', () => {
|
|
|
|
describe('invoke', () => {
|
2023-02-19 09:24:24 +00:00
|
|
|
let w: BrowserWindow;
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
before(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-05-31 17:25:19 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
after(async () => {
|
|
|
|
w.destroy();
|
|
|
|
});
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
async function rendererInvoke (...args: any[]) {
|
|
|
|
const { ipcRenderer } = require('electron');
|
2019-05-31 17:25:19 +00:00
|
|
|
try {
|
|
|
|
const result = await ipcRenderer.invoke('test', ...args);
|
2019-11-01 20:37:02 +00:00
|
|
|
ipcRenderer.send('result', { result });
|
2019-05-31 17:25:19 +00:00
|
|
|
} catch (e) {
|
2022-01-31 11:37:40 +00:00
|
|
|
ipcRenderer.send('result', { error: (e as Error).message });
|
2019-05-31 17:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it('receives a response from a synchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', (e: IpcMainInvokeEvent, arg: number) => {
|
|
|
|
expect(arg).to.equal(123);
|
|
|
|
return 3;
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
|
2019-11-01 20:37:02 +00:00
|
|
|
expect(arg).to.deep.equal({ result: 3 });
|
2019-05-31 17:25:19 +00:00
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})(123)`);
|
|
|
|
await done;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives a response from an asynchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', async (e: IpcMainInvokeEvent, arg: number) => {
|
|
|
|
expect(arg).to.equal(123);
|
2020-05-20 20:18:48 +00:00
|
|
|
await new Promise(setImmediate);
|
2019-05-31 17:25:19 +00:00
|
|
|
return 3;
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
|
2019-11-01 20:37:02 +00:00
|
|
|
expect(arg).to.deep.equal({ result: 3 });
|
2019-05-31 17:25:19 +00:00
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})(123)`);
|
|
|
|
await done;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives an error from a synchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', () => {
|
|
|
|
throw new Error('some error');
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
|
2019-05-31 17:25:19 +00:00
|
|
|
expect(arg.error).to.match(/some error/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives an error from an asynchronous handler', async () => {
|
|
|
|
ipcMain.handleOnce('test', async () => {
|
2020-05-20 20:18:48 +00:00
|
|
|
await new Promise(setImmediate);
|
2019-05-31 17:25:19 +00:00
|
|
|
throw new Error('some error');
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
|
2019-05-31 17:25:19 +00:00
|
|
|
expect(arg.error).to.match(/some error/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws an error if no handler is registered', async () => {
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
|
2019-05-31 17:25:19 +00:00
|
|
|
expect(arg.error).to.match(/No handler registered/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws an error when invoking a handler that was removed', async () => {
|
2022-08-03 23:55:12 +00:00
|
|
|
ipcMain.handle('test', () => { });
|
2019-05-31 17:25:19 +00:00
|
|
|
ipcMain.removeHandler('test');
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
|
2019-05-31 17:25:19 +00:00
|
|
|
expect(arg.error).to.match(/No handler registered/);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
|
|
|
await done;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forbids multiple handlers', async () => {
|
2022-08-03 23:55:12 +00:00
|
|
|
ipcMain.handle('test', () => { });
|
2019-06-05 00:10:31 +00:00
|
|
|
try {
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(() => { ipcMain.handle('test', () => { }); }).to.throw(/second handler/);
|
2019-06-05 00:10:31 +00:00
|
|
|
} finally {
|
|
|
|
ipcMain.removeHandler('test');
|
|
|
|
}
|
|
|
|
});
|
2020-03-11 01:16:58 +00:00
|
|
|
|
|
|
|
it('throws an error in the renderer if the reply callback is dropped', async () => {
|
2023-05-25 01:09:17 +00:00
|
|
|
ipcMain.handleOnce('test', () => new Promise(() => {
|
2020-03-11 01:16:58 +00:00
|
|
|
setTimeout(() => v8Util.requestGarbageCollectionForTesting());
|
|
|
|
/* never resolve */
|
|
|
|
}));
|
|
|
|
w.webContents.executeJavaScript(`(${rendererInvoke})()`);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, { error }] = await once(ipcMain, 'result');
|
2020-03-11 01:16:58 +00:00
|
|
|
expect(error).to.match(/reply was never sent/);
|
|
|
|
});
|
2019-06-05 00:10:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ordering', () => {
|
2023-02-19 09:24:24 +00:00
|
|
|
let w: BrowserWindow;
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
before(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-06-05 00:10:31 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
after(async () => {
|
|
|
|
w.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('between send and sendSync is consistent', async () => {
|
|
|
|
const received: number[] = [];
|
|
|
|
ipcMain.on('test-async', (e, i) => { received.push(i); });
|
|
|
|
ipcMain.on('test-sync', (e, i) => { received.push(i); e.returnValue = null; });
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('done', () => { resolve(); }));
|
2019-11-01 20:37:02 +00:00
|
|
|
function rendererStressTest () {
|
|
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
for (let i = 0; i < 1000; i++) {
|
|
|
|
switch ((Math.random() * 2) | 0) {
|
|
|
|
case 0:
|
|
|
|
ipcRenderer.send('test-async', i);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ipcRenderer.sendSync('test-sync', i);
|
|
|
|
break;
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-01 20:37:02 +00:00
|
|
|
ipcRenderer.send('done');
|
|
|
|
}
|
|
|
|
try {
|
2019-06-05 00:10:31 +00:00
|
|
|
w.webContents.executeJavaScript(`(${rendererStressTest})()`);
|
|
|
|
await done;
|
|
|
|
} finally {
|
|
|
|
ipcMain.removeAllListeners('test-async');
|
|
|
|
ipcMain.removeAllListeners('test-sync');
|
|
|
|
}
|
|
|
|
expect(received).to.have.lengthOf(1000);
|
|
|
|
expect(received).to.deep.equal([...received].sort((a, b) => a - b));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('between send, sendSync, and invoke is consistent', async () => {
|
|
|
|
const received: number[] = [];
|
|
|
|
ipcMain.handle('test-invoke', (e, i) => { received.push(i); });
|
|
|
|
ipcMain.on('test-async', (e, i) => { received.push(i); });
|
|
|
|
ipcMain.on('test-sync', (e, i) => { received.push(i); e.returnValue = null; });
|
2021-01-22 19:25:47 +00:00
|
|
|
const done = new Promise<void>(resolve => ipcMain.once('done', () => { resolve(); }));
|
2019-11-01 20:37:02 +00:00
|
|
|
function rendererStressTest () {
|
|
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
for (let i = 0; i < 1000; i++) {
|
|
|
|
switch ((Math.random() * 3) | 0) {
|
|
|
|
case 0:
|
|
|
|
ipcRenderer.send('test-async', i);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ipcRenderer.sendSync('test-sync', i);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ipcRenderer.invoke('test-invoke', i);
|
|
|
|
break;
|
2019-06-05 00:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-01 20:37:02 +00:00
|
|
|
ipcRenderer.send('done');
|
|
|
|
}
|
|
|
|
try {
|
2019-06-05 00:10:31 +00:00
|
|
|
w.webContents.executeJavaScript(`(${rendererStressTest})()`);
|
|
|
|
await done;
|
|
|
|
} finally {
|
|
|
|
ipcMain.removeHandler('test-invoke');
|
|
|
|
ipcMain.removeAllListeners('test-async');
|
|
|
|
ipcMain.removeAllListeners('test-sync');
|
|
|
|
}
|
|
|
|
expect(received).to.have.lengthOf(1000);
|
|
|
|
expect(received).to.deep.equal([...received].sort((a, b) => a - b));
|
2019-05-31 17:25:19 +00:00
|
|
|
});
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
describe('MessagePort', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('can send a port to the main process', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-12 01:07:54 +00:00
|
|
|
w.loadURL('about:blank');
|
2023-02-23 23:53:53 +00:00
|
|
|
const p = once(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const channel = new MessageChannel();
|
|
|
|
require('electron').ipcRenderer.postMessage('port', 'hi', [channel.port1]);
|
|
|
|
}})()`);
|
|
|
|
const [ev, msg] = await p;
|
|
|
|
expect(msg).to.equal('hi');
|
|
|
|
expect(ev.ports).to.have.length(1);
|
2022-04-14 04:01:00 +00:00
|
|
|
expect(ev.senderFrame.parent).to.be.null();
|
|
|
|
expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
|
2020-03-12 01:07:54 +00:00
|
|
|
const [port] = ev.ports;
|
|
|
|
expect(port).to.be.an.instanceOf(EventEmitter);
|
|
|
|
});
|
|
|
|
|
2022-01-13 13:52:48 +00:00
|
|
|
it('can sent a message without a transfer', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
2023-02-23 23:53:53 +00:00
|
|
|
const p = once(ipcMain, 'port');
|
2022-01-13 13:52:48 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
require('electron').ipcRenderer.postMessage('port', 'hi');
|
|
|
|
}})()`);
|
|
|
|
const [ev, msg] = await p;
|
|
|
|
expect(msg).to.equal('hi');
|
|
|
|
expect(ev.ports).to.deep.equal([]);
|
2022-04-14 04:01:00 +00:00
|
|
|
expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
|
2022-01-13 13:52:48 +00:00
|
|
|
});
|
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
it('can communicate between main and renderer', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-12 01:07:54 +00:00
|
|
|
w.loadURL('about:blank');
|
2023-02-23 23:53:53 +00:00
|
|
|
const p = once(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const channel = new MessageChannel();
|
2023-09-05 02:22:41 +00:00
|
|
|
channel.port2.onmessage = (ev: any) => {
|
2020-03-12 01:07:54 +00:00
|
|
|
channel.port2.postMessage(ev.data * 2);
|
|
|
|
};
|
|
|
|
require('electron').ipcRenderer.postMessage('port', '', [channel.port1]);
|
|
|
|
}})()`);
|
|
|
|
const [ev] = await p;
|
|
|
|
expect(ev.ports).to.have.length(1);
|
2022-04-14 04:01:00 +00:00
|
|
|
expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
|
2020-03-12 01:07:54 +00:00
|
|
|
const [port] = ev.ports;
|
|
|
|
port.start();
|
|
|
|
port.postMessage(42);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [ev2] = await once(port, 'message');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(ev2.data).to.equal(84);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive a port from a renderer over a MessagePort connection', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-12 01:07:54 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
function fn () {
|
|
|
|
const channel1 = new MessageChannel();
|
|
|
|
const channel2 = new MessageChannel();
|
|
|
|
channel1.port2.postMessage('', [channel2.port1]);
|
|
|
|
channel2.port2.postMessage('matryoshka');
|
|
|
|
require('electron').ipcRenderer.postMessage('port', '', [channel1.port1]);
|
|
|
|
}
|
|
|
|
w.webContents.executeJavaScript(`(${fn})()`);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [{ ports: [port1] }] = await once(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
port1.start();
|
2023-02-23 23:53:53 +00:00
|
|
|
const [{ ports: [port2] }] = await once(port1, 'message');
|
2020-03-12 01:07:54 +00:00
|
|
|
port2.start();
|
2023-02-23 23:53:53 +00:00
|
|
|
const [{ data }] = await once(port2, 'message');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(data).to.equal('matryoshka');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can forward a port from one renderer to another renderer', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w1 = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
const w2 = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-12 01:07:54 +00:00
|
|
|
w1.loadURL('about:blank');
|
|
|
|
w2.loadURL('about:blank');
|
|
|
|
w1.webContents.executeJavaScript(`(${function () {
|
|
|
|
const channel = new MessageChannel();
|
2023-09-05 02:22:41 +00:00
|
|
|
channel.port2.onmessage = (ev: any) => {
|
2020-03-12 01:07:54 +00:00
|
|
|
require('electron').ipcRenderer.send('message received', ev.data);
|
|
|
|
};
|
|
|
|
require('electron').ipcRenderer.postMessage('port', '', [channel.port1]);
|
|
|
|
}})()`);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [{ ports: [port] }] = await once(ipcMain, 'port');
|
2020-03-12 01:07:54 +00:00
|
|
|
await w2.webContents.executeJavaScript(`(${function () {
|
|
|
|
require('electron').ipcRenderer.on('port', ({ ports: [port] }: any) => {
|
|
|
|
port.postMessage('a message');
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
w2.webContents.postMessage('port', '', [port]);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, data] = await once(ipcMain, 'message received');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(data).to.equal('a message');
|
|
|
|
});
|
|
|
|
|
2020-03-13 17:00:50 +00:00
|
|
|
describe('close event', () => {
|
|
|
|
describe('in renderer', () => {
|
|
|
|
it('is emitted when the main process closes its end of the port', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-13 17:00:50 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const { ipcRenderer } = require('electron');
|
2020-09-17 22:08:57 +00:00
|
|
|
ipcRenderer.on('port', e => {
|
2020-03-13 17:00:50 +00:00
|
|
|
const [port] = e.ports;
|
|
|
|
port.start();
|
2023-09-05 02:22:41 +00:00
|
|
|
port.onclose = () => {
|
2020-03-13 17:00:50 +00:00
|
|
|
ipcRenderer.send('closed');
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
w.webContents.postMessage('port', null, [port2]);
|
|
|
|
port1.close();
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'closed');
|
2020-03-13 17:00:50 +00:00
|
|
|
});
|
|
|
|
|
2023-09-29 05:26:41 +00:00
|
|
|
// TODO(@vertedinde): This broke upstream in CL https://chromium-review.googlesource.com/c/chromium/src/+/4831380
|
|
|
|
// The behavior seems to be an intentional change, we need to either A) implement the task_container_ model in
|
|
|
|
// our renderer message ports or B) patch how we handle renderer message ports being garbage collected
|
|
|
|
// crbug: https://bugs.chromium.org/p/chromium/issues/detail?id=1487835
|
|
|
|
it.skip('is emitted when the other end of a port is garbage-collected', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-13 17:00:50 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`(${async function () {
|
|
|
|
const { port2 } = new MessageChannel();
|
2023-09-05 02:22:41 +00:00
|
|
|
await new Promise<void>(resolve => {
|
2020-03-13 17:00:50 +00:00
|
|
|
port2.start();
|
2023-09-05 02:22:41 +00:00
|
|
|
port2.onclose = resolve;
|
2020-06-23 03:32:45 +00:00
|
|
|
process._linkedBinding('electron_common_v8_util').requestGarbageCollectionForTesting();
|
2020-03-13 17:00:50 +00:00
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is emitted when the other end of a port is sent to nowhere', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-13 17:00:50 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
ipcMain.once('do-a-gc', () => v8Util.requestGarbageCollectionForTesting());
|
|
|
|
await w.webContents.executeJavaScript(`(${async function () {
|
|
|
|
const { port1, port2 } = new MessageChannel();
|
2023-09-05 02:22:41 +00:00
|
|
|
await new Promise<void>(resolve => {
|
2020-03-13 17:00:50 +00:00
|
|
|
port2.start();
|
2023-09-05 02:22:41 +00:00
|
|
|
port2.onclose = resolve;
|
2020-03-13 17:00:50 +00:00
|
|
|
require('electron').ipcRenderer.postMessage('nobody-listening', null, [port1]);
|
|
|
|
require('electron').ipcRenderer.send('do-a-gc');
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
describe('MessageChannelMain', () => {
|
|
|
|
it('can be created', () => {
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
expect(port1).not.to.be.null();
|
|
|
|
expect(port2).not.to.be.null();
|
|
|
|
});
|
|
|
|
|
2023-03-27 17:56:55 +00:00
|
|
|
it('throws an error when an invalid parameter is sent to postMessage', () => {
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
const buffer = new ArrayBuffer(10) as any;
|
|
|
|
port1.postMessage(null, [buffer]);
|
|
|
|
}).to.throw(/Port at index 0 is not a valid port/);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, ['1' as any]);
|
|
|
|
}).to.throw(/Port at index 0 is not a valid port/);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, [new Date() as any]);
|
|
|
|
}).to.throw(/Port at index 0 is not a valid port/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when postMessage transferables contains the source port', () => {
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, [port1]);
|
|
|
|
}).to.throw(/Port at index 0 contains the source port./);
|
|
|
|
});
|
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
it('can send messages within the process', async () => {
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port2.postMessage('hello');
|
|
|
|
port1.start();
|
2023-02-23 23:53:53 +00:00
|
|
|
const [ev] = await once(port1, 'message');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(ev.data).to.equal('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can pass one end to a WebContents', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-12 01:07:54 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const { ipcRenderer } = require('electron');
|
2020-09-17 22:08:57 +00:00
|
|
|
ipcRenderer.on('port', ev => {
|
2020-03-12 01:07:54 +00:00
|
|
|
const [port] = ev.ports;
|
|
|
|
port.onmessage = () => {
|
|
|
|
ipcRenderer.send('done');
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
w.webContents.postMessage('port', null, [port2]);
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'done');
|
2020-03-12 01:07:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be passed over another channel', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-12 01:07:54 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const { ipcRenderer } = require('electron');
|
2020-09-17 22:08:57 +00:00
|
|
|
ipcRenderer.on('port', e1 => {
|
|
|
|
e1.ports[0].onmessage = e2 => {
|
|
|
|
e2.ports[0].onmessage = e3 => {
|
2020-03-12 01:07:54 +00:00
|
|
|
ipcRenderer.send('done', e3.data);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
const { port1: port3, port2: port4 } = new MessageChannelMain();
|
|
|
|
port1.postMessage(null, [port4]);
|
|
|
|
port3.postMessage('hello');
|
|
|
|
w.webContents.postMessage('port', null, [port2]);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, message] = await once(ipcMain, 'done');
|
2020-03-12 01:07:54 +00:00
|
|
|
expect(message).to.equal('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can send messages to a closed port', () => {
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port2.start();
|
|
|
|
port2.on('message', () => { throw new Error('unexpected message received'); });
|
|
|
|
port1.close();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can send messages to a port whose remote end is closed', () => {
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
|
|
|
port2.start();
|
|
|
|
port2.on('message', () => { throw new Error('unexpected message received'); });
|
|
|
|
port2.close();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when passing null ports', () => {
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, [null] as any);
|
2023-03-27 17:56:55 +00:00
|
|
|
}).to.throw(/Port at index 0 is not a valid port/);
|
2020-03-12 01:07:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when passing duplicate ports', () => {
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
const { port1: port3 } = new MessageChannelMain();
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, [port3, port3]);
|
|
|
|
}).to.throw(/duplicate/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when passing ports that have already been neutered', () => {
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
const { port1: port3 } = new MessageChannelMain();
|
|
|
|
port1.postMessage(null, [port3]);
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, [port3]);
|
|
|
|
}).to.throw(/already neutered/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when passing itself', () => {
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
expect(() => {
|
|
|
|
port1.postMessage(null, [port1]);
|
|
|
|
}).to.throw(/contains the source port/);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GC behavior', () => {
|
|
|
|
it('is not collected while it could still receive messages', async () => {
|
|
|
|
let trigger: Function;
|
|
|
|
const promise = new Promise(resolve => { trigger = resolve; });
|
|
|
|
const port1 = (() => {
|
|
|
|
const { port1, port2 } = new MessageChannelMain();
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
port2.on('message', (e) => { trigger(e.data); });
|
|
|
|
port2.start();
|
|
|
|
return port1;
|
|
|
|
})();
|
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
port1.postMessage('hello');
|
|
|
|
expect(await promise).to.equal('hello');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-15 20:50:31 +00:00
|
|
|
const generateTests = (title: string, postMessage: (contents: WebContents) => WebContents['postMessage']) => {
|
2021-01-15 00:00:37 +00:00
|
|
|
describe(title, () => {
|
|
|
|
it('sends a message', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2021-01-15 00:00:37 +00:00
|
|
|
w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`(${function () {
|
|
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
ipcRenderer.on('foo', (_e, msg) => {
|
|
|
|
ipcRenderer.send('bar', msg);
|
|
|
|
});
|
|
|
|
}})()`);
|
|
|
|
postMessage(w.webContents)('foo', { some: 'message' });
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, msg] = await once(ipcMain, 'bar');
|
2021-01-15 00:00:37 +00:00
|
|
|
expect(msg).to.deep.equal({ some: 'message' });
|
2020-03-12 01:07:54 +00:00
|
|
|
});
|
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
describe('error handling', () => {
|
|
|
|
it('throws on missing channel', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
(postMessage(w.webContents) as any)();
|
|
|
|
}).to.throw(/Insufficient number of arguments/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws on invalid channel', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
postMessage(w.webContents)(null as any, '', []);
|
|
|
|
}).to.throw(/Error processing argument at index 0/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws on missing message', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
(postMessage(w.webContents) as any)('channel');
|
|
|
|
}).to.throw(/Insufficient number of arguments/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws on non-serializable message', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
postMessage(w.webContents)('channel', w);
|
|
|
|
}).to.throw(/An object could not be cloned/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws on invalid transferable list', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
postMessage(w.webContents)('', '', null as any);
|
|
|
|
}).to.throw(/Invalid value for transfer/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws on transferring non-transferable', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
(postMessage(w.webContents) as any)('channel', '', [123]);
|
|
|
|
}).to.throw(/Invalid value for transfer/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws when passing null ports', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(() => {
|
|
|
|
postMessage(w.webContents)('foo', null, [null] as any);
|
|
|
|
}).to.throw(/Invalid value for transfer/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when passing duplicate ports', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
expect(() => {
|
|
|
|
postMessage(w.webContents)('foo', null, [port1, port1]);
|
|
|
|
}).to.throw(/duplicate/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
|
2021-01-15 00:00:37 +00:00
|
|
|
it('throws when passing ports that have already been neutered', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const { port1 } = new MessageChannelMain();
|
|
|
|
postMessage(w.webContents)('foo', null, [port1]);
|
|
|
|
expect(() => {
|
|
|
|
postMessage(w.webContents)('foo', null, [port1]);
|
|
|
|
}).to.throw(/already neutered/);
|
|
|
|
});
|
2020-03-12 01:07:54 +00:00
|
|
|
});
|
|
|
|
});
|
2021-01-15 00:00:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
generateTests('WebContents.postMessage', contents => contents.postMessage.bind(contents));
|
|
|
|
generateTests('WebFrameMain.postMessage', contents => contents.mainFrame.postMessage.bind(contents.mainFrame));
|
2020-03-12 01:07:54 +00:00
|
|
|
});
|
2022-08-03 23:55:12 +00:00
|
|
|
|
|
|
|
describe('WebContents.ipc', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('receives ipc messages sent from the WebContents', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, num] = await once(w.webContents.ipc, 'test');
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(num).to.equal(42);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives sync-ipc messages sent from the WebContents', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.ipc.on('test', (event, arg) => {
|
|
|
|
event.returnValue = arg * 2;
|
|
|
|
});
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.sendSync(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives postMessage messages sent from the WebContents, w/ MessagePorts', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.postMessage(\'test\', null, [(new MessageChannel).port1])');
|
2023-02-23 23:53:53 +00:00
|
|
|
const [event] = await once(w.webContents.ipc, 'test');
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(event.ports.length).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles invoke messages sent from the WebContents', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.ipc.handle('test', (_event, arg) => arg * 2);
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cascades to ipcMain', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
let gotFromIpcMain = false;
|
|
|
|
const ipcMainReceived = new Promise<void>(resolve => ipcMain.on('test', () => { gotFromIpcMain = true; resolve(); }));
|
|
|
|
const ipcReceived = new Promise<boolean>(resolve => w.webContents.ipc.on('test', () => { resolve(gotFromIpcMain); }));
|
|
|
|
defer(() => ipcMain.removeAllListeners('test'));
|
|
|
|
w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
|
|
|
|
|
|
|
|
// assert that they are delivered in the correct order
|
|
|
|
expect(await ipcReceived).to.be.false();
|
|
|
|
await ipcMainReceived;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides ipcMain handlers', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.ipc.handle('test', (_event, arg) => arg * 2);
|
|
|
|
ipcMain.handle('test', () => { throw new Error('should not be called'); });
|
|
|
|
defer(() => ipcMain.removeHandler('test'));
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('falls back to ipcMain handlers', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
ipcMain.handle('test', (_event, arg) => { return arg * 2; });
|
|
|
|
defer(() => ipcMain.removeHandler('test'));
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives ipcs from child frames', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.setHeader('content-type', 'text/html');
|
|
|
|
res.end('');
|
|
|
|
});
|
2023-02-20 11:30:57 +00:00
|
|
|
const { port } = await listen(server);
|
2022-08-03 23:55:12 +00:00
|
|
|
defer(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegrationInSubFrames: true, preload: path.resolve(fixturesPath, 'preload-expose-ipc.js') } });
|
|
|
|
// Preloads don't run in about:blank windows, and file:// urls can't be loaded in iframes, so use a blank http page.
|
|
|
|
await w.loadURL(`data:text/html,<iframe src="http://localhost:${port}"></iframe>`);
|
|
|
|
w.webContents.mainFrame.frames[0].executeJavaScript('ipc.send(\'test\', 42)');
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, arg] = await once(w.webContents.ipc, 'test');
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(arg).to.equal(42);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('WebFrameMain.ipc', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('responds to ipc messages in the main frame', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, arg] = await once(w.webContents.mainFrame.ipc, 'test');
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(arg).to.equal(42);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('responds to sync ipc messages in the main frame', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.mainFrame.ipc.on('test', (event, arg) => {
|
|
|
|
event.returnValue = arg * 2;
|
|
|
|
});
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.sendSync(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives postMessage messages sent from the WebContents, w/ MessagePorts', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.postMessage(\'test\', null, [(new MessageChannel).port1])');
|
2023-02-23 23:53:53 +00:00
|
|
|
const [event] = await once(w.webContents.mainFrame.ipc, 'test');
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(event.ports.length).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles invoke messages sent from the WebContents', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.mainFrame.ipc.handle('test', (_event, arg) => arg * 2);
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cascades to WebContents and ipcMain', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
let gotFromIpcMain = false;
|
|
|
|
let gotFromWebContents = false;
|
|
|
|
const ipcMainReceived = new Promise<void>(resolve => ipcMain.on('test', () => { gotFromIpcMain = true; resolve(); }));
|
|
|
|
const ipcWebContentsReceived = new Promise<boolean>(resolve => w.webContents.ipc.on('test', () => { gotFromWebContents = true; resolve(gotFromIpcMain); }));
|
|
|
|
const ipcReceived = new Promise<boolean>(resolve => w.webContents.mainFrame.ipc.on('test', () => { resolve(gotFromWebContents); }));
|
|
|
|
defer(() => ipcMain.removeAllListeners('test'));
|
|
|
|
w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
|
|
|
|
|
|
|
|
// assert that they are delivered in the correct order
|
|
|
|
expect(await ipcReceived).to.be.false();
|
|
|
|
expect(await ipcWebContentsReceived).to.be.false();
|
|
|
|
await ipcMainReceived;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides ipcMain handlers', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.mainFrame.ipc.handle('test', (_event, arg) => arg * 2);
|
|
|
|
ipcMain.handle('test', () => { throw new Error('should not be called'); });
|
|
|
|
defer(() => ipcMain.removeHandler('test'));
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides WebContents handlers', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.ipc.handle('test', () => { throw new Error('should not be called'); });
|
|
|
|
w.webContents.mainFrame.ipc.handle('test', (_event, arg) => arg * 2);
|
|
|
|
ipcMain.handle('test', () => { throw new Error('should not be called'); });
|
|
|
|
defer(() => ipcMain.removeHandler('test'));
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('falls back to WebContents handlers', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.ipc.handle('test', (_event, arg) => { return arg * 2; });
|
|
|
|
const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
|
|
|
|
expect(result).to.equal(42 * 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('receives ipcs from child frames', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.setHeader('content-type', 'text/html');
|
|
|
|
res.end('');
|
|
|
|
});
|
2023-02-20 11:30:57 +00:00
|
|
|
const { port } = await listen(server);
|
2022-08-03 23:55:12 +00:00
|
|
|
defer(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegrationInSubFrames: true, preload: path.resolve(fixturesPath, 'preload-expose-ipc.js') } });
|
|
|
|
// Preloads don't run in about:blank windows, and file:// urls can't be loaded in iframes, so use a blank http page.
|
|
|
|
await w.loadURL(`data:text/html,<iframe src="http://localhost:${port}"></iframe>`);
|
|
|
|
w.webContents.mainFrame.frames[0].executeJavaScript('ipc.send(\'test\', 42)');
|
|
|
|
w.webContents.mainFrame.ipc.on('test', () => { throw new Error('should not be called'); });
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, arg] = await once(w.webContents.mainFrame.frames[0].ipc, 'test');
|
2022-08-03 23:55:12 +00:00
|
|
|
expect(arg).to.equal(42);
|
|
|
|
});
|
|
|
|
});
|
2019-05-31 17:25:19 +00:00
|
|
|
});
|