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

Side by Side Diff: content/network/network_context.cc

Issue 2962693002: NetworkService: Destroy NetworkContexts on NetworkService teardown. (Closed)
Patch Set: Fix a couple comments Created 3 years, 5 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 unified diff | Download patch
« no previous file with comments | « content/network/network_context.h ('k') | content/network/network_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/network/network_context.h" 5 #include "content/network/network_context.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "components/network_session_configurator/common/network_switches.h" 10 #include "components/network_session_configurator/common/network_switches.h"
11 #include "content/network/cache_url_loader.h" 11 #include "content/network/cache_url_loader.h"
12 #include "content/network/network_service.h"
12 #include "content/network/network_service_url_loader_factory_impl.h" 13 #include "content/network/network_service_url_loader_factory_impl.h"
13 #include "content/network/url_loader_impl.h" 14 #include "content/network/url_loader_impl.h"
14 #include "content/public/common/content_client.h" 15 #include "content/public/common/content_client.h"
15 #include "content/public/common/content_switches.h" 16 #include "content/public/common/content_switches.h"
16 #include "net/dns/host_resolver.h" 17 #include "net/dns/host_resolver.h"
17 #include "net/dns/mapped_host_resolver.h" 18 #include "net/dns/mapped_host_resolver.h"
18 #include "net/http/http_network_session.h" 19 #include "net/http/http_network_session.h"
19 #include "net/proxy/proxy_config.h" 20 #include "net/proxy/proxy_config.h"
20 #include "net/proxy/proxy_config_service_fixed.h" 21 #include "net/proxy/proxy_config_service_fixed.h"
21 #include "net/url_request/url_request_context.h" 22 #include "net/url_request/url_request_context.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 builder.set_proxy_config_service(std::move(fixed_config_service)); 80 builder.set_proxy_config_service(std::move(fixed_config_service));
80 } else { 81 } else {
81 builder.set_proxy_service(net::ProxyService::CreateDirect()); 82 builder.set_proxy_service(net::ProxyService::CreateDirect());
82 } 83 }
83 84
84 return builder.Build(); 85 return builder.Build();
85 } 86 }
86 87
87 } // namespace 88 } // namespace
88 89
89 NetworkContext::NetworkContext(mojom::NetworkContextRequest request, 90 NetworkContext::NetworkContext(NetworkService* network_service,
91 mojom::NetworkContextRequest request,
90 mojom::NetworkContextParamsPtr params) 92 mojom::NetworkContextParamsPtr params)
91 : url_request_context_(MakeURLRequestContext()), 93 : network_service_(network_service),
94 url_request_context_(MakeURLRequestContext()),
92 params_(std::move(params)), 95 params_(std::move(params)),
93 binding_(this, std::move(request)) {} 96 binding_(this, std::move(request)) {
97 network_service_->RegisterNetworkContext(this);
98 binding_.set_connection_error_handler(
99 base::Bind(&NetworkContext::OnConnectionError, base::Unretained(this)));
100 }
94 101
95 NetworkContext::~NetworkContext() { 102 NetworkContext::~NetworkContext() {
96 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the 103 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the
97 // corresponding net::URLRequestContext is going away with this 104 // corresponding net::URLRequestContext is going away with this
98 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), 105 // NetworkContext. The loaders can be deregistering themselves in Cleanup(),
99 // so have to be careful. 106 // so have to be careful.
100 while (!url_loaders_.empty()) 107 while (!url_loaders_.empty())
101 (*url_loaders_.begin())->Cleanup(); 108 (*url_loaders_.begin())->Cleanup();
109
110 // May be nullptr in tests.
111 if (network_service_)
112 network_service_->DeregisterNetworkContext(this);
102 } 113 }
103 114
104 std::unique_ptr<NetworkContext> NetworkContext::CreateForTesting() { 115 std::unique_ptr<NetworkContext> NetworkContext::CreateForTesting() {
105 return base::WrapUnique(new NetworkContext); 116 return base::WrapUnique(new NetworkContext);
106 } 117 }
107 118
108 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) { 119 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) {
109 DCHECK(url_loaders_.count(url_loader) == 0); 120 DCHECK(url_loaders_.count(url_loader) == 0);
110 url_loaders_.insert(url_loader); 121 url_loaders_.insert(url_loader);
111 } 122 }
112 123
113 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) { 124 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) {
114 size_t removed_count = url_loaders_.erase(url_loader); 125 size_t removed_count = url_loaders_.erase(url_loader);
115 DCHECK(removed_count); 126 DCHECK(removed_count);
116 } 127 }
117 128
118 void NetworkContext::CreateURLLoaderFactory( 129 void NetworkContext::CreateURLLoaderFactory(
119 mojom::URLLoaderFactoryRequest request, 130 mojom::URLLoaderFactoryRequest request,
120 uint32_t process_id) { 131 uint32_t process_id) {
121 loader_factory_bindings_.AddBinding( 132 loader_factory_bindings_.AddBinding(
122 base::MakeUnique<NetworkServiceURLLoaderFactoryImpl>(this, process_id), 133 base::MakeUnique<NetworkServiceURLLoaderFactoryImpl>(this, process_id),
123 std::move(request)); 134 std::move(request));
124 } 135 }
125 136
126 void NetworkContext::HandleViewCacheRequest(const GURL& url, 137 void NetworkContext::HandleViewCacheRequest(const GURL& url,
127 mojom::URLLoaderClientPtr client) { 138 mojom::URLLoaderClientPtr client) {
128 StartCacheURLLoader(url, url_request_context_.get(), std::move(client)); 139 StartCacheURLLoader(url, url_request_context_.get(), std::move(client));
129 } 140 }
130 141
142 void NetworkContext::Cleanup() {
143 // The NetworkService is going away, so have to destroy the
144 // net::URLRequestContext held by this NetworkContext.
145 delete this;
146 }
147
131 NetworkContext::NetworkContext() 148 NetworkContext::NetworkContext()
132 : url_request_context_(MakeURLRequestContext()), 149 : network_service_(nullptr),
150 url_request_context_(MakeURLRequestContext()),
133 binding_(this) {} 151 binding_(this) {}
134 152
153 void NetworkContext::OnConnectionError() {
154 // Don't delete |this| in response to connection errors when it was created by
155 // CreateForTesting.
156 if (network_service_)
157 delete this;
158 }
159
135 } // namespace content 160 } // namespace content
OLDNEW
« 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