2020-03-20 20:28:31 +00:00
|
|
|
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-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2021-05-15 07:42:07 +00:00
|
|
|
const { mainFrame: webFrame } = process._linkedBinding('electron_renderer_web_frame');
|
|
|
|
|
|
|
|
export interface GuestViewDelegate {
|
|
|
|
dispatchEvent (eventName: string, props: Record<string, any>): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function registerEvents (viewInstanceId: number, delegate: GuestViewDelegate) {
|
2021-08-17 16:10:27 +00:00
|
|
|
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT}-${viewInstanceId}`, function (event, eventName, props) {
|
|
|
|
delegate.dispatchEvent(eventName, props);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
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_DISPATCH_EVENT}-${viewInstanceId}`);
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 15:35:57 +00:00
|
|
|
export function createGuest (iframe: HTMLIFrameElement, elementInstanceId: number, params: Record<string, any>): Promise<number> {
|
2021-05-15 07:42:07 +00:00
|
|
|
if (!(iframe instanceof HTMLIFrameElement)) {
|
2023-07-25 16:08:46 +00:00
|
|
|
throw new TypeError('Invalid embedder frame');
|
2021-05-15 07:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const embedderFrameId = webFrame.getWebFrameId(iframe.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
|
|
|
}
|
2021-05-15 07:42:07 +00:00
|
|
|
|
2021-08-02 15:35:57 +00:00
|
|
|
return ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST, embedderFrameId, elementInstanceId, 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
|
|
|
}
|
2021-05-15 07:42:07 +00:00
|
|
|
|
|
|
|
export function invoke (guestInstanceId: number, method: string, args: any[]) {
|
|
|
|
return ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, guestInstanceId, method, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function invokeSync (guestInstanceId: number, method: string, args: any[]) {
|
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, guestInstanceId, method, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function propertyGet (guestInstanceId: number, name: string) {
|
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_PROPERTY_GET, guestInstanceId, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function propertySet (guestInstanceId: number, name: string, value: any) {
|
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_PROPERTY_SET, guestInstanceId, name, value);
|
|
|
|
}
|