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