1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/url_request/url_request_context.h"
6
7 #include <inttypes.h>
8 #include <stdint.h>
9
10 #include "base/compiler_specific.h"
11 #include "base/debug/alias.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/metrics/histogram_functions.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/strings/string_util.h"
16 #include "base/types/pass_key.h"
17 #include "build/build_config.h"
18 #include "build/chromeos_buildflags.h"
19 #include "net/base/http_user_agent_settings.h"
20 #include "net/base/network_delegate.h"
21 #include "net/base/proxy_delegate.h"
22 #include "net/cert/cert_verifier.h"
23 #include "net/cert/ct_policy_enforcer.h"
24 #include "net/cert/sct_auditing_delegate.h"
25 #include "net/cookies/cookie_store.h"
26 #include "net/dns/host_resolver.h"
27 #include "net/http/http_auth_handler_factory.h"
28 #include "net/http/http_cache.h"
29 #include "net/http/http_network_session.h"
30 #include "net/http/http_server_properties.h"
31 #include "net/http/http_transaction_factory.h"
32 #include "net/http/transport_security_persister.h"
33 #include "net/http/transport_security_state.h"
34 #include "net/log/net_log.h"
35 #include "net/log/net_log_source.h"
36 #include "net/nqe/network_quality_estimator.h"
37 #include "net/proxy_resolution/proxy_resolution_service.h"
38 #include "net/quic/quic_context.h"
39 #include "net/socket/client_socket_factory.h"
40 #include "net/socket/ssl_client_socket_impl.h"
41 #include "net/ssl/ssl_config_service.h"
42 #include "net/url_request/url_request.h"
43 #include "net/url_request/url_request_job_factory.h"
44 #include "net/url_request/url_request_throttler_manager.h"
45
46 #if BUILDFLAG(ENABLE_REPORTING)
47 #include "net/network_error_logging/network_error_logging_service.h"
48 #include "net/network_error_logging/persistent_reporting_and_nel_store.h"
49 #include "net/reporting/reporting_service.h"
50 #endif // BUILDFLAG(ENABLE_REPORTING)
51
52 namespace net {
53
URLRequestContext(base::PassKey<URLRequestContextBuilder> pass_key)54 URLRequestContext::URLRequestContext(
55 base::PassKey<URLRequestContextBuilder> pass_key)
56 : url_requests_(std::make_unique<std::set<const URLRequest*>>()),
57 bound_network_(handles::kInvalidNetworkHandle) {}
58
~URLRequestContext()59 URLRequestContext::~URLRequestContext() {
60 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
61 #if BUILDFLAG(ENABLE_REPORTING)
62 // Shut down the NetworkErrorLoggingService so that destroying the
63 // ReportingService (which might abort in-flight URLRequests, generating
64 // network errors) won't recursively try to queue more network error
65 // reports.
66 if (network_error_logging_service())
67 network_error_logging_service()->OnShutdown();
68
69 // Shut down the ReportingService before the rest of the URLRequestContext,
70 // so it cancels any pending requests it may have.
71 if (reporting_service())
72 reporting_service()->OnShutdown();
73 #endif // BUILDFLAG(ENABLE_REPORTING)
74
75 // Shut down the ProxyResolutionService, as it may have pending URLRequests
76 // using this context. Since this cancels requests, it's not safe to
77 // subclass this, as some parts of the URLRequestContext may then be torn
78 // down before this cancels the ProxyResolutionService's URLRequests.
79 proxy_resolution_service()->OnShutdown();
80
81 DCHECK(host_resolver());
82 host_resolver()->OnShutdown();
83
84 AssertNoURLRequests();
85 }
86
GetNetworkSessionParams() const87 const HttpNetworkSessionParams* URLRequestContext::GetNetworkSessionParams()
88 const {
89 HttpTransactionFactory* transaction_factory = http_transaction_factory();
90 if (!transaction_factory)
91 return nullptr;
92 HttpNetworkSession* network_session = transaction_factory->GetSession();
93 if (!network_session)
94 return nullptr;
95 return &network_session->params();
96 }
97
GetNetworkSessionContext() const98 const HttpNetworkSessionContext* URLRequestContext::GetNetworkSessionContext()
99 const {
100 HttpTransactionFactory* transaction_factory = http_transaction_factory();
101 if (!transaction_factory)
102 return nullptr;
103 HttpNetworkSession* network_session = transaction_factory->GetSession();
104 if (!network_session)
105 return nullptr;
106 return &network_session->context();
107 }
108
109 // TODO(crbug.com/1052397): Revisit once build flag switch of lacros-chrome is
110 // complete.
111 #if !BUILDFLAG(IS_WIN) && \
112 !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
CreateRequest(const GURL & url,RequestPriority priority,URLRequest::Delegate * delegate) const113 std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
114 const GURL& url,
115 RequestPriority priority,
116 URLRequest::Delegate* delegate) const {
117 return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION,
118 /*is_for_websockets=*/false);
119 }
120 #endif
121
CreateRequest(const GURL & url,RequestPriority priority,URLRequest::Delegate * delegate,NetworkTrafficAnnotationTag traffic_annotation,bool is_for_websockets,const absl::optional<net::NetLogSource> net_log_source) const122 std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
123 const GURL& url,
124 RequestPriority priority,
125 URLRequest::Delegate* delegate,
126 NetworkTrafficAnnotationTag traffic_annotation,
127 bool is_for_websockets,
128 const absl::optional<net::NetLogSource> net_log_source) const {
129 return std::make_unique<URLRequest>(
130 base::PassKey<URLRequestContext>(), url, priority, delegate, this,
131 traffic_annotation, is_for_websockets, net_log_source);
132 }
133
AssertNoURLRequests() const134 void URLRequestContext::AssertNoURLRequests() const {
135 int num_requests = url_requests_->size();
136 if (num_requests != 0) {
137 // We're leaking URLRequests :( Dump the URL of the first one and record how
138 // many we leaked so we have an idea of how bad it is.
139 const URLRequest* request = *url_requests_->begin();
140 int load_flags = request->load_flags();
141 DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
142 base::debug::Alias(&num_requests);
143 base::debug::Alias(&load_flags);
144 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
145 << request->url().spec().c_str() << ".";
146 }
147 }
148
set_net_log(NetLog * net_log)149 void URLRequestContext::set_net_log(NetLog* net_log) {
150 net_log_ = net_log;
151 }
set_host_resolver(std::unique_ptr<HostResolver> host_resolver)152 void URLRequestContext::set_host_resolver(
153 std::unique_ptr<HostResolver> host_resolver) {
154 DCHECK(host_resolver.get());
155 host_resolver_ = std::move(host_resolver);
156 }
set_cert_verifier(std::unique_ptr<CertVerifier> cert_verifier)157 void URLRequestContext::set_cert_verifier(
158 std::unique_ptr<CertVerifier> cert_verifier) {
159 cert_verifier_ = std::move(cert_verifier);
160 }
set_proxy_resolution_service(std::unique_ptr<ProxyResolutionService> proxy_resolution_service)161 void URLRequestContext::set_proxy_resolution_service(
162 std::unique_ptr<ProxyResolutionService> proxy_resolution_service) {
163 proxy_resolution_service_ = std::move(proxy_resolution_service);
164 }
set_proxy_delegate(std::unique_ptr<ProxyDelegate> proxy_delegate)165 void URLRequestContext::set_proxy_delegate(
166 std::unique_ptr<ProxyDelegate> proxy_delegate) {
167 proxy_delegate_ = std::move(proxy_delegate);
168 }
set_ssl_config_service(std::unique_ptr<SSLConfigService> service)169 void URLRequestContext::set_ssl_config_service(
170 std::unique_ptr<SSLConfigService> service) {
171 ssl_config_service_ = std::move(service);
172 }
set_http_auth_handler_factory(std::unique_ptr<HttpAuthHandlerFactory> factory)173 void URLRequestContext::set_http_auth_handler_factory(
174 std::unique_ptr<HttpAuthHandlerFactory> factory) {
175 http_auth_handler_factory_ = std::move(factory);
176 }
set_http_network_session(std::unique_ptr<HttpNetworkSession> http_network_session)177 void URLRequestContext::set_http_network_session(
178 std::unique_ptr<HttpNetworkSession> http_network_session) {
179 http_network_session_ = std::move(http_network_session);
180 }
set_http_transaction_factory(std::unique_ptr<HttpTransactionFactory> factory)181 void URLRequestContext::set_http_transaction_factory(
182 std::unique_ptr<HttpTransactionFactory> factory) {
183 http_transaction_factory_ = std::move(factory);
184 }
set_network_delegate(std::unique_ptr<NetworkDelegate> network_delegate)185 void URLRequestContext::set_network_delegate(
186 std::unique_ptr<NetworkDelegate> network_delegate) {
187 network_delegate_ = std::move(network_delegate);
188 }
set_http_server_properties(std::unique_ptr<HttpServerProperties> http_server_properties)189 void URLRequestContext::set_http_server_properties(
190 std::unique_ptr<HttpServerProperties> http_server_properties) {
191 http_server_properties_ = std::move(http_server_properties);
192 }
set_cookie_store(std::unique_ptr<CookieStore> cookie_store)193 void URLRequestContext::set_cookie_store(
194 std::unique_ptr<CookieStore> cookie_store) {
195 cookie_store_ = std::move(cookie_store);
196 }
set_transport_security_state(std::unique_ptr<TransportSecurityState> state)197 void URLRequestContext::set_transport_security_state(
198 std::unique_ptr<TransportSecurityState> state) {
199 transport_security_state_ = std::move(state);
200 }
set_ct_policy_enforcer(std::unique_ptr<CTPolicyEnforcer> enforcer)201 void URLRequestContext::set_ct_policy_enforcer(
202 std::unique_ptr<CTPolicyEnforcer> enforcer) {
203 ct_policy_enforcer_ = std::move(enforcer);
204 }
set_sct_auditing_delegate(std::unique_ptr<SCTAuditingDelegate> delegate)205 void URLRequestContext::set_sct_auditing_delegate(
206 std::unique_ptr<SCTAuditingDelegate> delegate) {
207 sct_auditing_delegate_ = std::move(delegate);
208 }
set_job_factory(std::unique_ptr<const URLRequestJobFactory> job_factory)209 void URLRequestContext::set_job_factory(
210 std::unique_ptr<const URLRequestJobFactory> job_factory) {
211 job_factory_storage_ = std::move(job_factory);
212 job_factory_ = job_factory_storage_.get();
213 }
set_throttler_manager(std::unique_ptr<URLRequestThrottlerManager> throttler_manager)214 void URLRequestContext::set_throttler_manager(
215 std::unique_ptr<URLRequestThrottlerManager> throttler_manager) {
216 throttler_manager_ = std::move(throttler_manager);
217 }
set_quic_context(std::unique_ptr<QuicContext> quic_context)218 void URLRequestContext::set_quic_context(
219 std::unique_ptr<QuicContext> quic_context) {
220 quic_context_ = std::move(quic_context);
221 }
set_http_user_agent_settings(std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings)222 void URLRequestContext::set_http_user_agent_settings(
223 std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings) {
224 http_user_agent_settings_ = std::move(http_user_agent_settings);
225 }
set_network_quality_estimator(NetworkQualityEstimator * network_quality_estimator)226 void URLRequestContext::set_network_quality_estimator(
227 NetworkQualityEstimator* network_quality_estimator) {
228 network_quality_estimator_ = network_quality_estimator;
229 }
set_client_socket_factory(std::unique_ptr<ClientSocketFactory> client_socket_factory)230 void URLRequestContext::set_client_socket_factory(
231 std::unique_ptr<ClientSocketFactory> client_socket_factory) {
232 client_socket_factory_ = std::move(client_socket_factory);
233 }
234 #if BUILDFLAG(ENABLE_REPORTING)
set_persistent_reporting_and_nel_store(std::unique_ptr<PersistentReportingAndNelStore> persistent_reporting_and_nel_store)235 void URLRequestContext::set_persistent_reporting_and_nel_store(
236 std::unique_ptr<PersistentReportingAndNelStore>
237 persistent_reporting_and_nel_store) {
238 persistent_reporting_and_nel_store_ =
239 std::move(persistent_reporting_and_nel_store);
240 }
set_reporting_service(std::unique_ptr<ReportingService> reporting_service)241 void URLRequestContext::set_reporting_service(
242 std::unique_ptr<ReportingService> reporting_service) {
243 reporting_service_ = std::move(reporting_service);
244 }
set_network_error_logging_service(std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service)245 void URLRequestContext::set_network_error_logging_service(
246 std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service) {
247 network_error_logging_service_ = std::move(network_error_logging_service);
248 }
249 #endif // BUILDFLAG(ENABLE_REPORTING)
250
set_transport_security_persister(std::unique_ptr<TransportSecurityPersister> transport_security_persister)251 void URLRequestContext::set_transport_security_persister(
252 std::unique_ptr<TransportSecurityPersister> transport_security_persister) {
253 transport_security_persister_ = std::move(transport_security_persister);
254 }
255
256 } // namespace net
257