From 948f018eeb36c3dd8fdb9e68f903a947925097bb Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 23 Mar 2022 11:43:20 +0100 Subject: [PATCH 1/2] fix: getting focused window with destroyed webContents --- lib/browser/api/browser-window.ts | 4 +++- spec-main/api-browser-window-spec.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index 62630bfffb2f5..5e99f3c76f1f8 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -72,7 +72,9 @@ BrowserWindow.getAllWindows = () => { BrowserWindow.getFocusedWindow = () => { for (const window of BrowserWindow.getAllWindows()) { - if (window.isFocused() || window.isDevToolsFocused()) return window; + if (!window.webContents.isDestroyed()) { + if (window.isFocused() || window.isDevToolsFocused()) return window; + } } return null; }; diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 67ec78ab5dd4d..1070a1b1f6db3 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -3724,6 +3724,25 @@ describe('BrowserWindow module', () => { expect(w.getChildWindows().length).to.equal(0); }); + it('closes a grandchild window when a middle child window is destroyed', (done) => { + const w = new BrowserWindow(); + + w.loadFile(path.join(fixtures, 'pages', 'base-page.html')); + w.webContents.executeJavaScript('window.open("")'); + + w.webContents.on('did-create-window', async (window) => { + const childWindow = new BrowserWindow({ parent: window }); + + await delay(); + window.close(); + + childWindow.on('closed', () => { + expect(() => { BrowserWindow.getFocusedWindow(); }).to.not.throw(); + done(); + }); + }); + }); + it('should not affect the show option', () => { const w = new BrowserWindow({ show: false }); const c = new BrowserWindow({ show: false, parent: w }); From f5786193dc79c8e5594ca1e0d0fc31f36a57d250 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 29 Mar 2022 15:59:31 +0200 Subject: [PATCH 2/2] fix: add extra safeguards --- lib/browser/api/browser-window.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index 5e99f3c76f1f8..d20ea0bda5571 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -72,7 +72,8 @@ BrowserWindow.getAllWindows = () => { BrowserWindow.getFocusedWindow = () => { for (const window of BrowserWindow.getAllWindows()) { - if (!window.webContents.isDestroyed()) { + const hasWC = window.webContents && !window.webContents.isDestroyed(); + if (!window.isDestroyed() && hasWC) { if (window.isFocused() || window.isDevToolsFocused()) return window; } }