From e4213e326b2456218cd540f47c773372769c81a9 Mon Sep 17 00:00:00 2001 From: LuoJinghua Date: Fri, 9 Oct 2020 00:45:36 +0800 Subject: [PATCH] feat: add ability to close connections for a session (#24945) These methods are needed for closing all idle and in-flight connections after switching to another proxy from same origin for a session, otherwise these connections may be reused for future requests which is unexpected for most of users. --- docs/api/session.md | 9 +++++++++ shell/browser/api/electron_api_session.cc | 14 ++++++++++++++ shell/browser/api/electron_api_session.h | 1 + 3 files changed, 24 insertions(+) diff --git a/docs/api/session.md b/docs/api/session.md index 390d750a5445..b1a3ead845ad 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -295,6 +295,9 @@ Sets the proxy settings. When `pacScript` and `proxyRules` are provided together, the `proxyRules` option is ignored and `pacScript` configuration is applied. +You may need `ses.closeAllConnections` to close currently in flight connections to prevent +pooled sockets using previous proxy from being reused by future requests. + The `proxyRules` has to follow the rules below: ```sh @@ -404,6 +407,12 @@ window.webContents.session.enableNetworkEmulation({ offline: true }) Preconnects the given number of sockets to an origin. +#### `ses.closeAllConnections()` + +Returns `Promise` - Resolves when all connections are closed. + +**Note:** It will terminate / fail all requests currently in flight. + #### `ses.disableNetworkEmulation()` Disables any network emulation already active for the `session`. Resets to diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 365dff41ec1f..4db3ba45ecb8 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -903,6 +903,19 @@ void Session::Preconnect(const gin_helper::Dictionary& options, url, num_sockets_to_preconnect)); } +v8::Local Session::CloseAllConnections() { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + gin_helper::Promise promise(isolate); + auto handle = promise.GetHandle(); + + content::BrowserContext::GetDefaultStoragePartition(browser_context_) + ->GetNetworkContext() + ->CloseAllConnections(base::BindOnce( + gin_helper::Promise::ResolvePromise, std::move(promise))); + + return handle; +} + #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value Session::GetSpellCheckerLanguages() { return browser_context_->prefs() @@ -1106,6 +1119,7 @@ gin::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( &Session::RemoveWordFromSpellCheckerDictionary) #endif .SetMethod("preconnect", &Session::Preconnect) + .SetMethod("closeAllConnections", &Session::CloseAllConnections) .SetProperty("cookies", &Session::Cookies) .SetProperty("netLog", &Session::NetLog) .SetProperty("protocol", &Session::Protocol) diff --git a/shell/browser/api/electron_api_session.h b/shell/browser/api/electron_api_session.h index cdb5cb3192ac..8dcfc970aab6 100644 --- a/shell/browser/api/electron_api_session.h +++ b/shell/browser/api/electron_api_session.h @@ -122,6 +122,7 @@ class Session : public gin::Wrappable, v8::Local WebRequest(v8::Isolate* isolate); v8::Local NetLog(v8::Isolate* isolate); void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args); + v8::Local CloseAllConnections(); #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value GetSpellCheckerLanguages(); void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,