diff --git a/src/main/browser/cdp/commands/cdp-screenshot.ts b/src/main/browser/cdp/commands/cdp-screenshot.ts index bf263be6653..76cc4726365 100644 --- a/src/main/browser/cdp/commands/cdp-screenshot.ts +++ b/src/main/browser/cdp/commands/cdp-screenshot.ts @@ -284,9 +284,7 @@ export async function captureScreenshot( } const fallback = await readRemoteCliAdmittedPage(webContents.id, () => withTimeout( - Promise.resolve().then(() => - holdPaintUntilSettled(holdPaint, () => webContents.capturePage()) - ), + holdPaintUntilSettled(holdPaint, () => webContents.capturePage()), FALLBACK_CAPTURE_TIMEOUT_MS, SCREENSHOT_TIMEOUT_MESSAGE ) diff --git a/src/main/browser/cdp/debugger/guest-paint-hold.ts b/src/main/browser/cdp/debugger/guest-paint-hold.ts index a17195d7808..c235cf72541 100644 --- a/src/main/browser/cdp/debugger/guest-paint-hold.ts +++ b/src/main/browser/cdp/debugger/guest-paint-hold.ts @@ -4,6 +4,18 @@ import { acquireElectronDebugger } from './electron-debugger-lease' /** Draws a hidden guest while a capture or agent input needs its frames; returns its release. */ export type CapturePaintHold = () => () => void +function retainUntilSettled(acquire: () => () => void, run: () => Promise): Promise { + let release: () => void + try { + release = acquire() + } catch (error) { + return Promise.reject(error) + } + const pending = new Promise((resolve) => resolve(run())) + void pending.then(release, release) + return pending +} + /** * Holds paint until Chromium answers the capture, not just while the caller waits. A capture stays * pending until the guest draws, and while pending it can hold macOS display sleep off ("Capturing"); @@ -13,16 +25,7 @@ export function holdPaintUntilSettled( holdPaint: CapturePaintHold, capture: () => Promise ): Promise { - const release = holdPaint() - let pending: Promise - try { - pending = capture() - } catch (error) { - release() - throw error - } - void pending.then(release, release) - return pending + return retainUntilSettled(holdPaint, capture) } /** Also keeps the debugger attached: a detach rejects the command while Chromium keeps the capture. */ @@ -31,19 +34,8 @@ export function holdPaintUntilCdpCaptureSettles( holdPaint: CapturePaintHold, capture: () => Promise ): Promise { - let debuggerLease: ReturnType - try { - debuggerLease = acquireElectronDebugger(webContents) - } catch (error) { - return Promise.reject(error) - } - let pending: Promise - try { - pending = holdPaintUntilSettled(holdPaint, capture) - } catch (error) { - debuggerLease.release() - throw error - } - void pending.then(debuggerLease.release, debuggerLease.release) - return pending + return retainUntilSettled( + () => acquireElectronDebugger(webContents).release, + () => retainUntilSettled(holdPaint, capture) + ) } diff --git a/src/main/browser/cdp/screencast/browser-screencast-snapshot-capture.ts b/src/main/browser/cdp/screencast/browser-screencast-snapshot-capture.ts index b4c393fb3ee..b9b3c1f037d 100644 --- a/src/main/browser/cdp/screencast/browser-screencast-snapshot-capture.ts +++ b/src/main/browser/cdp/screencast/browser-screencast-snapshot-capture.ts @@ -4,8 +4,7 @@ import { readBrowserScreencastImageSize } from './browser-screencast-image-size' import { runDebuggerCommandWithTimeout } from './browser-screencast-debugger-command' import { holdPaintUntilCdpCaptureSettles, - holdPaintUntilSettled, - type CapturePaintHold + holdPaintUntilSettled } from '../debugger/guest-paint-hold' import type { BrowserScreencastOptions, @@ -26,7 +25,6 @@ type BrowserScreencastSnapshotCaptureDeps = { getSeq: () => number queueFrame: (frame: PendingScreencastFrame) => void applyDeviceMetricsOverride: () => Promise - holdPaint: CapturePaintHold } export type BrowserScreencastSnapshotCapture = { @@ -40,7 +38,8 @@ export function createBrowserScreencastSnapshotCapture( deps: BrowserScreencastSnapshotCaptureDeps ): BrowserScreencastSnapshotCapture { const { webContents, dbg, options, isClosed, isStopping, getSeq, queueFrame } = deps - const { applyDeviceMetricsOverride, holdPaint } = deps + const { applyDeviceMetricsOverride } = deps + const { holdPaint } = options let snapshotGeneration = 0 let navigationCaptureTimer: ReturnType | null = null diff --git a/src/main/browser/cdp/screencast/browser-screencast-stream.ts b/src/main/browser/cdp/screencast/browser-screencast-stream.ts index 6f35bcbd1a0..130031924ae 100644 --- a/src/main/browser/cdp/screencast/browser-screencast-stream.ts +++ b/src/main/browser/cdp/screencast/browser-screencast-stream.ts @@ -90,7 +90,6 @@ export async function startBrowserScreencast( isStopping, getSeq: framePacer.getSeq, queueFrame: framePacer.queueFrame, - holdPaint: options.holdPaint, applyDeviceMetricsOverride: () => { const generation = emulationGeneration const applied = deviceMetrics.apply()