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

295 lines
10 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
'use strict'
2016-01-15 22:28:12 +00:00
2016-03-25 19:57:17 +00:00
const WebViewImpl = require('./web-view')
const guestViewInternal = require('./guest-view-internal')
const webViewConstants = require('./web-view-constants')
const remote = require('electron').remote
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-03-25 19:57:17 +00:00
var a = document.createElement('a')
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var resolveURL = function (url) {
a.href = url
return a.href
}
2016-01-12 02:40:23 +00:00
2016-01-14 19:10:12 +00:00
// Attribute objects.
// Default implementation of a WebView attribute.
2016-01-15 22:28:12 +00:00
class WebViewAttribute {
2016-03-25 19:57:17 +00:00
constructor (name, webViewImpl) {
this.name = name
this.value = webViewImpl.webviewNode[name] || ''
this.webViewImpl = webViewImpl
this.ignoreMutation = false
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.
2016-03-25 19:57:17 +00:00
getValue () {
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.
2016-03-25 19:57:17 +00:00
setValue (value) {
return 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.
2016-03-25 19:57:17 +00:00
setValueIgnoreMutation (value) {
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.
2016-03-25 19:57:17 +00:00
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.
2016-03-25 19:57:17 +00:00
handleMutation () {}
}
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
}
2016-03-25 19:57:17 +00:00
setValue (value) {
2016-01-15 22:28:12 +00:00
if (!value) {
2016-03-25 19:57:17 +00:00
return this.webViewImpl.webviewNode.removeAttribute(this.name)
2016-01-15 22:28:12 +00:00
} else {
2016-03-25 19:57:17 +00:00
return this.webViewImpl.webviewNode.setAttribute(this.name, '')
2016-01-15 22:28:12 +00:00
}
}
}
2016-01-12 02:40:23 +00:00
2016-01-15 22:28:12 +00:00
// Attribute used to define the demension limits of autosizing.
class AutosizeDimensionAttribute extends WebViewAttribute {
2016-03-25 19:57:17 +00:00
getValue () {
return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0
}
2016-01-15 22:28:12 +00:00
2016-03-25 19:57:17 +00:00
handleMutation () {
2016-01-15 22:28:12 +00:00
if (!this.webViewImpl.guestInstanceId) {
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
return guestViewInternal.setSize(this.webViewImpl.guestInstanceId, {
enableAutoSize: this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_AUTOSIZE].getValue(),
min: {
width: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MINWIDTH].getValue() || 0),
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MINHEIGHT].getValue() || 0)
},
max: {
width: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXWIDTH].getValue() || 0),
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0)
}
2016-03-25 19:57:17 +00:00
})
2016-01-15 22:28:12 +00:00
}
}
2016-01-14 18:35:29 +00:00
// Attribute that specifies whether the webview should be autosized.
2016-01-15 22:28:12 +00:00
class AutosizeAttribute extends BooleanAttribute {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl)
2016-01-15 22:28:12 +00:00
}
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation
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 {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_PARTITION, webViewImpl)
this.validPartitionId = true
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
handleMutation (oldValue, newValue) {
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) {
2016-03-25 19:57:17 +00:00
window.console.error(webViewConstants.ERROR_MSG_ALREADY_NAVIGATED)
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
return window.console.error(webViewConstants.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 {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_SRC, webViewImpl)
this.setupMutationObserver()
2016-01-15 22:28:12 +00:00
}
2016-03-25 19:57:17 +00:00
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
}
2016-03-25 19:57:17 +00:00
setValueIgnoreMutation (value) {
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-03-25 19:57:17 +00:00
return this.observer.takeRecords()
2016-01-15 22:28:12 +00:00
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
handleMutation (oldValue, newValue) {
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-03-25 19:57:17 +00:00
return 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.
2016-03-25 19:57:17 +00:00
setupMutationObserver () {
var params
2016-03-11 22:08:14 +00:00
this.observer = new MutationObserver((mutations) => {
2016-03-25 19:57:17 +00:00
var i, len, mutation, newValue, oldValue
2016-03-11 22:08:14 +00:00
for (i = 0, len = mutations.length; i < len; i++) {
2016-03-25 19:57:17 +00:00
mutation = mutations[i]
oldValue = mutation.oldValue
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-01-15 22:28:12 +00:00
params = {
attributes: true,
attributeOldValue: true,
attributeFilter: [this.name]
2016-03-25 19:57:17 +00:00
}
return this.observer.observe(this.webViewImpl.webviewNode, params)
}
2016-01-15 22:28:12 +00:00
2016-03-25 19:57:17 +00:00
parse () {
var guestContents, httpreferrer, opts, useragent
2016-01-15 22:28:12 +00:00
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.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|.
2016-03-25 19:57:17 +00:00
opts = {}
httpreferrer = this.webViewImpl.attributes[webViewConstants.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
}
2016-03-25 19:57:17 +00:00
useragent = this.webViewImpl.attributes[webViewConstants.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
}
2016-03-25 19:57:17 +00:00
guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId)
return guestContents.loadURL(this.getValue(), opts)
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 {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.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 {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.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 {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl)
}
2016-01-15 22:28:12 +00:00
2016-03-25 19:57:17 +00:00
getValue () {
var preload, protocol
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-03-25 19:57:17 +00:00
preload = resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name))
protocol = preload.substr(0, 5)
2016-01-15 22:28:12 +00:00
if (protocol !== 'file:') {
2016-03-25 19:57:17 +00:00
console.error(webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE)
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 {
2016-03-25 19:57:17 +00:00
constructor (webViewImpl) {
super(webViewConstants.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) {
2016-06-07 23:26:53 +00:00
super(webViewConstants.ATTRIBUTE_DISABLEBLINKFEATURES, webViewImpl)
}
}
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[webViewConstants.ATTRIBUTE_AUTOSIZE] = new AutosizeAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_PARTITION] = new PartitionAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_SRC] = new SrcAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER] = new HttpReferrerAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_USERAGENT] = new UserAgentAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_NODEINTEGRATION] = new BooleanAttribute(webViewConstants.ATTRIBUTE_NODEINTEGRATION, this)
this.attributes[webViewConstants.ATTRIBUTE_PLUGINS] = new BooleanAttribute(webViewConstants.ATTRIBUTE_PLUGINS, this)
this.attributes[webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY] = new BooleanAttribute(webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY, this)
this.attributes[webViewConstants.ATTRIBUTE_ALLOWPOPUPS] = new BooleanAttribute(webViewConstants.ATTRIBUTE_ALLOWPOPUPS, this)
this.attributes[webViewConstants.ATTRIBUTE_PRELOAD] = new PreloadAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_BLINKFEATURES] = new BlinkFeaturesAttribute(this)
2016-06-07 23:26:53 +00:00
this.attributes[webViewConstants.ATTRIBUTE_DISABLEBLINKFEATURES] = new DisableBlinkFeaturesAttribute(this)
2016-03-25 19:57:17 +00:00
const autosizeAttributes = [webViewConstants.ATTRIBUTE_MAXHEIGHT, webViewConstants.ATTRIBUTE_MAXWIDTH, webViewConstants.ATTRIBUTE_MINHEIGHT, webViewConstants.ATTRIBUTE_MINWIDTH]
autosizeAttributes.forEach((attribute) => {
2016-03-25 19:57:17 +00:00
this.attributes[attribute] = new AutosizeDimensionAttribute(attribute, this)
})
}