Open History to see the last commit
1import {
2 DesktopPreviewAnnotationSendEnabledInputSchema,
3 DesktopPreviewAnnotationThemeInputSchema,
4 DesktopPreviewAnnotationVoiceStateSchema,
5 DesktopPreviewPickElementInputSchema,
6 DesktopPreviewArtifactInputSchema,
7 DesktopPreviewConfigInputSchema,
8 DesktopPreviewFindInPageInputSchema,
9 DesktopPreviewNavigateInputSchema,
10 DesktopPreviewRecordingArtifactSchema,
11 DesktopPreviewRecordingSaveInputSchema,
12 DesktopPreviewRegisterWebviewInputSchema,
13 DesktopPreviewScreenshotArtifactSchema,
14 DesktopPreviewSetAudioMutedInputSchema,
15 DesktopPreviewSetShownInputSchema,
16 DesktopPreviewSetColorSchemeInputSchema,
17 DesktopPreviewSetZoomFactorInputSchema,
18 BrowserImportResult,
19 BrowserImportSource,
20 DesktopPreviewClearDataInputSchema,
21 DesktopPreviewImportCookiesInputSchema,
22 DesktopPreviewCreateTabInputSchema,
23 DesktopPreviewTabInputSchema,
24 DesktopPreviewWebviewConfigSchema,
25 PreviewAnnotationSubmissionResultSchema,
26 PreviewCaptureFailure,
27 DEFAULT_BROWSER_PROFILE_ID,
28 INCOGNITO_BROWSER_PROFILE_ID,
29 PreviewForwardedShortcut,
30 MAX_KEYBINDINGS_COUNT,
31} from "@t3tools/contracts";
32import * as Effect from "effect/Effect";
33import * as Schema from "effect/Schema";
34import * as Option from "effect/Option";
35import * as NodeURL from "node:url";
36
37import * as ElectronWindow from "../../electron/ElectronWindow.ts";
38import * as BrowserImport from "../../preview/BrowserImport/BrowserImport.ts";
39import * as PreviewManager from "../../preview/Manager.ts";
40import { normalizeCaptureFailure } from "../../preview/captureErrors.ts";
41import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
42import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts";
43import * as IpcChannels from "../channels.ts";
44import * as DesktopIpc from "../DesktopIpc.ts";
45
46export const installPreviewEventForwarding = Effect.fn(
47 "desktop.ipc.preview.installEventForwarding",
48)(function* () {
49 const electronWindow = yield* ElectronWindow.ElectronWindow;
50 const manager = yield* PreviewManager.PreviewManager;
51 yield* manager.subscribeStateChanges((tabId, state) =>
52 electronWindow.sendAll(IpcChannels.PREVIEW_STATE_CHANGE_CHANNEL, tabId, state),
53 );
54 yield* manager.subscribeRecordingInputs((event) =>
55 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_INPUT_CHANNEL, event),
56 );
57 yield* manager.subscribePointerEvents((event) =>
58 electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event),
59 );
60 yield* manager.subscribeOpenLinks((event) =>
61 electronWindow.sendAll(IpcChannels.PREVIEW_OPEN_LINK_CHANNEL, event),
62 );
63 yield* manager.subscribeFindResults((event) =>
64 electronWindow.sendAll(IpcChannels.PREVIEW_FIND_RESULT_CHANNEL, event),
65 );
66});
67
68const setForwardedShortcuts = DesktopIpc.makeIpcMethod({
69 channel: IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL,
70 payload: Schema.Array(PreviewForwardedShortcut).check(Schema.isMaxLength(MAX_KEYBINDINGS_COUNT)),
71 result: Schema.Void,
72 handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* (shortcuts) {
73 const manager = yield* PreviewManager.PreviewManager;
74 yield* manager.setForwardedShortcuts(shortcuts);
75 }),
76});
77
78const createTab = DesktopIpc.makeIpcMethod({
79 channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
80 payload: DesktopPreviewCreateTabInputSchema,
81 result: Schema.Void,
82 handler: Effect.fn("desktop.ipc.preview.createTab")(function* ({
83 tabId,
84 zoomFactor,
85 colorScheme,
86 serverTab,
87 }) {
88 const manager = yield* PreviewManager.PreviewManager;
89 yield* manager.createTab(tabId, { zoomFactor, colorScheme, serverTab });
90 }),
91});
92
93const closeTab = DesktopIpc.makeIpcMethod({
94 channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
95 payload: DesktopPreviewTabInputSchema,
96 result: Schema.Void,
97 handler: Effect.fn("desktop.ipc.preview.closeTab")(function* ({ tabId }) {
98 const manager = yield* PreviewManager.PreviewManager;
99 yield* manager.closeTab(tabId);
100 }),
101});
102
103const registerWebview = DesktopIpc.makeIpcMethod({
104 channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
105 payload: DesktopPreviewRegisterWebviewInputSchema,
106 result: Schema.Void,
107 handler: Effect.fn("desktop.ipc.preview.registerWebview")(function* ({ tabId, webContentsId }) {
108 const manager = yield* PreviewManager.PreviewManager;
109 yield* manager.registerWebview(tabId, webContentsId);
110 }),
111});
112
113const navigate = DesktopIpc.makeIpcMethod({
114 channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
115 payload: DesktopPreviewNavigateInputSchema,
116 result: Schema.Void,
117 handler: Effect.fn("desktop.ipc.preview.navigate")(function* ({ tabId, url }) {
118 const manager = yield* PreviewManager.PreviewManager;
119 yield* manager.navigate(tabId, url);
120 }),
121});
122
123const findInPage = DesktopIpc.makeIpcMethod({
124 channel: IpcChannels.PREVIEW_FIND_IN_PAGE_CHANNEL,
125 payload: DesktopPreviewFindInPageInputSchema,
126 result: Schema.Void,
127 handler: Effect.fn("desktop.ipc.preview.findInPage")(function* ({
128 tabId,
129 text,
130 forward,
131 newSession,
132 }) {
133 const manager = yield* PreviewManager.PreviewManager;
134 yield* manager.findInPage(tabId, text, { forward, newSession });
135 }),
136});
137
138const tabMethod = (
139 channel: string,
140 name: string,
141 invoke: (
142 manager: PreviewManager.PreviewManager["Service"],
143 tabId: string,
144 ) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
145) =>
146 DesktopIpc.makeIpcMethod({
147 channel,
148 payload: DesktopPreviewTabInputSchema,
149 result: Schema.Void,
150 handler: Effect.fn(name)(function* ({ tabId }) {
151 const manager = yield* PreviewManager.PreviewManager;
152 yield* invoke(manager, tabId);
153 }),
154 });
155
156const goBack = tabMethod(
157 IpcChannels.PREVIEW_GO_BACK_CHANNEL,
158 "desktop.ipc.preview.goBack",
159 (manager, tabId) => manager.goBack(tabId),
160);
161const goForward = tabMethod(
162 IpcChannels.PREVIEW_GO_FORWARD_CHANNEL,
163 "desktop.ipc.preview.goForward",
164 (manager, tabId) => manager.goForward(tabId),
165);
166const refresh = tabMethod(
167 IpcChannels.PREVIEW_REFRESH_CHANNEL,
168 "desktop.ipc.preview.refresh",
169 (manager, tabId) => manager.refresh(tabId),
170);
171const stopLoading = tabMethod(
172 IpcChannels.PREVIEW_STOP_LOADING_CHANNEL,
173 "desktop.ipc.preview.stopLoading",
174 (manager, tabId) => manager.stopLoading(tabId),
175);
176const zoomIn = tabMethod(
177 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
178 "desktop.ipc.preview.zoomIn",
179 (manager, tabId) => manager.zoomIn(tabId),
180);
181const zoomOut = tabMethod(
182 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
183 "desktop.ipc.preview.zoomOut",
184 (manager, tabId) => manager.zoomOut(tabId),
185);
186const resetZoom = tabMethod(
187 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
188 "desktop.ipc.preview.resetZoom",
189 (manager, tabId) => manager.resetZoom(tabId),
190);
191const hardReload = tabMethod(
192 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
193 "desktop.ipc.preview.hardReload",
194 (manager, tabId) => manager.hardReload(tabId),
195);
196const stopFindInPage = tabMethod(
197 IpcChannels.PREVIEW_STOP_FIND_IN_PAGE_CHANNEL,
198 "desktop.ipc.preview.stopFindInPage",
199 (manager, tabId) => manager.stopFindInPage(tabId),
200);
201const setZoomFactor = DesktopIpc.makeIpcMethod({
202 channel: IpcChannels.PREVIEW_SET_ZOOM_FACTOR_CHANNEL,
203 payload: DesktopPreviewSetZoomFactorInputSchema,
204 result: Schema.Void,
205 handler: Effect.fn("desktop.ipc.preview.setZoomFactor")(function* ({ tabId, zoomFactor }) {
206 const manager = yield* PreviewManager.PreviewManager;
207 yield* manager.setZoomFactor(tabId, zoomFactor);
208 }),
209});
210const setColorScheme = DesktopIpc.makeIpcMethod({
211 channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
212 payload: DesktopPreviewSetColorSchemeInputSchema,
213 result: Schema.Void,
214 handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
215 const manager = yield* PreviewManager.PreviewManager;
216 yield* manager.setColorScheme(tabId, colorScheme);
217 }),
218});
219const setAudioMuted = DesktopIpc.makeIpcMethod({
220 channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
221 payload: DesktopPreviewSetAudioMutedInputSchema,
222 result: Schema.Void,
223 handler: Effect.fn("desktop.ipc.preview.setAudioMuted")(function* ({ tabId, audioMuted }) {
224 const manager = yield* PreviewManager.PreviewManager;
225 yield* manager.setAudioMuted(tabId, audioMuted);
226 }),
227});
228const setShown = DesktopIpc.makeIpcMethod({
229 channel: IpcChannels.PREVIEW_SET_SHOWN_CHANNEL,
230 payload: DesktopPreviewSetShownInputSchema,
231 result: Schema.Void,
232 handler: Effect.fn("desktop.ipc.preview.setShown")(function* ({ tabId, shown }) {
233 const manager = yield* PreviewManager.PreviewManager;
234 yield* manager.setShown(tabId, shown);
235 }),
236});
237const openDevTools = tabMethod(
238 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
239 "desktop.ipc.preview.openDevTools",
240 (manager, tabId) => manager.openDevTools(tabId),
241);
242const cancelPickElement = tabMethod(
243 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
244 "desktop.ipc.preview.cancelPickElement",
245 (manager, tabId) => manager.cancelPickElement(tabId),
246);
247export const startRecording = DesktopIpc.makeIpcMethod({
248 channel: IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
249 payload: DesktopPreviewTabInputSchema,
250 // Void encodes any value as undefined, so the tagged failure must be tried first.
251 result: Schema.Union([PreviewCaptureFailure, Schema.Void]),
252 handler: Effect.fn("desktop.ipc.preview.startRecording")(function* ({ tabId }) {
253 const manager = yield* PreviewManager.PreviewManager;
254 const store = yield* DesktopClientSettings.DesktopClientSettings;
255 const settings = yield* store.get;
256 const options = Option.map(settings, (value) => ({
257 showKeyPresses: value.browserRecordingShowKeyPresses,
258 showMousePresses: value.browserRecordingShowMousePresses,
259 }));
260 return yield* manager
261 .startRecording(tabId, Option.getOrUndefined(options))
262 .pipe(
263 Effect.catch((error) => Effect.succeed(normalizeCaptureFailure(error, "recording-start"))),
264 );
265 }),
266});
267const stopRecording = tabMethod(
268 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
269 "desktop.ipc.preview.stopRecording",
270 (manager, tabId) => manager.stopRecording(tabId),
271);
272const openPictureInPicture = tabMethod(
273 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL,
274 "desktop.ipc.preview.openPictureInPicture",
275 (manager, tabId) => manager.openPictureInPicture(tabId),
276);
277const closePictureInPicture = tabMethod(
278 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL,
279 "desktop.ipc.preview.closePictureInPicture",
280 (manager, tabId) => manager.closePictureInPicture(tabId),
281);
282
283const clearCookies = DesktopIpc.makeIpcMethod({
284 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
285 payload: DesktopPreviewClearDataInputSchema,
286 result: Schema.Void,
287 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* ({ environmentId, profileId }) {
288 const manager = yield* PreviewManager.PreviewManager;
289 yield* manager.clearCookies(yield* resolveClearPartitions(manager, environmentId, profileId));
290 }),
291});
292
293const clearCache = DesktopIpc.makeIpcMethod({
294 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
295 payload: DesktopPreviewClearDataInputSchema,
296 result: Schema.Void,
297 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* ({ environmentId, profileId }) {
298 const manager = yield* PreviewManager.PreviewManager;
299 yield* manager.clearCache(yield* resolveClearPartitions(manager, environmentId, profileId));
300 }),
301});
302
303/** Keep the default profile's bare environment id so existing logins retain their partition. */
304export function resolvePartitionScope(
305 environmentId: string,
306 profileId: string | undefined,
307): {
308 readonly scope: string;
309 readonly persistent: boolean;
310 readonly namespace?: "profile";
311} {
312 if (profileId === undefined || profileId === DEFAULT_BROWSER_PROFILE_ID) {
313 return { scope: environmentId, persistent: true };
314 }
315 // JSON framing preserves lone UTF-16 surrogates; URI encoding throws and replacement would collide.
316 return {
317 scope: JSON.stringify([environmentId, profileId]),
318 persistent: profileId !== INCOGNITO_BROWSER_PROFILE_ID,
319 namespace: "profile" as const,
320 };
321}
322
323/** Omitting a profile clears every partition; a named profile clears only its own partition. */
324const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartitions")(function* (
325 manager: PreviewManager.PreviewManager["Service"],
326 environmentId: string,
327 profileId: string | undefined,
328) {
329 if (profileId === undefined) return undefined;
330 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
331 // getBrowserSession registers the partition; deriving its name alone leaves unopened profiles uncleared.
332 yield* manager.getBrowserSession(scope, persistent, namespace);
333 return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
334});
335
336const getPreviewConfig = DesktopIpc.makeIpcMethod({
337 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
338 payload: DesktopPreviewConfigInputSchema,
339 result: DesktopPreviewWebviewConfigSchema,
340 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId, profileId }) {
341 const manager = yield* PreviewManager.PreviewManager;
342 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
343 // Session creation installs UA and permission handlers before a guest can attach.
344 yield* manager.getBrowserSession(scope, persistent, namespace);
345 return {
346 partition: yield* manager.getBrowserPartition(scope, persistent, namespace),
347 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
348 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
349 };
350 }),
351});
352
353export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
354 channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL,
355 payload: Schema.Void,
356 result: Schema.Array(BrowserImportSource),
357 handler: Effect.fn("desktop.ipc.preview.listBrowserImportSources")(function* () {
358 const browserImport = yield* BrowserImport.BrowserImport;
359 return yield* browserImport.listSources;
360 }),
361});
362
363export const importBrowserCookies = DesktopIpc.makeIpcMethod({
364 channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL,
365 payload: DesktopPreviewImportCookiesInputSchema,
366 result: BrowserImportResult,
367 handler: Effect.fn("desktop.ipc.preview.importBrowserCookies")(function* ({
368 environmentId,
369 ...importInput
370 }) {
371 const browserImport = yield* BrowserImport.BrowserImport;
372 const { scope, persistent, namespace } = resolvePartitionScope(
373 environmentId,
374 importInput.targetProfileId,
375 );
376 return yield* browserImport.importCookies({
377 input: importInput,
378 scope,
379 persistent,
380 ...(namespace === undefined ? {} : { namespace }),
381 });
382 }),
383});
384
385const setAnnotationTheme = DesktopIpc.makeIpcMethod({
386 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
387 payload: DesktopPreviewAnnotationThemeInputSchema,
388 result: Schema.Void,
389 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
390 const manager = yield* PreviewManager.PreviewManager;
391 yield* manager.setAnnotationTheme(theme);
392 }),
393});
394
395const pickElement = DesktopIpc.makeIpcMethod({
396 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
397 payload: DesktopPreviewPickElementInputSchema,
398 result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
399 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId, voice }) {
400 const manager = yield* PreviewManager.PreviewManager;
401 return yield* manager.pickElement(tabId, voice);
402 }),
403});
404
405const updateAnnotationVoice = DesktopIpc.makeIpcMethod({
406 channel: IpcChannels.PREVIEW_ANNOTATION_VOICE_STATE_CHANNEL,
407 payload: DesktopPreviewAnnotationVoiceStateSchema,
408 result: Schema.Void,
409 handler: Effect.fn("desktop.ipc.preview.updateAnnotationVoice")(function* (state) {
410 const manager = yield* PreviewManager.PreviewManager;
411 yield* manager.updateAnnotationVoice(state);
412 }),
413});
414
415const setAnnotationSendEnabled = DesktopIpc.makeIpcMethod({
416 channel: IpcChannels.PREVIEW_SET_ANNOTATION_SEND_ENABLED_CHANNEL,
417 payload: DesktopPreviewAnnotationSendEnabledInputSchema,
418 result: Schema.Void,
419 handler: Effect.fn("desktop.ipc.preview.setAnnotationSendEnabled")(function* ({
420 tabId,
421 enabled,
422 }) {
423 const manager = yield* PreviewManager.PreviewManager;
424 yield* manager.setAnnotationSendEnabled(tabId, enabled);
425 }),
426});
427
428export const captureScreenshot = DesktopIpc.makeIpcMethod({
429 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
430 payload: DesktopPreviewTabInputSchema,
431 result: Schema.Union([DesktopPreviewScreenshotArtifactSchema, PreviewCaptureFailure]),
432 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
433 const manager = yield* PreviewManager.PreviewManager;
434 return yield* manager
435 .captureScreenshot(tabId)
436 .pipe(Effect.catch((error) => Effect.succeed(normalizeCaptureFailure(error, "screenshot"))));
437 }),
438});
439
440const revealArtifact = DesktopIpc.makeIpcMethod({
441 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
442 payload: DesktopPreviewArtifactInputSchema,
443 result: Schema.Void,
444 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
445 const manager = yield* PreviewManager.PreviewManager;
446 yield* manager.revealArtifact(path);
447 }),
448});
449
450const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
451 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
452 payload: DesktopPreviewArtifactInputSchema,
453 result: Schema.Void,
454 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
455 const manager = yield* PreviewManager.PreviewManager;
456 yield* manager.copyArtifactToClipboard(path);
457 }),
458});
459
460const 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 setForwardedShortcuts,
472 updateAnnotationVoice,
473 createTab,
474 closeTab,
475 registerWebview,
476 navigate,
477 goBack,
478 goForward,
479 refresh,
480 stopLoading,
481 zoomIn,
482 zoomOut,
483 resetZoom,
484 hardReload,
485 findInPage,
486 stopFindInPage,
487 setColorScheme,
488 setZoomFactor,
489 setAudioMuted,
490 setShown,
491 openDevTools,
492 clearCookies,
493 clearCache,
494 getPreviewConfig,
495 setAnnotationTheme,
496 setAnnotationSendEnabled,
497 pickElement,
498 cancelPickElement,
499 captureScreenshot,
500 revealArtifact,
501 copyArtifactToClipboard,
502 openPictureInPicture,
503 closePictureInPicture,
504 startRecording,
505 stopRecording,
506 saveRecording,
507] as const;