From 1d84d83fd4d26bd378edcb3e8558e550acafdb99 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 24 Feb 2017 10:18:09 -0800 Subject: [PATCH] Forward webview visibility change events from browser process --- lib/browser/api/browser-window.js | 4 +++- lib/browser/guest-view-manager.js | 12 ++++++++++++ lib/renderer/web-view/web-view.js | 8 -------- spec/webview-spec.js | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 6528da1ba001..1aa34d45a0a9 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -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) } } diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index 721da5acc444..0bfe77d1e5e4 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -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) } diff --git a/lib/renderer/web-view/web-view.js b/lib/renderer/web-view/web-view.js index 3e8d33c663b8..ac45ab32a1c6 100644 --- a/lib/renderer/web-view/web-view.js +++ b/lib/renderer/web-view/web-view.js @@ -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 element to the DOM. reset () { - ipcRenderer.removeListener('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', this.onVisibilityChanged) - // If guestInstanceId is defined then the 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() diff --git a/spec/webview-spec.js b/spec/webview-spec.js index 93924e036c50..c038e3db88c1 100644 --- a/spec/webview-spec.js +++ b/spec/webview-spec.js @@ -1086,7 +1086,7 @@ describe(' 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')