refactor: separate WEB_VIEW_ATTRIBUTES / WEB_VIEW_ERROR_MESSAGES (#36972)

This commit is contained in:
Milan Burda 2023-01-25 16:08:46 +01:00 committed by GitHub
parent a9b6041d38
commit 2f79444535
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 71 deletions

View file

@ -1,5 +1,5 @@
import type { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl'; import type { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl';
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'; import { WEB_VIEW_ATTRIBUTES, WEB_VIEW_ERROR_MESSAGES } from '@electron/internal/renderer/web-view/web-view-constants';
const resolveURL = function (url?: string | null) { const resolveURL = function (url?: string | null) {
return url ? new URL(url, location.href).href : ''; return url ? new URL(url, location.href).href : '';
@ -76,7 +76,7 @@ export class PartitionAttribute extends WebViewAttribute {
public validPartitionId = true public validPartitionId = true
constructor (public webViewImpl: WebViewImpl) { constructor (public webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, webViewImpl); super(WEB_VIEW_ATTRIBUTES.PARTITION, webViewImpl);
} }
public handleMutation = (oldValue: any, newValue: any) => { public handleMutation = (oldValue: any, newValue: any) => {
@ -84,13 +84,13 @@ export class PartitionAttribute extends WebViewAttribute {
// The partition cannot change if the webview has already navigated. // The partition cannot change if the webview has already navigated.
if (!this.webViewImpl.beforeFirstNavigation) { if (!this.webViewImpl.beforeFirstNavigation) {
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_ALREADY_NAVIGATED); console.error(WEB_VIEW_ERROR_MESSAGES.ALREADY_NAVIGATED);
this.setValueIgnoreMutation(oldValue); this.setValueIgnoreMutation(oldValue);
return; return;
} }
if (newValue === 'persist:') { if (newValue === 'persist:') {
this.validPartitionId = false; this.validPartitionId = false;
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE); console.error(WEB_VIEW_ERROR_MESSAGES.INVALID_PARTITION_ATTRIBUTE);
} }
} }
} }
@ -100,7 +100,7 @@ export class SrcAttribute extends WebViewAttribute {
public observer!: MutationObserver; public observer!: MutationObserver;
constructor (public webViewImpl: WebViewImpl) { constructor (public webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, webViewImpl); super(WEB_VIEW_ATTRIBUTES.SRC, webViewImpl);
this.setupMutationObserver(); this.setupMutationObserver();
} }
@ -162,7 +162,7 @@ export class SrcAttribute extends WebViewAttribute {
} }
public parse () { public parse () {
if (!this.webViewImpl.elementAttached || !(this.webViewImpl.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION) as PartitionAttribute).validPartitionId || !this.getValue()) { if (!this.webViewImpl.elementAttached || !(this.webViewImpl.attributes.get(WEB_VIEW_ATTRIBUTES.PARTITION) as PartitionAttribute).validPartitionId || !this.getValue()) {
return; return;
} }
if (this.webViewImpl.guestInstanceId == null) { if (this.webViewImpl.guestInstanceId == null) {
@ -176,12 +176,12 @@ export class SrcAttribute extends WebViewAttribute {
// Navigate to |this.src|. // Navigate to |this.src|.
const opts: Record<string, string> = {}; const opts: Record<string, string> = {};
const httpreferrer = this.webViewImpl.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER)!.getValue(); const httpreferrer = this.webViewImpl.attributes.get(WEB_VIEW_ATTRIBUTES.HTTPREFERRER)!.getValue();
if (httpreferrer) { if (httpreferrer) {
opts.httpReferrer = httpreferrer; opts.httpReferrer = httpreferrer;
} }
const useragent = this.webViewImpl.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT)!.getValue(); const useragent = this.webViewImpl.attributes.get(WEB_VIEW_ATTRIBUTES.USERAGENT)!.getValue();
if (useragent) { if (useragent) {
opts.userAgent = useragent; opts.userAgent = useragent;
} }
@ -196,21 +196,21 @@ export class SrcAttribute extends WebViewAttribute {
// Attribute specifies HTTP referrer. // Attribute specifies HTTP referrer.
class HttpReferrerAttribute extends WebViewAttribute { class HttpReferrerAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) { constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, webViewImpl); super(WEB_VIEW_ATTRIBUTES.HTTPREFERRER, webViewImpl);
} }
} }
// Attribute specifies user agent // Attribute specifies user agent
class UserAgentAttribute extends WebViewAttribute { class UserAgentAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) { constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, webViewImpl); super(WEB_VIEW_ATTRIBUTES.USERAGENT, webViewImpl);
} }
} }
// Attribute that set preload script. // Attribute that set preload script.
class PreloadAttribute extends WebViewAttribute { class PreloadAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) { constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, webViewImpl); super(WEB_VIEW_ATTRIBUTES.PRELOAD, webViewImpl);
} }
public getValue () { public getValue () {
@ -222,7 +222,7 @@ class PreloadAttribute extends WebViewAttribute {
const protocol = preload.substr(0, 5); const protocol = preload.substr(0, 5);
if (protocol !== 'file:') { if (protocol !== 'file:') {
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE); console.error(WEB_VIEW_ERROR_MESSAGES.INVALID_PRELOAD_ATTRIBUTE);
preload = ''; preload = '';
} }
@ -233,39 +233,39 @@ class PreloadAttribute extends WebViewAttribute {
// Attribute that specifies the blink features to be enabled. // Attribute that specifies the blink features to be enabled.
class BlinkFeaturesAttribute extends WebViewAttribute { class BlinkFeaturesAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) { constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, webViewImpl); super(WEB_VIEW_ATTRIBUTES.BLINKFEATURES, webViewImpl);
} }
} }
// Attribute that specifies the blink features to be disabled. // Attribute that specifies the blink features to be disabled.
class DisableBlinkFeaturesAttribute extends WebViewAttribute { class DisableBlinkFeaturesAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) { constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES, webViewImpl); super(WEB_VIEW_ATTRIBUTES.DISABLEBLINKFEATURES, webViewImpl);
} }
} }
// Attribute that specifies the web preferences to be enabled. // Attribute that specifies the web preferences to be enabled.
class WebPreferencesAttribute extends WebViewAttribute { class WebPreferencesAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) { constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES, webViewImpl); super(WEB_VIEW_ATTRIBUTES.WEBPREFERENCES, webViewImpl);
} }
} }
// Sets up all of the webview attributes. // Sets up all of the webview attributes.
export function setupWebViewAttributes (self: WebViewImpl) { export function setupWebViewAttributes (self: WebViewImpl) {
return new Map<string, WebViewAttribute>([ return new Map<string, WebViewAttribute>([
[WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, new PartitionAttribute(self)], [WEB_VIEW_ATTRIBUTES.PARTITION, new PartitionAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, new SrcAttribute(self)], [WEB_VIEW_ATTRIBUTES.SRC, new SrcAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, new HttpReferrerAttribute(self)], [WEB_VIEW_ATTRIBUTES.HTTPREFERRER, new HttpReferrerAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, new UserAgentAttribute(self)], [WEB_VIEW_ATTRIBUTES.USERAGENT, new UserAgentAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, self)], [WEB_VIEW_ATTRIBUTES.NODEINTEGRATION, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.NODEINTEGRATION, self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, self)], [WEB_VIEW_ATTRIBUTES.NODEINTEGRATIONINSUBFRAMES, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.NODEINTEGRATIONINSUBFRAMES, self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, self)], [WEB_VIEW_ATTRIBUTES.PLUGINS, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.PLUGINS, self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, self)], [WEB_VIEW_ATTRIBUTES.DISABLEWEBSECURITY, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.DISABLEWEBSECURITY, self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, self)], [WEB_VIEW_ATTRIBUTES.ALLOWPOPUPS, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.ALLOWPOPUPS, self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, new PreloadAttribute(self)], [WEB_VIEW_ATTRIBUTES.PRELOAD, new PreloadAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, new BlinkFeaturesAttribute(self)], [WEB_VIEW_ATTRIBUTES.BLINKFEATURES, new BlinkFeaturesAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES, new DisableBlinkFeaturesAttribute(self)], [WEB_VIEW_ATTRIBUTES.DISABLEBLINKFEATURES, new DisableBlinkFeaturesAttribute(self)],
[WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES, new WebPreferencesAttribute(self)] [WEB_VIEW_ATTRIBUTES.WEBPREFERENCES, new WebPreferencesAttribute(self)]
]); ]);
} }

