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