2020-03-20 20:28:31 +00:00
|
|
|
'use strict';
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { webContents } = require('electron');
|
|
|
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal');
|
|
|
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
|
2020-03-26 18:05:45 +00:00
|
|
|
const { parseFeaturesString } = require('@electron/internal/common/parse-features-string');
|
2020-03-20 20:28:31 +00:00
|
|
|
const { syncMethods, asyncMethods, properties } = require('@electron/internal/common/web-view-methods');
|
|
|
|
const { serialize } = require('@electron/internal/common/type-utils');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Doesn't exist in early initialization.
|
2020-03-20 20:28:31 +00:00
|
|
|
let webViewManager = null;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-07 17:29:24 +00:00
|
|
|
const supportedWebViewEvents = [
|
2016-01-25 16:37:15 +00:00
|
|
|
'load-commit',
|
2016-09-08 17:01:01 +00:00
|
|
|
'did-attach',
|
2016-01-25 16:37:15 +00:00
|
|
|
'did-finish-load',
|
|
|
|
'did-fail-load',
|
|
|
|
'did-frame-finish-load',
|
|
|
|
'did-start-loading',
|
|
|
|
'did-stop-loading',
|
|
|
|
'dom-ready',
|
|
|
|
'console-message',
|
2017-01-13 22:04:09 +00:00
|
|
|
'context-menu',
|
2016-01-25 16:37:15 +00:00
|
|
|
'devtools-opened',
|
|
|
|
'devtools-closed',
|
|
|
|
'devtools-focused',
|
|
|
|
'will-navigate',
|
2018-04-26 10:17:55 +00:00
|
|
|
'did-start-navigation',
|
2016-01-25 16:37:15 +00:00
|
|
|
'did-navigate',
|
2018-05-01 04:34:41 +00:00
|
|
|
'did-frame-navigate',
|
2016-01-25 16:37:15 +00:00
|
|
|
'did-navigate-in-page',
|
2018-08-28 18:35:44 +00:00
|
|
|
'focus-change',
|
2016-01-25 16:37:15 +00:00
|
|
|
'close',
|
|
|
|
'crashed',
|
|
|
|
'plugin-crashed',
|
|
|
|
'destroyed',
|
|
|
|
'page-title-updated',
|
|
|
|
'page-favicon-updated',
|
|
|
|
'enter-html-full-screen',
|
|
|
|
'leave-html-full-screen',
|
|
|
|
'media-started-playing',
|
|
|
|
'media-paused',
|
|
|
|
'found-in-page',
|
2016-06-07 06:56:19 +00:00
|
|
|
'did-change-theme-color',
|
|
|
|
'update-target-url'
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstances = {};
|
|
|
|
const embedderElementsMap = {};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
function sanitizeOptionsForGuest (options) {
|
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
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Create a new guest instance.
|
2016-06-07 17:29:24 +00:00
|
|
|
const createGuest = function (embedder, params) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (webViewManager == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
webViewManager = process.electronBinding('web_view_manager');
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-06-07 17:24:48 +00:00
|
|
|
|
|
|
|
const guest = webContents.create({
|
2019-05-20 10:55:46 +00:00
|
|
|
type: 'webview',
|
2016-01-12 02:40:23 +00:00
|
|
|
partition: params.partition,
|
|
|
|
embedder: embedder
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const guestInstanceId = guest.id;
|
2016-09-08 17:01:01 +00:00
|
|
|
guestInstances[guestInstanceId] = {
|
2016-01-12 02:40:23 +00:00
|
|
|
guest: guest,
|
|
|
|
embedder: embedder
|
2020-03-20 20:28:31 +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-03-17 20:17:55 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(guestInstances, 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.
|
2018-08-16 22:57:40 +00:00
|
|
|
guest.once('did-attach', function (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
params = this.attachParams;
|
|
|
|
delete this.attachParams;
|
2016-11-02 22:43:19 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const previouslyAttached = this.viewInstanceId != null;
|
|
|
|
this.viewInstanceId = params.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) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const opts = {};
|
2016-01-12 02:40:23 +00:00
|
|
|
if (params.httpreferrer) {
|
2020-03-20 20:28:31 +00:00
|
|
|
opts.httpReferrer = params.httpreferrer;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (params.useragent) {
|
2020-03-20 20:28:31 +00:00
|
|
|
opts.userAgent = params.useragent;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
this.loadURL(params.src, opts);
|
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
|
|
|
|
2016-11-03 17:39:40 +00:00
|
|
|
const sendToEmbedder = (channel, ...args) => {
|
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
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Dispatch events to embedder.
|
2016-06-07 17:24:48 +00:00
|
|
|
const fn = function (event) {
|
|
|
|
guest.on(event, function (_, ...args) {
|
2020-03-20 20:28:31 +00:00
|
|
|
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT', event, ...args);
|
|
|
|
});
|
|
|
|
};
|
2016-06-07 17:24:48 +00:00
|
|
|
for (const event of supportedWebViewEvents) {
|
2020-03-20 20:28:31 +00:00
|
|
|
fn(event);
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
guest.on('new-window', function (event, url, frameName, disposition, options, additionalFeatures, referrer) {
|
|
|
|
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT', 'new-window', url,
|
|
|
|
frameName, disposition, sanitizeOptionsForGuest(options),
|
2020-03-20 20:28:31 +00:00
|
|
|
additionalFeatures, referrer);
|
|
|
|
});
|
2019-10-09 17:59:08 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Dispatch guest's IPC messages to embedder.
|
2019-01-23 16:24:57 +00:00
|
|
|
guest.on('ipc-message-host', function (_, channel, args) {
|
2020-03-20 20:28:31 +00:00
|
|
|
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE', channel, ...args);
|
|
|
|
});
|
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-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
2017-06-12 19:58:26 +00:00
|
|
|
if (guestInstance != null && guestInstance.visibilityState != null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
guest._sendInternal('ELECTRON_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
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return guestInstanceId;
|
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Attach the guest to an element of embedder.
|
2018-08-16 22:57:40 +00:00
|
|
|
const attachGuest = function (event, embedderFrameId, elementInstanceId, guestInstanceId, params) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const embedder = event.sender;
|
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}`;
|
|
|
|
const oldGuestInstanceId = embedderElementsMap[key];
|
2016-01-12 02:40:23 +00:00
|
|
|
if (oldGuestInstanceId != null) {
|
2016-09-08 17:01:01 +00:00
|
|
|
// Reattachment to the same guest is just a no-op.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (oldGuestInstanceId === guestInstanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const oldGuestInstance = guestInstances[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-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
2016-09-08 17:01:01 +00:00
|
|
|
// If this isn't a valid guest instance then do nothing.
|
|
|
|
if (!guestInstance) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid guestInstanceId: ${guestInstanceId}`);
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
const { guest } = guestInstance;
|
2019-01-03 18:31:10 +00:00
|
|
|
if (guest.hostWebContents !== event.sender) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Access denied to guestInstanceId: ${guestInstanceId}`);
|
2019-01-03 18:31:10 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
|
|
|
|
// If this guest is already attached to an element then remove it
|
|
|
|
if (guestInstance.elementInstanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const oldKey = `${guestInstance.embedder.id}-${guestInstance.elementInstanceId}`;
|
|
|
|
delete embedderElementsMap[oldKey];
|
2016-11-02 22:19:16 +00:00
|
|
|
|
|
|
|
// Remove guest from embedder if moving across web views
|
|
|
|
if (guest.viewInstanceId !== params.instanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
webViewManager.removeGuest(guestInstance.embedder, guestInstanceId);
|
|
|
|
guestInstance.embedder._sendInternal(`ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-${guest.viewInstanceId}`);
|
2016-11-02 22:19:16 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 17:39:40 +00:00
|
|
|
const webPreferences = {
|
2016-01-12 02:40:23 +00:00
|
|
|
guestInstanceId: guestInstanceId,
|
2016-11-03 17:39:40 +00:00
|
|
|
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
|
2019-03-15 17:39:20 +00:00
|
|
|
nodeIntegrationInSubFrames: params.nodeintegrationinsubframes != null ? params.nodeintegrationinsubframes : false,
|
2018-10-13 17:50:07 +00:00
|
|
|
enableRemoteModule: params.enableremotemodule,
|
2016-01-12 02:40:23 +00:00
|
|
|
plugins: params.plugins,
|
2019-10-29 04:16:10 +00:00
|
|
|
zoomFactor: embedder.zoomFactor,
|
2019-05-27 00:44:54 +00:00
|
|
|
disablePopups: !params.allowpopups,
|
2016-01-21 10:13:41 +00:00
|
|
|
webSecurity: !params.disablewebsecurity,
|
2018-05-23 21:01:34 +00:00
|
|
|
enableBlinkFeatures: params.blinkfeatures,
|
2016-06-07 23:35:23 +00:00
|
|
|
disableBlinkFeatures: params.disableblinkfeatures
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-04-21 18:52:10 +00:00
|
|
|
|
2016-10-14 22:16:39 +00:00
|
|
|
// parse the 'webpreferences' attribute string, if set
|
|
|
|
// this uses the same parsing rules as window.open uses for its features
|
|
|
|
if (typeof params.webpreferences === 'string') {
|
2016-10-25 01:21:42 +00:00
|
|
|
parseFeaturesString(params.webpreferences, function (key, value) {
|
2016-10-14 22:16:39 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
// no value was specified, default it to true
|
2020-03-20 20:28:31 +00:00
|
|
|
value = true;
|
2016-10-14 22:16:39 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
webPreferences[key] = value;
|
|
|
|
});
|
2016-10-14 22:16:39 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
if (params.preload) {
|
2020-03-20 20:28:31 +00:00
|
|
|
webPreferences.preloadURL = params.preload;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-02-03 20:46:22 +00:00
|
|
|
|
2018-07-02 16:06:26 +00:00
|
|
|
// Security options that guest will always inherit from embedder
|
|
|
|
const inheritedWebPreferences = new Map([
|
|
|
|
['contextIsolation', true],
|
|
|
|
['javascript', false],
|
|
|
|
['nativeWindowOpen', true],
|
|
|
|
['nodeIntegration', false],
|
2018-10-13 17:50:07 +00:00
|
|
|
['enableRemoteModule', false],
|
2019-01-22 19:24:46 +00:00
|
|
|
['sandbox', true],
|
|
|
|
['nodeIntegrationInSubFrames', false]
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2018-07-02 16:06:26 +00:00
|
|
|
|
|
|
|
// Inherit certain option values from embedder
|
2020-03-20 20:28:31 +00:00
|
|
|
const lastWebPreferences = embedder.getLastWebPreferences();
|
2018-07-02 16:06:26 +00:00
|
|
|
for (const [name, value] of inheritedWebPreferences) {
|
|
|
|
if (lastWebPreferences[name] === value) {
|
2020-03-20 20:28:31 +00:00
|
|
|
webPreferences[name] = value;
|
2018-07-02 16:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
embedder.emit('will-attach-webview', event, webPreferences, params);
|
2017-02-03 20:46:22 +00:00
|
|
|
if (event.defaultPrevented) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (guest.viewInstanceId == null) guest.viewInstanceId = params.instanceId;
|
|
|
|
guest.destroy();
|
|
|
|
return;
|
2017-02-03 20:46:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
guest.attachParams = params;
|
|
|
|
embedderElementsMap[key] = guestInstanceId;
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
guest.setEmbedder(embedder);
|
|
|
|
guestInstance.embedder = embedder;
|
|
|
|
guestInstance.elementInstanceId = elementInstanceId;
|
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
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
webViewManager.addGuest(guestInstanceId, elementInstanceId, embedder, guest, webPreferences);
|
|
|
|
guest.attachToIframe(embedder, embedderFrameId);
|
|
|
|
};
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
// Remove an guest-embedder relationship.
|
|
|
|
const detachGuest = function (embedder, guestInstanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
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);
|
|
|
|
delete guestInstances[guestInstanceId];
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const key = `${embedder.id}-${guestInstance.elementInstanceId}`;
|
|
|
|
delete embedderElementsMap[key];
|
|
|
|
};
|
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-03-20 20:28:31 +00:00
|
|
|
const watchedEmbedders = new Set();
|
2016-09-08 17:01:01 +00:00
|
|
|
const watchEmbedder = function (embedder) {
|
|
|
|
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
|
|
|
|
2017-02-24 18:18:09 +00:00
|
|
|
// Forward embedder window visiblity change events to guest
|
|
|
|
const onVisibilityChange = function (visibilityState) {
|
2020-03-17 20:17:55 +00:00
|
|
|
for (const guestInstanceId of Object.keys(guestInstances)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
|
|
|
guestInstance.visibilityState = visibilityState;
|
2017-02-24 18:18:09 +00:00
|
|
|
if (guestInstance.embedder === embedder) {
|
2020-03-20 20:28:31 +00:00
|
|
|
guestInstance.guest._sendInternal('ELECTRON_GUEST_INSTANCE_VISIBILITY_CHANGE', visibilityState);
|
2017-02-24 18:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
embedder.on('-window-visibility-change', onVisibilityChange);
|
2017-02-24 18:18:09 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
embedder.once('will-destroy', () => {
|
|
|
|
// 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-03-17 20:17:55 +00:00
|
|
|
for (const guestInstanceId of Object.keys(guestInstances)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
2018-08-16 22:57:40 +00:00
|
|
|
if (guestInstance.embedder === embedder) {
|
2020-03-20 20:28:31 +00:00
|
|
|
detachGuest(embedder, parseInt(guestInstanceId));
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 22:57:40 +00:00
|
|
|
// Clear the listeners.
|
2020-03-20 20:28:31 +00:00
|
|
|
embedder.removeListener('-window-visibility-change', onVisibilityChange);
|
|
|
|
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
|
|
|
|
|
|
|
const isWebViewTagEnabled = function (contents) {
|
|
|
|
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
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
const makeSafeHandler = function (channel, handler) {
|
|
|
|
return (event, ...args) => {
|
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
|
|
|
|
|
|
|
const handleMessage = function (channel, handler) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMainInternal.handle(channel, makeSafeHandler(channel, handler));
|
|
|
|
};
|
2019-08-23 22:45:50 +00:00
|
|
|
|
|
|
|
const handleMessageSync = function (channel, handler) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMainUtils.handleSync(channel, makeSafeHandler(channel, handler));
|
|
|
|
};
|
2018-11-30 09:44:38 +00:00
|
|
|
|
2019-03-01 18:33:03 +00:00
|
|
|
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', function (event, params) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return createGuest(event.sender, params);
|
|
|
|
});
|
2018-07-21 02:11:28 +00:00
|
|
|
|
2018-11-30 09:44:38 +00:00
|
|
|
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', function (event, embedderFrameId, elementInstanceId, guestInstanceId, params) {
|
2019-01-03 18:31:10 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
attachGuest(event, embedderFrameId, elementInstanceId, guestInstanceId, params);
|
2019-01-03 18:31:10 +00:00
|
|
|
} catch (error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`Guest attach failed: ${error}`);
|
2019-01-03 18:31:10 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-01-03 18:31:10 +00:00
|
|
|
// this message is sent by the actual <webview>
|
2019-02-04 22:49:53 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', function (event, focus, guestInstanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuest(guestInstanceId);
|
2019-01-03 18:31:10 +00:00
|
|
|
if (guest === event.sender) {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.sender.emit('focus-change', {}, focus, guestInstanceId);
|
2019-01-03 18:31:10 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`focus-change for guestInstanceId: ${guestInstanceId}`);
|
2019-01-03 18:31:10 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-08-28 18:35:44 +00:00
|
|
|
|
2019-03-01 18:33:03 +00:00
|
|
|
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId, method, args) {
|
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-03-20 20:28:31 +00:00
|
|
|
return guest[method](...args);
|
|
|
|
});
|
2019-08-23 22:45:50 +00:00
|
|
|
|
|
|
|
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId, method, args) {
|
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-03-20 20:28:31 +00:00
|
|
|
return guest[method](...args);
|
|
|
|
});
|
2018-11-30 09:44:38 +00:00
|
|
|
|
2020-03-03 22:25:14 +00:00
|
|
|
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', function (event, guestInstanceId, property) {
|
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-03-20 20:28:31 +00:00
|
|
|
return guest[property];
|
|
|
|
});
|
2020-03-03 22:25:14 +00:00
|
|
|
|
|
|
|
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', function (event, guestInstanceId, property, val) {
|
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-03-20 20:28:31 +00:00
|
|
|
guest[property] = val;
|
|
|
|
});
|
2020-03-03 22:25:14 +00:00
|
|
|
|
2019-11-12 20:56:17 +00:00
|
|
|
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', async function (event, guestInstanceId, args) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuestForWebContents(guestInstanceId, event.sender);
|
2019-11-12 20:56:17 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return serialize(await guest.capturePage(...args));
|
|
|
|
});
|
2019-11-12 20:56:17 +00:00
|
|
|
|
2019-01-03 18:31:10 +00:00
|
|
|
// Returns WebContents from its guest id hosted in given webContents.
|
|
|
|
const getGuestForWebContents = function (guestInstanceId, contents) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guest = getGuest(guestInstanceId);
|
2019-01-03 18:31:10 +00:00
|
|
|
if (!guest) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid guestInstanceId: ${guestInstanceId}`);
|
2019-01-03 18:31:10 +00:00
|
|
|
}
|
|
|
|
if (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
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return guest;
|
|
|
|
};
|
2019-01-03 18:31:10 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns WebContents from its guest id.
|
2016-11-03 17:39:40 +00:00
|
|
|
const getGuest = function (guestInstanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
|
|
|
if (guestInstance != null) return guestInstance.guest;
|
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Returns the embedder of the guest.
|
2016-09-08 17:01:01 +00:00
|
|
|
const getEmbedder = function (guestInstanceId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestInstance = guestInstances[guestInstanceId];
|
|
|
|
if (guestInstance != null) return guestInstance.embedder;
|
|
|
|
};
|
2016-11-03 17:39:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
exports.isWebViewTagEnabled = isWebViewTagEnabled;
|