Skip to content

Commit 969f46a

Browse files
chore: remove IPC hiddens (electron#23720)
1 parent c6f4573 commit 969f46a

4 files changed

Lines changed: 61 additions & 75 deletions

File tree

lib/renderer/api/ipc-renderer.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
1+
import { EventEmitter } from 'events';
2+
13
const { ipc } = process.electronBinding('ipc');
2-
const v8Util = process.electronBinding('v8_util');
34

4-
// Created by init.js.
5-
const ipcRenderer = v8Util.getHiddenValue<Electron.IpcRenderer>(global, 'ipc');
65
const internal = false;
76

8-
// TODO(MarshallOfSound): Remove if statement when isolated_bundle and content_script_bundle are gone
9-
if (!ipcRenderer.send) {
10-
ipcRenderer.send = function (channel, ...args) {
11-
return ipc.send(internal, channel, args);
12-
};
13-
14-
ipcRenderer.sendSync = function (channel, ...args) {
15-
return ipc.sendSync(internal, channel, args)[0];
16-
};
17-
18-
ipcRenderer.sendToHost = function (channel, ...args) {
19-
return ipc.sendToHost(channel, args);
20-
};
21-
22-
ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
23-
return ipc.sendTo(internal, false, webContentsId, channel, args);
24-
};
25-
26-
ipcRenderer.invoke = async function (channel, ...args) {
27-
const { error, result } = await ipc.invoke(internal, channel, args);
28-
if (error) {
29-
throw new Error(`Error invoking remote method '${channel}': ${error}`);
30-
}
31-
return result;
32-
};
33-
34-
ipcRenderer.postMessage = function (channel: string, message: any, transferables: any) {
35-
return ipc.postMessage(channel, message, transferables);
36-
};
37-
}
7+
const ipcRenderer = new EventEmitter() as Electron.IpcRenderer;
8+
ipcRenderer.send = function (channel, ...args) {
9+
return ipc.send(internal, channel, args);
10+
};
11+
12+
ipcRenderer.sendSync = function (channel, ...args) {
13+
return ipc.sendSync(internal, channel, args)[0];
14+
};
15+
16+
ipcRenderer.sendToHost = function (channel, ...args) {
17+
return ipc.sendToHost(channel, args);
18+
};
19+
20+
ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
21+
return ipc.sendTo(internal, false, webContentsId, channel, args);
22+
};
23+
24+
ipcRenderer.invoke = async function (channel, ...args) {
25+
const { error, result } = await ipc.invoke(internal, channel, args);
26+
if (error) {
27+
throw new Error(`Error invoking remote method '${channel}': ${error}`);
28+
}
29+
return result;
30+
};
31+
32+
ipcRenderer.postMessage = function (channel: string, message: any, transferables: any) {
33+
return ipc.postMessage(channel, message, transferables);
34+
};
3835

3936
export default ipcRenderer;

lib/renderer/init.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { EventEmitter } from 'events';
21
import * as path from 'path';
32

43
const Module = require('module');
@@ -39,20 +38,17 @@ require('@electron/internal/common/init');
3938
// The global variable will be used by ipc for event dispatching
4039
const v8Util = process.electronBinding('v8_util');
4140

42-
const ipcEmitter = new EventEmitter();
43-
const ipcInternalEmitter = new EventEmitter();
44-
v8Util.setHiddenValue(global, 'ipc', ipcEmitter);
45-
v8Util.setHiddenValue(global, 'ipc-internal', ipcInternalEmitter);
41+
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
42+
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;
4643

4744
v8Util.setHiddenValue(global, 'ipcNative', {
4845
onMessage (internal: boolean, channel: string, ports: any[], args: any[], senderId: number) {
49-
const sender = internal ? ipcInternalEmitter : ipcEmitter;
46+
const sender = internal ? ipcRendererInternal : ipcRenderer;
5047
sender.emit(channel, { sender, senderId, ports }, ...args);
5148
}
5249
});
5350

5451
// Use electron module after everything is ready.
55-
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
5652
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init');
5753
webFrameInit();
5854

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1+
import { EventEmitter } from 'events';
2+
13
const { ipc } = process.electronBinding('ipc');
2-
const v8Util = process.electronBinding('v8_util');
34

4-
// Created by init.js.
5-
export const ipcRendererInternal = v8Util.getHiddenValue<Electron.IpcRendererInternal>(global, 'ipc-internal');
65
const internal = true;
76

8-
// TODO(MarshallOfSound): Remove if statement when isolated_bundle and content_script_bundle are gone
9-
if (!ipcRendererInternal.send) {
10-
ipcRendererInternal.send = function (channel, ...args) {
11-
return ipc.send(internal, channel, args);
12-
};
13-
14-
ipcRendererInternal.sendSync = function (channel, ...args) {
15-
return ipc.sendSync(internal, channel, args)[0];
16-
};
17-
18-
ipcRendererInternal.sendTo = function (webContentsId, channel, ...args) {
19-
return ipc.sendTo(internal, false, webContentsId, channel, args);
20-
};
21-
22-
ipcRendererInternal.sendToAll = function (webContentsId, channel, ...args) {
23-
return ipc.sendTo(internal, true, webContentsId, channel, args);
24-
};
25-
26-
ipcRendererInternal.invoke = async function<T> (channel: string, ...args: any[]) {
27-
const { error, result } = await ipc.invoke<T>(internal, channel, args);
28-
if (error) {
29-
throw new Error(`Error invoking remote method '${channel}': ${error}`);
30-
}
31-
return result;
32-
};
33-
}
7+
const ipcRendererInternal = new EventEmitter() as any as Electron.IpcRendererInternal;
8+
ipcRendererInternal.send = function (channel, ...args) {
9+
return ipc.send(internal, channel, args);
10+
};
11+
12+
ipcRendererInternal.sendSync = function (channel, ...args) {
13+
return ipc.sendSync(internal, channel, args)[0];
14+
};
15+
16+
ipcRendererInternal.sendTo = function (webContentsId, channel, ...args) {
17+
return ipc.sendTo(internal, false, webContentsId, channel, args);
18+
};
19+
20+
ipcRendererInternal.sendToAll = function (webContentsId, channel, ...args) {
21+
return ipc.sendTo(internal, true, webContentsId, channel, args);
22+
};
23+
24+
ipcRendererInternal.invoke = async function<T> (channel: string, ...args: any[]) {
25+
const { error, result } = await ipc.invoke<T>(internal, channel, args);
26+
if (error) {
27+
throw new Error(`Error invoking remote method '${channel}': ${error}`);
28+
}
29+
return result;
30+
};
31+
32+
export { ipcRendererInternal };

lib/sandboxed_renderer/init.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ const v8Util = process.electronBinding('v8_util');
1111
// Expose Buffer shim as a hidden value. This is used by C++ code to
1212
// deserialize Buffer instances sent from browser process.
1313
v8Util.setHiddenValue(global, 'Buffer', Buffer);
14-
// The `lib/renderer/api/ipc-renderer.ts` module looks for the ipc object in the
15-
// "ipc" hidden value
16-
v8Util.setHiddenValue(global, 'ipc', new EventEmitter());
17-
// The `lib/renderer/ipc-renderer-internal.ts` module looks for the ipc object in the
18-
// "ipc-internal" hidden value
19-
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter());
2014
// The process object created by webpack is not an event emitter, fix it so
2115
// the API is more compatible with non-sandboxed renderers.
2216
for (const prop of Object.keys(EventEmitter.prototype) as (keyof typeof process)[]) {

0 commit comments

Comments
 (0)