@@ -16,11 +16,14 @@ import {
1616 DesktopPreviewScreenshotArtifactSchema ,
1717 DesktopPreviewSetAudioMutedInputSchema ,
1818 DesktopPreviewSetColorSchemeInputSchema ,
19+ DesktopPreviewClearDataInputSchema ,
1920 DesktopPreviewCreateTabInputSchema ,
2021 DesktopPreviewTabInputSchema ,
2122 DesktopPreviewWebviewConfigSchema ,
2223 PreviewAnnotationSubmissionResultSchema ,
2324 PreviewAutomationSnapshot ,
25+ DEFAULT_BROWSER_PROFILE_ID ,
26+ INCOGNITO_BROWSER_PROFILE_ID ,
2427} from "@t3tools/contracts" ;
2528import * as Effect from "effect/Effect" ;
2629import * as Schema from "effect/Schema" ;
@@ -196,33 +199,85 @@ export const closePictureInPicture = tabMethod(
196199
197200export const clearCookies = DesktopIpc . makeIpcMethod ( {
198201 channel : IpcChannels . PREVIEW_CLEAR_COOKIES_CHANNEL ,
199- payload : Schema . Void ,
202+ payload : DesktopPreviewClearDataInputSchema ,
200203 result : Schema . Void ,
201- handler : Effect . fn ( "desktop.ipc.preview.clearCookies" ) ( function * ( ) {
204+ handler : Effect . fn ( "desktop.ipc.preview.clearCookies" ) ( function * ( { environmentId , profileId } ) {
202205 const manager = yield * PreviewManager . PreviewManager ;
203- yield * manager . clearCookies ( ) ;
206+ yield * manager . clearCookies ( yield * resolveClearPartitions ( manager , environmentId , profileId ) ) ;
204207 } ) ,
205208} ) ;
206209
207210export const clearCache = DesktopIpc . makeIpcMethod ( {
208211 channel : IpcChannels . PREVIEW_CLEAR_CACHE_CHANNEL ,
209- payload : Schema . Void ,
212+ payload : DesktopPreviewClearDataInputSchema ,
210213 result : Schema . Void ,
211- handler : Effect . fn ( "desktop.ipc.preview.clearCache" ) ( function * ( ) {
214+ handler : Effect . fn ( "desktop.ipc.preview.clearCache" ) ( function * ( { environmentId , profileId } ) {
212215 const manager = yield * PreviewManager . PreviewManager ;
213- yield * manager . clearCache ( ) ;
216+ yield * manager . clearCache ( yield * resolveClearPartitions ( manager , environmentId , profileId ) ) ;
214217 } ) ,
215218} ) ;
216219
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+ */
227+ export 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+ } ;
246+ }
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+ */
253+ const 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+
217268export const getPreviewConfig = DesktopIpc . makeIpcMethod ( {
218269 channel : IpcChannels . PREVIEW_GET_CONFIG_CHANNEL ,
219270 payload : DesktopPreviewConfigInputSchema ,
220271 result : DesktopPreviewWebviewConfigSchema ,
221- handler : Effect . fn ( "desktop.ipc.preview.getConfig" ) ( function * ( { environmentId } ) {
272+ handler : Effect . fn ( "desktop.ipc.preview.getConfig" ) ( function * ( { environmentId, profileId } ) {
222273 const manager = yield * PreviewManager . PreviewManager ;
223- yield * manager . getBrowserSession ( environmentId ) ;
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 ) ;
224279 return {
225- partition : yield * manager . getBrowserPartition ( environmentId ) ,
280+ partition : yield * manager . getBrowserPartition ( scope , persistent , namespace ) ,
226281 webPreferences : PREVIEW_WEBVIEW_PREFERENCES ,
227282 preloadUrl : NodeURL . pathToFileURL ( `${ __dirname } /preview-pick-preload.cjs` ) . href ,
228283 } ;
0 commit comments