electron/lib/renderer/web-view/web-view-attributes.ts

293 lines
10 KiB
TypeScript
Raw Normal View History

import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
import { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl'
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Helper function to resolve url set in attribute.
2016-11-03 17:39:40 +00:00
const a = document.createElement('a')
2016-01-12 02:40:23 +00:00
const resolveURL = function (url?: string | null) {
if (!url) return ''
2016-03-25 19:57:17 +00:00
a.href = url
return a.href
}
2016-01-12 02:40:23 +00:00
interface MutationHandler {
handleMutation (_oldValue: any, _newValue: any): any;
}
2016-01-14 19:10:12 +00:00
// Attribute objects.
// Default implementation of a WebView attribute.
class WebViewAttribute implements MutationHandler {
public value: any;
public ignoreMutation = false;
constructor (public name: string, public webViewImpl: WebViewImpl) {
2016-03-25 19:57:17 +00:00
this.name = name
this.value = (webViewImpl.webviewNode as Record<string, any>)[name] || ''
2016-03-25 19:57:17 +00:00
this.webViewImpl = webViewImpl
this.defineProperty()
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// Retrieves and returns the attribute's value.
public getValue () {
2016-03-25 19:57:17 +00:00
return this.webViewImpl.webviewNode.getAttribute(this.name) || this.value
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// Sets the attribute's value.
public setValue (value: any) {
2016-11-03 17:19:52 +00:00
this.webViewImpl.webviewNode.setAttribute(this.name, value || '')
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// Changes the attribute's value without triggering its mutation handler.
public setValueIgnoreMutation (value: any) {
2016-03-25 19:57:17 +00:00
this.ignoreMutation = true
this.setValue(value)
2016-03-29 00:35:49 +00:00
this.ignoreMutation = false
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// Defines this attribute as a property on the webview node.
public defineProperty () {
2016-01-15 22:28:12 +00:00
return Object.defineProperty(this.webViewImpl.webviewNode, this.name, {
2016-03-11 22:08:14 +00:00
get: () => {
2016-03-25 19:57:17 +00:00
return this.getValue()
2016-03-11 22:08:14 +00:00
},
set: (value) => {
2016-03-25 19:57:17 +00:00
return this.setValue(value)
2016-03-11 22:08:14 +00:00
},
2016-01-15 22:28:12 +00:00
enumerable: true
2016-03-25 19:57:17 +00:00
})
2016-01-15 23:10:26 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// Called when the attribute's value changes.
public handleMutation: MutationHandler['handleMutation'] = () => undefined as any
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// An attribute that is treated as a Boolean.
class BooleanAttribute extends WebViewAttribute {
2016-03-25 19:57:17 +00:00
getValue () {
return this.webViewImpl.webviewNode.hasAttribute(this.name)
2016-01-12 02:40:23 +00:00
}
setValue (value: boolean) {
2016-11-03 18:37:11 +00:00
if (value) {
2016-11-03 17:19:52 +00:00
this.webViewImpl.webviewNode.setAttribute(this.name, '')
2016-11-03 18:37:11 +00:00
} else {
this.webViewImpl.webviewNode.removeAttribute(this.name)
2016-01-15 22:28:12 +00:00
}
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Attribute representing the state of the storage partition.
2016-01-15 22:28:12 +00:00
class PartitionAttribute extends WebViewAttribute {
public validPartitionId = true
constructor (public webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION, webViewImpl)
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
public handleMutation = (oldValue: any, newValue: any) => {
2016-03-25 19:57:17 +00:00
newValue = newValue || ''
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// The partition cannot change if the webview has already navigated.
if (!this.webViewImpl.beforeFirstNavigation) {
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_ALREADY_NAVIGATED)
2016-03-25 19:57:17 +00:00
this.setValueIgnoreMutation(oldValue)
return
2016-01-15 22:28:12 +00:00
}
if (newValue === 'persist:') {
2016-03-25 19:57:17 +00:00
this.validPartitionId = false
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE)
2016-01-15 22:28:12 +00:00
}
}
}
2016-01-15 22:28:12 +00:00
// Attribute that handles the location and navigation of the webview.
class SrcAttribute extends WebViewAttribute {
public observer!: MutationObserver;
constructor (public webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC, webViewImpl)
2016-03-25 19:57:17 +00:00
this.setupMutationObserver()
2016-01-15 22:28:12 +00:00
}
public getValue () {
2016-01-15 22:28:12 +00:00
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) {
2016-03-25 19:57:17 +00:00
return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name))
2016-01-15 22:28:12 +00:00
} else {
2016-03-25 19:57:17 +00:00
return this.value
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
}
public setValueIgnoreMutation (value: any) {
2016-03-25 19:57:17 +00:00
super.setValueIgnoreMutation(value)
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// takeRecords() is needed to clear queued up src mutations. Without it, it
// is possible for this change to get picked up asyncronously by src's
// mutation observer |observer|, and then get handled even though we do not
// want to handle this mutation.
2016-11-03 17:19:52 +00:00
this.observer.takeRecords()
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
public handleMutation = (oldValue: any, newValue: any) => {
2016-01-15 22:28:12 +00: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.
2016-03-25 19:57:17 +00:00
this.setValueIgnoreMutation(oldValue)
return
2016-01-15 22:28:12 +00:00
}
2016-11-03 17:19:52 +00:00
this.parse()
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00: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.
public setupMutationObserver () {
2016-03-11 22:08:14 +00:00
this.observer = new MutationObserver((mutations) => {
2016-11-03 18:09:53 +00:00
for (const mutation of mutations) {
2018-09-13 16:10:51 +00:00
const { oldValue } = mutation
2016-11-03 18:09:53 +00:00
const newValue = this.getValue()
2016-03-11 22:08:14 +00:00
if (oldValue !== newValue) {
2016-03-25 19:57:17 +00:00
return
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
this.handleMutation(oldValue, newValue)
2016-03-11 22:08:14 +00:00
}
2016-03-25 19:57:17 +00:00
})
2016-11-03 17:39:40 +00:00
const params = {
2016-01-15 22:28:12 +00:00
attributes: true,
attributeOldValue: true,
attributeFilter: [this.name]
2016-03-25 19:57:17 +00:00
}
2016-11-03 17:19:52 +00:00
this.observer.observe(this.webViewImpl.webviewNode, params)
}
2016-01-15 22:28:12 +00:00
public parse () {
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) {
2016-03-25 19:57:17 +00:00
return
2016-01-15 22:28:12 +00:00
}
if (this.webViewImpl.guestInstanceId == null) {
if (this.webViewImpl.beforeFirstNavigation) {
2016-03-25 19:57:17 +00:00
this.webViewImpl.beforeFirstNavigation = false
this.webViewImpl.createGuest()
2016-01-15 22:28:12 +00:00
}
2016-03-25 19:57:17 +00:00
return
2016-01-12 02:40:23 +00:00
}
2016-01-15 22:28:12 +00:00
// Navigate to |this.src|.
const opts: Record<string, string> = {}
const httpreferrer = this.webViewImpl.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER].getValue()
2016-01-15 22:28:12 +00:00
if (httpreferrer) {
2016-03-25 19:57:17 +00:00
opts.httpReferrer = httpreferrer
2016-01-15 22:28:12 +00:00
}
const useragent = this.webViewImpl.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT].getValue()
2016-01-15 22:28:12 +00:00
if (useragent) {
2016-03-25 19:57:17 +00:00
opts.userAgent = useragent
2016-01-15 22:28:12 +00:00
}
const guestInstanceId = this.webViewImpl.guestInstanceId
const method = 'loadURL'
const args = [this.getValue(), opts]
ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', guestInstanceId, method, args)
2016-01-12 02:40:23 +00:00
}
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
// Attribute specifies HTTP referrer.
2016-01-15 22:28:12 +00:00
class HttpReferrerAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER, webViewImpl)
2016-01-15 22:28:12 +00:00
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Attribute specifies user agent
2016-01-15 22:28:12 +00:00
class UserAgentAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT, webViewImpl)
2016-01-15 22:28:12 +00:00
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Attribute that set preload script.
2016-01-15 23:09:25 +00:00
class PreloadAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD, webViewImpl)
}
2016-01-15 22:28:12 +00:00
public getValue () {
2016-01-15 22:28:12 +00:00
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) {
2016-03-25 19:57:17 +00:00
return this.value
2016-01-15 22:28:12 +00:00
}
2016-11-03 17:39:40 +00:00
let preload = resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name))
const protocol = preload.substr(0, 5)
2016-01-15 22:28:12 +00:00
if (protocol !== 'file:') {
console.error(WEB_VIEW_CONSTANTS.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE)
2016-03-25 19:57:17 +00:00
preload = ''
2016-01-15 22:28:12 +00:00
}
2016-03-25 19:57:17 +00:00
return preload
}
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-21 10:13:41 +00:00
// Attribute that specifies the blink features to be enabled.
class BlinkFeaturesAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES, webViewImpl)
2016-01-21 10:13:41 +00:00
}
}
// Attribute that specifies the blink features to be disabled.
class DisableBlinkFeaturesAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_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)
}
}
class EnableRemoteModuleAttribute extends WebViewAttribute {
constructor (webViewImpl: WebViewImpl) {
super(WEB_VIEW_CONSTANTS.ATTRIBUTE_ENABLEREMOTEMODULE, webViewImpl)
}
public getValue () {
return this.webViewImpl.webviewNode.getAttribute(this.name) !== 'false'
}
public setValue (value: any) {
this.webViewImpl.webviewNode.setAttribute(this.name, value ? 'true' : 'false')
}
}
2016-01-14 18:35:29 +00:00
// Sets up all of the webview attributes.
2016-03-25 19:57:17 +00:00
WebViewImpl.prototype.setupWebViewAttributes = function () {
this.attributes = {}
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION] = new PartitionAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC] = new SrcAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_HTTPREFERRER] = new HttpReferrerAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_USERAGENT] = new UserAgentAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION] = new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATION, this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES] = new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_NODEINTEGRATIONINSUBFRAMES, this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS] = new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_PLUGINS, this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY] = new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY, this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS] = new BooleanAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS, this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_ENABLEREMOTEMODULE] = new EnableRemoteModuleAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD] = new PreloadAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES] = new BlinkFeaturesAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES] = new DisableBlinkFeaturesAttribute(this)
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES] = new WebPreferencesAttribute(this)
2016-03-25 19:57:17 +00:00
}