diff --git a/emain/emain-ipc.ts b/emain/emain-ipc.ts index 1d1ec2108a..09830b9315 100644 --- a/emain/emain-ipc.ts +++ b/emain/emain-ipc.ts @@ -209,7 +209,7 @@ export function initIpcHandlers() { electron.ipcMain.on("webview-image-contextmenu", (event: electron.IpcMainEvent, payload: { src: string }) => { const menu = new electron.Menu(); - const win = getWaveWindowByWebContentsId(event.sender.hostWebContents.id); + const win = getWaveWindowByWebContentsId(event.sender.hostWebContents?.id); if (win == null) { return; } @@ -353,6 +353,7 @@ export function initIpcHandlers() { const png = PNG.sync.read(overlayBuffer); const color = fac.prepareResult(fac.getColorFromArray4(png.data)); const ww = getWaveWindowByWebContentsId(event.sender.id); + if (ww == null) return; ww.setTitleBarOverlay({ color: unamePlatform === "linux" ? color.rgba : "#00000000", symbolColor: color.isDark ? "white" : "black", diff --git a/emain/emain-tabview.ts b/emain/emain-tabview.ts index 7bf4cc23f3..753a53adec 100644 --- a/emain/emain-tabview.ts +++ b/emain/emain-tabview.ts @@ -109,6 +109,9 @@ function computeBgColor(fullConfig: FullConfigType): string { const wcIdToWaveTabMap = new Map(); export function getWaveTabViewByWebContentsId(webContentsId: number): WaveTabView { + if (webContentsId == null) { + return null; + } return wcIdToWaveTabMap.get(webContentsId); } @@ -154,14 +157,15 @@ export class WaveTabView extends WebContentsView { this.waveReadyPromise.then(() => { this.isWaveReady = true; }); - wcIdToWaveTabMap.set(this.webContents.id, this); + const wcId = this.webContents.id; + wcIdToWaveTabMap.set(wcId, this); if (isDevVite) { this.webContents.loadURL(`${process.env.ELECTRON_RENDERER_URL}/index.html`); } else { this.webContents.loadFile(path.join(getElectronAppBasePath(), "frontend", "index.html")); } this.webContents.on("destroyed", () => { - wcIdToWaveTabMap.delete(this.webContents.id); + wcIdToWaveTabMap.delete(wcId); removeWaveTabView(this.waveTabId); this.isDestroyed = true; }); @@ -283,7 +287,6 @@ function checkAndEvictCache(): void { // Otherwise, sort by lastUsedTs return a.lastUsedTs - b.lastUsedTs; }); - const now = Date.now(); for (let i = 0; i < sorted.length - MaxCacheSize; i++) { tryEvictEntry(sorted[i].waveTabId); } @@ -313,6 +316,9 @@ export async function getOrCreateWebViewForTab(waveWindowId: string, tabId: stri tabView.webContents.on("will-frame-navigate", shFrameNavHandler); tabView.webContents.on("did-attach-webview", (event, wc) => { wc.setWindowOpenHandler((details) => { + if (wc == null || wc.isDestroyed() || tabView.webContents == null || tabView.webContents.isDestroyed()) { + return { action: "deny" }; + } tabView.webContents.send("webview-new-window", wc.id, details); return { action: "deny" }; }); diff --git a/emain/emain-window.ts b/emain/emain-window.ts index 2c34d3a39c..5f481e30f1 100644 --- a/emain/emain-window.ts +++ b/emain/emain-window.ts @@ -674,6 +674,9 @@ export function getWaveWindowByTabId(tabId: string): WaveBrowserWindow { } export function getWaveWindowByWebContentsId(webContentsId: number): WaveBrowserWindow { + if (webContentsId == null) { + return null; + } const tabView = getWaveTabViewByWebContentsId(webContentsId); if (tabView == null) { return null;