• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/notreached.h"
16 #include "base/strings/string_util.h"
17 #include "base/types/pass_key.h"
18 #include "build/build_config.h"
19 #include "build/chromeos_buildflags.h"
20 #include "net/base/http_user_agent_settings.h"
21 #include "net/base/network_delegate.h"
22 #include "net/base/proxy_delegate.h"
23 #include "net/cert/cert_verifier.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 
45 #if BUILDFLAG(ENABLE_REPORTING)
46 #include "net/network_error_logging/network_error_logging_service.h"
47 #include "net/network_error_logging/persistent_reporting_and_nel_store.h"
48 #include "net/reporting/reporting_service.h"
49 #endif  // BUILDFLAG(ENABLE_REPORTING)
50 
51 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
52 #include "net/device_bound_sessions/session_service.h"
53 #include "net/device_bound_sessions/session_store.h"
54 #endif  // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
55 
56 namespace net {
57 
URLRequestContext(base::PassKey<URLRequestContextBuilder> pass_key)58 URLRequestContext::URLRequestContext(
59     base::PassKey<URLRequestContextBuilder> pass_key)
60     : url_requests_(std::make_unique<
61                     std::set<raw_ptr<const URLRequest, SetExperimental>>>()),
62       bound_network_(handles::kInvalidNetworkHandle) {}
63 
~URLRequestContext()64 URLRequestContext::~URLRequestContext() {
65   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
66 #if BUILDFLAG(ENABLE_REPORTING)
67   // Shut down the NetworkErrorLoggingService so that destroying the
68   // ReportingService (which might abort in-flight URLRequests, generating
69   // network errors) won't recursively try to queue more network error
70   // reports.
71   if (network_error_logging_service())
72     network_error_logging_service()->OnShutdown();
73 
74   // Shut down the ReportingService before the rest of the URLRequestContext,
75   // so it cancels any pending requests it may have.
76   if (reporting_service())
77     reporting_service()->OnShutdown();
78 #endif  // BUILDFLAG(ENABLE_REPORTING)
79 
80   // Shut down the ProxyResolutionService, as it may have pending URLRequests
81   // using this context. Since this cancels requests, it's not safe to
82   // subclass this, as some parts of the URLRequestContext may then be torn
83   // down before this cancels the ProxyResolutionService's URLRequests.
84   proxy_resolution_service()->OnShutdown();
85 
86   // If a ProxyDelegate is set then the builder gave it a pointer to the
87   // ProxyResolutionService, so clear that here to avoid having a dangling
88   // pointer. There's no need to clear the ProxyResolutionService's pointer to
89   // ProxyDelegate because the member destruction order ensures that
90   // ProxyResolutionService is destroyed first.
91   if (proxy_delegate()) {
92     proxy_delegate()->SetProxyResolutionService(nullptr);
93   }
94 
95   DCHECK(host_resolver());
96   host_resolver()->OnShutdown();
97 
98   AssertNoURLRequests();
99 }
100 
GetNetworkSessionParams() const101 const HttpNetworkSessionParams* URLRequestContext::GetNetworkSessionParams()
102     const {
103   HttpTransactionFactory* transaction_factory = http_transaction_factory();
104   if (!transaction_factory)
105     return nullptr;
106   HttpNetworkSession* network_session = transaction_factory->GetSession();
107   if (!network_session)
108     return nullptr;
109   return &network_session->params();
110 }
111 
GetNetworkSessionContext() const112 const HttpNetworkSessionContext* URLRequestContext::GetNetworkSessionContext()
113     const {
114   HttpTransactionFactory* transaction_factory = http_transaction_factory();
115   if (!transaction_factory)
116     return nullptr;
117   HttpNetworkSession* network_session = transaction_factory->GetSession();
118   if (!network_session)
119     return nullptr;
120   return &network_session->context();
121 }
122 
123 // TODO(crbug.com/40118868): Revisit once build flag switch of lacros-chrome is
124 // complete.
125 #if !BUILDFLAG(IS_WIN) && \
126     !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
CreateRequest(const GURL & url,RequestPriority priority,URLRequest::Delegate * delegate) const127 std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
128     const GURL& url,
129     RequestPriority priority,
130     URLRequest::Delegate* delegate) const {
131   return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION,
132                        /*is_for_websockets=*/false);
133 }
134 #endif
135 
CreateRequest(const GURL & url,RequestPriority priority,URLRequest::Delegate * delegate,NetworkTrafficAnnotationTag traffic_annotation,bool is_for_websockets,const std::optional<net::NetLogSource> net_log_source) const136 std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
137     const GURL& url,
138     RequestPriority priority,
139     URLRequest::Delegate* delegate,
140     NetworkTrafficAnnotationTag traffic_annotation,
141     bool is_for_websockets,
142     const std::optional<net::NetLogSource> net_log_source) const {
143   return std::make_unique<URLRequest>(
144       base::PassKey<URLRequestContext>(), url, priority, delegate, this,
145       traffic_annotation, is_for_websockets, net_log_source);
146 }
147 
AssertNoURLRequests() const148 void URLRequestContext::AssertNoURLRequests() const {
149   int num_requests = url_requests_->size();
150   if (num_requests != 0) {
151     // We're leaking URLRequests :( Dump the URL of the first one and record how
152     // many we leaked so we have an idea of how bad it is.
153     const URLRequest* request = *url_requests_->begin();
154     int load_flags = request->load_flags();
155     DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
156     base::debug::Alias(&num_requests);
157     base::debug::Alias(&load_flags);
158     NOTREACHED() << "Leaked " << num_requests << " URLRequest(s). First URL: "
159                  << request->url().spec().c_str() << ".";
160   }
161 }
162 
set_net_log(NetLog * net_log)163 void URLRequestContext::set_net_log(NetLog* net_log) {
164   net_log_ = net_log;
165 }
set_host_resolver(std::unique_ptr<HostResolver> host_resolver)166 void URLRequestContext::set_host_resolver(
167     std::unique_ptr<HostResolver> host_resolver) {
168   DCHECK(host_resolver.get());
169   host_resolver_ = std::move(host_resolver);
170 }
set_cert_verifier(std::unique_ptr<CertVerifier> cert_verifier)171 void URLRequestContext::set_cert_verifier(
172     std::unique_ptr<CertVerifier> cert_verifier) {
173   cert_verifier_ = std::move(cert_verifier);
174 }
set_proxy_resolution_service(std::unique_ptr<ProxyResolutionService> proxy_resolution_service)175 void URLRequestContext::set_proxy_resolution_service(
176     std::unique_ptr<ProxyResolutionService> proxy_resolution_service) {
177   proxy_resolution_service_ = std::move(proxy_resolution_service);
178 }
set_proxy_delegate(std::unique_ptr<ProxyDelegate> proxy_delegate)179 void URLRequestContext::set_proxy_delegate(
180     std::unique_ptr<ProxyDelegate> proxy_delegate) {
181   proxy_delegate_ = std::move(proxy_delegate);
182 }
set_ssl_config_service(std::unique_ptr<SSLConfigService> service)183 void URLRequestContext::set_ssl_config_service(
184     std::unique_ptr<SSLConfigService> service) {
185   ssl_config_service_ = std::move(service);
186 }
set_http_auth_handler_factory(std::unique_ptr<HttpAuthHandlerFactory> factory)187 void URLRequestContext::set_http_auth_handler_factory(
188     std::unique_ptr<HttpAuthHandlerFactory> factory) {
189   http_auth_handler_factory_ = std::move(factory);
190 }
set_http_network_session(std::unique_ptr<HttpNetworkSession> http_network_session)191 void URLRequestContext::set_http_network_session(
192     std::unique_ptr<HttpNetworkSession> http_network_session) {
193   http_network_session_ = std::move(http_network_session);
194 }
set_http_transaction_factory(std::unique_ptr<HttpTransactionFactory> factory)195 void URLRequestContext::set_http_transaction_factory(
196     std::unique_ptr<HttpTransactionFactory> factory) {
197   http_transaction_factory_ = std::move(factory);
198 }
set_network_delegate(std::unique_ptr<NetworkDelegate> network_delegate)199 void URLRequestContext::set_network_delegate(
200     std::unique_ptr<NetworkDelegate> network_delegate) {
201   network_delegate_ = std::move(network_delegate);
202 }
set_http_server_properties(std::unique_ptr<HttpServerProperties> http_server_properties)203 void URLRequestContext::set_http_server_properties(
204     std::unique_ptr<HttpServerProperties> http_server_properties) {
205   http_server_properties_ = std::move(http_server_properties);
206 }
set_cookie_store(std::unique_ptr<CookieStore> cookie_store)207 void URLRequestContext::set_cookie_store(
208     std::unique_ptr<CookieStore> cookie_store) {
209   cookie_store_ = std::move(cookie_store);
210 }
set_transport_security_state(std::unique_ptr<TransportSecurityState> state)211 void URLRequestContext::set_transport_security_state(
212     std::unique_ptr<TransportSecurityState> state) {
213   transport_security_state_ = std::move(state);
214 }
set_sct_auditing_delegate(std::unique_ptr<SCTAuditingDelegate> delegate)215 void URLRequestContext::set_sct_auditing_delegate(
216     std::unique_ptr<SCTAuditingDelegate> delegate) {
217   sct_auditing_delegate_ = std::move(delegate);
218 }
set_job_factory(std::unique_ptr<const URLRequestJobFactory> job_factory)219 void URLRequestContext::set_job_factory(
220     std::unique_ptr<const URLRequestJobFactory> job_factory) {
221   job_factory_storage_ = std::move(job_factory);
222   job_factory_ = job_factory_storage_.get();
223 }
set_quic_context(std::unique_ptr<QuicContext> quic_context)224 void URLRequestContext::set_quic_context(
225     std::unique_ptr<QuicContext> quic_context) {
226   quic_context_ = std::move(quic_context);
227 }
set_http_user_agent_settings(std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings)228 void URLRequestContext::set_http_user_agent_settings(
229     std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings) {
230   http_user_agent_settings_ = std::move(http_user_agent_settings);
231 }
set_network_quality_estimator(NetworkQualityEstimator * network_quality_estimator)232 void URLRequestContext::set_network_quality_estimator(
233     NetworkQualityEstimator* network_quality_estimator) {
234   network_quality_estimator_ = network_quality_estimator;
235 }
set_client_socket_factory(std::unique_ptr<ClientSocketFactory> client_socket_factory)236 void URLRequestContext::set_client_socket_factory(
237     std::unique_ptr<ClientSocketFactory> client_socket_factory) {
238   client_socket_factory_ = std::move(client_socket_factory);
239 }
240 #if BUILDFLAG(ENABLE_REPORTING)
set_persistent_reporting_and_nel_store(std::unique_ptr<PersistentReportingAndNelStore> persistent_reporting_and_nel_store)241 void URLRequestContext::set_persistent_reporting_and_nel_store(
242     std::unique_ptr<PersistentReportingAndNelStore>
243         persistent_reporting_and_nel_store) {
244   persistent_reporting_and_nel_store_ =
245       std::move(persistent_reporting_and_nel_store);
246 }
set_reporting_service(std::unique_ptr<ReportingService> reporting_service)247 void URLRequestContext::set_reporting_service(
248     std::unique_ptr<ReportingService> reporting_service) {
249   reporting_service_ = std::move(reporting_service);
250 }
set_network_error_logging_service(std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service)251 void URLRequestContext::set_network_error_logging_service(
252     std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service) {
253   network_error_logging_service_ = std::move(network_error_logging_service);
254 }
255 #endif  // BUILDFLAG(ENABLE_REPORTING)
256 
set_transport_security_persister(std::unique_ptr<TransportSecurityPersister> transport_security_persister)257 void URLRequestContext::set_transport_security_persister(
258     std::unique_ptr<TransportSecurityPersister> transport_security_persister) {
259   transport_security_persister_ = std::move(transport_security_persister);
260 }
261 
262 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
set_device_bound_session_service(std::unique_ptr<device_bound_sessions::SessionService> device_bound_session_service)263 void URLRequestContext::set_device_bound_session_service(
264     std::unique_ptr<device_bound_sessions::SessionService>
265         device_bound_session_service) {
266   device_bound_session_service_ = std::move(device_bound_session_service);
267 }
set_device_bound_session_store(std::unique_ptr<device_bound_sessions::SessionStore> device_bound_session_store)268 void URLRequestContext::set_device_bound_session_store(
269     std::unique_ptr<device_bound_sessions::SessionStore>
270         device_bound_session_store) {
271   device_bound_session_store_ = std::move(device_bound_session_store);
272 }
273 #endif  // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
274 
275 }  // namespace net
276