Skip to content

Commit 04aafcc

Browse files
miniaknornagon
andauthored
refactor: simplify <webview> event dispatch (electron#30458)
* refactor: simplify <webview> event dispatch * Update lib/browser/guest-view-manager.ts Co-authored-by: Jeremy Rose <jeremya@chromium.org> * remove undocumented new-window event properties Co-authored-by: Jeremy Rose <jeremya@chromium.org>
1 parent ff128a3 commit 04aafcc

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

filenames.auto.gni

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ auto_filenames = {
138138
"lib/common/define-properties.ts",
139139
"lib/common/ipc-messages.ts",
140140
"lib/common/type-utils.ts",
141-
"lib/common/web-view-events.ts",
142141
"lib/common/web-view-methods.ts",
143142
"lib/renderer/api/context-bridge.ts",
144143
"lib/renderer/api/crash-reporter.ts",
@@ -270,7 +269,6 @@ auto_filenames = {
270269
"lib/common/ipc-messages.ts",
271270
"lib/common/reset-search-paths.ts",
272271
"lib/common/type-utils.ts",
273-
"lib/common/web-view-events.ts",
274272
"lib/common/web-view-methods.ts",
275273
"lib/common/webpack-provider.ts",
276274
"lib/renderer/api/context-bridge.ts",

lib/browser/guest-view-manager.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,33 @@ const createGuest = function (embedder: Electron.WebContents, embedderFrameId: n
136136
}
137137
};
138138

139-
// Dispatch events to embedder.
140-
const fn = function (event: string) {
141-
guest.on(event as any, function (_, ...args: any[]) {
142-
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, ...args);
139+
const makeProps = (eventKey: string, args: any[]) => {
140+
const props: Record<string, any> = {};
141+
webViewEvents[eventKey].forEach((prop, index) => {
142+
props[prop] = args[index];
143143
});
144+
return props;
144145
};
146+
147+
// Dispatch events to embedder.
145148
for (const event of supportedWebViewEvents) {
146-
if (event !== 'new-window') {
147-
fn(event);
148-
}
149+
guest.on(event as any, function (_, ...args: any[]) {
150+
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, makeProps(event, args));
151+
});
149152
}
150153

151-
guest.on('new-window', function (event, url, frameName, disposition, options, additionalFeatures, referrer) {
152-
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', url,
153-
frameName, disposition, sanitizeOptionsForGuest(options),
154-
additionalFeatures, referrer);
154+
guest.on('new-window', function (event, url, frameName, disposition, options) {
155+
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', {
156+
url,
157+
frameName,
158+
disposition,
159+
options: sanitizeOptionsForGuest(options)
160+
});
155161
});
156162

157163
// Dispatch guest's IPC messages to embedder.
158164
guest.on('ipc-message-host' as any, function (_: Electron.Event, channel: string, args: any[]) {
159-
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE, channel, ...args);
165+
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'ipc-message', { channel, args });
160166
});
161167

162168
// Notify guest of embedder window visibility when it is ready

lib/common/ipc-messages.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export const enum IPC_MESSAGES {
88
GUEST_INSTANCE_VISIBILITY_CHANGE = 'GUEST_INSTANCE_VISIBILITY_CHANGE',
99

1010
GUEST_VIEW_INTERNAL_DISPATCH_EVENT = 'GUEST_VIEW_INTERNAL_DISPATCH_EVENT',
11-
GUEST_VIEW_INTERNAL_IPC_MESSAGE = 'GUEST_VIEW_INTERNAL_IPC_MESSAGE',
1211

1312
GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST = 'GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST',
1413
GUEST_VIEW_MANAGER_DETACH_GUEST = 'GUEST_VIEW_MANAGER_DETACH_GUEST',

lib/common/web-view-events.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const webViewEvents: Record<string, readonly string[]> = {
1212
'devtools-opened': [],
1313
'devtools-closed': [],
1414
'devtools-focused': [],
15-
'new-window': ['url', 'frameName', 'disposition', 'options'],
1615
'will-navigate': ['url'],
1716
'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
1817
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],

lib/renderer/web-view/guest-view-internal.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
22
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
3-
import { webViewEvents } from '@electron/internal/common/web-view-events';
43
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
54

65
const { mainFrame: webFrame } = process._linkedBinding('electron_renderer_web_frame');
@@ -13,32 +12,18 @@ const DEPRECATED_EVENTS: Record<string, string> = {
1312
'page-title-updated': 'page-title-set'
1413
} as const;
1514

16-
const dispatchEvent = function (delegate: GuestViewDelegate, eventName: string, eventKey: string, ...args: Array<any>) {
17-
if (DEPRECATED_EVENTS[eventName] != null) {
18-
dispatchEvent(delegate, DEPRECATED_EVENTS[eventName], eventKey, ...args);
19-
}
20-
21-
const props: Record<string, any> = {};
22-
webViewEvents[eventKey].forEach((prop, index) => {
23-
props[prop] = args[index];
24-
});
25-
26-
delegate.dispatchEvent(eventName, props);
27-
};
28-
2915
export function registerEvents (viewInstanceId: number, delegate: GuestViewDelegate) {
30-
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, ...args) {
31-
dispatchEvent(delegate, eventName, eventName, ...args);
32-
});
16+
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, props) {
17+
if (DEPRECATED_EVENTS[eventName] != null) {
18+
delegate.dispatchEvent(DEPRECATED_EVENTS[eventName], props);
19+
}
3320

34-
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`, function (event, channel, ...args) {
35-
delegate.dispatchEvent('ipc-message', { channel, args });
21+
delegate.dispatchEvent(eventName, props);
3622
});
3723
}
3824

3925
export function deregisterEvents (viewInstanceId: number) {
4026
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`);
41-
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`);
4227
}
4328

4429
export function createGuest (iframe: HTMLIFrameElement, elementInstanceId: number, params: Record<string, any>): Promise<number> {

0 commit comments

Comments
 (0)