From 8417be2a5e0ff5f78a957fb66551757714b591a7 Mon Sep 17 00:00:00 2001 From: Shrey Pandya <3813908+shrey150@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:16:27 +0000 Subject: [PATCH 1/2] fix: match remote viewport to the visible desktop content --- src/main/index.ts | 30 +++++------ src/main/ipc.ts | 9 ++++ src/main/preload.ts | 4 ++ src/main/session.ts | 30 ++--------- src/main/viewport.ts | 53 +++++++++++++++++++ src/renderer/components/ContentArea.ts | 22 ++++++++ src/shared/types.ts | 1 + tests/electron-viewport.cjs | 71 ++++++++++++++++++++++++++ tests/viewport.test.cjs | 45 ++++++++++++++++ 9 files changed, 222 insertions(+), 43 deletions(-) create mode 100644 src/main/viewport.ts create mode 100644 tests/electron-viewport.cjs create mode 100644 tests/viewport.test.cjs diff --git a/src/main/index.ts b/src/main/index.ts index b8480b6..396c1c6 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -20,6 +20,7 @@ import { IPC_CHANNELS } from "../shared/types"; import { getConfigSearchPaths, loadEnvironmentConfig } from "./config"; import { AutomationServer, getAutomationServerPort, isAutomationServerEnabled } from "./automation"; import { secureWindow } from "./security"; +import { parseViewport } from "./viewport"; let mainWindow: BrowserWindow | null = null; let resizeTimeout: ReturnType | null = null; @@ -345,23 +346,14 @@ async function createWindow(): Promise { mainWindow.maximize(); } - // Save window state on resize/move + // The renderer reports the actual live-view size, including toolbars and zoom. mainWindow.on("resize", () => { - if (mainWindow) { - saveWindowState(mainWindow); - // Debounce viewport updates to avoid too many CDP calls during drag - if (resizeTimeout) { - clearTimeout(resizeTimeout); - } - resizeTimeout = setTimeout(() => { - if (mainWindow) { - const bounds = mainWindow.getContentBounds(); - const chromeUIHeight = 76; // Tab bar (36px) + nav bar (40px) - const viewportHeight = Math.max(400, bounds.height - chromeUIHeight); - sessionManager.updateViewport(bounds.width, viewportHeight); - } - }, 150); // 150ms debounce - } + const window = mainWindow; + if (resizeTimeout) clearTimeout(resizeTimeout); + resizeTimeout = setTimeout(() => { + resizeTimeout = null; + if (window && !window.isDestroyed()) saveWindowState(window); + }, 100); }); mainWindow.on("move", () => mainWindow && saveWindowState(mainWindow)); mainWindow.on("close", () => mainWindow && saveWindowState(mainWindow)); @@ -378,6 +370,12 @@ async function createWindow(): Promise { console.log("Renderer loaded, initializing Browserbase session..."); try { + // Measure before provisioning so the initial remote surface matches the guest. + const viewport = parseViewport(await mainWindow?.webContents.executeJavaScript(`(() => { + const rect = document.getElementById("live-view-frame")?.getBoundingClientRect(); + return rect ? { width: Math.round(rect.width), height: Math.round(rect.height) } : null; + })()`)); + if (viewport) await sessionManager.updateViewport(viewport.width, viewport.height); const bbSession = await sessionManager.initialize(); console.log("Session initialized, sending SESSION_CREATED event"); mainWindow?.webContents.send(IPC_CHANNELS.SESSION_CREATED, bbSession.id); diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 7a78d6a..b8f94b7 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -12,6 +12,9 @@ import { ipcMain, BrowserWindow, IpcMainEvent, IpcMainInvokeEvent } from "electr import { IPC_CHANNELS, ScrollInputEvent } from "../shared/types"; import { sessionManager } from "./session"; import { isTrustedRenderer } from "./security"; +import { ViewportUpdater } from "./viewport"; + +let viewportUpdater: ViewportUpdater | null = null; function isAcceleratedScrollEnabled(): boolean { const value = process.env.BROWSERBASE_ACCELERATED_SCROLL; @@ -44,6 +47,10 @@ export function setupIpcHandlers(mainWindow: BrowserWindow): void { }); }; + viewportUpdater?.dispose(); + viewportUpdater = new ViewportUpdater(({ width, height }) => sessionManager.updateViewport(width, height)); + on(IPC_CHANNELS.VIEWPORT_CHANGED, (_event, viewport: unknown) => viewportUpdater?.push(viewport)); + // Navigation handlers handle(IPC_CHANNELS.NAVIGATE_TO, async (_event, url: string) => { try { @@ -189,6 +196,8 @@ export function setupIpcHandlers(mainWindow: BrowserWindow): void { * memory leaks and ensure clean shutdown. */ export function removeIpcHandlers(): void { + viewportUpdater?.dispose(); + viewportUpdater = null; const channels = Object.values(IPC_CHANNELS); channels.forEach((channel) => { ipcMain.removeHandler(channel); diff --git a/src/main/preload.ts b/src/main/preload.ts index b671d9c..9912b2c 100644 --- a/src/main/preload.ts +++ b/src/main/preload.ts @@ -33,6 +33,9 @@ contextBridge.exposeInMainWorld("electronAPI", { dispatchScroll: (scrollEvent: import("../shared/types").ScrollInputEvent) => ipcRenderer.send(IPC_CHANNELS.INPUT_SCROLL, scrollEvent), + updateViewport: (width: number, height: number) => + ipcRenderer.send(IPC_CHANNELS.VIEWPORT_CHANGED, { width, height }), + // Tabs newTab: () => ipcRenderer.invoke(IPC_CHANNELS.TAB_NEW), closeTab: (tabId: string) => ipcRenderer.invoke(IPC_CHANNELS.TAB_CLOSE, tabId), @@ -142,6 +145,7 @@ declare global { navigateHome: () => Promise<{ success: boolean; error?: string }>; isAcceleratedScrollEnabled: () => Promise; dispatchScroll: (scrollEvent: import("../shared/types").ScrollInputEvent) => void; + updateViewport: (width: number, height: number) => void; newTab: () => Promise<{ success: boolean; error?: string }>; closeTab: (tabId: string) => Promise<{ success: boolean; error?: string }>; switchTab: (tabId: string) => Promise<{ success: boolean; error?: string }>; diff --git a/src/main/session.ts b/src/main/session.ts index 5be9cdc..adeb7f7 100644 --- a/src/main/session.ts +++ b/src/main/session.ts @@ -111,37 +111,13 @@ export class SessionManager { async initialize(): Promise { try { - // Calculate viewport size based on window content area - // Chrome UI elements: tab bar (36px), nav bar (40px) = 76px total - const windowBounds = this.mainWindow?.getContentBounds(); - const chromeUIHeight = 76; // 36 (tab bar) + 40 (nav bar) - - // Use actual window size, with reasonable defaults - const defaultWidth = 1440; - const defaultHeight = 900; - - const contentWidth = windowBounds?.width || defaultWidth; - const contentHeight = windowBounds - ? Math.max(400, windowBounds.height - chromeUIHeight) - : defaultHeight - chromeUIHeight; - - // Store for later use with new tabs - this.currentViewportWidth = contentWidth; - this.currentViewportHeight = contentHeight; - - // For session creation, use larger viewport to avoid issues - we'll override with CDP later - const viewportWidth = Math.max(2560, contentWidth); - const viewportHeight = Math.max(1440, contentHeight); - - console.log("Window content bounds:", windowBounds); - console.log(`Chrome UI height: ${chromeUIHeight}px`); - console.log(`Actual viewport to use: ${contentWidth}x${contentHeight}`); - console.log(`Creating session with initial viewport: ${viewportWidth}x${viewportHeight} (will override with CDP)`); + // The renderer supplies measured content dimensions before initialization. + const viewportWidth = this.currentViewportWidth; + const viewportHeight = this.currentViewportHeight; // Create a Browserbase session with viewport matching our window. // BrowserbaseClient waits until the remote browser is RUNNING before // returning connection URLs when async/deferred scheduling is enabled. - const scaleFactor = this.mainWindow?.webContents.getZoomFactor() || 1; const deviceScaleFactor = process.platform === 'darwin' ? 2 : 1; this.session = await this.getBrowserbaseClient().createSession({ diff --git a/src/main/viewport.ts b/src/main/viewport.ts new file mode 100644 index 0000000..7ccace5 --- /dev/null +++ b/src/main/viewport.ts @@ -0,0 +1,53 @@ +import { ViewportConfig } from "../shared/types"; + +/** Reject hidden elements, malformed IPC values, and unreasonably large surfaces. */ +export function parseViewport(value: unknown): ViewportConfig | null { + if (!value || typeof value !== "object") return null; + const { width, height } = value as Partial; + if (typeof width !== "number" || typeof height !== "number" || + !Number.isInteger(width) || !Number.isInteger(height) || + width < 1 || height < 1 || width > 8192 || height > 8192) return null; + return { width, height }; +} + +/** Coalesce resize bursts and serialize CDP updates so the newest size wins. */ +export class ViewportUpdater { + private pending: ViewportConfig | null = null; + private timer: ReturnType | null = null; + private applying = false; + private disposed = false; + + constructor(private update: (viewport: ViewportConfig) => Promise, private delayMs = 100) {} + + push(value: unknown): void { + const viewport = parseViewport(value); + if (!viewport || this.disposed) return; + this.pending = viewport; + if (!this.timer && !this.applying) { + this.timer = setTimeout(() => { void this.flush(); }, this.delayMs); + } + } + + private async flush(): Promise { + this.timer = null; + const viewport = this.pending; + this.pending = null; + if (!viewport || this.disposed) return; + this.applying = true; + try { + await this.update(viewport); + } catch { + console.error("Failed to update the remote viewport"); + } finally { + this.applying = false; + if (this.pending && !this.disposed) this.push(this.pending); + } + } + + dispose(): void { + this.disposed = true; + this.pending = null; + if (this.timer) clearTimeout(this.timer); + this.timer = null; + } +} diff --git a/src/renderer/components/ContentArea.ts b/src/renderer/components/ContentArea.ts index ab56c61..171b75c 100644 --- a/src/renderer/components/ContentArea.ts +++ b/src/renderer/components/ContentArea.ts @@ -5,6 +5,8 @@ export class ContentArea { private isLoaded: boolean = false; private loadTimeout: ReturnType | null = null; private acceleratedScrollEnabled: boolean = false; + private resizeObserver: ResizeObserver | null = null; + private viewportTimeout: ReturnType | null = null; constructor() { console.log("[ContentArea] Initializing..."); @@ -19,10 +21,30 @@ export class ContentArea { }); this.setupEventListeners(); + this.observeViewport(); void this.loadInputSettings(); console.log("[ContentArea] Initialized successfully"); } + private observeViewport(): void { + if (!this.debugView) return; + this.resizeObserver = new ResizeObserver(() => { + if (this.viewportTimeout) return; + this.viewportTimeout = setTimeout(() => { + this.viewportTimeout = null; + const rect = this.debugView?.getBoundingClientRect(); + if (rect && rect.width > 0 && rect.height > 0) { + window.electronAPI.updateViewport(Math.round(rect.width), Math.round(rect.height)); + } + }, 100); + }); + this.resizeObserver.observe(this.debugView); + window.addEventListener("beforeunload", () => { + this.resizeObserver?.disconnect(); + if (this.viewportTimeout) clearTimeout(this.viewportTimeout); + }, { once: true }); + } + private async loadInputSettings(): Promise { try { this.acceleratedScrollEnabled = await window.electronAPI.isAcceleratedScrollEnabled(); diff --git a/src/shared/types.ts b/src/shared/types.ts index 8e60102..0b80709 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -161,6 +161,7 @@ export const IPC_CHANNELS = { // Input forwarding INPUT_SCROLL: "input:scroll", + VIEWPORT_CHANGED: "viewport:changed", GET_ACCELERATED_SCROLL_ENABLED: "input:get-accelerated-scroll-enabled", // Tab management diff --git a/tests/electron-viewport.cjs b/tests/electron-viewport.cjs new file mode 100644 index 0000000..d70bb4b --- /dev/null +++ b/tests/electron-viewport.cjs @@ -0,0 +1,71 @@ +// Run after npm run build: electron tests/electron-viewport.cjs +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { app, BrowserWindow, session } = require('electron'); +const { sessionManager } = require('../dist/main/session'); +const { IPC_CHANNELS } = require('../dist/shared/types'); +const config = require('../dist/main/config'); +const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'desktop-viewport-test-')); +app.setPath('userData', profile); +process.env.BROWSERBASE_API_KEY = 'synthetic-test-key'; +delete process.env.BROWSERBASE_AUTOMATION_SERVER; +config.loadEnvironmentConfig = () => []; +const updates = []; +let initialViewport; +sessionManager.updateViewport = async (width, height) => { updates.push({ width, height }); }; +sessionManager.initialize = async () => { initialViewport = updates.at(-1); return { id: 'synthetic-session' }; }; +sessionManager.getDebugUrl = () => 'https://www.browserbase.com/devtools-fullscreen-compiled/index.html'; +const timeout = setTimeout(() => { console.error('Viewport test timed out'); app.exit(1); }, 20000); +async function until(check) { + for (let i = 0; i < 100; i++) { if (await check()) return; await new Promise(resolve => setTimeout(resolve, 50)); } + throw new Error('Timed out waiting for viewport update'); +} +app.whenReady().then(async () => { + const guestSession = session.fromPartition('persist:browserbase'); + guestSession.protocol.handle('https', () => new Response('

