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

preview.ts

466 lines17.3 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 DEFAULT_BROWSER_PROFILE_ID,
23 INCOGNITO_BROWSER_PROFILE_ID,
24 PreviewForwardedShortcut,
25 MAX_KEYBINDINGS_COUNT,
26} from "@t3tools/contracts";
27import * as Effect from "effect/Effect";
28import * as Schema from "effect/Schema";
29import * as Option from "effect/Option";
30import * as NodeURL from "node:url";
31​
32import * as ElectronWindow from "../../electron/ElectronWindow.ts";
33import * as BrowserImport from "../../preview/BrowserImport/BrowserImport.ts";
34import * as PreviewManager from "../../preview/Manager.ts";
35import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
36import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts";
37import * as IpcChannels from "../channels.ts";
38import * as DesktopIpc from "../DesktopIpc.ts";
39​
40export const installPreviewEventForwarding = Effect.fn(
41 "desktop.ipc.preview.installEventForwarding",
42)(function* () {
43 const electronWindow = yield* ElectronWindow.ElectronWindow;
44 const manager = yield* PreviewManager.PreviewManager;
45 yield* manager.subscribeStateChanges((tabId, state) =>
46 electronWindow.sendAll(IpcChannels.PREVIEW_STATE_CHANGE_CHANNEL, tabId, state),
47 );
48 yield* manager.subscribeRecordingFrames((frame) =>
49 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_FRAME_CHANNEL, frame),
50 );
51 yield* manager.subscribeRecordingInputs((event) =>
52 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_INPUT_CHANNEL, event),
53 );
54 yield* manager.subscribePointerEvents((event) =>
55 electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event),
56 );
57});
58​
59export const setForwardedShortcuts = DesktopIpc.makeIpcMethod({
60 channel: IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL,
61 payload: Schema.Array(PreviewForwardedShortcut).check(Schema.isMaxLength(MAX_KEYBINDINGS_COUNT)),
62 result: Schema.Void,
63 handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* (shortcuts) {
64 const manager = yield* PreviewManager.PreviewManager;
65 yield* manager.setForwardedShortcuts(shortcuts);
66 }),
67});
68​
69export const createTab = DesktopIpc.makeIpcMethod({
70 channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
71 payload: DesktopPreviewCreateTabInputSchema,
72 result: Schema.Void,
73 handler: Effect.fn("desktop.ipc.preview.createTab")(function* ({
74 tabId,
75 zoomFactor,
76 colorScheme,
77 serverTab,
78 }) {
79 const manager = yield* PreviewManager.PreviewManager;
80 yield* manager.createTab(tabId, { zoomFactor, colorScheme, serverTab });
81 }),
82});
83​
84export const closeTab = DesktopIpc.makeIpcMethod({
85 channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
86 payload: DesktopPreviewTabInputSchema,
87 result: Schema.Void,
88 handler: Effect.fn("desktop.ipc.preview.closeTab")(function* ({ tabId }) {
89 const manager = yield* PreviewManager.PreviewManager;
90 yield* manager.closeTab(tabId);
91 }),
92});
93​
94export const registerWebview = DesktopIpc.makeIpcMethod({
95 channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
96 payload: DesktopPreviewRegisterWebviewInputSchema,
97 result: Schema.Void,
98 handler: Effect.fn("desktop.ipc.preview.registerWebview")(function* ({ tabId, webContentsId }) {
99 const manager = yield* PreviewManager.PreviewManager;
100 yield* manager.registerWebview(tabId, webContentsId);
101 }),
102});
103​
104export const navigate = DesktopIpc.makeIpcMethod({
105 channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
106 payload: DesktopPreviewNavigateInputSchema,
107 result: Schema.Void,
108 handler: Effect.fn("desktop.ipc.preview.navigate")(function* ({ tabId, url }) {
109 const manager = yield* PreviewManager.PreviewManager;
110 yield* manager.navigate(tabId, url);
111 }),
112});
113​
114const tabMethod = (
115 channel: string,
116 name: string,
117 invoke: (
118 manager: PreviewManager.PreviewManager["Service"],
119 tabId: string,
120 ) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
121) =>
122 DesktopIpc.makeIpcMethod({
123 channel,
124 payload: DesktopPreviewTabInputSchema,
125 result: Schema.Void,
126 handler: Effect.fn(name)(function* ({ tabId }) {
127 const manager = yield* PreviewManager.PreviewManager;
128 yield* invoke(manager, tabId);
129 }),
130 });
131​
132export const goBack = tabMethod(
133 IpcChannels.PREVIEW_GO_BACK_CHANNEL,
134 "desktop.ipc.preview.goBack",
135 (manager, tabId) => manager.goBack(tabId),
136);
137export const goForward = tabMethod(
138 IpcChannels.PREVIEW_GO_FORWARD_CHANNEL,
139 "desktop.ipc.preview.goForward",
140 (manager, tabId) => manager.goForward(tabId),
141);
142export const refresh = tabMethod(
143 IpcChannels.PREVIEW_REFRESH_CHANNEL,
144 "desktop.ipc.preview.refresh",
145 (manager, tabId) => manager.refresh(tabId),
146);
147export const zoomIn = tabMethod(
148 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
149 "desktop.ipc.preview.zoomIn",
150 (manager, tabId) => manager.zoomIn(tabId),
151);
152export const zoomOut = tabMethod(
153 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
154 "desktop.ipc.preview.zoomOut",
155 (manager, tabId) => manager.zoomOut(tabId),
156);
157export const resetZoom = tabMethod(
158 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
159 "desktop.ipc.preview.resetZoom",
160 (manager, tabId) => manager.resetZoom(tabId),
161);
162export const hardReload = tabMethod(
163 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
164 "desktop.ipc.preview.hardReload",
165 (manager, tabId) => manager.hardReload(tabId),
166);
167export const setZoomFactor = DesktopIpc.makeIpcMethod({
168 channel: IpcChannels.PREVIEW_SET_ZOOM_FACTOR_CHANNEL,
169 payload: DesktopPreviewSetZoomFactorInputSchema,
170 result: Schema.Void,
171 handler: Effect.fn("desktop.ipc.preview.setZoomFactor")(function* ({ tabId, zoomFactor }) {
172 const manager = yield* PreviewManager.PreviewManager;
173 yield* manager.setZoomFactor(tabId, zoomFactor);
174 }),
175});
176export const setColorScheme = DesktopIpc.makeIpcMethod({
177 channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
178 payload: DesktopPreviewSetColorSchemeInputSchema,
179 result: Schema.Void,
180 handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
181 const manager = yield* PreviewManager.PreviewManager;
182 yield* manager.setColorScheme(tabId, colorScheme);
183 }),
184});
185export const setAudioMuted = DesktopIpc.makeIpcMethod({
186 channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
187 payload: DesktopPreviewSetAudioMutedInputSchema,
188 result: Schema.Void,
189 handler: Effect.fn("desktop.ipc.preview.setAudioMuted")(function* ({ tabId, audioMuted }) {
190 const manager = yield* PreviewManager.PreviewManager;
191 yield* manager.setAudioMuted(tabId, audioMuted);
192 }),
193});
194export const openDevTools = tabMethod(
195 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
196 "desktop.ipc.preview.openDevTools",
197 (manager, tabId) => manager.openDevTools(tabId),
198);
199export const cancelPickElement = tabMethod(
200 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
201 "desktop.ipc.preview.cancelPickElement",
202 (manager, tabId) => manager.cancelPickElement(tabId),
203);
204export const startRecording = DesktopIpc.makeIpcMethod({
205 channel: IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
206 payload: DesktopPreviewTabInputSchema,
207 result: Schema.Void,
208 handler: Effect.fn("desktop.ipc.preview.startRecording")(function* ({ tabId }) {
209 const manager = yield* PreviewManager.PreviewManager;
210 const store = yield* DesktopClientSettings.DesktopClientSettings;
211 const settings = yield* store.get;
212 const options = Option.map(settings, (value) => ({
213 showKeyPresses: value.browserRecordingShowKeyPresses,
214 showMousePresses: value.browserRecordingShowMousePresses,
215 }));
216 yield* manager.startRecording(tabId, Option.getOrUndefined(options));
217 }),
218});
219export const stopRecording = tabMethod(
220 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
221 "desktop.ipc.preview.stopRecording",
222 (manager, tabId) => manager.stopRecording(tabId),
223);
224export const openPictureInPicture = tabMethod(
225 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL,
226 "desktop.ipc.preview.openPictureInPicture",
227 (manager, tabId) => manager.openPictureInPicture(tabId),
228);
229export const closePictureInPicture = tabMethod(
230 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL,
231 "desktop.ipc.preview.closePictureInPicture",
232 (manager, tabId) => manager.closePictureInPicture(tabId),
233);
234​
235export const clearCookies = DesktopIpc.makeIpcMethod({
236 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
237 payload: DesktopPreviewClearDataInputSchema,
238 result: Schema.Void,
239 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* ({ environmentId, profileId }) {
240 const manager = yield* PreviewManager.PreviewManager;
241 yield* manager.clearCookies(yield* resolveClearPartitions(manager, environmentId, profileId));
242 }),
243});
244​
245export const clearCache = DesktopIpc.makeIpcMethod({
246 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
247 payload: DesktopPreviewClearDataInputSchema,
248 result: Schema.Void,
249 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* ({ environmentId, profileId }) {
250 const manager = yield* PreviewManager.PreviewManager;
251 yield* manager.clearCache(yield* resolveClearPartitions(manager, environmentId, profileId));
252 }),
253});
254​
255/**
256 * Partition scope for an (environment, profile) pair.
257 *
258 * The default profile keeps the bare environment id it used before profiles
259 * existed, so upgrading does not strand anyone's existing logins in an
260 * orphaned partition. Incognito derives a non-persistent partition.
261 */
262export function resolvePartitionScope(
263 environmentId: string,
264 profileId: string | undefined,
265): {
266 readonly scope: string;
267 readonly persistent: boolean;
268 readonly namespace?: "profile";
269} {
270 if (profileId === undefined || profileId === DEFAULT_BROWSER_PROFILE_ID) {
271 return { scope: environmentId, persistent: true };
272 }
273 // JSON's tuple framing is injective for strings, including lone UTF-16
274 // surrogates (which it escapes). URI encoding throws on those supported ids,
275 // while replacing them with U+FFFD would collapse distinct identities.
276 return {
277 scope: JSON.stringify([environmentId, profileId]),
278 persistent: profileId !== INCOGNITO_BROWSER_PROFILE_ID,
279 namespace: "profile" as const,
280 };
282​
283/**
284 * Clearing without a profile keeps the historical "everything" behaviour for
285 * an explicit all-profiles action; naming a profile confines it to that
286 * profile's partition so one profile's sign-out cannot reach the others.
287 */
288const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartitions")(function* (
289 manager: PreviewManager.PreviewManager["Service"],
290 environmentId: string,
291 profileId: string | undefined,
292) {
293 if (profileId === undefined) return undefined;
294 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
295 // Loading the session is what puts the partition in the map the clear walks.
296 // Deriving the partition string alone leaves nothing to match, so clearing a
297 // profile with no tab open this run — after a restart, or when deleting a
298 // profile — would report success and delete nothing.
299 yield* manager.getBrowserSession(scope, persistent, namespace);
300 return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
301});
302​
303export const getPreviewConfig = DesktopIpc.makeIpcMethod({
304 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
305 payload: DesktopPreviewConfigInputSchema,
306 result: DesktopPreviewWebviewConfigSchema,
307 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId, profileId }) {
308 const manager = yield* PreviewManager.PreviewManager;
309 const { scope, persistent, namespace } = resolvePartitionScope(environmentId, profileId);
310 // Creating the session first is what installs the UA rewrite and permission
311 // handlers; a guest that attached to an untouched partition would run with
312 // Electron's default UA and Chromium's default permission behaviour.
313 yield* manager.getBrowserSession(scope, persistent, namespace);
314 return {
315 partition: yield* manager.getBrowserPartition(scope, persistent, namespace),
316 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
317 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
318 };
319 }),
320});
321​
322/**
323 * Registered separately from `methods`: these carry `BrowserImport` in their
324 * context and their own failure type, so they do not unify with the
325 * manager-backed handlers the shared loop iterates.
326 */
327export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
328 channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL,
329 payload: Schema.Void,
330 result: Schema.Array(BrowserImportSource),
331 handler: Effect.fn("desktop.ipc.preview.listBrowserImportSources")(function* () {
332 const browserImport = yield* BrowserImport.BrowserImport;
333 return yield* browserImport.listSources;
334 }),
335});
336​
337export const importBrowserCookies = DesktopIpc.makeIpcMethod({
338 channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL,
339 payload: DesktopPreviewImportCookiesInputSchema,
340 result: BrowserImportResult,
341 handler: Effect.fn("desktop.ipc.preview.importBrowserCookies")(function* ({
342 environmentId,
343 ...importInput
344 }) {
345 const browserImport = yield* BrowserImport.BrowserImport;
346 // Derived in main from the same helper the webview config uses, so cookies
347 // land in exactly the partition the profile's tabs attach to.
348 const { scope, persistent, namespace } = resolvePartitionScope(
349 environmentId,
350 importInput.targetProfileId,
351 );
352 return yield* browserImport.importCookies({
353 input: importInput,
354 scope,
355 persistent,
356 ...(namespace === undefined ? {} : { namespace }),
357 });
358 }),
359});
360​
361export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
362 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
363 payload: DesktopPreviewAnnotationThemeInputSchema,
364 result: Schema.Void,
365 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
366 const manager = yield* PreviewManager.PreviewManager;
367 yield* manager.setAnnotationTheme(theme);
368 }),
369});
370​
371export const pickElement = DesktopIpc.makeIpcMethod({
372 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
373 payload: DesktopPreviewTabInputSchema,
374 result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
375 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId }) {
376 const manager = yield* PreviewManager.PreviewManager;
377 return yield* manager.pickElement(tabId);
378 }),
379});
380​
381export const setAnnotationSendEnabled = DesktopIpc.makeIpcMethod({
382 channel: IpcChannels.PREVIEW_SET_ANNOTATION_SEND_ENABLED_CHANNEL,
383 payload: DesktopPreviewAnnotationSendEnabledInputSchema,
384 result: Schema.Void,
385 handler: Effect.fn("desktop.ipc.preview.setAnnotationSendEnabled")(function* ({
386 tabId,
387 enabled,
388 }) {
389 const manager = yield* PreviewManager.PreviewManager;
390 yield* manager.setAnnotationSendEnabled(tabId, enabled);
391 }),
392});
393​
394export const captureScreenshot = DesktopIpc.makeIpcMethod({
395 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
396 payload: DesktopPreviewTabInputSchema,
397 result: DesktopPreviewScreenshotArtifactSchema,
398 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
399 const manager = yield* PreviewManager.PreviewManager;
400 return yield* manager.captureScreenshot(tabId);
401 }),
402});
403​
404export const revealArtifact = DesktopIpc.makeIpcMethod({
405 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
406 payload: DesktopPreviewArtifactInputSchema,
407 result: Schema.Void,
408 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
409 const manager = yield* PreviewManager.PreviewManager;
410 yield* manager.revealArtifact(path);
411 }),
412});
413​
414export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
415 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
416 payload: DesktopPreviewArtifactInputSchema,
417 result: Schema.Void,
418 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
419 const manager = yield* PreviewManager.PreviewManager;
420 yield* manager.copyArtifactToClipboard(path);
421 }),
422});
423​
424export const saveRecording = DesktopIpc.makeIpcMethod({
425 channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
426 payload: DesktopPreviewRecordingSaveInputSchema,
427 result: DesktopPreviewRecordingArtifactSchema,
428 handler: Effect.fn("desktop.ipc.preview.saveRecording")(function* ({ tabId, mimeType, data }) {
429 const manager = yield* PreviewManager.PreviewManager;
430 return yield* manager.saveRecording(tabId, mimeType, data);
431 }),
432});
433​
434export const methods = [
435 setForwardedShortcuts,
436 createTab,
437 closeTab,
438 registerWebview,
439 navigate,
440 goBack,
441 goForward,
442 refresh,
443 zoomIn,
444 zoomOut,
445 resetZoom,
446 hardReload,
447 setColorScheme,
448 setZoomFactor,
449 setAudioMuted,
450 openDevTools,
451 clearCookies,
452 clearCache,
453 getPreviewConfig,
454 setAnnotationTheme,
455 setAnnotationSendEnabled,
456 pickElement,
457 cancelPickElement,
458 captureScreenshot,
459 revealArtifact,
460 copyArtifactToClipboard,
461 openPictureInPicture,
462 closePictureInPicture,
463 startRecording,
464 stopRecording,
465 saveRecording,
466] as const;