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:
Milan Burda 2021-08-17 18:10:27 +02:00 committed by GitHub
parent ff128a32d9
commit 04aafcc5ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 36 deletions

View file

@ -138,7 +138,6 @@ auto_filenames = {
"lib/common/define-properties.ts",
"lib/common/ipc-messages.ts",
"lib/common/type-utils.ts",
"lib/common/web-view-events.ts",
"lib/common/web-view-methods.ts",
"lib/renderer/api/context-bridge.ts",
"lib/renderer/api/crash-reporter.ts",
@ -270,7 +269,6 @@ auto_filenames = {
"lib/common/ipc-messages.ts",
"lib/common/reset-search-paths.ts",
"lib/common/type-utils.ts",
"lib/common/web-view-events.ts",
"lib/common/web-view-methods.ts",
"lib/common/webpack-provider.ts",
"lib/renderer/api/context-bridge.ts",

View file

@ -136,27 +136,33 @@ const createGuest = function (embedder: Electron.WebContents, embedderFrameId: n
}
};
// Dispatch events to embedder.
const fn = function (event: string) {
guest.on(event as any, function (_, ...args: any[]) {
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, ...args);
const makeProps = (eventKey: string, args: any[]) => {
const props: Record<string, any> = {};
webViewEvents[eventKey].forEach((prop, index) => {
props[prop] = args[index];
});
return props;
};
// Dispatch events to embedder.
for (const event of supportedWebViewEvents) {
if (event !== 'new-window') {
fn(event);
}
guest.on(event as any, function (_, ...args: any[]) {
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, makeProps(event, args));
});
}
guest.on('new-window', function (event, url, frameName, disposition, options, additionalFeatures, referrer) {
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', url,
frameName, disposition, sanitizeOptionsForGuest(options),
additionalFeatures, referrer);
guest.on('new-window', function (event, url, frameName, disposition, options) {
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'new-window', {
url,
frameName,
disposition,
options: sanitizeOptionsForGuest(options)
});
});
// Dispatch guest's IPC messages to embedder.
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

View file

@ -8,7 +8,6 @@ export const enum IPC_MESSAGES {
GUEST_INSTANCE_VISIBILITY_CHANGE = 'GUEST_INSTANCE_VISIBILITY_CHANGE',
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_DETACH_GUEST = 'GUEST_VIEW_MANAGER_DETACH_GUEST',

View file

@ -12,7 +12,6 @@ export const webViewEvents: Record<string, readonly string[]> = {
'devtools-opened': [],
'devtools-closed': [],
'devtools-focused': [],
'new-window': ['url', 'frameName', 'disposition', 'options'],
'will-navigate': ['url'],
'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],

View file

@ -1,6 +1,5 @@
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
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';
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'
} as const;
const dispatchEvent = function (delegate: GuestViewDelegate, eventName: string, eventKey: string, ...args: Array<any>) {
if (DEPRECATED_EVENTS[eventName] != null) {
dispatchEvent(delegate, DEPRECATED_EVENTS[eventName], eventKey, ...args);
}
const props: Record<string, any> = {};
webViewEvents[eventKey].forEach((prop, index) => {
props[prop] = args[index];
});
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_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, props) {
if (DEPRECATED_EVENTS[eventName] != null) {
delegate.dispatchEvent(DEPRECATED_EVENTS[eventName], props);
}
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`, function (event, channel, ...args) {
delegate.dispatchEvent('ipc-message', { channel, args });
delegate.dispatchEvent(eventName, props);
});
}
export function deregisterEvents (viewInstanceId: number) {
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> {