Skip to content

Commit

Permalink
fix: getting focused window with destroyed webContents (#33540)
Browse files Browse the repository at this point in the history
* fix: getting focused window with destroyed webContents

* fix: add extra safeguards

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Apr 19, 2022
1 parent 7d97988 commit a6c71cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/browser/api/browser-window.ts
Expand Up @@ -72,7 +72,10 @@ BrowserWindow.getAllWindows = () => {

BrowserWindow.getFocusedWindow = () => {
for (const window of BrowserWindow.getAllWindows()) {
if (window.isFocused() || window.isDevToolsFocused()) return window;
const hasWC = window.webContents && !window.webContents.isDestroyed();
if (!window.isDestroyed() && hasWC) {
if (window.isFocused() || window.isDevToolsFocused()) return window;
}
}
return null;
};
Expand Down
19 changes: 19 additions & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -3717,6 +3717,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 });
Expand Down

0 comments on commit a6c71cc

Please sign in to comment.