2021-05-07 09:07:47 +02:00
|
|
|
import type { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl';
|
2020-03-20 13:28:31 -07:00
|
|
|
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants';
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-03-07 15:26:23 -08:00
|
|
|
const resolveURL = function (url?: string | null) {
|
2021-04-09 21:22:18 +00:00
|
|
|
return url ? new URL(url, location.href).href : '';
|
2020-03-20 13:28:31 -07:00
|
|
|
};
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-03-07 17:18:10 -08:00
|
|
|
interface MutationHandler {
|
|
|
|
handleMutation (_oldValue: any, _newValue: any): any;
|
|
|
|
}
|
|
|
|
|
2016-01-14 11:10:12 -08:00
|
|
|
// Attribute objects.
|
|
|
|
// Default implementation of a WebView attribute.
|
2020-11-04 00:02:23 +01:00
|
|
|
export class WebViewAttribute implements MutationHandler {
|
2019-03-07 15:26:23 -08:00
|
|
|
public value: any;
|
|
|
|
public ignoreMutation = false;
|
|
|
|
|
|
|
|
constructor (public name: string, public webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.name = name;
|
|
|
|
this.value = (webViewImpl.webviewNode as Record<string, any>)[name] || '';
|
|
|
|
this.webViewImpl = webViewImpl;
|
|
|
|
this.defineProperty();
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Retrieves and returns the attribute's value.
|
2019-03-07 15:26:23 -08:00
|
|
|
public getValue () {
|
2020-03-20 13:28:31 -07:00
|
|
|
return this.webViewImpl.webviewNode.getAttribute(this.name) || this.value;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Sets the attribute's value.
|
2019-03-07 15:26:23 -08:00
|
|
|
public setValue (value: any) {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.webViewImpl.webviewNode.setAttribute(this.name, value || '');
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Changes the attribute's value without triggering its mutation handler.
|
2019-03-07 15:26:23 -08:00
|
|
|
public setValueIgnoreMutation (value: any) {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.ignoreMutation = true;
|
|
|
|
this.setValue(value);
|
|
|
|
this.ignoreMutation = false;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Defines this attribute as a property on the webview node.
|
2019-03-07 15:26:23 -08:00
|
|
|
public defineProperty () {
|
2016-01-15 14:28:12 -08:00
|
|
|
return Object.defineProperty(this.webViewImpl.webviewNode, this.name, {
|
2016-03-11 14:08:14 -08:00
|
|
|
get: () => {
|
2020-03-20 13:28:31 -07:00
|
|
|
return this.getValue();
|
2016-03-11 14:08:14 -08:00
|
|
|
},
|
|
|
|
set: (value) => {
|
2020-03-20 13:28:31 -07:00
|
|
|
return this.setValue(value);
|
2016-03-11 14:08:14 -08:00
|
|
|
},
|
2016-01-15 14:28:12 -08:00
|
|
|
enumerable: true
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|
2016-01-15 15:10:26 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Called when the attribute's value changes.
|
2021-01-29 21:41:59 +01:00
|
|
|
public handleMutation: MutationHandler['handleMutation'] = () => undefined
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// An attribute that is treated as a Boolean.
|
|
|
|
class BooleanAttribute extends WebViewAttribute {
|
2016-03-25 12:57:17 -07:00
|
|
|
getValue () {
|
2020-03-20 13:28:31 -07:00
|
|
|
return this.webViewImpl.webviewNode.hasAttribute(this.name);
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:26:23 -08:00
|
|
|
setValue (value: boolean) {
|
2016-11-03 11:37:11 -07:00
|
|
|
if (value) {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.webViewImpl.webviewNode.setAttribute(this.name, '');
|
2016-11-03 11:37:11 -07:00
|
|
|
} else {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.webViewImpl.webviewNode.removeAttribute(this.name);
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Attribute representing the state of the storage partition.
|
2020-11-04 00:02:23 +01:00
|
|
|
export class PartitionAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
public validPartitionId = true
|
|
|
|
|
|
|
|
constructor (public webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, webViewImpl);
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-03-07 17:18:10 -08:00
|
|
|
public handleMutation = (oldValue: any, newValue: any) => {
|
2020-03-20 13:28:31 -07:00
|
|
|
newValue = newValue || '';
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// The partition cannot change if the webview has already navigated.
|
|
|
|
if (!this.webViewImpl.beforeFirstNavigation) {
|
2020-03-20 13:28:31 -07:00
|
|
|
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_ALREADY_NAVIGATED);
|
|
|
|
this.setValueIgnoreMutation(oldValue);
|
|
|
|
return;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
|
|
|
if (newValue === 'persist:') {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.validPartitionId = false;
|
|
|
|
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE);
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Attribute that handles the location and navigation of the webview.
|
2020-11-04 00:02:23 +01:00
|
|
|
export class SrcAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
public observer!: MutationObserver;
|
|
|
|
|
|
|
|
constructor (public webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, webViewImpl);
|
|
|
|
this.setupMutationObserver();
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-15 09:57:36 -08:00
|
|
|
|
2019-03-07 15:26:23 -08:00
|
|
|
public getValue () {
|
2016-01-15 14:28:12 -08:00
|
|
|
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) {
|
2020-03-20 13:28:31 -07:00
|
|
|
return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name));
|
2016-01-15 14:28:12 -08:00
|
|
|
} else {
|
2020-03-20 13:28:31 -07:00
|
|
|
return this.value;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:26:23 -08:00
|
|
|
public setValueIgnoreMutation (value: any) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super.setValueIgnoreMutation(value);
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// takeRecords() is needed to clear queued up src mutations. Without it, it
|
2020-10-13 10:25:21 -07:00
|
|
|
// is possible for this change to get picked up asynchronously by src's
|
2016-01-15 14:28:12 -08:00
|
|
|
// mutation observer |observer|, and then get handled even though we do not
|
|
|
|
// want to handle this mutation.
|
2020-03-20 13:28:31 -07:00
|
|
|
this.observer.takeRecords();
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-03-07 17:18:10 -08:00
|
|
|
public handleMutation = (oldValue: any, newValue: any) => {
|
2016-01-15 14:28:12 -08:00
|
|
|
// Once we have navigated, we don't allow clearing the src attribute.
|
|
|
|
// Once <webview> enters a navigated state, it cannot return to a
|
|
|
|
// placeholder state.
|
|
|
|
if (!newValue && oldValue) {
|
|
|
|
// src attribute changes normally initiate a navigation. We suppress
|
|
|
|
// the next src attribute handler call to avoid reloading the page
|
|
|
|
// on every guest-initiated navigation.
|
2020-03-20 13:28:31 -07:00
|
|
|
this.setValueIgnoreMutation(oldValue);
|
|
|
|
return;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2020-03-20 13:28:31 -07:00
|
|
|
this.parse();
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// The purpose of this mutation observer is to catch assignment to the src
|
|
|
|
// attribute without any changes to its value. This is useful in the case
|
|
|
|
// where the webview guest has crashed and navigating to the same address
|
|
|
|
// spawns off a new process.
|
2019-03-07 15:26:23 -08:00
|
|
|
public setupMutationObserver () {
|
2016-03-11 14:08:14 -08:00
|
|
|
this.observer = new MutationObserver((mutations) => {
|
2016-11-03 11:09:53 -07:00
|
|
|
for (const mutation of mutations) {
|
2020-03-20 13:28:31 -07:00
|
|
|
const { oldValue } = mutation;
|
|
|
|
const newValue = this.getValue();
|
2016-03-11 14:08:14 -08:00
|
|
|
if (oldValue !== newValue) {
|
2020-03-20 13:28:31 -07:00
|
|
|
return;
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2020-03-20 13:28:31 -07:00
|
|
|
this.handleMutation(oldValue, newValue);
|
2016-03-11 14:08:14 -08:00
|
|
|
}
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2016-11-03 10:39:40 -07:00
|
|
|
const params = {
|
2016-01-15 14:28:12 -08:00
|
|
|
attributes: true,
|
|
|
|
attributeOldValue: true,
|
|
|
|
attributeFilter: [this.name]
|
2020-03-20 13:28:31 -07:00
|
|
|
};
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
this.observer.observe(this.webViewImpl.webviewNode, params);
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-15 14:28:12 -08:00
|
|
|
|
2019-03-07 15:26:23 -08:00
|
|
|
public parse () {
|
2020-11-04 00:02:23 +01:00
|
|
|
if (!this.webViewImpl.elementAttached || !(this.webViewImpl.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION) as PartitionAttribute).validPartitionId || !this.getValue()) {
|
2020-03-20 13:28:31 -07:00
|
|
|
return;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
|
|
|
if (this.webViewImpl.guestInstanceId == null) {
|
|
|
|
if (this.webViewImpl.beforeFirstNavigation) {
|
2020-03-20 13:28:31 -07:00
|
|
|
this.webViewImpl.beforeFirstNavigation = false;
|
|
|
|
this.webViewImpl.createGuest();
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2020-03-20 13:28:31 -07:00
|
|
|
return;
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
// Navigate to |this.src|.
|
2020-03-20 13:28:31 -07:00
|
|
|
const opts: Record<string, string> = {};
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2020-11-04 00:02:23 +01:00
|
|
|
const httpreferrer = this.webViewImpl.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER)!.getValue();
|
2016-01-15 14:28:12 -08:00
|
|
|
if (httpreferrer) {
|
2020-03-20 13:28:31 -07:00
|
|
|
opts.httpReferrer = httpreferrer;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2020-11-04 00:02:23 +01:00
|
|
|
const useragent = this.webViewImpl.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT)!.getValue();
|
2016-01-15 14:28:12 -08:00
|
|
|
if (useragent) {
|
2020-03-20 13:28:31 -07:00
|
|
|
opts.userAgent = useragent;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2018-10-01 03:07:50 +02:00
|
|
|
|
2022-10-27 15:16:26 +08:00
|
|
|
(this.webViewImpl.webviewNode as Electron.WebviewTag).loadURL(this.getValue(), opts)
|
|
|
|
.catch(err => {
|
|
|
|
console.error('Unexpected error while loading URL', err);
|
|
|
|
});
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-15 09:57:36 -08:00
|
|
|
// Attribute specifies HTTP referrer.
|
2016-01-15 14:28:12 -08:00
|
|
|
class HttpReferrerAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
constructor (webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, webViewImpl);
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Attribute specifies user agent
|
2016-01-15 14:28:12 -08:00
|
|
|
class UserAgentAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
constructor (webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, webViewImpl);
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Attribute that set preload script.
|
2016-01-15 15:09:25 -08:00
|
|
|
class PreloadAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
constructor (webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, webViewImpl);
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-15 14:28:12 -08:00
|
|
|
|
2019-03-07 15:26:23 -08:00
|
|
|
public getValue () {
|
2016-01-15 14:28:12 -08:00
|
|
|
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) {
|
2020-03-20 13:28:31 -07:00
|
|
|
return this.value;
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
let preload = resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name));
|
|
|
|
const protocol = preload.substr(0, 5);
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2016-01-15 14:28:12 -08:00
|
|
|
if (protocol !== 'file:') {
|
2020-03-20 13:28:31 -07:00
|
|
|
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE);
|
|
|
|
preload = '';
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2019-03-07 15:26:23 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
return preload;
|
2016-01-15 09:57:36 -08:00
|
|
|
}
|
2016-01-15 14:28:12 -08:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-21 15:43:41 +05:30
|
|
|
// Attribute that specifies the blink features to be enabled.
|
|
|
|
class BlinkFeaturesAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
constructor (webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, webViewImpl);
|
2016-01-21 15:43:41 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:49:31 -07:00
|
|
|
// Attribute that specifies the blink features to be disabled.
|
|
|
|
class DisableBlinkFeaturesAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
constructor (webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES, webViewImpl);
|
2016-06-07 13:49:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 17:16:39 -05:00
|
|
|
// Attribute that specifies the web preferences to be enabled.
|
|
|
|
class WebPreferencesAttribute extends WebViewAttribute {
|
2019-03-07 15:26:23 -08:00
|
|
|
constructor (webViewImpl: WebViewImpl) {
|
2020-03-20 13:28:31 -07:00
|
|
|
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES, webViewImpl);
|
2016-10-14 17:16:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Sets up all of the webview attributes.
|
2021-05-07 09:07:47 +02:00
|
|
|
export function setupWebViewAttributes (self: WebViewImpl) {
|
|
|
|
return new Map<string, WebViewAttribute>([
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, new PartitionAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, new SrcAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, new HttpReferrerAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, new UserAgentAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, new PreloadAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, new BlinkFeaturesAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES, new DisableBlinkFeaturesAttribute(self)],
|
|
|
|
[WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES, new WebPreferencesAttribute(self)]
|
|
|
|
]);
|
|
|
|
}
|