Viewport sizing fixture

The desktop reports the visible content area after resizing, toolbar changes, and zoom.

', { headers: { 'content-type': 'text/html' } })); + let passed = false; + try { + let window; + await until(() => { window = BrowserWindow.getAllWindows()[0]; return window; }); + const shell = window.webContents; + await until(() => shell.executeJavaScript('!!window.app && !!window.electronAPI')); + const layout = () => shell.executeJavaScript('(() => { const r=document.getElementById("live-view-frame").getBoundingClientRect(); return {width:Math.round(r.width),height:Math.round(r.height)}; })()'); + await until(() => initialViewport); + assert.deepEqual(initialViewport, await layout()); + const evidence = []; + const expectSize = async (name, expected) => { + await until(async () => JSON.stringify(updates.at(-1)) === JSON.stringify(await layout())); + assert.deepEqual(await layout(), expected); + evidence.push({ name, ...expected }); + }; + window.setContentSize(800, 600); + await expectSize('window resized', { width: 800, height: 520 }); + await shell.executeJavaScript('window.electronAPI.toggleBookmarks()'); + await expectSize('bookmarks shown', { width: 800, height: 492 }); + shell.send(IPC_CHANNELS.DOWNLOAD_STARTED, { id: 'synthetic-download', filename: 'sample.txt', receivedBytes: 10, totalBytes: 100, state: 'in_progress' }); + await expectSize('downloads shown', { width: 800, height: 444 }); + if (process.env.DESKTOP_TEST_SCREENSHOT) fs.writeFileSync(process.env.DESKTOP_TEST_SCREENSHOT, (await shell.capturePage()).toPNG()); + shell.setZoomFactor(1.25); + await expectSize('shell zoom 125 percent', { width: 640, height: 324 }); + shell.setZoomFactor(1); + await shell.executeJavaScript('window.app.getBookmarksBar().hide(); window.app.getDownloadsBar().hide()'); + window.setContentSize(400, 300); + await expectSize('small window', { width: 400, height: 220 }); + const count = updates.length; + await shell.executeJavaScript('window.electronAPI.updateViewport(0, Infinity)'); + await new Promise(resolve => setTimeout(resolve, 250)); + assert.equal(updates.length, count); + console.log('PASS: measured viewport sizing', JSON.stringify(evidence)); + passed = true; + } catch (error) { console.error(error); } + finally { + clearTimeout(timeout); + for (const window of BrowserWindow.getAllWindows()) window.destroy(); + guestSession.protocol.unhandle('https'); + fs.rmSync(profile, { recursive: true, force: true }); + app.exit(passed ? 0 : 1); + } +}); +require('../dist/main/index'); diff --git a/tests/viewport.test.cjs b/tests/viewport.test.cjs new file mode 100644 index 0000000..bcaee6f --- /dev/null +++ b/tests/viewport.test.cjs @@ -0,0 +1,45 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { parseViewport, ViewportUpdater } = require('../dist/main/viewport'); + +test('viewport input accepts bounded integer dimensions and rejects malformed or hidden surfaces', () => { + assert.deepEqual(parseViewport({ width: 800, height: 220 }), { width: 800, height: 220 }); + for (const value of [null, '800x600', {}, { width: 0, height: 600 }, { width: 800, height: -1 }, { width: '800', height: 600 }, { width: 800.1, height: 600 }, { width: Infinity, height: 600 }, { width: 800, height: 8193 }]) { + assert.equal(parseViewport(value), null); + } +}); + +test('resize bursts send only the latest size and serialize updates while CDP is busy', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + let release; + const gate = new Promise(resolve => { release = resolve; }); + const updates = []; + const updater = new ViewportUpdater(async (viewport) => { + updates.push(viewport); + if (updates.length === 1) await gate; + }); + updater.push({ width: 700, height: 500 }); + updater.push({ width: 800, height: 520 }); + t.mock.timers.tick(100); + assert.deepEqual(updates, [{ width: 800, height: 520 }]); + updater.push({ width: 900, height: 620 }); + updater.push({ width: 1000, height: 720 }); + t.mock.timers.tick(500); + assert.equal(updates.length, 1); + release(); + await new Promise(setImmediate); + t.mock.timers.tick(100); + assert.deepEqual(updates, [{ width: 800, height: 520 }, { width: 1000, height: 720 }]); + updater.dispose(); +}); + +test('disposing a window cancels pending viewport work and rejects later reports', (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + const updates = []; + const updater = new ViewportUpdater(async (viewport) => { updates.push(viewport); }); + updater.push({ width: 800, height: 600 }); + updater.dispose(); + updater.push({ width: 900, height: 700 }); + t.mock.timers.tick(1000); + assert.deepEqual(updates, []); +}); From f310fd1515f952ed1309ec7fa4aa1c74cab3e67a Mon Sep 17 00:00:00 2001 From: Shrey Pandya <3813908+shrey150@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:32:26 +0000 Subject: [PATCH 2/2] Cancel window startup after its viewport measurement becomes stale --- src/main/index.ts | 15 +++++--- tests/electron-startup.cjs | 78 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/electron-startup.cjs diff --git a/src/main/index.ts b/src/main/index.ts index 396c1c6..9b9a6ad 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -365,23 +365,28 @@ async function createWindow(): Promise { // Load the renderer HTML mainWindow.loadFile(path.join(__dirname, "../renderer/index.html")); - // Wait for renderer to be ready before initializing session - mainWindow.webContents.on("did-finish-load", async () => { + // Keep this callback tied to its window while initialization is in flight. + const window = mainWindow; + const isCurrentWindow = () => !window.isDestroyed() && mainWindow === window; + window.webContents.on("did-finish-load", async () => { + if (!isCurrentWindow()) return; console.log("Renderer loaded, initializing Browserbase session..."); try { // Measure before provisioning so the initial remote surface matches the guest. - const viewport = parseViewport(await mainWindow?.webContents.executeJavaScript(`(() => { + const viewport = parseViewport(await window.webContents.executeJavaScript(`(() => { const rect = document.getElementById("live-view-frame")?.getBoundingClientRect(); return rect ? { width: Math.round(rect.width), height: Math.round(rect.height) } : null; })()`)); + if (!isCurrentWindow()) return; if (viewport) await sessionManager.updateViewport(viewport.width, viewport.height); + if (!isCurrentWindow()) return; const bbSession = await sessionManager.initialize(); console.log("Session initialized, sending SESSION_CREATED event"); - mainWindow?.webContents.send(IPC_CHANNELS.SESSION_CREATED, bbSession.id); + if (isCurrentWindow()) window.webContents.send(IPC_CHANNELS.SESSION_CREATED, bbSession.id); } catch (error) { console.error("Failed to initialize Browserbase session:", error); - mainWindow?.webContents.send(IPC_CHANNELS.SESSION_ERROR, (error as Error).message); + if (isCurrentWindow()) window.webContents.send(IPC_CHANNELS.SESSION_ERROR, (error as Error).message); } }); diff --git a/tests/electron-startup.cjs b/tests/electron-startup.cjs new file mode 100644 index 0000000..4a93469 --- /dev/null +++ b/tests/electron-startup.cjs @@ -0,0 +1,78 @@ +// Run after npm run build: electron tests/electron-startup.cjs +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { app, BrowserWindow } = require('electron'); +const { sessionManager } = require('../dist/main/session'); +const config = require('../dist/main/config'); +const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'desktop-startup-test-')); +app.setPath('userData', profile); +// Exercise macOS's persistent app lifecycle on every test host. +Object.defineProperty(process, 'platform', { value: 'darwin' }); +process.env.BROWSERBASE_API_KEY = 'synthetic-test-key'; +delete process.env.BROWSERBASE_AUTOMATION_SERVER; +config.loadEnvironmentConfig = () => []; +let stage = 'measurement'; +let releaseMeasurement; +let releaseViewport; +let measured = false; +let initialized = 0; +sessionManager.initialize = async () => { initialized++; return { id: 'synthetic-session' }; }; +sessionManager.cleanup = async () => {}; +sessionManager.getDebugUrl = () => ''; +sessionManager.updateViewport = async () => { + if (stage === 'viewport' && measured && !releaseViewport) { + await new Promise(resolve => { releaseViewport = resolve; }); + } +}; +app.on('web-contents-created', (_event, contents) => { + const execute = contents.executeJavaScript.bind(contents); + contents.executeJavaScript = (code, ...args) => { + if (code.includes('const rect = document.getElementById("live-view-frame")')) { + measured = true; + if (stage === 'measurement') return new Promise(resolve => { releaseMeasurement = resolve; }); + } + return execute(code, ...args); + }; +}); +const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); +async function until(check) { + for (let i = 0; i < 100; i++) { if (await check()) return; await delay(20); } + throw new Error('Timed out waiting for window startup'); +} +const timeout = setTimeout(() => { console.error('Startup test timed out'); app.exit(1); }, 15000); +app.whenReady().then(async () => { + let passed = false; + try { + await until(() => releaseMeasurement); + BrowserWindow.getAllWindows()[0].close(); + await until(() => BrowserWindow.getAllWindows().length === 0); + releaseMeasurement({ width: 800, height: 520 }); + await delay(100); + assert.equal(initialized, 0, 'a closed window cannot start a session after measurement'); + + stage = 'viewport'; measured = false; + app.emit('activate'); + await until(() => releaseViewport); + BrowserWindow.getAllWindows()[0].close(); + await until(() => BrowserWindow.getAllWindows().length === 0); + releaseViewport(); + await delay(100); + assert.equal(initialized, 0, 'a closed window cannot start a session after a viewport update'); + + stage = 'ready'; + app.emit('activate'); + await until(() => initialized === 1); + assert.equal(BrowserWindow.getAllWindows().length, 1); + console.log('PASS: stale measurement and viewport updates cannot initialize a closed window; reopening starts normally'); + passed = true; + } catch (error) { console.error(error); } + finally { + clearTimeout(timeout); + for (const window of BrowserWindow.getAllWindows()) window.destroy(); + fs.rmSync(profile, { recursive: true, force: true }); + app.exit(passed ? 0 : 1); + } +}); +require('../dist/main/index');