Open History to see the last commit
1import {
2 DesktopPreviewAnnotationThemeInputSchema,
3 DesktopPreviewArtifactInputSchema,
4 DesktopPreviewAutomationClickInputSchema,
5 DesktopPreviewAutomationEvaluateInputSchema,
6 DesktopPreviewAutomationPressInputSchema,
7 DesktopPreviewAutomationScrollInputSchema,
8 DesktopPreviewAutomationStatusSchema,
9 DesktopPreviewAutomationTypeInputSchema,
10 DesktopPreviewAutomationWaitForInputSchema,
11 DesktopPreviewConfigInputSchema,
12 DesktopPreviewNavigateInputSchema,
13 DesktopPreviewRecordingArtifactSchema,
14 DesktopPreviewRecordingSaveInputSchema,
15 DesktopPreviewRegisterWebviewInputSchema,
16 DesktopPreviewScreenshotArtifactSchema,
17 DesktopPreviewSetAudioMutedInputSchema,
18 DesktopPreviewSetColorSchemeInputSchema,
19 BrowserImportResult,
20 BrowserImportSource,
21 DesktopPreviewClearDataInputSchema,
22 DesktopPreviewImportCookiesInputSchema,
23 DesktopPreviewCreateTabInputSchema,
24 DesktopPreviewTabInputSchema,
25 DesktopPreviewWebviewConfigSchema,
26 PreviewAnnotationSubmissionResultSchema,
27 PreviewAutomationSnapshot,
28 DEFAULT_BROWSER_PROFILE_ID,
29 INCOGNITO_BROWSER_PROFILE_ID,
30} from "@t3tools/contracts";
31import * as Effect from "effect/Effect";
32import * as Schema from "effect/Schema";
33import * as Option from "effect/Option";
34import * as NodeURL from "node:url";
35
36import * as ElectronWindow from "../../electron/ElectronWindow.ts";
37import * as BrowserImport from "../../preview/BrowserImport/BrowserImport.ts";
38import * as PreviewManager from "../../preview/Manager.ts";
39import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
40import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts";
41import * as IpcChannels from "../channels.ts";
42import * as DesktopIpc from "../DesktopIpc.ts";
43
44export const installPreviewEventForwarding = Effect.fn(
45 "desktop.ipc.preview.installEventForwarding",
46)(function* () {
47 const electronWindow = yield* ElectronWindow.ElectronWindow;
48 const manager = yield* PreviewManager.PreviewManager;
49 yield* manager.subscribeStateChanges((tabId, state) =>
50 electronWindow.sendAll(IpcChannels.PREVIEW_STATE_CHANGE_CHANNEL, tabId, state),
51 );
52 yield* manager.subscribeRecordingFrames((frame) =>
53 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_FRAME_CHANNEL, frame),
54 );
55 yield* manager.subscribeRecordingInputs((event) =>
56 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_INPUT_CHANNEL, event),
57 );
58 yield* manager.subscribePointerEvents((event) =>
59 electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event),
60 );
61});
62
63export const createTab = DesktopIpc.makeIpcMethod({
64 channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
65 payload: DesktopPreviewCreateTabInputSchema,
66 result: Schema.Void,
67 handler: Effect.fn("desktop.ipc.preview.createTab")(function* ({
68 tabId,
69 zoomFactor,
70 colorScheme,
71 }) {
72 const manager = yield* PreviewManager.PreviewManager;
73 yield* manager.createTab(tabId, { zoomFactor, colorScheme });
74 }),
75});
76
77export const closeTab = DesktopIpc.makeIpcMethod({
78 channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
79 payload: DesktopPreviewTabInputSchema,
80 result: Schema.Void,
81 handler: Effect.fn("desktop.ipc.preview.closeTab")(function* ({ tabId }) {
82 const manager = yield* PreviewManager.PreviewManager;
83 yield* manager.closeTab(tabId);
84 }),
85});
86
87export const registerWebview = DesktopIpc.makeIpcMethod({
88 channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
89 payload: DesktopPreviewRegisterWebviewInputSchema,
90 result: Schema.Void,
91 handler: Effect.fn("desktop.ipc.preview.registerWebview")(function* ({ tabId, webContentsId }) {
92 const manager = yield* PreviewManager.PreviewManager;
93 yield* manager.registerWebview(tabId, webContentsId);
94 }),
95});
96
97export const navigate = DesktopIpc.makeIpcMethod({
98 channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
99 payload: DesktopPreviewNavigateInputSchema,
100 result: Schema.Void,
101 handler: Effect.fn("desktop.ipc.preview.navigate")(function* ({ tabId, url }) {
102 const manager = yield* PreviewManager.PreviewManager;
103 yield* manager.navigate(tabId, url);
104 }),
105});
106
107const tabMethod = (
108 channel: string,
109 name: string,
110 invoke: (
111 manager: PreviewManager.PreviewManager["Service"],
112 tabId: string,
113 ) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
114) =>
115 DesktopIpc.makeIpcMethod({
116 channel,
117 payload: DesktopPreviewTabInputSchema,
118 result: Schema.Void,
119 handler: Effect.fn(name)(function* ({ tabId }) {
120 const manager = yield* PreviewManager.PreviewManager;
121 yield* invoke(manager, tabId);
122 }),
123 });
124
125export const goBack = tabMethod(
126 IpcChannels.PREVIEW_GO_BACK_CHANNEL,
127 "desktop.ipc.preview.goBack",
128 (manager, tabId) => manager.goBack(tabId),
129);
130export const goForward = tabMethod(
131 IpcChannels.PREVIEW_GO_FORWARD_CHANNEL,
132 "desktop.ipc.preview.goForward",
133 (manager, tabId) => manager.goForward(tabId),
134);
135export const refresh = tabMethod(
136 IpcChannels.PREVIEW_REFRESH_CHANNEL,
137 "desktop.ipc.preview.refresh",
138 (manager, tabId) => manager.refresh(tabId),
139);
140export const zoomIn = tabMethod(
141 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
142 "desktop.ipc.preview.zoomIn",
143 (manager, tabId) => manager.zoomIn(tabId),
144);
145export const zoomOut = tabMethod(
146 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
147 "desktop.ipc.preview.zoomOut",
148 (manager, tabId) => manager.zoomOut(tabId),
149);
150export const resetZoom = tabMethod(
151 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
152 "desktop.ipc.preview.resetZoom",
153 (manager, tabId) => manager.resetZoom(tabId),
154);
155export const hardReload = tabMethod(
156 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
157 "desktop.ipc.preview.hardReload",
158 (manager, tabId) => manager.hardReload(tabId),
159);
160export const setColorScheme = DesktopIpc.makeIpcMethod({
161 channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
162 payload: DesktopPreviewSetColorSchemeInputSchema,
163 result: Schema.Void,
164 handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
165 const manager = yield* PreviewManager.PreviewManager;
166 yield* manager.setColorScheme(tabId, colorScheme);
167 }),
168});
169export const setAudioMuted = DesktopIpc.makeIpcMethod({
170 channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
171 payload: DesktopPreviewSetAudioMutedInputSchema,
172 result: Schema.Void,
173 handler: Effect.fn("desktop.ipc.preview.setAudioMuted")(function* ({ tabId, audioMuted }) {
174 const manager = yield* PreviewManager.PreviewManager;
175 yield* manager.setAudioMuted(tabId, audioMuted);
176 }),
177});
178export const openDevTools = tabMethod(
179 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
180 "desktop.ipc.preview.openDevTools",
181 (manager, tabId) => manager.openDevTools(tabId),
182);
183export const cancelPickElement = tabMethod(
184 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
185 "desktop.ipc.preview.cancelPickElement",
186 (manager, tabId) => manager.cancelPickElement(tabId),
187);
188export const startRecording = DesktopIpc.makeIpcMethod({
189 channel: IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
190 payload: DesktopPreviewTabInputSchema,
191 result: Schema.Void,
192 handler: Effect.fn("desktop.ipc.preview.startRecording")(function* ({ tabId }) {
193 const manager = yield* PreviewManager.PreviewManager;
194 const store = yield* DesktopClientSettings.DesktopClientSettings;
195 const settings = yield* store.get;
196 const options = Option.map(settings, (value) => ({
197 showKeyPresses: value.browserRecordingShowKeyPresses,
198 showMousePresses: value.browserRecordingShowMousePresses,
199 }));
200 yield* manager.startRecording(tabId, Option.getOrUndefined(options));
201 }),
202});
203export const stopRecording = tabMethod(
204 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
205 "desktop.ipc.preview.stopRecording",
206 (manager, tabId) => manager.stopRecording(tabId),
207);
208export const openPictureInPicture = tabMethod(
209 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL,
210 "desktop.ipc.preview.openPictureInPicture",
211 (manager, tabId) => manager.openPictureInPicture(tabId),
212);
213export const closePictureInPicture = tabMethod(
214 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL,
215 "desktop.ipc.preview.closePictureInPicture",
216 (manager, tabId) => manager.closePictureInPicture(tabId),
217);
218
219export const clearCookies = DesktopIpc.makeIpcMethod({
220 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
221 payload: DesktopPreviewClearDataInputSchema,
222 result: Schema.Void,
223 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* ({ environmentId, profileId }) {
224 const manager = yield* PreviewManager.PreviewManager;
225 yield* manager.clearCookies(yield* resolveClearPartitions(manager, environmentId, profileId));
226 }),
227});
228
229export const clearCache = DesktopIpc.makeIpcMethod({
230 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
231 payload: DesktopPreviewClearDataInputSchema,
232 result: Schema.Void,
233 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* ({ environmentId, profileId }) {
234 const manager = yield* PreviewManager.PreviewManager;
235 yield* manager.clearCache(yield* resolveClearPartitions(manager, environmentId, profileId));
236 }),
237});
238
239/**
240 * Partition scope for an (environment, profile) pair.
241 *
242 * The default profile keeps the bare environment id it used before profiles
243 * existed, so upgrading does not strand anyone's existing logins in an
244 * orphaned partition. Incognito derives a non-persistent partition.
245 */
246export function resolvePartitionScope(
247 environmentId: string,
248 profileId: string | undefined,
249): {
250 readonly scope: string;
251 readonly persistent: boolean;
252 readonly namespace?: "profile";
253} {
254 if (profileId === undefined || profileId === DEFAULT_BROWSER_PROFILE_ID) {
255 return { scope: environmentId, persistent: true };
256 }
257 // JSON's tuple framing is injective for strings, including lone UTF-16
258 // surrogates (which it escapes). URI encoding throws on those supported ids,
259 // while replacing them with U+FFFD would collapse distinct identities.
260 return {
261 scope: JSON.stringify([environmentId, profileId]),
262 persistent: profileId !== INCOGNITO_BROWSER_PROFILE_ID,
263 namespace: "profile" as const,
264 };
265}
266
267/**
268 * Clearing without a profile keeps the historical "everything" behaviour for
269 * an explicit all-profiles action; naming a profile confines it to that
270 * profile's partition so one profile's sign-out cannot reach the others.
271 */
272const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartitions")(function* (
273 manager: PreviewManager.PreviewManager["Service"],
274 environmentId: string,
275 profileId: string | undefined,
276) {
277 if (profileId === undefined) return undefined;
278 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
279 // Loading the session is what puts the partition in the map the clear walks.
280 // Deriving the partition string alone leaves nothing to match, so clearing a
281 // profile with no tab open this run — after a restart, or when deleting a
282 // profile — would report success and delete nothing.
283 yield* manager.getBrowserSession(scope, persistent, namespace);
284 return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
285});
286
287export const getPreviewConfig = DesktopIpc.makeIpcMethod({
288 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
289 payload: DesktopPreviewConfigInputSchema,
290 result: DesktopPreviewWebviewConfigSchema,
291 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId, profileId }) {
292 const manager = yield* PreviewManager.PreviewManager;
293 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
294 // Creating the session first is what installs the UA rewrite and permission
295 // handlers; a guest that attached to an untouched partition would run with
296 // Electron's default UA and Chromium's default permission behaviour.
297 yield* manager.getBrowserSession(scope, persistent, namespace);
298 return {
299 partition: yield* manager.getBrowserPartition(scope, persistent, namespace),
300 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
301 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
302 };
303 }),
304});
305
306/**
307 * Registered separately from `methods`: these carry `BrowserImport` in their
308 * context and their own failure type, so they do not unify with the
309 * manager-backed handlers the shared loop iterates.
310 */
311export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
312 channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL,
313 payload: Schema.Void,
314 result: Schema.Array(BrowserImportSource),
315 handler: Effect.fn("desktop.ipc.preview.listBrowserImportSources")(function* () {
316 const browserImport = yield* BrowserImport.BrowserImport;
317 return yield* browserImport.listSources;
318 }),
319});
320
321export const importBrowserCookies = DesktopIpc.makeIpcMethod({
322 channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL,
323 payload: DesktopPreviewImportCookiesInputSchema,
324 result: BrowserImportResult,
325 handler: Effect.fn("desktop.ipc.preview.importBrowserCookies")(function* ({
326 environmentId,
327 ...importInput
328 }) {
329 const browserImport = yield* BrowserImport.BrowserImport;
330 // Derived in main from the same helper the webview config uses, so cookies
331 // land in exactly the partition the profile's tabs attach to.
332 const { scope, persistent, namespace } = resolvePartitionScope(
333 environmentId,
334 importInput.targetProfileId,
335 );
336 return yield* browserImport.importCookies({
337 input: importInput,
338 scope,
339 persistent,
340 ...(namespace === undefined ? {} : { namespace }),
341 });
342 }),
343});
344
345export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
346 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
347 payload: DesktopPreviewAnnotationThemeInputSchema,
348 result: Schema.Void,
349 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
350 const manager = yield* PreviewManager.PreviewManager;
351 yield* manager.setAnnotationTheme(theme);
352 }),
353});
354
355export const pickElement = DesktopIpc.makeIpcMethod({
356 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
357 payload: DesktopPreviewTabInputSchema,
358 result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
359 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId }) {
360 const manager = yield* PreviewManager.PreviewManager;
361 return yield* manager.pickElement(tabId);
362 }),
363});
364
365export const captureScreenshot = DesktopIpc.makeIpcMethod({
366 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
367 payload: DesktopPreviewTabInputSchema,
368 result: DesktopPreviewScreenshotArtifactSchema,
369 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
370 const manager = yield* PreviewManager.PreviewManager;
371 return yield* manager.captureScreenshot(tabId);
372 }),
373});
374
375export const revealArtifact = DesktopIpc.makeIpcMethod({
376 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
377 payload: DesktopPreviewArtifactInputSchema,
378 result: Schema.Void,
379 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
380 const manager = yield* PreviewManager.PreviewManager;
381 yield* manager.revealArtifact(path);
382 }),
383});
384
385export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
386 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
387 payload: DesktopPreviewArtifactInputSchema,
388 result: Schema.Void,
389 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
390 const manager = yield* PreviewManager.PreviewManager;
391 yield* manager.copyArtifactToClipboard(path);
392 }),
393});
394
395export const automationStatus = DesktopIpc.makeIpcMethod({
396 channel: IpcChannels.PREVIEW_AUTOMATION_STATUS_CHANNEL,
397 payload: DesktopPreviewTabInputSchema,
398 result: DesktopPreviewAutomationStatusSchema,
399 handler: Effect.fn("desktop.ipc.preview.automationStatus")(function* ({ tabId }) {
400 const manager = yield* PreviewManager.PreviewManager;
401 return yield* manager.automationStatus(tabId);
402 }),
403});
404
405export const automationSnapshot = DesktopIpc.makeIpcMethod({
406 channel: IpcChannels.PREVIEW_AUTOMATION_SNAPSHOT_CHANNEL,
407 payload: DesktopPreviewTabInputSchema,
408 result: PreviewAutomationSnapshot,
409 handler: Effect.fn("desktop.ipc.preview.automationSnapshot")(function* ({ tabId }) {
410 const manager = yield* PreviewManager.PreviewManager;
411 return yield* manager.automationSnapshot(tabId);
412 }),
413});
414
415export const automationClick = DesktopIpc.makeIpcMethod({
416 channel: IpcChannels.PREVIEW_AUTOMATION_CLICK_CHANNEL,
417 payload: DesktopPreviewAutomationClickInputSchema,
418 result: Schema.Void,
419 handler: Effect.fn("desktop.ipc.preview.automationClick")(function* ({ tabId, input }) {
420 const manager = yield* PreviewManager.PreviewManager;
421 yield* manager.automationClick(tabId, input);
422 }),
423});
424
425export const automationType = DesktopIpc.makeIpcMethod({
426 channel: IpcChannels.PREVIEW_AUTOMATION_TYPE_CHANNEL,
427 payload: DesktopPreviewAutomationTypeInputSchema,
428 result: Schema.Void,
429 handler: Effect.fn("desktop.ipc.preview.automationType")(function* ({ tabId, input }) {
430 const manager = yield* PreviewManager.PreviewManager;
431 yield* manager.automationType(tabId, input);
432 }),
433});
434
435export const automationPress = DesktopIpc.makeIpcMethod({
436 channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL,
437 payload: DesktopPreviewAutomationPressInputSchema,
438 result: Schema.Void,
439 handler: Effect.fn("desktop.ipc.preview.automationPress")(function* ({ tabId, input }) {
440 const manager = yield* PreviewManager.PreviewManager;
441 yield* manager.automationPress(tabId, input);
442 }),
443});
444
445export const automationScroll = DesktopIpc.makeIpcMethod({
446 channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL,
447 payload: DesktopPreviewAutomationScrollInputSchema,
448 result: Schema.Void,
449 handler: Effect.fn("desktop.ipc.preview.automationScroll")(function* ({ tabId, input }) {
450 const manager = yield* PreviewManager.PreviewManager;
451 yield* manager.automationScroll(tabId, input);
452 }),
453});
454
455export const automationEvaluate = DesktopIpc.makeIpcMethod({
456 channel: IpcChannels.PREVIEW_AUTOMATION_EVALUATE_CHANNEL,
457 payload: DesktopPreviewAutomationEvaluateInputSchema,
458 result: Schema.Unknown,
459 handler: Effect.fn("desktop.ipc.preview.automationEvaluate")(function* ({ tabId, input }) {
460 const manager = yield* PreviewManager.PreviewManager;
461 return yield* manager.automationEvaluate(tabId, input);
462 }),
463});
464
465export const automationWaitFor = DesktopIpc.makeIpcMethod({
466 channel: IpcChannels.PREVIEW_AUTOMATION_WAIT_FOR_CHANNEL,
467 payload: DesktopPreviewAutomationWaitForInputSchema,
468 result: Schema.Void,
469 handler: Effect.fn("desktop.ipc.preview.automationWaitFor")(function* ({ tabId, input }) {
470 const manager = yield* PreviewManager.PreviewManager;
471 yield* manager.automationWaitFor(tabId, input);
472 }),
473});
474
475export const saveRecording = DesktopIpc.makeIpcMethod({
476 channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
477 payload: DesktopPreviewRecordingSaveInputSchema,
478 result: DesktopPreviewRecordingArtifactSchema,
479 handler: Effect.fn("desktop.ipc.preview.saveRecording")(function* ({ tabId, mimeType, data }) {
480 const manager = yield* PreviewManager.PreviewManager;
481 return yield* manager.saveRecording(tabId, mimeType, data);
482 }),
483});
484
485export const methods = [
486 createTab,
487 closeTab,
488 registerWebview,
489 navigate,
490 goBack,
491 goForward,
492 refresh,
493 zoomIn,
494 zoomOut,
495 resetZoom,
496 hardReload,
497 setColorScheme,
498 setAudioMuted,
499 openDevTools,
500 clearCookies,
501 clearCache,
502 getPreviewConfig,
503 setAnnotationTheme,
504 pickElement,
505 cancelPickElement,
506 captureScreenshot,
507 revealArtifact,
508 copyArtifactToClipboard,
509 openPictureInPicture,
510 closePictureInPicture,
511 automationStatus,
512 automationSnapshot,
513 automationClick,
514 automationType,
515 automationPress,
516 automationScroll,
517 automationEvaluate,
518 automationWaitFor,
519 startRecording,
520 stopRecording,
521 saveRecording,
522] as const;