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 // This class represents contextual information (cookies, cache, etc.) 6 // that's necessary when processing resource requests. 7 8 #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 9 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 10 11 #include <stdint.h> 12 13 #include <memory> 14 #include <optional> 15 #include <set> 16 #include <string> 17 18 #include "base/memory/raw_ptr.h" 19 #include "base/memory/weak_ptr.h" 20 #include "base/threading/thread_checker.h" 21 #include "base/types/pass_key.h" 22 #include "build/build_config.h" 23 #include "build/chromeos_buildflags.h" 24 #include "net/base/net_export.h" 25 #include "net/base/network_handle.h" 26 #include "net/base/request_priority.h" 27 #include "net/log/net_log_source.h" 28 #include "net/net_buildflags.h" 29 #include "net/traffic_annotation/network_traffic_annotation.h" 30 #include "net/url_request/url_request.h" 31 32 namespace net { 33 class CertVerifier; 34 class ClientSocketFactory; 35 class CookieStore; 36 class HostResolver; 37 class HttpAuthHandlerFactory; 38 class HttpNetworkSession; 39 struct HttpNetworkSessionContext; 40 struct HttpNetworkSessionParams; 41 class HttpServerProperties; 42 class HttpTransactionFactory; 43 class HttpUserAgentSettings; 44 class NetLog; 45 class NetworkDelegate; 46 class NetworkQualityEstimator; 47 class ProxyDelegate; 48 class ProxyResolutionService; 49 class QuicContext; 50 class SCTAuditingDelegate; 51 class SSLConfigService; 52 class TransportSecurityPersister; 53 class TransportSecurityState; 54 class URLRequest; 55 class URLRequestJobFactory; 56 class URLRequestContextBuilder; 57 58 #if BUILDFLAG(ENABLE_REPORTING) 59 class NetworkErrorLoggingService; 60 class PersistentReportingAndNelStore; 61 class ReportingService; 62 #endif // BUILDFLAG(ENABLE_REPORTING) 63 64 namespace device_bound_sessions { 65 class SessionService; 66 class SessionStore; 67 } 68 69 // Class that provides application-specific context for URLRequest 70 // instances. May only be created by URLRequestContextBuilder. 71 // Owns most of its member variables, except a few that may be shared 72 // with other contexts. 73 class NET_EXPORT URLRequestContext final { 74 public: 75 // URLRequestContext must be created by URLRequestContextBuilder. 76 explicit URLRequestContext(base::PassKey<URLRequestContextBuilder> pass_key); 77 URLRequestContext(const URLRequestContext&) = delete; 78 URLRequestContext& operator=(const URLRequestContext&) = delete; 79 80 ~URLRequestContext(); 81 82 // May return nullptr if this context doesn't have an associated network 83 // session. 84 const HttpNetworkSessionParams* GetNetworkSessionParams() const; 85 86 // May return nullptr if this context doesn't have an associated network 87 // session. 88 const HttpNetworkSessionContext* GetNetworkSessionContext() const; 89 90 // TODO(crbug.com/40118868): Revisit once build flag switch of lacros-chrome is 91 // complete. 92 #if !BUILDFLAG(IS_WIN) && \ 93 !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)) 94 // This function should not be used in Chromium, please use the version with 95 // NetworkTrafficAnnotationTag in the future. 96 // 97 // The unannotated method is not available on desktop Linux + Windows. It's 98 // available on other platforms, since we only audit network annotations on 99 // Linux & Windows. 100 std::unique_ptr<URLRequest> CreateRequest( 101 const GURL& url, 102 RequestPriority priority, 103 URLRequest::Delegate* delegate) const; 104 #endif 105 106 // `traffic_annotation` is metadata about the network traffic send via this 107 // URLRequest, see net::DefineNetworkTrafficAnnotation. Note that: 108 // - net provides the API for tagging requests with an opaque identifier. 109 // - chrome/browser/privacy/traffic_annotation.proto contains the Chrome 110 // specific .proto describing the verbose annotation format that Chrome's 111 // callsites are expected to follow. 112 // - tools/traffic_annotation/ contains sample and template for annotation and 113 // tools will be added for verification following crbug.com/690323. 114 // 115 // `is_for_websockets` should be true iff this was created for use by a 116 // websocket. HTTP/HTTPS requests fail if it's true, and WS/WSS requests fail 117 // if it's false. This is to protect against broken consumers. 118 // 119 // `net_log_source_id` is used to construct NetLogWithSource using the 120 // specified Source ID. This method is expected to be used when URLRequest 121 // wants to take over existing NetLogSource. 122 std::unique_ptr<URLRequest> CreateRequest( 123 const GURL& url, 124 RequestPriority priority, 125 URLRequest::Delegate* delegate, 126 NetworkTrafficAnnotationTag traffic_annotation, 127 bool is_for_websockets = false, 128 const std::optional<net::NetLogSource> net_log_source = 129 std::nullopt) const; 130 net_log()131 NetLog* net_log() const { return net_log_; } 132 host_resolver()133 HostResolver* host_resolver() const { return host_resolver_.get(); } 134 cert_verifier()135 CertVerifier* cert_verifier() const { return cert_verifier_.get(); } 136 137 // Get the proxy service for this context. proxy_resolution_service()138 ProxyResolutionService* proxy_resolution_service() const { 139 return proxy_resolution_service_.get(); 140 } 141 proxy_delegate()142 ProxyDelegate* proxy_delegate() const { return proxy_delegate_.get(); } 143 144 // Get the ssl config service for this context. ssl_config_service()145 SSLConfigService* ssl_config_service() const { 146 return ssl_config_service_.get(); 147 } 148 149 // Gets the HTTP Authentication Handler Factory for this context. 150 // The factory is only valid for the lifetime of this URLRequestContext http_auth_handler_factory()151 HttpAuthHandlerFactory* http_auth_handler_factory() const { 152 return http_auth_handler_factory_.get(); 153 } 154 155 // Gets the http transaction factory for this context. http_transaction_factory()156 HttpTransactionFactory* http_transaction_factory() const { 157 return http_transaction_factory_.get(); 158 } 159 network_delegate()160 NetworkDelegate* network_delegate() const { return network_delegate_.get(); } 161 http_server_properties()162 HttpServerProperties* http_server_properties() const { 163 return http_server_properties_.get(); 164 } 165 166 // Gets the cookie store for this context (may be null, in which case 167 // cookies are not stored). cookie_store()168 CookieStore* cookie_store() const { return cookie_store_.get(); } 169 transport_security_state()170 TransportSecurityState* transport_security_state() const { 171 return transport_security_state_.get(); 172 } 173 sct_auditing_delegate()174 SCTAuditingDelegate* sct_auditing_delegate() const { 175 return sct_auditing_delegate_.get(); 176 } 177 job_factory()178 const URLRequestJobFactory* job_factory() const { return job_factory_; } 179 quic_context()180 QuicContext* quic_context() const { return quic_context_.get(); } 181 182 // Gets the URLRequest objects that hold a reference to this 183 // URLRequestContext. url_requests()184 std::set<raw_ptr<const URLRequest, SetExperimental>>* url_requests() const { 185 return url_requests_.get(); 186 } 187 188 // CHECKs that no URLRequests using this context remain. Subclasses should 189 // additionally call AssertNoURLRequests() within their own destructor, 190 // prior to implicit destruction of subclass-owned state. 191 void AssertNoURLRequests() const; 192 193 // Get the underlying |HttpUserAgentSettings| implementation that provides 194 // the HTTP Accept-Language and User-Agent header values. http_user_agent_settings()195 const HttpUserAgentSettings* http_user_agent_settings() const { 196 return http_user_agent_settings_.get(); 197 } 198 199 // Gets the NetworkQualityEstimator associated with this context. 200 // May return nullptr. network_quality_estimator()201 NetworkQualityEstimator* network_quality_estimator() const { 202 return network_quality_estimator_.get(); 203 } 204 205 #if BUILDFLAG(ENABLE_REPORTING) reporting_service()206 ReportingService* reporting_service() const { 207 return reporting_service_.get(); 208 } 209 network_error_logging_service()210 NetworkErrorLoggingService* network_error_logging_service() const { 211 return network_error_logging_service_.get(); 212 } 213 #endif // BUILDFLAG(ENABLE_REPORTING) 214 215 // May return nullptr if the feature is disabled. device_bound_session_store()216 device_bound_sessions::SessionStore* device_bound_session_store() const { 217 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS) 218 return device_bound_session_store_.get(); 219 #else 220 return nullptr; 221 #endif 222 } 223 // May return nullptr if the feature is disabled. device_bound_session_service()224 device_bound_sessions::SessionService* device_bound_session_service() const { 225 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS) 226 return device_bound_session_service_.get(); 227 #else 228 return nullptr; 229 #endif 230 } 231 enable_brotli()232 bool enable_brotli() const { return enable_brotli_; } 233 enable_zstd()234 bool enable_zstd() const { return enable_zstd_; } 235 236 // Returns current value of the |check_cleartext_permitted| flag. check_cleartext_permitted()237 bool check_cleartext_permitted() const { return check_cleartext_permitted_; } 238 require_network_anonymization_key()239 bool require_network_anonymization_key() const { 240 return require_network_anonymization_key_; 241 } 242 243 // If != handles::kInvalidNetworkHandle, the network which this 244 // context has been bound to. bound_network()245 handles::NetworkHandle bound_network() const { return bound_network_; } 246 AssertCalledOnValidThread()247 void AssertCalledOnValidThread() { 248 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 249 } 250 251 // DEPRECATED: Do not use this even in tests. This is for a legacy use. SetJobFactoryForTesting(const URLRequestJobFactory * job_factory)252 void SetJobFactoryForTesting(const URLRequestJobFactory* job_factory) { 253 job_factory_ = job_factory; 254 } 255 cookie_deprecation_label()256 const std::optional<std::string>& cookie_deprecation_label() const { 257 return cookie_deprecation_label_; 258 } 259 set_cookie_deprecation_label(const std::optional<std::string> & label)260 void set_cookie_deprecation_label(const std::optional<std::string>& label) { 261 cookie_deprecation_label_ = label; 262 } 263 264 private: 265 friend class URLRequestContextBuilder; 266 http_network_session()267 HttpNetworkSession* http_network_session() const { 268 return http_network_session_.get(); 269 } 270 271 void set_net_log(NetLog* net_log); 272 void set_host_resolver(std::unique_ptr<HostResolver> host_resolver); 273 void set_cert_verifier(std::unique_ptr<CertVerifier> cert_verifier); 274 void set_proxy_resolution_service( 275 std::unique_ptr<ProxyResolutionService> proxy_resolution_service); 276 void set_proxy_delegate(std::unique_ptr<ProxyDelegate> proxy_delegate); 277 void set_ssl_config_service(std::unique_ptr<SSLConfigService> service); 278 void set_http_auth_handler_factory( 279 std::unique_ptr<HttpAuthHandlerFactory> factory); 280 void set_http_network_session( 281 std::unique_ptr<HttpNetworkSession> http_network_session); 282 void set_http_transaction_factory( 283 std::unique_ptr<HttpTransactionFactory> factory); 284 void set_network_delegate(std::unique_ptr<NetworkDelegate> network_delegate); 285 void set_http_server_properties( 286 std::unique_ptr<HttpServerProperties> http_server_properties); 287 void set_cookie_store(std::unique_ptr<CookieStore> cookie_store); 288 void set_transport_security_state( 289 std::unique_ptr<TransportSecurityState> state); 290 void set_sct_auditing_delegate(std::unique_ptr<SCTAuditingDelegate> delegate); 291 void set_job_factory(std::unique_ptr<const URLRequestJobFactory> job_factory); 292 void set_quic_context(std::unique_ptr<QuicContext> quic_context); 293 void set_http_user_agent_settings( 294 std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings); 295 void set_network_quality_estimator( 296 NetworkQualityEstimator* network_quality_estimator); 297 void set_client_socket_factory( 298 std::unique_ptr<ClientSocketFactory> client_socket_factory); 299 #if BUILDFLAG(ENABLE_REPORTING) 300 void set_persistent_reporting_and_nel_store( 301 std::unique_ptr<PersistentReportingAndNelStore> 302 persistent_reporting_and_nel_store); 303 void set_reporting_service( 304 std::unique_ptr<ReportingService> reporting_service); 305 void set_network_error_logging_service( 306 std::unique_ptr<NetworkErrorLoggingService> 307 network_error_logging_service); 308 #endif // BUILDFLAG(ENABLE_REPORTING) set_enable_brotli(bool enable_brotli)309 void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; } set_enable_zstd(bool enable_zstd)310 void set_enable_zstd(bool enable_zstd) { enable_zstd_ = enable_zstd; } set_check_cleartext_permitted(bool check_cleartext_permitted)311 void set_check_cleartext_permitted(bool check_cleartext_permitted) { 312 check_cleartext_permitted_ = check_cleartext_permitted; 313 } set_require_network_anonymization_key(bool require_network_anonymization_key)314 void set_require_network_anonymization_key( 315 bool require_network_anonymization_key) { 316 require_network_anonymization_key_ = require_network_anonymization_key; 317 } set_bound_network(handles::NetworkHandle network)318 void set_bound_network(handles::NetworkHandle network) { 319 bound_network_ = network; 320 } 321 322 void set_transport_security_persister( 323 std::unique_ptr<TransportSecurityPersister> transport_security_persister); 324 325 raw_ptr<NetLog> net_log_ = nullptr; 326 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS) 327 void set_device_bound_session_store( 328 std::unique_ptr<device_bound_sessions::SessionStore> 329 device_bound_session_store); 330 void set_device_bound_session_service( 331 std::unique_ptr<device_bound_sessions::SessionService> 332 device_bound_session_service); 333 #endif // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS) 334 335 std::unique_ptr<HostResolver> host_resolver_; 336 std::unique_ptr<CertVerifier> cert_verifier_; 337 std::unique_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_; 338 std::unique_ptr<NetworkDelegate> network_delegate_; 339 // `proxy_resolution_service_` may store a pointer to `proxy_delegate_`, so 340 // ensure that the latter outlives the former. 341 std::unique_ptr<ProxyDelegate> proxy_delegate_; 342 std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; 343 std::unique_ptr<SSLConfigService> ssl_config_service_; 344 std::unique_ptr<HttpServerProperties> http_server_properties_; 345 std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings_; 346 std::unique_ptr<CookieStore> cookie_store_; 347 std::unique_ptr<TransportSecurityState> transport_security_state_; 348 std::unique_ptr<SCTAuditingDelegate> sct_auditing_delegate_; 349 std::unique_ptr<QuicContext> quic_context_; 350 std::unique_ptr<ClientSocketFactory> client_socket_factory_; 351 352 // The storage duplication for URLRequestJobFactory is needed because of 353 // SetJobFactoryForTesting. Once this method is removable, we can only store a 354 // unique_ptr similarly to the other fields. 355 std::unique_ptr<const URLRequestJobFactory> job_factory_storage_; 356 raw_ptr<const URLRequestJobFactory> job_factory_ = nullptr; 357 358 #if BUILDFLAG(ENABLE_REPORTING) 359 // Must precede |reporting_service_| and |network_error_logging_service_| 360 std::unique_ptr<PersistentReportingAndNelStore> 361 persistent_reporting_and_nel_store_; 362 363 std::unique_ptr<ReportingService> reporting_service_; 364 std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service_; 365 #endif // BUILDFLAG(ENABLE_REPORTING) 366 367 // May be used (but not owned) by the HttpTransactionFactory. 368 std::unique_ptr<HttpNetworkSession> http_network_session_; 369 370 // `http_transaction_factory_` might hold a raw pointer on 371 // `http_network_session_` so it needs to be declared last. 372 std::unique_ptr<HttpTransactionFactory> http_transaction_factory_; 373 374 raw_ptr<NetworkQualityEstimator> network_quality_estimator_ = nullptr; 375 376 std::unique_ptr<TransportSecurityPersister> transport_security_persister_; 377 378 std::unique_ptr<std::set<raw_ptr<const URLRequest, SetExperimental>>> 379 url_requests_; 380 381 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS) 382 std::unique_ptr<device_bound_sessions::SessionStore> 383 device_bound_session_store_; 384 std::unique_ptr<device_bound_sessions::SessionService> 385 device_bound_session_service_; 386 #endif // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS) 387 388 // Enables Brotli Content-Encoding support. 389 bool enable_brotli_ = false; 390 // Enables Zstd Content-Encoding support. 391 bool enable_zstd_ = false; 392 // Enables checking system policy before allowing a cleartext http or ws 393 // request. Only used on Android. 394 bool check_cleartext_permitted_ = false; 395 396 // Triggers a DCHECK if a NetworkAnonymizationKey/IsolationInfo is not 397 // provided to a request when true. 398 bool require_network_anonymization_key_ = false; 399 400 std::optional<std::string> cookie_deprecation_label_; 401 402 handles::NetworkHandle bound_network_; 403 404 THREAD_CHECKER(thread_checker_); 405 }; 406 407 } // namespace net 408 409 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 410