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 5e5a10cb004f..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) - - 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 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 @@ -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 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 { diff --git a/spec/webview-spec.js b/spec/webview-spec.js index 93924e036c50..a1585922e12d 100644 --- a/spec/webview-spec.js +++ b/spec/webview-spec.js @@ -1086,13 +1086,13 @@ describe(' 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')