Skip to content

Commit 4034526

Browse files
authored
fix electron v41 regression (webContents is null in destroyed hander) (#3057)
also adds a bit more defensiveness around using webContents.id.
1 parent 1b708ea commit 4034526

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

emain/emain-ipc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export function initIpcHandlers() {
209209

210210
electron.ipcMain.on("webview-image-contextmenu", (event: electron.IpcMainEvent, payload: { src: string }) => {
211211
const menu = new electron.Menu();
212-
const win = getWaveWindowByWebContentsId(event.sender.hostWebContents.id);
212+
const win = getWaveWindowByWebContentsId(event.sender.hostWebContents?.id);
213213
if (win == null) {
214214
return;
215215
}
@@ -353,6 +353,7 @@ export function initIpcHandlers() {
353353
const png = PNG.sync.read(overlayBuffer);
354354
const color = fac.prepareResult(fac.getColorFromArray4(png.data));
355355
const ww = getWaveWindowByWebContentsId(event.sender.id);
356+
if (ww == null) return;
356357
ww.setTitleBarOverlay({
357358
color: unamePlatform === "linux" ? color.rgba : "#00000000",
358359
symbolColor: color.isDark ? "white" : "black",

emain/emain-tabview.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ function computeBgColor(fullConfig: FullConfigType): string {
109109
const wcIdToWaveTabMap = new Map<number, WaveTabView>();
110110

111111
export function getWaveTabViewByWebContentsId(webContentsId: number): WaveTabView {
112+
if (webContentsId == null) {
113+
return null;
114+
}
112115
return wcIdToWaveTabMap.get(webContentsId);
113116
}
114117

@@ -154,14 +157,15 @@ export class WaveTabView extends WebContentsView {
154157
this.waveReadyPromise.then(() => {
155158
this.isWaveReady = true;
156159
});
157-
wcIdToWaveTabMap.set(this.webContents.id, this);
160+
const wcId = this.webContents.id;
161+
wcIdToWaveTabMap.set(wcId, this);
158162
if (isDevVite) {
159163
this.webContents.loadURL(`${process.env.ELECTRON_RENDERER_URL}/index.html`);
160164
} else {
161165
this.webContents.loadFile(path.join(getElectronAppBasePath(), "frontend", "index.html"));
162166
}
163167
this.webContents.on("destroyed", () => {
164-
wcIdToWaveTabMap.delete(this.webContents.id);
168+
wcIdToWaveTabMap.delete(wcId);
165169
removeWaveTabView(this.waveTabId);
166170
this.isDestroyed = true;
167171
});
@@ -283,7 +287,6 @@ function checkAndEvictCache(): void {
283287
// Otherwise, sort by lastUsedTs
284288
return a.lastUsedTs - b.lastUsedTs;
285289
});
286-
const now = Date.now();
287290
for (let i = 0; i < sorted.length - MaxCacheSize; i++) {
288291
tryEvictEntry(sorted[i].waveTabId);
289292
}
@@ -313,6 +316,9 @@ export async function getOrCreateWebViewForTab(waveWindowId: string, tabId: stri
313316
tabView.webContents.on("will-frame-navigate", shFrameNavHandler);
314317
tabView.webContents.on("did-attach-webview", (event, wc) => {
315318
wc.setWindowOpenHandler((details) => {
319+
if (wc == null || wc.isDestroyed() || tabView.webContents == null || tabView.webContents.isDestroyed()) {
320+
return { action: "deny" };
321+
}
316322
tabView.webContents.send("webview-new-window", wc.id, details);
317323
return { action: "deny" };
318324
});

emain/emain-window.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@ export function getWaveWindowByTabId(tabId: string): WaveBrowserWindow {
674674
}
675675

676676
export function getWaveWindowByWebContentsId(webContentsId: number): WaveBrowserWindow {
677+
if (webContentsId == null) {
678+
return null;
679+
}
677680
const tabView = getWaveTabViewByWebContentsId(webContentsId);
678681
if (tabView == null) {
679682
return null;

0 commit comments

Comments
 (0)