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

preview.ts

464 lines17.1 KB

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