Skip to content

Commit c273e65

Browse files
committed
Refactor window open handler
Make it consistent with will-navigate.
1 parent 7a8436c commit c273e65

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src-main/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ app.on('session-created', (session) => {
104104
});
105105

106106
app.on('web-contents-created', (event, webContents) => {
107-
// Event handler added here as a safety measure.
107+
// For safety reasons, we add these listeners here so that they apply to any web contents,
108+
// even ones that somehow got created without an associated one of our AbstractWindows
109+
// also being created.
110+
108111
webContents.on('will-navigate', (event, url) => {
109112
const window = AbstractWindow.getWindowByWebContents(webContents);
110113
if (window) {
@@ -115,10 +118,16 @@ app.on('web-contents-created', (event, webContents) => {
115118
}
116119
});
117120

118-
// Overwritten by AbstractWindow. We just set this here as a safety measure.
119-
webContents.setWindowOpenHandler((details) => ({
120-
action: 'deny'
121-
}));
121+
webContents.setWindowOpenHandler((details) => {
122+
const window = AbstractWindow.getWindowByWebContents(webContents);
123+
if (window) {
124+
return window.handleWindowOpen(details);
125+
}
126+
// Unknown web contents; give minimal possible permissions.
127+
return {
128+
action: 'deny'
129+
};
130+
});
122131
});
123132

124133
app.on('window-all-closed', () => {

src-main/windows/abstract.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class AbstractWindow {
1919

2020
/** @type {Electron.BrowserWindow} */
2121
this.window = options.existingWindow || new BrowserWindow(this.getWindowOptions());
22-
this.window.webContents.setWindowOpenHandler(this.handleWindowOpen.bind(this));
2322
this.window.webContents.on('before-input-event', this.handleInput.bind(this));
2423
this.applySettings();
2524

0 commit comments

Comments
 (0)