chore: remove IPC hiddens (#23720)

This commit is contained in:
Samuel Attard 2020-06-02 02:33:06 -07:00 committed by GitHub
parent c6f4573a13
commit 969f46a48f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 68 deletions

View file

@ -1,39 +1,36 @@
const { ipc } = process.electronBinding('ipc'); import { EventEmitter } from 'events';
const v8Util = process.electronBinding('v8_util');
const { ipc } = process.electronBinding('ipc');
// Created by init.js.
const ipcRenderer = v8Util.getHiddenValue<Electron.IpcRenderer>(global, 'ipc');
const internal = false; const internal = false;
// TODO(MarshallOfSound): Remove if statement when isolated_bundle and content_script_bundle are gone const ipcRenderer = new EventEmitter() as Electron.IpcRenderer;
if (!ipcRenderer.send) { ipcRenderer.send = function (channel, ...args) {
ipcRenderer.send = function (channel, ...args) { return ipc.send(internal, channel, args);
return ipc.send(internal, channel, args); };
};
ipcRenderer.sendSync = function (channel, ...args) { ipcRenderer.sendSync = function (channel, ...args) {
return ipc.sendSync(internal, channel, args)[0]; return ipc.sendSync(internal, channel, args)[0];
}; };
ipcRenderer.sendToHost = function (channel, ...args) { ipcRenderer.sendToHost = function (channel, ...args) {
return ipc.sendToHost(channel, args); return ipc.sendToHost(channel, args);
}; };
ipcRenderer.sendTo = function (webContentsId, channel, ...args) { ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
return ipc.sendTo(internal, false, webContentsId, channel, args); return ipc.sendTo(internal, false, webContentsId, channel, args);
}; };
ipcRenderer.invoke = async function (channel, ...args) { ipcRenderer.invoke = async function (channel, ...args) {
const { error, result } = await ipc.invoke(internal, channel, args); const { error, result } = await ipc.invoke(internal, channel, args);
if (error) { if (error) {
throw new Error(`Error invoking remote method '${channel}': ${error}`); throw new Error(`Error invoking remote method '${channel}': ${error}`);
} }
return result; return result;
}; };
ipcRenderer.postMessage = function (channel: string, message: any, transferables: any) { ipcRenderer.postMessage = function (channel: string, message: any, transferables: any) {
return ipc.postMessage(channel, message, transferables); return ipc.postMessage(channel, message, transferables);
}; };
}
export default ipcRenderer; export default ipcRenderer;

View file

@ -1,4 +1,3 @@
import { EventEmitter } from 'events';
import * as path from 'path'; import * as path from 'path';
const Module = require('module'); const Module = require('module');
@ -39,20 +38,17 @@ require('@electron/internal/common/init');
// The global variable will be used by ipc for event dispatching // The global variable will be used by ipc for event dispatching
const v8Util = process.electronBinding('v8_util'); const v8Util = process.electronBinding('v8_util');
const ipcEmitter = new EventEmitter(); const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
const ipcInternalEmitter = new EventEmitter(); const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;
v8Util.setHiddenValue(global, 'ipc', ipcEmitter);
v8Util.setHiddenValue(global, 'ipc-internal', ipcInternalEmitter);
v8Util.setHiddenValue(global, 'ipcNative', { v8Util.setHiddenValue(global, 'ipcNative', {
onMessage (internal: boolean, channel: string, ports: any[], args: any[], senderId: number) { onMessage (internal: boolean, channel: string, ports: any[], args: any[], senderId: number) {
const sender = internal ? ipcInternalEmitter : ipcEmitter; const sender = internal ? ipcRendererInternal : ipcRenderer;
sender.emit(channel, { sender, senderId, ports }, ...args); sender.emit(channel, { sender, senderId, ports }, ...args);
} }
}); });
// Use electron module after everything is ready. // Use electron module after everything is ready.
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init'); const { webFrameInit } = require('@electron/internal/renderer/web-frame-init');
webFrameInit(); webFrameInit();

View file

@ -1,33 +1,32 @@
const { ipc } = process.electronBinding('ipc'); import { EventEmitter } from 'events';
const v8Util = process.electronBinding('v8_util');
const { ipc } = process.electronBinding('ipc');
// Created by init.js.
export const ipcRendererInternal = v8Util.getHiddenValue<Electron.IpcRendererInternal>(global, 'ipc-internal');
const internal = true; const internal = true;
// TODO(MarshallOfSound): Remove if statement when isolated_bundle and content_script_bundle are gone const ipcRendererInternal = new EventEmitter() as any as Electron.IpcRendererInternal;
if (!ipcRendererInternal.send) { ipcRendererInternal.send = function (channel, ...args) {
ipcRendererInternal.send = function (channel, ...args) { return ipc.send(internal, channel, args);
return ipc.send(internal, channel, args); };
};
ipcRendererInternal.sendSync = function (channel, ...args) { ipcRendererInternal.sendSync = function (channel, ...args) {
return ipc.sendSync(internal, channel, args)[0]; return ipc.sendSync(internal, channel, args)[0];
}; };
ipcRendererInternal.sendTo = function (webContentsId, channel, ...args) { ipcRendererInternal.sendTo = function (webContentsId, channel, ...args) {
return ipc.sendTo(internal, false, webContentsId, channel, args); return ipc.sendTo(internal, false, webContentsId, channel, args);
}; };
ipcRendererInternal.sendToAll = function (webContentsId, channel, ...args) { ipcRendererInternal.sendToAll = function (webContentsId, channel, ...args) {
return ipc.sendTo(internal, true, webContentsId, channel, args); return ipc.sendTo(internal, true, webContentsId, channel, args);
}; };
ipcRendererInternal.invoke = async function<T> (channel: string, ...args: any[]) { ipcRendererInternal.invoke = async function<T> (channel: string, ...args: any[]) {
const { error, result } = await ipc.invoke<T>(internal, channel, args); const { error, result } = await ipc.invoke<T>(internal, channel, args);
if (error) { if (error) {
throw new Error(`Error invoking remote method '${channel}': ${error}`); throw new Error(`Error invoking remote method '${channel}': ${error}`);
} }
return result; return result;
}; };
}
export { ipcRendererInternal };

View file

@ -11,12 +11,6 @@ const v8Util = process.electronBinding('v8_util');
// Expose Buffer shim as a hidden value. This is used by C++ code to // Expose Buffer shim as a hidden value. This is used by C++ code to
// deserialize Buffer instances sent from browser process. // deserialize Buffer instances sent from browser process.
v8Util.setHiddenValue(global, 'Buffer', Buffer); v8Util.setHiddenValue(global, 'Buffer', Buffer);
// The `lib/renderer/api/ipc-renderer.ts` module looks for the ipc object in the
// "ipc" hidden value
v8Util.setHiddenValue(global, 'ipc', new EventEmitter());
// The `lib/renderer/ipc-renderer-internal.ts` module looks for the ipc object in the
// "ipc-internal" hidden value
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter());
// The process object created by webpack is not an event emitter, fix it so // The process object created by webpack is not an event emitter, fix it so
// the API is more compatible with non-sandboxed renderers. // the API is more compatible with non-sandboxed renderers.
for (const prop of Object.keys(EventEmitter.prototype) as (keyof typeof process)[]) { for (const prop of Object.keys(EventEmitter.prototype) as (keyof typeof process)[]) {