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

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

Issue 2960843005: Revert of NetworkService: Destroy NetworkContexts on NetworkService teardown. (Closed)
Patch Set: 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"
13 #include "content/network/network_service_url_loader_factory_impl.h" 12 #include "content/network/network_service_url_loader_factory_impl.h"
14 #include "content/network/url_loader_impl.h" 13 #include "content/network/url_loader_impl.h"
15 #include "content/public/common/content_client.h" 14 #include "content/public/common/content_client.h"
16 #include "content/public/common/content_switches.h" 15 #include "content/public/common/content_switches.h"
17 #include "net/dns/host_resolver.h" 16 #include "net/dns/host_resolver.h"
18 #include "net/dns/mapped_host_resolver.h" 17 #include "net/dns/mapped_host_resolver.h"
19 #include "net/http/http_network_session.h" 18 #include "net/http/http_network_session.h"
20 #include "net/proxy/proxy_config.h" 19 #include "net/proxy/proxy_config.h"
21 #include "net/proxy/proxy_config_service_fixed.h" 20 #include "net/proxy/proxy_config_service_fixed.h"
22 #include "net/url_request/url_request_context.h" 21 #include "net/url_request/url_request_context.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 builder.set_proxy_config_service(std::move(fixed_config_service)); 79 builder.set_proxy_config_service(std::move(fixed_config_service));
81 } else { 80 } else {
82 builder.set_proxy_service(net::ProxyService::CreateDirect()); 81 builder.set_proxy_service(net::ProxyService::CreateDirect());
83 } 82 }
84 83
85 return builder.Build(); 84 return builder.Build();
86 } 85 }
87 86
88 } // namespace 87 } // namespace
89 88
90 NetworkContext::NetworkContext(NetworkService* network_service, 89 NetworkContext::NetworkContext(mojom::NetworkContextRequest request,
91 mojom::NetworkContextRequest request,
92 mojom::NetworkContextParamsPtr params) 90 mojom::NetworkContextParamsPtr params)
93 : network_service_(network_service), 91 : url_request_context_(MakeURLRequestContext()),
94 url_request_context_(MakeURLRequestContext()),
95 params_(std::move(params)), 92 params_(std::move(params)),
96 binding_(this, std::move(request)) { 93 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 }
101 94
102 NetworkContext::~NetworkContext() { 95 NetworkContext::~NetworkContext() {
103 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the 96 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the
104 // corresponding net::URLRequestContext is going away with this 97 // corresponding net::URLRequestContext is going away with this
105 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), 98 // NetworkContext. The loaders can be deregistering themselves in Cleanup(),
106 // so have to be careful. 99 // so have to be careful.
107 while (!url_loaders_.empty()) 100 while (!url_loaders_.empty())
108 (*url_loaders_.begin())->Cleanup(); 101 (*url_loaders_.begin())->Cleanup();
109
110 // May be nullptr in tests.
111 if (network_service_)
112 network_service_->DeregisterNetworkContext(this);
113 } 102 }
114 103
115 std::unique_ptr<NetworkContext> NetworkContext::CreateForTesting() { 104 std::unique_ptr<NetworkContext> NetworkContext::CreateForTesting() {
116 return base::WrapUnique(new NetworkContext); 105 return base::WrapUnique(new NetworkContext);
117 } 106 }
118 107
119 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) { 108 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) {
120 DCHECK(url_loaders_.count(url_loader) == 0); 109 DCHECK(url_loaders_.count(url_loader) == 0);
121 url_loaders_.insert(url_loader); 110 url_loaders_.insert(url_loader);
122 } 111 }
123 112
124 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) { 113 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) {
125 size_t removed_count = url_loaders_.erase(url_loader); 114 size_t removed_count = url_loaders_.erase(url_loader);
126 DCHECK(removed_count); 115 DCHECK(removed_count);
127 } 116 }
128 117
129 void NetworkContext::CreateURLLoaderFactory( 118 void NetworkContext::CreateURLLoaderFactory(
130 mojom::URLLoaderFactoryRequest request, 119 mojom::URLLoaderFactoryRequest request,
131 uint32_t process_id) { 120 uint32_t process_id) {
132 loader_factory_bindings_.AddBinding( 121 loader_factory_bindings_.AddBinding(
133 base::MakeUnique<NetworkServiceURLLoaderFactoryImpl>(this, process_id), 122 base::MakeUnique<NetworkServiceURLLoaderFactoryImpl>(this, process_id),
134 std::move(request)); 123 std::move(request));
135 } 124 }
136 125
137 void NetworkContext::HandleViewCacheRequest(const GURL& url, 126 void NetworkContext::HandleViewCacheRequest(const GURL& url,
138 mojom::URLLoaderClientPtr client) { 127 mojom::URLLoaderClientPtr client) {
139 StartCacheURLLoader(url, url_request_context_.get(), std::move(client)); 128 StartCacheURLLoader(url, url_request_context_.get(), std::move(client));
140 } 129 }
141 130
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
148 NetworkContext::NetworkContext() 131 NetworkContext::NetworkContext()
149 : network_service_(nullptr), 132 : url_request_context_(MakeURLRequestContext()),
150 url_request_context_(MakeURLRequestContext()),
151 binding_(this) {} 133 binding_(this) {}
152 134
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
160 } // namespace content 135 } // 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