Skip to content

Commit 97fff06

Browse files
committed
feat: add setSessionPartitionResolver
1 parent c058947 commit 97fff06

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

packages/electron-chrome-extensions/src/browser/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ExtensionRouter } from './router'
2020
import { checkLicense, License } from './license'
2121
import { readLoadedExtensionManifest } from './manifest'
2222
import { PermissionsAPI } from './api/permissions'
23+
import { resolvePartition } from './partition'
2324

2425
function checkVersion() {
2526
const electronVersion = process.versions.electron
@@ -105,8 +106,7 @@ export class ElectronChromeExtensions extends EventEmitter {
105106
}
106107

107108
const partition = url?.searchParams.get('partition') || '_self'
108-
const remoteSession =
109-
partition === '_self' ? session : electronSession.fromPartition(partition)
109+
const remoteSession = partition === '_self' ? session : resolvePartition(partition)
110110
const extensions = ElectronChromeExtensions.fromSession(remoteSession)
111111
if (!extensions) {
112112
return new Response(`ElectronChromeExtensions not found for "${partition}"`, {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { session } from 'electron'
2+
3+
type SessionPartitionResolver = (partition: string) => Electron.Session
4+
5+
let resolvePartitionImpl: SessionPartitionResolver = (partition) => session.fromPartition(partition)
6+
7+
/**
8+
* Overrides the default `session.fromPartition()` behavior for retrieving Electron Sessions.
9+
* This allows using custom identifiers (e.g., profile IDs) to find sessions, enabling features like
10+
* `<browser-actions>` to work with non-standard session management schemes.
11+
* @param handler A function that receives a string identifier and returns the corresponding Electron `Session`.
12+
*/
13+
export function setSessionPartitionResolver(resolver: SessionPartitionResolver) {
14+
resolvePartitionImpl = resolver
15+
}
16+
17+
export function resolvePartition(partition: string) {
18+
return resolvePartitionImpl(partition)
19+
}

packages/electron-chrome-extensions/src/browser/router.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { app, ipcMain, session, Session, WebContents } from 'electron'
1+
import { app, ipcMain, Session } from 'electron'
22
import debug from 'debug'
33

4+
import { resolvePartition } from './partition'
5+
46
// Shorten base64 encoded icons
57
const shortenValues = (k: string, v: any) =>
68
typeof v === 'string' && v.length > 128 ? v.substr(0, 128) + '...' : v
@@ -117,7 +119,7 @@ class RoutingDelegate {
117119
const ses =
118120
sessionPartition === DEFAULT_SESSION
119121
? getSessionFromEvent(event)
120-
: session.fromPartition(sessionPartition)
122+
: resolvePartition(sessionPartition)
121123

122124
const observer = this.sessionMap.get(ses)
123125

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './browser'
2+
export { setSessionPartitionResolver } from './browser/partition'

0 commit comments

Comments
 (0)