Skip to content

Commit f4fceb8

Browse files
committed
fix(desktop): serialize preview pointer commands
1 parent 38becbb commit f4fceb8

2 files changed

Lines changed: 91 additions & 5 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,69 @@ describe("preview guest input", () => {
124124
},
125125
);
126126

127+
it.each([
128+
{
129+
method: "Input.dispatchMouseEvent",
130+
press: "mousePressed",
131+
release: "mouseReleased",
132+
failPress: false,
133+
},
134+
{
135+
method: "Input.dispatchTouchEvent",
136+
press: "touchStart",
137+
release: "touchEnd",
138+
failPress: false,
139+
},
140+
{
141+
method: "Input.dispatchMouseEvent",
142+
press: "mousePressed",
143+
release: "mouseReleased",
144+
failPress: true,
145+
},
146+
])(
147+
"keeps $method releases behind presses waiting for host focus, failed press: $failPress",
148+
async ({ method, press, release, failPress }) => {
149+
const captureStarted = Promise.withResolvers<void>();
150+
const finishCapture = Promise.withResolvers<void>();
151+
const events: Array<unknown> = [];
152+
const guest = {
153+
id: 7,
154+
focusedFrame: null,
155+
hostWebContents: {
156+
isDestroyed: () => false,
157+
executeJavaScript: async () => {
158+
captureStarted.resolve();
159+
await finishCapture.promise;
160+
},
161+
},
162+
};
163+
const debuggee = {
164+
sendCommand: async (_method: string, params: Record<string, unknown>) => {
165+
events.push(params["type"]);
166+
if (failPress && params["type"] === press) throw new Error("Press failed");
167+
return {};
168+
},
169+
};
170+
const send = (type: string) =>
171+
sendPageCommand(
172+
guest as unknown as Electron.WebContents,
173+
debuggee as unknown as Electron.Debugger,
174+
method,
175+
{ type },
176+
);
177+
const pressed = send(press);
178+
await captureStarted.promise;
179+
const released = send(release);
180+
finishCapture.resolve();
181+
const results = await Promise.allSettled([pressed, released]);
182+
expect(results.map((result) => result.status)).toEqual([
183+
failPress ? "rejected" : "fulfilled",
184+
"fulfilled",
185+
]);
186+
expect(events).toEqual([press, release]);
187+
},
188+
);
189+
127190
it("refuses input when a cross-site frame holds the unfocused page's focus", async () => {
128191
const guest = makeGuest({ focused: false, focusedDocument: false });
129192
await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow(

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ const sendPointerPress = async (
126126
}
127127
};
128128

129+
const pointerCommands = new WeakMap<Electron.WebContents, Promise<unknown>>();
130+
131+
/** Focus capture crosses processes; later pointer events must not overtake the press. */
132+
const sendPointerCommand = async (
133+
webContents: Electron.WebContents,
134+
debuggee: Electron.Debugger,
135+
method: string,
136+
params: Record<string, unknown>,
137+
sessionId: string | undefined,
138+
) => {
139+
const previous = pointerCommands.get(webContents) ?? Promise.resolve();
140+
const current = previous
141+
.catch(() => {})
142+
.then(() =>
143+
params["type"] === "mousePressed" || params["type"] === "touchStart"
144+
? sendPointerPress(webContents, debuggee, method, params, sessionId)
145+
: debuggee.sendCommand(method, params, sessionId),
146+
);
147+
pointerCommands.set(webContents, current);
148+
try {
149+
return await current;
150+
} finally {
151+
if (pointerCommands.get(webContents) === current) pointerCommands.delete(webContents);
152+
}
153+
};
154+
129155
/** Sends one command to the guest's page, keeping reloads and input inside it. */
130156
export const sendPageCommand = async (
131157
webContents: Electron.WebContents,
@@ -134,11 +160,8 @@ export const sendPageCommand = async (
134160
params: Record<string, unknown>,
135161
sessionId?: string,
136162
): Promise<unknown> => {
137-
if (
138-
(method === "Input.dispatchMouseEvent" && params["type"] === "mousePressed") ||
139-
(method === "Input.dispatchTouchEvent" && params["type"] === "touchStart")
140-
) {
141-
return sendPointerPress(webContents, debuggee, method, params, sessionId);
163+
if (method === "Input.dispatchMouseEvent" || method === "Input.dispatchTouchEvent") {
164+
return sendPointerCommand(webContents, debuggee, method, params, sessionId);
142165
}
143166
if (sessionId !== undefined) return debuggee.sendCommand(method, params, sessionId);
144167
// Electron's debugger routes Page.reload on a webview to its embedder window.

0 commit comments

Comments
 (0)