fix(preview): restore browser tab recording via display media handler #8957
ClosedGigioxx(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/8957/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/desktop/src/preview/Manager.ts line 3171Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3099,3 +3107,15 @@30993107 return yield* Effect.failCause(initializationExit.cause);31003108 });310131093110+ /** Only drops the armed target when it still belongs to `tabId`, so tabs cannot clobber each other. */3111+ const clearPendingRecording = (tabId: string) => {3112+ if (pendingRecording?.tabId === tabId) pendingRecording = null;3113+ };3114+3115+ // Installed once per session: answers the renderer's `getDisplayMedia()` with the tab that3116+ // `startRecording` armed, and denies anything else so pages cannot capture on their own.3117+ const installDisplayMediaRequestHandler = (session: Session) => {3118+ if (displayMediaHandlerSessions.has(session)) return;3119+ displayMediaHandlerSessions.add(session);3120+ session.setDisplayMediaRequestHandler((_request, callback) => {3121+ const target = pendingRecording?.webContents;
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
- apps/desktop/src/preview/Manager.ts lines 3184 to 3185OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -3152,17 +3181,5 @@31523181 webContentsId: wc.id,31533182 });31543183 }3155- const sourceId = yield* attempt(3156- {3157- operation: "recording.getMediaSourceId",3158- tabId,3159- webContentsId: wc.id,3160- },3161- () => wc.getMediaSourceId(requestWebContents),3162- );3163- return {3164- sourceId,3165- width: measuredSize.width,3166- height: measuredSize.height,3167- } satisfies DesktopPreviewRecordingSource;3168- }).pipe(Effect.onError(() => stopFrameCapture(tabId, "recording").pipe(Effect.ignore))),3184+ installDisplayMediaRequestHandler(requestWebContents.session);3185+ pendingRecording = { tabId, webContents: wc };apps/web/src/components/preview/previewAutomationErrors.ts line 229OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -218,3 +218,12 @@218218219219 export const isPreviewAutomationHostError = Schema.is(PreviewAutomationHostError);220220221+/** Compact, stack-free rendering of a failure cause so the server log keeps the real reason. */222+const summarizeCause = (cause: unknown): string | null => {223+ if (cause === undefined || cause === null) return null;224+ if (typeof cause === "object" && "message" in cause && typeof cause.message === "string") {225+ const name =226+ "name" in cause && typeof cause.name === "string" && cause.name.length > 0227+ ? cause.name228+ : null;229+ return name ? `${name}: ${cause.message}` : cause.message;apps/web/src/components/preview/previewAutomationErrors.ts line 231OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -218,3 +218,14 @@218218219219 export const isPreviewAutomationHostError = Schema.is(PreviewAutomationHostError);220220221+/** Compact, stack-free rendering of a failure cause so the server log keeps the real reason. */222+const summarizeCause = (cause: unknown): string | null => {223+ if (cause === undefined || cause === null) return null;224+ if (typeof cause === "object" && "message" in cause && typeof cause.message === "string") {225+ const name =226+ "name" in cause && typeof cause.name === "string" && cause.name.length > 0227+ ? cause.name228+ : null;229+ return name ? `${name}: ${cause.message}` : cause.message;230+ }231+ const rendered = String(cause);apps/web/src/components/preview/previewAutomationErrors.ts line 224Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -219,6 +219,20 @@219219 export const isPreviewAutomationHostError = Schema.is(PreviewAutomationHostError);220220221+/** Compact, stack-free rendering of a failure cause so the server log keeps the real reason. */222+const summarizeCause = (cause: unknown): string | null => {223+ if (cause === undefined || cause === null) return null;224+ if (typeof cause === "object" && "message" in cause && typeof cause.message === "string") {225+ const name =226+ "name" in cause && typeof cause.name === "string" && cause.name.length > 0227+ ? cause.name228+ : null;229+ return name ? `${name}: ${cause.message}` : cause.message;230+ }231+ const rendered = String(cause);232+ return rendered.length === 0 ? null : rendered;233+};234+221235 export function serializePreviewAutomationHostError(222236 error: PreviewAutomationHostError,223237 ): NonNullable<PreviewAutomationResponse["error"]> {224- const detail = Object.fromEntries(238+ const detail: Record<string, unknown> = Object.fromEntries(
- apps/desktop/src/preview/Manager.ts line 3185OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3152,17 +3181,5 @@31523181 webContentsId: wc.id,31533182 });31543183 }3155- const sourceId = yield* attempt(3156- {3157- operation: "recording.getMediaSourceId",3158- tabId,3159- webContentsId: wc.id,3160- },3161- () => wc.getMediaSourceId(requestWebContents),3162- );3163- return {3164- sourceId,3165- width: measuredSize.width,3166- height: measuredSize.height,3167- } satisfies DesktopPreviewRecordingSource;3168- }).pipe(Effect.onError(() => stopFrameCapture(tabId, "recording").pipe(Effect.ignore))),3184+ installDisplayMediaRequestHandler(requestWebContents.session);3185+ pendingRecording = { tabId, webContents: wc };
- apps/desktop/src/preview/Manager.ts line 3171Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3101,0 +3135,21 @@3135+ if (3136+ previous !== null &&3137+ previous.tabId !== tabId &&3138+ now - previous.armedAtMillis < RECORDING_ARM_GRACE_MS3139+ ) {3140+ return yield* new PreviewRecordingArmConflictError({3141+ tabId,3142+ webContentsId: wc.id,3143+ armedTabId: previous.tabId,3144+ });3145+ }3146+ pendingRecording = { tabId, webContents: wc, armedAtMillis: now };3147+ });3148+3149+ // Installed once per session: answers the renderer's `getDisplayMedia()` with the tab that3150+ // `startRecording` armed, and denies anything else so pages cannot capture on their own.3151+ const installDisplayMediaRequestHandler = (session: Session) => {3152+ if (displayMediaHandlerSessions.has(session)) return;3153+ displayMediaHandlerSessions.add(session);3154+ session.setDisplayMediaRequestHandler((_request, callback) => {3155+ const target = pendingRecording?.webContents;apps/web/src/components/preview/previewAutomationErrors.ts lines 222 to 240OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -220 +220,21 @@220220221+const renderCause = (cause: unknown): string => {222+ if (223+ typeof cause === "object" &&224+ cause !== null &&225+ "message" in cause &&226+ typeof cause.message === "string"227+ ) {228+ const name =229+ "name" in cause && typeof cause.name === "string" && cause.name.length > 0230+ ? cause.name231+ : null;232+ return name ? `${name}: ${cause.message}` : cause.message;233+ }234+ try {235+ // Null-prototype objects and hostile `toString` implementations both throw here.236+ return String(cause);237+ } catch {238+ return "[unserializable cause]";239+ }240+};
- apps/desktop/src/preview/Manager.ts line 3146OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3101,0 +3126,21 @@3126+ * fails fast and the renderer can retry. An arm the renderer never redeemed goes stale after3127+ * `RECORDING_ARM_GRACE_MS` so it cannot hold the slot forever.3128+ */3129+ const armPendingRecording = Effect.fn("PreviewManager.armPendingRecording")(function* (3130+ tabId: string,3131+ wc: Electron.WebContents,3132+ ) {3133+ const now = yield* Clock.currentTimeMillis;3134+ const previous = pendingRecording;3135+ if (3136+ previous !== null &&3137+ previous.tabId !== tabId &&3138+ now - previous.armedAtMillis < RECORDING_ARM_GRACE_MS3139+ ) {3140+ return yield* new PreviewRecordingArmConflictError({3141+ tabId,3142+ webContentsId: wc.id,3143+ armedTabId: previous.tabId,3144+ });3145+ }3146+ pendingRecording = { tabId, webContents: wc, armedAtMillis: now };
- apps/web/src/components/preview/previewAutomationErrors.ts lines 258 to 259OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -221,9 +240,20 @@240+};241+242+/** Compact, stack-free rendering of a failure cause so the server log keeps the real reason. */243+const summarizeCause = (cause: unknown): string | null => {244+ if (cause === undefined || cause === null) return null;245+ const rendered = renderCause(cause);246+ return rendered.length === 0 ? null : rendered;247+};248+221249 export function serializePreviewAutomationHostError(222250 error: PreviewAutomationHostError,223251 ): NonNullable<PreviewAutomationResponse["error"]> {224- const detail = Object.fromEntries(252+ const detail: Record<string, unknown> = Object.fromEntries(225253 Object.entries(error).filter(226254 ([key]) =>227255 key !== "_tag" && key !== "cause" && key !== "name" && key !== "message" && key !== "stack",228256 ),229257 );258+ const causeSummary = "cause" in error ? summarizeCause(error.cause) : null;259+ if (causeSummary !== null) detail.cause = causeSummary;
- apps/desktop/src/preview/Manager.ts line 3171Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3101,0 +3149,21 @@3149+ // The handler callback is sync and cannot read a clock, so expiry is driven from here.3150+ // Identity compare: a re-arm replaces the object, and this fiber must not clobber it.3151+ yield* Effect.forkIn(3152+ Effect.sleep(RECORDING_ARM_GRACE_MS).pipe(3153+ Effect.andThen(3154+ Effect.sync(() => {3155+ if (pendingRecording === armed) pendingRecording = null;3156+ }),3157+ ),3158+ ),3159+ parentScope,3160+ );3161+ });3162+3163+ // Installed once per session: answers the renderer's `getDisplayMedia()` with the tab that3164+ // `startRecording` armed, and denies anything else so pages cannot capture on their own.3165+ const installDisplayMediaRequestHandler = (session: Session) => {3166+ if (displayMediaHandlerSessions.has(session)) return;3167+ displayMediaHandlerSessions.add(session);3168+ session.setDisplayMediaRequestHandler((_request, callback) => {3169+ const target = pendingRecording?.webContents;
juliusmarminge closed this
Sign in to join this conversation.




macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed