1/**
2 * Browser import - pulling cookies from a browser already installed on the
3 * machine into a T3 Code browser profile.
4 *
5 * Only cookies are imported. They carry the logged-in sessions, which is what
6 * makes an imported profile useful; saved passwords are deliberately out of
7 * scope because Electron exposes no password store to put them in.
8 *
9 * Availability is per source and per platform, and the reasons are modelled
10 * explicitly: some are a permission the user can grant, one is a limitation
11 * no amount of consent works around. The UI needs to tell those apart.
12 *
13 * @module BrowserImport
14 */
15import * as Schema from "effect/Schema";
16import { TrimmedNonEmptyString } from "./baseSchemas.ts";
17import { BrowserProfileId } from "./browserProfile.ts";
18
19const BROWSER_IMPORT_SOURCE_IDS = [
20 "chrome",
21 "edge",
22 "brave",
23 "vivaldi",
24 "opera",
25 "arc",
26 "helium",
27 "firefox",
28 "safari",
29] as const;
30
31export const BrowserImportSourceId = Schema.Literals(BROWSER_IMPORT_SOURCE_IDS);
32export type BrowserImportSourceId = typeof BrowserImportSourceId.Type;
33
34/**
35 * Why a detected source cannot be imported right now.
36 *
37 * `needsKeychainApproval` and `browserRunning` are recoverable — the user
38 * grants access or quits the browser. `unsupportedPlatform` is not: it covers
39 * cases like Chrome on Windows, whose App-Bound Encryption is designed to stop
40 * exactly this, and which we will not work around.
41 */
42export const BrowserImportUnavailableReason = Schema.Literals([
43 "notInstalled",
44 "needsKeychainApproval",
45 "keychainItemMissing",
46 "needsFullDiskAccess",
47 "browserRunning",
48 "unsupportedPlatform",
49]);
50export type BrowserImportUnavailableReason = typeof BrowserImportUnavailableReason.Type;
51
52/**
53 * Why an import that was actually attempted failed.
54 *
55 * A superset of the unavailable reasons: a source can pass the pre-flight
56 * check and still fail, most often because the user declined the keychain
57 * prompt the read triggers.
58 */
59export const BrowserImportFailureReason = Schema.Literals([
60 ...BrowserImportUnavailableReason.literals,
61 /** The operating system's keyring or its bundled reader is unavailable. */
62 "keychainUnavailable",
63 /** No source registered under the requested id. */
64 "unknownSource",
65 /** The requested profile directory is not one the source reported. */
66 "unknownSourceProfile",
67 /** The target profile's Electron session could not be opened. */
68 "sessionUnavailable",
69 /**
70 * The cookies were written, but the new profile could not be saved to
71 * settings, so its partition was cleared again rather than left orphaned.
72 */
73 "profileNotSaved",
74 /**
75 * The cookies were written, but the profile count reached its cap while the
76 * import ran, so the new profile was not saved and its partition was cleared.
77 */
78 "profileLimitReached",
79 /** Anything else: a corrupt database, a failed decrypt, a vanished file. */
80 "readFailed",
81]);
82export type BrowserImportFailureReason = typeof BrowserImportFailureReason.Type;
83
84/** A profile inside the source browser, e.g. Chromium's "Default" directory. */
85export const BrowserImportSourceProfile = Schema.Struct({
86 /** Directory name under the source's user-data dir. */
87 directory: TrimmedNonEmptyString,
88 /** The name the source browser shows for it. */
89 name: TrimmedNonEmptyString,
90 /**
91 * How many cookies the profile holds. Counted without decrypting, so it is
92 * cheap; absent when the store could not be read yet (Safari before Full
93 * Disk Access is granted).
94 */
95 cookieCount: Schema.optional(Schema.Int),
96});
97export type BrowserImportSourceProfile = typeof BrowserImportSourceProfile.Type;
98
99export const BrowserImportSource = Schema.Struct({
100 id: BrowserImportSourceId,
101 name: TrimmedNonEmptyString,
102 profiles: Schema.Array(BrowserImportSourceProfile),
103 /** Absent when the source is importable. */
104 unavailable: Schema.optional(BrowserImportUnavailableReason),
105});
106export type BrowserImportSource = typeof BrowserImportSource.Type;
107
108export const BrowserImportInput = Schema.Struct({
109 sourceId: BrowserImportSourceId,
110 sourceProfileDirectory: TrimmedNonEmptyString,
111 /** T3 Code profile the cookies are written into. */
112 targetProfileId: BrowserProfileId,
113});
114export type BrowserImportInput = typeof BrowserImportInput.Type;
115
116/** IPC payload: the import input plus the environment the partition belongs to. */
117export const DesktopPreviewImportCookiesInputSchema = Schema.Struct({
118 environmentId: TrimmedNonEmptyString,
119 sourceId: BrowserImportSourceId,
120 sourceProfileDirectory: TrimmedNonEmptyString,
121 targetProfileId: BrowserProfileId,
122});
123
124export const BrowserImportResult = Schema.Struct({
125 /** Cookies successfully written into the target partition. */
126 imported: Schema.Int,
127 /**
128 * Cookies read but not written — expired, rejected as malformed, or held
129 * under a key we could not use. Surfaced rather than hidden so a
130 * mostly-failed import doesn't look like a success.
131 */
132 skipped: Schema.Int,
133 /**
134 * The distinct hosts those skipped cookies belonged to, so the user can be
135 * told what didn't come over rather than just how many. Capped, since a
136 * broken key can skip thousands across many sites.
137 */
138 skippedDomains: Schema.Array(Schema.String),
139});
140export type BrowserImportResult = typeof BrowserImportResult.Type;
141
142const BROWSER_IMPORT_UNAVAILABLE_COPY: Readonly<Record<BrowserImportUnavailableReason, string>> = {
143 notInstalled: "Not installed on this machine.",
144 needsKeychainApproval: "Needs Keychain access to read its cookies.",
145 keychainItemMissing:
146 "No encryption key in your Keychain — sign in to that browser once, then retry.",
147 needsFullDiskAccess:
148 "Give T3 Code Full Disk Access in System Settings → Privacy & Security, then retry.",
149 browserRunning: "Quit the browser first so its cookie database can be read.",
150 unsupportedPlatform: "Importing from this browser isn't possible on this platform.",
151};
152
153/** What to tell the user when an attempted import fails. */
154export const BROWSER_IMPORT_FAILURE_COPY: Readonly<Record<BrowserImportFailureReason, string>> = {
155 ...BROWSER_IMPORT_UNAVAILABLE_COPY,
156 keychainUnavailable:
157 "The system keyring could not be accessed. Make sure your desktop keyring is running and unlocked, then retry.",
158 unknownSource: "That browser is no longer available to import from.",
159 unknownSourceProfile: "That browser profile no longer exists.",
160 sessionUnavailable: "The target profile could not be opened.",
161 profileNotSaved: "The cookies were imported, but the new profile couldn't be saved. Try again.",
162 profileLimitReached:
163 "You've reached the profile limit. Delete a profile or import into an existing one.",
164 readFailed: "The browser's cookie database could not be read.",
165};