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

preview.ts

454 lines16.6 KB

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