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