The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.copyFile cannot request an exclusive copy. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import * as NodeZlib from "node:zlib"; |
| 4 | import { |
| 5 | DesktopPreviewAnnotationVoiceEventSchema, |
| 6 | DesktopPreviewRecordingInputSchema, |
| 7 | DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import type { |
| 10 | DesktopPreviewAnnotationTheme, |
| 11 | DesktopPreviewAnnotationVoiceConfig, |
| 12 | DesktopPreviewAnnotationVoiceState, |
| 13 | DesktopPreviewColorScheme, |
| 14 | DesktopPreviewFavicon, |
| 15 | DesktopPreviewFindResultEvent, |
| 16 | DesktopPreviewOpenLinkEvent, |
| 17 | DesktopPreviewPointerEvent, |
| 18 | PreviewAnnotationPayload, |
| 19 | PreviewAnnotationRect, |
| 20 | PreviewAnnotationSubmissionResult, |
| 21 | DesktopPreviewRecordingArtifact, |
| 22 | DesktopPreviewRecordingFrame, |
| 23 | DesktopPreviewRecordingInputEvent, |
| 24 | DesktopPreviewScreenshotArtifact, |
| 25 | DesktopPreviewTabDefaults, |
| 26 | PreviewForwardedShortcut, |
| 27 | PreviewAutomationLoadError, |
| 28 | } from "@t3tools/contracts"; |
| 29 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 30 | import { matchesKeybindingShortcut } from "@t3tools/shared/keybindings"; |
| 31 | import { normalizePreviewUrl } from "@t3tools/shared/preview"; |
| 32 | import { |
| 33 | BrowserWindow, |
| 34 | ClipboardItem, |
| 35 | app, |
| 36 | type Session, |
| 37 | clipboard, |
| 38 | nativeImage, |
| 39 | shell, |
| 40 | webContents, |
| 41 | } from "electron"; |
| 42 | import * as Cause from "effect/Cause"; |
| 43 | import * as Clock from "effect/Clock"; |
| 44 | import * as Context from "effect/Context"; |
| 45 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 46 | import * as DateTime from "effect/DateTime"; |
| 47 | import * as Deferred from "effect/Deferred"; |
| 48 | import * as Effect from "effect/Effect"; |
| 49 | import * as Exit from "effect/Exit"; |
| 50 | import * as FileSystem from "effect/FileSystem"; |
| 51 | import * as Fiber from "effect/Fiber"; |
| 52 | import * as Layer from "effect/Layer"; |
| 53 | import * as Option from "effect/Option"; |
| 54 | import * as Path from "effect/Path"; |
| 55 | import * as Predicate from "effect/Predicate"; |
| 56 | import * as Queue from "effect/Queue"; |
| 57 | import * as Ref from "effect/Ref"; |
| 58 | import * as Schema from "effect/Schema"; |
| 59 | import * as Semaphore from "effect/Semaphore"; |
| 60 | import * as Stream from "effect/Stream"; |
| 61 | import * as Schedule from "effect/Schedule"; |
| 62 | import * as Scope from "effect/Scope"; |
| 63 | import * as SynchronizedRef from "effect/SynchronizedRef"; |
| 64 | import { msg } from "gt-i18n"; |
| 65 | |
| 66 | import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 67 | import { m } from "../i18n/messages.ts"; |
| 68 | import * as DesktopRendererHistory from "../telemetry/DesktopRendererHistory.ts"; |
| 69 | import { |
| 70 | MENU_ACTION_CHANNEL, |
| 71 | PREVIEW_ANNOTATION_VOICE_EVENT_CHANNEL, |
| 72 | PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL, |
| 73 | } from "../ipc/channels.ts"; |
| 74 | import * as DesktopBrowserHost from "./DesktopBrowserHost.ts"; |
| 75 | import * as PreviewPasskeys from "./Passkeys.ts"; |
| 76 | import * as BrowserSession from "./BrowserSession.ts"; |
| 77 | import { |
| 78 | ANNOTATION_CAPTURED_CHANNEL, |
| 79 | ANNOTATION_SEND_ENABLED_CHANNEL, |
| 80 | ANNOTATION_VOICE_EVENT_CHANNEL, |
| 81 | ANNOTATION_VOICE_STATE_CHANNEL, |
| 82 | ANNOTATION_THEME_CHANNEL, |
| 83 | CANCEL_PICK_CHANNEL, |
| 84 | ELEMENT_PICKED_CHANNEL, |
| 85 | HUMAN_INPUT_CHANNEL, |
| 86 | PAGE_UNHANDLED_SHORTCUT_CHANNEL, |
| 87 | RECORDING_CURSOR_CHANNEL, |
| 88 | RECORDING_INPUT_CHANNEL, |
| 89 | RECORDING_CONTROLLER_CHANNEL, |
| 90 | RECORDING_POINTER_CHANNEL, |
| 91 | START_PICK_CHANNEL, |
| 92 | THUMB_PRESS_CHANNEL, |
| 93 | } from "./GuestProtocol.ts"; |
| 94 | import { isPreviewAnnotationPayload } from "./PickedElementPayload.ts"; |
| 95 | import { captureFavicon, safeHttpOrigin, selectFaviconCandidates } from "./FaviconCapture.ts"; |
| 96 | import { DEFAULT_RECORDING_INPUT_OPTIONS, type RecordingInputOptions } from "./RecordingInput.ts"; |
| 97 | |
| 98 | export type PreviewNavStatus = |
| 99 | | { kind: "Idle" } |
| 100 | | { kind: "Loading"; url: string; title: string } |
| 101 | | { kind: "Success"; url: string; title: string } |
| 102 | | { |
| 103 | kind: "LoadFailed"; |
| 104 | url: string; |
| 105 | title: string; |
| 106 | code: number; |
| 107 | description: string; |
| 108 | }; |
| 109 | |
| 110 | export interface PreviewTabState { |
| 111 | tabId: string; |
| 112 | webContentsId: number | null; |
| 113 | navStatus: PreviewNavStatus; |
| 114 | canGoBack: boolean; |
| 115 | canGoForward: boolean; |
| 116 | zoomFactor: number; |
| 117 | pictureInPicture: boolean; |
| 118 | colorScheme: DesktopPreviewColorScheme; |
| 119 | audioMuted: boolean; |
| 120 | audible: boolean; |
| 121 | controller: "human" | "agent" | "none"; |
| 122 | capturing: boolean; |
| 123 | favicon?: DesktopPreviewFavicon; |
| 124 | serverTab?: { readonly threadId: string; readonly tabId: string }; |
| 125 | updatedAt: string; |
| 126 | } |
| 127 | |
| 128 | const ZOOM_LEVELS: ReadonlyArray<number> = [ |
| 129 | 0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0, |
| 130 | ]; |
| 131 | |
| 132 | const DEFAULT_ZOOM_FACTOR = 1.0; |
| 133 | const ZOOM_EPSILON = 0.001; |
| 134 | const RECORDING_ARM_GRACE_MS = 10_000; |
| 135 | const PICTURE_IN_PICTURE_FRAME_INTERVAL_MS = Math.ceil(1_000 / 12); |
| 136 | const PICTURE_IN_PICTURE_JPEG_QUALITY = 80; |
| 137 | // Cold guests can reject capturePage with UnknownVizError or stall; bound each attempt. |
| 138 | const CAPTURE_PAGE_RETRY_ATTEMPTS = 3; |
| 139 | const CAPTURE_PAGE_RETRY_DELAY_MS = 120; |
| 140 | const CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS = 1_000; |
| 141 | const BUSY_PAGE_PROBE_MS = 1_000; |
| 142 | const PICTURE_IN_PICTURE_INITIAL_WIDTH = 480; |
| 143 | const PICTURE_IN_PICTURE_INITIAL_HEIGHT = 320; |
| 144 | const PICTURE_IN_PICTURE_MIN_WIDTH = 240; |
| 145 | const PICTURE_IN_PICTURE_MIN_HEIGHT = 160; |
| 146 | const PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON = 0.002; |
| 147 | const MAX_ARTIFACT_SITE_SLUG_LENGTH = 80; |
| 148 | const requestRecordingCaptureExpression = (tabId: string): string => |
| 149 | `globalThis[${JSON.stringify(DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER)}]?.(${JSON.stringify(tabId)}) === true`; |
| 150 | const DEFAULT_ANNOTATION_THEME: DesktopPreviewAnnotationTheme = { |
| 151 | colorScheme: "light", |
| 152 | radius: "0.625rem", |
| 153 | background: "white", |
| 154 | foreground: "oklch(0.269 0 0)", |
| 155 | popover: "white", |
| 156 | popoverForeground: "oklch(0.269 0 0)", |
| 157 | primary: "oklch(0.488 0.217 264)", |
| 158 | primaryForeground: "white", |
| 159 | muted: "rgb(0 0 0 / 4%)", |
| 160 | mutedForeground: "oklch(0.556 0 0)", |
| 161 | accent: "rgb(0 0 0 / 4%)", |
| 162 | accentForeground: "oklch(0.269 0 0)", |
| 163 | border: "rgb(0 0 0 / 8%)", |
| 164 | input: "rgb(0 0 0 / 10%)", |
| 165 | ring: "oklch(0.488 0.217 264)", |
| 166 | fontSans: "system-ui, sans-serif", |
| 167 | fontMono: "ui-monospace, monospace", |
| 168 | }; |
| 169 | |
| 170 | const buildPreviewPictureInPictureDataUrl = (): string => { |
| 171 | const html = `<!doctype html> |
| 172 | <html> |
| 173 | <head> |
| 174 | <meta charset="utf-8"> |
| 175 | <meta |
| 176 | http-equiv="Content-Security-Policy" |
| 177 | content="default-src 'none'; img-src data:; style-src 'unsafe-inline'; script-src 'unsafe-inline'" |
| 178 | > |
| 179 | <meta name="color-scheme" content="dark"> |
| 180 | <style> |
| 181 | html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: #111; } |
| 182 | body { display: grid; place-items: center; } |
| 183 | img { width: 100%; height: 100%; object-fit: contain; user-select: none; -webkit-user-drag: none; } |
| 184 | </style> |
| 185 | </head> |
| 186 | <body> |
| 187 | <img id="preview-frame" alt="Live browser preview"> |
| 188 | <script> |
| 189 | const frame = document.getElementById("preview-frame"); |
| 190 | window.previewPictureInPicture.onFrame((next) => { |
| 191 | frame.src = "data:image/jpeg;base64," + next.data; |
| 192 | }); |
| 193 | </script> |
| 194 | </body> |
| 195 | </html>`; |
| 196 | return `data:text/html;charset=utf-8,${encodeURIComponent(html)}`; |
| 197 | }; |
| 198 | |
| 199 | export const fitPictureInPictureContentSize = ( |
| 200 | current: ReadonlyArray<number>, |
| 201 | aspectRatio: number, |
| 202 | ): readonly [width: number, height: number] => { |
| 203 | const currentWidth = Math.max(1, current[0] ?? PICTURE_IN_PICTURE_INITIAL_WIDTH); |
| 204 | const currentHeight = Math.max(1, current[1] ?? PICTURE_IN_PICTURE_INITIAL_HEIGHT); |
| 205 | const currentArea = currentWidth * currentHeight; |
| 206 | let width = Math.sqrt(currentArea * aspectRatio); |
| 207 | let height = width / aspectRatio; |
| 208 | const minimumScale = Math.max( |
| 209 | 1, |
| 210 | PICTURE_IN_PICTURE_MIN_WIDTH / width, |
| 211 | PICTURE_IN_PICTURE_MIN_HEIGHT / height, |
| 212 | ); |
| 213 | width *= minimumScale; |
| 214 | height *= minimumScale; |
| 215 | return [Math.round(width), Math.round(height)]; |
| 216 | }; |
| 217 | |
| 218 | const recordingFileExtension = (mimeType: string): string => { |
| 219 | const subtype = mimeType.split(";", 1)[0]?.trim().toLowerCase().split("/")[1] ?? ""; |
| 220 | const extension = subtype.replace(/^x-/, "").replace(/[^a-z0-9]/g, ""); |
| 221 | return extension || "video"; |
| 222 | }; |
| 223 | |
| 224 | const artifactSiteSlug = (rawUrl: string): string => { |
| 225 | try { |
| 226 | const url = new URL(rawUrl); |
| 227 | const slug = url.hostname |
| 228 | .toLowerCase() |
| 229 | .replace(/[^a-z0-9]+/g, "-") |
| 230 | .replace(/^-+|-+$/g, "") |
| 231 | .slice(0, MAX_ARTIFACT_SITE_SLUG_LENGTH) |
| 232 | .replace(/-+$/g, ""); |
| 233 | return slug || "site"; |
| 234 | } catch { |
| 235 | return "site"; |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | export const PreviewAutomationSelectorKind = Schema.Literals([ |
| 240 | "focused-element", |
| 241 | "selector", |
| 242 | "locator", |
| 243 | ]); |
| 244 | export type PreviewAutomationSelectorKind = typeof PreviewAutomationSelectorKind.Type; |
| 245 | |
| 246 | export const PreviewAutomationEvaluationDetailKind = Schema.Literals([ |
| 247 | "exception-description", |
| 248 | "exception-text", |
| 249 | "unknown", |
| 250 | ]); |
| 251 | export type PreviewAutomationEvaluationDetailKind = |
| 252 | typeof PreviewAutomationEvaluationDetailKind.Type; |
| 253 | |
| 254 | interface PreviewOperationContext { |
| 255 | readonly operation: string; |
| 256 | readonly tabId?: string; |
| 257 | readonly webContentsId?: number; |
| 258 | readonly artifactPath?: string; |
| 259 | } |
| 260 | |
| 261 | const normalizeCaptureRect = (value: unknown): PreviewAnnotationRect | null => { |
| 262 | if (typeof value !== "object" || value === null) return null; |
| 263 | const rect = value as Record<string, unknown>; |
| 264 | const x = rect["x"]; |
| 265 | const y = rect["y"]; |
| 266 | const width = rect["width"]; |
| 267 | const height = rect["height"]; |
| 268 | if ( |
| 269 | typeof x !== "number" || |
| 270 | !Number.isFinite(x) || |
| 271 | typeof y !== "number" || |
| 272 | !Number.isFinite(y) || |
| 273 | typeof width !== "number" || |
| 274 | !Number.isFinite(width) || |
| 275 | typeof height !== "number" || |
| 276 | !Number.isFinite(height) || |
| 277 | width <= 0 || |
| 278 | height <= 0 |
| 279 | ) { |
| 280 | return null; |
| 281 | } |
| 282 | return { x, y, width, height }; |
| 283 | }; |
| 284 | |
| 285 | // capturePage returns a 1x NativeImage containing device pixels, so crop() uses |
| 286 | // CSS coordinates scaled by the guest's devicePixelRatio (including page zoom). |
| 287 | const cropAnnotationScreenshot = ( |
| 288 | image: Electron.NativeImage, |
| 289 | cropRect: PreviewAnnotationRect | null, |
| 290 | devicePixelRatio: number, |
| 291 | targetRects: ReadonlyArray<PreviewAnnotationRect>, |
| 292 | ): PreviewAnnotationPayload["screenshot"] => { |
| 293 | const full = image.getSize(); |
| 294 | if (full.width <= 0 || full.height <= 0) return null; |
| 295 | const scale = Number.isFinite(devicePixelRatio) && devicePixelRatio > 0 ? devicePixelRatio : 1; |
| 296 | // Padding or a union of offscreen targets can overlap an empty viewport area. |
| 297 | // A zero-width/height SVG line can still paint at the top or left edge. |
| 298 | if ( |
| 299 | targetRects.length > 0 && |
| 300 | !targetRects.some( |
| 301 | (rect) => |
| 302 | (rect.width > 0 || rect.height > 0) && |
| 303 | rect.x * scale < full.width && |
| 304 | rect.y * scale < full.height && |
| 305 | (rect.width === 0 ? rect.x >= 0 : rect.x + rect.width > 0) && |
| 306 | (rect.height === 0 ? rect.y >= 0 : rect.y + rect.height > 0), |
| 307 | ) |
| 308 | ) { |
| 309 | return null; |
| 310 | } |
| 311 | if (!cropRect) { |
| 312 | return { |
| 313 | dataUrl: image.toDataURL(), |
| 314 | width: full.width, |
| 315 | height: full.height, |
| 316 | cropRect: { x: 0, y: 0, width: full.width, height: full.height }, |
| 317 | }; |
| 318 | } |
| 319 | const x = Math.max(0, Math.floor(cropRect.x * scale)); |
| 320 | const y = Math.max(0, Math.floor(cropRect.y * scale)); |
| 321 | const right = Math.min(full.width, Math.ceil((cropRect.x + cropRect.width) * scale)); |
| 322 | const bottom = Math.min(full.height, Math.ceil((cropRect.y + cropRect.height) * scale)); |
| 323 | // A target scrolled out of view has no pixels in this capture. Clamping its |
| 324 | // origin onto the edge would attach an unrelated strip as a successful crop. |
| 325 | if (right <= x || bottom <= y) return null; |
| 326 | const cropped = image.crop({ |
| 327 | x, |
| 328 | y, |
| 329 | width: right - x, |
| 330 | height: bottom - y, |
| 331 | }); |
| 332 | const size = cropped.getSize(); |
| 333 | if (size.width <= 0 || size.height <= 0) return null; |
| 334 | return { dataUrl: cropped.toDataURL(), width: size.width, height: size.height, cropRect }; |
| 335 | }; |
| 336 | |
| 337 | // An invalid color space skips costly P3-to-sRGB conversion; fingerprints need no color accuracy. |
| 338 | const FINGERPRINT_BITMAP_OPTIONS: Electron.ToBitmapOptions = { |
| 339 | colorSpace: { primaries: "invalid", transfer: "invalid", matrix: "invalid", range: "invalid" }, |
| 340 | }; |
| 341 | |
| 342 | /** Empty readPixels results have no fingerprint and must be treated as changed frames. */ |
| 343 | const fingerprintCapturedFrame = (image: Electron.NativeImage, size: Electron.Size) => { |
| 344 | let bitmap: Buffer; |
| 345 | try { |
| 346 | bitmap = image.toBitmap(FINGERPRINT_BITMAP_OPTIONS); |
| 347 | } catch { |
| 348 | return null; |
| 349 | } |
| 350 | if (bitmap.byteLength === 0) return null; |
| 351 | return `${size.width}x${size.height}:${NodeZlib.crc32(bitmap)}`; |
| 352 | }; |
| 353 | |
| 354 | const findZoomStep = (current: number): number => { |
| 355 | const index = ZOOM_LEVELS.findIndex( |
| 356 | (level) => Math.abs(level - current) < ZOOM_EPSILON || level > current, |
| 357 | ); |
| 358 | if (index < 0) return ZOOM_LEVELS.length - 1; |
| 359 | return Math.abs(ZOOM_LEVELS[index]! - current) < ZOOM_EPSILON ? index : index - 1; |
| 360 | }; |
| 361 | |
| 362 | const normalizeZoomFactor = (value: number | undefined): number => { |
| 363 | if (value === undefined || !Number.isFinite(value)) return DEFAULT_ZOOM_FACTOR; |
| 364 | let closest = ZOOM_LEVELS[0]!; |
| 365 | for (const level of ZOOM_LEVELS) { |
| 366 | if (Math.abs(level - value) < Math.abs(closest - value)) closest = level; |
| 367 | } |
| 368 | return closest; |
| 369 | }; |
| 370 | |
| 371 | const nextZoomLevel = (current: number, direction: "in" | "out"): number => { |
| 372 | const step = findZoomStep(current); |
| 373 | if (direction === "in") { |
| 374 | return ZOOM_LEVELS[Math.min(step + 1, ZOOM_LEVELS.length - 1)] ?? current; |
| 375 | } |
| 376 | return ZOOM_LEVELS[Math.max(step - 1, 0)] ?? current; |
| 377 | }; |
| 378 | |
| 379 | type Listener = (tabId: string, state: PreviewTabState) => Effect.Effect<void>; |
| 380 | |
| 381 | type PreviewInputSignal = |
| 382 | | { readonly kind: "pointer"; readonly x: number; readonly y: number; readonly button: number } |
| 383 | | { readonly kind: "key"; readonly key: string; readonly code: string }; |
| 384 | |
| 385 | interface ManagedListeners { |
| 386 | readonly attachmentId: symbol; |
| 387 | readonly cancelFaviconCapture: () => void; |
| 388 | readonly scope: Scope.Closeable; |
| 389 | readonly webContents: Electron.WebContents; |
| 390 | } |
| 391 | |
| 392 | // Each still-image request owns a separate, short-lived rendering lease. |
| 393 | type FrameCaptureConsumer = "picture-in-picture" | "recording" | symbol; |
| 394 | |
| 395 | interface FrameCaptureSession { |
| 396 | readonly recordingInputOptions?: RecordingInputOptions; |
| 397 | readonly scope: Scope.Closeable | null; |
| 398 | readonly consumers: ReadonlySet<FrameCaptureConsumer>; |
| 399 | readonly unthrottledWebContentsIds: ReadonlySet<number>; |
| 400 | readonly lastPictureInPictureFingerprint: string | null; |
| 401 | } |
| 402 | |
| 403 | interface PictureInPictureSession { |
| 404 | readonly window: BrowserWindow; |
| 405 | readonly webContentsId: number; |
| 406 | readonly ready: Deferred.Deferred<void, PreviewManagerError>; |
| 407 | readonly initializationScope: Scope.Closeable; |
| 408 | } |
| 409 | |
| 410 | interface PendingRecording { |
| 411 | readonly tabId: string; |
| 412 | readonly webContents: Electron.WebContents; |
| 413 | readonly requestingFrameTreeNodeId: number; |
| 414 | readonly armedAtMillis: number; |
| 415 | } |
| 416 | |
| 417 | interface PickSession { |
| 418 | readonly voiceSessionId?: string | undefined; |
| 419 | readonly cancel: Effect.Effect<void>; |
| 420 | } |
| 421 | |
| 422 | const decodeAnnotationVoiceEvent = Schema.decodeUnknownOption( |
| 423 | DesktopPreviewAnnotationVoiceEventSchema, |
| 424 | ); |
| 425 | |
| 426 | interface BrowserControlSession { |
| 427 | readonly webContentsId: number; |
| 428 | // Retain the attached Debugger to prevent GC crashes (electron/electron#53376); wc.debugger throws after destruction. |
| 429 | readonly debugger: Electron.Debugger; |
| 430 | readonly semaphore: Semaphore.Semaphore; |
| 431 | readonly scope: Scope.Closeable; |
| 432 | readonly onMessage: ( |
| 433 | event: Electron.Event, |
| 434 | method: string, |
| 435 | params: Record<string, unknown>, |
| 436 | ) => void; |
| 437 | } |
| 438 | |
| 439 | const isRecordingInput = Schema.is(DesktopPreviewRecordingInputSchema); |
| 440 | |
| 441 | type RecordingInputListener = (event: DesktopPreviewRecordingInputEvent) => Effect.Effect<void>; |
| 442 | |
| 443 | type PointerEventListener = (event: DesktopPreviewPointerEvent) => Effect.Effect<void>; |
| 444 | |
| 445 | type OpenLinkListener = (event: DesktopPreviewOpenLinkEvent) => Effect.Effect<void>; |
| 446 | |
| 447 | type FindResultListener = (event: DesktopPreviewFindResultEvent) => Effect.Effect<void>; |
| 448 | |
| 449 | const PAGE_FIRST_FORWARDED_COMMANDS: ReadonlySet<PreviewForwardedShortcut["command"]> = new Set([ |
| 450 | "preview.find", |
| 451 | "preview.zoomIn", |
| 452 | "preview.zoomOut", |
| 453 | "preview.resetZoom", |
| 454 | ]); |
| 455 | |
| 456 | const decodePageShortcutKeys = Schema.decodeUnknownOption( |
| 457 | Schema.Struct({ |
| 458 | key: Schema.String, |
| 459 | code: Schema.String, |
| 460 | metaKey: Schema.Boolean, |
| 461 | ctrlKey: Schema.Boolean, |
| 462 | shiftKey: Schema.Boolean, |
| 463 | altKey: Schema.Boolean, |
| 464 | }), |
| 465 | ); |
| 466 | |
| 467 | // about:blank inherits relaxed guest preferences; external editor schemes must reach the OS handler. |
| 468 | const POPUP_PROTOCOLS = new Set(["http:", "https:"]); |
| 469 | |
| 470 | // Main-process loadURL bypasses Chromium's renderer-side restrictions on local and inline URLs. |
| 471 | const IN_PLACE_DENIED_PROTOCOLS = new Set([ |
| 472 | "about:", |
| 473 | "chrome:", |
| 474 | "data:", |
| 475 | "devtools:", |
| 476 | "file:", |
| 477 | "filesystem:", |
| 478 | "javascript:", |
| 479 | "view-source:", |
| 480 | ]); |
| 481 | |
| 482 | /** Popups bypass will-attach-webview hardening and would inherit the picker's contextIsolation:false. */ |
| 483 | const POPUP_WINDOW_OPTIONS = { |
| 484 | webPreferences: { |
| 485 | contextIsolation: true, |
| 486 | nodeIntegration: false, |
| 487 | sandbox: true, |
| 488 | }, |
| 489 | } satisfies Electron.BrowserWindowConstructorOptions; |
| 490 | |
| 491 | /** |
| 492 | * OAuth needs a surviving popup opener. `blob:`, app links (behind the session's `openExternal` |
| 493 | * prompt) and form POSTs stay in place. |
| 494 | */ |
| 495 | export const previewWindowOpenAction = (details: { |
| 496 | readonly url: string; |
| 497 | readonly disposition: Electron.HandlerDetails["disposition"]; |
| 498 | readonly postBody?: Electron.PostBody; |
| 499 | }): "popup" | "new-tab" | "navigate" | "deny" => { |
| 500 | const protocol = URL.parse(details.url)?.protocol; |
| 501 | // MSAL redirects on a denied blank popup; navigating the tab would destroy that fallback. |
| 502 | if (protocol === undefined || IN_PLACE_DENIED_PROTOCOLS.has(protocol)) return "deny"; |
| 503 | if (!POPUP_PROTOCOLS.has(protocol)) return "navigate"; |
| 504 | if (details.disposition === "new-window") return "popup"; |
| 505 | return !details.postBody && |
| 506 | (details.disposition === "foreground-tab" || details.disposition === "background-tab") |
| 507 | ? "new-tab" |
| 508 | : "navigate"; |
| 509 | }; |
| 510 | |
| 511 | const isPreviewRefreshShortcut = (input: Electron.Input, platform: NodeJS.Platform): boolean => |
| 512 | input.type === "keyDown" && |
| 513 | input.key.toLowerCase() === "r" && |
| 514 | (platform === "darwin" ? input.meta && !input.control : input.control && !input.meta) && |
| 515 | !input.shift && |
| 516 | !input.alt; |
| 517 | |
| 518 | export const isPreviewEditingShortcut = ( |
| 519 | input: Electron.Input, |
| 520 | platform: NodeJS.Platform, |
| 521 | ): boolean => { |
| 522 | const isMac = platform === "darwin"; |
| 523 | if (isMac ? !input.meta || input.control : !input.control || input.meta) return false; |
| 524 | |
| 525 | const key = input.key.toLowerCase(); |
| 526 | // Option changes the DOM key for macOS Paste and Match Style (for example, to ◊). |
| 527 | if (isMac && input.alt && input.shift && input.code === "KeyV") return true; |
| 528 | if (key === "v" && input.shift) return input.alt === isMac; |
| 529 | if (input.alt) return false; |
| 530 | if (key === "z") return !input.shift || platform !== "win32"; |
| 531 | if (input.shift) return false; |
| 532 | return ( |
| 533 | key === "a" || |
| 534 | key === "c" || |
| 535 | key === "v" || |
| 536 | key === "x" || |
| 537 | (key === "y" && platform === "win32") |
| 538 | ); |
| 539 | }; |
| 540 | |
| 541 | const isPreviewInputSignal = (value: unknown): value is PreviewInputSignal => { |
| 542 | if (typeof value !== "object" || value === null || !("kind" in value)) return false; |
| 543 | if (value.kind === "pointer") { |
| 544 | return ( |
| 545 | "x" in value && |
| 546 | typeof value.x === "number" && |
| 547 | "y" in value && |
| 548 | typeof value.y === "number" && |
| 549 | "button" in value && |
| 550 | typeof value.button === "number" |
| 551 | ); |
| 552 | } |
| 553 | return ( |
| 554 | value.kind === "key" && |
| 555 | "key" in value && |
| 556 | typeof value.key === "string" && |
| 557 | "code" in value && |
| 558 | typeof value.code === "string" |
| 559 | ); |
| 560 | }; |
| 561 | |
| 562 | const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function* ( |
| 563 | artifactDirectory: string, |
| 564 | pictureInPicturePreloadPath: string, |
| 565 | ) { |
| 566 | const fileSystem = yield* FileSystem.FileSystem; |
| 567 | const rendererHistory = yield* DesktopRendererHistory.DesktopRendererHistory; |
| 568 | const hostPlatform = yield* HostProcess.Platform; |
| 569 | const path = yield* Path.Path; |
| 570 | const random = yield* SecureRandom.SecureRandom; |
| 571 | const parentScope = yield* Scope.Scope; |
| 572 | const browserHost = yield* DesktopBrowserHost.DesktopBrowserHost; |
| 573 | const browserSession = yield* BrowserSession.BrowserSession; |
| 574 | const passkeys = yield* PreviewPasskeys.PreviewPasskeys; |
| 575 | const context = yield* Effect.context<never>(); |
| 576 | const runFork = Effect.runForkWith(context); |
| 577 | const runPromise = Effect.runPromiseWith(context); |
| 578 | const resolvedArtifactDirectory = path.resolve(artifactDirectory); |
| 579 | |
| 580 | let forwardedShortcuts: ReadonlyArray<PreviewForwardedShortcut> = []; |
| 581 | const annotationThemeRef = yield* Ref.make(DEFAULT_ANNOTATION_THEME); |
| 582 | const annotationSendEnabled = new Map<string, boolean>(); |
| 583 | const mainWindowRef = yield* Ref.make<Option.Option<BrowserWindow>>(Option.none()); |
| 584 | const tabsRef = yield* SynchronizedRef.make<ReadonlyMap<string, PreviewTabState>>(new Map()); |
| 585 | const attachedRef = yield* Ref.make<ReadonlyMap<number, ManagedListeners>>(new Map()); |
| 586 | const listenersRef = yield* Ref.make<ReadonlySet<Listener>>(new Set()); |
| 587 | const pointerEventListenersRef = yield* Ref.make<ReadonlySet<PointerEventListener>>(new Set()); |
| 588 | const openLinkListenersRef = yield* Ref.make<ReadonlySet<OpenLinkListener>>(new Set()); |
| 589 | const findResultListenersRef = yield* Ref.make<ReadonlySet<FindResultListener>>(new Set()); |
| 590 | const recordingInputListenersRef = yield* Ref.make<ReadonlySet<RecordingInputListener>>( |
| 591 | new Set(), |
| 592 | ); |
| 593 | const pickSessionsRef = yield* Ref.make<ReadonlyMap<string, PickSession>>(new Map()); |
| 594 | const controlSessionsRef = yield* SynchronizedRef.make< |
| 595 | ReadonlyMap<number, BrowserControlSession> |
| 596 | >(new Map()); |
| 597 | const frameCaptureSessionsRef = yield* SynchronizedRef.make< |
| 598 | ReadonlyMap<string, FrameCaptureSession> |
| 599 | >(new Map()); |
| 600 | const pictureInPictureSessionsRef = yield* SynchronizedRef.make< |
| 601 | ReadonlyMap<string, PictureInPictureSession> |
| 602 | >(new Map()); |
| 603 | const pictureInPictureAspectRatiosRef = yield* Ref.make<ReadonlyMap<string, number>>(new Map()); |
| 604 | const pictureInPictureMutationSemaphore = yield* Semaphore.make(1); |
| 605 | const closingTabIdsRef = yield* Ref.make<ReadonlySet<string>>(new Set()); |
| 606 | // Legacy tab capture is unsupported (electron/electron#44618); recording uses setDisplayMediaRequestHandler. |
| 607 | let pendingRecording: PendingRecording | null = null; |
| 608 | const displayMediaHandlerSessions = new WeakSet<Session>(); |
| 609 | let frameCaptureWindowOpen = true; |
| 610 | let currentMainWindow: BrowserWindow | undefined; |
| 611 | let mainWindowCleanupFiber: Fiber.Fiber<void, never> | undefined; |
| 612 | const tabLifecycleLocks = new Map< |
| 613 | string, |
| 614 | { readonly semaphore: Semaphore.Semaphore; users: number } |
| 615 | >(); |
| 616 | const tabLifecycleGenerations = new Map<string, number>(); |
| 617 | // A guest can fail its first load before registerWebview installs its tab listeners. |
| 618 | const guestLoadFailures = new Map<number, PreviewAutomationLoadError>(); |
| 619 | |
| 620 | const attempt = <A>(errorContext: PreviewOperationContext, evaluate: () => A) => |
| 621 | Effect.try({ |
| 622 | try: evaluate, |
| 623 | catch: (cause) => new PreviewOperationError({ ...errorContext, cause }), |
| 624 | }); |
| 625 | const attemptPromise = <A>( |
| 626 | errorContext: PreviewOperationContext, |
| 627 | evaluate: () => PromiseLike<A>, |
| 628 | ) => |
| 629 | Effect.tryPromise({ |
| 630 | try: evaluate, |
| 631 | catch: (cause) => new PreviewOperationError({ ...errorContext, cause }), |
| 632 | }); |
| 633 | const capturePageWithRetry = Effect.fn("PreviewManager.capturePageWithRetry")(function* ( |
| 634 | errorContext: PreviewOperationContext, |
| 635 | tabId: string, |
| 636 | wc: Electron.WebContents, |
| 637 | ) { |
| 638 | const requireCurrentGuest = Effect.gen(function* () { |
| 639 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 640 | if (wc.isDestroyed() || tabs.get(tabId)?.webContentsId !== wc.id) { |
| 641 | return yield* new PreviewWebContentsNotFoundError({ tabId, webContentsId: wc.id }); |
| 642 | } |
| 643 | }); |
| 644 | const capture = Effect.gen(function* () { |
| 645 | yield* requireCurrentGuest; |
| 646 | const image = yield* Effect.tryPromise({ |
| 647 | try: (_signal) => wc.capturePage(), |
| 648 | catch: (cause) => new PreviewOperationError({ ...errorContext, cause }), |
| 649 | }).pipe( |
| 650 | Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS), |
| 651 | Effect.catchTags({ |
| 652 | TimeoutError: (cause) => |
| 653 | Effect.fail(new PreviewOperationError({ ...errorContext, cause })), |
| 654 | }), |
| 655 | ); |
| 656 | yield* requireCurrentGuest; |
| 657 | return image; |
| 658 | }); |
| 659 | return yield* capture.pipe( |
| 660 | Effect.retry({ |
| 661 | times: CAPTURE_PAGE_RETRY_ATTEMPTS - 1, |
| 662 | schedule: Schedule.spaced(CAPTURE_PAGE_RETRY_DELAY_MS), |
| 663 | while: isPreviewOperationError, |
| 664 | }), |
| 665 | ); |
| 666 | }); |
| 667 | const currentIso = DateTime.now.pipe(Effect.map(DateTime.formatIso)); |
| 668 | const currentMillis = Clock.currentTimeMillis; |
| 669 | const replaceMap = <K, V>( |
| 670 | source: ReadonlyMap<K, V>, |
| 671 | update: (copy: Map<K, V>) => void, |
| 672 | ): ReadonlyMap<K, V> => { |
| 673 | const copy = new Map(source); |
| 674 | update(copy); |
| 675 | return copy; |
| 676 | }; |
| 677 | const withTabLifecycleLock = <A, E, R>( |
| 678 | tabId: string, |
| 679 | effect: Effect.Effect<A, E, R>, |
| 680 | ): Effect.Effect<A, E, R> => |
| 681 | Effect.suspend(() => { |
| 682 | const lifecycle = tabLifecycleLocks.get(tabId) ?? { |
| 683 | semaphore: Semaphore.makeUnsafe(1), |
| 684 | users: 0, |
| 685 | }; |
| 686 | lifecycle.users += 1; |
| 687 | tabLifecycleLocks.set(tabId, lifecycle); |
| 688 | return lifecycle.semaphore.withPermit(effect).pipe( |
| 689 | Effect.ensuring( |
| 690 | Effect.sync(() => { |
| 691 | lifecycle.users -= 1; |
| 692 | if (lifecycle.users === 0 && tabLifecycleLocks.get(tabId) === lifecycle) { |
| 693 | tabLifecycleLocks.delete(tabId); |
| 694 | } |
| 695 | }), |
| 696 | ), |
| 697 | ); |
| 698 | }); |
| 699 | const setWindowBackgroundThrottling = Effect.fnUntraced(function* ( |
| 700 | window: BrowserWindow, |
| 701 | enabled: boolean, |
| 702 | ) { |
| 703 | if (window.isDestroyed()) return; |
| 704 | yield* attempt({ operation: "frameCapture.setBackgroundThrottling" }, () => |
| 705 | window.webContents.setBackgroundThrottling(enabled), |
| 706 | ); |
| 707 | }); |
| 708 | const setFrameCaptureBackgroundThrottling = Effect.fnUntraced(function* (enabled: boolean) { |
| 709 | const mainWindow = yield* Ref.get(mainWindowRef); |
| 710 | if (Option.isNone(mainWindow)) return; |
| 711 | yield* setWindowBackgroundThrottling(mainWindow.value, enabled); |
| 712 | }); |
| 713 | const setFrameCaptureWebContentsBackgroundThrottling = Effect.fnUntraced(function* ( |
| 714 | wc: Electron.WebContents, |
| 715 | enabled: boolean, |
| 716 | ) { |
| 717 | if (wc.isDestroyed()) return; |
| 718 | yield* attempt( |
| 719 | { |
| 720 | operation: "frameCapture.setBackgroundThrottling", |
| 721 | webContentsId: wc.id, |
| 722 | }, |
| 723 | () => wc.setBackgroundThrottling(enabled), |
| 724 | ); |
| 725 | }); |
| 726 | const restoreFrameCaptureWebContentsBackgroundThrottling = Effect.fnUntraced(function* ( |
| 727 | webContentsIds: ReadonlySet<number>, |
| 728 | ) { |
| 729 | yield* Effect.forEach( |
| 730 | webContentsIds, |
| 731 | (webContentsId) => { |
| 732 | const wc = webContents.fromId(webContentsId); |
| 733 | if (!wc || wc.isDestroyed()) return Effect.void; |
| 734 | return setFrameCaptureWebContentsBackgroundThrottling(wc, true).pipe( |
| 735 | Effect.retry({ times: 2 }), |
| 736 | Effect.catch((error) => |
| 737 | Effect.logWarning("Failed to restore preview webview frame capture throttling.", { |
| 738 | webContentsId, |
| 739 | error, |
| 740 | }), |
| 741 | ), |
| 742 | ); |
| 743 | }, |
| 744 | { concurrency: "unbounded", discard: true }, |
| 745 | ); |
| 746 | }); |
| 747 | const keepFrameCaptureWebContentsUnthrottled = Effect.fnUntraced(function* ( |
| 748 | tabId: string, |
| 749 | wc: Electron.WebContents, |
| 750 | ) { |
| 751 | yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) => { |
| 752 | const current = sessions.get(tabId); |
| 753 | if (!current || current.unthrottledWebContentsIds.has(wc.id)) { |
| 754 | return Effect.succeed([undefined, sessions] as const); |
| 755 | } |
| 756 | return setFrameCaptureWebContentsBackgroundThrottling(wc, false).pipe( |
| 757 | Effect.tap(() => |
| 758 | Effect.gen(function* () { |
| 759 | if (!current.consumers.has("recording")) return; |
| 760 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 761 | yield* attempt({ operation: "recording.cursor", tabId, webContentsId: wc.id }, () => |
| 762 | wc.send( |
| 763 | RECORDING_CURSOR_CHANNEL, |
| 764 | true, |
| 765 | current.recordingInputOptions, |
| 766 | tab?.controller, |
| 767 | ), |
| 768 | ); |
| 769 | }), |
| 770 | ), |
| 771 | Effect.map( |
| 772 | () => |
| 773 | [ |
| 774 | undefined, |
| 775 | replaceMap(sessions, (copy) => { |
| 776 | copy.set(tabId, { |
| 777 | ...current, |
| 778 | unthrottledWebContentsIds: new Set([...current.unthrottledWebContentsIds, wc.id]), |
| 779 | }); |
| 780 | }), |
| 781 | ] as const, |
| 782 | ), |
| 783 | ); |
| 784 | }); |
| 785 | }); |
| 786 | const stopFrameCapture = Effect.fn("PreviewManager.stopFrameCapture")(function* ( |
| 787 | tabId: string, |
| 788 | consumer: FrameCaptureConsumer, |
| 789 | ) { |
| 790 | yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) => |
| 791 | Effect.gen(function* () { |
| 792 | const current = sessions.get(tabId); |
| 793 | if (!current || !current.consumers.has(consumer)) { |
| 794 | return [undefined, sessions] as const; |
| 795 | } |
| 796 | if (consumer === "recording") { |
| 797 | yield* Effect.forEach(current.unthrottledWebContentsIds, (id) => |
| 798 | attempt({ operation: "recording.cursor", tabId, webContentsId: id }, () => { |
| 799 | const contents = webContents.fromId(id); |
| 800 | if (contents && !contents.isDestroyed()) |
| 801 | contents.send(RECORDING_CURSOR_CHANNEL, false); |
| 802 | }).pipe(Effect.ignore), |
| 803 | ); |
| 804 | } |
| 805 | const consumers = new Set(current.consumers); |
| 806 | consumers.delete(consumer); |
| 807 | if (consumers.size > 0) { |
| 808 | return [ |
| 809 | consumer === "picture-in-picture" ? current.scope : undefined, |
| 810 | replaceMap(sessions, (copy) => { |
| 811 | copy.set(tabId, { |
| 812 | ...current, |
| 813 | scope: consumer === "picture-in-picture" ? null : current.scope, |
| 814 | consumers, |
| 815 | lastPictureInPictureFingerprint: |
| 816 | consumer === "picture-in-picture" |
| 817 | ? null |
| 818 | : current.lastPictureInPictureFingerprint, |
| 819 | }); |
| 820 | }), |
| 821 | ] as const; |
| 822 | } |
| 823 | const remainingSessions = replaceMap(sessions, (copy) => { |
| 824 | copy.delete(tabId); |
| 825 | }); |
| 826 | yield* restoreFrameCaptureWebContentsBackgroundThrottling( |
| 827 | current.unthrottledWebContentsIds, |
| 828 | ); |
| 829 | if (remainingSessions.size === 0) { |
| 830 | yield* setFrameCaptureBackgroundThrottling(true).pipe( |
| 831 | Effect.retry({ times: 2 }), |
| 832 | Effect.catch((error) => |
| 833 | Effect.logWarning("Failed to restore preview frame capture throttling.", { error }), |
| 834 | ), |
| 835 | ); |
| 836 | } |
| 837 | return [current.scope, remainingSessions] as const; |
| 838 | }), |
| 839 | ).pipe( |
| 840 | Effect.flatMap((captureScope) => |
| 841 | captureScope ? Scope.close(captureScope, Exit.void).pipe(Effect.ignore) : Effect.void, |
| 842 | ), |
| 843 | Effect.uninterruptible, |
| 844 | ); |
| 845 | }); |
| 846 | |
| 847 | const stopAllRecordings = Effect.fn("PreviewManager.stopAllRecordings")(function* () { |
| 848 | pendingRecording = null; |
| 849 | const sessions = yield* SynchronizedRef.get(frameCaptureSessionsRef); |
| 850 | yield* Effect.forEach(sessions.keys(), (tabId) => stopFrameCapture(tabId, "recording"), { |
| 851 | concurrency: "unbounded", |
| 852 | discard: true, |
| 853 | }); |
| 854 | }); |
| 855 | |
| 856 | const deliverEvent = ( |
| 857 | eventKind: "state-change" | "recording-input" | "pointer-event" | "open-link" | "find-result", |
| 858 | tabId: string, |
| 859 | delivery: () => Effect.Effect<void>, |
| 860 | ) => |
| 861 | Effect.suspend(delivery).pipe( |
| 862 | Effect.catchCauseIf( |
| 863 | (cause) => !Cause.hasInterrupts(cause), |
| 864 | (cause) => |
| 865 | Effect.logWarning("Desktop preview event listener failed.", { |
| 866 | eventKind, |
| 867 | tabId, |
| 868 | cause, |
| 869 | }), |
| 870 | ), |
| 871 | ); |
| 872 | |
| 873 | const emit = Effect.fn("PreviewManager.emit")(function* (tabId: string, state: PreviewTabState) { |
| 874 | const listeners = yield* Ref.get(listenersRef); |
| 875 | yield* Effect.forEach( |
| 876 | listeners, |
| 877 | (listener) => deliverEvent("state-change", tabId, () => listener(tabId, state)), |
| 878 | { discard: true }, |
| 879 | ); |
| 880 | }); |
| 881 | |
| 882 | const emitIfCurrent = Effect.fn("PreviewManager.emitIfCurrent")(function* ( |
| 883 | tabId: string, |
| 884 | state: PreviewTabState, |
| 885 | ) { |
| 886 | if ((yield* SynchronizedRef.get(tabsRef)).get(tabId) === state) { |
| 887 | yield* emit(tabId, state); |
| 888 | } |
| 889 | }); |
| 890 | |
| 891 | const update = Effect.fn("PreviewManager.update")(function* ( |
| 892 | tabId: string, |
| 893 | patch: Partial<PreviewTabState>, |
| 894 | ) { |
| 895 | const updatedAt = yield* currentIso; |
| 896 | const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 897 | const current = tabs.get(tabId); |
| 898 | if (!current) return [Option.none<PreviewTabState>(), tabs] as const; |
| 899 | const state: PreviewTabState = { ...current, ...patch, updatedAt }; |
| 900 | return [ |
| 901 | Option.some(state), |
| 902 | replaceMap(tabs, (copy) => { |
| 903 | copy.set(tabId, state); |
| 904 | }), |
| 905 | ] as const; |
| 906 | }); |
| 907 | // syncTabAudible can commit after this update; emitIfCurrent prevents publishing a stale snapshot. |
| 908 | if (Option.isSome(next)) { |
| 909 | yield* emitIfCurrent(tabId, next.value); |
| 910 | } |
| 911 | }); |
| 912 | |
| 913 | /** Read zoom after awaits so concurrent zoom actions cannot be rolled back by an older snapshot. */ |
| 914 | const assertTabZoom = Effect.fn("PreviewManager.assertTabZoom")(function* (tabId: string) { |
| 915 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 916 | if (!tab || tab.webContentsId == null) return; |
| 917 | const wc = webContents.fromId(tab.webContentsId); |
| 918 | if (!wc || wc.isDestroyed()) return; |
| 919 | yield* attempt({ operation: "assertTabZoom", tabId, webContentsId: wc.id }, () => |
| 920 | wc.setZoomFactor(tab.zoomFactor), |
| 921 | ).pipe(Effect.ignore); |
| 922 | }); |
| 923 | |
| 924 | /** Read committed mute and current guest at call time so stale snapshots cannot roll back newer actions. */ |
| 925 | const assertTabAudioMuted = Effect.fn("PreviewManager.assertTabAudioMuted")(function* ( |
| 926 | tabId: string, |
| 927 | ) { |
| 928 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 929 | if (!tab || tab.webContentsId == null) return; |
| 930 | const wc = webContents.fromId(tab.webContentsId); |
| 931 | if (!wc || wc.isDestroyed()) return; |
| 932 | yield* attempt({ operation: "assertTabAudioMuted", tabId, webContentsId: wc.id }, () => |
| 933 | wc.setAudioMuted(tab.audioMuted), |
| 934 | ); |
| 935 | }); |
| 936 | |
| 937 | const syncTabAudible = Effect.fn("PreviewManager.syncTabAudible")(function* ( |
| 938 | tabId: string, |
| 939 | wc: Electron.WebContents, |
| 940 | audible: boolean, |
| 941 | ) { |
| 942 | if (wc.isDestroyed()) return; |
| 943 | const updatedAt = yield* currentIso; |
| 944 | const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 945 | const current = tabs.get(tabId); |
| 946 | if ( |
| 947 | !current || |
| 948 | current.webContentsId !== wc.id || |
| 949 | webContents.fromId(wc.id) !== wc || |
| 950 | current.audible === audible |
| 951 | ) { |
| 952 | return [Option.none<PreviewTabState>(), tabs] as const; |
| 953 | } |
| 954 | const state: PreviewTabState = { ...current, audible, updatedAt }; |
| 955 | return [ |
| 956 | Option.some(state), |
| 957 | replaceMap(tabs, (copy) => { |
| 958 | copy.set(tabId, state); |
| 959 | }), |
| 960 | ] as const; |
| 961 | }); |
| 962 | if (Option.isSome(next)) yield* emitIfCurrent(tabId, next.value); |
| 963 | }); |
| 964 | |
| 965 | const requireWebContents = Effect.fn("PreviewManager.requireWebContents")(function* ( |
| 966 | tabId: string, |
| 967 | ) { |
| 968 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 969 | const tab = tabs.get(tabId); |
| 970 | if (!tab) { |
| 971 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 972 | } |
| 973 | if (tab.webContentsId == null) { |
| 974 | return yield* new PreviewWebviewNotInitializedError({ tabId }); |
| 975 | } |
| 976 | const wc = webContents.fromId(tab.webContentsId); |
| 977 | if (!wc) { |
| 978 | return yield* new PreviewWebContentsNotFoundError({ |
| 979 | tabId, |
| 980 | webContentsId: tab.webContentsId, |
| 981 | }); |
| 982 | } |
| 983 | return wc; |
| 984 | }); |
| 985 | |
| 986 | const resolveArtifactPath = (artifactPath: string) => |
| 987 | attempt({ operation: "resolveArtifactPath", artifactPath }, () => { |
| 988 | const resolvedPath = path.resolve(artifactPath); |
| 989 | const relativePath = path.relative(resolvedArtifactDirectory, resolvedPath); |
| 990 | if ( |
| 991 | relativePath.length === 0 || |
| 992 | relativePath === ".." || |
| 993 | relativePath.startsWith(`..${path.sep}`) || |
| 994 | path.isAbsolute(relativePath) |
| 995 | ) { |
| 996 | return null; |
| 997 | } |
| 998 | return resolvedPath; |
| 999 | }).pipe( |
| 1000 | Effect.filterOrFail( |
| 1001 | (resolvedPath) => resolvedPath !== null, |
| 1002 | () => |
| 1003 | new PreviewArtifactPathOutsideDirectoryError({ |
| 1004 | artifactPath, |
| 1005 | artifactDirectory: resolvedArtifactDirectory, |
| 1006 | }), |
| 1007 | ), |
| 1008 | ); |
| 1009 | |
| 1010 | const tabIdForWebContents = Effect.fnUntraced(function* (webContentsId: number) { |
| 1011 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 1012 | return ( |
| 1013 | Array.from(tabs.entries()).find(([, tab]) => tab.webContentsId === webContentsId)?.[0] ?? null |
| 1014 | ); |
| 1015 | }); |
| 1016 | |
| 1017 | const detachControlSession = Effect.fn("PreviewManager.detachControlSession")(function* ( |
| 1018 | webContentsId: number, |
| 1019 | closedServerTab?: PreviewTabState["serverTab"], |
| 1020 | ) { |
| 1021 | const control = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) => [ |
| 1022 | sessions.get(webContentsId), |
| 1023 | replaceMap(sessions, (copy) => { |
| 1024 | copy.delete(webContentsId); |
| 1025 | }), |
| 1026 | ]); |
| 1027 | if (control) yield* releaseControlSession(control, closedServerTab); |
| 1028 | }); |
| 1029 | |
| 1030 | // Closes a session already removed from `controlSessionsRef`. The server can |
| 1031 | // only drive a tab while the desktop holds its debugger. |
| 1032 | const releaseControlSession = Effect.fnUntraced(function* ( |
| 1033 | control: BrowserControlSession, |
| 1034 | closedServerTab?: PreviewTabState["serverTab"], |
| 1035 | ) { |
| 1036 | if (closedServerTab) browserHost.detach(closedServerTab); |
| 1037 | for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) { |
| 1038 | if (tab.webContentsId === control.webContentsId && tab.serverTab) { |
| 1039 | browserHost.detach(tab.serverTab); |
| 1040 | } |
| 1041 | } |
| 1042 | yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore); |
| 1043 | }); |
| 1044 | |
| 1045 | const ensureControlSession = Effect.fn("PreviewManager.ensureControlSession")(function* ( |
| 1046 | wc: Electron.WebContents, |
| 1047 | ) { |
| 1048 | return yield* SynchronizedRef.modifyEffect( |
| 1049 | controlSessionsRef, |
| 1050 | ( |
| 1051 | sessions, |
| 1052 | ): Effect.Effect< |
| 1053 | readonly [BrowserControlSession, ReadonlyMap<number, BrowserControlSession>], |
| 1054 | PreviewManagerError |
| 1055 | > => { |
| 1056 | const existing = sessions.get(wc.id); |
| 1057 | if (existing) return Effect.succeed([existing, sessions] as const); |
| 1058 | if (wc.isDestroyed()) { |
| 1059 | return Effect.fail( |
| 1060 | new PreviewOperationError({ |
| 1061 | operation: "ensureControlSession", |
| 1062 | webContentsId: wc.id, |
| 1063 | cause: new Error("WebContents was destroyed"), |
| 1064 | }), |
| 1065 | ); |
| 1066 | } |
| 1067 | if (wc.isDevToolsOpened()) { |
| 1068 | return Effect.fail( |
| 1069 | new PreviewAutomationDevToolsOpenError({ |
| 1070 | webContentsId: wc.id, |
| 1071 | }), |
| 1072 | ); |
| 1073 | } |
| 1074 | if (wc.debugger.isAttached()) { |
| 1075 | return Effect.fail( |
| 1076 | new PreviewAutomationDebuggerAttachedError({ |
| 1077 | webContentsId: wc.id, |
| 1078 | }), |
| 1079 | ); |
| 1080 | } |
| 1081 | const createControlSession = Effect.fn("PreviewManager.createControlSession")(function* () { |
| 1082 | const semaphore = yield* Semaphore.make(1); |
| 1083 | const scope = yield* Scope.fork(parentScope, "sequential"); |
| 1084 | const wcDebugger = wc.debugger; |
| 1085 | const consoleReleases = yield* Queue.sliding<void>(1); |
| 1086 | // Console eviction leaves strong debugger handles; release the group because we keep only text. |
| 1087 | yield* Effect.forkIn( |
| 1088 | Effect.forever( |
| 1089 | Queue.take(consoleReleases).pipe( |
| 1090 | Effect.andThen( |
| 1091 | attemptPromise({ operation: "releaseConsoleObjects", webContentsId: wc.id }, () => |
| 1092 | wcDebugger.sendCommand("Runtime.releaseObjectGroup", { |
| 1093 | objectGroup: "console", |
| 1094 | }), |
| 1095 | ).pipe(Effect.ignore), |
| 1096 | ), |
| 1097 | ), |
| 1098 | ), |
| 1099 | scope, |
| 1100 | ); |
| 1101 | const handleDebuggerMessage = Effect.fnUntraced(function* (method: string) { |
| 1102 | if (method === "Runtime.consoleAPICalled" || method === "Runtime.exceptionThrown") { |
| 1103 | yield* Queue.offer(consoleReleases, undefined); |
| 1104 | } |
| 1105 | }); |
| 1106 | const onMessage: BrowserControlSession["onMessage"] = (_event, method) => { |
| 1107 | runFork(handleDebuggerMessage(method)); |
| 1108 | }; |
| 1109 | const control: BrowserControlSession = { |
| 1110 | webContentsId: wc.id, |
| 1111 | debugger: wcDebugger, |
| 1112 | semaphore, |
| 1113 | scope, |
| 1114 | onMessage, |
| 1115 | }; |
| 1116 | // Electron emits `detach` when Chromium closes the debugger session on its |
| 1117 | // own. The cached session would then fail every command until the webview |
| 1118 | // is replaced. |
| 1119 | let dropped = false; |
| 1120 | const onDetach = () => { |
| 1121 | dropped = true; |
| 1122 | runFork(recoverDetachedControlSession(wc, control)); |
| 1123 | }; |
| 1124 | // The listener comes off before detach() so a detach this manager requested |
| 1125 | // never looks like a dropped one. A dropped session has nothing left to |
| 1126 | // detach: `wc.debugger` is shared, so detaching here would hit a successor. |
| 1127 | yield* Scope.addFinalizer( |
| 1128 | scope, |
| 1129 | attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => { |
| 1130 | wcDebugger.off("message", onMessage); |
| 1131 | wcDebugger.off("detach", onDetach); |
| 1132 | if (!dropped && wcDebugger.isAttached()) wcDebugger.detach(); |
| 1133 | }).pipe(Effect.ignore), |
| 1134 | ); |
| 1135 | const initialize = Effect.fn("PreviewManager.initializeControlSession")(function* () { |
| 1136 | yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => { |
| 1137 | wcDebugger.on("message", onMessage); |
| 1138 | wcDebugger.on("detach", onDetach); |
| 1139 | wcDebugger.attach("1.3"); |
| 1140 | }); |
| 1141 | // Set an opaque base before first paint or background-less dark pages retain white-on-white text. |
| 1142 | yield* attemptPromise( |
| 1143 | { operation: "initializeDebugger.defaultBackground", webContentsId: wc.id }, |
| 1144 | () => |
| 1145 | wcDebugger.sendCommand("Emulation.setDefaultBackgroundColorOverride", { |
| 1146 | color: { r: 255, g: 255, b: 255, a: 1 }, |
| 1147 | }), |
| 1148 | ); |
| 1149 | return [ |
| 1150 | control, |
| 1151 | replaceMap(sessions, (copy) => { |
| 1152 | copy.set(wc.id, control); |
| 1153 | }), |
| 1154 | ] as const; |
| 1155 | }); |
| 1156 | return yield* initialize().pipe( |
| 1157 | Effect.onError(() => Scope.close(scope, Exit.void).pipe(Effect.ignore)), |
| 1158 | ); |
| 1159 | }); |
| 1160 | return createControlSession(); |
| 1161 | }, |
| 1162 | ); |
| 1163 | }); |
| 1164 | |
| 1165 | const cancelPickElement = Effect.fn("PreviewManager.cancelPickElement")(function* ( |
| 1166 | tabId: string, |
| 1167 | ) { |
| 1168 | const session = (yield* Ref.get(pickSessionsRef)).get(tabId); |
| 1169 | if (session) yield* session.cancel; |
| 1170 | }); |
| 1171 | |
| 1172 | const detachListeners = Effect.fn("PreviewManager.detachListeners")(function* ( |
| 1173 | webContentsId: number, |
| 1174 | ) { |
| 1175 | const managed = yield* Ref.modify(attachedRef, (attached) => [ |
| 1176 | attached.get(webContentsId), |
| 1177 | replaceMap(attached, (copy) => { |
| 1178 | copy.delete(webContentsId); |
| 1179 | }), |
| 1180 | ]); |
| 1181 | if (managed) { |
| 1182 | managed.cancelFaviconCapture(); |
| 1183 | yield* Scope.close(managed.scope, Exit.void).pipe(Effect.ignore); |
| 1184 | } |
| 1185 | }); |
| 1186 | |
| 1187 | const computeNavStatus = (wc: Electron.WebContents): PreviewNavStatus => { |
| 1188 | const url = wc.getURL(); |
| 1189 | const title = wc.getTitle(); |
| 1190 | if (url === "" || url === "about:blank") return { kind: "Idle" }; |
| 1191 | // isLoading() includes late iframes whose completion may never clear the tab's loading bar. |
| 1192 | if (wc.isLoadingMainFrame()) return { kind: "Loading", url, title }; |
| 1193 | return { kind: "Success", url, title }; |
| 1194 | }; |
| 1195 | |
| 1196 | const attachListeners = Effect.fn("PreviewManager.attachListeners")(function* ( |
| 1197 | tabId: string, |
| 1198 | wc: Electron.WebContents, |
| 1199 | ) { |
| 1200 | const scope = yield* Scope.fork(parentScope, "sequential"); |
| 1201 | const attachmentId = Symbol(); |
| 1202 | let detachPasskeys = () => {}; |
| 1203 | let documentId = 0; |
| 1204 | let nextRequestId = 0; |
| 1205 | let activeCapture: { |
| 1206 | readonly controller: AbortController; |
| 1207 | readonly documentId: number; |
| 1208 | readonly eventKey: string; |
| 1209 | readonly requestId: number; |
| 1210 | } | null = null; |
| 1211 | const cancelFaviconCapture = () => { |
| 1212 | documentId += 1; |
| 1213 | activeCapture?.controller.abort(); |
| 1214 | activeCapture = null; |
| 1215 | }; |
| 1216 | const syncState = Effect.fn("PreviewManager.syncWebContentsState")(function* ( |
| 1217 | preserveLoadFailure: boolean, |
| 1218 | confirmedNavigation = false, |
| 1219 | ) { |
| 1220 | if (wc.isDestroyed()) return; |
| 1221 | const computedNavStatus = computeNavStatus(wc); |
| 1222 | const canGoBack = wc.navigationHistory.canGoBack(); |
| 1223 | const canGoForward = wc.navigationHistory.canGoForward(); |
| 1224 | const updatedAt = yield* currentIso; |
| 1225 | const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 1226 | const current = tabs.get(tabId); |
| 1227 | if (!current || current.webContentsId !== wc.id || webContents.fromId(wc.id) !== wc) { |
| 1228 | return [Option.none<PreviewTabState>(), tabs] as const; |
| 1229 | } |
| 1230 | // did-stop-loading follows did-fail-load without proving successful navigation. |
| 1231 | const navStatus = |
| 1232 | preserveLoadFailure && |
| 1233 | current.navStatus.kind === "LoadFailed" && |
| 1234 | computedNavStatus.kind === "Success" |
| 1235 | ? current.navStatus |
| 1236 | : computedNavStatus; |
| 1237 | const clearFavicon = |
| 1238 | confirmedNavigation && |
| 1239 | current.favicon !== undefined && |
| 1240 | safeHttpOrigin(current.favicon.pageUrl) !== |
| 1241 | safeHttpOrigin(navStatus.kind === "Idle" ? wc.getURL() : navStatus.url); |
| 1242 | const { favicon: _favicon, ...currentWithoutFavicon } = current; |
| 1243 | const state: PreviewTabState = { |
| 1244 | ...(clearFavicon ? currentWithoutFavicon : current), |
| 1245 | navStatus, |
| 1246 | canGoBack, |
| 1247 | canGoForward, |
| 1248 | // Guest zoom inherits app zoom; mirroring it would overwrite the tab's own setting. |
| 1249 | updatedAt, |
| 1250 | }; |
| 1251 | return [ |
| 1252 | Option.some(state), |
| 1253 | replaceMap(tabs, (copy) => { |
| 1254 | copy.set(tabId, state); |
| 1255 | }), |
| 1256 | ] as const; |
| 1257 | }); |
| 1258 | if (Option.isSome(next)) yield* emitIfCurrent(tabId, next.value); |
| 1259 | }); |
| 1260 | const sync = () => runFork(syncState(true)); |
| 1261 | const syncNavigation = () => runFork(syncState(false, true)); |
| 1262 | const syncInPageNavigation = () => runFork(syncState(false)); |
| 1263 | const restoreRecordingCursor = () => |
| 1264 | runFork( |
| 1265 | Effect.gen(function* () { |
| 1266 | const session = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); |
| 1267 | if (!wc.isDestroyed()) { |
| 1268 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 1269 | wc.send( |
| 1270 | RECORDING_CURSOR_CHANNEL, |
| 1271 | session?.consumers.has("recording") ?? false, |
| 1272 | session?.recordingInputOptions, |
| 1273 | tab?.controller, |
| 1274 | ); |
| 1275 | } |
| 1276 | }), |
| 1277 | ); |
| 1278 | const navigationStarted = ( |
| 1279 | event: Electron.Event<Electron.WebContentsDidStartNavigationEventParams>, |
| 1280 | ) => { |
| 1281 | if (event.isMainFrame && !event.isSameDocument) cancelFaviconCapture(); |
| 1282 | }; |
| 1283 | const audioStateChanged = ( |
| 1284 | event: Electron.Event<Electron.WebContentsAudioStateChangedEventParams>, |
| 1285 | ) => runFork(syncTabAudible(tabId, wc, event.audible)); |
| 1286 | const publishFavicon = Effect.fn("PreviewManager.publishFavicon")(function* (input: { |
| 1287 | readonly captureDocumentId: number; |
| 1288 | readonly dataUrl: string; |
| 1289 | readonly pageUrl: string; |
| 1290 | readonly requestId: number; |
| 1291 | }) { |
| 1292 | const pageOrigin = safeHttpOrigin(input.pageUrl); |
| 1293 | const managed = (yield* Ref.get(attachedRef)).get(wc.id); |
| 1294 | if ( |
| 1295 | !pageOrigin || |
| 1296 | wc.isDestroyed() || |
| 1297 | webContents.fromId(wc.id) !== wc || |
| 1298 | managed?.attachmentId !== attachmentId || |
| 1299 | activeCapture?.documentId !== input.captureDocumentId || |
| 1300 | activeCapture.requestId !== input.requestId || |
| 1301 | safeHttpOrigin(wc.getURL()) !== pageOrigin |
| 1302 | ) { |
| 1303 | return; |
| 1304 | } |
| 1305 | const capturedAt = yield* currentMillis; |
| 1306 | const updatedAt = yield* currentIso; |
| 1307 | const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 1308 | const current = tabs.get(tabId); |
| 1309 | if ( |
| 1310 | !current || |
| 1311 | current.webContentsId !== wc.id || |
| 1312 | webContents.fromId(wc.id) !== wc || |
| 1313 | activeCapture?.documentId !== input.captureDocumentId || |
| 1314 | activeCapture.requestId !== input.requestId |
| 1315 | ) { |
| 1316 | return [Option.none<PreviewTabState>(), tabs] as const; |
| 1317 | } |
| 1318 | const state: PreviewTabState = { |
| 1319 | ...current, |
| 1320 | favicon: { dataUrl: input.dataUrl, pageUrl: pageOrigin, capturedAt }, |
| 1321 | updatedAt, |
| 1322 | }; |
| 1323 | return [ |
| 1324 | Option.some(state), |
| 1325 | replaceMap(tabs, (copy) => { |
| 1326 | copy.set(tabId, state); |
| 1327 | }), |
| 1328 | ] as const; |
| 1329 | }); |
| 1330 | if (Option.isSome(next)) yield* emitIfCurrent(tabId, next.value); |
| 1331 | }); |
| 1332 | const faviconUpdated = (_event: Event, rawCandidates: ReadonlyArray<string>): void => { |
| 1333 | const pageUrl = wc.getURL(); |
| 1334 | if (!safeHttpOrigin(pageUrl)) return; |
| 1335 | const candidates = selectFaviconCandidates(rawCandidates); |
| 1336 | if (candidates.length === 0) return; |
| 1337 | const eventKey = JSON.stringify([pageUrl, ...candidates]); |
| 1338 | if (activeCapture?.eventKey === eventKey) return; |
| 1339 | activeCapture?.controller.abort(); |
| 1340 | const captureDocumentId = documentId; |
| 1341 | const requestId = ++nextRequestId; |
| 1342 | const controller = new AbortController(); |
| 1343 | activeCapture = { controller, documentId: captureDocumentId, eventKey, requestId }; |
| 1344 | runFork( |
| 1345 | Effect.tryPromise({ |
| 1346 | try: () => |
| 1347 | captureFavicon({ webContents: wc, pageUrl, candidates, signal: controller.signal }), |
| 1348 | catch: (cause) => |
| 1349 | new PreviewOperationError({ |
| 1350 | operation: "captureFavicon", |
| 1351 | tabId, |
| 1352 | webContentsId: wc.id, |
| 1353 | cause, |
| 1354 | }), |
| 1355 | }).pipe( |
| 1356 | Effect.flatMap((result) => |
| 1357 | result.kind === "captured" |
| 1358 | ? publishFavicon({ |
| 1359 | captureDocumentId, |
| 1360 | dataUrl: result.dataUrl, |
| 1361 | pageUrl, |
| 1362 | requestId, |
| 1363 | }) |
| 1364 | : Effect.void, |
| 1365 | ), |
| 1366 | Effect.catch((error) => |
| 1367 | controller.signal.aborted |
| 1368 | ? Effect.void |
| 1369 | : Effect.logDebug("Favicon capture failed.", { error, tabId, webContentsId: wc.id }), |
| 1370 | ), |
| 1371 | Effect.ensuring( |
| 1372 | Effect.sync(() => { |
| 1373 | if (activeCapture?.requestId === requestId) activeCapture = null; |
| 1374 | }), |
| 1375 | ), |
| 1376 | ), |
| 1377 | ); |
| 1378 | }; |
| 1379 | const failed = ( |
| 1380 | _event: Event, |
| 1381 | code: number, |
| 1382 | description: string, |
| 1383 | validatedUrl: string, |
| 1384 | isMainFrame: boolean, |
| 1385 | ): void => { |
| 1386 | if (code === -3 || !isMainFrame || wc.isDestroyed()) return; |
| 1387 | const url = validatedUrl || wc.getURL(); |
| 1388 | runFork( |
| 1389 | Effect.gen(function* () { |
| 1390 | const updatedAt = yield* currentIso; |
| 1391 | const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 1392 | const current = tabs.get(tabId); |
| 1393 | if ( |
| 1394 | !current || |
| 1395 | current.webContentsId !== wc.id || |
| 1396 | wc.isDestroyed() || |
| 1397 | webContents.fromId(wc.id) !== wc |
| 1398 | ) { |
| 1399 | return [Option.none<PreviewTabState>(), tabs] as const; |
| 1400 | } |
| 1401 | const state: PreviewTabState = { |
| 1402 | ...current, |
| 1403 | navStatus: { kind: "LoadFailed", url, title: wc.getTitle(), code, description }, |
| 1404 | updatedAt, |
| 1405 | }; |
| 1406 | return [ |
| 1407 | Option.some(state), |
| 1408 | replaceMap(tabs, (copy) => copy.set(tabId, state)), |
| 1409 | ] as const; |
| 1410 | }); |
| 1411 | if (Option.isNone(next)) return; |
| 1412 | const current = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 1413 | if (current?.webContentsId !== wc.id || current.navStatus !== next.value.navStatus) |
| 1414 | return; |
| 1415 | if (current.serverTab) { |
| 1416 | browserHost.loadFailed(current.serverTab, { url, code, description }); |
| 1417 | } |
| 1418 | yield* emitIfCurrent(tabId, next.value); |
| 1419 | }), |
| 1420 | ); |
| 1421 | }; |
| 1422 | let humanInputWorker = false; |
| 1423 | let humanInputDeadline = 0; |
| 1424 | let listening = true; |
| 1425 | const publishHumanController = Effect.fn("PreviewManager.publishHumanController")(function* ( |
| 1426 | controller: "human" | "none", |
| 1427 | humanPoint?: { readonly x: number; readonly y: number }, |
| 1428 | ) { |
| 1429 | const now = yield* currentMillis; |
| 1430 | const updatedAt = yield* currentIso; |
| 1431 | const result = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 1432 | const current = tabs.get(tabId); |
| 1433 | if ( |
| 1434 | !current || |
| 1435 | current.webContentsId !== wc.id || |
| 1436 | (controller === "human" |
| 1437 | ? !listening || webContents.fromId(wc.id) !== wc |
| 1438 | : current.controller !== "human" || humanInputDeadline > now) |
| 1439 | ) |
| 1440 | return [undefined, tabs] as const; |
| 1441 | const changed = current.controller !== controller; |
| 1442 | const state = changed ? { ...current, controller, updatedAt } : current; |
| 1443 | return [ |
| 1444 | { state, changed }, |
| 1445 | changed |
| 1446 | ? replaceMap(tabs, (copy) => { |
| 1447 | copy.set(tabId, state); |
| 1448 | }) |
| 1449 | : tabs, |
| 1450 | ] as const; |
| 1451 | }); |
| 1452 | if (!result) return; |
| 1453 | const capture = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); |
| 1454 | if (capture?.consumers.has("recording")) { |
| 1455 | yield* attempt({ operation: "recording.controller", tabId }, () => { |
| 1456 | if (!wc.isDestroyed()) wc.send(RECORDING_CONTROLLER_CHANNEL, controller, humanPoint); |
| 1457 | }).pipe(Effect.ignore); |
| 1458 | } |
| 1459 | if (result.changed) yield* emitIfCurrent(tabId, result.state); |
| 1460 | }); |
| 1461 | yield* Scope.addFinalizer( |
| 1462 | scope, |
| 1463 | Effect.gen(function* () { |
| 1464 | listening = false; |
| 1465 | humanInputDeadline = 0; |
| 1466 | yield* publishHumanController("none"); |
| 1467 | }), |
| 1468 | ); |
| 1469 | const handleHumanInput = Effect.fn("PreviewManager.handleHumanInput")(function* ( |
| 1470 | rawSignal?: unknown, |
| 1471 | ) { |
| 1472 | if (!listening) return; |
| 1473 | humanInputDeadline = (yield* currentMillis) + 750; |
| 1474 | if (!humanInputWorker) { |
| 1475 | humanInputWorker = true; |
| 1476 | yield* Effect.gen(function* () { |
| 1477 | let remaining: number; |
| 1478 | while ((remaining = humanInputDeadline - (yield* currentMillis)) > 0) { |
| 1479 | yield* Effect.sleep(remaining); |
| 1480 | } |
| 1481 | humanInputWorker = false; |
| 1482 | yield* publishHumanController("none"); |
| 1483 | }).pipe(Effect.forkIn(scope)); |
| 1484 | } |
| 1485 | yield* publishHumanController( |
| 1486 | "human", |
| 1487 | isPreviewInputSignal(rawSignal) && rawSignal.kind === "pointer" |
| 1488 | ? { x: rawSignal.x, y: rawSignal.y } |
| 1489 | : undefined, |
| 1490 | ); |
| 1491 | }); |
| 1492 | const recordingInput = (_event: unknown, input: unknown) => { |
| 1493 | if (!isRecordingInput(input)) return; |
| 1494 | return runFork( |
| 1495 | Effect.gen(function* () { |
| 1496 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 1497 | const capture = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); |
| 1498 | if (tab?.webContentsId !== wc.id || !capture?.consumers.has("recording")) return; |
| 1499 | if (input.type === "key" && !capture.recordingInputOptions?.showKeyPresses) return; |
| 1500 | if (input.type === "pointer" && !capture.recordingInputOptions?.showMousePresses) return; |
| 1501 | const listeners = yield* Ref.get(recordingInputListenersRef); |
| 1502 | yield* Effect.forEach( |
| 1503 | listeners, |
| 1504 | (listener) => deliverEvent("recording-input", tabId, () => listener({ tabId, input })), |
| 1505 | { discard: true }, |
| 1506 | ); |
| 1507 | }), |
| 1508 | ); |
| 1509 | }; |
| 1510 | // Chromium splits one search across several updates (the ordinal and the |
| 1511 | // final count can land separately), so every update is forwarded. |
| 1512 | const foundInPage = (_event: unknown, result: Electron.Result): void => { |
| 1513 | runFork( |
| 1514 | emitFindResult({ |
| 1515 | tabId, |
| 1516 | activeMatchOrdinal: result.activeMatchOrdinal, |
| 1517 | matches: result.matches, |
| 1518 | }), |
| 1519 | ); |
| 1520 | }; |
| 1521 | const humanInput = (_event: unknown, rawSignal?: unknown): void => { |
| 1522 | runFork(handleHumanInput(rawSignal)); |
| 1523 | }; |
| 1524 | // Chromium navigates the outermost WebContents (the host app) on thumb buttons, even from guest iframes. |
| 1525 | const beforeMouse = (event: Electron.Event, mouse: Electron.MouseInputEvent): void => { |
| 1526 | // Electron reports thumb buttons as "back" and "forward", which its typings omit. |
| 1527 | const button: string | undefined = mouse.button; |
| 1528 | if (button !== "back" && button !== "forward") return; |
| 1529 | if (mouse.type !== "mouseDown" && mouse.type !== "mouseUp") return; |
| 1530 | event.preventDefault(); |
| 1531 | // The page never sees the press, so the guest replays it; these coordinates are DIPs, not CSS px. |
| 1532 | const zoom = wc.getZoomFactor(); |
| 1533 | const phase = mouse.type === "mouseDown" ? "down" : "up"; |
| 1534 | wc.send( |
| 1535 | THUMB_PRESS_CHANNEL, |
| 1536 | phase, |
| 1537 | mouse.x / zoom, |
| 1538 | mouse.y / zoom, |
| 1539 | button === "back" ? 3 : 4, |
| 1540 | ); |
| 1541 | if (phase !== "up") return; |
| 1542 | runFork( |
| 1543 | attempt({ operation: "mouseNavigate", tabId, webContentsId: wc.id }, () => { |
| 1544 | if (button === "back") { |
| 1545 | if (wc.navigationHistory.canGoBack()) wc.navigationHistory.goBack(); |
| 1546 | } else if (wc.navigationHistory.canGoForward()) { |
| 1547 | wc.navigationHistory.goForward(); |
| 1548 | } |
| 1549 | }).pipe(Effect.ignore), |
| 1550 | ); |
| 1551 | }; |
| 1552 | // Every guest of a focused window reports focus; GuestInput handles guests without a focused frame. |
| 1553 | const holdsKeyboardFocus = (contents: Electron.WebContents) => |
| 1554 | contents.isFocused() && contents.focusedFrame !== null; |
| 1555 | const syncMenuShortcuts = (contents: Electron.WebContents, input: Electron.Input): void => { |
| 1556 | if (input.type !== "keyDown") return; |
| 1557 | contents.setIgnoreMenuShortcuts( |
| 1558 | !isPreviewEditingShortcut(input, hostPlatform) || !holdsKeyboardFocus(contents), |
| 1559 | ); |
| 1560 | }; |
| 1561 | // Popups inherit Electron's unrestricted window handler; block nested popups to bound the chain. |
| 1562 | const windowCreated = (window: Electron.BrowserWindow): void => { |
| 1563 | window.webContents.setIgnoreMenuShortcuts(true); |
| 1564 | window.webContents.setWindowOpenHandler(() => ({ action: "deny" })); |
| 1565 | window.webContents.on("before-input-event", (_event, input) => { |
| 1566 | syncMenuShortcuts(window.webContents, input); |
| 1567 | }); |
| 1568 | }; |
| 1569 | const findForwardedShortcut = (keys: Parameters<typeof matchesKeybindingShortcut>[0]) => |
| 1570 | !( |
| 1571 | hostPlatform !== "darwin" && |
| 1572 | keys.ctrlKey && |
| 1573 | keys.altKey && |
| 1574 | /^(?:[^a-zA-Z0-9]|Dead)$/u.test(keys.key) |
| 1575 | ) && |
| 1576 | forwardedShortcuts.find(({ shortcut }) => |
| 1577 | matchesKeybindingShortcut( |
| 1578 | keys, |
| 1579 | shortcut, |
| 1580 | hostPlatform === "darwin" ? "MacIntel" : hostPlatform, |
| 1581 | ), |
| 1582 | ); |
| 1583 | const beforeInput = (event: Electron.Event, input: Electron.Input): void => { |
| 1584 | syncMenuShortcuts(wc, input); |
| 1585 | const host = wc.hostWebContents; |
| 1586 | const forwarded = |
| 1587 | input.type === "keyDown" && |
| 1588 | !input.isComposing && |
| 1589 | host && |
| 1590 | !host.isDestroyed() && |
| 1591 | holdsKeyboardFocus(wc) && |
| 1592 | findForwardedShortcut({ |
| 1593 | key: input.key, |
| 1594 | code: input.code, |
| 1595 | metaKey: input.meta, |
| 1596 | ctrlKey: input.control, |
| 1597 | shiftKey: input.shift, |
| 1598 | altKey: input.alt, |
| 1599 | }); |
| 1600 | if (forwarded && host) { |
| 1601 | // The page keeps these keys. Only the main-frame preload can report one it |
| 1602 | // left unhandled, so a focused iframe (e.g. Google Docs' input) keeps its own find or zoom. |
| 1603 | if (PAGE_FIRST_FORWARDED_COMMANDS.has(forwarded.command)) return; |
| 1604 | event.preventDefault(); |
| 1605 | if (!input.isAutoRepeat) { |
| 1606 | if ( |
| 1607 | forwarded.command === "workspace.focusNext" || |
| 1608 | forwarded.command === "workspace.focusPrevious" || |
| 1609 | forwarded.command === "preview.focusUrl" |
| 1610 | ) { |
| 1611 | // DOM focus in the renderer cannot by itself release the guest's native keyboard focus. |
| 1612 | host.focus(); |
| 1613 | } |
| 1614 | host.send(MENU_ACTION_CHANNEL, forwarded.command); |
| 1615 | } |
| 1616 | return; |
| 1617 | } |
| 1618 | if (isPreviewRefreshShortcut(input, hostPlatform)) { |
| 1619 | event.preventDefault(); |
| 1620 | runFork( |
| 1621 | attempt({ operation: "shortcut.refresh", tabId, webContentsId: wc.id }, () => |
| 1622 | wc.reload(), |
| 1623 | ).pipe(Effect.ignore), |
| 1624 | ); |
| 1625 | return; |
| 1626 | } |
| 1627 | }; |
| 1628 | const pageUnhandledShortcut = (_event: unknown, rawKeys?: unknown): void => { |
| 1629 | const host = wc.hostWebContents; |
| 1630 | if (!host || host.isDestroyed() || !holdsKeyboardFocus(wc)) return; |
| 1631 | const keys = decodePageShortcutKeys(rawKeys); |
| 1632 | if (Option.isNone(keys)) return; |
| 1633 | const forwarded = findForwardedShortcut(keys.value); |
| 1634 | if (!forwarded || !PAGE_FIRST_FORWARDED_COMMANDS.has(forwarded.command)) return; |
| 1635 | host.send(MENU_ACTION_CHANNEL, forwarded.command); |
| 1636 | }; |
| 1637 | yield* Scope.addFinalizer( |
| 1638 | scope, |
| 1639 | attempt({ operation: "detachListeners", tabId, webContentsId: wc.id }, () => { |
| 1640 | cancelFaviconCapture(); |
| 1641 | wc.off("did-start-navigation", navigationStarted); |
| 1642 | wc.off("did-navigate", syncNavigation); |
| 1643 | wc.off("did-navigate-in-page", syncInPageNavigation); |
| 1644 | wc.off("page-title-updated", sync); |
| 1645 | wc.off("page-favicon-updated", faviconUpdated as never); |
| 1646 | wc.off("did-start-loading", sync); |
| 1647 | wc.off("did-stop-loading", sync); |
| 1648 | wc.off("dom-ready", restoreRecordingCursor); |
| 1649 | wc.off("did-fail-load", failed as never); |
| 1650 | wc.off("audio-state-changed", audioStateChanged); |
| 1651 | wc.off("found-in-page", foundInPage as never); |
| 1652 | wc.off("did-create-window", windowCreated); |
| 1653 | wc.off("before-input-event", beforeInput); |
| 1654 | wc.off("before-mouse-event", beforeMouse); |
| 1655 | wc.ipc.off(HUMAN_INPUT_CHANNEL, humanInput); |
| 1656 | wc.ipc.off(RECORDING_INPUT_CHANNEL, recordingInput); |
| 1657 | wc.ipc.off(PAGE_UNHANDLED_SHORTCUT_CHANNEL, pageUnhandledShortcut); |
| 1658 | detachPasskeys(); |
| 1659 | }).pipe(Effect.ignore), |
| 1660 | ); |
| 1661 | const install = Effect.fn("PreviewManager.installWebContentsListeners")(function* () { |
| 1662 | yield* attempt({ operation: "attachListeners", tabId, webContentsId: wc.id }, () => { |
| 1663 | wc.setIgnoreMenuShortcuts(true); |
| 1664 | wc.on("did-start-navigation", navigationStarted); |
| 1665 | wc.on("did-navigate", syncNavigation); |
| 1666 | wc.on("did-navigate-in-page", syncInPageNavigation); |
| 1667 | wc.on("page-title-updated", sync); |
| 1668 | wc.on("page-favicon-updated", faviconUpdated as never); |
| 1669 | wc.on("did-start-loading", sync); |
| 1670 | wc.on("did-stop-loading", sync); |
| 1671 | wc.on("dom-ready", restoreRecordingCursor); |
| 1672 | wc.on("did-fail-load", failed as never); |
| 1673 | wc.on("audio-state-changed", audioStateChanged); |
| 1674 | wc.on("found-in-page", foundInPage as never); |
| 1675 | wc.ipc.on(HUMAN_INPUT_CHANNEL, humanInput); |
| 1676 | wc.ipc.on(RECORDING_INPUT_CHANNEL, recordingInput); |
| 1677 | wc.ipc.on(PAGE_UNHANDLED_SHORTCUT_CHANNEL, pageUnhandledShortcut); |
| 1678 | detachPasskeys = passkeys.attachGuest(wc); |
| 1679 | wc.setWindowOpenHandler((details) => { |
| 1680 | const action = previewWindowOpenAction(details); |
| 1681 | if (action === "popup") { |
| 1682 | return { action: "allow", overrideBrowserWindowOptions: POPUP_WINDOW_OPTIONS }; |
| 1683 | } |
| 1684 | if (action === "deny") return { action: "deny" }; |
| 1685 | if (action === "new-tab") { |
| 1686 | runFork( |
| 1687 | emitOpenLink({ |
| 1688 | tabId, |
| 1689 | url: details.url, |
| 1690 | // A person's own click or key leaves the guest a focused frame; GuestInput hands |
| 1691 | // focus back after the agent's press, so a foreground tab would take it again. |
| 1692 | background: |
| 1693 | details.disposition === "background-tab" || |
| 1694 | (wc.focusedFrame === null && browserHost.agentLedPage(wc)), |
| 1695 | }), |
| 1696 | ); |
| 1697 | return { action: "deny" }; |
| 1698 | } |
| 1699 | runFork( |
| 1700 | attemptPromise({ operation: "openPreviewWindow", tabId, webContentsId: wc.id }, () => |
| 1701 | wc.loadURL(details.url), |
| 1702 | ).pipe(Effect.ignore), |
| 1703 | ); |
| 1704 | return { action: "deny" }; |
| 1705 | }); |
| 1706 | wc.on("did-create-window", windowCreated); |
| 1707 | wc.on("before-input-event", beforeInput); |
| 1708 | wc.on("before-mouse-event", beforeMouse); |
| 1709 | }); |
| 1710 | yield* Ref.update(attachedRef, (attached) => |
| 1711 | replaceMap(attached, (copy) => { |
| 1712 | copy.set(wc.id, { attachmentId, cancelFaviconCapture, scope, webContents: wc }); |
| 1713 | }), |
| 1714 | ); |
| 1715 | }); |
| 1716 | yield* install().pipe(Effect.onError(() => Scope.close(scope, Exit.void).pipe(Effect.ignore))); |
| 1717 | }); |
| 1718 | |
| 1719 | const setMainWindow = Effect.fn("PreviewManager.setMainWindow")(function* ( |
| 1720 | window: BrowserWindow, |
| 1721 | ) { |
| 1722 | if (mainWindowCleanupFiber) { |
| 1723 | yield* Fiber.join(mainWindowCleanupFiber); |
| 1724 | mainWindowCleanupFiber = undefined; |
| 1725 | } |
| 1726 | yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) => |
| 1727 | Effect.gen(function* () { |
| 1728 | // Cleanup or lock acquisition can outlive the replacement window. |
| 1729 | if (window.isDestroyed()) return [undefined, sessions] as const; |
| 1730 | if (sessions.size > 0) { |
| 1731 | yield* setWindowBackgroundThrottling(window, false); |
| 1732 | } |
| 1733 | yield* Ref.set(mainWindowRef, Option.some(window)); |
| 1734 | currentMainWindow = window; |
| 1735 | frameCaptureWindowOpen = true; |
| 1736 | window.once("closed", () => { |
| 1737 | if (currentMainWindow !== window) return; |
| 1738 | currentMainWindow = undefined; |
| 1739 | frameCaptureWindowOpen = false; |
| 1740 | mainWindowCleanupFiber = runFork( |
| 1741 | Effect.all([closeAllPictureInPicture(), stopAllRecordings()], { |
| 1742 | concurrency: "unbounded", |
| 1743 | discard: true, |
| 1744 | }).pipe(Effect.ignore), |
| 1745 | ); |
| 1746 | }); |
| 1747 | return [undefined, sessions] as const; |
| 1748 | }), |
| 1749 | ).pipe(Effect.uninterruptible); |
| 1750 | }); |
| 1751 | |
| 1752 | const createTabUnlocked = Effect.fn("PreviewManager.createTabUnlocked")(function* ( |
| 1753 | tabId: string, |
| 1754 | defaults?: DesktopPreviewTabDefaults, |
| 1755 | ) { |
| 1756 | const updatedAt = yield* currentIso; |
| 1757 | const result = yield* SynchronizedRef.modify( |
| 1758 | tabsRef, |
| 1759 | ( |
| 1760 | tabs, |
| 1761 | ): readonly [ |
| 1762 | { readonly state: PreviewTabState; readonly created: boolean }, |
| 1763 | ReadonlyMap<string, PreviewTabState>, |
| 1764 | ] => { |
| 1765 | const existing = tabs.get(tabId); |
| 1766 | if (existing) return [{ state: existing, created: false }, tabs] as const; |
| 1767 | const initial: PreviewTabState = { |
| 1768 | tabId, |
| 1769 | webContentsId: null, |
| 1770 | navStatus: { kind: "Idle" }, |
| 1771 | canGoBack: false, |
| 1772 | canGoForward: false, |
| 1773 | zoomFactor: normalizeZoomFactor(defaults?.zoomFactor), |
| 1774 | pictureInPicture: false, |
| 1775 | colorScheme: defaults?.colorScheme ?? "system", |
| 1776 | ...(defaults?.serverTab === undefined ? {} : { serverTab: defaults.serverTab }), |
| 1777 | audioMuted: false, |
| 1778 | audible: false, |
| 1779 | controller: "none", |
| 1780 | capturing: false, |
| 1781 | updatedAt, |
| 1782 | }; |
| 1783 | return [ |
| 1784 | { state: initial, created: true }, |
| 1785 | replaceMap(tabs, (copy) => { |
| 1786 | copy.set(tabId, initial); |
| 1787 | }), |
| 1788 | ] as const; |
| 1789 | }, |
| 1790 | ); |
| 1791 | if (result.created) { |
| 1792 | tabLifecycleGenerations.set(tabId, (tabLifecycleGenerations.get(tabId) ?? 0) + 1); |
| 1793 | } |
| 1794 | yield* emit(tabId, result.state); |
| 1795 | return result.state; |
| 1796 | }); |
| 1797 | |
| 1798 | const createTab = Effect.fn("PreviewManager.createTab")(function* ( |
| 1799 | tabId: string, |
| 1800 | defaults?: DesktopPreviewTabDefaults, |
| 1801 | ) { |
| 1802 | return yield* withTabLifecycleLock(tabId, createTabUnlocked(tabId, defaults)); |
| 1803 | }); |
| 1804 | |
| 1805 | const closeTabUnlocked = Effect.fn("PreviewManager.closeTabUnlocked")(function* (tabId: string) { |
| 1806 | if (!(yield* SynchronizedRef.get(tabsRef)).has(tabId)) return; |
| 1807 | clearPendingRecording(tabId); |
| 1808 | annotationSendEnabled.delete(tabId); |
| 1809 | yield* Effect.all( |
| 1810 | [ |
| 1811 | cancelPickElement(tabId), |
| 1812 | closePictureInPicture(tabId), |
| 1813 | stopFrameCapture(tabId, "recording"), |
| 1814 | ], |
| 1815 | { |
| 1816 | concurrency: 3, |
| 1817 | discard: true, |
| 1818 | }, |
| 1819 | ); |
| 1820 | const tab = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 1821 | const current = tabs.get(tabId); |
| 1822 | if (!current) return [Option.none<PreviewTabState>(), tabs] as const; |
| 1823 | return [ |
| 1824 | Option.some(current), |
| 1825 | replaceMap(tabs, (copy) => { |
| 1826 | copy.delete(tabId); |
| 1827 | }), |
| 1828 | ] as const; |
| 1829 | }); |
| 1830 | if (Option.isNone(tab)) return; |
| 1831 | const closedTab = tab.value; |
| 1832 | if (closedTab.serverTab) { |
| 1833 | browserHost.setShown(closedTab.serverTab, undefined); |
| 1834 | browserHost.setCaptureDrawn(closedTab.serverTab, undefined); |
| 1835 | } |
| 1836 | if (closedTab.webContentsId != null) { |
| 1837 | yield* Effect.all( |
| 1838 | [ |
| 1839 | detachControlSession(closedTab.webContentsId, closedTab.serverTab), |
| 1840 | detachListeners(closedTab.webContentsId), |
| 1841 | ], |
| 1842 | { concurrency: 2, discard: true }, |
| 1843 | ); |
| 1844 | } |
| 1845 | const updatedAt = yield* currentIso; |
| 1846 | const closed: PreviewTabState = { |
| 1847 | ...closedTab, |
| 1848 | webContentsId: null, |
| 1849 | navStatus: { kind: "Idle" }, |
| 1850 | canGoBack: false, |
| 1851 | canGoForward: false, |
| 1852 | zoomFactor: DEFAULT_ZOOM_FACTOR, |
| 1853 | pictureInPicture: false, |
| 1854 | colorScheme: "system", |
| 1855 | audioMuted: false, |
| 1856 | audible: false, |
| 1857 | controller: "none", |
| 1858 | capturing: false, |
| 1859 | updatedAt, |
| 1860 | }; |
| 1861 | yield* emit(tabId, closed); |
| 1862 | }); |
| 1863 | |
| 1864 | const closeTab = Effect.fn("PreviewManager.closeTab")(function* (tabId: string) { |
| 1865 | const claimed = yield* Ref.modify(closingTabIdsRef, (closingTabIds) => { |
| 1866 | if (closingTabIds.has(tabId)) return [false, closingTabIds] as const; |
| 1867 | return [true, new Set([...closingTabIds, tabId])] as const; |
| 1868 | }); |
| 1869 | if (!claimed) return; |
| 1870 | return yield* withTabLifecycleLock(tabId, closeTabUnlocked(tabId)).pipe( |
| 1871 | Effect.ensuring( |
| 1872 | Ref.update(closingTabIdsRef, (closingTabIds) => { |
| 1873 | if (!closingTabIds.has(tabId)) return closingTabIds; |
| 1874 | const next = new Set(closingTabIds); |
| 1875 | next.delete(tabId); |
| 1876 | return next; |
| 1877 | }), |
| 1878 | ), |
| 1879 | ); |
| 1880 | }); |
| 1881 | |
| 1882 | const registerWebviewUnlocked = Effect.fn("PreviewManager.registerWebviewUnlocked")(function* ( |
| 1883 | tabId: string, |
| 1884 | webContentsId: number, |
| 1885 | expectedGeneration: number | undefined, |
| 1886 | ) { |
| 1887 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 1888 | if ( |
| 1889 | !tab || |
| 1890 | tabLifecycleGenerations.get(tabId) !== expectedGeneration || |
| 1891 | (yield* Ref.get(closingTabIdsRef)).has(tabId) |
| 1892 | ) { |
| 1893 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 1894 | } |
| 1895 | const wc = webContents.fromId(webContentsId); |
| 1896 | const mainWindow = yield* Ref.get(mainWindowRef); |
| 1897 | if ( |
| 1898 | !wc || |
| 1899 | wc.isDestroyed() || |
| 1900 | wc.getType() !== "webview" || |
| 1901 | (Option.isSome(mainWindow) && wc.hostWebContents !== mainWindow.value.webContents) |
| 1902 | ) { |
| 1903 | return yield* new PreviewWebContentsNotFoundError({ tabId, webContentsId }); |
| 1904 | } |
| 1905 | yield* rendererHistory.register(wc, { surface: "preview", tabId }); |
| 1906 | const attached = yield* Ref.get(attachedRef); |
| 1907 | const annotationTheme = yield* Ref.get(annotationThemeRef); |
| 1908 | const currentAttachment = attached.get(webContentsId); |
| 1909 | yield* keepFrameCaptureWebContentsUnthrottled(tabId, wc); |
| 1910 | if (tab.webContentsId === webContentsId && currentAttachment?.webContents === wc) { |
| 1911 | yield* assertTabZoom(tabId); |
| 1912 | yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () => { |
| 1913 | wc.send(ANNOTATION_THEME_CHANNEL, annotationTheme); |
| 1914 | wc.send(ANNOTATION_SEND_ENABLED_CHANNEL, annotationSendEnabled.get(tabId) === true); |
| 1915 | }); |
| 1916 | return; |
| 1917 | } |
| 1918 | const replacedWebContentsId = |
| 1919 | tab.webContentsId != null && |
| 1920 | (tab.webContentsId !== webContentsId || currentAttachment?.webContents !== wc) |
| 1921 | ? tab.webContentsId |
| 1922 | : null; |
| 1923 | if (replacedWebContentsId !== null) { |
| 1924 | clearPendingRecording(tabId); |
| 1925 | yield* Effect.all( |
| 1926 | [ |
| 1927 | detachControlSession(replacedWebContentsId), |
| 1928 | detachListeners(replacedWebContentsId), |
| 1929 | cancelPickElement(tabId), |
| 1930 | ], |
| 1931 | { concurrency: 3, discard: true }, |
| 1932 | ); |
| 1933 | } |
| 1934 | const currentTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 1935 | if ( |
| 1936 | !currentTab || |
| 1937 | tabLifecycleGenerations.get(tabId) !== expectedGeneration || |
| 1938 | (yield* Ref.get(closingTabIdsRef)).has(tabId) |
| 1939 | ) { |
| 1940 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 1941 | } |
| 1942 | // Restore the tab's zoom before publishing the guest so it never paints at inherited app zoom. |
| 1943 | yield* attempt({ operation: "registerWebview.restoreZoomFactor", tabId, webContentsId }, () => |
| 1944 | wc.setZoomFactor(currentTab.zoomFactor), |
| 1945 | ); |
| 1946 | // Replacement guests start unmuted; restore mute before publishing them. |
| 1947 | yield* attempt({ operation: "registerWebview.restoreAudioMuted", tabId, webContentsId }, () => |
| 1948 | wc.setAudioMuted(currentTab.audioMuted), |
| 1949 | ); |
| 1950 | yield* attachListeners(tabId, wc); |
| 1951 | const readAudible = attempt( |
| 1952 | { operation: "registerWebview.readAudible", tabId, webContentsId }, |
| 1953 | () => wc.isCurrentlyAudible(), |
| 1954 | ).pipe(Effect.orElseSucceed(() => false)); |
| 1955 | const attachedAudible = yield* readAudible; |
| 1956 | const registeredAt = yield* currentIso; |
| 1957 | const registration = yield* SynchronizedRef.modifyEffect(tabsRef, (tabs) => |
| 1958 | Effect.gen(function* () { |
| 1959 | const current = tabs.get(tabId); |
| 1960 | if ( |
| 1961 | !current || |
| 1962 | tabLifecycleGenerations.get(tabId) !== expectedGeneration || |
| 1963 | (yield* Ref.get(closingTabIdsRef)).has(tabId) |
| 1964 | ) { |
| 1965 | return [ |
| 1966 | Option.none<{ readonly state: PreviewTabState; readonly pendingUrl: string | null }>(), |
| 1967 | tabs, |
| 1968 | ] as const; |
| 1969 | } |
| 1970 | const pendingUrl = current.navStatus.kind === "Loading" ? current.navStatus.url : null; |
| 1971 | const failure = guestLoadFailures.get(webContentsId); |
| 1972 | const { favicon: _favicon, ...currentWithoutFavicon } = current; |
| 1973 | const next: PreviewTabState = { |
| 1974 | ...currentWithoutFavicon, |
| 1975 | webContentsId, |
| 1976 | navStatus: |
| 1977 | failure && (pendingUrl === null || pendingUrl === failure.url) |
| 1978 | ? { kind: "LoadFailed", title: wc.getTitle(), ...failure } |
| 1979 | : pendingUrl === null |
| 1980 | ? computeNavStatus(wc) |
| 1981 | : current.navStatus, |
| 1982 | canGoBack: wc.navigationHistory.canGoBack(), |
| 1983 | canGoForward: wc.navigationHistory.canGoForward(), |
| 1984 | audible: attachedAudible, |
| 1985 | updatedAt: registeredAt, |
| 1986 | }; |
| 1987 | return [ |
| 1988 | Option.some({ |
| 1989 | state: next, |
| 1990 | pendingUrl, |
| 1991 | }), |
| 1992 | replaceMap(tabs, (copy) => { |
| 1993 | copy.set(tabId, next); |
| 1994 | }), |
| 1995 | ] as const; |
| 1996 | }), |
| 1997 | ); |
| 1998 | if (Option.isNone(registration)) { |
| 1999 | yield* Effect.all([detachControlSession(webContentsId), detachListeners(webContentsId)], { |
| 2000 | concurrency: 2, |
| 2001 | discard: true, |
| 2002 | }); |
| 2003 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 2004 | } |
| 2005 | const { state: registered, pendingUrl } = registration.value; |
| 2006 | // Concurrent actions targeted the previous guest; reconcile this one with committed zoom and mute. |
| 2007 | yield* assertTabZoom(tabId); |
| 2008 | yield* assertTabAudioMuted(tabId).pipe(Effect.ignore); |
| 2009 | runFork(restoreControlSession(tabId, wc)); |
| 2010 | yield* emitIfCurrent(tabId, registered); |
| 2011 | // Ownership checks dropped pre-attach audibility events; reconcile through the event path. |
| 2012 | yield* syncTabAudible(tabId, wc, yield* readAudible); |
| 2013 | yield* attempt({ operation: "registerWebview.sendTheme", tabId, webContentsId }, () => { |
| 2014 | wc.send(ANNOTATION_THEME_CHANNEL, annotationTheme); |
| 2015 | wc.send(ANNOTATION_SEND_ENABLED_CHANNEL, annotationSendEnabled.get(tabId) === true); |
| 2016 | }); |
| 2017 | const latestNavStatus = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.navStatus; |
| 2018 | if ( |
| 2019 | pendingUrl && |
| 2020 | latestNavStatus?.kind === "Loading" && |
| 2021 | latestNavStatus.url === pendingUrl && |
| 2022 | wc.getURL() !== pendingUrl |
| 2023 | ) { |
| 2024 | runFork( |
| 2025 | attemptPromise({ operation: "registerWebview.loadPendingUrl", tabId, webContentsId }, () => |
| 2026 | wc.loadURL(pendingUrl), |
| 2027 | ).pipe(Effect.ignore), |
| 2028 | ); |
| 2029 | } |
| 2030 | }); |
| 2031 | |
| 2032 | const registerWebview = Effect.fn("PreviewManager.registerWebview")(function* ( |
| 2033 | tabId: string, |
| 2034 | webContentsId: number, |
| 2035 | ) { |
| 2036 | const expectedGeneration = tabLifecycleGenerations.get(tabId); |
| 2037 | return yield* withTabLifecycleLock( |
| 2038 | tabId, |
| 2039 | registerWebviewUnlocked(tabId, webContentsId, expectedGeneration), |
| 2040 | ); |
| 2041 | }); |
| 2042 | |
| 2043 | // URL tabs can paint before registerWebview; set the background at attach time. |
| 2044 | const prepareWebview = Effect.fn("PreviewManager.prepareWebview")(function* ( |
| 2045 | wc: Electron.WebContents, |
| 2046 | ) { |
| 2047 | yield* rendererHistory.register(wc, { surface: "preview" }); |
| 2048 | const webContentsId = wc.id; |
| 2049 | wc.on("did-start-navigation", (event) => { |
| 2050 | if (event.isMainFrame && !event.isSameDocument) guestLoadFailures.delete(webContentsId); |
| 2051 | }); |
| 2052 | wc.on("did-fail-load", (_event, code, description, validatedUrl, isMainFrame) => { |
| 2053 | if (code === -3 || !isMainFrame) return; |
| 2054 | guestLoadFailures.set(webContentsId, { |
| 2055 | url: validatedUrl || wc.getURL(), |
| 2056 | code, |
| 2057 | description, |
| 2058 | }); |
| 2059 | }); |
| 2060 | wc.once("destroyed", () => { |
| 2061 | guestLoadFailures.delete(webContentsId); |
| 2062 | runFork(hideDestroyedGuest(webContentsId)); |
| 2063 | runFork(detachControlSession(webContentsId)); |
| 2064 | }); |
| 2065 | yield* ensureControlSession(wc).pipe( |
| 2066 | Effect.catchCause((cause) => |
| 2067 | Effect.logDebug("Preview webview control session was not opened on attach.", { |
| 2068 | webContentsId, |
| 2069 | cause, |
| 2070 | }), |
| 2071 | ), |
| 2072 | ); |
| 2073 | }); |
| 2074 | |
| 2075 | /** |
| 2076 | * A same-site navigation reuses the renderer, so it never commits while a page script holds it. |
| 2077 | * Runs before the navigation starts: Chromium does not deliver a terminate once one is pending. |
| 2078 | */ |
| 2079 | const leaveBusyPage = Effect.fn("PreviewManager.leaveBusyPage")(function* ( |
| 2080 | wc: Electron.WebContents, |
| 2081 | ) { |
| 2082 | const control = (yield* SynchronizedRef.get(controlSessionsRef)).get(wc.id); |
| 2083 | if (!control) return; |
| 2084 | const answered = yield* Effect.promise(() => |
| 2085 | control.debugger.sendCommand("Runtime.evaluate", { expression: "0" }).then( |
| 2086 | () => undefined, |
| 2087 | () => undefined, |
| 2088 | ), |
| 2089 | ).pipe(Effect.timeoutOption(BUSY_PAGE_PROBE_MS)); |
| 2090 | if (Option.isSome(answered)) return; |
| 2091 | // Not awaited: a renderer blocked on something other than script may never acknowledge it. |
| 2092 | yield* Effect.sync(() => { |
| 2093 | void control.debugger.sendCommand("Runtime.terminateExecution").catch(() => undefined); |
| 2094 | }); |
| 2095 | }); |
| 2096 | |
| 2097 | const navigate = Effect.fn("PreviewManager.navigate")(function* (tabId: string, rawUrl: string) { |
| 2098 | const url = yield* attempt({ operation: "navigate.normalizeUrl", tabId }, () => |
| 2099 | normalizePreviewUrl(rawUrl), |
| 2100 | ); |
| 2101 | const updatedAt = yield* currentIso; |
| 2102 | const pending = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 2103 | const current = tabs.get(tabId); |
| 2104 | const next: PreviewTabState = { |
| 2105 | tabId, |
| 2106 | webContentsId: current?.webContentsId ?? null, |
| 2107 | navStatus: { |
| 2108 | kind: "Loading", |
| 2109 | url, |
| 2110 | title: current?.navStatus.kind === "Idle" || !current ? "" : current.navStatus.title, |
| 2111 | }, |
| 2112 | canGoBack: current?.canGoBack ?? false, |
| 2113 | canGoForward: current?.canGoForward ?? false, |
| 2114 | zoomFactor: current?.zoomFactor ?? DEFAULT_ZOOM_FACTOR, |
| 2115 | pictureInPicture: current?.pictureInPicture ?? false, |
| 2116 | colorScheme: current?.colorScheme ?? "system", |
| 2117 | // Keep audibility until Chromium replaces the document; clearing it here can lose the final transition. |
| 2118 | audioMuted: current?.audioMuted ?? false, |
| 2119 | audible: current?.audible ?? false, |
| 2120 | controller: current?.controller ?? "none", |
| 2121 | capturing: current?.capturing ?? false, |
| 2122 | ...(current?.favicon ? { favicon: current.favicon } : {}), |
| 2123 | ...(current?.serverTab ? { serverTab: current.serverTab } : {}), |
| 2124 | updatedAt, |
| 2125 | }; |
| 2126 | return [ |
| 2127 | next, |
| 2128 | replaceMap(tabs, (copy) => { |
| 2129 | copy.set(tabId, next); |
| 2130 | }), |
| 2131 | ] as const; |
| 2132 | }); |
| 2133 | yield* emitIfCurrent(tabId, pending); |
| 2134 | if (pending.webContentsId == null) return; |
| 2135 | const webContentsId = pending.webContentsId; |
| 2136 | const wc = webContents.fromId(webContentsId); |
| 2137 | if (!wc || wc.isDestroyed()) { |
| 2138 | const expectedAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); |
| 2139 | yield* withTabLifecycleLock( |
| 2140 | tabId, |
| 2141 | Effect.gen(function* () { |
| 2142 | const currentTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2143 | const currentAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); |
| 2144 | const currentWebContents = webContents.fromId(webContentsId); |
| 2145 | if ( |
| 2146 | currentTab?.webContentsId !== webContentsId || |
| 2147 | currentAttachment !== expectedAttachment || |
| 2148 | (currentWebContents && !currentWebContents.isDestroyed()) |
| 2149 | ) { |
| 2150 | return; |
| 2151 | } |
| 2152 | yield* Effect.all( |
| 2153 | [ |
| 2154 | detachControlSession(webContentsId), |
| 2155 | detachListeners(webContentsId), |
| 2156 | cancelPickElement(tabId), |
| 2157 | ], |
| 2158 | { concurrency: 3, discard: true }, |
| 2159 | ); |
| 2160 | const detached = yield* SynchronizedRef.modify(tabsRef, (tabs) => { |
| 2161 | const current = tabs.get(tabId); |
| 2162 | if (current?.webContentsId !== webContentsId) { |
| 2163 | return [Option.none<PreviewTabState>(), tabs] as const; |
| 2164 | } |
| 2165 | const { favicon: _favicon, ...currentWithoutFavicon } = current; |
| 2166 | const next: PreviewTabState = { ...currentWithoutFavicon, webContentsId: null }; |
| 2167 | return [ |
| 2168 | Option.some(next), |
| 2169 | replaceMap(tabs, (copy) => { |
| 2170 | copy.set(tabId, next); |
| 2171 | }), |
| 2172 | ] as const; |
| 2173 | }); |
| 2174 | if (Option.isSome(detached)) yield* emitIfCurrent(tabId, detached.value); |
| 2175 | }), |
| 2176 | ); |
| 2177 | return; |
| 2178 | } |
| 2179 | yield* leaveBusyPage(wc); |
| 2180 | if (wc.getURL() === url) { |
| 2181 | yield* attempt({ operation: "navigate.reload", tabId, webContentsId: wc.id }, () => |
| 2182 | wc.reload(), |
| 2183 | ); |
| 2184 | return; |
| 2185 | } |
| 2186 | let unloadPrevented = false; |
| 2187 | const onPreventUnload = () => { |
| 2188 | unloadPrevented = true; |
| 2189 | }; |
| 2190 | yield* attemptPromise({ operation: "navigate.loadURL", tabId, webContentsId: wc.id }, () => { |
| 2191 | wc.on("will-prevent-unload", onPreventUnload); |
| 2192 | return wc.loadURL(url); |
| 2193 | }).pipe( |
| 2194 | // Blocked unloads and superseding navigations both reject with ERR_ABORTED. |
| 2195 | Effect.catchIf( |
| 2196 | (error) => |
| 2197 | !unloadPrevented && |
| 2198 | Predicate.hasProperty(error.cause, "errno") && |
| 2199 | error.cause.errno === -3, |
| 2200 | () => Effect.void, |
| 2201 | ), |
| 2202 | Effect.ensuring(Effect.sync(() => wc.off("will-prevent-unload", onPreventUnload))), |
| 2203 | ); |
| 2204 | }); |
| 2205 | |
| 2206 | const withWebContents = Effect.fn("PreviewManager.withWebContents")(function* ( |
| 2207 | operation: string, |
| 2208 | tabId: string, |
| 2209 | use: (wc: Electron.WebContents) => void, |
| 2210 | ) { |
| 2211 | const wc = yield* requireWebContents(tabId); |
| 2212 | yield* attempt({ operation, tabId, webContentsId: wc.id }, () => use(wc)); |
| 2213 | }); |
| 2214 | |
| 2215 | const leavePage = Effect.fn("PreviewManager.leavePage")(function* ( |
| 2216 | operation: string, |
| 2217 | tabId: string, |
| 2218 | use: (wc: Electron.WebContents) => void, |
| 2219 | ) { |
| 2220 | const wc = yield* requireWebContents(tabId); |
| 2221 | yield* leaveBusyPage(wc); |
| 2222 | yield* attempt({ operation, tabId, webContentsId: wc.id }, () => use(wc)); |
| 2223 | }); |
| 2224 | |
| 2225 | const goBack = (tabId: string) => |
| 2226 | leavePage("goBack", tabId, (wc) => { |
| 2227 | if (wc.navigationHistory.canGoBack()) wc.navigationHistory.goBack(); |
| 2228 | }); |
| 2229 | const goForward = (tabId: string) => |
| 2230 | leavePage("goForward", tabId, (wc) => { |
| 2231 | if (wc.navigationHistory.canGoForward()) wc.navigationHistory.goForward(); |
| 2232 | }); |
| 2233 | const refresh = (tabId: string) => leavePage("refresh", tabId, (wc) => wc.reload()); |
| 2234 | const stopLoading = (tabId: string) => withWebContents("stopLoading", tabId, (wc) => wc.stop()); |
| 2235 | const hardReload = (tabId: string) => |
| 2236 | leavePage("hardReload", tabId, (wc) => wc.reloadIgnoringCache()); |
| 2237 | const findInPage = ( |
| 2238 | tabId: string, |
| 2239 | text: string, |
| 2240 | options: { readonly forward: boolean; readonly newSession: boolean }, |
| 2241 | ) => |
| 2242 | withWebContents("findInPage", tabId, (wc) => { |
| 2243 | // Electron's `findNext` means "begin a new session", not "go to the next match". |
| 2244 | wc.findInPage(text, { forward: options.forward, findNext: options.newSession }); |
| 2245 | }); |
| 2246 | const stopFindInPage = (tabId: string) => |
| 2247 | withWebContents("stopFindInPage", tabId, (wc) => wc.stopFindInPage("clearSelection")).pipe( |
| 2248 | Effect.catchTags({ |
| 2249 | PreviewTabNotFoundError: () => Effect.void, |
| 2250 | PreviewWebContentsNotFoundError: () => Effect.void, |
| 2251 | }), |
| 2252 | ); |
| 2253 | |
| 2254 | const openDevTools = Effect.fn("PreviewManager.openDevTools")(function* (tabId: string) { |
| 2255 | const wc = yield* requireWebContents(tabId); |
| 2256 | if (wc.isDevToolsOpened()) { |
| 2257 | yield* attempt({ operation: "openDevTools.focus", tabId, webContentsId: wc.id }, () => |
| 2258 | wc.devToolsWebContents?.focus(), |
| 2259 | ); |
| 2260 | return; |
| 2261 | } |
| 2262 | yield* detachControlSession(wc.id); |
| 2263 | yield* attempt({ operation: "openDevTools", tabId, webContentsId: wc.id }, () => { |
| 2264 | wc.once("devtools-closed", () => { |
| 2265 | if (!wc.isDestroyed()) runFork(restoreControlSession(tabId, wc)); |
| 2266 | }); |
| 2267 | wc.openDevTools({ mode: "detach" }); |
| 2268 | }); |
| 2269 | }); |
| 2270 | |
| 2271 | const setAnnotationTheme = Effect.fn("PreviewManager.setAnnotationTheme")(function* ( |
| 2272 | theme: DesktopPreviewAnnotationTheme, |
| 2273 | ) { |
| 2274 | yield* Ref.set(annotationThemeRef, theme); |
| 2275 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 2276 | yield* Effect.forEach( |
| 2277 | tabs.values(), |
| 2278 | (tab) => { |
| 2279 | if (tab.webContentsId == null) return Effect.void; |
| 2280 | const wc = webContents.fromId(tab.webContentsId); |
| 2281 | return !wc || wc.isDestroyed() |
| 2282 | ? Effect.void |
| 2283 | : attempt( |
| 2284 | { |
| 2285 | operation: "setAnnotationTheme", |
| 2286 | tabId: tab.tabId, |
| 2287 | webContentsId: tab.webContentsId, |
| 2288 | }, |
| 2289 | () => wc.send(ANNOTATION_THEME_CHANNEL, theme), |
| 2290 | ).pipe(Effect.ignore); |
| 2291 | }, |
| 2292 | { discard: true }, |
| 2293 | ); |
| 2294 | }); |
| 2295 | |
| 2296 | const setAnnotationSendEnabled = Effect.fn("PreviewManager.setAnnotationSendEnabled")(function* ( |
| 2297 | tabId: string, |
| 2298 | enabled: boolean, |
| 2299 | ) { |
| 2300 | const wc = yield* requireWebContents(tabId); |
| 2301 | yield* attempt({ operation: "setAnnotationSendEnabled", tabId, webContentsId: wc.id }, () => { |
| 2302 | annotationSendEnabled.set(tabId, enabled); |
| 2303 | wc.send(ANNOTATION_SEND_ENABLED_CHANNEL, enabled); |
| 2304 | }); |
| 2305 | }); |
| 2306 | |
| 2307 | // Voice state reaches a guest only while its pick session, by id, still owns the tab. |
| 2308 | const updateAnnotationVoice = Effect.fn("PreviewManager.updateAnnotationVoice")(function* ( |
| 2309 | state: DesktopPreviewAnnotationVoiceState, |
| 2310 | ) { |
| 2311 | const sessions = yield* Ref.get(pickSessionsRef); |
| 2312 | if (sessions.get(state.tabId)?.voiceSessionId !== state.sessionId) return; |
| 2313 | const wc = yield* requireWebContents(state.tabId); |
| 2314 | yield* attempt({ operation: "annotationVoice.update", tabId: state.tabId }, () => |
| 2315 | wc.send(ANNOTATION_VOICE_STATE_CHANNEL, state), |
| 2316 | ); |
| 2317 | }); |
| 2318 | |
| 2319 | const pickElement = Effect.fn("PreviewManager.pickElement")(function* ( |
| 2320 | tabId: string, |
| 2321 | voice?: DesktopPreviewAnnotationVoiceConfig, |
| 2322 | ) { |
| 2323 | const wc = yield* requireWebContents(tabId); |
| 2324 | yield* cancelPickElement(tabId); |
| 2325 | const annotationTheme = yield* Ref.get(annotationThemeRef); |
| 2326 | return yield* Effect.callback<PreviewAnnotationSubmissionResult | null, PreviewManagerError>( |
| 2327 | (resume) => { |
| 2328 | const session: PickSession = { |
| 2329 | voiceSessionId: voice?.sessionId, |
| 2330 | cancel: Effect.suspend(() => cancelPickSession()), |
| 2331 | }; |
| 2332 | const cleanup = Effect.fn("PreviewManager.cleanupPickElement")(function* () { |
| 2333 | yield* attempt({ operation: "pickElement.cleanup", tabId, webContentsId: wc.id }, () => { |
| 2334 | wc.ipc.removeListener(ELEMENT_PICKED_CHANNEL, onMessage); |
| 2335 | wc.ipc.removeListener(ANNOTATION_VOICE_EVENT_CHANNEL, onVoiceEvent); |
| 2336 | wc.off("destroyed", onDestroyed); |
| 2337 | wc.off("did-start-navigation", onNavigated); |
| 2338 | }).pipe(Effect.ignore); |
| 2339 | // A newer session may already own the slot when this one is cancelled. |
| 2340 | yield* Ref.update(pickSessionsRef, (sessions) => |
| 2341 | sessions.get(tabId) === session |
| 2342 | ? replaceMap(sessions, (copy) => { |
| 2343 | copy.delete(tabId); |
| 2344 | }) |
| 2345 | : sessions, |
| 2346 | ); |
| 2347 | }); |
| 2348 | let settled = false; |
| 2349 | const claimSettle = (): boolean => { |
| 2350 | if (settled) return false; |
| 2351 | settled = true; |
| 2352 | return true; |
| 2353 | }; |
| 2354 | const finishPick = Effect.fn("PreviewManager.finishPickElement")(function* ( |
| 2355 | payload: PreviewAnnotationSubmissionResult | null, |
| 2356 | ) { |
| 2357 | yield* cleanup(); |
| 2358 | resume(Effect.succeed(payload)); |
| 2359 | }); |
| 2360 | const settlePick = Effect.fn("PreviewManager.settlePickElement")(function* ( |
| 2361 | payload: PreviewAnnotationSubmissionResult | null, |
| 2362 | ) { |
| 2363 | if (!claimSettle()) return; |
| 2364 | yield* finishPick(payload); |
| 2365 | }); |
| 2366 | const settle = (payload: PreviewAnnotationSubmissionResult | null) => { |
| 2367 | runFork(settlePick(payload)); |
| 2368 | }; |
| 2369 | const cancelPickSession = Effect.fn("PreviewManager.cancelPickSession")(function* () { |
| 2370 | if (!claimSettle()) return; |
| 2371 | yield* cleanup(); |
| 2372 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 2373 | const activeTab = tabs.get(tabId); |
| 2374 | if (activeTab?.webContentsId != null) { |
| 2375 | const activeWc = webContents.fromId(activeTab.webContentsId); |
| 2376 | if (activeWc && !activeWc.isDestroyed()) { |
| 2377 | yield* attempt( |
| 2378 | { |
| 2379 | operation: "cancelPickElement", |
| 2380 | tabId, |
| 2381 | webContentsId: activeWc.id, |
| 2382 | }, |
| 2383 | () => activeWc.send(CANCEL_PICK_CHANNEL), |
| 2384 | ).pipe(Effect.ignore); |
| 2385 | } |
| 2386 | } |
| 2387 | resume(Effect.succeed(null)); |
| 2388 | }); |
| 2389 | const onMessage = (_event: Electron.IpcMainEvent, ...args: unknown[]): void => { |
| 2390 | const payload = args[0]; |
| 2391 | if (!isPreviewAnnotationPayload(payload)) { |
| 2392 | settle(null); |
| 2393 | return; |
| 2394 | } |
| 2395 | const cropRect = normalizeCaptureRect(args[1]); |
| 2396 | const submission = |
| 2397 | args[2] === "send" && annotationSendEnabled.get(tabId) === true ? "send" : "attach"; |
| 2398 | const devicePixelRatio = typeof args[3] === "number" ? args[3] : 1; |
| 2399 | const targetRects = [ |
| 2400 | ...payload.elements.map((target) => target.rect), |
| 2401 | ...payload.regions.map((target) => target.rect), |
| 2402 | ...payload.strokes.map((target) => target.bounds), |
| 2403 | ]; |
| 2404 | runFork( |
| 2405 | capturePageWithRetry( |
| 2406 | { operation: "captureAnnotationScreenshot", tabId, webContentsId: wc.id }, |
| 2407 | tabId, |
| 2408 | wc, |
| 2409 | ).pipe( |
| 2410 | Effect.map((image) => |
| 2411 | cropAnnotationScreenshot(image, cropRect, devicePixelRatio, targetRects), |
| 2412 | ), |
| 2413 | Effect.tapError((error) => |
| 2414 | Effect.logWarning("preview annotation screenshot failed").pipe( |
| 2415 | Effect.annotateLogs({ tabId, webContentsId: wc.id, error: error.message }), |
| 2416 | ), |
| 2417 | ), |
| 2418 | Effect.withSpan("PreviewManager.captureAnnotationScreenshot"), |
| 2419 | Effect.match({ |
| 2420 | onFailure: (): PreviewAnnotationSubmissionResult => ({ |
| 2421 | annotation: payload, |
| 2422 | submission, |
| 2423 | screenshotFailed: true, |
| 2424 | }), |
| 2425 | onSuccess: (screenshot): PreviewAnnotationSubmissionResult => |
| 2426 | screenshot === null |
| 2427 | ? { annotation: payload, submission, screenshotFailed: true } |
| 2428 | : { annotation: { ...payload, screenshot }, submission }, |
| 2429 | }), |
| 2430 | Effect.flatMap((result) => { |
| 2431 | // Late captures must not signal an overlay that may now belong to a newer pick. |
| 2432 | if (!claimSettle()) return Effect.void; |
| 2433 | return attempt( |
| 2434 | { operation: "pickElement.captureComplete", tabId, webContentsId: wc.id }, |
| 2435 | () => { |
| 2436 | if (!wc.isDestroyed()) wc.send(ANNOTATION_CAPTURED_CHANNEL); |
| 2437 | }, |
| 2438 | ).pipe(Effect.ignore, Effect.andThen(finishPick(result))); |
| 2439 | }), |
| 2440 | ), |
| 2441 | ); |
| 2442 | }; |
| 2443 | // Only this guest's listener sees these; the renderer still checks the session id. |
| 2444 | const onVoiceEvent = (_event: Electron.IpcMainEvent, value: unknown) => { |
| 2445 | const decoded = decodeAnnotationVoiceEvent(value); |
| 2446 | if (settled || !voice || Option.isNone(decoded)) return; |
| 2447 | if (decoded.value.sessionId !== voice.sessionId) return; |
| 2448 | runFork( |
| 2449 | Effect.gen(function* () { |
| 2450 | const window = yield* Ref.get(mainWindowRef); |
| 2451 | if (Option.isNone(window) || window.value.isDestroyed()) return; |
| 2452 | yield* attempt({ operation: "annotationVoice.forward", tabId }, () => |
| 2453 | window.value.webContents.send( |
| 2454 | PREVIEW_ANNOTATION_VOICE_EVENT_CHANNEL, |
| 2455 | tabId, |
| 2456 | decoded.value, |
| 2457 | ), |
| 2458 | ).pipe(Effect.ignore); |
| 2459 | }), |
| 2460 | ); |
| 2461 | }; |
| 2462 | const onDestroyed = () => settle(null); |
| 2463 | const onNavigated = ( |
| 2464 | event: Electron.Event<Electron.WebContentsDidStartNavigationEventParams>, |
| 2465 | ) => { |
| 2466 | if (event.isMainFrame) settle(null); |
| 2467 | }; |
| 2468 | const registerPickElement = Effect.fn("PreviewManager.registerPickElement")(function* () { |
| 2469 | // Atomically swap/cancel overlapping picks so another pick cannot claim a transient empty slot. |
| 2470 | const replaced = yield* Ref.modify(pickSessionsRef, (sessions) => [ |
| 2471 | sessions.get(tabId) ?? null, |
| 2472 | replaceMap(sessions, (copy) => { |
| 2473 | copy.set(tabId, session); |
| 2474 | }), |
| 2475 | ]); |
| 2476 | if (replaced) yield* replaced.cancel; |
| 2477 | // A newer pick may cancel during teardown; do not leak listeners or start an abandoned overlay. |
| 2478 | if (settled) return; |
| 2479 | yield* attempt({ operation: "pickElement.register", tabId, webContentsId: wc.id }, () => { |
| 2480 | wc.ipc.on(ELEMENT_PICKED_CHANNEL, onMessage); |
| 2481 | if (voice) wc.ipc.on(ANNOTATION_VOICE_EVENT_CHANNEL, onVoiceEvent); |
| 2482 | wc.once("destroyed", onDestroyed); |
| 2483 | wc.on("did-start-navigation", onNavigated); |
| 2484 | if (!wc.isFocused()) wc.focus(); |
| 2485 | wc.send( |
| 2486 | START_PICK_CHANNEL, |
| 2487 | annotationTheme, |
| 2488 | annotationSendEnabled.get(tabId) === true, |
| 2489 | voice, |
| 2490 | ); |
| 2491 | }); |
| 2492 | }); |
| 2493 | runFork( |
| 2494 | registerPickElement().pipe( |
| 2495 | Effect.catch((error: PreviewManagerError) => { |
| 2496 | if (!claimSettle()) return Effect.void; |
| 2497 | resume(Effect.fail(error)); |
| 2498 | return cleanup(); |
| 2499 | }), |
| 2500 | ), |
| 2501 | ); |
| 2502 | return session.cancel; |
| 2503 | }, |
| 2504 | ); |
| 2505 | }); |
| 2506 | |
| 2507 | const reapplyZoom = Effect.fn("PreviewManager.reapplyZoom")(function* () { |
| 2508 | const tabIds = Array.from((yield* SynchronizedRef.get(tabsRef)).keys()); |
| 2509 | yield* Effect.forEach(tabIds, assertTabZoom, { discard: true }); |
| 2510 | }); |
| 2511 | |
| 2512 | const applyZoom = Effect.fn("PreviewManager.applyZoom")(function* ( |
| 2513 | tabId: string, |
| 2514 | transform: (current: number) => number, |
| 2515 | ) { |
| 2516 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2517 | if (!tab) return; |
| 2518 | const next = transform(tab.zoomFactor); |
| 2519 | if (Math.abs(next - tab.zoomFactor) < ZOOM_EPSILON) return; |
| 2520 | if (tab.webContentsId != null) { |
| 2521 | const wc = webContents.fromId(tab.webContentsId); |
| 2522 | if (wc && !wc.isDestroyed()) { |
| 2523 | yield* attempt({ operation: "applyZoom", tabId, webContentsId: wc.id }, () => |
| 2524 | wc.setZoomFactor(next), |
| 2525 | ); |
| 2526 | } |
| 2527 | } |
| 2528 | yield* update(tabId, { zoomFactor: next }); |
| 2529 | }); |
| 2530 | |
| 2531 | // Media emulation belongs to the debugger session and must be restored after every reattach. |
| 2532 | const applyColorScheme = Effect.fn("PreviewManager.applyColorScheme")(function* ( |
| 2533 | tabId: string, |
| 2534 | wc: Electron.WebContents, |
| 2535 | colorScheme: DesktopPreviewColorScheme, |
| 2536 | ) { |
| 2537 | const control = yield* ensureControlSession(wc); |
| 2538 | yield* attemptPromise({ operation: "applyColorScheme", tabId, webContentsId: wc.id }, () => |
| 2539 | control.debugger.sendCommand("Emulation.setEmulatedMedia", { |
| 2540 | features: [ |
| 2541 | { |
| 2542 | name: "prefers-color-scheme", |
| 2543 | value: colorScheme === "system" ? "" : colorScheme, |
| 2544 | }, |
| 2545 | ], |
| 2546 | }), |
| 2547 | ); |
| 2548 | }); |
| 2549 | |
| 2550 | // The scheme is read after the session attaches so a concurrent setColorScheme is not |
| 2551 | // overwritten with a stale snapshot. |
| 2552 | const restoreControlSession = Effect.fn("PreviewManager.restoreControlSession")( |
| 2553 | function* (tabId: string, wc: Electron.WebContents) { |
| 2554 | const beforeAttach = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2555 | if (beforeAttach?.webContentsId !== wc.id) return; |
| 2556 | const control = yield* ensureControlSession(wc); |
| 2557 | if (beforeAttach.serverTab) yield* listenToBrowserHost; |
| 2558 | const afterAttach = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2559 | if (afterAttach?.webContentsId !== wc.id) { |
| 2560 | yield* detachControlSession(wc.id); |
| 2561 | return; |
| 2562 | } |
| 2563 | if (afterAttach.serverTab) { |
| 2564 | browserHost.attach(afterAttach.serverTab, { |
| 2565 | webContents: wc, |
| 2566 | debugger: control.debugger, |
| 2567 | cookiesCleared: () => runPromise(browserSession.cookiesCleared(wc.session)), |
| 2568 | }); |
| 2569 | // CDP cannot recover a failure that happened before it attached. |
| 2570 | const { navStatus } = afterAttach; |
| 2571 | if (navStatus.kind === "LoadFailed") { |
| 2572 | browserHost.loadFailed(afterAttach.serverTab, { |
| 2573 | url: navStatus.url, |
| 2574 | code: navStatus.code, |
| 2575 | description: navStatus.description, |
| 2576 | }); |
| 2577 | } |
| 2578 | } |
| 2579 | if (afterAttach.colorScheme !== "system") { |
| 2580 | yield* attemptPromise({ operation: "applyColorScheme", tabId, webContentsId: wc.id }, () => |
| 2581 | control.debugger.sendCommand("Emulation.setEmulatedMedia", { |
| 2582 | features: [ |
| 2583 | { |
| 2584 | name: "prefers-color-scheme", |
| 2585 | value: afterAttach.colorScheme, |
| 2586 | }, |
| 2587 | ], |
| 2588 | }), |
| 2589 | ); |
| 2590 | } |
| 2591 | }, |
| 2592 | (effect, _tabId, _wc) => effect.pipe(Effect.ignore), |
| 2593 | ); |
| 2594 | |
| 2595 | // Only the session that saw the detach is replaced: by the time the event is |
| 2596 | // handled the guest may already hold a newer one, which must stay. |
| 2597 | const recoverDetachedControlSession = Effect.fn("PreviewManager.recoverDetachedControlSession")( |
| 2598 | function* (wc: Electron.WebContents, detached: BrowserControlSession) { |
| 2599 | const removed = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) => |
| 2600 | sessions.get(wc.id) === detached |
| 2601 | ? ([ |
| 2602 | true, |
| 2603 | replaceMap(sessions, (copy) => { |
| 2604 | copy.delete(wc.id); |
| 2605 | }), |
| 2606 | ] as const) |
| 2607 | : ([false, sessions] as const), |
| 2608 | ); |
| 2609 | if (!removed) return; |
| 2610 | yield* releaseControlSession(detached); |
| 2611 | if (wc.isDestroyed()) return; |
| 2612 | const tabId = yield* tabIdForWebContents(wc.id); |
| 2613 | if (tabId === null) return; |
| 2614 | yield* restoreControlSession(tabId, wc); |
| 2615 | }, |
| 2616 | (recover) => Effect.ignore(recover), |
| 2617 | ); |
| 2618 | |
| 2619 | const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* ( |
| 2620 | tabId: string, |
| 2621 | colorScheme: DesktopPreviewColorScheme, |
| 2622 | ) { |
| 2623 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2624 | if (!tab) { |
| 2625 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 2626 | } |
| 2627 | if (tab.colorScheme !== colorScheme) { |
| 2628 | yield* update(tabId, { colorScheme }); |
| 2629 | } |
| 2630 | // registerWebview may swap guests during the update; apply to the current one. |
| 2631 | const webContentsId = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.webContentsId; |
| 2632 | if (webContentsId == null) return; |
| 2633 | const wc = webContents.fromId(webContentsId); |
| 2634 | if (!wc || wc.isDestroyed()) return; |
| 2635 | yield* applyColorScheme(tabId, wc, colorScheme); |
| 2636 | }); |
| 2637 | |
| 2638 | const setShown = Effect.fn("PreviewManager.setShown")(function* (tabId: string, shown: boolean) { |
| 2639 | const serverTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.serverTab; |
| 2640 | if (serverTab) browserHost.setShown(serverTab, shown); |
| 2641 | }); |
| 2642 | |
| 2643 | const setCaptureDrawn = Effect.fn("PreviewManager.setCaptureDrawn")(function* ( |
| 2644 | tabId: string, |
| 2645 | drawn: boolean, |
| 2646 | ) { |
| 2647 | const serverTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.serverTab; |
| 2648 | if (serverTab) browserHost.setCaptureDrawn(serverTab, drawn); |
| 2649 | }); |
| 2650 | |
| 2651 | // A window reload or guest crash destroys the guest without the renderer reporting it hidden. |
| 2652 | // The renderer reports again once a new guest registers. |
| 2653 | const hideDestroyedGuest = Effect.fnUntraced(function* (webContentsId: number) { |
| 2654 | for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) { |
| 2655 | if (tab.webContentsId === webContentsId && tab.serverTab) { |
| 2656 | browserHost.setShown(tab.serverTab, false); |
| 2657 | browserHost.setCaptureDrawn(tab.serverTab, false); |
| 2658 | } |
| 2659 | } |
| 2660 | }); |
| 2661 | |
| 2662 | const setAudioMuted = Effect.fn("PreviewManager.setAudioMuted")(function* ( |
| 2663 | tabId: string, |
| 2664 | audioMuted: boolean, |
| 2665 | ) { |
| 2666 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2667 | if (!tab) { |
| 2668 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 2669 | } |
| 2670 | // Serialize commit/apply, then read committed mute so concurrent toggles cannot reach Chromium out of order. |
| 2671 | yield* withTabLifecycleLock( |
| 2672 | tabId, |
| 2673 | Effect.gen(function* () { |
| 2674 | const previous = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.audioMuted; |
| 2675 | const committed = previous !== undefined && previous !== audioMuted; |
| 2676 | if (committed) { |
| 2677 | yield* update(tabId, { audioMuted }); |
| 2678 | } |
| 2679 | // Roll back rejected mute commits so the UI reflects actual playback. |
| 2680 | yield* assertTabAudioMuted(tabId).pipe( |
| 2681 | Effect.tapError(() => |
| 2682 | committed ? update(tabId, { audioMuted: previous }) : Effect.void, |
| 2683 | ), |
| 2684 | ); |
| 2685 | }), |
| 2686 | ); |
| 2687 | }); |
| 2688 | |
| 2689 | const captureScreenshot = Effect.fn("PreviewManager.captureScreenshot")(function* ( |
| 2690 | tabId: string, |
| 2691 | ) { |
| 2692 | const wc = yield* requireWebContents(tabId); |
| 2693 | const [createdAt, millis, image] = yield* Effect.all([ |
| 2694 | currentIso, |
| 2695 | currentMillis, |
| 2696 | captureStillImage( |
| 2697 | { |
| 2698 | operation: "captureScreenshot.capturePage", |
| 2699 | tabId, |
| 2700 | webContentsId: wc.id, |
| 2701 | }, |
| 2702 | tabId, |
| 2703 | wc, |
| 2704 | ), |
| 2705 | ]); |
| 2706 | const uuid = yield* random.uuidV4(); |
| 2707 | const id = `browser-screenshot-${artifactSiteSlug(wc.getURL())}-${millis.toString(36)}-${uuid.slice(0, 8)}`; |
| 2708 | const artifactPath = path.join(resolvedArtifactDirectory, `${id}.png`); |
| 2709 | const data = image.toPNG(); |
| 2710 | yield* fileSystem.makeDirectory(resolvedArtifactDirectory, { recursive: true }).pipe( |
| 2711 | Effect.mapError( |
| 2712 | (cause) => |
| 2713 | new PreviewOperationError({ |
| 2714 | operation: "captureScreenshot.makeDirectory", |
| 2715 | tabId, |
| 2716 | webContentsId: wc.id, |
| 2717 | artifactPath, |
| 2718 | cause, |
| 2719 | }), |
| 2720 | ), |
| 2721 | ); |
| 2722 | yield* fileSystem.writeFile(artifactPath, data).pipe( |
| 2723 | Effect.mapError( |
| 2724 | (cause) => |
| 2725 | new PreviewOperationError({ |
| 2726 | operation: "captureScreenshot.writeFile", |
| 2727 | tabId, |
| 2728 | webContentsId: wc.id, |
| 2729 | artifactPath, |
| 2730 | cause, |
| 2731 | }), |
| 2732 | ), |
| 2733 | ); |
| 2734 | return { |
| 2735 | id, |
| 2736 | tabId, |
| 2737 | path: artifactPath, |
| 2738 | mimeType: "image/png" as const, |
| 2739 | sizeBytes: data.byteLength, |
| 2740 | createdAt, |
| 2741 | }; |
| 2742 | }); |
| 2743 | |
| 2744 | const captureStillImage = Effect.fn("PreviewManager.captureStillImage")(function* ( |
| 2745 | errorContext: PreviewOperationContext, |
| 2746 | tabId: string, |
| 2747 | wc: Electron.WebContents, |
| 2748 | ) { |
| 2749 | const consumer = Symbol("still-image"); |
| 2750 | return yield* Effect.acquireUseRelease( |
| 2751 | startFrameCapture(tabId, consumer), |
| 2752 | () => |
| 2753 | Effect.gen(function* () { |
| 2754 | // A DOM mutation can precede its compositor frame even in a shown |
| 2755 | // window. Keep the original guest rendering through the next paint; |
| 2756 | // a hidden guest otherwise returns old pixels or never completes. |
| 2757 | const probe = Effect.fn("PreviewManager.probeStillImagePaint")(function* ( |
| 2758 | send: ( |
| 2759 | method: string, |
| 2760 | params?: Record<string, unknown>, |
| 2761 | ) => Effect.Effect<unknown, PreviewOperationError>, |
| 2762 | ) { |
| 2763 | // CDP executes against the current document without Electron's |
| 2764 | // did-stop-loading gate. Isolate scheduling globals from page code. |
| 2765 | const tree = (yield* send("Page.getFrameTree")) as |
| 2766 | | { frameTree?: { frame?: { id?: string } } } |
| 2767 | | undefined; |
| 2768 | const frameId = tree?.frameTree?.frame?.id; |
| 2769 | if (typeof frameId !== "string") |
| 2770 | return yield* new PreviewOperationError({ |
| 2771 | ...errorContext, |
| 2772 | cause: new Error("The preview paint frame is unavailable."), |
| 2773 | }); |
| 2774 | const world = (yield* send("Page.createIsolatedWorld", { |
| 2775 | frameId, |
| 2776 | worldName: "t3-preview-still-image", |
| 2777 | })) as { executionContextId?: number } | undefined; |
| 2778 | if (typeof world?.executionContextId !== "number") |
| 2779 | return yield* new PreviewOperationError({ |
| 2780 | ...errorContext, |
| 2781 | cause: new Error("The preview paint context is unavailable."), |
| 2782 | }); |
| 2783 | const response = (yield* send("Runtime.evaluate", { |
| 2784 | // Settle and release scheduled work when only timers keep running. |
| 2785 | // The outer deadline still bounds a frozen renderer. |
| 2786 | expression: `new Promise(resolve => { let frame; const timer = setTimeout(() => { cancelAnimationFrame(frame); resolve(false); }, ${CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS}); frame = requestAnimationFrame(() => { frame = requestAnimationFrame(() => { clearTimeout(timer); resolve(true); }); }); })`, |
| 2787 | contextId: world.executionContextId, |
| 2788 | awaitPromise: true, |
| 2789 | returnByValue: true, |
| 2790 | })) as { result?: { value?: unknown }; exceptionDetails?: unknown } | undefined; |
| 2791 | if (response?.exceptionDetails || response?.result?.value !== true) |
| 2792 | return yield* new PreviewOperationError({ |
| 2793 | ...errorContext, |
| 2794 | cause: new Error("The preview paint boundary was not confirmed."), |
| 2795 | }); |
| 2796 | }); |
| 2797 | const paintReady = Effect.gen(function* () { |
| 2798 | // Only use an existing owned session; never attach or displace |
| 2799 | // another debugger to obtain a screenshot. |
| 2800 | const control = (yield* SynchronizedRef.get(controlSessionsRef)).get(wc.id); |
| 2801 | if (!control) |
| 2802 | return yield* new PreviewOperationError({ |
| 2803 | ...errorContext, |
| 2804 | cause: new Error("The preview paint session is unavailable."), |
| 2805 | }); |
| 2806 | return yield* probe((method, params) => |
| 2807 | attemptPromise(errorContext, () => control.debugger.sendCommand(method, params)), |
| 2808 | ); |
| 2809 | }).pipe( |
| 2810 | Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS), |
| 2811 | Effect.catch((error) => |
| 2812 | Effect.logWarning( |
| 2813 | "Preview paint readiness was not confirmed; capturing available pixels with mutation freshness unconfirmed.", |
| 2814 | { tabId, webContentsId: wc.id, error }, |
| 2815 | ), |
| 2816 | ), |
| 2817 | ); |
| 2818 | // Minimized guests can suspend frames even with throttling disabled. |
| 2819 | // A native capture can resume them; discard its pixels and read again |
| 2820 | // after the bounded paint probe. |
| 2821 | const warmup = capturePageWithRetry(errorContext, tabId, wc).pipe( |
| 2822 | Effect.catchTags({ |
| 2823 | PreviewOperationError: (error) => |
| 2824 | Effect.logWarning("Preview warmup capture failed; continuing with final capture.", { |
| 2825 | tabId, |
| 2826 | webContentsId: wc.id, |
| 2827 | error, |
| 2828 | }), |
| 2829 | }), |
| 2830 | ); |
| 2831 | yield* Effect.all([paintReady, warmup], { concurrency: 2, discard: true }); |
| 2832 | return yield* capturePageWithRetry(errorContext, tabId, wc); |
| 2833 | }), |
| 2834 | () => stopFrameCapture(tabId, consumer), |
| 2835 | ); |
| 2836 | }); |
| 2837 | |
| 2838 | const capturePreviewFrame = Effect.fn("PreviewManager.capturePreviewFrame")(function* ( |
| 2839 | tabId: string, |
| 2840 | ) { |
| 2841 | const captureSession = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); |
| 2842 | if (!captureSession?.consumers.has("picture-in-picture") || captureSession.scope === null) |
| 2843 | return; |
| 2844 | const wc = yield* requireWebContents(tabId); |
| 2845 | const captureContext = { operation: "frameCapture.capturePage", tabId, webContentsId: wc.id }; |
| 2846 | const image = yield* attemptPromise(captureContext, () => wc.capturePage()).pipe( |
| 2847 | Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS), |
| 2848 | Effect.catchTags({ |
| 2849 | TimeoutError: (cause) => |
| 2850 | Effect.fail(new PreviewOperationError({ ...captureContext, cause })), |
| 2851 | }), |
| 2852 | ); |
| 2853 | const currentCaptureSession = yield* Effect.all( |
| 2854 | [SynchronizedRef.get(frameCaptureSessionsRef), SynchronizedRef.get(tabsRef)], |
| 2855 | { concurrency: 2 }, |
| 2856 | ).pipe( |
| 2857 | Effect.map(([captureSessions, tabs]) => { |
| 2858 | const current = captureSessions.get(tabId); |
| 2859 | return current?.scope === captureSession.scope && |
| 2860 | tabs.get(tabId)?.webContentsId === wc.id && |
| 2861 | !wc.isDestroyed() |
| 2862 | ? current |
| 2863 | : undefined; |
| 2864 | }), |
| 2865 | ); |
| 2866 | if (!currentCaptureSession) return; |
| 2867 | const size = yield* attempt( |
| 2868 | { |
| 2869 | operation: "frameCapture.measureFrame", |
| 2870 | tabId, |
| 2871 | webContentsId: wc.id, |
| 2872 | }, |
| 2873 | () => image.getSize(), |
| 2874 | ); |
| 2875 | if ( |
| 2876 | !Number.isFinite(size.width) || |
| 2877 | !Number.isFinite(size.height) || |
| 2878 | size.width <= 0 || |
| 2879 | size.height <= 0 |
| 2880 | ) { |
| 2881 | return; |
| 2882 | } |
| 2883 | const frameSession = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); |
| 2884 | if (frameSession?.scope !== captureSession.scope) return; |
| 2885 | // Fingerprint before encoding so unchanged frames skip JPEG, base64, and IPC; unreadable frames must still be sent. |
| 2886 | const fingerprint = fingerprintCapturedFrame(image, size); |
| 2887 | const pictureInPicture = |
| 2888 | frameSession.consumers.has("picture-in-picture") && |
| 2889 | (fingerprint === null || frameSession.lastPictureInPictureFingerprint !== fingerprint); |
| 2890 | if (!pictureInPicture) return; |
| 2891 | const encoded = yield* attempt( |
| 2892 | { |
| 2893 | operation: "frameCapture.encodeFrame", |
| 2894 | tabId, |
| 2895 | webContentsId: wc.id, |
| 2896 | }, |
| 2897 | () => image.toJPEG(PICTURE_IN_PICTURE_JPEG_QUALITY), |
| 2898 | ); |
| 2899 | const receivedAt = yield* currentIso; |
| 2900 | const frame: DesktopPreviewRecordingFrame = { |
| 2901 | tabId, |
| 2902 | data: encoded.toString("base64"), |
| 2903 | width: size.width, |
| 2904 | height: size.height, |
| 2905 | receivedAt, |
| 2906 | }; |
| 2907 | const pictureInPictureWindow = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get( |
| 2908 | tabId, |
| 2909 | )?.window; |
| 2910 | if (!pictureInPictureWindow || pictureInPictureWindow.isDestroyed()) return; |
| 2911 | yield* Effect.gen(function* () { |
| 2912 | const previousAspectRatio = (yield* Ref.get(pictureInPictureAspectRatiosRef)).get(tabId); |
| 2913 | const aspectRatio = frame.width / frame.height; |
| 2914 | if ( |
| 2915 | previousAspectRatio === undefined || |
| 2916 | Math.abs(previousAspectRatio - aspectRatio) > PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON |
| 2917 | ) { |
| 2918 | yield* attempt( |
| 2919 | { |
| 2920 | operation: "pictureInPicture.setAspectRatio", |
| 2921 | tabId, |
| 2922 | webContentsId: wc.id, |
| 2923 | }, |
| 2924 | () => { |
| 2925 | const contentSize = fitPictureInPictureContentSize( |
| 2926 | pictureInPictureWindow.getContentSize(), |
| 2927 | aspectRatio, |
| 2928 | ); |
| 2929 | pictureInPictureWindow.setAspectRatio(0); |
| 2930 | pictureInPictureWindow.setContentSize(contentSize[0], contentSize[1], false); |
| 2931 | pictureInPictureWindow.setAspectRatio(aspectRatio); |
| 2932 | }, |
| 2933 | ); |
| 2934 | yield* Ref.update(pictureInPictureAspectRatiosRef, (aspectRatios) => |
| 2935 | replaceMap(aspectRatios, (copy) => { |
| 2936 | copy.set(tabId, aspectRatio); |
| 2937 | }), |
| 2938 | ); |
| 2939 | } |
| 2940 | yield* attempt( |
| 2941 | { |
| 2942 | operation: "pictureInPicture.deliverFrame", |
| 2943 | tabId, |
| 2944 | webContentsId: wc.id, |
| 2945 | }, |
| 2946 | () => { |
| 2947 | pictureInPictureWindow.webContents.send(PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL, frame); |
| 2948 | }, |
| 2949 | ); |
| 2950 | yield* SynchronizedRef.update(frameCaptureSessionsRef, (sessions) => { |
| 2951 | if (sessions.get(tabId) !== frameSession) return sessions; |
| 2952 | return replaceMap(sessions, (copy) => { |
| 2953 | copy.set(tabId, { |
| 2954 | ...frameSession, |
| 2955 | lastPictureInPictureFingerprint: fingerprint, |
| 2956 | }); |
| 2957 | }); |
| 2958 | }); |
| 2959 | }).pipe( |
| 2960 | Effect.catch((error) => |
| 2961 | Effect.logWarning("Picture-in-picture frame delivery failed.", { |
| 2962 | tabId, |
| 2963 | error, |
| 2964 | }), |
| 2965 | ), |
| 2966 | ); |
| 2967 | }); |
| 2968 | |
| 2969 | const startFrameCapture = Effect.fn("PreviewManager.startFrameCapture")(function* ( |
| 2970 | tabId: string, |
| 2971 | consumer: FrameCaptureConsumer, |
| 2972 | ) { |
| 2973 | const captureNextFrame = Effect.sleep(PICTURE_IN_PICTURE_FRAME_INTERVAL_MS).pipe( |
| 2974 | Effect.andThen(capturePreviewFrame(tabId)), |
| 2975 | Effect.catch((error) => |
| 2976 | Effect.logWarning("Background preview frame capture failed.", { |
| 2977 | tabId, |
| 2978 | error, |
| 2979 | }), |
| 2980 | ), |
| 2981 | ); |
| 2982 | const captureInitialFrame = yield* SynchronizedRef.modifyEffect( |
| 2983 | frameCaptureSessionsRef, |
| 2984 | (sessions) => { |
| 2985 | return Effect.gen(function* () { |
| 2986 | if (!frameCaptureWindowOpen) { |
| 2987 | return yield* new PreviewMainWindowClosedError({ tabId }); |
| 2988 | } |
| 2989 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 2990 | if (!tab || (yield* Ref.get(closingTabIdsRef)).has(tabId)) { |
| 2991 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 2992 | } |
| 2993 | const wc = yield* requireWebContents(tabId); |
| 2994 | const current = sessions.get(tabId); |
| 2995 | if (current) { |
| 2996 | if (current.consumers.has(consumer)) { |
| 2997 | return [false, sessions] as const; |
| 2998 | } |
| 2999 | if (!current.unthrottledWebContentsIds.has(wc.id)) { |
| 3000 | yield* setFrameCaptureWebContentsBackgroundThrottling(wc, false); |
| 3001 | } |
| 3002 | let scope = current.scope; |
| 3003 | if (consumer === "picture-in-picture" && scope === null) { |
| 3004 | scope = yield* Scope.fork(parentScope, "sequential"); |
| 3005 | yield* Effect.forkIn(Effect.forever(captureNextFrame), scope); |
| 3006 | } |
| 3007 | return [ |
| 3008 | consumer === "picture-in-picture", |
| 3009 | replaceMap(sessions, (copy) => { |
| 3010 | copy.set(tabId, { |
| 3011 | ...current, |
| 3012 | scope, |
| 3013 | consumers: new Set([...current.consumers, consumer]), |
| 3014 | unthrottledWebContentsIds: new Set([...current.unthrottledWebContentsIds, wc.id]), |
| 3015 | }); |
| 3016 | }), |
| 3017 | ] as const; |
| 3018 | } |
| 3019 | if (sessions.size === 0) { |
| 3020 | yield* setFrameCaptureBackgroundThrottling(false); |
| 3021 | } |
| 3022 | yield* setFrameCaptureWebContentsBackgroundThrottling(wc, false).pipe( |
| 3023 | Effect.onError(() => |
| 3024 | sessions.size === 0 |
| 3025 | ? setFrameCaptureBackgroundThrottling(true).pipe(Effect.ignore) |
| 3026 | : Effect.void, |
| 3027 | ), |
| 3028 | ); |
| 3029 | const scope = |
| 3030 | consumer === "picture-in-picture" ? yield* Scope.fork(parentScope, "sequential") : null; |
| 3031 | if (scope !== null) { |
| 3032 | yield* Effect.forkIn(Effect.forever(captureNextFrame), scope); |
| 3033 | } |
| 3034 | return [ |
| 3035 | consumer === "picture-in-picture", |
| 3036 | replaceMap(sessions, (copy) => { |
| 3037 | copy.set(tabId, { |
| 3038 | scope, |
| 3039 | consumers: new Set([consumer]), |
| 3040 | unthrottledWebContentsIds: new Set([wc.id]), |
| 3041 | lastPictureInPictureFingerprint: null, |
| 3042 | }); |
| 3043 | }), |
| 3044 | ] as const; |
| 3045 | }); |
| 3046 | }, |
| 3047 | ).pipe(Effect.uninterruptible); |
| 3048 | if (!captureInitialFrame) return; |
| 3049 | yield* capturePreviewFrame(tabId).pipe( |
| 3050 | Effect.catch((error) => |
| 3051 | Effect.logWarning("Initial background preview frame was not ready; capture will retry.", { |
| 3052 | tabId, |
| 3053 | consumer, |
| 3054 | error, |
| 3055 | }), |
| 3056 | ), |
| 3057 | ); |
| 3058 | }); |
| 3059 | |
| 3060 | const releasePictureInPicture = Effect.fn("PreviewManager.releasePictureInPicture")(function* ( |
| 3061 | tabId: string, |
| 3062 | expectedSession: PictureInPictureSession, |
| 3063 | closeWindow: boolean, |
| 3064 | ) { |
| 3065 | const removed = yield* SynchronizedRef.modify(pictureInPictureSessionsRef, (sessions) => { |
| 3066 | if (sessions.get(tabId) !== expectedSession) { |
| 3067 | return [false, sessions] as const; |
| 3068 | } |
| 3069 | return [ |
| 3070 | true, |
| 3071 | replaceMap(sessions, (copy) => { |
| 3072 | copy.delete(tabId); |
| 3073 | }), |
| 3074 | ] as const; |
| 3075 | }); |
| 3076 | if (!removed) return; |
| 3077 | yield* Deferred.interrupt(expectedSession.ready); |
| 3078 | yield* Scope.close(expectedSession.initializationScope, Exit.void).pipe(Effect.ignore); |
| 3079 | yield* Ref.update(pictureInPictureAspectRatiosRef, (aspectRatios) => |
| 3080 | replaceMap(aspectRatios, (copy) => { |
| 3081 | copy.delete(tabId); |
| 3082 | }), |
| 3083 | ); |
| 3084 | yield* stopFrameCapture(tabId, "picture-in-picture"); |
| 3085 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 3086 | if (tabs.has(tabId)) { |
| 3087 | yield* update(tabId, { pictureInPicture: false }); |
| 3088 | } |
| 3089 | if (closeWindow && !expectedSession.window.isDestroyed()) { |
| 3090 | yield* attempt({ operation: "pictureInPicture.close", tabId }, () => |
| 3091 | expectedSession.window.close(), |
| 3092 | ).pipe(Effect.ignore); |
| 3093 | } |
| 3094 | }); |
| 3095 | |
| 3096 | const closePictureInPictureUnlocked = Effect.fn("PreviewManager.closePictureInPictureUnlocked")( |
| 3097 | function* (tabId: string) { |
| 3098 | const pictureInPictureSession = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get( |
| 3099 | tabId, |
| 3100 | ); |
| 3101 | if (!pictureInPictureSession) { |
| 3102 | yield* stopFrameCapture(tabId, "picture-in-picture"); |
| 3103 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 3104 | if (tabs.has(tabId)) { |
| 3105 | yield* update(tabId, { pictureInPicture: false }); |
| 3106 | } |
| 3107 | return; |
| 3108 | } |
| 3109 | yield* releasePictureInPicture(tabId, pictureInPictureSession, true); |
| 3110 | }, |
| 3111 | ); |
| 3112 | |
| 3113 | const closePictureInPicture = Effect.fn("PreviewManager.closePictureInPicture")(function* ( |
| 3114 | tabId: string, |
| 3115 | ) { |
| 3116 | yield* pictureInPictureMutationSemaphore.withPermit(closePictureInPictureUnlocked(tabId)); |
| 3117 | }); |
| 3118 | |
| 3119 | const closeAllPictureInPicture = Effect.fn("PreviewManager.closeAllPictureInPicture")( |
| 3120 | function* () { |
| 3121 | const sessions = yield* SynchronizedRef.get(pictureInPictureSessionsRef); |
| 3122 | yield* Effect.forEach(sessions.keys(), closePictureInPicture, { |
| 3123 | concurrency: "unbounded", |
| 3124 | discard: true, |
| 3125 | }); |
| 3126 | }, |
| 3127 | ); |
| 3128 | |
| 3129 | const openPictureInPicture = Effect.fn("PreviewManager.openPictureInPicture")(function* ( |
| 3130 | tabId: string, |
| 3131 | ) { |
| 3132 | const claim = yield* pictureInPictureMutationSemaphore.withPermit( |
| 3133 | Effect.gen(function* () { |
| 3134 | const existing = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(tabId); |
| 3135 | if (existing && !existing.window.isDestroyed()) { |
| 3136 | return { kind: "existing" as const, session: existing }; |
| 3137 | } |
| 3138 | if (existing) { |
| 3139 | yield* releasePictureInPicture(tabId, existing, false); |
| 3140 | } |
| 3141 | const wc = yield* requireWebContents(tabId); |
| 3142 | const title = yield* attempt( |
| 3143 | { |
| 3144 | operation: "pictureInPicture.readTitle", |
| 3145 | tabId, |
| 3146 | webContentsId: wc.id, |
| 3147 | }, |
| 3148 | () => wc.getTitle().trim(), |
| 3149 | ); |
| 3150 | const pictureInPictureWindow = yield* attempt( |
| 3151 | { |
| 3152 | operation: "pictureInPicture.create", |
| 3153 | tabId, |
| 3154 | webContentsId: wc.id, |
| 3155 | }, |
| 3156 | () => |
| 3157 | new BrowserWindow({ |
| 3158 | width: PICTURE_IN_PICTURE_INITIAL_WIDTH, |
| 3159 | height: PICTURE_IN_PICTURE_INITIAL_HEIGHT, |
| 3160 | minWidth: PICTURE_IN_PICTURE_MIN_WIDTH, |
| 3161 | minHeight: PICTURE_IN_PICTURE_MIN_HEIGHT, |
| 3162 | title: |
| 3163 | title.length > 0 |
| 3164 | ? m(msg("Preview · {title}"), { title }) |
| 3165 | : m(msg("Browser preview")), |
| 3166 | show: false, |
| 3167 | alwaysOnTop: true, |
| 3168 | autoHideMenuBar: true, |
| 3169 | fullscreenable: false, |
| 3170 | maximizable: false, |
| 3171 | minimizable: false, |
| 3172 | resizable: true, |
| 3173 | skipTaskbar: true, |
| 3174 | backgroundColor: "#111111", |
| 3175 | ...(hostPlatform === "darwin" ? { type: "panel" as const } : {}), |
| 3176 | webPreferences: { |
| 3177 | preload: pictureInPicturePreloadPath, |
| 3178 | backgroundThrottling: false, |
| 3179 | contextIsolation: true, |
| 3180 | nodeIntegration: false, |
| 3181 | sandbox: true, |
| 3182 | }, |
| 3183 | }), |
| 3184 | ); |
| 3185 | yield* rendererHistory.register(pictureInPictureWindow.webContents, { |
| 3186 | surface: "picture-in-picture", |
| 3187 | tabId, |
| 3188 | }); |
| 3189 | const initializationScope = yield* Scope.fork(parentScope, "sequential"); |
| 3190 | const ready = yield* Deferred.make<void, PreviewManagerError>(); |
| 3191 | const session: PictureInPictureSession = { |
| 3192 | window: pictureInPictureWindow, |
| 3193 | webContentsId: wc.id, |
| 3194 | ready, |
| 3195 | initializationScope, |
| 3196 | }; |
| 3197 | const onClosed = () => { |
| 3198 | runFork( |
| 3199 | pictureInPictureMutationSemaphore.withPermit( |
| 3200 | releasePictureInPicture(tabId, session, false), |
| 3201 | ), |
| 3202 | ); |
| 3203 | }; |
| 3204 | const onDidFinishLoad = () => { |
| 3205 | runFork( |
| 3206 | SynchronizedRef.update(frameCaptureSessionsRef, (sessions) => { |
| 3207 | const current = sessions.get(tabId); |
| 3208 | if (!current?.consumers.has("picture-in-picture")) return sessions; |
| 3209 | return replaceMap(sessions, (copy) => { |
| 3210 | copy.set(tabId, { ...current, lastPictureInPictureFingerprint: null }); |
| 3211 | }); |
| 3212 | }), |
| 3213 | ); |
| 3214 | }; |
| 3215 | const pipWebContents = pictureInPictureWindow.webContents; |
| 3216 | yield* attempt( |
| 3217 | { |
| 3218 | operation: "pictureInPicture.configure", |
| 3219 | tabId, |
| 3220 | webContentsId: wc.id, |
| 3221 | }, |
| 3222 | () => { |
| 3223 | pictureInPictureWindow.once("closed", onClosed); |
| 3224 | pictureInPictureWindow.setAlwaysOnTop( |
| 3225 | true, |
| 3226 | hostPlatform === "darwin" ? "floating" : "normal", |
| 3227 | ); |
| 3228 | if (hostPlatform === "darwin") { |
| 3229 | pictureInPictureWindow.setVisibleOnAllWorkspaces(true, { |
| 3230 | visibleOnFullScreen: true, |
| 3231 | // Electron's temporary UIElement transformation would remove the app from the Dock. |
| 3232 | skipTransformProcessType: true, |
| 3233 | }); |
| 3234 | } |
| 3235 | pipWebContents.on("did-finish-load", onDidFinishLoad); |
| 3236 | }, |
| 3237 | ).pipe( |
| 3238 | Effect.onError(() => |
| 3239 | Effect.all( |
| 3240 | [ |
| 3241 | Scope.close(initializationScope, Exit.void).pipe(Effect.ignore), |
| 3242 | attempt({ operation: "pictureInPicture.close", tabId }, () => |
| 3243 | pictureInPictureWindow.close(), |
| 3244 | ).pipe(Effect.ignore), |
| 3245 | ], |
| 3246 | { discard: true }, |
| 3247 | ), |
| 3248 | ), |
| 3249 | ); |
| 3250 | yield* Scope.addFinalizer( |
| 3251 | initializationScope, |
| 3252 | Effect.sync(() => { |
| 3253 | pipWebContents.off("did-finish-load", onDidFinishLoad); |
| 3254 | }).pipe(Effect.ignore), |
| 3255 | ); |
| 3256 | yield* SynchronizedRef.update(pictureInPictureSessionsRef, (sessions) => |
| 3257 | replaceMap(sessions, (copy) => { |
| 3258 | copy.set(tabId, session); |
| 3259 | }), |
| 3260 | ); |
| 3261 | return { kind: "created" as const, session }; |
| 3262 | }), |
| 3263 | ); |
| 3264 | const pictureInPictureSession = claim.session; |
| 3265 | if (claim.kind === "existing") { |
| 3266 | yield* Deferred.await(pictureInPictureSession.ready); |
| 3267 | return yield* pictureInPictureMutationSemaphore.withPermit( |
| 3268 | Effect.gen(function* () { |
| 3269 | const current = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(tabId); |
| 3270 | if (current !== pictureInPictureSession || pictureInPictureSession.window.isDestroyed()) { |
| 3271 | return yield* new PreviewOperationError({ |
| 3272 | operation: "pictureInPicture.showExisting", |
| 3273 | tabId, |
| 3274 | webContentsId: pictureInPictureSession.webContentsId, |
| 3275 | cause: new Error("Picture-in-picture session closed before it became visible."), |
| 3276 | }); |
| 3277 | } |
| 3278 | yield* attempt( |
| 3279 | { |
| 3280 | operation: "pictureInPicture.showExisting", |
| 3281 | tabId, |
| 3282 | webContentsId: pictureInPictureSession.webContentsId, |
| 3283 | }, |
| 3284 | () => pictureInPictureSession.window.showInactive(), |
| 3285 | ); |
| 3286 | }), |
| 3287 | ); |
| 3288 | } |
| 3289 | |
| 3290 | const initialize = Effect.gen(function* () { |
| 3291 | yield* attemptPromise( |
| 3292 | { |
| 3293 | operation: "pictureInPicture.load", |
| 3294 | tabId, |
| 3295 | webContentsId: pictureInPictureSession.webContentsId, |
| 3296 | }, |
| 3297 | () => pictureInPictureSession.window.loadURL(buildPreviewPictureInPictureDataUrl()), |
| 3298 | ); |
| 3299 | const currentWebContents = yield* requireWebContents(tabId); |
| 3300 | if ( |
| 3301 | currentWebContents.id !== pictureInPictureSession.webContentsId || |
| 3302 | currentWebContents.isDestroyed() |
| 3303 | ) { |
| 3304 | return yield* new PreviewOperationError({ |
| 3305 | operation: "pictureInPicture.validateWebContents", |
| 3306 | tabId, |
| 3307 | webContentsId: pictureInPictureSession.webContentsId, |
| 3308 | cause: new Error("Preview webview changed while picture-in-picture was opening."), |
| 3309 | }); |
| 3310 | } |
| 3311 | yield* startFrameCapture(tabId, "picture-in-picture"); |
| 3312 | yield* attempt( |
| 3313 | { |
| 3314 | operation: "pictureInPicture.show", |
| 3315 | tabId, |
| 3316 | webContentsId: pictureInPictureSession.webContentsId, |
| 3317 | }, |
| 3318 | () => pictureInPictureSession.window.showInactive(), |
| 3319 | ); |
| 3320 | }); |
| 3321 | const initializationExit = yield* Effect.gen(function* () { |
| 3322 | const initializationFiber = yield* Effect.forkIn( |
| 3323 | initialize, |
| 3324 | pictureInPictureSession.initializationScope, |
| 3325 | ); |
| 3326 | return yield* Fiber.await(initializationFiber); |
| 3327 | }).pipe( |
| 3328 | Effect.onInterrupt(() => |
| 3329 | pictureInPictureMutationSemaphore.withPermit( |
| 3330 | releasePictureInPicture(tabId, pictureInPictureSession, true), |
| 3331 | ), |
| 3332 | ), |
| 3333 | ); |
| 3334 | if (Exit.isSuccess(initializationExit)) { |
| 3335 | const published = yield* pictureInPictureMutationSemaphore.withPermit( |
| 3336 | Effect.gen(function* () { |
| 3337 | const current = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(tabId); |
| 3338 | if (current !== pictureInPictureSession || pictureInPictureSession.window.isDestroyed()) { |
| 3339 | if (current === pictureInPictureSession) { |
| 3340 | yield* releasePictureInPicture(tabId, pictureInPictureSession, false); |
| 3341 | } |
| 3342 | return false; |
| 3343 | } |
| 3344 | yield* update(tabId, { pictureInPicture: true }); |
| 3345 | yield* Deferred.done(pictureInPictureSession.ready, initializationExit); |
| 3346 | return true; |
| 3347 | }), |
| 3348 | ); |
| 3349 | if (published) return; |
| 3350 | return yield* Deferred.await(pictureInPictureSession.ready); |
| 3351 | } |
| 3352 | yield* Deferred.done(pictureInPictureSession.ready, initializationExit); |
| 3353 | const current = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(tabId); |
| 3354 | if (current === pictureInPictureSession) { |
| 3355 | yield* pictureInPictureMutationSemaphore.withPermit( |
| 3356 | releasePictureInPicture(tabId, pictureInPictureSession, true), |
| 3357 | ); |
| 3358 | } |
| 3359 | return yield* Effect.failCause(initializationExit.cause); |
| 3360 | }); |
| 3361 | |
| 3362 | const clearPendingRecording = (tabId: string) => { |
| 3363 | if (pendingRecording?.tabId === tabId) pendingRecording = null; |
| 3364 | }; |
| 3365 | |
| 3366 | /** Display-media requests lack tab identity; only one tab may arm a recording to prevent stream redirection. */ |
| 3367 | const armPendingRecording = Effect.fn("PreviewManager.armPendingRecording")(function* ( |
| 3368 | tabId: string, |
| 3369 | wc: Electron.WebContents, |
| 3370 | requestingFrameTreeNodeId: number, |
| 3371 | ) { |
| 3372 | const now = yield* Clock.currentTimeMillis; |
| 3373 | const previous = pendingRecording; |
| 3374 | if ( |
| 3375 | previous !== null && |
| 3376 | previous.tabId !== tabId && |
| 3377 | !previous.webContents.isDestroyed() && |
| 3378 | now - previous.armedAtMillis < RECORDING_ARM_GRACE_MS |
| 3379 | ) { |
| 3380 | return yield* new PreviewRecordingArmConflictError({ |
| 3381 | tabId, |
| 3382 | webContentsId: wc.id, |
| 3383 | armedTabId: previous.tabId, |
| 3384 | }); |
| 3385 | } |
| 3386 | const armed: PendingRecording = { |
| 3387 | tabId, |
| 3388 | webContents: wc, |
| 3389 | requestingFrameTreeNodeId, |
| 3390 | armedAtMillis: now, |
| 3391 | }; |
| 3392 | pendingRecording = armed; |
| 3393 | // The synchronous callback cannot read a clock. Compare identity so expiry cannot clobber a re-arm. |
| 3394 | yield* Effect.forkIn( |
| 3395 | Effect.sleep(RECORDING_ARM_GRACE_MS).pipe( |
| 3396 | Effect.andThen( |
| 3397 | Effect.sync(() => { |
| 3398 | if (pendingRecording === armed) pendingRecording = null; |
| 3399 | }), |
| 3400 | ), |
| 3401 | ), |
| 3402 | parentScope, |
| 3403 | ); |
| 3404 | }); |
| 3405 | |
| 3406 | // Only serve the tab armed by startRecording; pages must not capture independently. |
| 3407 | const installDisplayMediaRequestHandler = (session: Session) => { |
| 3408 | if (displayMediaHandlerSessions.has(session)) return; |
| 3409 | displayMediaHandlerSessions.add(session); |
| 3410 | session.setDisplayMediaRequestHandler((request, callback) => { |
| 3411 | const armed = pendingRecording; |
| 3412 | if (!armed) { |
| 3413 | callback({}); |
| 3414 | return; |
| 3415 | } |
| 3416 | if (armed.webContents.isDestroyed()) { |
| 3417 | pendingRecording = null; |
| 3418 | callback({}); |
| 3419 | return; |
| 3420 | } |
| 3421 | if (request.frame?.frameTreeNodeId !== armed.requestingFrameTreeNodeId) { |
| 3422 | callback({}); |
| 3423 | return; |
| 3424 | } |
| 3425 | pendingRecording = null; |
| 3426 | callback({ video: armed.webContents.mainFrame }); |
| 3427 | }); |
| 3428 | }; |
| 3429 | |
| 3430 | const startRecording = Effect.fn("PreviewManager.startRecording")(function* ( |
| 3431 | tabId: string, |
| 3432 | options: RecordingInputOptions = DEFAULT_RECORDING_INPUT_OPTIONS, |
| 3433 | ) { |
| 3434 | if ((yield* Ref.get(closingTabIdsRef)).has(tabId)) { |
| 3435 | return yield* new PreviewTabNotFoundError({ tabId }); |
| 3436 | } |
| 3437 | return yield* withTabLifecycleLock( |
| 3438 | tabId, |
| 3439 | Effect.gen(function* () { |
| 3440 | yield* startFrameCapture(tabId, "recording"); |
| 3441 | yield* SynchronizedRef.update(frameCaptureSessionsRef, (sessions) => |
| 3442 | replaceMap(sessions, (copy) => { |
| 3443 | const current = copy.get(tabId); |
| 3444 | if (current) copy.set(tabId, { ...current, recordingInputOptions: options }); |
| 3445 | }), |
| 3446 | ); |
| 3447 | const wc = yield* requireWebContents(tabId); |
| 3448 | const requestWebContents = wc.hostWebContents; |
| 3449 | if (requestWebContents === null) { |
| 3450 | return yield* new PreviewMainWindowClosedError({ tabId }); |
| 3451 | } |
| 3452 | const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); |
| 3453 | yield* attempt({ operation: "recording.cursor", tabId, webContentsId: wc.id }, () => |
| 3454 | wc.send(RECORDING_CURSOR_CHANNEL, true, options, tab?.controller), |
| 3455 | ); |
| 3456 | const warmupContext = { operation: "recording.warmSource", tabId, webContentsId: wc.id }; |
| 3457 | yield* Effect.tryPromise({ |
| 3458 | // Electron cannot cancel capturePage, but a stalled warmup must release the tab lock. |
| 3459 | try: (_signal) => wc.capturePage(), |
| 3460 | catch: (cause) => new PreviewOperationError({ ...warmupContext, cause }), |
| 3461 | }).pipe( |
| 3462 | Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS), |
| 3463 | Effect.catchTags({ |
| 3464 | TimeoutError: (cause) => |
| 3465 | Effect.fail(new PreviewOperationError({ ...warmupContext, cause })), |
| 3466 | }), |
| 3467 | Effect.retry({ times: 1 }), |
| 3468 | Effect.ignore, |
| 3469 | ); |
| 3470 | const currentWebContents = yield* requireWebContents(tabId); |
| 3471 | if (currentWebContents !== wc || wc.isDestroyed()) { |
| 3472 | return yield* new PreviewWebContentsNotFoundError({ |
| 3473 | tabId, |
| 3474 | webContentsId: wc.id, |
| 3475 | }); |
| 3476 | } |
| 3477 | if (!frameCaptureWindowOpen || requestWebContents.isDestroyed()) { |
| 3478 | return yield* new PreviewMainWindowClosedError({ tabId }); |
| 3479 | } |
| 3480 | installDisplayMediaRequestHandler(requestWebContents.session); |
| 3481 | yield* armPendingRecording(tabId, wc, requestWebContents.mainFrame.frameTreeNodeId); |
| 3482 | const captureRequested = yield* attemptPromise( |
| 3483 | { |
| 3484 | operation: "recording.requestCapture", |
| 3485 | tabId, |
| 3486 | webContentsId: requestWebContents.id, |
| 3487 | }, |
| 3488 | () => |
| 3489 | requestWebContents.executeJavaScript(requestRecordingCaptureExpression(tabId), true), |
| 3490 | ); |
| 3491 | if (captureRequested !== true) { |
| 3492 | return yield* new PreviewRecordingCaptureUnavailableError({ |
| 3493 | tabId, |
| 3494 | webContentsId: requestWebContents.id, |
| 3495 | }); |
| 3496 | } |
| 3497 | }).pipe( |
| 3498 | Effect.onError(() => { |
| 3499 | clearPendingRecording(tabId); |
| 3500 | return stopFrameCapture(tabId, "recording").pipe(Effect.ignore); |
| 3501 | }), |
| 3502 | ), |
| 3503 | ); |
| 3504 | }); |
| 3505 | |
| 3506 | const stopRecording = Effect.fn("PreviewManager.stopRecording")(function* (tabId: string) { |
| 3507 | // Clearing runs under the tab lock so it cannot land before an in-flight start arms. |
| 3508 | yield* withTabLifecycleLock( |
| 3509 | tabId, |
| 3510 | Effect.suspend(() => { |
| 3511 | clearPendingRecording(tabId); |
| 3512 | return stopFrameCapture(tabId, "recording"); |
| 3513 | }), |
| 3514 | ); |
| 3515 | }); |
| 3516 | |
| 3517 | const saveRecording = Effect.fn("PreviewManager.saveRecording")(function* ( |
| 3518 | tabId: string, |
| 3519 | mimeType: string, |
| 3520 | data: Uint8Array, |
| 3521 | ) { |
| 3522 | const [createdAt, millis] = yield* Effect.all([currentIso, currentMillis]); |
| 3523 | const uuid = yield* random.uuidV4(); |
| 3524 | const id = `browser-recording-${millis.toString(36)}-${uuid.slice(0, 8)}`; |
| 3525 | const extension = recordingFileExtension(mimeType); |
| 3526 | const artifactPath = path.join(resolvedArtifactDirectory, `${id}.${extension}`); |
| 3527 | yield* fileSystem.makeDirectory(resolvedArtifactDirectory, { recursive: true }).pipe( |
| 3528 | Effect.mapError( |
| 3529 | (cause) => |
| 3530 | new PreviewOperationError({ |
| 3531 | operation: "saveRecording.makeDirectory", |
| 3532 | tabId, |
| 3533 | artifactPath, |
| 3534 | cause, |
| 3535 | }), |
| 3536 | ), |
| 3537 | ); |
| 3538 | yield* fileSystem.writeFile(artifactPath, data, { flag: "wx" }).pipe( |
| 3539 | Effect.mapError( |
| 3540 | (cause) => |
| 3541 | new PreviewOperationError({ |
| 3542 | operation: "saveRecording.writeFile", |
| 3543 | tabId, |
| 3544 | artifactPath, |
| 3545 | cause, |
| 3546 | }), |
| 3547 | ), |
| 3548 | ); |
| 3549 | return { |
| 3550 | id, |
| 3551 | tabId, |
| 3552 | path: artifactPath, |
| 3553 | mimeType, |
| 3554 | sizeBytes: data.byteLength, |
| 3555 | createdAt, |
| 3556 | }; |
| 3557 | }); |
| 3558 | |
| 3559 | const revealArtifact = Effect.fn("PreviewManager.revealArtifact")(function* ( |
| 3560 | artifactPath: string, |
| 3561 | ) { |
| 3562 | const resolvedPath = yield* resolveArtifactPath(artifactPath); |
| 3563 | yield* attempt({ operation: "revealArtifact", artifactPath: resolvedPath }, () => |
| 3564 | shell.showItemInFolder(resolvedPath), |
| 3565 | ); |
| 3566 | }); |
| 3567 | |
| 3568 | const copyArtifactToClipboard = Effect.fn("PreviewManager.copyArtifactToClipboard")(function* ( |
| 3569 | artifactPath: string, |
| 3570 | ) { |
| 3571 | const resolvedPath = yield* resolveArtifactPath(artifactPath); |
| 3572 | const image = yield* attempt( |
| 3573 | { operation: "copyArtifactToClipboard.load", artifactPath: resolvedPath }, |
| 3574 | () => nativeImage.createFromPath(resolvedPath), |
| 3575 | ); |
| 3576 | if (image.isEmpty()) { |
| 3577 | return yield* new PreviewArtifactImageLoadError({ artifactPath: resolvedPath }); |
| 3578 | } |
| 3579 | yield* attemptPromise( |
| 3580 | { operation: "copyArtifactToClipboard.write", artifactPath: resolvedPath }, |
| 3581 | () => |
| 3582 | clipboard.write([ |
| 3583 | new ClipboardItem({ |
| 3584 | "image/png": new Blob([Uint8Array.from(image.toPNG())], { type: "image/png" }), |
| 3585 | }), |
| 3586 | ]), |
| 3587 | ); |
| 3588 | }); |
| 3589 | |
| 3590 | let pointerSequence = 0; |
| 3591 | const emitAgentPointer = Effect.fn("PreviewManager.emitAgentPointer")(function* (pointer: { |
| 3592 | readonly key: { readonly threadId: string; readonly tabId: string }; |
| 3593 | readonly phase: "move" | "click"; |
| 3594 | readonly x: number; |
| 3595 | readonly y: number; |
| 3596 | }) { |
| 3597 | const tab = [...(yield* SynchronizedRef.get(tabsRef)).values()].find( |
| 3598 | (candidate) => |
| 3599 | candidate.serverTab?.threadId === pointer.key.threadId && |
| 3600 | candidate.serverTab.tabId === pointer.key.tabId, |
| 3601 | ); |
| 3602 | if (!tab) return; |
| 3603 | const event: DesktopPreviewPointerEvent = { |
| 3604 | tabId: tab.tabId, |
| 3605 | phase: pointer.phase, |
| 3606 | x: pointer.x, |
| 3607 | y: pointer.y, |
| 3608 | sequence: ++pointerSequence, |
| 3609 | createdAt: yield* currentIso, |
| 3610 | }; |
| 3611 | const recording = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tab.tabId); |
| 3612 | const webContentsId = tab.webContentsId; |
| 3613 | if (recording?.consumers.has("recording") && webContentsId != null) { |
| 3614 | yield* attempt({ operation: "recording.pointer", tabId: tab.tabId }, () => { |
| 3615 | const contents = webContents.fromId(webContentsId); |
| 3616 | if (contents && !contents.isDestroyed()) contents.send(RECORDING_POINTER_CHANNEL, event); |
| 3617 | }).pipe(Effect.ignore); |
| 3618 | } |
| 3619 | const listeners = yield* Ref.get(pointerEventListenersRef); |
| 3620 | yield* Effect.forEach( |
| 3621 | listeners, |
| 3622 | (listener) => deliverEvent("pointer-event", tab.tabId, () => listener(event)), |
| 3623 | { discard: true }, |
| 3624 | ); |
| 3625 | }); |
| 3626 | // A hidden or locked window draws no frames, so a guest it hid stays unpainted until unthrottled. |
| 3627 | const serverCaptureLeases = new Map<string, { readonly tabId: string; readonly lease: symbol }>(); |
| 3628 | const updateServerTabCapturing = Effect.fn("PreviewManager.updateServerTabCapturing")( |
| 3629 | function* (painting: { |
| 3630 | readonly key: { readonly threadId: string; readonly tabId: string }; |
| 3631 | readonly active: boolean; |
| 3632 | }) { |
| 3633 | const tab = [...(yield* SynchronizedRef.get(tabsRef)).values()].find( |
| 3634 | (candidate) => |
| 3635 | candidate.serverTab?.threadId === painting.key.threadId && |
| 3636 | candidate.serverTab.tabId === painting.key.tabId, |
| 3637 | ); |
| 3638 | const key = `${painting.key.threadId}\u0000${painting.key.tabId}`; |
| 3639 | const held = serverCaptureLeases.get(key); |
| 3640 | if (!painting.active && held) { |
| 3641 | serverCaptureLeases.delete(key); |
| 3642 | yield* stopFrameCapture(held.tabId, held.lease); |
| 3643 | } else if (painting.active && !held && tab) { |
| 3644 | const lease = Symbol("server-capture"); |
| 3645 | yield* startFrameCapture(tab.tabId, lease).pipe( |
| 3646 | Effect.andThen( |
| 3647 | Effect.sync(() => serverCaptureLeases.set(key, { tabId: tab.tabId, lease })), |
| 3648 | ), |
| 3649 | Effect.catch((error) => |
| 3650 | Effect.logWarning("Failed to unthrottle a server tab for its capture.", { |
| 3651 | tabId: tab.tabId, |
| 3652 | error, |
| 3653 | }), |
| 3654 | ), |
| 3655 | ); |
| 3656 | } |
| 3657 | if (tab && tab.capturing !== painting.active) { |
| 3658 | yield* update(tab.tabId, { capturing: painting.active }); |
| 3659 | } |
| 3660 | }, |
| 3661 | ); |
| 3662 | let browserHostStarted = false; |
| 3663 | // Subscribes before the attach that follows, so no event falls between the two. |
| 3664 | const listenToBrowserHost = Effect.suspend(() => { |
| 3665 | if (browserHostStarted) return Effect.void; |
| 3666 | browserHostStarted = true; |
| 3667 | const listen = Effect.forkIn(parentScope, { startImmediately: true }); |
| 3668 | return Effect.all( |
| 3669 | [ |
| 3670 | browserHost.pointers.pipe(Stream.runForEach(emitAgentPointer), listen), |
| 3671 | browserHost.painting.pipe(Stream.runForEach(updateServerTabCapturing), listen), |
| 3672 | ], |
| 3673 | { discard: true }, |
| 3674 | ); |
| 3675 | }); |
| 3676 | |
| 3677 | const emitOpenLink = Effect.fn("PreviewManager.emitOpenLink")(function* ( |
| 3678 | event: DesktopPreviewOpenLinkEvent, |
| 3679 | ) { |
| 3680 | const listeners = yield* Ref.get(openLinkListenersRef); |
| 3681 | yield* Effect.forEach( |
| 3682 | listeners, |
| 3683 | (listener) => deliverEvent("open-link", event.tabId, () => listener(event)), |
| 3684 | { discard: true }, |
| 3685 | ); |
| 3686 | }); |
| 3687 | const emitFindResult = Effect.fn("PreviewManager.emitFindResult")(function* ( |
| 3688 | event: DesktopPreviewFindResultEvent, |
| 3689 | ) { |
| 3690 | const listeners = yield* Ref.get(findResultListenersRef); |
| 3691 | yield* Effect.forEach( |
| 3692 | listeners, |
| 3693 | (listener) => deliverEvent("find-result", event.tabId, () => listener(event)), |
| 3694 | { discard: true }, |
| 3695 | ); |
| 3696 | }); |
| 3697 | const subscribe = <A>( |
| 3698 | ref: Ref.Ref<ReadonlySet<A>>, |
| 3699 | listener: A, |
| 3700 | ): Effect.Effect<void, never, Scope.Scope> => |
| 3701 | Effect.acquireRelease( |
| 3702 | Ref.update(ref, (listeners) => new Set([...listeners, listener])), |
| 3703 | () => |
| 3704 | Ref.update(ref, (listeners) => { |
| 3705 | const next = new Set(listeners); |
| 3706 | next.delete(listener); |
| 3707 | return next; |
| 3708 | }), |
| 3709 | ).pipe(Effect.asVoid); |
| 3710 | |
| 3711 | const destroy = Effect.fn("PreviewManager.destroy")(function* () { |
| 3712 | const tabs = yield* SynchronizedRef.get(tabsRef); |
| 3713 | yield* Effect.forEach(tabs.keys(), closeTab, { discard: true }); |
| 3714 | yield* Effect.all( |
| 3715 | [ |
| 3716 | Ref.set(listenersRef, new Set()), |
| 3717 | Ref.set(pointerEventListenersRef, new Set()), |
| 3718 | Ref.set(openLinkListenersRef, new Set()), |
| 3719 | Ref.set(findResultListenersRef, new Set()), |
| 3720 | Ref.set(recordingInputListenersRef, new Set()), |
| 3721 | ], |
| 3722 | { discard: true }, |
| 3723 | ); |
| 3724 | }); |
| 3725 | |
| 3726 | yield* Effect.addFinalizer(() => destroy().pipe(Effect.ignore)); |
| 3727 | |
| 3728 | return { |
| 3729 | cancelPickElement, |
| 3730 | captureScreenshot, |
| 3731 | closeTab, |
| 3732 | copyArtifactToClipboard, |
| 3733 | createTab, |
| 3734 | findInPage, |
| 3735 | goBack, |
| 3736 | goForward, |
| 3737 | hardReload, |
| 3738 | navigate, |
| 3739 | openPictureInPicture, |
| 3740 | openDevTools, |
| 3741 | pickElement, |
| 3742 | updateAnnotationVoice, |
| 3743 | prepareWebview, |
| 3744 | reapplyZoom, |
| 3745 | refresh, |
| 3746 | stopLoading, |
| 3747 | registerWebview, |
| 3748 | resetZoom: (tabId: string) => applyZoom(tabId, () => DEFAULT_ZOOM_FACTOR), |
| 3749 | setZoomFactor: (tabId: string, zoomFactor: number) => |
| 3750 | applyZoom(tabId, () => normalizeZoomFactor(zoomFactor)), |
| 3751 | revealArtifact, |
| 3752 | saveRecording, |
| 3753 | setAnnotationTheme, |
| 3754 | setAnnotationSendEnabled, |
| 3755 | setAudioMuted, |
| 3756 | setShown, |
| 3757 | setCaptureDrawn, |
| 3758 | setColorScheme, |
| 3759 | setMainWindow, |
| 3760 | setForwardedShortcuts: (shortcuts: ReadonlyArray<PreviewForwardedShortcut>) => |
| 3761 | Effect.sync(() => { |
| 3762 | forwardedShortcuts = shortcuts; |
| 3763 | }), |
| 3764 | startRecording, |
| 3765 | closePictureInPicture, |
| 3766 | stopFindInPage, |
| 3767 | stopRecording, |
| 3768 | subscribePointerEvents: (listener: PointerEventListener) => |
| 3769 | subscribe(pointerEventListenersRef, listener), |
| 3770 | subscribeOpenLinks: (listener: OpenLinkListener) => subscribe(openLinkListenersRef, listener), |
| 3771 | subscribeFindResults: (listener: FindResultListener) => |
| 3772 | subscribe(findResultListenersRef, listener), |
| 3773 | subscribeRecordingInputs: (listener: RecordingInputListener) => |
| 3774 | subscribe(recordingInputListenersRef, listener), |
| 3775 | subscribeStateChanges: (listener: Listener) => subscribe(listenersRef, listener), |
| 3776 | zoomIn: (tabId: string) => applyZoom(tabId, (current) => nextZoomLevel(current, "in")), |
| 3777 | zoomOut: (tabId: string) => applyZoom(tabId, (current) => nextZoomLevel(current, "out")), |
| 3778 | }; |
| 3779 | }); |
| 3780 | |
| 3781 | export class PreviewTabNotFoundError extends Schema.TaggedError<PreviewTabNotFoundError>()( |
| 3782 | "PreviewTabNotFoundError", |
| 3783 | { tabId: Schema.String }, |
| 3784 | ) { |
| 3785 | override get message(): string { |
| 3786 | return `Preview tab not found: ${this.tabId}`; |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | export class PreviewWebContentsNotFoundError extends Schema.TaggedError<PreviewWebContentsNotFoundError>()( |
| 3791 | "PreviewWebContentsNotFoundError", |
| 3792 | { tabId: Schema.String, webContentsId: Schema.Number }, |
| 3793 | ) { |
| 3794 | override get message(): string { |
| 3795 | return `WebContents ${this.webContentsId} not found for preview tab ${this.tabId}`; |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | export class PreviewWebviewNotInitializedError extends Schema.TaggedError<PreviewWebviewNotInitializedError>()( |
| 3800 | "PreviewWebviewNotInitializedError", |
| 3801 | { tabId: Schema.String }, |
| 3802 | ) { |
| 3803 | override get message(): string { |
| 3804 | return `Preview tab "${this.tabId}" has no webview registered`; |
| 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | export class PreviewMainWindowClosedError extends Schema.TaggedError<PreviewMainWindowClosedError>()( |
| 3809 | "PreviewMainWindowClosedError", |
| 3810 | { tabId: Schema.String }, |
| 3811 | ) { |
| 3812 | override get message(): string { |
| 3813 | return `Cannot start preview frame capture while the main window is closed: ${this.tabId}`; |
| 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | export class PreviewRecordingArmConflictError extends Schema.TaggedError<PreviewRecordingArmConflictError>()( |
| 3818 | "PreviewRecordingArmConflictError", |
| 3819 | { |
| 3820 | tabId: Schema.String, |
| 3821 | webContentsId: Schema.Number, |
| 3822 | armedTabId: Schema.String, |
| 3823 | }, |
| 3824 | ) { |
| 3825 | override get message(): string { |
| 3826 | return `Preview tab ${this.armedTabId} is still claiming the capture stream, so recording could not start for tab ${this.tabId}`; |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | export class PreviewRecordingCaptureUnavailableError extends Schema.TaggedError<PreviewRecordingCaptureUnavailableError>()( |
| 3831 | "PreviewRecordingCaptureUnavailableError", |
| 3832 | { |
| 3833 | tabId: Schema.String, |
| 3834 | webContentsId: Schema.Number, |
| 3835 | }, |
| 3836 | ) { |
| 3837 | override get message(): string { |
| 3838 | return `Preview recording capture is unavailable for tab ${this.tabId} in WebContents ${this.webContentsId}`; |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | export class PreviewOperationError extends Schema.TaggedError<PreviewOperationError>()( |
| 3843 | "PreviewOperationError", |
| 3844 | { |
| 3845 | operation: Schema.String, |
| 3846 | tabId: Schema.optional(Schema.String), |
| 3847 | webContentsId: Schema.optional(Schema.Number), |
| 3848 | artifactPath: Schema.optional(Schema.String), |
| 3849 | cause: Schema.Defect(), |
| 3850 | }, |
| 3851 | ) { |
| 3852 | static toTimelineMessage(error: PreviewOperationError): string { |
| 3853 | return error.cause instanceof Error ? error.cause.message : String(error.cause); |
| 3854 | } |
| 3855 | |
| 3856 | override get message(): string { |
| 3857 | const context = [ |
| 3858 | this.tabId === undefined ? undefined : `tab ${this.tabId}`, |
| 3859 | this.webContentsId === undefined ? undefined : `WebContents ${this.webContentsId}`, |
| 3860 | this.artifactPath === undefined ? undefined : `artifact ${this.artifactPath}`, |
| 3861 | ].filter((value): value is string => value !== undefined); |
| 3862 | return `Desktop preview operation failed: ${this.operation}${context.length === 0 ? "" : ` (${context.join(", ")})`}`; |
| 3863 | } |
| 3864 | } |
| 3865 | |
| 3866 | const isPreviewOperationError = Schema.is(PreviewOperationError); |
| 3867 | |
| 3868 | export class PreviewArtifactPathOutsideDirectoryError extends Schema.TaggedError<PreviewArtifactPathOutsideDirectoryError>()( |
| 3869 | "PreviewArtifactPathOutsideDirectoryError", |
| 3870 | { |
| 3871 | artifactPath: Schema.String, |
| 3872 | artifactDirectory: Schema.String, |
| 3873 | }, |
| 3874 | ) { |
| 3875 | override get message(): string { |
| 3876 | return `Preview artifact path ${this.artifactPath} is outside ${this.artifactDirectory}`; |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | export class PreviewArtifactImageLoadError extends Schema.TaggedError<PreviewArtifactImageLoadError>()( |
| 3881 | "PreviewArtifactImageLoadError", |
| 3882 | { artifactPath: Schema.String }, |
| 3883 | ) { |
| 3884 | override get message(): string { |
| 3885 | return `Preview artifact could not be loaded as an image: ${this.artifactPath}`; |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | export class PreviewAutomationDevToolsOpenError extends Schema.TaggedError<PreviewAutomationDevToolsOpenError>()( |
| 3890 | "PreviewAutomationDevToolsOpenError", |
| 3891 | { webContentsId: Schema.Number }, |
| 3892 | ) { |
| 3893 | override get message(): string { |
| 3894 | return `Close preview DevTools before using agent browser control for WebContents ${this.webContentsId}`; |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | export class PreviewAutomationDebuggerAttachedError extends Schema.TaggedError<PreviewAutomationDebuggerAttachedError>()( |
| 3899 | "PreviewAutomationDebuggerAttachedError", |
| 3900 | { webContentsId: Schema.Number }, |
| 3901 | ) { |
| 3902 | override get message(): string { |
| 3903 | return `Preview control cannot attach to WebContents ${this.webContentsId} because another debugger owns it`; |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | export const PreviewManagerError = Schema.Union([ |
| 3908 | PreviewTabNotFoundError, |
| 3909 | PreviewWebContentsNotFoundError, |
| 3910 | PreviewWebviewNotInitializedError, |
| 3911 | PreviewMainWindowClosedError, |
| 3912 | PreviewRecordingArmConflictError, |
| 3913 | PreviewRecordingCaptureUnavailableError, |
| 3914 | PreviewOperationError, |
| 3915 | PreviewArtifactPathOutsideDirectoryError, |
| 3916 | PreviewArtifactImageLoadError, |
| 3917 | PreviewAutomationDevToolsOpenError, |
| 3918 | PreviewAutomationDebuggerAttachedError, |
| 3919 | ]); |
| 3920 | export type PreviewManagerError = typeof PreviewManagerError.Type; |
| 3921 | |
| 3922 | export class PreviewManager extends Context.Service< |
| 3923 | PreviewManager, |
| 3924 | { |
| 3925 | readonly setMainWindow: (window: BrowserWindow) => Effect.Effect<void, PreviewManagerError>; |
| 3926 | readonly setForwardedShortcuts: ( |
| 3927 | shortcuts: ReadonlyArray<PreviewForwardedShortcut>, |
| 3928 | ) => Effect.Effect<void>; |
| 3929 | readonly getBrowserSession: ( |
| 3930 | scope?: string, |
| 3931 | persistent?: boolean, |
| 3932 | namespace?: BrowserSession.BrowserSessionPartitionNamespace, |
| 3933 | ) => Effect.Effect<Session, PreviewManagerError>; |
| 3934 | readonly isBrowserPartition: (partition: string) => boolean; |
| 3935 | readonly createTab: ( |
| 3936 | tabId: string, |
| 3937 | defaults?: DesktopPreviewTabDefaults, |
| 3938 | ) => Effect.Effect<PreviewTabState, PreviewManagerError>; |
| 3939 | readonly closeTab: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3940 | readonly registerWebview: ( |
| 3941 | tabId: string, |
| 3942 | webContentsId: number, |
| 3943 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3944 | readonly prepareWebview: (webContents: Electron.WebContents) => Effect.Effect<void>; |
| 3945 | readonly navigate: (tabId: string, url: string) => Effect.Effect<void, PreviewManagerError>; |
| 3946 | readonly goBack: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3947 | readonly goForward: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3948 | readonly refresh: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3949 | readonly stopLoading: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3950 | readonly zoomIn: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3951 | readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3952 | readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3953 | readonly reapplyZoom: () => Effect.Effect<void>; |
| 3954 | readonly hardReload: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3955 | readonly findInPage: ( |
| 3956 | tabId: string, |
| 3957 | text: string, |
| 3958 | options: { readonly forward: boolean; readonly newSession: boolean }, |
| 3959 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3960 | readonly stopFindInPage: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3961 | readonly setColorScheme: ( |
| 3962 | tabId: string, |
| 3963 | colorScheme: DesktopPreviewColorScheme, |
| 3964 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3965 | readonly setZoomFactor: ( |
| 3966 | tabId: string, |
| 3967 | zoomFactor: number, |
| 3968 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3969 | readonly setAudioMuted: ( |
| 3970 | tabId: string, |
| 3971 | audioMuted: boolean, |
| 3972 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3973 | readonly setShown: (tabId: string, shown: boolean) => Effect.Effect<void>; |
| 3974 | readonly setCaptureDrawn: (tabId: string, drawn: boolean) => Effect.Effect<void>; |
| 3975 | readonly openDevTools: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 3976 | readonly clearCookies: ( |
| 3977 | partitions?: ReadonlyArray<string>, |
| 3978 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3979 | readonly clearCache: ( |
| 3980 | partitions?: ReadonlyArray<string>, |
| 3981 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3982 | readonly getBrowserPartition: ( |
| 3983 | scope?: string, |
| 3984 | persistent?: boolean, |
| 3985 | namespace?: BrowserSession.BrowserSessionPartitionNamespace, |
| 3986 | ) => Effect.Effect<string, PreviewManagerError>; |
| 3987 | readonly setAnnotationTheme: ( |
| 3988 | theme: DesktopPreviewAnnotationTheme, |
| 3989 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3990 | readonly setAnnotationSendEnabled: ( |
| 3991 | tabId: string, |
| 3992 | enabled: boolean, |
| 3993 | ) => Effect.Effect<void, PreviewManagerError>; |
| 3994 | readonly pickElement: ( |
| 3995 | tabId: string, |
| 3996 | voice?: DesktopPreviewAnnotationVoiceConfig, |
| 3997 | ) => Effect.Effect<PreviewAnnotationSubmissionResult | null, PreviewManagerError>; |
| 3998 | readonly updateAnnotationVoice: ( |
| 3999 | state: DesktopPreviewAnnotationVoiceState, |
| 4000 | ) => Effect.Effect<void, PreviewManagerError>; |
| 4001 | readonly cancelPickElement: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 4002 | readonly captureScreenshot: ( |
| 4003 | tabId: string, |
| 4004 | ) => Effect.Effect<DesktopPreviewScreenshotArtifact, PreviewManagerError>; |
| 4005 | readonly revealArtifact: (path: string) => Effect.Effect<void, PreviewManagerError>; |
| 4006 | readonly copyArtifactToClipboard: (path: string) => Effect.Effect<void, PreviewManagerError>; |
| 4007 | readonly openPictureInPicture: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 4008 | readonly closePictureInPicture: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 4009 | readonly startRecording: ( |
| 4010 | tabId: string, |
| 4011 | options?: RecordingInputOptions, |
| 4012 | ) => Effect.Effect<void, PreviewManagerError>; |
| 4013 | readonly stopRecording: (tabId: string) => Effect.Effect<void, PreviewManagerError>; |
| 4014 | readonly saveRecording: ( |
| 4015 | tabId: string, |
| 4016 | mimeType: string, |
| 4017 | data: Uint8Array, |
| 4018 | ) => Effect.Effect<DesktopPreviewRecordingArtifact, PreviewManagerError>; |
| 4019 | readonly subscribeStateChanges: (listener: Listener) => Effect.Effect<void, never, Scope.Scope>; |
| 4020 | readonly subscribePointerEvents: ( |
| 4021 | listener: PointerEventListener, |
| 4022 | ) => Effect.Effect<void, never, Scope.Scope>; |
| 4023 | readonly subscribeOpenLinks: ( |
| 4024 | listener: OpenLinkListener, |
| 4025 | ) => Effect.Effect<void, never, Scope.Scope>; |
| 4026 | readonly subscribeFindResults: ( |
| 4027 | listener: FindResultListener, |
| 4028 | ) => Effect.Effect<void, never, Scope.Scope>; |
| 4029 | readonly subscribeRecordingInputs: ( |
| 4030 | listener: RecordingInputListener, |
| 4031 | ) => Effect.Effect<void, never, Scope.Scope>; |
| 4032 | } |
| 4033 | >()("@t3tools/desktop/preview/Manager/PreviewManager") {} |
| 4034 | |
| 4035 | /** @public */ |
| 4036 | export const make = Effect.gen(function* PreviewManagerMake() { |
| 4037 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 4038 | const browserSession = yield* BrowserSession.BrowserSession; |
| 4039 | const browserHost = yield* DesktopBrowserHost.DesktopBrowserHost; |
| 4040 | const passkeys = yield* PreviewPasskeys.PreviewPasskeys; |
| 4041 | const downloadSessions = new WeakSet<Electron.Session>(); |
| 4042 | const fileSystem = yield* FileSystem.FileSystem; |
| 4043 | const path = yield* Path.Path; |
| 4044 | const runForkDownload = Effect.runForkWith(yield* Effect.context<never>()); |
| 4045 | const copyToDownloads = Effect.fn("PreviewManager.copyToDownloads")(function* ( |
| 4046 | source: string, |
| 4047 | fileName: string, |
| 4048 | ) { |
| 4049 | const directory = app.getPath("downloads"); |
| 4050 | const extension = path.extname(fileName); |
| 4051 | const stem = path.basename(fileName, extension) || "download"; |
| 4052 | for (let attempt = 0; attempt < 100; attempt += 1) { |
| 4053 | const target = path.join( |
| 4054 | directory, |
| 4055 | attempt === 0 ? `${stem}${extension}` : `${stem} (${attempt})${extension}`, |
| 4056 | ); |
| 4057 | if (yield* fileSystem.exists(target)) continue; |
| 4058 | const copied = yield* Effect.tryPromise({ |
| 4059 | try: () => NodeFSP.copyFile(source, target, NodeFSP.constants.COPYFILE_EXCL), |
| 4060 | catch: (cause) => new PreviewOperationError({ operation: "copyToDownloads", cause }), |
| 4061 | }).pipe( |
| 4062 | Effect.as(true), |
| 4063 | Effect.catchIf( |
| 4064 | (error) => |
| 4065 | error.cause instanceof Error && "code" in error.cause && error.cause.code === "EEXIST", |
| 4066 | () => Effect.succeed(false), |
| 4067 | ), |
| 4068 | ); |
| 4069 | if (!copied) continue; |
| 4070 | shell.showItemInFolder(target); |
| 4071 | return; |
| 4072 | } |
| 4073 | }); |
| 4074 | const placeServerDownloads = (session: Electron.Session) => { |
| 4075 | if (downloadSessions.has(session)) return; |
| 4076 | downloadSessions.add(session); |
| 4077 | session.on("will-download", (_event, item, source) => { |
| 4078 | // Decided before the server's path is set: placing it consumes the guid. |
| 4079 | const human = browserHost.humanStartedDownload(source); |
| 4080 | if (!browserHost.placeDownload(source, item) || !human) return; |
| 4081 | item.once("done", (_doneEvent, state) => { |
| 4082 | if (state !== "completed") return; |
| 4083 | runForkDownload( |
| 4084 | copyToDownloads(item.getSavePath(), item.getFilename()).pipe( |
| 4085 | Effect.catch((error) => |
| 4086 | Effect.logWarning("preview download copy failed", { cause: error }), |
| 4087 | ), |
| 4088 | ), |
| 4089 | ); |
| 4090 | }); |
| 4091 | }); |
| 4092 | }; |
| 4093 | |
| 4094 | const operations = yield* makeNativeOperations( |
| 4095 | environment.browserArtifactsDir, |
| 4096 | environment.path.join(environment.dirname, "preview-pip-preload.cjs"), |
| 4097 | ); |
| 4098 | |
| 4099 | return PreviewManager.of({ |
| 4100 | setMainWindow: operations.setMainWindow, |
| 4101 | setForwardedShortcuts: operations.setForwardedShortcuts, |
| 4102 | getBrowserSession: Effect.fn("PreviewManager.getBrowserSession")( |
| 4103 | function* (scope, persistent, namespace) { |
| 4104 | const session = yield* browserSession |
| 4105 | .getSession(scope, persistent, namespace) |
| 4106 | .pipe( |
| 4107 | Effect.mapError( |
| 4108 | (cause) => new PreviewOperationError({ operation: "getBrowserSession", cause }), |
| 4109 | ), |
| 4110 | ); |
| 4111 | placeServerDownloads(session); |
| 4112 | passkeys.installSessionHandlers(session); |
| 4113 | return session; |
| 4114 | }, |
| 4115 | ), |
| 4116 | isBrowserPartition: browserSession.isPartition, |
| 4117 | createTab: operations.createTab, |
| 4118 | closeTab: operations.closeTab, |
| 4119 | registerWebview: operations.registerWebview, |
| 4120 | prepareWebview: operations.prepareWebview, |
| 4121 | navigate: operations.navigate, |
| 4122 | goBack: operations.goBack, |
| 4123 | goForward: operations.goForward, |
| 4124 | refresh: operations.refresh, |
| 4125 | stopLoading: operations.stopLoading, |
| 4126 | zoomIn: operations.zoomIn, |
| 4127 | zoomOut: operations.zoomOut, |
| 4128 | resetZoom: operations.resetZoom, |
| 4129 | reapplyZoom: operations.reapplyZoom, |
| 4130 | hardReload: operations.hardReload, |
| 4131 | findInPage: operations.findInPage, |
| 4132 | stopFindInPage: operations.stopFindInPage, |
| 4133 | setColorScheme: operations.setColorScheme, |
| 4134 | setZoomFactor: operations.setZoomFactor, |
| 4135 | setAudioMuted: operations.setAudioMuted, |
| 4136 | setShown: operations.setShown, |
| 4137 | setCaptureDrawn: operations.setCaptureDrawn, |
| 4138 | openDevTools: operations.openDevTools, |
| 4139 | clearCookies: Effect.fn("PreviewManager.clearCookies")(function* (partitions) { |
| 4140 | yield* browserSession |
| 4141 | .clearCookies(partitions) |
| 4142 | .pipe( |
| 4143 | Effect.mapError( |
| 4144 | (cause) => new PreviewOperationError({ operation: "clearCookies", cause }), |
| 4145 | ), |
| 4146 | ); |
| 4147 | }), |
| 4148 | clearCache: Effect.fn("PreviewManager.clearCache")(function* (partitions) { |
| 4149 | yield* browserSession |
| 4150 | .clearCache(partitions) |
| 4151 | .pipe( |
| 4152 | Effect.mapError((cause) => new PreviewOperationError({ operation: "clearCache", cause })), |
| 4153 | ); |
| 4154 | }), |
| 4155 | getBrowserPartition: Effect.fn("PreviewManager.getBrowserPartition")( |
| 4156 | function* (scope, persistent, namespace) { |
| 4157 | return yield* browserSession |
| 4158 | .getPartition(scope, persistent, namespace) |
| 4159 | .pipe( |
| 4160 | Effect.mapError( |
| 4161 | (cause) => new PreviewOperationError({ operation: "getBrowserPartition", cause }), |
| 4162 | ), |
| 4163 | ); |
| 4164 | }, |
| 4165 | ), |
| 4166 | setAnnotationTheme: operations.setAnnotationTheme, |
| 4167 | setAnnotationSendEnabled: operations.setAnnotationSendEnabled, |
| 4168 | pickElement: operations.pickElement, |
| 4169 | updateAnnotationVoice: operations.updateAnnotationVoice, |
| 4170 | cancelPickElement: operations.cancelPickElement, |
| 4171 | captureScreenshot: operations.captureScreenshot, |
| 4172 | revealArtifact: operations.revealArtifact, |
| 4173 | copyArtifactToClipboard: operations.copyArtifactToClipboard, |
| 4174 | openPictureInPicture: operations.openPictureInPicture, |
| 4175 | closePictureInPicture: operations.closePictureInPicture, |
| 4176 | startRecording: operations.startRecording, |
| 4177 | stopRecording: operations.stopRecording, |
| 4178 | saveRecording: operations.saveRecording, |
| 4179 | subscribeStateChanges: operations.subscribeStateChanges, |
| 4180 | subscribePointerEvents: operations.subscribePointerEvents, |
| 4181 | subscribeOpenLinks: operations.subscribeOpenLinks, |
| 4182 | subscribeFindResults: operations.subscribeFindResults, |
| 4183 | subscribeRecordingInputs: operations.subscribeRecordingInputs, |
| 4184 | }); |
| 4185 | }).pipe(Effect.withSpan("PreviewManager.make")); |
| 4186 | |
| 4187 | export const layer = Layer.effect(PreviewManager, make); |