Skip to content

Commit fa61e3b

Browse files
feat: add session.storagePath to get path on disk for session data (electron#28665)
1 parent b97b973 commit fa61e3b

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

docs/api/session.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,11 @@ Returns `Extension[]` - A list of all loaded extensions.
817817
**Note:** This API cannot be called before the `ready` event of the `app` module
818818
is emitted.
819819

820+
#### `ses.getStoragePath()`
821+
822+
A `String | null` indicating the absolute file system path where data for this
823+
session is persisted on disk. For in memory sessions this returns `null`.
824+
820825
### Instance Properties
821826

822827
The following properties are available on instances of `Session`:
@@ -830,6 +835,11 @@ code to the `setSpellCheckerLanguages` API that isn't in this array will result
830835

831836
A `Boolean` indicating whether builtin spell checker is enabled.
832837

838+
#### `ses.storagePath` _Readonly_
839+
840+
A `String | null` indicating the absolute file system path where data for this
841+
session is persisted on disk. For in memory sessions this returns `null`.
842+
833843
#### `ses.cookies` _Readonly_
834844

835845
A [`Cookies`](cookies.md) object for this session.

shell/browser/api/electron_api_session.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,13 @@ v8::Local<v8::Promise> Session::CloseAllConnections() {
964964
return handle;
965965
}
966966

967+
v8::Local<v8::Value> Session::GetPath(v8::Isolate* isolate) {
968+
if (browser_context_->IsOffTheRecord()) {
969+
return v8::Null(isolate);
970+
}
971+
return gin::ConvertToV8(isolate, browser_context_->GetPath());
972+
}
973+
967974
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
968975
base::Value Session::GetSpellCheckerLanguages() {
969976
return browser_context_->prefs()
@@ -1188,11 +1195,13 @@ gin::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
11881195
#endif
11891196
.SetMethod("preconnect", &Session::Preconnect)
11901197
.SetMethod("closeAllConnections", &Session::CloseAllConnections)
1198+
.SetMethod("getStoragePath", &Session::GetPath)
11911199
.SetProperty("cookies", &Session::Cookies)
11921200
.SetProperty("netLog", &Session::NetLog)
11931201
.SetProperty("protocol", &Session::Protocol)
11941202
.SetProperty("serviceWorkers", &Session::ServiceWorkerContext)
1195-
.SetProperty("webRequest", &Session::WebRequest);
1203+
.SetProperty("webRequest", &Session::WebRequest)
1204+
.SetProperty("storagePath", &Session::GetPath);
11961205
}
11971206

11981207
const char* Session::GetTypeName() {

shell/browser/api/electron_api_session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class Session : public gin::Wrappable<Session>,
124124
v8::Local<v8::Value> NetLog(v8::Isolate* isolate);
125125
void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args);
126126
v8::Local<v8::Promise> CloseAllConnections();
127+
v8::Local<v8::Value> GetPath(v8::Isolate* isolate);
127128
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
128129
base::Value GetSpellCheckerLanguages();
129130
void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,

spec-main/api-session-spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,24 @@ describe('session module', () => {
10901090
});
10911091
});
10921092

1093+
describe('session.storagePage', () => {
1094+
it('returns a string', () => {
1095+
expect(session.defaultSession.storagePath).to.be.a('string');
1096+
});
1097+
1098+
it('returns null for in memory sessions', () => {
1099+
expect(session.fromPartition('in-memory').storagePath).to.equal(null);
1100+
});
1101+
1102+
it('returns different paths for partitions and the default session', () => {
1103+
expect(session.defaultSession.storagePath).to.not.equal(session.fromPartition('persist:two').storagePath);
1104+
});
1105+
1106+
it('returns different paths for different partitions', () => {
1107+
expect(session.fromPartition('persist:one').storagePath).to.not.equal(session.fromPartition('persist:two').storagePath);
1108+
});
1109+
});
1110+
10931111
describe('ses.setSSLConfig()', () => {
10941112
it('can disable cipher suites', async () => {
10951113
const ses = session.fromPartition('' + Math.random());

0 commit comments

Comments
 (0)