refactor: simplify <webview> event dispatch (#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>
This commit is contained in:
parent
ff128a32d9
commit
04aafcc5ef
5 changed files with 23 additions and 36 deletions
|
@ -138,7 +138,6 @@ auto_filenames = {
|
||||||
"lib/common/define-properties.ts",
|
"lib/common/define-properties.ts",
|
||||||
"lib/common/ipc-messages.ts",
|
"lib/common/ipc-messages.ts",
|
||||||
"lib/common/type-utils.ts",
|
"lib/common/type-utils.ts",
|
||||||
"lib/common/web-view-events.ts",
|
|
||||||
"lib/common/web-view-methods.ts",
|
"lib/common/web-view-methods.ts",
|
||||||
"lib/renderer/api/context-bridge.ts",
|
"lib/renderer/api/context-bridge.ts",
|
||||||
"lib/renderer/api/crash-reporter.ts",
|
"lib/renderer/api/crash-reporter.ts",
|
||||||
|
@ -270,7 +269,6 @@ auto_filenames = {
|
||||||
"lib/common/ipc-messages.ts",
|
"lib/common/ipc-messages.ts",
|
||||||
"lib/common/reset-search-paths.ts",
|
"lib/common/reset-search-paths.ts",
|
||||||
"lib/common/type-utils.ts",
|
"lib/common/type-utils.ts",
|
||||||
"lib/common/web-view-events.ts",
|
|
||||||
"lib/common/web-view-methods.ts",
|
"lib/common/web-view-methods.ts",
|
||||||
"lib/common/webpack-provider.ts",
|
"lib/common/webpack-provider.ts",
|
||||||
"lib/renderer/api/context-bridge.ts",
|
"lib/renderer/api/context-bridge.ts",
|
||||||
|
|
|
@ -136,27 +136,33 @@ const createGuest = function (embedder: Electron.WebContents, embedderFrameId: n
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Dispatch events to embedder.
|
const makeProps = (eventKey: string, args: any[]) => {
|
||||||
const fn = function (event: string) {
|
const props: Record<string, any> = {};
|
||||||
guest.on(event as any, function (_, ...args: any[]) {
|
webViewEvents[eventKey].forEach((prop, index) => {
|
||||||
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, ...args);
|
props[prop] = args[index];
|
||||||
});
|
});
|
||||||
|
return props;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Dispatch events to embedder.
|
||||||
for (const event of supportedWebViewEvents) {
|
for (const event of supportedWebViewEvents) {
|
||||||
if (event !== 'new-window') {
|
guest.on(event as any, function (_, ...args: any[]) {
|
||||||
fn(event);
|
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, makeProps(event, args));
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
guest.on('new-window', function (event, url, frameName, disposition, options, additionalFeatures, referrer) {
|
guest.on('new-window', function (event, url, frameName, disposition, options) {
|
||||||
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', url,
|
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', {
|
||||||
frameName, disposition, sanitizeOptionsForGuest(options),
|
url,
|
||||||
additionalFeatures, referrer);
|
frameName,
|
||||||
|
disposition,
|
||||||
|
options: sanitizeOptionsForGuest(options)
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Dispatch guest's IPC messages to embedder.
|
// Dispatch guest's IPC messages to embedder.
|
||||||
guest.on('ipc-message-host' as any, function (_: Electron.Event, channel: string, args: any[]) {
|
guest.on('ipc-message-host' as any, function (_: Electron.Event, channel: string, args: any[]) {
|
||||||
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE, channel, ...args);
|
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'ipc-message', { channel, args });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Notify guest of embedder window visibility when it is ready
|
// Notify guest of embedder window visibility when it is ready
|
||||||
|
|
|
@ -8,7 +8,6 @@ export const enum IPC_MESSAGES {
|
||||||
GUEST_INSTANCE_VISIBILITY_CHANGE = 'GUEST_INSTANCE_VISIBILITY_CHANGE',
|
GUEST_INSTANCE_VISIBILITY_CHANGE = 'GUEST_INSTANCE_VISIBILITY_CHANGE',
|
||||||
|
|
||||||
GUEST_VIEW_INTERNAL_DISPATCH_EVENT = 'GUEST_VIEW_INTERNAL_DISPATCH_EVENT',
|
GUEST_VIEW_INTERNAL_DISPATCH_EVENT = 'GUEST_VIEW_INTERNAL_DISPATCH_EVENT',
|
||||||
GUEST_VIEW_INTERNAL_IPC_MESSAGE = 'GUEST_VIEW_INTERNAL_IPC_MESSAGE',
|
|
||||||
|
|
||||||
GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST = 'GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST',
|
GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST = 'GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST',
|
||||||
GUEST_VIEW_MANAGER_DETACH_GUEST = 'GUEST_VIEW_MANAGER_DETACH_GUEST',
|
GUEST_VIEW_MANAGER_DETACH_GUEST = 'GUEST_VIEW_MANAGER_DETACH_GUEST',
|
||||||
|
|
|
@ -12,7 +12,6 @@ export const webViewEvents: Record<string, readonly string[]> = {
|
||||||
'devtools-opened': [],
|
'devtools-opened': [],
|
||||||
'devtools-closed': [],
|
'devtools-closed': [],
|
||||||
'devtools-focused': [],
|
'devtools-focused': [],
|
||||||
'new-window': ['url', 'frameName', 'disposition', 'options'],
|
|
||||||
'will-navigate': ['url'],
|
'will-navigate': ['url'],
|
||||||
'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
|
'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
|
||||||
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],
|
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
||||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
||||||
import { webViewEvents } from '@electron/internal/common/web-view-events';
|
|
||||||
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
||||||
|
|
||||||
const { mainFrame: webFrame } = process._linkedBinding('electron_renderer_web_frame');
|
const { mainFrame: webFrame } = process._linkedBinding('electron_renderer_web_frame');
|
||||||
|
@ -13,32 +12,18 @@ const DEPRECATED_EVENTS: Record<string, string> = {
|
||||||
'page-title-updated': 'page-title-set'
|
'page-title-updated': 'page-title-set'
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
const dispatchEvent = function (delegate: GuestViewDelegate, eventName: string, eventKey: string, ...args: Array<any>) {
|
export function registerEvents (viewInstanceId: number, delegate: GuestViewDelegate) {
|
||||||
|
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, props) {
|
||||||
if (DEPRECATED_EVENTS[eventName] != null) {
|
if (DEPRECATED_EVENTS[eventName] != null) {
|
||||||
dispatchEvent(delegate, DEPRECATED_EVENTS[eventName], eventKey, ...args);
|
delegate.dispatchEvent(DEPRECATED_EVENTS[eventName], props);
|
||||||
}
|
}
|
||||||
|
|
||||||
const props: Record<string, any> = {};
|
|
||||||
webViewEvents[eventKey].forEach((prop, index) => {
|
|
||||||
props[prop] = args[index];
|
|
||||||
});
|
|
||||||
|
|
||||||
delegate.dispatchEvent(eventName, props);
|
delegate.dispatchEvent(eventName, props);
|
||||||
};
|
|
||||||
|
|
||||||
export function registerEvents (viewInstanceId: number, delegate: GuestViewDelegate) {
|
|
||||||
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, ...args) {
|
|
||||||
dispatchEvent(delegate, eventName, eventName, ...args);
|
|
||||||
});
|
|
||||||
|
|
||||||
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`, function (event, channel, ...args) {
|
|
||||||
delegate.dispatchEvent('ipc-message', { channel, args });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deregisterEvents (viewInstanceId: number) {
|
export function deregisterEvents (viewInstanceId: number) {
|
||||||
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`);
|
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`);
|
||||||
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createGuest (iframe: HTMLIFrameElement, elementInstanceId: number, params: Record<string, any>): Promise<number> {
|
export function createGuest (iframe: HTMLIFrameElement, elementInstanceId: number, params: Record<string, any>): Promise<number> {
|
||||||
|
|
Loading…
Reference in a new issue