View file

@ -1,23 +1,24 @@
export const enum WEB_VIEW_CONSTANTS { export const enum WEB_VIEW_ATTRIBUTES {
// Attributes. NAME = 'name',
ATTRIBUTE_NAME = 'name', PARTITION = 'partition',
ATTRIBUTE_PARTITION = 'partition', SRC = 'src',
ATTRIBUTE_SRC = 'src', HTTPREFERRER = 'httpreferrer',
ATTRIBUTE_HTTPREFERRER = 'httpreferrer', NODEINTEGRATION = 'nodeintegration',
ATTRIBUTE_NODEINTEGRATION = 'nodeintegration', NODEINTEGRATIONINSUBFRAMES = 'nodeintegrationinsubframes',
ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES = 'nodeintegrationinsubframes', PLUGINS = 'plugins',
ATTRIBUTE_PLUGINS = 'plugins', DISABLEWEBSECURITY = 'disablewebsecurity',
ATTRIBUTE_DISABLEWEBSECURITY = 'disablewebsecurity', ALLOWPOPUPS = 'allowpopups',
ATTRIBUTE_ALLOWPOPUPS = 'allowpopups', PRELOAD = 'preload',
ATTRIBUTE_PRELOAD = 'preload', USERAGENT = 'useragent',
ATTRIBUTE_USERAGENT = 'useragent', BLINKFEATURES = 'blinkfeatures',
ATTRIBUTE_BLINKFEATURES = 'blinkfeatures', DISABLEBLINKFEATURES = 'disableblinkfeatures',
ATTRIBUTE_DISABLEBLINKFEATURES = 'disableblinkfeatures', WEBPREFERENCES = 'webpreferences',
ATTRIBUTE_WEBPREFERENCES = 'webpreferences', }
// Error messages. export const enum WEB_VIEW_ERROR_MESSAGES {
ERROR_MSG_ALREADY_NAVIGATED = 'The object has already navigated, so its partition cannot be changed.', // Error messages.
ERROR_MSG_INVALID_PARTITION_ATTRIBUTE = 'Invalid partition attribute.', ALREADY_NAVIGATED = 'The object has already navigated, so its partition cannot be changed.',
ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE = 'Only "file:" protocol is supported in "preload" attribute.', INVALID_PARTITION_ATTRIBUTE = 'Invalid partition attribute.',
ERROR_MSG_NOT_ATTACHED = 'The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.' INVALID_PRELOAD_ATTRIBUTE = 'Only "file:" protocol is supported in "preload" attribute.',
NOT_ATTACHED = 'The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.'
} }

