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 zoomIn = tabMethod(
172 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
173 "desktop.ipc.preview.zoomIn",
174 (manager, tabId) => manager.zoomIn(tabId),
175);
176const zoomOut = tabMethod(
177 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
178 "desktop.ipc.preview.zoomOut",
179 (manager, tabId) => manager.zoomOut(tabId),
180);
181const resetZoom = tabMethod(
182 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
183 "desktop.ipc.preview.resetZoom",
184 (manager, tabId) => manager.resetZoom(tabId),
185);
186const hardReload = tabMethod(
187 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
188 "desktop.ipc.preview.hardReload",
189 (manager, tabId) => manager.hardReload(tabId),
190);
191const stopFindInPage = tabMethod(
192 IpcChannels.PREVIEW_STOP_FIND_IN_PAGE_CHANNEL,
193 "desktop.ipc.preview.stopFindInPage",
194 (manager, tabId) => manager.stopFindInPage(tabId),
195);
196const setZoomFactor = DesktopIpc.makeIpcMethod({
197 channel: IpcChannels.PREVIEW_SET_ZOOM_FACTOR_CHANNEL,
198 payload: DesktopPreviewSetZoomFactorInputSchema,
199 result: Schema.Void,
200 handler: Effect.fn("desktop.ipc.preview.setZoomFactor")(function* ({ tabId, zoomFactor }) {
201 const manager = yield* PreviewManager.PreviewManager;
202 yield* manager.setZoomFactor(tabId, zoomFactor);
203 }),
204});
205const setColorScheme = DesktopIpc.makeIpcMethod({
206 channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
207 payload: DesktopPreviewSetColorSchemeInputSchema,
208 result: Schema.Void,
209 handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
210 const manager = yield* PreviewManager.PreviewManager;
211 yield* manager.setColorScheme(tabId, colorScheme);
212 }),
213});
214const setAudioMuted = DesktopIpc.makeIpcMethod({
215 channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
216 payload: DesktopPreviewSetAudioMutedInputSchema,
217 result: Schema.Void,
218 handler: Effect.fn("desktop.ipc.preview.setAudioMuted")(function* ({ tabId, audioMuted }) {
219 const manager = yield* PreviewManager.PreviewManager;
220 yield* manager.setAudioMuted(tabId, audioMuted);
221 }),
222});
223const setShown = DesktopIpc.makeIpcMethod({
224 channel: IpcChannels.PREVIEW_SET_SHOWN_CHANNEL,
225 payload: DesktopPreviewSetShownInputSchema,
226 result: Schema.Void,
227 handler: Effect.fn("desktop.ipc.preview.setShown")(function* ({ tabId, shown }) {
228 const manager = yield* PreviewManager.PreviewManager;
229 yield* manager.setShown(tabId, shown);
230 }),
231});
232const openDevTools = tabMethod(
233 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
234 "desktop.ipc.preview.openDevTools",
235 (manager, tabId) => manager.openDevTools(tabId),
236);
237const cancelPickElement = tabMethod(
238 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
239 "desktop.ipc.preview.cancelPickElement",
240 (manager, tabId) => manager.cancelPickElement(tabId),
241);
242export const startRecording = DesktopIpc.makeIpcMethod({
243 channel: IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
244 payload: DesktopPreviewTabInputSchema,
245 // Void encodes any value as undefined, so the tagged failure must be tried first.
246 result: Schema.Union([PreviewCaptureFailure, Schema.Void]),
247 handler: Effect.fn("desktop.ipc.preview.startRecording")(function* ({ tabId }) {
248 const manager = yield* PreviewManager.PreviewManager;
249 const store = yield* DesktopClientSettings.DesktopClientSettings;
250 const settings = yield* store.get;
251 const options = Option.map(settings, (value) => ({
252 showKeyPresses: value.browserRecordingShowKeyPresses,
253 showMousePresses: value.browserRecordingShowMousePresses,
254 }));
255 return yield* manager
256 .startRecording(tabId, Option.getOrUndefined(options))
257 .pipe(
258 Effect.catch((error) => Effect.succeed(normalizeCaptureFailure(error, "recording-start"))),
259 );
260 }),
261});
262const stopRecording = tabMethod(
263 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
264 "desktop.ipc.preview.stopRecording",
265 (manager, tabId) => manager.stopRecording(tabId),
266);
267const openPictureInPicture = tabMethod(
268 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL,
269 "desktop.ipc.preview.openPictureInPicture",
270 (manager, tabId) => manager.openPictureInPicture(tabId),
271);
272const closePictureInPicture = tabMethod(
273 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL,
274 "desktop.ipc.preview.closePictureInPicture",
275 (manager, tabId) => manager.closePictureInPicture(tabId),
276);
277
278const clearCookies = DesktopIpc.makeIpcMethod({
279 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
280 payload: DesktopPreviewClearDataInputSchema,
281 result: Schema.Void,
282 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* ({ environmentId, profileId }) {
283 const manager = yield* PreviewManager.PreviewManager;
284 yield* manager.clearCookies(yield* resolveClearPartitions(manager, environmentId, profileId));
285 }),
286});
287
288const clearCache = DesktopIpc.makeIpcMethod({
289 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
290 payload: DesktopPreviewClearDataInputSchema,
291 result: Schema.Void,
292 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* ({ environmentId, profileId }) {
293 const manager = yield* PreviewManager.PreviewManager;
294 yield* manager.clearCache(yield* resolveClearPartitions(manager, environmentId, profileId));
295 }),
296});
297
298/** Keep the default profile's bare environment id so existing logins retain their partition. */
299export function resolvePartitionScope(
300 environmentId: string,
301 profileId: string | undefined,
302): {
303 readonly scope: string;
304 readonly persistent: boolean;
305 readonly namespace?: "profile";
306} {
307 if (profileId === undefined || profileId === DEFAULT_BROWSER_PROFILE_ID) {
308 return { scope: environmentId, persistent: true };
309 }
310 // JSON framing preserves lone UTF-16 surrogates; URI encoding throws and replacement would collide.
311 return {
312 scope: JSON.stringify([environmentId, profileId]),
313 persistent: profileId !== INCOGNITO_BROWSER_PROFILE_ID,
314 namespace: "profile" as const,
315 };
316}
317
318/** Omitting a profile clears every partition; a named profile clears only its own partition. */
319const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartitions")(function* (
320 manager: PreviewManager.PreviewManager["Service"],
321 environmentId: string,
322 profileId: string | undefined,
323) {
324 if (profileId === undefined) return undefined;
325 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
326 // getBrowserSession registers the partition; deriving its name alone leaves unopened profiles uncleared.
327 yield* manager.getBrowserSession(scope, persistent, namespace);
328 return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
329});
330
331const getPreviewConfig = DesktopIpc.makeIpcMethod({
332 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
333 payload: DesktopPreviewConfigInputSchema,
334 result: DesktopPreviewWebviewConfigSchema,
335 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId, profileId }) {
336 const manager = yield* PreviewManager.PreviewManager;
337 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
338 // Session creation installs UA and permission handlers before a guest can attach.
339 yield* manager.getBrowserSession(scope, persistent, namespace);
340 return {
341 partition: yield* manager.getBrowserPartition(scope, persistent, namespace),
342 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
343 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
344 };
345 }),
346});
347
348export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
349 channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL,
350 payload: Schema.Void,
351 result: Schema.Array(BrowserImportSource),
352 handler: Effect.fn("desktop.ipc.preview.listBrowserImportSources")(function* () {
353 const browserImport = yield* BrowserImport.BrowserImport;
354 return yield* browserImport.listSources;
355 }),
356});
357
358export const importBrowserCookies = DesktopIpc.makeIpcMethod({
359 channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL,
360 payload: DesktopPreviewImportCookiesInputSchema,
361 result: BrowserImportResult,
362 handler: Effect.fn("desktop.ipc.preview.importBrowserCookies")(function* ({
363 environmentId,
364 ...importInput
365 }) {
366 const browserImport = yield* BrowserImport.BrowserImport;
367 const { scope, persistent, namespace } = resolvePartitionScope(
368 environmentId,
369 importInput.targetProfileId,
370 );
371 return yield* browserImport.importCookies({
372 input: importInput,
373 scope,
374 persistent,
375 ...(namespace === undefined ? {} : { namespace }),
376 });
377 }),
378});
379
380const setAnnotationTheme = DesktopIpc.makeIpcMethod({
381 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
382 payload: DesktopPreviewAnnotationThemeInputSchema,
383 result: Schema.Void,
384 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
385 const manager = yield* PreviewManager.PreviewManager;
386 yield* manager.setAnnotationTheme(theme);
387 }),
388});
389
390const pickElement = DesktopIpc.makeIpcMethod({
391 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
392 payload: DesktopPreviewPickElementInputSchema,
393 result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
394 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId, voice }) {
395 const manager = yield* PreviewManager.PreviewManager;
396 return yield* manager.pickElement(tabId, voice);
397 }),
398});
399
400const updateAnnotationVoice = DesktopIpc.makeIpcMethod({
401 channel: IpcChannels.PREVIEW_ANNOTATION_VOICE_STATE_CHANNEL,
402 payload: DesktopPreviewAnnotationVoiceStateSchema,
403 result: Schema.Void,
404 handler: Effect.fn("desktop.ipc.preview.updateAnnotationVoice")(function* (state) {
405 const manager = yield* PreviewManager.PreviewManager;
406 yield* manager.updateAnnotationVoice(state);
407 }),
408});
409
410const setAnnotationSendEnabled = DesktopIpc.makeIpcMethod({
411 channel: IpcChannels.PREVIEW_SET_ANNOTATION_SEND_ENABLED_CHANNEL,
412 payload: DesktopPreviewAnnotationSendEnabledInputSchema,
413 result: Schema.Void,
414 handler: Effect.fn("desktop.ipc.preview.setAnnotationSendEnabled")(function* ({
415 tabId,
416 enabled,
417 }) {
418 const manager = yield* PreviewManager.PreviewManager;
419 yield* manager.setAnnotationSendEnabled(tabId, enabled);
420 }),
421});
422
423export const captureScreenshot = DesktopIpc.makeIpcMethod({
424 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
425 payload: DesktopPreviewTabInputSchema,
426 result: Schema.Union([DesktopPreviewScreenshotArtifactSchema, PreviewCaptureFailure]),
427 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
428 const manager = yield* PreviewManager.PreviewManager;
429 return yield* manager
430 .captureScreenshot(tabId)
431 .pipe(Effect.catch((error) => Effect.succeed(normalizeCaptureFailure(error, "screenshot"))));
432 }),
433});
434
435const revealArtifact = DesktopIpc.makeIpcMethod({
436 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
437 payload: DesktopPreviewArtifactInputSchema,
438 result: Schema.Void,
439 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
440 const manager = yield* PreviewManager.PreviewManager;
441 yield* manager.revealArtifact(path);
442 }),
443});
444
445const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
446 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
447 payload: DesktopPreviewArtifactInputSchema,
448 result: Schema.Void,
449 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
450 const manager = yield* PreviewManager.PreviewManager;
451 yield* manager.copyArtifactToClipboard(path);
452 }),
453});
454
455const saveRecording = DesktopIpc.makeIpcMethod({
456 channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
457 payload: DesktopPreviewRecordingSaveInputSchema,
458 result: DesktopPreviewRecordingArtifactSchema,
459 handler: Effect.fn("desktop.ipc.preview.saveRecording")(function* ({ tabId, mimeType, data }) {
460 const manager = yield* PreviewManager.PreviewManager;
461 return yield* manager.saveRecording(tabId, mimeType, data);
462 }),
463});
464
465export const methods = [
466 setForwardedShortcuts,
467 updateAnnotationVoice,
468 createTab,
469 closeTab,
470 registerWebview,
471 navigate,
472 goBack,
473 goForward,
474 refresh,
475 zoomIn,
476 zoomOut,
477 resetZoom,
478 hardReload,
479 findInPage,
480 stopFindInPage,
481 setColorScheme,
482 setZoomFactor,
483 setAudioMuted,
484 setShown,
485 openDevTools,
486 clearCookies,
487 clearCache,
488 getPreviewConfig,
489 setAnnotationTheme,
490 setAnnotationSendEnabled,
491 pickElement,
492 cancelPickElement,
493 captureScreenshot,
494 revealArtifact,
495 copyArtifactToClipboard,
496 openPictureInPicture,
497 closePictureInPicture,
498 startRecording,
499 stopRecording,
500 saveRecording,
501] as const;