api-ipc-spec.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. import { EventEmitter } from 'events';
  2. import { expect } from 'chai';
  3. import { BrowserWindow, ipcMain, IpcMainInvokeEvent, MessageChannelMain, WebContents } from 'electron/main';
  4. import { closeAllWindows } from './window-helpers';
  5. import { emittedOnce } from './events-helpers';
  6. import { defer } from './spec-helpers';
  7. import * as path from 'path';
  8. import * as http from 'http';
  9. import { AddressInfo } from 'net';
  10. const v8Util = process._linkedBinding('electron_common_v8_util');
  11. const fixturesPath = path.resolve(__dirname, 'fixtures');
  12. describe('ipc module', () => {
  13. describe('invoke', () => {
  14. let w = (null as unknown as BrowserWindow);
  15. before(async () => {
  16. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  17. await w.loadURL('about:blank');
  18. });
  19. after(async () => {
  20. w.destroy();
  21. });
  22. async function rendererInvoke (...args: any[]) {
  23. const { ipcRenderer } = require('electron');
  24. try {
  25. const result = await ipcRenderer.invoke('test', ...args);
  26. ipcRenderer.send('result', { result });
  27. } catch (e) {
  28. ipcRenderer.send('result', { error: (e as Error).message });
  29. }
  30. }
  31. it('receives a response from a synchronous handler', async () => {
  32. ipcMain.handleOnce('test', (e: IpcMainInvokeEvent, arg: number) => {
  33. expect(arg).to.equal(123);
  34. return 3;
  35. });
  36. const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
  37. expect(arg).to.deep.equal({ result: 3 });
  38. resolve();
  39. }));
  40. await w.webContents.executeJavaScript(`(${rendererInvoke})(123)`);
  41. await done;
  42. });
  43. it('receives a response from an asynchronous handler', async () => {
  44. ipcMain.handleOnce('test', async (e: IpcMainInvokeEvent, arg: number) => {
  45. expect(arg).to.equal(123);
  46. await new Promise(setImmediate);
  47. return 3;
  48. });
  49. const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
  50. expect(arg).to.deep.equal({ result: 3 });
  51. resolve();
  52. }));
  53. await w.webContents.executeJavaScript(`(${rendererInvoke})(123)`);
  54. await done;
  55. });
  56. it('receives an error from a synchronous handler', async () => {
  57. ipcMain.handleOnce('test', () => {
  58. throw new Error('some error');
  59. });
  60. const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
  61. expect(arg.error).to.match(/some error/);
  62. resolve();
  63. }));
  64. await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
  65. await done;
  66. });
  67. it('receives an error from an asynchronous handler', async () => {
  68. ipcMain.handleOnce('test', async () => {
  69. await new Promise(setImmediate);
  70. throw new Error('some error');
  71. });
  72. const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
  73. expect(arg.error).to.match(/some error/);
  74. resolve();
  75. }));
  76. await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
  77. await done;
  78. });
  79. it('throws an error if no handler is registered', async () => {
  80. const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
  81. expect(arg.error).to.match(/No handler registered/);
  82. resolve();
  83. }));
  84. await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
  85. await done;
  86. });
  87. it('throws an error when invoking a handler that was removed', async () => {
  88. ipcMain.handle('test', () => { });
  89. ipcMain.removeHandler('test');
  90. const done = new Promise<void>(resolve => ipcMain.once('result', (e, arg) => {
  91. expect(arg.error).to.match(/No handler registered/);
  92. resolve();
  93. }));
  94. await w.webContents.executeJavaScript(`(${rendererInvoke})()`);
  95. await done;
  96. });
  97. it('forbids multiple handlers', async () => {
  98. ipcMain.handle('test', () => { });
  99. try {
  100. expect(() => { ipcMain.handle('test', () => { }); }).to.throw(/second handler/);
  101. } finally {
  102. ipcMain.removeHandler('test');
  103. }
  104. });
  105. it('throws an error in the renderer if the reply callback is dropped', async () => {
  106. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  107. ipcMain.handleOnce('test', () => new Promise(resolve => {
  108. setTimeout(() => v8Util.requestGarbageCollectionForTesting());
  109. /* never resolve */
  110. }));
  111. w.webContents.executeJavaScript(`(${rendererInvoke})()`);
  112. const [, { error }] = await emittedOnce(ipcMain, 'result');
  113. expect(error).to.match(/reply was never sent/);
  114. });
  115. });
  116. describe('ordering', () => {
  117. let w = (null as unknown as BrowserWindow);
  118. before(async () => {
  119. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  120. await w.loadURL('about:blank');
  121. });
  122. after(async () => {
  123. w.destroy();
  124. });
  125. it('between send and sendSync is consistent', async () => {
  126. const received: number[] = [];
  127. ipcMain.on('test-async', (e, i) => { received.push(i); });
  128. ipcMain.on('test-sync', (e, i) => { received.push(i); e.returnValue = null; });
  129. const done = new Promise<void>(resolve => ipcMain.once('done', () => { resolve(); }));
  130. function rendererStressTest () {
  131. const { ipcRenderer } = require('electron');
  132. for (let i = 0; i < 1000; i++) {
  133. switch ((Math.random() * 2) | 0) {
  134. case 0:
  135. ipcRenderer.send('test-async', i);
  136. break;
  137. case 1:
  138. ipcRenderer.sendSync('test-sync', i);
  139. break;
  140. }
  141. }
  142. ipcRenderer.send('done');
  143. }
  144. try {
  145. w.webContents.executeJavaScript(`(${rendererStressTest})()`);
  146. await done;
  147. } finally {
  148. ipcMain.removeAllListeners('test-async');
  149. ipcMain.removeAllListeners('test-sync');
  150. }
  151. expect(received).to.have.lengthOf(1000);
  152. expect(received).to.deep.equal([...received].sort((a, b) => a - b));
  153. });
  154. it('between send, sendSync, and invoke is consistent', async () => {
  155. const received: number[] = [];
  156. ipcMain.handle('test-invoke', (e, i) => { received.push(i); });
  157. ipcMain.on('test-async', (e, i) => { received.push(i); });
  158. ipcMain.on('test-sync', (e, i) => { received.push(i); e.returnValue = null; });
  159. const done = new Promise<void>(resolve => ipcMain.once('done', () => { resolve(); }));
  160. function rendererStressTest () {
  161. const { ipcRenderer } = require('electron');
  162. for (let i = 0; i < 1000; i++) {
  163. switch ((Math.random() * 3) | 0) {
  164. case 0:
  165. ipcRenderer.send('test-async', i);
  166. break;
  167. case 1:
  168. ipcRenderer.sendSync('test-sync', i);
  169. break;
  170. case 2:
  171. ipcRenderer.invoke('test-invoke', i);
  172. break;
  173. }
  174. }
  175. ipcRenderer.send('done');
  176. }
  177. try {
  178. w.webContents.executeJavaScript(`(${rendererStressTest})()`);
  179. await done;
  180. } finally {
  181. ipcMain.removeHandler('test-invoke');
  182. ipcMain.removeAllListeners('test-async');
  183. ipcMain.removeAllListeners('test-sync');
  184. }
  185. expect(received).to.have.lengthOf(1000);
  186. expect(received).to.deep.equal([...received].sort((a, b) => a - b));
  187. });
  188. });
  189. describe('MessagePort', () => {
  190. afterEach(closeAllWindows);
  191. it('can send a port to the main process', async () => {
  192. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  193. w.loadURL('about:blank');
  194. const p = emittedOnce(ipcMain, 'port');
  195. await w.webContents.executeJavaScript(`(${function () {
  196. const channel = new MessageChannel();
  197. require('electron').ipcRenderer.postMessage('port', 'hi', [channel.port1]);
  198. }})()`);
  199. const [ev, msg] = await p;
  200. expect(msg).to.equal('hi');
  201. expect(ev.ports).to.have.length(1);
  202. expect(ev.senderFrame.parent).to.be.null();
  203. expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
  204. const [port] = ev.ports;
  205. expect(port).to.be.an.instanceOf(EventEmitter);
  206. });
  207. it('can sent a message without a transfer', async () => {
  208. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  209. w.loadURL('about:blank');
  210. const p = emittedOnce(ipcMain, 'port');
  211. await w.webContents.executeJavaScript(`(${function () {
  212. require('electron').ipcRenderer.postMessage('port', 'hi');
  213. }})()`);
  214. const [ev, msg] = await p;
  215. expect(msg).to.equal('hi');
  216. expect(ev.ports).to.deep.equal([]);
  217. expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
  218. });
  219. it('can communicate between main and renderer', async () => {
  220. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  221. w.loadURL('about:blank');
  222. const p = emittedOnce(ipcMain, 'port');
  223. await w.webContents.executeJavaScript(`(${function () {
  224. const channel = new MessageChannel();
  225. (channel.port2 as any).onmessage = (ev: any) => {
  226. channel.port2.postMessage(ev.data * 2);
  227. };
  228. require('electron').ipcRenderer.postMessage('port', '', [channel.port1]);
  229. }})()`);
  230. const [ev] = await p;
  231. expect(ev.ports).to.have.length(1);
  232. expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
  233. const [port] = ev.ports;
  234. port.start();
  235. port.postMessage(42);
  236. const [ev2] = await emittedOnce(port, 'message');
  237. expect(ev2.data).to.equal(84);
  238. });
  239. it('can receive a port from a renderer over a MessagePort connection', async () => {
  240. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  241. w.loadURL('about:blank');
  242. function fn () {
  243. const channel1 = new MessageChannel();
  244. const channel2 = new MessageChannel();
  245. channel1.port2.postMessage('', [channel2.port1]);
  246. channel2.port2.postMessage('matryoshka');
  247. require('electron').ipcRenderer.postMessage('port', '', [channel1.port1]);
  248. }
  249. w.webContents.executeJavaScript(`(${fn})()`);
  250. const [{ ports: [port1] }] = await emittedOnce(ipcMain, 'port');
  251. port1.start();
  252. const [{ ports: [port2] }] = await emittedOnce(port1, 'message');
  253. port2.start();
  254. const [{ data }] = await emittedOnce(port2, 'message');
  255. expect(data).to.equal('matryoshka');
  256. });
  257. it('can forward a port from one renderer to another renderer', async () => {
  258. const w1 = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  259. const w2 = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  260. w1.loadURL('about:blank');
  261. w2.loadURL('about:blank');
  262. w1.webContents.executeJavaScript(`(${function () {
  263. const channel = new MessageChannel();
  264. (channel.port2 as any).onmessage = (ev: any) => {
  265. require('electron').ipcRenderer.send('message received', ev.data);
  266. };
  267. require('electron').ipcRenderer.postMessage('port', '', [channel.port1]);
  268. }})()`);
  269. const [{ ports: [port] }] = await emittedOnce(ipcMain, 'port');
  270. await w2.webContents.executeJavaScript(`(${function () {
  271. require('electron').ipcRenderer.on('port', ({ ports: [port] }: any) => {
  272. port.postMessage('a message');
  273. });
  274. }})()`);
  275. w2.webContents.postMessage('port', '', [port]);
  276. const [, data] = await emittedOnce(ipcMain, 'message received');
  277. expect(data).to.equal('a message');
  278. });
  279. describe('close event', () => {
  280. describe('in renderer', () => {
  281. it('is emitted when the main process closes its end of the port', async () => {
  282. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  283. w.loadURL('about:blank');
  284. await w.webContents.executeJavaScript(`(${function () {
  285. const { ipcRenderer } = require('electron');
  286. ipcRenderer.on('port', e => {
  287. const [port] = e.ports;
  288. port.start();
  289. (port as any).onclose = () => {
  290. ipcRenderer.send('closed');
  291. };
  292. });
  293. }})()`);
  294. const { port1, port2 } = new MessageChannelMain();
  295. w.webContents.postMessage('port', null, [port2]);
  296. port1.close();
  297. await emittedOnce(ipcMain, 'closed');
  298. });
  299. it('is emitted when the other end of a port is garbage-collected', async () => {
  300. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  301. w.loadURL('about:blank');
  302. await w.webContents.executeJavaScript(`(${async function () {
  303. const { port2 } = new MessageChannel();
  304. await new Promise(resolve => {
  305. port2.start();
  306. (port2 as any).onclose = resolve;
  307. process._linkedBinding('electron_common_v8_util').requestGarbageCollectionForTesting();
  308. });
  309. }})()`);
  310. });
  311. it('is emitted when the other end of a port is sent to nowhere', async () => {
  312. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  313. w.loadURL('about:blank');
  314. ipcMain.once('do-a-gc', () => v8Util.requestGarbageCollectionForTesting());
  315. await w.webContents.executeJavaScript(`(${async function () {
  316. const { port1, port2 } = new MessageChannel();
  317. await new Promise(resolve => {
  318. port2.start();
  319. (port2 as any).onclose = resolve;
  320. require('electron').ipcRenderer.postMessage('nobody-listening', null, [port1]);
  321. require('electron').ipcRenderer.send('do-a-gc');
  322. });
  323. }})()`);
  324. });
  325. });
  326. });
  327. describe('MessageChannelMain', () => {
  328. it('can be created', () => {
  329. const { port1, port2 } = new MessageChannelMain();
  330. expect(port1).not.to.be.null();
  331. expect(port2).not.to.be.null();
  332. });
  333. it('can send messages within the process', async () => {
  334. const { port1, port2 } = new MessageChannelMain();
  335. port2.postMessage('hello');
  336. port1.start();
  337. const [ev] = await emittedOnce(port1, 'message');
  338. expect(ev.data).to.equal('hello');
  339. });
  340. it('can pass one end to a WebContents', async () => {
  341. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  342. w.loadURL('about:blank');
  343. await w.webContents.executeJavaScript(`(${function () {
  344. const { ipcRenderer } = require('electron');
  345. ipcRenderer.on('port', ev => {
  346. const [port] = ev.ports;
  347. port.onmessage = () => {
  348. ipcRenderer.send('done');
  349. };
  350. });
  351. }})()`);
  352. const { port1, port2 } = new MessageChannelMain();
  353. port1.postMessage('hello');
  354. w.webContents.postMessage('port', null, [port2]);
  355. await emittedOnce(ipcMain, 'done');
  356. });
  357. it('can be passed over another channel', async () => {
  358. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  359. w.loadURL('about:blank');
  360. await w.webContents.executeJavaScript(`(${function () {
  361. const { ipcRenderer } = require('electron');
  362. ipcRenderer.on('port', e1 => {
  363. e1.ports[0].onmessage = e2 => {
  364. e2.ports[0].onmessage = e3 => {
  365. ipcRenderer.send('done', e3.data);
  366. };
  367. };
  368. });
  369. }})()`);
  370. const { port1, port2 } = new MessageChannelMain();
  371. const { port1: port3, port2: port4 } = new MessageChannelMain();
  372. port1.postMessage(null, [port4]);
  373. port3.postMessage('hello');
  374. w.webContents.postMessage('port', null, [port2]);
  375. const [, message] = await emittedOnce(ipcMain, 'done');
  376. expect(message).to.equal('hello');
  377. });
  378. it('can send messages to a closed port', () => {
  379. const { port1, port2 } = new MessageChannelMain();
  380. port2.start();
  381. port2.on('message', () => { throw new Error('unexpected message received'); });
  382. port1.close();
  383. port1.postMessage('hello');
  384. });
  385. it('can send messages to a port whose remote end is closed', () => {
  386. const { port1, port2 } = new MessageChannelMain();
  387. port2.start();
  388. port2.on('message', () => { throw new Error('unexpected message received'); });
  389. port2.close();
  390. port1.postMessage('hello');
  391. });
  392. it('throws when passing null ports', () => {
  393. const { port1 } = new MessageChannelMain();
  394. expect(() => {
  395. port1.postMessage(null, [null] as any);
  396. }).to.throw(/conversion failure/);
  397. });
  398. it('throws when passing duplicate ports', () => {
  399. const { port1 } = new MessageChannelMain();
  400. const { port1: port3 } = new MessageChannelMain();
  401. expect(() => {
  402. port1.postMessage(null, [port3, port3]);
  403. }).to.throw(/duplicate/);
  404. });
  405. it('throws when passing ports that have already been neutered', () => {
  406. const { port1 } = new MessageChannelMain();
  407. const { port1: port3 } = new MessageChannelMain();
  408. port1.postMessage(null, [port3]);
  409. expect(() => {
  410. port1.postMessage(null, [port3]);
  411. }).to.throw(/already neutered/);
  412. });
  413. it('throws when passing itself', () => {
  414. const { port1 } = new MessageChannelMain();
  415. expect(() => {
  416. port1.postMessage(null, [port1]);
  417. }).to.throw(/contains the source port/);
  418. });
  419. describe('GC behavior', () => {
  420. it('is not collected while it could still receive messages', async () => {
  421. let trigger: Function;
  422. const promise = new Promise(resolve => { trigger = resolve; });
  423. const port1 = (() => {
  424. const { port1, port2 } = new MessageChannelMain();
  425. port2.on('message', (e) => { trigger(e.data); });
  426. port2.start();
  427. return port1;
  428. })();
  429. v8Util.requestGarbageCollectionForTesting();
  430. port1.postMessage('hello');
  431. expect(await promise).to.equal('hello');
  432. });
  433. });
  434. });
  435. const generateTests = (title: string, postMessage: (contents: WebContents) => WebContents['postMessage']) => {
  436. describe(title, () => {
  437. it('sends a message', async () => {
  438. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  439. w.loadURL('about:blank');
  440. await w.webContents.executeJavaScript(`(${function () {
  441. const { ipcRenderer } = require('electron');
  442. ipcRenderer.on('foo', (_e, msg) => {
  443. ipcRenderer.send('bar', msg);
  444. });
  445. }})()`);
  446. postMessage(w.webContents)('foo', { some: 'message' });
  447. const [, msg] = await emittedOnce(ipcMain, 'bar');
  448. expect(msg).to.deep.equal({ some: 'message' });
  449. });
  450. describe('error handling', () => {
  451. it('throws on missing channel', async () => {
  452. const w = new BrowserWindow({ show: false });
  453. await w.loadURL('about:blank');
  454. expect(() => {
  455. (postMessage(w.webContents) as any)();
  456. }).to.throw(/Insufficient number of arguments/);
  457. });
  458. it('throws on invalid channel', async () => {
  459. const w = new BrowserWindow({ show: false });
  460. await w.loadURL('about:blank');
  461. expect(() => {
  462. postMessage(w.webContents)(null as any, '', []);
  463. }).to.throw(/Error processing argument at index 0/);
  464. });
  465. it('throws on missing message', async () => {
  466. const w = new BrowserWindow({ show: false });
  467. await w.loadURL('about:blank');
  468. expect(() => {
  469. (postMessage(w.webContents) as any)('channel');
  470. }).to.throw(/Insufficient number of arguments/);
  471. });
  472. it('throws on non-serializable message', async () => {
  473. const w = new BrowserWindow({ show: false });
  474. await w.loadURL('about:blank');
  475. expect(() => {
  476. postMessage(w.webContents)('channel', w);
  477. }).to.throw(/An object could not be cloned/);
  478. });
  479. it('throws on invalid transferable list', async () => {
  480. const w = new BrowserWindow({ show: false });
  481. await w.loadURL('about:blank');
  482. expect(() => {
  483. postMessage(w.webContents)('', '', null as any);
  484. }).to.throw(/Invalid value for transfer/);
  485. });
  486. it('throws on transferring non-transferable', async () => {
  487. const w = new BrowserWindow({ show: false });
  488. await w.loadURL('about:blank');
  489. expect(() => {
  490. (postMessage(w.webContents) as any)('channel', '', [123]);
  491. }).to.throw(/Invalid value for transfer/);
  492. });
  493. it('throws when passing null ports', async () => {
  494. const w = new BrowserWindow({ show: false });
  495. await w.loadURL('about:blank');
  496. expect(() => {
  497. postMessage(w.webContents)('foo', null, [null] as any);
  498. }).to.throw(/Invalid value for transfer/);
  499. });
  500. it('throws when passing duplicate ports', async () => {
  501. const w = new BrowserWindow({ show: false });
  502. await w.loadURL('about:blank');
  503. const { port1 } = new MessageChannelMain();
  504. expect(() => {
  505. postMessage(w.webContents)('foo', null, [port1, port1]);
  506. }).to.throw(/duplicate/);
  507. });
  508. it('throws when passing ports that have already been neutered', async () => {
  509. const w = new BrowserWindow({ show: false });
  510. await w.loadURL('about:blank');
  511. const { port1 } = new MessageChannelMain();
  512. postMessage(w.webContents)('foo', null, [port1]);
  513. expect(() => {
  514. postMessage(w.webContents)('foo', null, [port1]);
  515. }).to.throw(/already neutered/);
  516. });
  517. });
  518. });
  519. };
  520. generateTests('WebContents.postMessage', contents => contents.postMessage.bind(contents));
  521. generateTests('WebFrameMain.postMessage', contents => contents.mainFrame.postMessage.bind(contents.mainFrame));
  522. });
  523. describe('WebContents.ipc', () => {
  524. afterEach(closeAllWindows);
  525. it('receives ipc messages sent from the WebContents', async () => {
  526. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  527. w.loadURL('about:blank');
  528. w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
  529. const [, num] = await emittedOnce(w.webContents.ipc, 'test');
  530. expect(num).to.equal(42);
  531. });
  532. it('receives sync-ipc messages sent from the WebContents', async () => {
  533. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  534. w.loadURL('about:blank');
  535. w.webContents.ipc.on('test', (event, arg) => {
  536. event.returnValue = arg * 2;
  537. });
  538. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.sendSync(\'test\', 42)');
  539. expect(result).to.equal(42 * 2);
  540. });
  541. it('receives postMessage messages sent from the WebContents, w/ MessagePorts', async () => {
  542. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  543. w.loadURL('about:blank');
  544. w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.postMessage(\'test\', null, [(new MessageChannel).port1])');
  545. const [event] = await emittedOnce(w.webContents.ipc, 'test');
  546. expect(event.ports.length).to.equal(1);
  547. });
  548. it('handles invoke messages sent from the WebContents', async () => {
  549. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  550. w.loadURL('about:blank');
  551. w.webContents.ipc.handle('test', (_event, arg) => arg * 2);
  552. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  553. expect(result).to.equal(42 * 2);
  554. });
  555. it('cascades to ipcMain', async () => {
  556. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  557. w.loadURL('about:blank');
  558. let gotFromIpcMain = false;
  559. const ipcMainReceived = new Promise<void>(resolve => ipcMain.on('test', () => { gotFromIpcMain = true; resolve(); }));
  560. const ipcReceived = new Promise<boolean>(resolve => w.webContents.ipc.on('test', () => { resolve(gotFromIpcMain); }));
  561. defer(() => ipcMain.removeAllListeners('test'));
  562. w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
  563. // assert that they are delivered in the correct order
  564. expect(await ipcReceived).to.be.false();
  565. await ipcMainReceived;
  566. });
  567. it('overrides ipcMain handlers', async () => {
  568. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  569. w.loadURL('about:blank');
  570. w.webContents.ipc.handle('test', (_event, arg) => arg * 2);
  571. ipcMain.handle('test', () => { throw new Error('should not be called'); });
  572. defer(() => ipcMain.removeHandler('test'));
  573. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  574. expect(result).to.equal(42 * 2);
  575. });
  576. it('falls back to ipcMain handlers', async () => {
  577. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  578. w.loadURL('about:blank');
  579. ipcMain.handle('test', (_event, arg) => { return arg * 2; });
  580. defer(() => ipcMain.removeHandler('test'));
  581. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  582. expect(result).to.equal(42 * 2);
  583. });
  584. it('receives ipcs from child frames', async () => {
  585. const server = http.createServer((req, res) => {
  586. res.setHeader('content-type', 'text/html');
  587. res.end('');
  588. });
  589. await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  590. const port = (server.address() as AddressInfo).port;
  591. defer(() => {
  592. server.close();
  593. });
  594. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegrationInSubFrames: true, preload: path.resolve(fixturesPath, 'preload-expose-ipc.js') } });
  595. // Preloads don't run in about:blank windows, and file:// urls can't be loaded in iframes, so use a blank http page.
  596. await w.loadURL(`data:text/html,<iframe src="http://localhost:${port}"></iframe>`);
  597. w.webContents.mainFrame.frames[0].executeJavaScript('ipc.send(\'test\', 42)');
  598. const [, arg] = await emittedOnce(w.webContents.ipc, 'test');
  599. expect(arg).to.equal(42);
  600. });
  601. });
  602. describe('WebFrameMain.ipc', () => {
  603. afterEach(closeAllWindows);
  604. it('responds to ipc messages in the main frame', async () => {
  605. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  606. w.loadURL('about:blank');
  607. w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
  608. const [, arg] = await emittedOnce(w.webContents.mainFrame.ipc, 'test');
  609. expect(arg).to.equal(42);
  610. });
  611. it('responds to sync ipc messages in the main frame', async () => {
  612. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  613. w.loadURL('about:blank');
  614. w.webContents.mainFrame.ipc.on('test', (event, arg) => {
  615. event.returnValue = arg * 2;
  616. });
  617. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.sendSync(\'test\', 42)');
  618. expect(result).to.equal(42 * 2);
  619. });
  620. it('receives postMessage messages sent from the WebContents, w/ MessagePorts', async () => {
  621. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  622. w.loadURL('about:blank');
  623. w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.postMessage(\'test\', null, [(new MessageChannel).port1])');
  624. const [event] = await emittedOnce(w.webContents.mainFrame.ipc, 'test');
  625. expect(event.ports.length).to.equal(1);
  626. });
  627. it('handles invoke messages sent from the WebContents', async () => {
  628. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  629. w.loadURL('about:blank');
  630. w.webContents.mainFrame.ipc.handle('test', (_event, arg) => arg * 2);
  631. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  632. expect(result).to.equal(42 * 2);
  633. });
  634. it('cascades to WebContents and ipcMain', async () => {
  635. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  636. w.loadURL('about:blank');
  637. let gotFromIpcMain = false;
  638. let gotFromWebContents = false;
  639. const ipcMainReceived = new Promise<void>(resolve => ipcMain.on('test', () => { gotFromIpcMain = true; resolve(); }));
  640. const ipcWebContentsReceived = new Promise<boolean>(resolve => w.webContents.ipc.on('test', () => { gotFromWebContents = true; resolve(gotFromIpcMain); }));
  641. const ipcReceived = new Promise<boolean>(resolve => w.webContents.mainFrame.ipc.on('test', () => { resolve(gotFromWebContents); }));
  642. defer(() => ipcMain.removeAllListeners('test'));
  643. w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.send(\'test\', 42)');
  644. // assert that they are delivered in the correct order
  645. expect(await ipcReceived).to.be.false();
  646. expect(await ipcWebContentsReceived).to.be.false();
  647. await ipcMainReceived;
  648. });
  649. it('overrides ipcMain handlers', async () => {
  650. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  651. w.loadURL('about:blank');
  652. w.webContents.mainFrame.ipc.handle('test', (_event, arg) => arg * 2);
  653. ipcMain.handle('test', () => { throw new Error('should not be called'); });
  654. defer(() => ipcMain.removeHandler('test'));
  655. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  656. expect(result).to.equal(42 * 2);
  657. });
  658. it('overrides WebContents handlers', async () => {
  659. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  660. w.loadURL('about:blank');
  661. w.webContents.ipc.handle('test', () => { throw new Error('should not be called'); });
  662. w.webContents.mainFrame.ipc.handle('test', (_event, arg) => arg * 2);
  663. ipcMain.handle('test', () => { throw new Error('should not be called'); });
  664. defer(() => ipcMain.removeHandler('test'));
  665. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  666. expect(result).to.equal(42 * 2);
  667. });
  668. it('falls back to WebContents handlers', async () => {
  669. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  670. w.loadURL('about:blank');
  671. w.webContents.ipc.handle('test', (_event, arg) => { return arg * 2; });
  672. const result = await w.webContents.executeJavaScript('require(\'electron\').ipcRenderer.invoke(\'test\', 42)');
  673. expect(result).to.equal(42 * 2);
  674. });
  675. it('receives ipcs from child frames', async () => {
  676. const server = http.createServer((req, res) => {
  677. res.setHeader('content-type', 'text/html');
  678. res.end('');
  679. });
  680. await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  681. const port = (server.address() as AddressInfo).port;
  682. defer(() => {
  683. server.close();
  684. });
  685. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegrationInSubFrames: true, preload: path.resolve(fixturesPath, 'preload-expose-ipc.js') } });
  686. // Preloads don't run in about:blank windows, and file:// urls can't be loaded in iframes, so use a blank http page.
  687. await w.loadURL(`data:text/html,<iframe src="http://localhost:${port}"></iframe>`);
  688. w.webContents.mainFrame.frames[0].executeJavaScript('ipc.send(\'test\', 42)');
  689. w.webContents.mainFrame.ipc.on('test', () => { throw new Error('should not be called'); });
  690. const [, arg] = await emittedOnce(w.webContents.mainFrame.frames[0].ipc, 'test');
  691. expect(arg).to.equal(42);
  692. });
  693. });
  694. });