2019-01-12 01:00:43 +00:00
|
|
|
// When using context isolation, the WebViewElement and the custom element
|
|
|
|
// methods have to be defined in the main world to be able to be registered.
|
|
|
|
//
|
|
|
|
// Note: The hidden values can only be read/set inside the same context, all
|
|
|
|
// methods that access the "internal" hidden value must be put in this file.
|
|
|
|
//
|
|
|
|
// Note: This file could be loaded in the main world of contextIsolation page,
|
|
|
|
// which runs in browserify environment instead of Node environment, all native
|
|
|
|
// modules must be passed from outside, all included files must be plain JS.
|
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
|
|
|
|
import { WebViewImpl, webViewImplModule } from '@electron/internal/renderer/web-view/web-view-impl'
|
2019-01-12 01:00:43 +00:00
|
|
|
|
|
|
|
// Return a WebViewElement class that is defined in this context.
|
2019-03-07 23:26:23 +00:00
|
|
|
const defineWebViewElement = (v8Util: NodeJS.V8UtilBinding, webViewImpl: typeof webViewImplModule) => {
|
2019-01-12 01:00:43 +00:00
|
|
|
const { guestViewInternal, WebViewImpl } = webViewImpl
|
|
|
|
return class WebViewElement extends HTMLElement {
|
2019-03-07 23:26:23 +00:00
|
|
|
public internalInstanceId?: number;
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
static get observedAttributes () {
|
|
|
|
return [
|
2019-03-07 23:26:23 +00:00
|
|
|
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_PLUGINS,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEWEBSECURITY,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_ALLOWPOPUPS,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_ENABLEREMOTEMODULE,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_PRELOAD,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_BLINKFEATURES,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_DISABLEBLINKFEATURES,
|
|
|
|
WEB_VIEW_CONSTANTS.ATTRIBUTE_WEBPREFERENCES
|
2019-01-12 01:00:43 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
super()
|
|
|
|
v8Util.setHiddenValue(this, 'internal', new WebViewImpl(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback () {
|
2019-03-07 23:26:23 +00:00
|
|
|
const internal = v8Util.getHiddenValue<WebViewImpl>(this, 'internal')
|
2019-01-12 01:00:43 +00:00
|
|
|
if (!internal) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!internal.elementAttached) {
|
|
|
|
guestViewInternal.registerEvents(internal, internal.viewInstanceId)
|
|
|
|
internal.elementAttached = true
|
2019-03-07 23:26:23 +00:00
|
|
|
internal.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC].parse()
|
2019-01-12 01:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
attributeChangedCallback (name: string, oldValue: any, newValue: any) {
|
|
|
|
const internal = v8Util.getHiddenValue<WebViewImpl>(this, 'internal')
|
2019-01-12 01:00:43 +00:00
|
|
|
if (internal) {
|
|
|
|
internal.handleWebviewAttributeMutation(name, oldValue, newValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback () {
|
2019-03-07 23:26:23 +00:00
|
|
|
const internal = v8Util.getHiddenValue<WebViewImpl>(this, 'internal')
|
2019-01-12 01:00:43 +00:00
|
|
|
if (!internal) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guestViewInternal.deregisterEvents(internal.viewInstanceId)
|
|
|
|
internal.elementAttached = false
|
|
|
|
this.internalInstanceId = 0
|
|
|
|
internal.reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register <webview> custom element.
|
2019-03-07 23:26:23 +00:00
|
|
|
const registerWebViewElement = (v8Util: NodeJS.V8UtilBinding, webViewImpl: typeof webViewImplModule) => {
|
|
|
|
// I wish eslint wasn't so stupid, but it is
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const WebViewElement = defineWebViewElement(v8Util, webViewImpl) as unknown as typeof ElectronInternal.WebViewElement
|
2019-01-12 01:00:43 +00:00
|
|
|
|
|
|
|
webViewImpl.setupMethods(WebViewElement)
|
|
|
|
|
|
|
|
// The customElements.define has to be called in a special scope.
|
2019-03-07 23:26:23 +00:00
|
|
|
const webFrame = webViewImpl.webFrame as ElectronInternal.WebFrameInternal
|
|
|
|
webFrame.allowGuestViewElementDefinition(window, () => {
|
|
|
|
window.customElements.define('webview', WebViewElement);
|
|
|
|
(window as any).WebView = WebViewElement
|
2019-01-12 01:00:43 +00:00
|
|
|
|
|
|
|
// Delete the callbacks so developers cannot call them and produce unexpected
|
|
|
|
// behavior.
|
|
|
|
delete WebViewElement.prototype.connectedCallback
|
|
|
|
delete WebViewElement.prototype.disconnectedCallback
|
|
|
|
delete WebViewElement.prototype.attributeChangedCallback
|
|
|
|
|
|
|
|
// Now that |observedAttributes| has been retrieved, we can hide it from
|
|
|
|
// user code as well.
|
2019-03-07 23:26:23 +00:00
|
|
|
// TypeScript is concerned that we're deleting a read-only attribute
|
|
|
|
delete (WebViewElement as any).observedAttributes
|
2019-01-12 01:00:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare to register the <webview> element.
|
2019-03-07 23:26:23 +00:00
|
|
|
export const setupWebView = (v8Util: NodeJS.V8UtilBinding, webViewImpl: typeof webViewImplModule) => {
|
2019-01-12 01:00:43 +00:00
|
|
|
const useCapture = true
|
2019-03-07 23:26:23 +00:00
|
|
|
const listener = (event: Event) => {
|
2019-01-12 01:00:43 +00:00
|
|
|
if (document.readyState === 'loading') {
|
|
|
|
return
|
|
|
|
}
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
webViewImpl.setupAttributes()
|
|
|
|
registerWebViewElement(v8Util, webViewImpl)
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
window.removeEventListener(event.type, listener, useCapture)
|
2019-03-07 23:26:23 +00:00
|
|
|
}
|
2019-01-12 01:00:43 +00:00
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
window.addEventListener('readystatechange', listener, useCapture)
|
|
|
|
}
|