feat: add API to check if session is persistent (#22622)
This commit is contained in:
parent
19314d3caf
commit
34e004015d
4 changed files with 37 additions and 0 deletions
|
@ -439,6 +439,13 @@ example `"en-US,fr,de,ko,zh-CN,ja"`.
|
||||||
This doesn't affect existing `WebContents`, and each `WebContents` can use
|
This doesn't affect existing `WebContents`, and each `WebContents` can use
|
||||||
`webContents.setUserAgent` to override the session-wide user agent.
|
`webContents.setUserAgent` to override the session-wide user agent.
|
||||||
|
|
||||||
|
#### `ses.isPersistent()`
|
||||||
|
|
||||||
|
Returns `Boolean` - Whether or not this session is a persistent one. The default
|
||||||
|
`webContents` session of a `BrowserWindow` is persistent. When creating a session
|
||||||
|
from a partition, session prefixed with `persist:` will be persistent, while others
|
||||||
|
will be temporary.
|
||||||
|
|
||||||
#### `ses.getUserAgent()`
|
#### `ses.getUserAgent()`
|
||||||
|
|
||||||
Returns `String` - The user agent for this session.
|
Returns `String` - The user agent for this session.
|
||||||
|
|
|
@ -615,6 +615,10 @@ std::string Session::GetUserAgent() {
|
||||||
return browser_context_->GetUserAgent();
|
return browser_context_->GetUserAgent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Session::IsPersistent() {
|
||||||
|
return !browser_context_->IsOffTheRecord();
|
||||||
|
}
|
||||||
|
|
||||||
v8::Local<v8::Promise> Session::GetBlobData(v8::Isolate* isolate,
|
v8::Local<v8::Promise> Session::GetBlobData(v8::Isolate* isolate,
|
||||||
const std::string& uuid) {
|
const std::string& uuid) {
|
||||||
gin::Handle<DataPipeHolder> holder = DataPipeHolder::From(isolate, uuid);
|
gin::Handle<DataPipeHolder> holder = DataPipeHolder::From(isolate, uuid);
|
||||||
|
@ -981,6 +985,7 @@ void Session::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("clearAuthCache", &Session::ClearAuthCache)
|
.SetMethod("clearAuthCache", &Session::ClearAuthCache)
|
||||||
.SetMethod("allowNTLMCredentialsForDomains",
|
.SetMethod("allowNTLMCredentialsForDomains",
|
||||||
&Session::AllowNTLMCredentialsForDomains)
|
&Session::AllowNTLMCredentialsForDomains)
|
||||||
|
.SetMethod("isPersistent", &Session::IsPersistent)
|
||||||
.SetMethod("setUserAgent", &Session::SetUserAgent)
|
.SetMethod("setUserAgent", &Session::SetUserAgent)
|
||||||
.SetMethod("getUserAgent", &Session::GetUserAgent)
|
.SetMethod("getUserAgent", &Session::GetUserAgent)
|
||||||
.SetMethod("getBlobData", &Session::GetBlobData)
|
.SetMethod("getBlobData", &Session::GetBlobData)
|
||||||
|
|
|
@ -86,6 +86,7 @@ class Session : public gin_helper::TrackableObject<Session>,
|
||||||
void AllowNTLMCredentialsForDomains(const std::string& domains);
|
void AllowNTLMCredentialsForDomains(const std::string& domains);
|
||||||
void SetUserAgent(const std::string& user_agent, gin_helper::Arguments* args);
|
void SetUserAgent(const std::string& user_agent, gin_helper::Arguments* args);
|
||||||
std::string GetUserAgent();
|
std::string GetUserAgent();
|
||||||
|
bool IsPersistent();
|
||||||
v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate,
|
v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate,
|
||||||
const std::string& uuid);
|
const std::string& uuid);
|
||||||
void DownloadURL(const GURL& url);
|
void DownloadURL(const GURL& url);
|
||||||
|
|
|
@ -898,6 +898,30 @@ describe('session module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('ses.isPersistent()', () => {
|
||||||
|
afterEach(closeAllWindows)
|
||||||
|
|
||||||
|
it('returns default session as persistent', () => {
|
||||||
|
const w = new BrowserWindow({
|
||||||
|
show: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const ses = w.webContents.session
|
||||||
|
|
||||||
|
expect(ses.isPersistent()).to.be.true()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns persist: session as persistent', () => {
|
||||||
|
const ses = session.fromPartition(`persist:${Math.random()}`)
|
||||||
|
expect(ses.isPersistent()).to.be.true()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns temporary session as not persistent', () => {
|
||||||
|
const ses = session.fromPartition(`${Math.random()}`)
|
||||||
|
expect(ses.isPersistent()).to.be.false()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('ses.setUserAgent()', () => {
|
describe('ses.setUserAgent()', () => {
|
||||||
afterEach(closeAllWindows)
|
afterEach(closeAllWindows)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue