diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 2a01977302594..9c15a0e0a4a95 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1680,6 +1680,30 @@ describe("PreviewManager", () => { ), ); + effectIt.effect( + "does not open picture-in-picture against a destroyed webview's WebContents", + () => + withManager((manager) => + Effect.gen(function* () { + const guest = makeAttachingGuest(49); + fromId.mockImplementation((id) => + id === 49 && !guest.wc.isDestroyed() ? guest.wc : null, + ); + yield* manager.prepareWebview(guest.wc); + yield* manager.createTab("tab_destroyed_guest"); + yield* manager.registerWebview("tab_destroyed_guest", 49); + + guest.destroy(); + yield* Effect.yieldNow; + + const pictureInPicture = yield* Effect.flip( + manager.openPictureInPicture("tab_destroyed_guest"), + ).pipe(Effect.orDie); + expect(pictureInPicture._tag).toBe("PreviewWebviewNotInitializedError"); + }), + ), + ); + effectIt.effect("skips a guest destroyed while another guest's session opens", () => withManager((manager) => Effect.gen(function* () { diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index fbf70e12fd375..531e269a07f11 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -2040,6 +2040,51 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function ); }); + // Clears a tab's dead guest so callers see "no webview" until the renderer registers a new one. + const detachDestroyedWebContents = Effect.fn("PreviewManager.detachDestroyedWebContents")( + function* (tabId: string, webContentsId: number) { + const expectedAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); + yield* withTabLifecycleLock( + tabId, + Effect.gen(function* () { + const currentTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); + const currentAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); + const currentWebContents = webContents.fromId(webContentsId); + if ( + currentTab?.webContentsId !== webContentsId || + currentAttachment !== expectedAttachment || + (currentWebContents && !currentWebContents.isDestroyed()) + ) { + return; + } + yield* Effect.all( + [ + detachControlSession(webContentsId), + detachListeners(webContentsId), + cancelPickElement(tabId), + ], + { concurrency: 3, discard: true }, + ); + const detached = yield* SynchronizedRef.modify(tabsRef, (tabs) => { + const current = tabs.get(tabId); + if (current?.webContentsId !== webContentsId) { + return [Option.none(), tabs] as const; + } + const { favicon: _favicon, ...currentWithoutFavicon } = current; + const next: PreviewTabState = { ...currentWithoutFavicon, webContentsId: null }; + return [ + Option.some(next), + replaceMap(tabs, (copy) => { + copy.set(tabId, next); + }), + ] as const; + }); + if (Option.isSome(detached)) yield* emitIfCurrent(tabId, detached.value); + }), + ); + }, + ); + // URL tabs can paint before registerWebview; set the background at attach time. const prepareWebview = Effect.fn("PreviewManager.prepareWebview")(function* ( wc: Electron.WebContents, @@ -2059,7 +2104,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }); wc.once("destroyed", () => { guestLoadFailures.delete(webContentsId); - runFork(hideDestroyedGuest(webContentsId)); + runFork( + Effect.gen(function* () { + yield* hideDestroyedGuest(webContentsId); + const tabId = yield* tabIdForWebContents(webContentsId); + if (tabId) yield* detachDestroyedWebContents(tabId, webContentsId); + }), + ); runFork(detachControlSession(webContentsId)); }); yield* ensureControlSession(wc).pipe( @@ -2135,45 +2186,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function const webContentsId = pending.webContentsId; const wc = webContents.fromId(webContentsId); if (!wc || wc.isDestroyed()) { - const expectedAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); - yield* withTabLifecycleLock( - tabId, - Effect.gen(function* () { - const currentTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); - const currentAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); - const currentWebContents = webContents.fromId(webContentsId); - if ( - currentTab?.webContentsId !== webContentsId || - currentAttachment !== expectedAttachment || - (currentWebContents && !currentWebContents.isDestroyed()) - ) { - return; - } - yield* Effect.all( - [ - detachControlSession(webContentsId), - detachListeners(webContentsId), - cancelPickElement(tabId), - ], - { concurrency: 3, discard: true }, - ); - const detached = yield* SynchronizedRef.modify(tabsRef, (tabs) => { - const current = tabs.get(tabId); - if (current?.webContentsId !== webContentsId) { - return [Option.none(), tabs] as const; - } - const { favicon: _favicon, ...currentWithoutFavicon } = current; - const next: PreviewTabState = { ...currentWithoutFavicon, webContentsId: null }; - return [ - Option.some(next), - replaceMap(tabs, (copy) => { - copy.set(tabId, next); - }), - ] as const; - }); - if (Option.isSome(detached)) yield* emitIfCurrent(tabId, detached.value); - }), - ); + yield* detachDestroyedWebContents(tabId, webContentsId); return; } yield* leaveBusyPage(wc); @@ -2248,6 +2261,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function Effect.catchTags({ PreviewTabNotFoundError: () => Effect.void, PreviewWebContentsNotFoundError: () => Effect.void, + PreviewWebviewNotInitializedError: () => Effect.void, }), ); diff --git a/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx b/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx index 29c217e064adc..d561254290427 100644 --- a/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx +++ b/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx @@ -43,6 +43,7 @@ vi.mock(import("~/components/preview/previewBridge"), async (original) => ({ closeTab: mocks.closeTab, registerWebview: mocks.registerWebview, getPreviewConfig: mocks.getPreviewConfig, + onStateChange: () => () => {}, } as never, })); diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsx index 023fb03c3aa17..b20d737095116 100644 --- a/apps/web/src/browser/HostedBrowserWebview.tsx +++ b/apps/web/src/browser/HostedBrowserWebview.tsx @@ -159,6 +159,7 @@ export function HostedBrowserWebview(props: { if (!clientSettingsHydrated || !webview || !config || !bridge) return; let disposed = false; let recoveryTimeout: ReturnType | null = null; + let registeredWebContentsId: number | null = null; const register = () => { const lease = tabLeaseRef.current; if (!lease) return; @@ -170,6 +171,7 @@ export function HostedBrowserWebview(props: { const webContentsId = webview.getWebContentsId(); if (Number.isInteger(webContentsId) && webContentsId > 0) { await bridge.registerWebview(runtimeTabId, webContentsId); + registeredWebContentsId = webContentsId; withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, reportedShown())); } } catch {} @@ -198,6 +200,13 @@ export function HostedBrowserWebview(props: { dismissHostPopups(); } }; + // The desktop clears a destroyed guest's id; nothing reattaches this element, so remount it. + const unsubscribeState = bridge.onStateChange((changedTabId, state) => { + if (changedTabId !== runtimeTabId || registeredWebContentsId === null) return; + if (state.webContentsId !== null) return; + registeredWebContentsId = null; + recoverGuest(); + }); webview.addEventListener("did-attach", register); webview.addEventListener("dom-ready", register); webview.addEventListener("render-process-gone", recoverGuest); @@ -207,6 +216,7 @@ export function HostedBrowserWebview(props: { return () => { disposed = true; if (recoveryTimeout !== null) clearTimeout(recoveryTimeout); + unsubscribeState(); webview.removeEventListener("did-attach", register); webview.removeEventListener("dom-ready", register); webview.removeEventListener("render-process-gone", recoverGuest); @@ -374,6 +384,8 @@ export function HostedBrowserWebview(props: { /> ) : null}