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

@ -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