Skip to content

Commit 2c68bad

Browse files
authored
refactor: create IPC_MESSAGES enum for IPC message channels (electron#25694)
1 parent 8dfb1cf commit 2c68bad

26 files changed

+225
-126
lines changed

filenames.auto.gni

+8
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ auto_filenames = {
140140
"lib/common/api/module-list.ts",
141141
"lib/common/api/shell.ts",
142142
"lib/common/define-properties.ts",
143+
"lib/common/ipc-messages.ts",
144+
"lib/common/remote/ipc-messages.ts",
143145
"lib/common/type-utils.ts",
144146
"lib/common/web-view-events.ts",
145147
"lib/common/web-view-methods.ts",
@@ -247,7 +249,9 @@ auto_filenames = {
247249
"lib/common/api/shell.ts",
248250
"lib/common/define-properties.ts",
249251
"lib/common/init.ts",
252+
"lib/common/ipc-messages.ts",
250253
"lib/common/parse-features-string.ts",
254+
"lib/common/remote/ipc-messages.ts",
251255
"lib/common/reset-search-paths.ts",
252256
"lib/common/type-utils.ts",
253257
"lib/common/web-view-events.ts",
@@ -270,6 +274,8 @@ auto_filenames = {
270274
"lib/common/api/shell.ts",
271275
"lib/common/define-properties.ts",
272276
"lib/common/init.ts",
277+
"lib/common/ipc-messages.ts",
278+
"lib/common/remote/ipc-messages.ts",
273279
"lib/common/reset-search-paths.ts",
274280
"lib/common/type-utils.ts",
275281
"lib/common/web-view-events.ts",
@@ -314,6 +320,8 @@ auto_filenames = {
314320
"lib/common/api/shell.ts",
315321
"lib/common/define-properties.ts",
316322
"lib/common/init.ts",
323+
"lib/common/ipc-messages.ts",
324+
"lib/common/remote/ipc-messages.ts",
317325
"lib/common/reset-search-paths.ts",
318326
"lib/common/type-utils.ts",
319327
"lib/common/webpack-globals-provider.ts",

lib/browser/api/web-contents.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
99
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
1010
import { parseFeatures } from '@electron/internal/common/parse-features-string';
1111
import { MessagePortMain } from '@electron/internal/browser/message-port-main';
12+
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
1213

1314
// session is not used here, the purpose is to make sure session is initalized
1415
// before the webContents module.
@@ -196,7 +197,7 @@ const webFrameMethods = [
196197

197198
for (const method of webFrameMethods) {
198199
WebContents.prototype[method] = function (...args: any[]): Promise<any> {
199-
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args);
200+
return ipcMainUtils.invokeInWebContents(this, false, IPC_MESSAGES.RENDERER_WEB_FRAME_METHOD, method, ...args);
200201
};
201202
}
202203

@@ -214,11 +215,11 @@ const waitTillCanExecuteJavaScript = async (webContents: Electron.WebContents) =
214215
// WebContents has been loaded.
215216
WebContents.prototype.executeJavaScript = async function (code, hasUserGesture) {
216217
await waitTillCanExecuteJavaScript(this);
217-
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScript', String(code), !!hasUserGesture);
218+
return ipcMainUtils.invokeInWebContents(this, false, IPC_MESSAGES.RENDERER_WEB_FRAME_METHOD, 'executeJavaScript', String(code), !!hasUserGesture);
218219
};
219220
WebContents.prototype.executeJavaScriptInIsolatedWorld = async function (worldId, code, hasUserGesture) {
220221
await waitTillCanExecuteJavaScript(this);
221-
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScriptInIsolatedWorld', worldId, code, !!hasUserGesture);
222+
return ipcMainUtils.invokeInWebContents(this, false, IPC_MESSAGES.RENDERER_WEB_FRAME_METHOD, 'executeJavaScriptInIsolatedWorld', worldId, code, !!hasUserGesture);
222223
};
223224

224225
// Translate the options of printToPDF.

lib/browser/devtools.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as url from 'url';
44

55
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
66
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
7+
import { IPC_MESSAGES } from '@electron/internal//common/ipc-messages';
78

89
const convertToMenuTemplate = function (items: ContextMenuItem[], handler: (id: number) => void) {
910
return items.map(function (item) {
@@ -60,7 +61,7 @@ const assertChromeDevTools = function (contents: Electron.WebContents, api: stri
6061
}
6162
};
6263

63-
ipcMainInternal.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items: ContextMenuItem[], isEditMenu: boolean) {
64+
ipcMainInternal.handle(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, function (event, items: ContextMenuItem[], isEditMenu: boolean) {
6465
return new Promise(resolve => {
6566
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()');
6667

@@ -72,7 +73,7 @@ ipcMainInternal.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items
7273
});
7374
});
7475

75-
ipcMainInternal.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event) {
76+
ipcMainInternal.handle(IPC_MESSAGES.INSPECTOR_SELECT_FILE, async function (event) {
7677
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()');
7778

7879
const result = await dialog.showOpenDialog({});
@@ -84,7 +85,7 @@ ipcMainInternal.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event)
8485
return [path, data];
8586
});
8687

87-
ipcMainUtils.handleSync('ELECTRON_INSPECTOR_CONFIRM', async function (event, message: string = '', title: string = '') {
88+
ipcMainUtils.handleSync(IPC_MESSAGES.INSPECTOR_CONFIRM, async function (event, message: string = '', title: string = '') {
8889
assertChromeDevTools(event.sender, 'window.confirm()');
8990

9091
const options = {

lib/browser/guest-view-manager.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { parseWebViewWebPreferences } from '@electron/internal/common/parse-feat
55
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods';
66
import { webViewEvents } from '@electron/internal/common/web-view-events';
77
import { serialize } from '@electron/internal/common/type-utils';
8+
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
89

910
interface GuestInstance {
1011
elementInstanceId?: number;
@@ -83,7 +84,7 @@ const createGuest = function (embedder: Electron.WebContents, params: Record<str
8384
// Dispatch events to embedder.
8485
const fn = function (event: string) {
8586
guest.on(event as any, function (_, ...args: any[]) {
86-
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT', event, ...args);
87+
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, ...args);
8788
});
8889
};
8990
for (const event of supportedWebViewEvents) {
@@ -93,22 +94,22 @@ const createGuest = function (embedder: Electron.WebContents, params: Record<str
9394
}
9495

9596
guest.on('new-window', function (event, url, frameName, disposition, options, additionalFeatures, referrer) {
96-
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT', 'new-window', url,
97+
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', url,
9798
frameName, disposition, sanitizeOptionsForGuest(options),
9899
additionalFeatures, referrer);
99100
});
100101

101102
// Dispatch guest's IPC messages to embedder.
102103
guest.on('ipc-message-host' as any, function (_: Electron.Event, channel: string, args: any[]) {
103-
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE', channel, ...args);
104+
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE, channel, ...args);
104105
});
105106

106107
// Notify guest of embedder window visibility when it is ready
107108
// FIXME Remove once https://github.com/electron/electron/issues/6828 is fixed
108109
guest.on('dom-ready', function () {
109110
const guestInstance = guestInstances[guestInstanceId];
110111
if (guestInstance != null && guestInstance.visibilityState != null) {
111-
guest._sendInternal('ELECTRON_GUEST_INSTANCE_VISIBILITY_CHANGE', guestInstance.visibilityState);
112+
guest._sendInternal(IPC_MESSAGES.GUEST_INSTANCE_VISIBILITY_CHANGE, guestInstance.visibilityState);
112113
}
113114
});
114115

@@ -152,7 +153,7 @@ const attachGuest = function (event: Electron.IpcMainInvokeEvent,
152153
// Remove guest from embedder if moving across web views
153154
if (guest.viewInstanceId !== params.instanceId) {
154155
webViewManager.removeGuest(guestInstance.embedder, guestInstanceId);
155-
guestInstance.embedder._sendInternal(`ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-${guest.viewInstanceId}`);
156+
guestInstance.embedder._sendInternal(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DESTROY_GUEST}-${guest.viewInstanceId}`);
156157
}
157158
}
158159

@@ -253,7 +254,7 @@ const watchEmbedder = function (embedder: Electron.WebContents) {
253254
const guestInstance = guestInstances[guestInstanceId];
254255
guestInstance.visibilityState = visibilityState;
255256
if (guestInstance.embedder === embedder) {
256-
guestInstance.guest._sendInternal('ELECTRON_GUEST_INSTANCE_VISIBILITY_CHANGE', visibilityState);
257+
guestInstance.guest._sendInternal(IPC_MESSAGES.GUEST_INSTANCE_VISIBILITY_CHANGE, visibilityState);
257258
}
258259
}
259260
};
@@ -305,24 +306,24 @@ const handleMessageSync = function (channel: string, handler: (event: ElectronIn
305306
ipcMainUtils.handleSync(channel, makeSafeHandler(channel, handler));
306307
};
307308

308-
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', function (event, params) {
309+
handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CREATE_GUEST, function (event, params) {
309310
return createGuest(event.sender, params);
310311
});
311312

312-
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', function (event, embedderFrameId: number, elementInstanceId: number, guestInstanceId: number, params) {
313+
handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_ATTACH_GUEST, function (event, embedderFrameId: number, elementInstanceId: number, guestInstanceId: number, params) {
313314
try {
314315
attachGuest(event, embedderFrameId, elementInstanceId, guestInstanceId, params);
315316
} catch (error) {
316317
console.error(`Guest attach failed: ${error}`);
317318
}
318319
});
319320

320-
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_DETACH_GUEST', function (event, guestInstanceId: number) {
321+
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_DETACH_GUEST, function (event, guestInstanceId: number) {
321322
return detachGuest(event.sender, guestInstanceId);
322323
});
323324

324325
// this message is sent by the actual <webview>
325-
ipcMainInternal.on('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', function (event: ElectronInternal.IpcMainInternalEvent, focus: boolean, guestInstanceId: number) {
326+
ipcMainInternal.on(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, function (event: ElectronInternal.IpcMainInternalEvent, focus: boolean, guestInstanceId: number) {
326327
const guest = getGuest(guestInstanceId);
327328
if (guest === event.sender) {
328329
event.sender.emit('focus-change', {}, focus, guestInstanceId);
@@ -331,7 +332,7 @@ ipcMainInternal.on('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', function (event:
331332
}
332333
});
333334

334-
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId: number, method: string, args: any[]) {
335+
handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, function (event, guestInstanceId: number, method: string, args: any[]) {
335336
const guest = getGuestForWebContents(guestInstanceId, event.sender);
336337
if (!asyncMethods.has(method)) {
337338
throw new Error(`Invalid method: ${method}`);
@@ -340,7 +341,7 @@ handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstance
340341
return (guest as any)[method](...args);
341342
});
342343

343-
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId: number, method: string, args: any[]) {
344+
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, function (event, guestInstanceId: number, method: string, args: any[]) {
344345
const guest = getGuestForWebContents(guestInstanceId, event.sender);
345346
if (!syncMethods.has(method)) {
346347
throw new Error(`Invalid method: ${method}`);
@@ -349,7 +350,7 @@ handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInst
349350
return (guest as any)[method](...args);
350351
});
351352

352-
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', function (event, guestInstanceId: number, property: string) {
353+
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_PROPERTY_GET, function (event, guestInstanceId: number, property: string) {
353354
const guest = getGuestForWebContents(guestInstanceId, event.sender);
354355
if (!properties.has(property)) {
355356
throw new Error(`Invalid property: ${property}`);
@@ -358,7 +359,7 @@ handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', function (event, g
358359
return (guest as any)[property];
359360
});
360361

361-
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', function (event, guestInstanceId: number, property: string, val: any) {
362+
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_PROPERTY_SET, function (event, guestInstanceId: number, property: string, val: any) {
362363
const guest = getGuestForWebContents(guestInstanceId, event.sender);
363364
if (!properties.has(property)) {
364365
throw new Error(`Invalid property: ${property}`);
@@ -367,7 +368,7 @@ handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', function (event, g
367368
(guest as any)[property] = val;
368369
});
369370

370-
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', async function (event, guestInstanceId: number, args: any[]) {
371+
handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CAPTURE_PAGE, async function (event, guestInstanceId: number, args: any[]) {
371372
const guest = getGuestForWebContents(guestInstanceId, event.sender);
372373

373374
return serialize(await guest.capturePage(...args));

lib/browser/guest-window-manager.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as electron from 'electron/main';
22
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
33
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
44
import { parseFeatures } from '@electron/internal/common/parse-features-string';
5+
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
56

67
const { isSameOrigin } = process._linkedBinding('electron_common_v8_util');
78

@@ -132,7 +133,7 @@ const setupGuest = function (embedder: Electron.WebContents, frameName: string,
132133
guest.destroy();
133134
};
134135
const closedByUser = function () {
135-
embedder._sendInternal('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_' + guestId);
136+
embedder._sendInternal(`${IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_CLOSED}_${guestId}`);
136137
embedder.removeListener('current-render-view-deleted' as any, closedByEmbedder);
137138
};
138139
embedder.once('current-render-view-deleted' as any, closedByEmbedder);
@@ -219,12 +220,12 @@ const canAccessWindow = function (sender: Electron.WebContents, target: Electron
219220
};
220221

221222
// Routed window.open messages with raw options
222-
ipcMainInternal.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, url: string, frameName: string, features: string) => {
223+
ipcMainInternal.on(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_OPEN, (event, url: string, frameName: string, features: string) => {
223224
// This should only be allowed for senders that have nativeWindowOpen: false
224225
const lastWebPreferences = event.sender.getLastWebPreferences();
225226
if (lastWebPreferences.nativeWindowOpen || lastWebPreferences.sandbox) {
226227
event.returnValue = null;
227-
throw new Error('GUEST_WINDOW_MANAGER_WINDOW_OPEN denied: expected native window.open');
228+
throw new Error(`${IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_OPEN} denied: expected native window.open`);
228229
}
229230
if (url == null || url === '') url = 'about:blank';
230231
if (frameName == null) frameName = '';
@@ -299,7 +300,7 @@ const windowMethods = new Set([
299300
'blur'
300301
]);
301302

302-
handleMessage('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestContents, method: string, ...args: any[]) => {
303+
handleMessage(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_METHOD, (event, guestContents, method: string, ...args: any[]) => {
303304
securityCheck(event.sender, guestContents, canAccessWindow);
304305

305306
if (!windowMethods.has(method)) {
@@ -310,7 +311,7 @@ handleMessage('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestConten
310311
return (getGuestWindow(guestContents) as any)[method](...args);
311312
});
312313

313-
handleMessage('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestContents, message, targetOrigin, sourceOrigin) => {
314+
handleMessage(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE, (event, guestContents, message, targetOrigin, sourceOrigin) => {
314315
if (targetOrigin == null) {
315316
targetOrigin = '*';
316317
}
@@ -322,7 +323,7 @@ handleMessage('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestC
322323

323324
if (targetOrigin === '*' || isSameOrigin(guestContents.getURL(), targetOrigin)) {
324325
const sourceId = event.sender.id;
325-
guestContents._sendInternal('ELECTRON_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin);
326+
guestContents._sendInternal(IPC_MESSAGES.GUEST_WINDOW_POSTMESSAGE, sourceId, message, sourceOrigin);
326327
}
327328
});
328329

@@ -332,7 +333,7 @@ const webContentsMethodsAsync = new Set([
332333
'print'
333334
]);
334335

335-
handleMessage('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestContents, method: string, ...args: any[]) => {
336+
handleMessage(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD, (event, guestContents, method: string, ...args: any[]) => {
336337
securityCheck(event.sender, guestContents, canAccessWindow);
337338

338339
if (!webContentsMethodsAsync.has(method)) {
@@ -347,7 +348,7 @@ const webContentsMethodsSync = new Set([
347348
'getURL'
348349
]);
349350

350-
handleMessageSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestContents, method: string, ...args: any[]) => {
351+
handleMessageSync(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD, (event, guestContents, method: string, ...args: any[]) => {
351352
securityCheck(event.sender, guestContents, canAccessWindow);
352353

353354
if (!webContentsMethodsSync.has(method)) {

lib/browser/navigation-controller.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
22
import type { WebContents, LoadURLOptions } from 'electron/main';
33
import { EventEmitter } from 'events';
4+
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
45

56
// The history operation in renderer is redirected to browser.
6-
ipcMainInternal.on('ELECTRON_NAVIGATION_CONTROLLER_GO_BACK', function (event) {
7+
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_BACK, function (event) {
78
event.sender.goBack();
89
});
910

10-
ipcMainInternal.on('ELECTRON_NAVIGATION_CONTROLLER_GO_FORWARD', function (event) {
11+
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_FORWARD, function (event) {
1112
event.sender.goForward();
1213
});
1314

14-
ipcMainInternal.on('ELECTRON_NAVIGATION_CONTROLLER_GO_TO_OFFSET', function (event, offset) {
15+
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_TO_OFFSET, function (event, offset) {
1516
event.sender.goToOffset(offset);
1617
});
1718

18-
ipcMainInternal.on('ELECTRON_NAVIGATION_CONTROLLER_LENGTH', function (event) {
19+
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_LENGTH, function (event) {
1920
event.returnValue = event.sender.length();
2021
});
2122

0 commit comments

Comments
 (0)