Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions apps/desktop/src/preview/DesktopBrowserHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const makeDebuggee = (
readonly deferFocus?: boolean;
/** The guest is still loading its first page. */
readonly loading?: boolean;
/** Like a guest of a hidden window: it answers a capture only once Electron captures it. */
readonly drawsOnlyWhenCaptured?: () => boolean;
/** Electron's capture of this guest never settles. */
readonly capturesStall?: () => boolean;
readonly refuse?: (method: string, sessionId: string | undefined) => boolean;
} = {},
) => {
Expand Down Expand Up @@ -90,7 +94,14 @@ const makeDebuggee = (
}),
});
let destroyed = false;
let nativeCaptures = 0;
const webContents = Object.assign(new NodeEvents.EventEmitter(), {
capturePage: () => {
nativeCaptures += 1;
if (options.capturesStall?.()) return new Promise(() => {});
if (options.drawsOnlyWhenCaptured?.()) pending.splice(0).forEach((resolve) => resolve());
return Promise.resolve({});
},
getURL: () => "http://localhost/",
getTitle: () => "Page",
getUserAgent: () => "Electron",
Expand All @@ -117,6 +128,7 @@ const makeDebuggee = (
/** Refuses the pending command the debugger received with this id. */
refuse: (id: number) => rejects[sent.indexOf(id)]?.(),
focusEmulation,
nativeCaptures: () => nativeCaptures,
nextSend: () => new Promise<void>((resolve) => sendWaiters.push(resolve)),
release: () => pending.splice(0).forEach((resolve) => resolve()),
releaseFirst: () => pending.shift()?.(),
Expand Down Expand Up @@ -364,6 +376,93 @@ describe("DesktopBrowserHost", () => {
}).pipe(Effect.scoped),
);

it.effect("prods a guest its window stopped drawing until what waits on it settles", () =>
Effect.gen(function* () {
let drawsWhenCaptured = true;
let capturesStall = false;
const host = yield* makeDrawnHost;
const debuggee = makeDebuggee({
drawsOnlyWhenCaptured: () => drawsWhenCaptured,
capturesStall: () => capturesStall,
});
host.attach(key, debuggee.tab);
const replies = yield* watchReplies(host);

yield* command(host, 1, "Page.captureScreenshot");
yield* settle;
expect(debuggee.sent).toEqual([1]);
yield* TestClock.adjust("250 millis");
expect(yield* Queue.take(replies)).toBe(1);
expect(debuggee.nativeCaptures()).toBe(1);

// A click waits on the guest's next frame the same way.
yield* command(host, 2, "Input.dispatchMouseEvent");
yield* settle;
yield* TestClock.adjust("250 millis");
expect(yield* Queue.take(replies)).toBe(2);
expect(debuggee.nativeCaptures()).toBe(2);

// A wait that settles within a frame is never prodded.
yield* command(host, 3, "Page.captureScreenshot");
yield* settle;
debuggee.releaseFirst();
expect(yield* Queue.take(replies)).toBe(3);
yield* TestClock.adjust("1 second");
expect(debuggee.nativeCaptures()).toBe(2);

// A long wait is prodded less often as it goes on, once per tick though it holds focus too.
drawsWhenCaptured = false;
yield* command(host, 4, "Page.captureScreenshot");
yield* settle;
for (const [wait, prods] of [
["250 millis", 3],
["500 millis", 4],
["1 second", 5],
["2 seconds", 6],
["2 seconds", 7],
] as const) {
yield* TestClock.adjust(wait);
expect(debuggee.nativeCaptures()).toBe(prods);
}
debuggee.releaseFirst();
expect(yield* Queue.take(replies)).toBe(4);
yield* TestClock.adjust("10 seconds");
expect(debuggee.nativeCaptures()).toBe(7);

// A wait the agent's turn gave up on is no longer prodded.
yield* command(host, 5, "Runtime.evaluate");
yield* settle;
yield* TestClock.adjust("250 millis");
expect(debuggee.nativeCaptures()).toBe(8);
yield* host.handleCommandLine(
encodeJson({ type: "turn", threadId: key.threadId, active: false }),
);
yield* TestClock.adjust("10 seconds");
expect(debuggee.nativeCaptures()).toBe(8);
debuggee.releaseFirst();
expect(yield* Queue.take(replies)).toBe(5);

// Captures Electron never settles pile up no further than two.
capturesStall = true;
yield* command(host, 6, "Runtime.evaluate");
yield* settle;
yield* TestClock.adjust("10 seconds");
expect(debuggee.nativeCaptures()).toBe(10);
debuggee.releaseFirst();
expect(yield* Queue.take(replies)).toBe(6);
// Even across a reattach of the same guest, as when DevTools closes.
host.detach(key);
host.attach(key, debuggee.tab);
const reattached = yield* watchReplies(host);
yield* command(host, 7, "Runtime.evaluate");
yield* settle;
yield* TestClock.adjust("10 seconds");
expect(debuggee.nativeCaptures()).toBe(10);
debuggee.releaseFirst();
expect(yield* Queue.take(reattached)).toBe(7);
}).pipe(Effect.scoped),
);

it.effect("stops waiting on a screenshot the page navigated away from", () =>
Effect.gen(function* () {
const host = yield* makeDrawnHost;
Expand Down
77 changes: 72 additions & 5 deletions apps/desktop/src/preview/DesktopBrowserHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const POINTER_TRAVEL = new Set<unknown>(["mouseMoved", "mouseWheel"]);
const FOCUS_IDLE = "5 seconds";
/** How long a screenshot waits for the client to draw its guest at full detail. */
const CAPTURE_DRAW_WAIT = "1 second";
/** Most waits settle within a frame or two of this, so a quick one is never prodded. */
const PAINT_NUDGE_FIRST_MS = 250;
/** A long wait, such as a page awaiting a timer, is prodded less often as it goes on. */
const PAINT_NUDGE_LONGEST_MS = 2_000;
/** Electron's capture has no deadline and a cold guest can stall one, so a second may follow. */
const PAINT_NUDGE_IN_FLIGHT = 2;
/** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */
const FIRST_PAGE_TIMEOUT = "5 seconds";
const ERR_ABORTED = -3;
Expand Down Expand Up @@ -73,6 +79,9 @@ interface AttachedTab {
readonly focusPending: Map<object, string | undefined>;
/** Screenshots Chromium was sent and has not answered; each keeps its wake lock until then. */
readonly capturing: Set<object>;
/** Each wait on the guest's next frame, as whether it still waits; the guest is prodded until none do. */
readonly frameWaits: Set<() => boolean>;
nudging: Fiber.Fiber<void> | null;
/** Whether the client keeps the guest painting, as published on `painting`. */
painting: boolean;
/**
Expand Down Expand Up @@ -258,14 +267,67 @@ export const make = Effect.gen(function* () {
runFork(PubSub.publish(painting, { key: tab.key, active }));
};

/**
* A hidden, minimized or covered window stops drawing its guest even while unthrottled, and
* Electron reports a covered one as visible, so a capture, or a command that waits on a frame,
* can wait forever. Electron's own capture still draws one, so a wait that outlasts a frame
* prods the guest, less often as it goes on.
*/
/** By guest rather than attachment, since reopening DevTools reattaches the same guest. */
const nativeCaptures = new WeakMap<Electron.WebContents, number>();

const nudgePaint = (tab: AttachedTab, delayMs: number): Effect.Effect<void> =>
Effect.sleep(delayMs).pipe(
Effect.andThen(
Effect.suspend(() => {
const { webContents } = tab.debuggee;
for (const waits of tab.frameWaits) if (!waits()) tab.frameWaits.delete(waits);
if (
tab.frameWaits.size === 0 ||
tabs.get(keyOf(tab.key)) !== tab ||
webContents.isDestroyed()
) {
tab.nudging = null;
return Effect.void;
}
const inFlight = nativeCaptures.get(webContents) ?? 0;
if (inFlight < PAINT_NUDGE_IN_FLIGHT) {
nativeCaptures.set(webContents, inFlight + 1);
const settled = () => {
nativeCaptures.set(webContents, (nativeCaptures.get(webContents) ?? 1) - 1);
};
webContents.capturePage().then(settled, settled);
}
return nudgePaint(tab, Math.min(delayMs * 2, PAINT_NUDGE_LONGEST_MS));
}),
),
);

/** Prods the guest while `work` is pending and `waits` holds; one loop serves all of a tab's waits. */
const nudgedUntilSettled = <A>(
tab: AttachedTab,
waits: () => boolean,
work: () => Promise<A>,
) => {
tab.frameWaits.add(waits);
tab.nudging ??= runFork(nudgePaint(tab, PAINT_NUDGE_FIRST_MS));
return work().finally(() => {
tab.frameWaits.delete(waits);
if (tab.frameWaits.size > 0 || tab.nudging === null) return;
runFork(Fiber.interrupt(tab.nudging));
tab.nudging = null;
});
};

/** A command may wait on the page's next frame, so the guest stays focused until it settles. */
const holdFocus = <A>(tab: AttachedTab, owner: string | undefined, work: () => Promise<A>) => {
const pending = {};
tab.focusPending.set(pending, owner);
tab.focusUses += 1;
setFocusEmulation(tab, true);
releaseFocusWhenIdle(tab);
return work().finally(() => {
// Dropping the hold, at a turn's end or a new document, stops the prods too.
return nudgedUntilSettled(tab, () => tab.focusPending.has(pending), work).finally(() => {
if (!tab.focusPending.delete(pending)) return;
tab.focusUses += 1;
releaseFocusWhenIdle(tab);
Expand Down Expand Up @@ -296,11 +358,14 @@ export const make = Effect.gen(function* () {
const sent = {};
tab.capturing.add(sent);
syncPainting(tab);
const sendNudged = () => nudgedUntilSettled(tab, () => tab.capturing.has(sent), send);
// Sent at once when already drawn, so it keeps its place among the commands after it.
return (drawnTabs.has(keyOf(tab.key)) ? send() : whenDrawn(tab).then(send)).finally(() => {
tab.capturing.delete(sent);
syncPainting(tab);
});
return (drawnTabs.has(keyOf(tab.key)) ? sendNudged() : whenDrawn(tab).then(sendNudged)).finally(
() => {
tab.capturing.delete(sent);
syncPainting(tab);
},
);
};

/**
Expand Down Expand Up @@ -537,6 +602,8 @@ export const make = Effect.gen(function* () {
focusUses: 0,
focusPending: new Map(),
capturing: new Set(),
frameWaits: new Set(),
nudging: null,
painting: false,
agentUsed: false,
focusReleasing: false,
Expand Down
Loading