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

preview.ts

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