2020-10-13 01:29:08 +00:00
|
|
|
import { webContents } from 'electron/main';
|
|
|
|
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
|
|
|
|
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
|
2021-11-10 16:54:51 +00:00
|
|
|
import { parseWebViewWebPreferences } from '@electron/internal/browser/parse-features-string';
|
2020-10-13 01:29:08 +00:00
|
|
|
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods';
|
2021-11-10 16:54:51 +00:00
|
|
|
import { webViewEvents } from '@electron/internal/browser/web-view-events';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2020-10-13 01:29:08 +00:00
|
|
|
|
|
|
|
interface GuestInstance {
|
2022-07-21 09:29:31 +00:00
|
|
|
elementInstanceId: number;
|
2020-10-13 01:29:08 +00:00
|
|
|
visibilityState?: VisibilityState;
|
|
|
|
embedder: Electron.WebContents;
|
|
|
|
guest: Electron.WebContents;
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
const webViewManager = process._linkedBinding('electron_browser_web_view_manager');
|
2021-08-02 15:35:57 +00:00
|
|
|
const eventBinding = process._linkedBinding('electron_browser_event');
|
2022-03-16 23:23:41 +00:00
|
|
|
const netBinding = process._linkedBinding('electron_browser_net');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-06 13:11:26 +00:00
|
|
|
const supportedWebViewEvents = Object.keys(webViewEvents);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-19 00:24:51 +00:00
|
|
|
const guestInstances = new Map<number, GuestInstance>();
|
|
|
|
const embedderElementsMap = new Map<string, number>();
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
function sanitizeOptionsForGuest (options: Record<string, any>) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const ret = { ...options };
|
2019-10-09 17:59:08 +00:00
|
|
|
// WebContents values can't be sent over IPC.
|
2020-03-20 20:28:31 +00:00
|
|
|
delete ret.webContents;
|
|
|
|
return ret;
|
2019-10-09 17:59:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 22:32:53 +00:00
|
|
|
function makeWebPreferences (embedder: Electron.WebContents, params: Record<string, any>) {
|
2021-07-27 23:12:49 +00:00
|
|
|
// parse the 'webpreferences' attribute string, if set
|
|
|
|
// this uses the same parsing rules as window.open uses for its features
|
|
|
|
const parsedWebPreferences =
|
|
|
|
typeof params.webpreferences === 'string'
|
|
|
|
? parseWebViewWebPreferences(params.webpreferences)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const webPreferences: Electron.WebPreferences = {
|
|
|
|
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
|
|
|
|
nodeIntegrationInSubFrames: params.nodeintegrationinsubframes != null ? params.nodeintegrationinsubframes : false,
|
|
|
|
plugins: params.plugins,
|
|
|
|
zoomFactor: embedder.zoomFactor,
|
|
|
|
disablePopups: !params.allowpopups,
|
|
|
|
webSecurity: !params.disablewebsecurity,
|
|
|
|
enableBlinkFeatures: params.blinkfeatures,
|
|
|
|
disableBlinkFeatures: params.disableblinkfeatures,
|
2022-07-21 09:29:31 +00:00
|
|
|
partition: params.partition,
|
2021-07-27 23:12:49 +00:00
|
|
|
...parsedWebPreferences
|
|
|
|
};
|
|
|
|
|
|
|
|
if (params.preload) {
|
2022-03-16 23:23:41 +00:00
|
|
|
webPreferences.preload = netBinding.fileURLToFilePath(params.preload);
|
2021-07-27 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Security options that guest will always inherit from embedder
|
|
|
|
const inheritedWebPreferences = new Map([
|
|
|
|
['contextIsolation', true],
|
|
|
|
['javascript', false],
|
|
|
|
['nodeIntegration', false],
|
|
|
|
['sandbox', true],
|
|
|
|
['nodeIntegrationInSubFrames', false],
|
|
|
|
['enableWebSQL', false]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Inherit certain option values from embedder
|
|
|
|
const lastWebPreferences = embedder.getLastWebPreferences()!;
|
|
|
|
for (const [name, value] of inheritedWebPreferences) {
|
|
|
|
if (lastWebPreferences[name as keyof Electron.WebPreferences] === value) {
|
|
|
|
(webPreferences as any)[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return webPreferences;
|
|
|
|
}
|
|
|
|
|
2022-01-12 07:41:20 +00:00
|
|
|
function makeLoadURLOptions (params: Record<string, any>) {
|
|
|
|
const opts: Electron.LoadURLOptions = {};
|
|
|
|
if (params.httpreferrer) {
|
|
|
|
opts.httpReferrer = params.httpreferrer;
|
|
|
|
}
|
|
|
|
if (params.useragent) {
|
|
|
|
opts.userAgent = params.useragent;
|
|
|
|
}
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Create a new guest instance.
|
2021-08-02 15:35:57 +00:00
|
|
|
const createGuest = function (embedder: Electron.WebContents, embedderFrameId: number, elementInstanceId: number, params: Record<string, any>) {
|
2022-07-21 09:29:31 +00:00
|
|
|
const webPreferences = makeWebPreferences(embedder, params);
|
|
|
|
const event = eventBinding.createWithSender(embedder);
|
|
|
|
|
|
|
|
const { instanceId } = params;
|
|
|
|
|
|
|
|
embedder.emit('will-attach-webview', event, webPreferences, params);
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const guest = (webContents as typeof ElectronInternal.WebContents).create({
|
2022-07-21 09:29:31 +00:00
|
|
|
...webPreferences,
|
2019-05-20 10:55:46 +00:00
|
|
|
type: 'webview',
|
2021-05-06 20:56:05 +00:00
|
|
|
embedder
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2022-07-21 09:29:31 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstanceId = guest.id;
|
2020-10-19 00:24:51 +00:00
|
|
|
guestInstances.set(guestInstanceId, {
|
2022-07-21 09:29:31 +00:00
|
|
|
elementInstanceId,
|
2021-05-06 20:56:05 +00:00
|
|
|
guest,
|
|
|
|
embedder
|
2020-10-19 00:24:51 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
// Clear the guest from map when it is destroyed.
|
|
|
|
guest.once('destroyed', () => {
|
2020-10-19 00:24:51 +00:00
|
|
|
if (guestInstances.has(guestInstanceId)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
detachGuest(embedder, guestInstanceId);
|
2018-08-16 22:57:40 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Init guest web view after attached.
|
2020-10-13 01:29:08 +00:00
|
|
|
guest.once('did-attach' as any, function (this: Electron.WebContents, event: Electron.Event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const previouslyAttached = this.viewInstanceId != null;
|
2022-07-21 09:29:31 +00:00
|
|
|
this.viewInstanceId = instanceId;
|
2016-11-02 22:43:19 +00:00
|
|
|
|
|
|
|
// Only load URL and set size on first attach
|
|
|
|
if (previouslyAttached) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return;
|
2016-11-02 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
if (params.src) {
|
2022-07-21 09:29:31 +00:00
|
|
|
this.loadURL(params.src, makeLoadURLOptions(params));
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
embedder.emit('did-attach-webview', event, guest);
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
const sendToEmbedder = (channel: string, ...args: any[]) => {
|
2018-08-16 22:57:40 +00:00
|
|
|
if (!embedder.isDestroyed()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
embedder._sendInternal(`${channel}-${guest.viewInstanceId}`, ...args);
|
2016-11-03 17:39:40 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-11-03 17:39:40 +00:00
|
|
|
|
2021-08-17 16:10:27 +00:00
|
|
|
const makeProps = (eventKey: string, args: any[]) => {
|
|
|
|
const props: Record<string, any> = {};
|
|
|
|
webViewEvents[eventKey].forEach((prop, index) => {
|
|
|
|
props[prop] = args[index];
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2021-08-17 16:10:27 +00:00
|
|
|
return props;
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2021-08-17 16:10:27 +00:00
|
|
|
|
|
|
|
// Dispatch events to embedder.
|
2016-06-07 17:24:48 +00:00
|
|
|
for (const event of supportedWebViewEvents) {
|
2021-08-17 16:10:27 +00:00
|
|
|
guest.on(event as any, function (_, ...args: any[]) {
|
|
|
|
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, event, makeProps(event, args));
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 16:10:27 +00:00
|
|
|
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)
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-09 17:59:08 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Dispatch guest's IPC messages to embedder.
|
2021-08-25 07:46:46 +00:00
|
|
|
guest.on('ipc-message-host' as any, function (event: Electron.IpcMainEvent, channel: string, args: any[]) {
|
|
|
|
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'ipc-message', {
|
|
|
|
frameId: [event.processId, event.frameId],
|
|
|
|
channel,
|
|
|
|
args
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-06-12 19:58:26 +00:00
|
|
|
// Notify guest of embedder window visibility when it is ready
|
2017-06-14 21:12:51 +00:00
|
|
|
// FIXME Remove once https://github.com/electron/electron/issues/6828 is fixed
|
2017-06-12 19:58:26 +00:00
|
|
|
guest.on('dom-ready', function () {
|
2020-10-19 00:24:51 +00:00
|
|
|
const guestInstance = guestInstances.get(guestInstanceId);
|
2017-06-12 19:58:26 +00:00
|
|
|
if (guestInstance != null && guestInstance.visibilityState != null) {
|
2020-10-13 21:11:06 +00:00
|
|
|
guest._sendInternal(IPC_MESSAGES.GUEST_INSTANCE_VISIBILITY_CHANGE, guestInstance.visibilityState);
|
2017-06-12 19:58:26 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2017-06-12 19:58:26 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Destroy the old guest when attaching.
|
2020-03-20 20:28:31 +00:00
|
|
|
const key = `${embedder.id}-${elementInstanceId}`;
|
2020-10-19 00:24:51 +00:00
|
|
|
const oldGuestInstanceId = embedderElementsMap.get(key);
|
2016-01-12 02:40:23 +00:00
|
|
|
if (oldGuestInstanceId != null) {
|
2020-10-19 00:24:51 +00:00
|
|
|
const oldGuestInstance = guestInstances.get(oldGuestInstanceId);
|
2018-08-16 22:57:40 +00:00
|
|
|
if (oldGuestInstance) {
|
2020-03-20 20:28:31 +00:00
|
|
|
oldGuestInstance.guest.detachFromOuterFrame();
|
2018-08-16 22:57:40 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-10-19 00:24:51 +00:00
|
|
|
embedderElementsMap.set(key, guestInstanceId);
|
2020-03-20 20:28:31 +00:00
|
|
|
guest.setEmbedder(embedder);
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
watchEmbedder(embedder);
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2021-05-04 13:59:44 +00:00
|
|
|
webViewManager.addGuest(guestInstanceId, embedder, guest, webPreferences);
|
2020-03-20 20:28:31 +00:00
|
|
|
guest.attachToIframe(embedder, embedderFrameId);
|
2022-07-21 09:29:31 +00:00
|
|
|
|
|
|
|
return guestInstanceId;
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
// Remove an guest-embedder relationship.
|
2020-10-13 01:29:08 +00:00
|
|
|
const detachGuest = function (embedder: Electron.WebContents, guestInstanceId: number) {
|
2020-10-19 00:24:51 +00:00
|
|
|
const guestInstance = guestInstances.get(guestInstanceId);
|
2020-05-01 04:33:14 +00:00
|
|
|
|
|
|
|
if (!guestInstance) return;
|
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
if (embedder !== guestInstance.embedder) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return;
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
webViewManager.removeGuest(embedder, guestInstanceId);
|
2020-10-19 00:24:51 +00:00
|
|
|
guestInstances.delete(guestInstanceId);
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const key = `${embedder.id}-${guestInstance.elementInstanceId}`;
|
2020-10-19 00:24:51 +00:00
|
|
|
embedderElementsMap.delete(key);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-09-08 17:01:01 +00:00
|
|
|
|
|
|
|
// Once an embedder has had a guest attached we watch it for destruction to
|
|
|
|
// destroy any remaining guests.
|
2020-10-13 01:29:08 +00:00
|
|
|
const watchedEmbedders = new Set<Electron.WebContents>();
|
|
|
|
const watchEmbedder = function (embedder: Electron.WebContents) {
|
2016-09-08 17:01:01 +00:00
|
|
|
if (watchedEmbedders.has(embedder)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return;
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
watchedEmbedders.add(embedder);
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-10-13 17:25:21 +00:00
|
|
|
// Forward embedder window visibility change events to guest
|
2020-10-13 01:29:08 +00:00
|
|
|
const onVisibilityChange = function (visibilityState: VisibilityState) {
|
2020-10-19 00:24:51 +00:00
|
|
|
for (const guestInstance of guestInstances.values()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
guestInstance.visibilityState = visibilityState;
|
2017-02-24 18:18:09 +00:00
|
|
|
if (guestInstance.embedder === embedder) {
|
2020-10-13 21:11:06 +00:00
|
|
|
guestInstance.guest._sendInternal(IPC_MESSAGES.GUEST_INSTANCE_VISIBILITY_CHANGE, visibilityState);
|
2017-02-24 18:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-10-13 01:29:08 +00:00
|
|
|
embedder.on('-window-visibility-change' as any, onVisibilityChange);
|
2017-02-24 18:18:09 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
embedder.once('will-destroy' as any, () => {
|
2018-08-16 22:57:40 +00:00
|
|
|
// Usually the guestInstances is cleared when guest is destroyed, but it
|
|
|
|
// may happen that the embedder gets manually destroyed earlier than guest,
|
|
|
|
// and the embedder will be invalid in the usual code path.
|
2020-10-19 00:24:51 +00:00
|
|
|
for (const [guestInstanceId, guestInstance] of guestInstances) {
|
2018-08-16 22:57:40 +00:00
|
|
|
if (guestInstance.embedder === embedder) {
|
2020-10-19 00:24:51 +00:00
|
|
|
detachGuest(embedder, guestInstanceId);
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 22:57:40 +00:00
|
|
|
// Clear the listeners.
|
2020-10-13 01:29:08 +00:00
|
|
|
embedder.removeListener('-window-visibility-change' as any, onVisibilityChange);
|
2020-03-20 20:28:31 +00:00
|
|
|
watchedEmbedders.delete(embedder);
|
|
|
|
});
|
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const isWebViewTagEnabledCache = new WeakMap();
|
2018-11-30 09:44:38 +00:00
|
|
|
|
2020-11-23 19:39:08 +00:00
|
|
|
const isWebViewTagEnabled = function (contents: Electron.WebContents) {
|
2018-11-30 09:44:38 +00:00
|
|
|
if (!isWebViewTagEnabledCache.has(contents)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const webPreferences = contents.getLastWebPreferences() || {};
|
|
|
|
isWebViewTagEnabledCache.set(contents, !!webPreferences.webviewTag);
|
2018-11-30 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return isWebViewTagEnabledCache.get(contents);
|
|
|
|
};
|
2018-11-30 09:44:38 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
const makeSafeHandler = function<Event extends { sender: Electron.WebContents }> (channel: string, handler: (event: Event, ...args: any[]) => any) {
|
|
|
|
return (event: Event, ...args: any[]) => {
|
2018-11-30 09:44:38 +00:00
|
|
|
if (isWebViewTagEnabled(event.sender)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return handler(event, ...args);
|
2018-11-30 09:44:38 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`<webview> IPC message ${channel} sent by WebContents with <webview> disabled (${event.sender.id})`);
|
|
|
|
throw new Error('<webview> disabled');
|
2018-11-30 09:44:38 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
};
|
2019-08-23 22:45:50 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
const handleMessage = function (channel: string, handler: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMainInternal.handle(channel, makeSafeHandler(channel, handler));
|
|
|
|
};
|
2019-08-23 22:45:50 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
const handleMessageSync = function (channel: string, handler: (event: ElectronInternal.IpcMainInternalEvent, ...args: any[]) => any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMainUtils.handleSync(channel, makeSafeHandler(channel, handler));
|
|
|
|
};
|
2018-11-30 09:44:38 +00:00
|
|
|
|
2021-08-02 15:35:57 +00:00
|
|
|
handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CREATE_AND_ATTACH_GUEST, function (event, embedderFrameId: number, elementInstanceId: number, params) {
|
|
|
|
return createGuest(event.sender, embedderFrameId, elementInstanceId, params);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_DETACH_GUEST, function (event, guestInstanceId: number) {
|
2020-05-01 04:33:14 +00:00
|
|
|
return detachGuest(event.sender, guestInstanceId);
|
|
|
|
});
|
|
|
|
|
2019-01-03 18:31:10 +00:00
|
|
|
// this message is sent by the actual <webview>
|
2021-05-05 18:37:40 +00:00
|
|
|
ipcMainInternal.on(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, function (event: ElectronInternal.IpcMainInternalEvent, focus: boolean) {
|
|
|
|
event.sender.emit('-focus-change', {}, focus);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-08-28 18:35:44 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, function (event, guestInstanceId: number, method: string, args: any[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuestForWebContents(guestInstanceId, event.sender);
|
2019-08-23 22:45:50 +00:00
|
|
|
if (!asyncMethods.has(method)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid method: ${method}`);
|
2019-08-23 22:45:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
return (guest as any)[method](...args);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-08-23 22:45:50 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, function (event, guestInstanceId: number, method: string, args: any[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuestForWebContents(guestInstanceId, event.sender);
|
2019-08-23 22:45:50 +00:00
|
|
|
if (!syncMethods.has(method)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid method: ${method}`);
|
2018-11-30 09:44:38 +00:00
|
|
|
}
|
2019-03-01 18:33:03 +00:00
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
return (guest as any)[method](...args);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-11-30 09:44:38 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_PROPERTY_GET, function (event, guestInstanceId: number, property: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuestForWebContents(guestInstanceId, event.sender);
|
2020-03-03 22:25:14 +00:00
|
|
|
if (!properties.has(property)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid property: ${property}`);
|
2020-03-03 22:25:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
return (guest as any)[property];
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-03 22:25:14 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_PROPERTY_SET, function (event, guestInstanceId: number, property: string, val: any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuestForWebContents(guestInstanceId, event.sender);
|
2020-03-03 22:25:14 +00:00
|
|
|
if (!properties.has(property)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid property: ${property}`);
|
2020-03-03 22:25:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:29:08 +00:00
|
|
|
(guest as any)[property] = val;
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-03 22:25:14 +00:00
|
|
|
|
2019-01-03 18:31:10 +00:00
|
|
|
// Returns WebContents from its guest id hosted in given webContents.
|
2020-10-13 01:29:08 +00:00
|
|
|
const getGuestForWebContents = function (guestInstanceId: number, contents: Electron.WebContents) {
|
2021-05-05 18:37:40 +00:00
|
|
|
const guestInstance = guestInstances.get(guestInstanceId);
|
|
|
|
if (!guestInstance) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid guestInstanceId: ${guestInstanceId}`);
|
2019-01-03 18:31:10 +00:00
|
|
|
}
|
2021-05-05 18:37:40 +00:00
|
|
|
if (guestInstance.guest.hostWebContents !== contents) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Access denied to guestInstanceId: ${guestInstanceId}`);
|
2019-01-03 18:31:10 +00:00
|
|
|
}
|
2021-05-05 18:37:40 +00:00
|
|
|
return guestInstance.guest;
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|