From db8f3e3f17c88e8d2e8504017ba439240ffa0f54 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 14 Apr 2021 13:36:33 -0700 Subject: [PATCH 1/2] feat: add session.storagePath to get path on disk for session data --- docs/api/session.md | 10 ++++++++++ shell/browser/api/electron_api_session.cc | 11 ++++++++++- shell/browser/api/electron_api_session.h | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/api/session.md b/docs/api/session.md index 25b3af870453c..8ad94ee239931 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -822,6 +822,11 @@ Returns `Extension[]` - A list of all loaded extensions. **Note:** This API cannot be called before the `ready` event of the `app` module is emitted. +#### `ses.getStoragePath()` + +A `String | null` indicating the absolute file system path where data for this +session is persisted on disk. For in memory sessions this returns `null`. + ### Instance Properties The following properties are available on instances of `Session`: @@ -835,6 +840,11 @@ code to the `setSpellCheckerLanguages` API that isn't in this array will result A `Boolean` indicating whether builtin spell checker is enabled. +#### `ses.storagePath` _Readonly_ + +A `String | null` indicating the absolute file system path where data for this +session is persisted on disk. For in memory sessions this returns `null`. + #### `ses.cookies` _Readonly_ A [`Cookies`](cookies.md) object for this session. diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 1b42e561c94a1..095bc5d23d311 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -971,6 +971,13 @@ v8::Local Session::CloseAllConnections() { return handle; } +v8::Local Session::GetPath(v8::Isolate* isolate) { + if (browser_context_->IsOffTheRecord()) { + return v8::Null(isolate); + } + return gin::ConvertToV8(isolate, browser_context_->GetPath()); +} + #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value Session::GetSpellCheckerLanguages() { return browser_context_->prefs() @@ -1195,11 +1202,13 @@ gin::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( #endif .SetMethod("preconnect", &Session::Preconnect) .SetMethod("closeAllConnections", &Session::CloseAllConnections) + .SetMethod("getStoragePath", &Session::GetPath) .SetProperty("cookies", &Session::Cookies) .SetProperty("netLog", &Session::NetLog) .SetProperty("protocol", &Session::Protocol) .SetProperty("serviceWorkers", &Session::ServiceWorkerContext) - .SetProperty("webRequest", &Session::WebRequest); + .SetProperty("webRequest", &Session::WebRequest) + .SetProperty("storagePath", &Session::GetPath); } const char* Session::GetTypeName() { diff --git a/shell/browser/api/electron_api_session.h b/shell/browser/api/electron_api_session.h index 317c4a2985c7f..1e56fc357d6da 100644 --- a/shell/browser/api/electron_api_session.h +++ b/shell/browser/api/electron_api_session.h @@ -124,6 +124,7 @@ class Session : public gin::Wrappable, v8::Local NetLog(v8::Isolate* isolate); void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args); v8::Local CloseAllConnections(); + v8::Local GetPath(v8::Isolate* isolate); #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value GetSpellCheckerLanguages(); void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower, From 0d8fcf188a831e3e23dbc4cad8a1ed9afe812628 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 14 Apr 2021 15:08:01 -0700 Subject: [PATCH 2/2] spec: add session.storagePath tests --- spec-main/api-session-spec.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec-main/api-session-spec.ts b/spec-main/api-session-spec.ts index 295acdd16f6a1..9ffce8e19463f 100644 --- a/spec-main/api-session-spec.ts +++ b/spec-main/api-session-spec.ts @@ -1090,6 +1090,24 @@ describe('session module', () => { }); }); + describe('session.storagePage', () => { + it('returns a string', () => { + expect(session.defaultSession.storagePath).to.be.a('string'); + }); + + it('returns null for in memory sessions', () => { + expect(session.fromPartition('in-memory').storagePath).to.equal(null); + }); + + it('returns different paths for partitions and the default session', () => { + expect(session.defaultSession.storagePath).to.not.equal(session.fromPartition('persist:two').storagePath); + }); + + it('returns different paths for different partitions', () => { + expect(session.fromPartition('persist:one').storagePath).to.not.equal(session.fromPartition('persist:two').storagePath); + }); + }); + describe('ses.setSSLConfig()', () => { it('can disable cipher suites', async () => { const ses = session.fromPartition('' + Math.random());