Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: content/network/network_context.cc

Issue 2962693002: NetworkService: Destroy NetworkContexts on NetworkService teardown. (Closed)
Patch Set: Fix a couple comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/network/network_context.h ('k') | content/network/network_service.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/network/network_context.cc
diff --git a/content/network/network_context.cc b/content/network/network_context.cc
index 5862ea67d5b17977c9f4bac18390441b8c77a485..ea81b5ca2d8fa20ea9e6e82b18bafcd1ee97cb96 100644
--- a/content/network/network_context.cc
+++ b/content/network/network_context.cc
@@ -9,6 +9,7 @@
#include "base/strings/string_number_conversions.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/network/cache_url_loader.h"
+#include "content/network/network_service.h"
#include "content/network/network_service_url_loader_factory_impl.h"
#include "content/network/url_loader_impl.h"
#include "content/public/common/content_client.h"
@@ -86,11 +87,17 @@ std::unique_ptr<net::URLRequestContext> MakeURLRequestContext() {
} // namespace
-NetworkContext::NetworkContext(mojom::NetworkContextRequest request,
+NetworkContext::NetworkContext(NetworkService* network_service,
+ mojom::NetworkContextRequest request,
mojom::NetworkContextParamsPtr params)
- : url_request_context_(MakeURLRequestContext()),
+ : network_service_(network_service),
+ url_request_context_(MakeURLRequestContext()),
params_(std::move(params)),
- binding_(this, std::move(request)) {}
+ binding_(this, std::move(request)) {
+ network_service_->RegisterNetworkContext(this);
+ binding_.set_connection_error_handler(
+ base::Bind(&NetworkContext::OnConnectionError, base::Unretained(this)));
+}
NetworkContext::~NetworkContext() {
// Call each URLLoaderImpl and ask it to release its net::URLRequest, as the
@@ -99,6 +106,10 @@ NetworkContext::~NetworkContext() {
// so have to be careful.
while (!url_loaders_.empty())
(*url_loaders_.begin())->Cleanup();
+
+ // May be nullptr in tests.
+ if (network_service_)
+ network_service_->DeregisterNetworkContext(this);
}
std::unique_ptr<NetworkContext> NetworkContext::CreateForTesting() {
@@ -128,8 +139,22 @@ void NetworkContext::HandleViewCacheRequest(const GURL& url,
StartCacheURLLoader(url, url_request_context_.get(), std::move(client));
}
+void NetworkContext::Cleanup() {
+ // The NetworkService is going away, so have to destroy the
+ // net::URLRequestContext held by this NetworkContext.
+ delete this;
+}
+
NetworkContext::NetworkContext()
- : url_request_context_(MakeURLRequestContext()),
+ : network_service_(nullptr),
+ url_request_context_(MakeURLRequestContext()),
binding_(this) {}
+void NetworkContext::OnConnectionError() {
+ // Don't delete |this| in response to connection errors when it was created by
+ // CreateForTesting.
+ if (network_service_)
+ delete this;
+}
+
} // namespace content
« no previous file with comments | « content/network/network_context.h ('k') | content/network/network_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698