Skip to content

Commit 6a4ba28

Browse files
committed
fix(desktop): detach popup child debugger sessions
1 parent f34f0a9 commit 6a4ba28

3 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎apps/desktop/src/preview/CdpRelay.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe("CDP relay", () => {
5353
);
5454
const written: Array<Record<string, unknown>> = [];
5555
const relay = makeRelay(makeTarget(send), (raw) => written.push(JSON.parse(raw)));
56+
relay.event("Target.attachedToTarget", { sessionId: "iframe-session" }, "");
5657
relay.receive(
5758
JSON.stringify({ id: 5, method: "Page.captureScreenshot", sessionId: "t3-preview-page" }),
5859
);
@@ -76,6 +77,7 @@ describe("CDP relay", () => {
7677
it("resumes paused child frames, so cross-site iframes run", async () => {
7778
const send = vi.fn(async () => ({}));
7879
const relay = makeRelay(makeTarget(send), () => {});
80+
relay.event("Target.attachedToTarget", { sessionId: "iframe-session" }, "");
7981
relay.receive(
8082
JSON.stringify({
8183
id: 1,
@@ -185,6 +187,59 @@ describe("CDP relay popups", () => {
185187
const resultOf = (written: Array<Record<string, unknown>>, id: number) =>
186188
written.find((message) => message["id"] === id)!;
187189

190+
it.each(["browser", "popup", "child"])(
191+
"detaches a popup child through its %s session without affecting the other sessions",
192+
async (source) => {
193+
const pageSend = vi.fn(async () => ({}));
194+
const popupSend = vi.fn(async () => ({}));
195+
const written: Array<Record<string, unknown>> = [];
196+
const relay = makeRelay(makeTarget(pageSend), (raw) => written.push(JSON.parse(raw)));
197+
const popup = relay.addPopup(makePopup("POPUP", popupSend));
198+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach" }));
199+
await settle();
200+
const popupSession = "t3-preview-popup-1";
201+
popup.event("Target.attachedToTarget", { sessionId: "parent-frame" }, "");
202+
popup.event("Target.attachedToTarget", { sessionId: "popup-child" }, "parent-frame");
203+
popup.event("Target.attachedToTarget", { sessionId: "sibling-frame" }, "");
204+
relay.receive(JSON.stringify({ id: 2, method: "DOM.enable", sessionId: "popup-child" }));
205+
await settle();
206+
expect(popupSend).toHaveBeenLastCalledWith("DOM.enable", {}, "popup-child", undefined);
207+
208+
const sessionId =
209+
source === "browser" ? undefined : source === "popup" ? popupSession : "parent-frame";
210+
relay.receive(
211+
JSON.stringify({
212+
id: 3,
213+
method: "Target.detachFromTarget",
214+
params: { sessionId: "popup-child" },
215+
sessionId,
216+
}),
217+
);
218+
await settle();
219+
expect(popupSend).toHaveBeenLastCalledWith(
220+
"Target.detachFromTarget",
221+
{ sessionId: "popup-child" },
222+
"parent-frame",
223+
undefined,
224+
);
225+
expect(resultOf(written, 3)).toHaveProperty("result");
226+
227+
popupSend.mockClear();
228+
relay.receive(JSON.stringify({ id: 4, method: "DOM.enable", sessionId: "popup-child" }));
229+
relay.receive(JSON.stringify({ id: 5, method: "Page.enable", sessionId: popupSession }));
230+
relay.receive(JSON.stringify({ id: 6, method: "DOM.enable", sessionId: "sibling-frame" }));
231+
relay.receive(JSON.stringify({ id: 7, method: "Page.enable", sessionId: "t3-preview-page" }));
232+
await settle();
233+
expect(resultOf(written, 4)).toHaveProperty("error");
234+
expect(popupSend.mock.calls).toEqual([
235+
["Page.enable", {}, undefined, undefined],
236+
["DOM.enable", {}, "sibling-frame", undefined],
237+
]);
238+
expect(pageSend.mock.calls).toEqual([["Page.enable", {}, undefined, undefined]]);
239+
for (const id of [5, 6, 7]) expect(resultOf(written, id)).toHaveProperty("result");
240+
},
241+
);
242+
188243
it("announces the page's popups after it, as its popups, and routes them separately", async () => {
189244
const pageSend = vi.fn(async () => ({}));
190245
const earlySend = vi.fn(async () => ({}));

‎apps/desktop/src/preview/CdpRelay.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ export function createCdpRelayConnection(
140140
const sessions = new Map<string, RelaySession>([
141141
[PAGE_SESSION_ID, { kind: "page", owner: page, parent: undefined }],
142142
]);
143-
/** Child frame and worker sessions, by the debugger that attached them; others are the page's. */
144-
const children = new Map<string, RelayedTarget>();
143+
const children = new Map<
144+
string,
145+
{ readonly owner: RelayedTarget; readonly parent: string | undefined }
146+
>();
145147
let sessionSequence = 0;
146148
const newSession = (session: RelaySession) => {
147149
const sessionId = `t3-preview-${session.kind}-${++sessionSequence}`;
@@ -246,7 +248,16 @@ export function createCdpRelayConnection(
246248
case "Target.detachFromTarget": {
247249
const sessionId = command.params?.["sessionId"];
248250
const session = typeof sessionId === "string" ? sessions.get(sessionId) : undefined;
249-
if (session?.kind === "page" && sessionId !== session.owner.sessionId) {
251+
const child = typeof sessionId === "string" ? children.get(sessionId) : undefined;
252+
if (child !== undefined) {
253+
await child.owner.target.send(
254+
command.method,
255+
command.params ?? {},
256+
child.parent,
257+
undefined,
258+
);
259+
children.delete(sessionId as string);
260+
} else if (session?.kind === "page" && sessionId !== session.owner.sessionId) {
250261
sessions.delete(sessionId as string);
251262
session.owner.target.sessionDetached?.(sessionId as string);
252263
} else if (session?.kind === "browser") {
@@ -275,7 +286,9 @@ export function createCdpRelayConnection(
275286
const pageCommand = (command: CdpCommand): Promise<unknown> => {
276287
const session = sessions.get(command.sessionId!);
277288
const child = session?.kind === "page" ? undefined : command.sessionId;
278-
const owner = session?.kind === "page" ? session.owner : (children.get(child!) ?? page);
289+
const owner = session?.kind === "page" ? session.owner : children.get(child!)?.owner;
290+
if (owner === undefined) return Promise.reject(new Error("Unknown or detached CDP session"));
291+
if (command.method === "Target.detachFromTarget") return browserCommand(command);
279292
const extra = child === undefined && command.sessionId !== owner.sessionId;
280293
// Child frames/workers pause on auto-attach until resumed; only a main page can skip this command.
281294
if (command.method === "Runtime.runIfWaitingForDebugger" && child === undefined) {
@@ -296,7 +309,7 @@ export function createCdpRelayConnection(
296309
(method, params, sessionId) => {
297310
const child = (params as { sessionId?: unknown } | null | undefined)?.sessionId;
298311
if (method === "Target.attachedToTarget" && typeof child === "string") {
299-
children.set(child, relayed);
312+
children.set(child, { owner: relayed, parent: sessionId || undefined });
300313
} else if (method === "Target.detachedFromTarget" && typeof child === "string") {
301314
children.delete(child);
302315
}
@@ -321,7 +334,7 @@ export function createCdpRelayConnection(
321334
if (relayed.removed) return;
322335
relayed.removed = true;
323336
targets.delete(relayed);
324-
for (const [id, owner] of children) if (owner === relayed) children.delete(id);
337+
for (const [id, child] of children) if (child.owner === relayed) children.delete(id);
325338
const targetId = relayed.targetId;
326339
if (targetId === null) return;
327340
for (const [id, session] of sessions) {

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ describe("DesktopBrowserHost", () => {
678678
yield* settle;
679679
debuggee.release();
680680
expect(yield* Queue.take(replies)).toBe(1);
681+
debuggee.emit("Target.attachedToTarget", { sessionId: "child-frame" });
681682
yield* raw({ id: 2, method: "Page.startScreencast", sessionId: "child-frame" });
682683
expect(yield* Queue.take(replies)).toBe(2);
683684
yield* TestClock.adjust("1 minute");
@@ -703,11 +704,13 @@ describe("DesktopBrowserHost", () => {
703704
});
704705
expect(yield* Queue.take(replies)).toBe(2);
705706
yield* raw({ id: 3, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
707+
expect(yield* Queue.take(replies)).toBe(3);
706708
yield* settle;
707709
debuggee.release();
708710
yield* settle;
709711
yield* TestClock.adjust("1 minute");
710-
expect(debuggee.focusEmulation).toEqual([true, false]);
712+
expect(debuggee.focusEmulation).toEqual([]);
713+
expect(debuggee.sent).toEqual([]);
711714
}).pipe(Effect.scoped),
712715
);
713716

0 commit comments

Comments
 (0)