Forward webview visibility change events from browser process

This commit is contained in:
Kevin Sawicki 2017-02-24 10:18:09 -08:00
parent 7564a9973b
commit 1d84d83fd4
4 changed files with 16 additions and 10 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)
// Forward window visibility changes to guest contents
this.onVisibilityChanged = (event, visibilityState) => {
this.webviewNode.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', visibilityState)
}
}
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
@ -311,7 +304,6 @@ const registerWebViewElement = function () {
return
}
if (!internal.elementAttached) {
ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', internal.onVisibilityChanged)
guestViewInternal.registerEvents(internal, internal.viewInstanceId)
internal.elementAttached = true
const instance = internal.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE].getValue()

View file

@ -1086,7 +1086,7 @@ describe('<webview> tag', function () {
assert.equal(visibilityState, 'hidden')
assert.equal(hidden, true)
w.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', 'visible')
w.webContents.emit('-window-visibility-change', 'visible')
ipcMain.once('pong', function (event, visibilityState, hidden) {
assert.equal(visibilityState, 'visible')