Merge pull request #8742 from electron/webview-visibility

Route webview window visibility change events in browser process
This commit is contained in:
Kevin Sawicki 2017-02-28 08:45:15 -08:00 committed by GitHub
commit 51d6a55061
4 changed files with 23 additions and 20 deletions

View file

@ -110,7 +110,9 @@ BrowserWindow.prototype._init = function () {
const newState = this.isVisible() && !this.isMinimized()
if (isVisible !== newState) {
isVisible = newState
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', isVisible ? 'visible' : 'hidden')
const visibilityState = isVisible ? 'visible' : 'hidden'
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', visibilityState)
this.webContents.emit('-window-visibility-change', visibilityState)
}
}

View file

@ -252,6 +252,17 @@ const watchEmbedder = function (embedder) {
}
watchedEmbedders.add(embedder)
// Forward embedder window visiblity change events to guest
const onVisibilityChange = function (visibilityState) {
for (const guestInstanceId of Object.keys(guestInstances)) {
const guestInstance = guestInstances[guestInstanceId]
if (guestInstance.embedder === embedder) {
guestInstance.guest.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', visibilityState)
}
}
}
embedder.on('-window-visibility-change', onVisibilityChange)
const destroyEvents = ['will-destroy', 'crashed', 'did-navigate']
const destroy = function () {
for (const guestInstanceId of Object.keys(guestInstances)) {
@ -263,6 +274,7 @@ const watchEmbedder = function (embedder) {
for (const event of destroyEvents) {
embedder.removeListener(event, destroy)
}
embedder.removeListener('-window-visibility-change', onVisibilityChange)
watchedEmbedders.delete(embedder)
}

View file

@ -33,11 +33,6 @@ class WebViewImpl {
this.setupFocusPropagation()
this.viewInstanceId = getNextId()
shadowRoot.appendChild(this.browserPluginNode)
this.onVisibilityChanged = (event, visibilityState) => {
this.webviewNode.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', visibilityState)
}
ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.onVisibilityChanged)
}
createBrowserPluginNode () {
@ -50,8 +45,6 @@ class WebViewImpl {
// Resets some state upon reattaching <webview> element to the DOM.
reset () {
ipcRenderer.removeListener('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.onVisibilityChanged)
// If guestInstanceId is defined then the <webview> has navigated and has
// already picked up a partition ID. Thus, we need to reset the initialization
// state. However, it may be the case that beforeFirstNavigation is false BUT
@ -264,8 +257,7 @@ const registerBrowserPluginElement = function () {
this.style.flex = '1 1 auto'
}
proto.attributeChangedCallback = function (name, oldValue, newValue) {
var internal
internal = v8Util.getHiddenValue(this, 'internal')
const internal = v8Util.getHiddenValue(this, 'internal')
if (internal) {
internal.handleBrowserPluginAttributeMutation(name, oldValue, newValue)
}
@ -285,21 +277,19 @@ const registerBrowserPluginElement = function () {
}
// Registers <webview> custom element.
var registerWebViewElement = function () {
const registerWebViewElement = function () {
const proto = Object.create(HTMLObjectElement.prototype)
proto.createdCallback = function () {
return new WebViewImpl(this)
}
proto.attributeChangedCallback = function (name, oldValue, newValue) {
var internal
internal = v8Util.getHiddenValue(this, 'internal')
const internal = v8Util.getHiddenValue(this, 'internal')
if (internal) {
internal.handleWebviewAttributeMutation(name, oldValue, newValue)
}
}
proto.detachedCallback = function () {
var internal
internal = v8Util.getHiddenValue(this, 'internal')
const internal = v8Util.getHiddenValue(this, 'internal')
if (!internal) {
return
}
@ -309,15 +299,14 @@ var registerWebViewElement = function () {
internal.reset()
}
proto.attachedCallback = function () {
var internal, instance
internal = v8Util.getHiddenValue(this, 'internal')
const internal = v8Util.getHiddenValue(this, 'internal')
if (!internal) {
return
}
if (!internal.elementAttached) {
guestViewInternal.registerEvents(internal, internal.viewInstanceId)
internal.elementAttached = true
instance = internal.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE].getValue()
const instance = internal.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE].getValue()
if (instance) {
internal.attachGuestInstance(instance)
} else {

View file

@ -1086,13 +1086,13 @@ describe('<webview> tag', function () {
assert.equal(visibilityState, 'hidden')
assert.equal(hidden, true)
w.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', 'visible')
ipcMain.once('pong', function (event, visibilityState, hidden) {
assert.equal(visibilityState, 'visible')
assert.equal(hidden, false)
done()
})
w.webContents.emit('-window-visibility-change', 'visible')
})
w.loadURL('file://' + fixtures + '/pages/webview-visibilitychange.html')