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