Skip to content

Commit 5aa3a9a

Browse files
committed
fix(server): preview copy keys reach frames in their own process
A frame that runs in its own process, such as one sent with an Origin-Agent-Cluster header, is out of reach of the page's CDP session, so its copy and cut keys sent the viewer nothing. Each such frame now gets its own session that installs the same copy recorder, is cleared and read with the page's frames, and is dropped when its target closes.
1 parent 3661e61 commit 5aa3a9a

2 files changed

Lines changed: 121 additions & 20 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,41 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
6060
},
6161
}));
6262

63-
function makeSession() {
63+
/** A frame in its own process. Only its own session reaches the copy it holds. */
64+
function makeFrame() {
65+
const frame = {
66+
copy: "",
67+
detached: false,
68+
isDetached: () => frame.detached,
69+
recording: Promise.withResolvers<void>(),
70+
};
71+
return frame;
72+
}
73+
74+
function makeSession(frame?: ReturnType<typeof makeFrame>) {
75+
const events = new NodeEvents.EventEmitter();
6476
return {
65-
on: vi.fn(),
77+
on: vi.fn((name: string, listener: () => void) => events.on(name, listener)),
78+
emit: (name: string) => events.emit(name),
6679
detach: vi.fn(async () => {}),
6780
send: vi.fn(async (method: string, input?: unknown): Promise<Record<string, unknown>> => {
6881
if (method === "Page.getNavigationHistory") return { currentIndex: 0, entries: [{}] };
6982
if (method === "Page.getLayoutMetrics") return { cssVisualViewport: { pageX: 0, pageY: 0 } };
7083
if (method === "Page.captureScreenshot") return { data: "ZnJhbWU=" };
7184
if (method === "Page.getFrameTree") return { frameTree: { frame: { id: "main" } } };
7285
if (method === "Page.createIsolatedWorld") return { executionContextId: 7 };
73-
// A copy or cut key copies keyCopy; one that copies nothing leaves the frames as they were.
74-
if (method === "Input.dispatchKeyEvent" && (input as { commands?: unknown }).commands)
75-
pageCopy = keyCopy || pageCopy;
76-
// The clipboard world hands over what the page's frames copied, once.
86+
if (method === "Page.addScriptToEvaluateOnNewDocument") frame?.recording.resolve();
87+
// A copy or cut key copies keyCopy in the focused frame, or the page's process if none;
88+
// one that copies nothing leaves the frames as they were.
89+
if (method === "Input.dispatchKeyEvent" && (input as { commands?: unknown }).commands) {
90+
if (focusedFrame) focusedFrame.copy = keyCopy || focusedFrame.copy;
91+
else pageCopy = keyCopy || pageCopy;
92+
}
93+
// The clipboard world hands over what the session's frames copied, once.
7794
if (method === "Runtime.evaluate" && (input as { contextId?: number }).contextId === 7) {
78-
const value = pageCopy;
79-
pageCopy = "";
95+
const value = frame ? frame.copy : pageCopy;
96+
if (frame) frame.copy = "";
97+
else pageCopy = "";
8098
return { result: { value } };
8199
}
82100
return { result: { value: "evaluated" } };
@@ -102,6 +120,7 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
102120
setDefaultNavigationTimeout: vi.fn(),
103121
context: () => context,
104122
mainFrame: () => page,
123+
frames: () => [page],
105124
url: () => url,
106125
title: vi.fn(async () => "test page"),
107126
viewportSize: () => viewport,
@@ -141,8 +160,10 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
141160
sessions,
142161
newPage: async () => page as unknown as Page,
143162
grantPermissions: vi.fn(async () => {}),
144-
newCDPSession: async () => {
145-
const session = makeSession();
163+
newCDPSession: async (target: unknown) => {
164+
const session = makeSession(
165+
target === page ? undefined : (target as ReturnType<typeof makeFrame>),
166+
);
146167
sessions.push(session);
147168
return session;
148169
},
@@ -164,6 +185,8 @@ let contextGate: PromiseWithResolvers<void> | null = null;
164185
let pageCopy = "";
165186
/** The text the next copy or cut key copies. */
166187
let keyCopy = "";
188+
/** The frame in its own process that has focus; otherwise keys copy in the page's process. */
189+
let focusedFrame: ReturnType<typeof makeFrame> | null = null;
167190
let contextFailure: Error | null = null;
168191
/** Server tabs the fake desktop renders, and the endpoints the server connected to. */
169192
let desktopRendersNext = false;
@@ -283,6 +306,7 @@ beforeEach(() => {
283306
contextFailure = null;
284307
pageCopy = "";
285308
keyCopy = "";
309+
focusedFrame = null;
286310
desktopTabs.clear();
287311
desktopRendersNext = false;
288312
releasedDesktopTabs.length = 0;
@@ -1057,6 +1081,40 @@ it.live("only the controlling viewer's copy and cut keys reach its clipboard", (
10571081
).pipe(Effect.provide(layer)),
10581082
);
10591083

1084+
it.live("copy keys reach the viewer from a frame in its own process", () =>
1085+
Effect.scoped(
1086+
Effect.gen(function* () {
1087+
const { browser, tabId } = yield* ready;
1088+
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1089+
yield* viewer.input({ type: "takeControl" });
1090+
const copy = () =>
1091+
viewer.input({ type: "key", action: "down", key: "c", code: "KeyC", modifiers: 4 }).pipe(
1092+
Effect.andThen(Queue.clear(viewer.output)),
1093+
Effect.map((items) => items.filter((item) => item._tag === "clipboard")),
1094+
);
1095+
const frame = makeFrame();
1096+
contexts[0]!.page.emit("framenavigated", frame);
1097+
yield* Effect.promise(() => frame.recording.promise);
1098+
const frameSession = contexts[0]!.sessions.at(-1)!;
1099+
focusedFrame = frame;
1100+
keyCopy = "framed";
1101+
expect(yield* copy()).toEqual([{ _tag: "clipboard", text: "framed" }]);
1102+
// What the frame kept from before is cleared, not sent by a key elsewhere.
1103+
frame.copy = "left over";
1104+
focusedFrame = null;
1105+
keyCopy = "";
1106+
expect(yield* copy()).toEqual([]);
1107+
expect(frame.copy).toBe("");
1108+
// A frame that went away is no longer read.
1109+
frame.detached = true;
1110+
frame.copy = "gone";
1111+
frameSession.emit("close");
1112+
expect(yield* copy()).toEqual([]);
1113+
expect(frame.copy).toBe("gone");
1114+
}),
1115+
).pipe(Effect.provide(layer)),
1116+
);
1117+
10601118
it.live("a page download is saved, offered to the controller, and listed for the agent", () =>
10611119
Effect.scoped(
10621120
Effect.gen(function* () {

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import type {
6666
Dialog,
6767
Download,
6868
FileChooser,
69+
Frame,
6970
Page,
7071
} from "playwright-core";
7172

@@ -288,6 +289,8 @@ interface ServerTab {
288289
readonly tabId: string;
289290
readonly page: Page;
290291
readonly cdp: CDPSession;
292+
/** Sessions of the page's frames that run in their own process. */
293+
readonly frameSessions: Map<Frame, CDPSession>;
291294
readonly createdAt: number;
292295
/** The agent's latest request on this tab; idle agent tabs close. */
293296
usedAt: number;
@@ -786,6 +789,7 @@ const make = Effect.gen(function* () {
786789
tabId: snapshot.tabId,
787790
page,
788791
cdp,
792+
frameSessions: new Map(),
789793
createdAt: Date.now(),
790794
usedAt: Date.now(),
791795
viewers: new Set(),
@@ -834,7 +838,10 @@ const make = Effect.gen(function* () {
834838
page.on("framenavigated", (frame) => {
835839
// Same-document navigations (SPA routes) fire no load event.
836840
if (frame === page.mainFrame() && !tab.loading) void reportLoaded(tab);
841+
if (!desktop) void recordFrameCopies(tab, frame);
837842
});
843+
// An adopted popup's frames may have loaded before the tab listened.
844+
if (adopted) for (const frame of page.frames()) void recordFrameCopies(tab, frame);
838845
page.on("requestfailed", (request) => {
839846
const errorText = request.failure()?.errorText ?? "";
840847
pushBounded(tab.networkEntries, {
@@ -1137,7 +1144,7 @@ const make = Effect.gen(function* () {
11371144
await context.grantPermissions(["clipboard-write"]);
11381145
};
11391146

1140-
/** Records the copies made in each of a headless page's frames. */
1147+
/** Records the copies made in each frame a session reaches. */
11411148
const recordCopies = async (cdp: CDPSession) => {
11421149
// Scripts for new documents run only while this session's Page domain is on.
11431150
await cdp.send("Page.enable");
@@ -1148,19 +1155,48 @@ const make = Effect.gen(function* () {
11481155
});
11491156
};
11501157

1151-
/** Clears every frame's copy and returns what the key just handled copied or cut. */
1152-
const takeCopiedText = async (tab: ServerTab) => {
1153-
const { frameTree } = await tab.cdp.send("Page.getFrameTree");
1154-
const frameIds = (node: typeof frameTree): Array<string> => [
1158+
/**
1159+
* A frame in its own process, such as one with an `Origin-Agent-Cluster`
1160+
* header, is out of the page session's reach, so it records copies through
1161+
* its own session until that target closes.
1162+
*/
1163+
const recordFrameCopies = async (tab: ServerTab, frame: Frame) => {
1164+
if (frame === tab.page.mainFrame() || tab.frameSessions.has(frame)) return;
1165+
// A frame in its parent's process has no session of its own.
1166+
const cdp = await tab.page
1167+
.context()
1168+
.newCDPSession(frame)
1169+
.catch(() => null);
1170+
if (!cdp) return;
1171+
if (tab.frameSessions.has(frame) || frame.isDetached()) {
1172+
await cdp.detach().catch(constVoid);
1173+
return;
1174+
}
1175+
tab.frameSessions.set(frame, cdp);
1176+
cdp.on("close", () => {
1177+
if (tab.frameSessions.get(frame) !== cdp) return;
1178+
tab.frameSessions.delete(frame);
1179+
// A frame that moved to another process records through its new target.
1180+
if (!frame.isDetached()) void recordFrameCopies(tab, frame);
1181+
});
1182+
await recordCopies(cdp).catch(constVoid);
1183+
};
1184+
1185+
/** Clears and returns the copy of each frame a session reaches. */
1186+
const takeSessionCopies = async (cdp: CDPSession) => {
1187+
// An out-of-process frame that closed meanwhile copied nothing.
1188+
const tree = await cdp.send("Page.getFrameTree").catch(() => null);
1189+
if (!tree) return [];
1190+
const frameIds = (node: typeof tree.frameTree): Array<string> => [
11551191
node.frame.id,
11561192
...(node.childFrames ?? []).flatMap(frameIds),
11571193
];
1158-
const copies = await Promise.all(
1159-
frameIds(frameTree).map((frameId) =>
1160-
tab.cdp
1194+
return Promise.all(
1195+
frameIds(tree.frameTree).map((frameId) =>
1196+
cdp
11611197
.send("Page.createIsolatedWorld", { frameId, worldName: CLIPBOARD_WORLD })
11621198
.then(({ executionContextId }) =>
1163-
tab.cdp.send("Runtime.evaluate", {
1199+
cdp.send("Runtime.evaluate", {
11641200
contextId: executionContextId,
11651201
expression: "globalThis.takeCopy?.()",
11661202
returnByValue: true,
@@ -1173,7 +1209,14 @@ const make = Effect.gen(function* () {
11731209
),
11741210
),
11751211
);
1176-
return copies.find((text) => text.length > 0);
1212+
};
1213+
1214+
/** Clears every frame's copy and returns what the key just handled copied or cut. */
1215+
const takeCopiedText = async (tab: ServerTab) => {
1216+
const copies = await Promise.all(
1217+
[tab.cdp, ...tab.frameSessions.values()].map(takeSessionCopies),
1218+
);
1219+
return copies.flat().find((text) => text.length > 0);
11771220
};
11781221

11791222
const adoptPopup = async (opener: ServerTab, popup: Page) => {

0 commit comments

Comments
 (0)