From 04aafcc5efd2cf35b54891a77b5813f8a08e754e Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 17 Aug 2021 18:10:27 +0200 Subject: [PATCH] refactor: simplify event dispatch (#30458) * refactor: simplify event dispatch * Update lib/browser/guest-view-manager.ts Co-authored-by: Jeremy Rose * remove undocumented new-window event properties Co-authored-by: Jeremy Rose --- filenames.auto.gni | 2 -- lib/browser/guest-view-manager.ts | 30 ++++++++++++-------- lib/common/ipc-messages.ts | 1 - lib/common/web-view-events.ts | 1 - lib/renderer/web-view/guest-view-internal.ts | 25 ++++------------ 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/filenames.auto.gni b/filenames.auto.gni index 5d3730b718f2..163f7be1d131 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -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", diff --git a/lib/browser/guest-view-manager.ts b/lib/browser/guest-view-manager.ts index d65ae245fa10..b2b5f49bfcbb 100644 --- a/lib/browser/guest-view-manager.ts +++ b/lib/browser/guest-view-manager.ts @@ -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 = {}; + 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 diff --git a/lib/common/ipc-messages.ts b/lib/common/ipc-messages.ts index 8997605a85ba..debb2f0bb935 100644 --- a/lib/common/ipc-messages.ts +++ b/lib/common/ipc-messages.ts @@ -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', diff --git a/lib/common/web-view-events.ts b/lib/common/web-view-events.ts index be840b1e174f..6b81acf63bd4 100644 --- a/lib/common/web-view-events.ts +++ b/lib/common/web-view-events.ts @@ -12,7 +12,6 @@ export const webViewEvents: Record = { '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'], diff --git a/lib/renderer/web-view/guest-view-internal.ts b/lib/renderer/web-view/guest-view-internal.ts index 0acfa787b1e3..b5a99f8744e0 100644 --- a/lib/renderer/web-view/guest-view-internal.ts +++ b/lib/renderer/web-view/guest-view-internal.ts @@ -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 = { 'page-title-updated': 'page-title-set' } as const; -const dispatchEvent = function (delegate: GuestViewDelegate, eventName: string, eventKey: string, ...args: Array) { - if (DEPRECATED_EVENTS[eventName] != null) { - dispatchEvent(delegate, DEPRECATED_EVENTS[eventName], eventKey, ...args); - } - - const props: Record = {}; - 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): Promise {