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