/
web-view-attributes.js
281 lines (246 loc) · 9.32 KB
/
web-view-attributes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict'
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
const { WebViewImpl } = require('@electron/internal/renderer/web-view/web-view')
const webViewConstants = require('@electron/internal/renderer/web-view/web-view-constants')
const errorUtils = require('@electron/internal/common/error-utils')
// Helper function to resolve url set in attribute.
const a = document.createElement('a')
const resolveURL = function (url) {
if (url === '') return ''
a.href = url
return a.href
}
// Attribute objects.
// Default implementation of a WebView attribute.
class WebViewAttribute {
constructor (name, webViewImpl) {
this.name = name
this.value = webViewImpl.webviewNode[name] || ''
this.webViewImpl = webViewImpl
this.ignoreMutation = false
this.defineProperty()
}
// Retrieves and returns the attribute's value.
getValue () {
return this.webViewImpl.webviewNode.getAttribute(this.name) || this.value
}
// Sets the attribute's value.
setValue (value) {
this.webViewImpl.webviewNode.setAttribute(this.name, value || '')
}
// Changes the attribute's value without triggering its mutation handler.
setValueIgnoreMutation (value) {
this.ignoreMutation = true
this.setValue(value)
this.ignoreMutation = false
}
// Defines this attribute as a property on the webview node.
defineProperty () {
return Object.defineProperty(this.webViewImpl.webviewNode, this.name, {
get: () => {
return this.getValue()
},
set: (value) => {
return this.setValue(value)
},
enumerable: true
})
}
// Called when the attribute's value changes.
handleMutation () {}
}
// An attribute that is treated as a Boolean.
class BooleanAttribute extends WebViewAttribute {
getValue () {
return this.webViewImpl.webviewNode.hasAttribute(this.name)
}
setValue (value) {
if (value) {
this.webViewImpl.webviewNode.setAttribute(this.name, '')
} else {
this.webViewImpl.webviewNode.removeAttribute(this.name)
}
}
}
// Attribute representing the state of the storage partition.
class PartitionAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_PARTITION, webViewImpl)
this.validPartitionId = true
}
handleMutation (oldValue, newValue) {
newValue = newValue || ''
// The partition cannot change if the webview has already navigated.
if (!this.webViewImpl.beforeFirstNavigation) {
console.error(webViewConstants.ERROR_MSG_ALREADY_NAVIGATED)
this.setValueIgnoreMutation(oldValue)
return
}
if (newValue === 'persist:') {
this.validPartitionId = false
console.error(webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE)
}
}
}
// Attribute that handles the location and navigation of the webview.
class SrcAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_SRC, webViewImpl)
this.setupMutationObserver()
}
getValue () {
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name))
} else {
return this.value
}
}
setValueIgnoreMutation (value) {
super.setValueIgnoreMutation(value)
// 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.
this.observer.takeRecords()
}
handleMutation (oldValue, newValue) {
// 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.
this.setValueIgnoreMutation(oldValue)
return
}
this.parse()
}
// 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.
setupMutationObserver () {
this.observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
const { oldValue } = mutation
const newValue = this.getValue()
if (oldValue !== newValue) {
return
}
this.handleMutation(oldValue, newValue)
}
})
const params = {
attributes: true,
attributeOldValue: true,
attributeFilter: [this.name]
}
this.observer.observe(this.webViewImpl.webviewNode, params)
}
parse () {
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) {
return
}
if (this.webViewImpl.guestInstanceId == null) {
if (this.webViewImpl.beforeFirstNavigation) {
this.webViewImpl.beforeFirstNavigation = false
this.webViewImpl.createGuest()
}
return
}
// Navigate to |this.src|.
const opts = {}
const httpreferrer = this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER].getValue()
if (httpreferrer) {
opts.httpReferrer = httpreferrer
}
const useragent = this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_USERAGENT].getValue()
if (useragent) {
opts.userAgent = useragent
}
const guestInstanceId = this.webViewImpl.guestInstanceId
const method = 'loadURL'
const args = [this.getValue(), opts]
const [error] = ipcRenderer.sendSync('ELECTRON_GUEST_VIEW_MANAGER_SYNC_CALL', guestInstanceId, method, args)
if (error) {
throw errorUtils.deserialize(error)
}
}
}
// Attribute specifies HTTP referrer.
class HttpReferrerAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl)
}
}
// Attribute specifies user agent
class UserAgentAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl)
}
}
// Attribute that set preload script.
class PreloadAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl)
}
getValue () {
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return this.value
}
let preload = resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name))
const protocol = preload.substr(0, 5)
if (protocol !== 'file:') {
console.error(webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE)
preload = ''
}
return preload
}
}
// Attribute that specifies the blink features to be enabled.
class BlinkFeaturesAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_BLINKFEATURES, webViewImpl)
}
}
// Attribute that specifies the blink features to be disabled.
class DisableBlinkFeaturesAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_DISABLEBLINKFEATURES, webViewImpl)
}
}
// Attribute that specifies the web preferences to be enabled.
class WebPreferencesAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_WEBPREFERENCES, webViewImpl)
}
}
class EnableRemoteModuleAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_ENABLEREMOTEMODULE, webViewImpl)
}
getValue () {
return this.webViewImpl.webviewNode.getAttribute(this.name) !== 'false'
}
setValue (value) {
this.webViewImpl.webviewNode.setAttribute(this.name, value ? 'true' : 'false')
}
}
// Sets up all of the webview attributes.
WebViewImpl.prototype.setupWebViewAttributes = function () {
this.attributes = {}
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_ENABLEREMOTEMODULE] = new EnableRemoteModuleAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_PRELOAD] = new PreloadAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_BLINKFEATURES] = new BlinkFeaturesAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_DISABLEBLINKFEATURES] = new DisableBlinkFeaturesAttribute(this)
this.attributes[webViewConstants.ATTRIBUTE_WEBPREFERENCES] = new WebPreferencesAttribute(this)
}