View file

@ -8,7 +8,7 @@
// which runs in browserify environment instead of Node environment, all native // which runs in browserify environment instead of Node environment, all native
// modules must be passed from outside, all included files must be plain JS. // modules must be passed from outside, all included files must be plain JS.
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'; import { WEB_VIEW_ATTRIBUTES, WEB_VIEW_ERROR_MESSAGES } from '@electron/internal/renderer/web-view/web-view-constants';
import { WebViewImpl, WebViewImplHooks, setupMethods } from '@electron/internal/renderer/web-view/web-view-impl'; import { WebViewImpl, WebViewImplHooks, setupMethods } from '@electron/internal/renderer/web-view/web-view-impl';
import type { SrcAttribute } from '@electron/internal/renderer/web-view/web-view-attributes'; import type { SrcAttribute } from '@electron/internal/renderer/web-view/web-view-attributes';
@ -19,19 +19,19 @@ const defineWebViewElement = (hooks: WebViewImplHooks) => {
return class WebViewElement extends HTMLElement { return class WebViewElement extends HTMLElement {
static get observedAttributes () { static get observedAttributes () {
return [ return [
WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, WEB_VIEW_ATTRIBUTES.PARTITION,
WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, WEB_VIEW_ATTRIBUTES.SRC,
WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, WEB_VIEW_ATTRIBUTES.HTTPREFERRER,
WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, WEB_VIEW_ATTRIBUTES.USERAGENT,
WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, WEB_VIEW_ATTRIBUTES.NODEINTEGRATION,
WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, WEB_VIEW_ATTRIBUTES.NODEINTEGRATIONINSUBFRAMES,
WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, WEB_VIEW_ATTRIBUTES.PLUGINS,
WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, WEB_VIEW_ATTRIBUTES.DISABLEWEBSECURITY,
WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, WEB_VIEW_ATTRIBUTES.ALLOWPOPUPS,
WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, WEB_VIEW_ATTRIBUTES.PRELOAD,
WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, WEB_VIEW_ATTRIBUTES.BLINKFEATURES,
WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES, WEB_VIEW_ATTRIBUTES.DISABLEBLINKFEATURES,
WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES WEB_VIEW_ATTRIBUTES.WEBPREFERENCES
]; ];
} }
@ -43,7 +43,7 @@ const defineWebViewElement = (hooks: WebViewImplHooks) => {
getWebContentsId () { getWebContentsId () {
const internal = internals.get(this); const internal = internals.get(this);
if (!internal || !internal.guestInstanceId) { if (!internal || !internal.guestInstanceId) {
throw new Error(WEB_VIEW_CONSTANTS.ERROR_MSG_NOT_ATTACHED); throw new Error(WEB_VIEW_ERROR_MESSAGES.NOT_ATTACHED);
} }
return internal.guestInstanceId; return internal.guestInstanceId;
} }
@ -58,7 +58,7 @@ const defineWebViewElement = (hooks: WebViewImplHooks) => {
dispatchEvent: internal.dispatchEvent.bind(internal) dispatchEvent: internal.dispatchEvent.bind(internal)
}); });
internal.elementAttached = true; internal.elementAttached = true;
(internal.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC) as SrcAttribute).parse(); (internal.attributes.get(WEB_VIEW_ATTRIBUTES.SRC) as SrcAttribute).parse();
} }
} }

