Add a guestinstance attribute to webviews reflecting their current guest

instance ID and allowing moving a guest instance to a new webview.
This commit is contained in:
Dave Townsend 2016-09-08 10:01:01 -07:00
parent 01fa9827b4
commit 313b2faa3c
12 changed files with 424 additions and 63 deletions

View file

@ -7,6 +7,7 @@ var requestId = 0
var WEB_VIEW_EVENTS = {
'load-commit': ['url', 'isMainFrame'],
'did-attach': [],
'did-finish-load': [],
'did-fail-load': ['errorCode', 'errorDescription', 'validatedURL', 'isMainFrame'],
'did-frame-finish-load': ['isMainFrame'],
@ -62,6 +63,15 @@ var dispatchEvent = function (webView, eventName, eventKey, ...args) {
module.exports = {
registerEvents: function (webView, viewInstanceId) {
ipcRenderer.on('ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-' + viewInstanceId, function () {
var domEvent
webFrame.detachGuest(webView.internalInstanceId)
webView.guestInstanceId = undefined
webView.reset()
domEvent = new Event('destroyed')
webView.dispatchEvent(domEvent)
})
ipcRenderer.on('ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-' + viewInstanceId, function (event, eventName, ...args) {
dispatchEvent.apply(null, [webView, eventName, eventName].concat(args))
})
@ -85,6 +95,7 @@ module.exports = {
})
},
deregisterEvents: function (viewInstanceId) {
ipcRenderer.removeAllListeners('ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-' + viewInstanceId)
ipcRenderer.removeAllListeners('ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-' + viewInstanceId)
ipcRenderer.removeAllListeners('ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE-' + viewInstanceId)
return ipcRenderer.removeAllListeners('ELECTRON_GUEST_VIEW_INTERNAL_SIZE_CHANGED-' + viewInstanceId)

View file

@ -130,6 +130,46 @@ class PartitionAttribute extends WebViewAttribute {
}
}
// An attribute that controls the guest instance this webview is connected to
class GuestInstanceAttribute extends WebViewAttribute {
constructor (webViewImpl) {
super(webViewConstants.ATTRIBUTE_GUESTINSTANCE, webViewImpl)
}
// Retrieves and returns the attribute's value.
getValue () {
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name))
}
return undefined
}
// Sets the attribute's value.
setValue (value) {
if (!value) {
return this.webViewImpl.webviewNode.removeAttribute(this.name)
}
if (isNaN(value)) {
return
}
return this.webViewImpl.webviewNode.setAttribute(this.name, value)
}
handleMutation (oldValue, newValue) {
if (!newValue) {
this.webViewImpl.reset()
return
}
const intVal = parseInt(newValue)
if (!isNaN(newValue) && remote.getGuestWebContents(intVal)) {
this.webViewImpl.attachGuestInstance(intVal)
} else {
this.setValueIgnoreMutation(oldValue)
}
}
}
// Attribute that handles the location and navigation of the webview.
class SrcAttribute extends WebViewAttribute {
constructor (webViewImpl) {
@ -287,6 +327,7 @@ WebViewImpl.prototype.setupWebViewAttributes = function () {
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_GUESTINSTANCE] = new GuestInstanceAttribute(this)
const autosizeAttributes = [webViewConstants.ATTRIBUTE_MAXHEIGHT, webViewConstants.ATTRIBUTE_MAXWIDTH, webViewConstants.ATTRIBUTE_MINHEIGHT, webViewConstants.ATTRIBUTE_MINWIDTH]
autosizeAttributes.forEach((attribute) => {

View file

@ -17,6 +17,7 @@ module.exports = {
ATTRIBUTE_USERAGENT: 'useragent',
ATTRIBUTE_BLINKFEATURES: 'blinkfeatures',
ATTRIBUTE_DISABLEBLINKFEATURES: 'disableblinkfeatures',
ATTRIBUTE_GUESTINSTANCE: 'guestinstance',
// Internal attribute.
ATTRIBUTE_INTERNALINSTANCEID: 'internalinstanceid',

View file

@ -71,12 +71,13 @@ var WebViewImpl = (function () {
// that we don't end up allocating a second guest.
if (this.guestInstanceId) {
guestViewInternal.destroyGuest(this.guestInstanceId)
this.webContents = null
this.guestInstanceId = void 0
this.beforeFirstNavigation = true
this.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId = true
}
this.internalInstanceId = 0
this.webContents = null
this.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE].setValueIgnoreMutation(undefined)
this.beforeFirstNavigation = true
this.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId = true
}
// Sets the <webview>.request property.
@ -184,7 +185,7 @@ var WebViewImpl = (function () {
WebViewImpl.prototype.createGuest = function () {
return guestViewInternal.createGuest(this.buildParams(), (event, guestInstanceId) => {
this.attachWindow(guestInstanceId)
this.attachGuestInstance(guestInstanceId)
})
}
@ -257,8 +258,9 @@ var WebViewImpl = (function () {
return params
}
WebViewImpl.prototype.attachWindow = function (guestInstanceId) {
WebViewImpl.prototype.attachGuestInstance = function (guestInstanceId) {
this.guestInstanceId = guestInstanceId
this.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE].setValueIgnoreMutation(guestInstanceId)
this.webContents = remote.getGuestWebContents(this.guestInstanceId)
if (!this.internalInstanceId) {
return true
@ -324,10 +326,11 @@ var registerWebViewElement = function () {
}
guestViewInternal.deregisterEvents(internal.viewInstanceId)
internal.elementAttached = false
return internal.reset()
internal.reset()
this.internalInstanceId = 0
}
proto.attachedCallback = function () {
var internal
var internal, instance
internal = v8Util.getHiddenValue(this, 'internal')
if (!internal) {
return
@ -335,6 +338,10 @@ var registerWebViewElement = function () {
if (!internal.elementAttached) {
guestViewInternal.registerEvents(internal, internal.viewInstanceId)
internal.elementAttached = true
instance = internal.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE].getValue()
if (instance) {
return internal.attachGuestInstance(instance)
}
return internal.attributes[webViewConstants.ATTRIBUTE_SRC].parse()
}
}