Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

preview.ts

518 lines18.8 KB

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