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 222 // Returns current value of the |check_cleartext_permitted| flag. check_cleartext_permitted()223 bool check_cleartext_permitted() const { return check_cleartext_permitted_; } 224 require_network_isolation_key()225 bool require_network_isolation_key() const { 226 return require_network_isolation_key_; 227 } 228 229 // If != handles::kInvalidNetworkHandle, the network which this 230 // context has been bound to. bound_network()231 handles::NetworkHandle bound_network() const { return bound_network_; } 232 AssertCalledOnValidThread()233 void AssertCalledOnValidThread() { 234 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 235 } 236 237 // DEPRECATED: Do not use this even in tests. This is for a legacy use. SetJobFactoryForTesting(const URLRequestJobFactory * job_factory)238 void SetJobFactoryForTesting(const URLRequestJobFactory* job_factory) { 239 job_factory_ = job_factory; 240 } 241 242 private: 243 friend class URLRequestContextBuilder; 244 http_network_session()245 HttpNetworkSession* http_network_session() const { 246 return http_network_session_.get(); 247 } 248 249 void set_net_log(NetLog* net_log); 250 void set_host_resolver(std::unique_ptr<HostResolver> host_resolver); 251 void set_cert_verifier(std::unique_ptr<CertVerifier> cert_verifier); 252 void set_proxy_resolution_service( 253 std::unique_ptr<ProxyResolutionService> proxy_resolution_service); 254 void set_proxy_delegate(std::unique_ptr<ProxyDelegate> proxy_delegate); 255 void set_ssl_config_service(std::unique_ptr<SSLConfigService> service); 256 void set_http_auth_handler_factory( 257 std::unique_ptr<HttpAuthHandlerFactory> factory); 258 void set_http_network_session( 259 std::unique_ptr<HttpNetworkSession> http_network_session); 260 void set_http_transaction_factory( 261 std::unique_ptr<HttpTransactionFactory> factory); 262 void set_network_delegate(std::unique_ptr<NetworkDelegate> network_delegate); 263 void set_http_server_properties( 264 std::unique_ptr<HttpServerProperties> http_server_properties); 265 void set_cookie_store(std::unique_ptr<CookieStore> cookie_store); 266 void set_transport_security_state( 267 std::unique_ptr<TransportSecurityState> state); 268 void set_ct_policy_enforcer(std::unique_ptr<CTPolicyEnforcer> enforcer); 269 void set_sct_auditing_delegate(std::unique_ptr<SCTAuditingDelegate> delegate); 270 void set_job_factory(std::unique_ptr<const URLRequestJobFactory> job_factory); 271 void set_throttler_manager( 272 std::unique_ptr<URLRequestThrottlerManager> throttler_manager); 273 void set_quic_context(std::unique_ptr<QuicContext> quic_context); 274 void set_http_user_agent_settings( 275 std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings); 276 void set_network_quality_estimator( 277 NetworkQualityEstimator* network_quality_estimator); 278 void set_client_socket_factory( 279 std::unique_ptr<ClientSocketFactory> client_socket_factory); 280 #if BUILDFLAG(ENABLE_REPORTING) 281 void set_persistent_reporting_and_nel_store( 282 std::unique_ptr<PersistentReportingAndNelStore> 283 persistent_reporting_and_nel_store); 284 void set_reporting_service( 285 std::unique_ptr<ReportingService> reporting_service); 286 void set_network_error_logging_service( 287 std::unique_ptr<NetworkErrorLoggingService> 288 network_error_logging_service); 289 #endif // BUILDFLAG(ENABLE_REPORTING) set_enable_brotli(bool enable_brotli)290 void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; } set_check_cleartext_permitted(bool check_cleartext_permitted)291 void set_check_cleartext_permitted(bool check_cleartext_permitted) { 292 check_cleartext_permitted_ = check_cleartext_permitted; 293 } set_require_network_isolation_key(bool require_network_isolation_key)294 void set_require_network_isolation_key(bool require_network_isolation_key) { 295 require_network_isolation_key_ = require_network_isolation_key; 296 } set_bound_network(handles::NetworkHandle network)297 void set_bound_network(handles::NetworkHandle network) { 298 bound_network_ = network; 299 } 300 301 void set_transport_security_persister( 302 std::unique_ptr<TransportSecurityPersister> transport_security_persister); 303 304 raw_ptr<NetLog> net_log_ = nullptr; 305 306 std::unique_ptr<HostResolver> host_resolver_; 307 std::unique_ptr<CertVerifier> cert_verifier_; 308 std::unique_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_; 309 std::unique_ptr<ProxyDelegate> proxy_delegate_; 310 std::unique_ptr<NetworkDelegate> network_delegate_; 311 std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; 312 std::unique_ptr<SSLConfigService> ssl_config_service_; 313 std::unique_ptr<HttpServerProperties> http_server_properties_; 314 std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings_; 315 std::unique_ptr<CookieStore> cookie_store_; 316 std::unique_ptr<TransportSecurityState> transport_security_state_; 317 std::unique_ptr<CTPolicyEnforcer> ct_policy_enforcer_; 318 std::unique_ptr<SCTAuditingDelegate> sct_auditing_delegate_; 319 std::unique_ptr<QuicContext> quic_context_; 320 std::unique_ptr<ClientSocketFactory> client_socket_factory_; 321 322 // The storage duplication for URLRequestJobFactory is needed because of 323 // SetJobFactoryForTesting. Once this method is removable, we can only store a 324 // unique_ptr similarly to the other fields. 325 std::unique_ptr<const URLRequestJobFactory> job_factory_storage_; 326 raw_ptr<const URLRequestJobFactory> job_factory_ = nullptr; 327 328 std::unique_ptr<URLRequestThrottlerManager> throttler_manager_; 329 330 #if BUILDFLAG(ENABLE_REPORTING) 331 // Must precede |reporting_service_| and |network_error_logging_service_| 332 std::unique_ptr<PersistentReportingAndNelStore> 333 persistent_reporting_and_nel_store_; 334 335 std::unique_ptr<ReportingService> reporting_service_; 336 std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service_; 337 #endif // BUILDFLAG(ENABLE_REPORTING) 338 339 // May be used (but not owned) by the HttpTransactionFactory. 340 std::unique_ptr<HttpNetworkSession> http_network_session_; 341 342 // `http_transaction_factory_` might hold a raw pointer on 343 // `http_network_session_` so it needs to be declared last. 344 std::unique_ptr<HttpTransactionFactory> http_transaction_factory_; 345 346 raw_ptr<NetworkQualityEstimator> network_quality_estimator_ = nullptr; 347 348 std::unique_ptr<TransportSecurityPersister> transport_security_persister_; 349 350 std::unique_ptr<std::set<const URLRequest*>> url_requests_; 351 352 // Enables Brotli Content-Encoding support. 353 bool enable_brotli_ = false; 354 // Enables checking system policy before allowing a cleartext http or ws 355 // request. Only used on Android. 356 bool check_cleartext_permitted_ = false; 357 358 // Triggers a DCHECK if a NetworkAnonymizationKey/IsolationInfo is not 359 // provided to a request when true. 360 bool require_network_isolation_key_ = false; 361 362 handles::NetworkHandle bound_network_; 363 364 THREAD_CHECKER(thread_checker_); 365 }; 366 367 } // namespace net 368 369 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 370