Repository navigation
fix(server): a busy embedded frame no longer holds copy keys back #34204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fffb0a7
b6e0b78
7754818
8ce3250
f5f438f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,6 +295,10 @@ interface ServerTab { | |
| readonly page: Page; | ||
| readonly cdp: CDPSession; | ||
| readonly frameSessions: Map<Frame, CDPSession>; | ||
| /** Copy reads run in key order, outside the input queue. */ | ||
| copyReads: Promise<void>; | ||
| /** When the last copy or cut key was acknowledged; the next one starts after it. */ | ||
| copyKeyedUntil: number; | ||
| readonly createdAt: number; | ||
| usedAt: number; | ||
| readonly viewers: Set<ViewerState>; | ||
|
|
@@ -427,14 +431,15 @@ const CLIPBOARD_WORLD = "t3-preview-clipboard"; | |
| // Chromium shares its clipboard across contexts; capture real copy/cut keys in a page-inaccessible world instead. | ||
| const CLIPBOARD_SCRIPT = `(() => { | ||
| let keying = false; | ||
| let copied = ""; | ||
| // One entry per copy or cut key, oldest first, so a later key cannot overwrite an unread copy. | ||
| const copies = []; | ||
| // The same Ctrl or Cmd + C or X that editingCommand sends as copy or cut. | ||
| addEventListener("keydown", (event) => { | ||
| if (!event.isTrusted || event.altKey || event.ctrlKey === event.metaKey) return; | ||
| const key = event.key.toLowerCase(); | ||
| if (key !== "c" && key !== "x") return; | ||
| keying = true; | ||
| copied = ""; | ||
| copies.push({ at: Date.now(), text: "" }); | ||
| setTimeout(() => { keying = false; }); | ||
| }, true); | ||
| const selection = () => { | ||
|
|
@@ -445,7 +450,8 @@ const CLIPBOARD_SCRIPT = `(() => { | |
| return String(document.getSelection() ?? ""); | ||
| }; | ||
| const record = (text) => { | ||
| copied = String(text ?? "").slice(0, ${CLIPBOARD_TEXT_LIMIT}); | ||
| const copy = copies.at(-1); | ||
| if (copy) copy.text = String(text ?? "").slice(0, ${CLIPBOARD_TEXT_LIMIT}); | ||
| }; | ||
| // Capturing at window runs before page handlers can stop the event, and | ||
| // before cut removes the selection. | ||
|
|
@@ -460,10 +466,10 @@ const CLIPBOARD_SCRIPT = `(() => { | |
| addEventListener(type, snapshot, true); | ||
| addEventListener(type, replace); | ||
| } | ||
| globalThis.takeCopy = () => { | ||
| const text = copied; | ||
| copied = ""; | ||
| return text; | ||
| // A key's copy was keyed between its dispatch and acknowledgement; older copies are stale. | ||
| globalThis.takeCopy = (since, until) => { | ||
| while (copies.length > 0 && copies[0].at < since) copies.shift(); | ||
| return copies[0] && copies[0].at <= until ? copies.shift().text : ""; | ||
| }; | ||
| })();`; | ||
|
|
||
|
|
@@ -867,6 +873,8 @@ const make = Effect.gen(function* () { | |
| page, | ||
| cdp, | ||
| frameSessions: new Map(), | ||
| copyReads: Promise.resolve(), | ||
| copyKeyedUntil: 0, | ||
| createdAt: Date.now(), | ||
| usedAt: Date.now(), | ||
| viewers: new Set(), | ||
|
|
@@ -1310,7 +1318,7 @@ const make = Effect.gen(function* () { | |
| await recordCopies(cdp).catch(constVoid); | ||
| }; | ||
|
|
||
| const takeSessionCopies = async (cdp: CDPSession) => { | ||
| const takeSessionCopies = async (cdp: CDPSession, since: number, until: number) => { | ||
| const tree = await cdp.send("Page.getFrameTree").catch(() => null); | ||
| if (!tree) return []; | ||
| const frameIds = (node: typeof tree.frameTree): Array<string> => [ | ||
|
|
@@ -1324,7 +1332,7 @@ const make = Effect.gen(function* () { | |
| .then(({ executionContextId }) => | ||
| cdp.send("Runtime.evaluate", { | ||
| contextId: executionContextId, | ||
| expression: "globalThis.takeCopy?.()", | ||
| expression: `globalThis.takeCopy?.(${since}, ${until})`, | ||
| returnByValue: true, | ||
| }), | ||
| ) | ||
|
|
@@ -1336,9 +1344,9 @@ const make = Effect.gen(function* () { | |
| ); | ||
| }; | ||
|
|
||
| const takeCopiedText = async (tab: ServerTab) => { | ||
| const takeCopiedText = async (tab: ServerTab, since: number, until: number) => { | ||
| const copies = await Promise.all( | ||
| [tab.cdp, ...tab.frameSessions.values()].map(takeSessionCopies), | ||
| [tab.cdp, ...tab.frameSessions.values()].map((cdp) => takeSessionCopies(cdp, since, until)), | ||
| ); | ||
| return copies.flat().find((text) => text.length > 0); | ||
| }; | ||
|
|
@@ -2447,7 +2455,10 @@ const make = Effect.gen(function* () { | |
| const copying = | ||
| !tab.desktop && (command?.commands[0] === "copy" || command?.commands[0] === "cut"); | ||
|
|
||
| if (copying) await takeCopiedText(tab); | ||
| // Copy keys get disjoint millisecond windows so each frame's copy belongs to one key. | ||
| if (copying) | ||
| while (Date.now() <= tab.copyKeyedUntil) await sleepUntil(tab.copyKeyedUntil + 1); | ||
|
Comment on lines
+2459
to
+2460
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve copies until their reads complete. This wait does not wait for Preserve unread entries across navigation, or prevent navigation from discarding them. (raw.githubusercontent.com)
|
||
| const since = Date.now(); | ||
| await session.send("Input.dispatchKeyEvent", { | ||
| type: text ? "keyDown" : "rawKeyDown", | ||
| key, | ||
|
|
@@ -2461,11 +2472,16 @@ const make = Effect.gen(function* () { | |
| ), | ||
| }); | ||
| viewer.pressedKeys.set(code || key, { key, code }); | ||
| if (copying) { | ||
| const copied = await takeCopiedText(tab); | ||
| if (!copying) return; | ||
| const until = Date.now(); | ||
| tab.copyKeyedUntil = until; | ||
| // A busy frame answers late; its read must not hold later input. | ||
| const read = tab.copyReads.then(async () => { | ||
| const copied = await takeCopiedText(tab, since, until); | ||
| if (copied) viewer.push({ _tag: "clipboard", text: copied }); | ||
| } | ||
| return; | ||
| }); | ||
| tab.copyReads = read; | ||
| return () => read; | ||
| } | ||
| case "text": | ||
| if (typeof message.text === "string" && message.text.length > 0) { | ||
|
|
@@ -2721,8 +2737,9 @@ const make = Effect.gen(function* () { | |
| : {}), | ||
| }); | ||
| } else { | ||
| const dispatch = (input: Record<string, unknown>) => | ||
| dispatchViewerInput(tab, session, viewer, input); | ||
| const dispatch = async (input: Record<string, unknown>) => { | ||
| await dispatchViewerInput(tab, session, viewer, input); | ||
| }; | ||
| if ( | ||
| message.type === "mouse" && | ||
| message.action === "move" && | ||
|
|
@@ -2749,7 +2766,10 @@ const make = Effect.gen(function* () { | |
| }), | ||
| ); | ||
| } else { | ||
| await tab.control.human(viewer.id, () => dispatch(message)); | ||
| const copyRead = await tab.control.human(viewer.id, () => | ||
| dispatchViewerInput(tab, session, viewer, message), | ||
| ); | ||
| await copyRead?.(); | ||
| } | ||
| } | ||
| } catch { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bound the pending clipboard entries.
When an embedded-frame read stalls, later copy keys still reach the responsive main frame. Each key adds an entry containing up to
CLIPBOARD_TEXT_LIMITcharacters. However,tab.copyReadsprevents subsequent reads from draining those entries until the stalled read finishes.Repeated copy keys can therefore retain an unbounded amount of clipboard text. Bound the queue and define its overflow behavior.