View file

@ -1,5 +1,5 @@
import type * as guestViewInternalModule from '@electron/internal/renderer/web-view/guest-view-internal'; import type * as guestViewInternalModule from '@electron/internal/renderer/web-view/guest-view-internal';
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'; import { WEB_VIEW_ATTRIBUTES } from '@electron/internal/renderer/web-view/web-view-constants';
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods'; import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods';
import type { WebViewAttribute, PartitionAttribute } from '@electron/internal/renderer/web-view/web-view-attributes'; import type { WebViewAttribute, PartitionAttribute } from '@electron/internal/renderer/web-view/web-view-attributes';
import { setupWebViewAttributes } from '@electron/internal/renderer/web-view/web-view-attributes'; import { setupWebViewAttributes } from '@electron/internal/renderer/web-view/web-view-attributes';
@ -75,7 +75,7 @@ export class WebViewImpl {
} }
this.beforeFirstNavigation = true; this.beforeFirstNavigation = true;
(this.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION) as PartitionAttribute).validPartitionId = true; (this.attributes.get(WEB_VIEW_ATTRIBUTES.PARTITION) as PartitionAttribute).validPartitionId = true;
// Since attachment swaps a local frame for a remote frame, we need our // Since attachment swaps a local frame for a remote frame, we need our
// internal iframe element to be local again before we can reattach. // internal iframe element to be local again before we can reattach.
@ -145,13 +145,13 @@ export class WebViewImpl {
// Updates state upon loadcommit. // Updates state upon loadcommit.
onLoadCommit (props: Record<string, any>) { onLoadCommit (props: Record<string, any>) {
const oldValue = this.webviewNode.getAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC); const oldValue = this.webviewNode.getAttribute(WEB_VIEW_ATTRIBUTES.SRC);
const newValue = props.url; const newValue = props.url;
if (props.isMainFrame && (oldValue !== newValue)) { if (props.isMainFrame && (oldValue !== newValue)) {
// Touching the src attribute triggers a navigation. To avoid // Touching the src attribute triggers a navigation. To avoid
// triggering a page reload on every guest-initiated navigation, // triggering a page reload on every guest-initiated navigation,
// we do not handle this mutation. // we do not handle this mutation.
this.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC)!.setValueIgnoreMutation(newValue); this.attributes.get(WEB_VIEW_ATTRIBUTES.SRC)!.setValueIgnoreMutation(newValue);
} }
} }
@ -165,7 +165,7 @@ export class WebViewImpl {
} }
onAttach (storagePartitionId: number) { onAttach (storagePartitionId: number) {
return this.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION)!.setValue(storagePartitionId); return this.attributes.get(WEB_VIEW_ATTRIBUTES.PARTITION)!.setValue(storagePartitionId);
} }
buildParams () { buildParams () {