From 2f79444535440608f9ddf2f26c634847396674b7 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Wed, 25 Jan 2023 16:08:46 +0100 Subject: [PATCH] refactor: separate WEB_VIEW_ATTRIBUTES / WEB_VIEW_ERROR_MESSAGES (#36972) --- lib/renderer/web-view/web-view-attributes.ts | 56 ++++++++++---------- lib/renderer/web-view/web-view-constants.ts | 45 ++++++++-------- lib/renderer/web-view/web-view-element.ts | 32 +++++------ lib/renderer/web-view/web-view-impl.ts | 10 ++-- 4 files changed, 72 insertions(+), 71 deletions(-) diff --git a/lib/renderer/web-view/web-view-attributes.ts b/lib/renderer/web-view/web-view-attributes.ts index 6a8e1d8728ea..ff971e494bce 100644 --- a/lib/renderer/web-view/web-view-attributes.ts +++ b/lib/renderer/web-view/web-view-attributes.ts @@ -1,5 +1,5 @@ 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) { return url ? new URL(url, location.href).href : ''; @@ -76,7 +76,7 @@ export class PartitionAttribute extends WebViewAttribute { public validPartitionId = true constructor (public webViewImpl: WebViewImpl) { - super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, webViewImpl); + super(WEB_VIEW_ATTRIBUTES.PARTITION, webViewImpl); } 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. if (!this.webViewImpl.beforeFirstNavigation) { - console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_ALREADY_NAVIGATED); + console.error(WEB_VIEW_ERROR_MESSAGES.ALREADY_NAVIGATED); this.setValueIgnoreMutation(oldValue); return; } if (newValue === 'persist:') { 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; constructor (public webViewImpl: WebViewImpl) { - super(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, webViewImpl); + super(WEB_VIEW_ATTRIBUTES.SRC, webViewImpl); this.setupMutationObserver(); } @@ -162,7 +162,7 @@ export class SrcAttribute extends WebViewAttribute { } 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; } if (this.webViewImpl.guestInstanceId == null) { @@ -176,12 +176,12 @@ export class SrcAttribute extends WebViewAttribute { // Navigate to |this.src|. const opts: Record = {}; - 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) { 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) { opts.userAgent = useragent; } @@ -196,21 +196,21 @@ export class SrcAttribute extends WebViewAttribute { // Attribute specifies HTTP referrer. class HttpReferrerAttribute extends WebViewAttribute { constructor (webViewImpl: WebViewImpl) { - super(WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, webViewImpl); + super(WEB_VIEW_ATTRIBUTES.HTTPREFERRER, webViewImpl); } } // Attribute specifies user agent class UserAgentAttribute extends WebViewAttribute { constructor (webViewImpl: WebViewImpl) { - super(WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, webViewImpl); + super(WEB_VIEW_ATTRIBUTES.USERAGENT, webViewImpl); } } // Attribute that set preload script. class PreloadAttribute extends WebViewAttribute { constructor (webViewImpl: WebViewImpl) { - super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, webViewImpl); + super(WEB_VIEW_ATTRIBUTES.PRELOAD, webViewImpl); } public getValue () { @@ -222,7 +222,7 @@ class PreloadAttribute extends WebViewAttribute { const protocol = preload.substr(0, 5); if (protocol !== 'file:') { - console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE); + console.error(WEB_VIEW_ERROR_MESSAGES.INVALID_PRELOAD_ATTRIBUTE); preload = ''; } @@ -233,39 +233,39 @@ class PreloadAttribute extends WebViewAttribute { // Attribute that specifies the blink features to be enabled. class BlinkFeaturesAttribute extends WebViewAttribute { 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. class DisableBlinkFeaturesAttribute extends WebViewAttribute { 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. class WebPreferencesAttribute extends WebViewAttribute { constructor (webViewImpl: WebViewImpl) { - super(WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES, webViewImpl); + super(WEB_VIEW_ATTRIBUTES.WEBPREFERENCES, webViewImpl); } } // Sets up all of the webview attributes. export function setupWebViewAttributes (self: WebViewImpl) { return new Map([ - [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)] + [WEB_VIEW_ATTRIBUTES.PARTITION, new PartitionAttribute(self)], + [WEB_VIEW_ATTRIBUTES.SRC, new SrcAttribute(self)], + [WEB_VIEW_ATTRIBUTES.HTTPREFERRER, new HttpReferrerAttribute(self)], + [WEB_VIEW_ATTRIBUTES.USERAGENT, new UserAgentAttribute(self)], + [WEB_VIEW_ATTRIBUTES.NODEINTEGRATION, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.NODEINTEGRATION, self)], + [WEB_VIEW_ATTRIBUTES.NODEINTEGRATIONINSUBFRAMES, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.NODEINTEGRATIONINSUBFRAMES, self)], + [WEB_VIEW_ATTRIBUTES.PLUGINS, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.PLUGINS, self)], + [WEB_VIEW_ATTRIBUTES.DISABLEWEBSECURITY, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.DISABLEWEBSECURITY, self)], + [WEB_VIEW_ATTRIBUTES.ALLOWPOPUPS, new BooleanAttribute(WEB_VIEW_ATTRIBUTES.ALLOWPOPUPS, self)], + [WEB_VIEW_ATTRIBUTES.PRELOAD, new PreloadAttribute(self)], + [WEB_VIEW_ATTRIBUTES.BLINKFEATURES, new BlinkFeaturesAttribute(self)], + [WEB_VIEW_ATTRIBUTES.DISABLEBLINKFEATURES, new DisableBlinkFeaturesAttribute(self)], + [WEB_VIEW_ATTRIBUTES.WEBPREFERENCES, new WebPreferencesAttribute(self)] ]); } diff --git a/lib/renderer/web-view/web-view-constants.ts b/lib/renderer/web-view/web-view-constants.ts index bfd56212a2f7..393a680fde14 100644 --- a/lib/renderer/web-view/web-view-constants.ts +++ b/lib/renderer/web-view/web-view-constants.ts @@ -1,23 +1,24 @@ -export const enum WEB_VIEW_CONSTANTS { - // Attributes. - ATTRIBUTE_NAME = 'name', - ATTRIBUTE_PARTITION = 'partition', - ATTRIBUTE_SRC = 'src', - ATTRIBUTE_HTTPREFERRER = 'httpreferrer', - ATTRIBUTE_NODEINTEGRATION = 'nodeintegration', - ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES = 'nodeintegrationinsubframes', - ATTRIBUTE_PLUGINS = 'plugins', - ATTRIBUTE_DISABLEWEBSECURITY = 'disablewebsecurity', - ATTRIBUTE_ALLOWPOPUPS = 'allowpopups', - ATTRIBUTE_PRELOAD = 'preload', - ATTRIBUTE_USERAGENT = 'useragent', - ATTRIBUTE_BLINKFEATURES = 'blinkfeatures', - ATTRIBUTE_DISABLEBLINKFEATURES = 'disableblinkfeatures', - ATTRIBUTE_WEBPREFERENCES = 'webpreferences', - - // Error messages. - ERROR_MSG_ALREADY_NAVIGATED = 'The object has already navigated, so its partition cannot be changed.', - ERROR_MSG_INVALID_PARTITION_ATTRIBUTE = 'Invalid partition attribute.', - ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE = 'Only "file:" protocol is supported in "preload" 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.' +export const enum WEB_VIEW_ATTRIBUTES { + NAME = 'name', + PARTITION = 'partition', + SRC = 'src', + HTTPREFERRER = 'httpreferrer', + NODEINTEGRATION = 'nodeintegration', + NODEINTEGRATIONINSUBFRAMES = 'nodeintegrationinsubframes', + PLUGINS = 'plugins', + DISABLEWEBSECURITY = 'disablewebsecurity', + ALLOWPOPUPS = 'allowpopups', + PRELOAD = 'preload', + USERAGENT = 'useragent', + BLINKFEATURES = 'blinkfeatures', + DISABLEBLINKFEATURES = 'disableblinkfeatures', + WEBPREFERENCES = 'webpreferences', +} + +export const enum WEB_VIEW_ERROR_MESSAGES { + // Error messages. + ALREADY_NAVIGATED = 'The object has already navigated, so its partition cannot be changed.', + INVALID_PARTITION_ATTRIBUTE = 'Invalid partition attribute.', + 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.' } diff --git a/lib/renderer/web-view/web-view-element.ts b/lib/renderer/web-view/web-view-element.ts index ee290ce09b0f..005c41ba2417 100644 --- a/lib/renderer/web-view/web-view-element.ts +++ b/lib/renderer/web-view/web-view-element.ts @@ -8,7 +8,7 @@ // which runs in browserify environment instead of Node environment, all native // 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 type { SrcAttribute } from '@electron/internal/renderer/web-view/web-view-attributes'; @@ -19,19 +19,19 @@ const defineWebViewElement = (hooks: WebViewImplHooks) => { return class WebViewElement extends HTMLElement { static get observedAttributes () { return [ - WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, - WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, - WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, - WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, - WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, - WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, - WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, - WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, - WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, - WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, - WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, - WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES, - WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES + WEB_VIEW_ATTRIBUTES.PARTITION, + WEB_VIEW_ATTRIBUTES.SRC, + WEB_VIEW_ATTRIBUTES.HTTPREFERRER, + WEB_VIEW_ATTRIBUTES.USERAGENT, + WEB_VIEW_ATTRIBUTES.NODEINTEGRATION, + WEB_VIEW_ATTRIBUTES.NODEINTEGRATIONINSUBFRAMES, + WEB_VIEW_ATTRIBUTES.PLUGINS, + WEB_VIEW_ATTRIBUTES.DISABLEWEBSECURITY, + WEB_VIEW_ATTRIBUTES.ALLOWPOPUPS, + WEB_VIEW_ATTRIBUTES.PRELOAD, + WEB_VIEW_ATTRIBUTES.BLINKFEATURES, + WEB_VIEW_ATTRIBUTES.DISABLEBLINKFEATURES, + WEB_VIEW_ATTRIBUTES.WEBPREFERENCES ]; } @@ -43,7 +43,7 @@ const defineWebViewElement = (hooks: WebViewImplHooks) => { getWebContentsId () { const internal = internals.get(this); 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; } @@ -58,7 +58,7 @@ const defineWebViewElement = (hooks: WebViewImplHooks) => { dispatchEvent: internal.dispatchEvent.bind(internal) }); internal.elementAttached = true; - (internal.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC) as SrcAttribute).parse(); + (internal.attributes.get(WEB_VIEW_ATTRIBUTES.SRC) as SrcAttribute).parse(); } } diff --git a/lib/renderer/web-view/web-view-impl.ts b/lib/renderer/web-view/web-view-impl.ts index cef62fd2c769..d071a41abdb8 100644 --- a/lib/renderer/web-view/web-view-impl.ts +++ b/lib/renderer/web-view/web-view-impl.ts @@ -1,5 +1,5 @@ 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 type { WebViewAttribute, PartitionAttribute } 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.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 // internal iframe element to be local again before we can reattach. @@ -145,13 +145,13 @@ export class WebViewImpl { // Updates state upon loadcommit. onLoadCommit (props: Record) { - const oldValue = this.webviewNode.getAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC); + const oldValue = this.webviewNode.getAttribute(WEB_VIEW_ATTRIBUTES.SRC); const newValue = props.url; if (props.isMainFrame && (oldValue !== newValue)) { // Touching the src attribute triggers a navigation. To avoid // triggering a page reload on every guest-initiated navigation, // 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) { - return this.attributes.get(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION)!.setValue(storagePartitionId); + return this.attributes.get(WEB_VIEW_ATTRIBUTES.PARTITION)!.setValue(storagePartitionId); } buildParams () {