2020-03-20 20:28:31 +00:00
|
|
|
import { webFrame, IpcMessageEvent } from 'electron';
|
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
2020-05-01 04:33:14 +00:00
|
|
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
2020-10-06 13:11:26 +00:00
|
|
|
import { webViewEvents } from '@electron/internal/common/web-view-events';
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
import { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2019-03-07 23:26:23 +00:00
|
|
|
|
|
|
|
const DEPRECATED_EVENTS: Record<string, string> = {
|
|
|
|
'page-title-updated': 'page-title-set'
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-03-07 23:26:23 +00:00
|
|
|
|
|
|
|
const dispatchEvent = function (
|
|
|
|
webView: WebViewImpl, eventName: string, eventKey: string, ...args: Array<any>
|
|
|
|
) {
|
|
|
|
if (DEPRECATED_EVENTS[eventName] != null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
dispatchEvent(webView, DEPRECATED_EVENTS[eventName], eventKey, ...args);
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const domEvent = new Event(eventName) as ElectronInternal.WebViewEvent;
|
2020-10-06 13:11:26 +00:00
|
|
|
webViewEvents[eventKey].forEach((prop, index) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(domEvent as any)[prop] = args[index];
|
|
|
|
});
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
webView.dispatchEvent(domEvent);
|
2019-03-07 23:26:23 +00:00
|
|
|
|
|
|
|
if (eventName === 'load-commit') {
|
2020-03-20 20:28:31 +00:00
|
|
|
webView.onLoadCommit(domEvent);
|
2019-03-07 23:26:23 +00:00
|
|
|
} else if (eventName === 'focus-change') {
|
2020-03-20 20:28:31 +00:00
|
|
|
webView.onFocusChange();
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-03-07 23:26:23 +00:00
|
|
|
|
|
|
|
export function registerEvents (webView: WebViewImpl, viewInstanceId: number) {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DESTROY_GUEST}-${viewInstanceId}`, function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
webView.guestInstanceId = undefined;
|
|
|
|
webView.reset();
|
|
|
|
const domEvent = new Event('destroyed');
|
|
|
|
webView.dispatchEvent(domEvent);
|
|
|
|
});
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, ...args) {
|
2020-03-20 20:28:31 +00:00
|
|
|
dispatchEvent(webView, eventName, eventName, ...args);
|
|
|
|
});
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`, function (event, channel, ...args) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const domEvent = new Event('ipc-message') as IpcMessageEvent;
|
|
|
|
domEvent.channel = channel;
|
|
|
|
domEvent.args = args;
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
webView.dispatchEvent(domEvent);
|
|
|
|
});
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function deregisterEvents (viewInstanceId: number) {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DESTROY_GUEST}-${viewInstanceId}`);
|
|
|
|
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`);
|
|
|
|
ipcRendererInternal.removeAllListeners(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`);
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createGuest (params: Record<string, any>): Promise<number> {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_VIEW_MANAGER_CREATE_GUEST, params);
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function attachGuest (
|
|
|
|
elementInstanceId: number, guestInstanceId: number, params: Record<string, any>, contentWindow: Window
|
|
|
|
) {
|
2020-10-02 02:52:29 +00:00
|
|
|
const embedderFrameId = webFrame.getWebFrameId(contentWindow);
|
2019-03-07 23:26:23 +00:00
|
|
|
if (embedderFrameId < 0) { // this error should not happen.
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Invalid embedder frame');
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_VIEW_MANAGER_ATTACH_GUEST, embedderFrameId, elementInstanceId, guestInstanceId, params);
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 04:33:14 +00:00
|
|
|
export function detachGuest (guestInstanceId: number) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_DETACH_GUEST, guestInstanceId);
|
2020-05-01 04:33:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
export const guestViewInternalModule = {
|
|
|
|
deregisterEvents,
|
|
|
|
createGuest,
|
2020-05-01 04:33:14 +00:00
|
|
|
attachGuest,
|
|
|
|
detachGuest
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|