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 is useful for building a simple URLRequestContext. Most creators 6 // of new URLRequestContexts should use this helper class to construct it. Call 7 // any configuration params, and when done, invoke Build() to construct the 8 // URLRequestContext. This URLRequestContext will own all its own storage. 9 // 10 // URLRequestContextBuilder and its associated params classes are initially 11 // populated with "sane" default values. Read through the comments to figure out 12 // what these are. 13 14 #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ 15 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ 16 17 #include <stdint.h> 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "base/files/file_path.h" 26 #include "base/functional/callback.h" 27 #include "base/memory/raw_ptr.h" 28 #include "base/memory/scoped_refptr.h" 29 #include "base/task/task_traits.h" 30 #include "build/build_config.h" 31 #include "build/buildflag.h" 32 #include "net/base/net_export.h" 33 #include "net/base/network_delegate.h" 34 #include "net/base/network_handle.h" 35 #include "net/base/proxy_delegate.h" 36 #include "net/disk_cache/disk_cache.h" 37 #include "net/dns/host_resolver.h" 38 #include "net/http/http_network_session.h" 39 #include "net/net_buildflags.h" 40 #include "net/network_error_logging/network_error_logging_service.h" 41 #include "net/proxy_resolution/proxy_config_service.h" 42 #include "net/proxy_resolution/proxy_resolution_service.h" 43 #include "net/socket/client_socket_factory.h" 44 #include "net/ssl/ssl_config_service.h" 45 #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h" 46 #include "net/url_request/url_request_job_factory.h" 47 #include "third_party/abseil-cpp/absl/types/optional.h" 48 49 namespace net { 50 51 class CertVerifier; 52 class ClientSocketFactory; 53 class CookieStore; 54 class CTPolicyEnforcer; 55 class HttpAuthHandlerFactory; 56 class HttpTransactionFactory; 57 class HttpUserAgentSettings; 58 class HttpServerProperties; 59 class HostResolverManager; 60 class NetworkQualityEstimator; 61 class ProxyConfigService; 62 class URLRequestContext; 63 64 #if BUILDFLAG(ENABLE_REPORTING) 65 struct ReportingPolicy; 66 class PersistentReportingAndNelStore; 67 #endif // BUILDFLAG(ENABLE_REPORTING) 68 69 // A URLRequestContextBuilder creates a single URLRequestContext. It provides 70 // methods to manage various URLRequestContext components which should be called 71 // before creating the Context. Once configuration is complete, calling Build() 72 // will create a URLRequestContext with the specified configuration. Components 73 // that are not explicitly configured will use reasonable in-memory defaults. 74 // 75 // The returned URLRequestContext is self-contained: Deleting it will safely 76 // shut down all of the URLRequests owned by its internal components, and then 77 // tear down those components. The only exception to this are objects not owned 78 // by URLRequestContext. This includes components passed in to the methods that 79 // take raw pointers, and objects that components passed in to the Builder have 80 // raw pointers to. 81 // 82 // A Builder should be destroyed after calling Build, and there is no need to 83 // keep it around for the lifetime of the created URLRequestContext. Each 84 // Builder may be used to create only a single URLRequestContext. 85 class NET_EXPORT URLRequestContextBuilder { 86 public: 87 // Creates an HttpNetworkTransactionFactory given an HttpNetworkSession. Does 88 // not take ownership of the session. 89 using CreateHttpTransactionFactoryCallback = 90 base::OnceCallback<std::unique_ptr<HttpTransactionFactory>( 91 HttpNetworkSession* session)>; 92 93 struct NET_EXPORT HttpCacheParams { 94 enum Type { 95 // In-memory cache. 96 IN_MEMORY, 97 // Disk cache using "default" backend. 98 DISK, 99 // Disk cache using "blockfile" backend (BackendImpl). 100 DISK_BLOCKFILE, 101 // Disk cache using "simple" backend (SimpleBackendImpl). 102 DISK_SIMPLE, 103 }; 104 105 HttpCacheParams(); 106 ~HttpCacheParams(); 107 108 // The type of HTTP cache. Default is IN_MEMORY. 109 Type type = IN_MEMORY; 110 111 // The max size of the cache in bytes. Default is algorithmically determined 112 // based off available disk space. 113 int max_size = 0; 114 115 // Whether or not we need to reset the cache due to an experiment change. 116 bool reset_cache = false; 117 118 // The cache path (when type is DISK). 119 base::FilePath path; 120 121 // A factory to broker file operations. This is needed for network process 122 // sandboxing in some platforms. 123 scoped_refptr<disk_cache::BackendFileOperationsFactory> 124 file_operations_factory; 125 126 #if BUILDFLAG(IS_ANDROID) 127 // If this is set, will override the default ApplicationStatusListener. This 128 // is useful if the cache will not be in the main process. 129 disk_cache::ApplicationStatusListenerGetter app_status_listener_getter; 130 #endif 131 }; 132 133 URLRequestContextBuilder(); 134 135 URLRequestContextBuilder(const URLRequestContextBuilder&) = delete; 136 URLRequestContextBuilder& operator=(const URLRequestContextBuilder&) = delete; 137 138 virtual ~URLRequestContextBuilder(); 139 140 // Sets whether Brotli compression is enabled. Disabled by default; set_enable_brotli(bool enable_brotli)141 void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; } 142 143 // Sets whether Zstd compression is enabled. Disabled by default. set_enable_zstd(bool enable_zstd)144 void set_enable_zstd(bool enable_zstd) { enable_zstd_ = enable_zstd; } 145 146 // Sets the |check_cleartext_permitted| flag, which controls whether to check 147 // system policy before allowing a cleartext http or ws request. set_check_cleartext_permitted(bool value)148 void set_check_cleartext_permitted(bool value) { 149 check_cleartext_permitted_ = value; 150 } 151 set_require_network_anonymization_key(bool value)152 void set_require_network_anonymization_key(bool value) { 153 require_network_anonymization_key_ = value; 154 } 155 156 // Unlike most other setters, the builder does not take ownership of the 157 // NetworkQualityEstimator. set_network_quality_estimator(NetworkQualityEstimator * network_quality_estimator)158 void set_network_quality_estimator( 159 NetworkQualityEstimator* network_quality_estimator) { 160 network_quality_estimator_ = network_quality_estimator; 161 } 162 163 // These functions are mutually exclusive. The ProxyConfigService, if 164 // set, will be used to construct a ConfiguredProxyResolutionService. set_proxy_config_service(std::unique_ptr<ProxyConfigService> proxy_config_service)165 void set_proxy_config_service( 166 std::unique_ptr<ProxyConfigService> proxy_config_service) { 167 proxy_config_service_ = std::move(proxy_config_service); 168 } 169 170 // Sets whether quick PAC checks are enabled. Defaults to true. Ignored if 171 // a ConfiguredProxyResolutionService is set directly. set_pac_quick_check_enabled(bool pac_quick_check_enabled)172 void set_pac_quick_check_enabled(bool pac_quick_check_enabled) { 173 pac_quick_check_enabled_ = pac_quick_check_enabled; 174 } 175 176 // Sets the proxy service. If one is not provided, by default, uses system 177 // libraries to evaluate PAC scripts, if available (And if not, skips PAC 178 // resolution). Subclasses may override CreateProxyResolutionService for 179 // different default behavior. set_proxy_resolution_service(std::unique_ptr<ProxyResolutionService> proxy_resolution_service)180 void set_proxy_resolution_service( 181 std::unique_ptr<ProxyResolutionService> proxy_resolution_service) { 182 proxy_resolution_service_ = std::move(proxy_resolution_service); 183 } 184 set_ssl_config_service(std::unique_ptr<SSLConfigService> ssl_config_service)185 void set_ssl_config_service( 186 std::unique_ptr<SSLConfigService> ssl_config_service) { 187 ssl_config_service_ = std::move(ssl_config_service); 188 } 189 190 // Call these functions to specify hard-coded Accept-Language 191 // or User-Agent header values for all requests that don't 192 // have the headers already set. 193 void set_accept_language(const std::string& accept_language); 194 void set_user_agent(const std::string& user_agent); 195 196 // Makes the created URLRequestContext use a particular HttpUserAgentSettings 197 // object. Not compatible with set_accept_language() / set_user_agent(). 198 // 199 // The object will be live until the URLRequestContext is destroyed. 200 void set_http_user_agent_settings( 201 std::unique_ptr<HttpUserAgentSettings> http_user_agent_settings); 202 203 // Sets a valid ProtocolHandler for a scheme. 204 // A ProtocolHandler already exists for |scheme| will be overwritten. 205 void SetProtocolHandler( 206 const std::string& scheme, 207 std::unique_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler); 208 209 // Unlike the other setters, the builder does not take ownership of the 210 // NetLog. 211 // TODO(mmenke): Probably makes sense to get rid of this, and have consumers 212 // set their own NetLog::Observers instead. set_net_log(NetLog * net_log)213 void set_net_log(NetLog* net_log) { net_log_ = net_log; } 214 215 // Sets a HostResolver instance to be used instead of default construction. 216 // Should not be used if set_host_resolver_manager(), 217 // set_host_mapping_rules(), or set_host_resolver_factory() are used. On 218 // building the context, will call HostResolver::SetRequestContext, so 219 // |host_resolver| may not already be associated with a context. 220 void set_host_resolver(std::unique_ptr<HostResolver> host_resolver); 221 222 // If set to non-empty, the mapping rules will be applied to requests to the 223 // created host resolver. See MappedHostResolver for details. Should not be 224 // used if set_host_resolver() is used. 225 void set_host_mapping_rules(std::string host_mapping_rules); 226 227 // Sets a shared HostResolverManager to be used for created HostResolvers. 228 // Should not be used if set_host_resolver() is used. The consumer must ensure 229 // |manager| outlives the URLRequestContext returned by the builder. 230 void set_host_resolver_manager(HostResolverManager* manager); 231 232 // Sets the factory used for any HostResolverCreation. By default, a default 233 // implementation will be used. Should not be used if set_host_resolver() is 234 // used. 235 void set_host_resolver_factory(HostResolver::Factory* factory); 236 237 // Uses NetworkDelegateImpl by default. Note that calling Build will unset 238 // any custom delegate in builder, so this must be called each time before 239 // Build is called. 240 // Returns a raw pointer to the set delegate. 241 template <typename T> set_network_delegate(std::unique_ptr<T> delegate)242 T* set_network_delegate(std::unique_ptr<T> delegate) { 243 network_delegate_ = std::move(delegate); 244 return static_cast<T*>(network_delegate_.get()); 245 } 246 247 // Sets the ProxyDelegate. 248 void set_proxy_delegate(std::unique_ptr<ProxyDelegate> proxy_delegate); 249 250 // Sets a specific HttpAuthHandlerFactory to be used by the URLRequestContext 251 // rather than the default |HttpAuthHandlerRegistryFactory|. The builder 252 // takes ownership of the factory and will eventually transfer it to the new 253 // URLRequestContext. 254 void SetHttpAuthHandlerFactory( 255 std::unique_ptr<HttpAuthHandlerFactory> factory); 256 257 // By default HttpCache is enabled with a default constructed HttpCacheParams. 258 void EnableHttpCache(const HttpCacheParams& params); 259 void DisableHttpCache(); 260 261 // Override default HttpNetworkSessionParams settings. set_http_network_session_params(const HttpNetworkSessionParams & http_network_session_params)262 void set_http_network_session_params( 263 const HttpNetworkSessionParams& http_network_session_params) { 264 http_network_session_params_ = http_network_session_params; 265 } 266 set_transport_security_persister_file_path(const base::FilePath & transport_security_persister_file_path)267 void set_transport_security_persister_file_path( 268 const base::FilePath& transport_security_persister_file_path) { 269 transport_security_persister_file_path_ = 270 transport_security_persister_file_path; 271 } 272 set_hsts_policy_bypass_list(const std::vector<std::string> & hsts_policy_bypass_list)273 void set_hsts_policy_bypass_list( 274 const std::vector<std::string>& hsts_policy_bypass_list) { 275 hsts_policy_bypass_list_ = hsts_policy_bypass_list; 276 } 277 278 void SetSpdyAndQuicEnabled(bool spdy_enabled, bool quic_enabled); 279 set_throttling_enabled(bool throttling_enabled)280 void set_throttling_enabled(bool throttling_enabled) { 281 throttling_enabled_ = throttling_enabled; 282 } 283 284 void set_ct_policy_enforcer( 285 std::unique_ptr<CTPolicyEnforcer> ct_policy_enforcer); 286 void set_sct_auditing_delegate( 287 std::unique_ptr<SCTAuditingDelegate> sct_auditing_delegate); 288 void set_quic_context(std::unique_ptr<QuicContext> quic_context); 289 290 void SetCertVerifier(std::unique_ptr<CertVerifier> cert_verifier); 291 292 #if BUILDFLAG(ENABLE_REPORTING) 293 void set_reporting_service( 294 std::unique_ptr<ReportingService> reporting_service); 295 void set_reporting_policy(std::unique_ptr<ReportingPolicy> reporting_policy); 296 set_network_error_logging_enabled(bool network_error_logging_enabled)297 void set_network_error_logging_enabled(bool network_error_logging_enabled) { 298 network_error_logging_enabled_ = network_error_logging_enabled; 299 } 300 301 template <typename T> SetNetworkErrorLoggingServiceForTesting(std::unique_ptr<T> service)302 T* SetNetworkErrorLoggingServiceForTesting(std::unique_ptr<T> service) { 303 network_error_logging_service_ = std::move(service); 304 return static_cast<T*>(network_error_logging_service_.get()); 305 } 306 307 void set_persistent_reporting_and_nel_store( 308 std::unique_ptr<PersistentReportingAndNelStore> 309 persistent_reporting_and_nel_store); 310 #endif // BUILDFLAG(ENABLE_REPORTING) 311 312 // Override the default in-memory cookie store. If |cookie_store| is NULL, 313 // CookieStore will be disabled for this context. 314 void SetCookieStore(std::unique_ptr<CookieStore> cookie_store); 315 316 // Sets a specific HttpServerProperties for use in the 317 // URLRequestContext rather than creating a default HttpServerPropertiesImpl. 318 void SetHttpServerProperties( 319 std::unique_ptr<HttpServerProperties> http_server_properties); 320 321 // Sets a callback that will be used to create the 322 // HttpNetworkTransactionFactory. If a cache is enabled, the cache's 323 // HttpTransactionFactory will wrap the one this creates. 324 // TODO(mmenke): Get rid of this. See https://crbug.com/721408 325 void SetCreateHttpTransactionFactoryCallback( 326 CreateHttpTransactionFactoryCallback 327 create_http_network_transaction_factory); 328 329 template <typename T> SetHttpTransactionFactoryForTesting(std::unique_ptr<T> factory)330 T* SetHttpTransactionFactoryForTesting(std::unique_ptr<T> factory) { 331 create_http_network_transaction_factory_.Reset(); 332 http_transaction_factory_ = std::move(factory); 333 return static_cast<T*>(http_transaction_factory_.get()); 334 } 335 336 // Sets a ClientSocketFactory so a test can mock out sockets. This must 337 // outlive the URLRequestContext that will be built. set_client_socket_factory_for_testing(ClientSocketFactory * client_socket_factory_for_testing)338 void set_client_socket_factory_for_testing( 339 ClientSocketFactory* client_socket_factory_for_testing) { 340 set_client_socket_factory(client_socket_factory_for_testing); 341 } 342 343 // Sets a ClientSocketFactory when the network service sandbox is enabled. The 344 // unique_ptr is moved to a URLRequestContext once Build() is called. set_client_socket_factory(std::unique_ptr<ClientSocketFactory> client_socket_factory)345 void set_client_socket_factory( 346 std::unique_ptr<ClientSocketFactory> client_socket_factory) { 347 set_client_socket_factory(client_socket_factory.get()); 348 client_socket_factory_ = std::move(client_socket_factory); 349 } 350 set_cookie_deprecation_label(const std::string & label)351 void set_cookie_deprecation_label(const std::string& label) { 352 cookie_deprecation_label_ = label; 353 } 354 355 // Binds the context to `network`. All requests scheduled through the context 356 // built by this builder will be sent using `network`. Requests will fail if 357 // `network` disconnects. `options` allows to specify the ManagerOptions that 358 // will be passed to the special purpose HostResolver created internally. 359 // This also imposes some limitations on the context capabilities: 360 // * By design, QUIC connection migration will be turned off. 361 // Only implemented for Android (API level > 23). 362 void BindToNetwork( 363 handles::NetworkHandle network, 364 absl::optional<HostResolver::ManagerOptions> options = absl::nullopt); 365 366 // Creates a mostly self-contained URLRequestContext. May only be called once 367 // per URLRequestContextBuilder. After this is called, the Builder can be 368 // safely destroyed. 369 std::unique_ptr<URLRequestContext> Build(); 370 SuppressSettingSocketPerformanceWatcherFactoryForTesting()371 void SuppressSettingSocketPerformanceWatcherFactoryForTesting() { 372 suppress_setting_socket_performance_watcher_factory_for_testing_ = true; 373 } 374 375 protected: 376 // Lets subclasses override ProxyResolutionService creation, using a 377 // ProxyResolutionService that uses the URLRequestContext itself to get PAC 378 // scripts. When this method is invoked, the URLRequestContext is not yet 379 // ready to service requests. 380 virtual std::unique_ptr<ProxyResolutionService> CreateProxyResolutionService( 381 std::unique_ptr<ProxyConfigService> proxy_config_service, 382 URLRequestContext* url_request_context, 383 HostResolver* host_resolver, 384 NetworkDelegate* network_delegate, 385 NetLog* net_log, 386 bool pac_quick_check_enabled); 387 388 private: 389 // Extracts the component pointers required to construct an HttpNetworkSession 390 // and copies them into the HttpNetworkSession::Context used to create the 391 // session. This function should be used to ensure that a context and its 392 // associated HttpNetworkSession are consistent. 393 static void SetHttpNetworkSessionComponents( 394 const URLRequestContext* request_context, 395 HttpNetworkSessionContext* session_context, 396 bool suppress_setting_socket_performance_watcher_factory = false, 397 ClientSocketFactory* client_socket_factory = nullptr); 398 399 // Factory that will be used to create all client sockets used by the 400 // URLRequestContext that will be built. 401 // `client_socket_factory` must outlive the context. set_client_socket_factory(ClientSocketFactory * client_socket_factory)402 void set_client_socket_factory(ClientSocketFactory* client_socket_factory) { 403 client_socket_factory_raw_ = client_socket_factory; 404 } 405 406 bool enable_brotli_ = false; 407 bool enable_zstd_ = false; 408 bool check_cleartext_permitted_ = false; 409 bool require_network_anonymization_key_ = false; 410 raw_ptr<NetworkQualityEstimator> network_quality_estimator_ = nullptr; 411 412 std::string accept_language_; 413 std::string user_agent_; 414 std::unique_ptr<HttpUserAgentSettings> http_user_agent_settings_; 415 416 absl::optional<std::string> cookie_deprecation_label_; 417 418 bool http_cache_enabled_ = true; 419 bool throttling_enabled_ = false; 420 bool cookie_store_set_by_client_ = false; 421 bool suppress_setting_socket_performance_watcher_factory_for_testing_ = false; 422 423 handles::NetworkHandle bound_network_ = handles::kInvalidNetworkHandle; 424 // Used only if the context is bound to a network to customize the 425 // HostResolver created internally. 426 HostResolver::ManagerOptions manager_options_; 427 428 HttpCacheParams http_cache_params_; 429 HttpNetworkSessionParams http_network_session_params_; 430 CreateHttpTransactionFactoryCallback create_http_network_transaction_factory_; 431 std::unique_ptr<HttpTransactionFactory> http_transaction_factory_; 432 base::FilePath transport_security_persister_file_path_; 433 std::vector<std::string> hsts_policy_bypass_list_; 434 raw_ptr<NetLog> net_log_ = nullptr; 435 std::unique_ptr<HostResolver> host_resolver_; 436 std::string host_mapping_rules_; 437 raw_ptr<HostResolverManager, DanglingUntriaged> host_resolver_manager_ = 438 nullptr; 439 raw_ptr<HostResolver::Factory> host_resolver_factory_ = nullptr; 440 std::unique_ptr<ProxyConfigService> proxy_config_service_; 441 bool pac_quick_check_enabled_ = true; 442 std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; 443 std::unique_ptr<SSLConfigService> ssl_config_service_; 444 std::unique_ptr<NetworkDelegate> network_delegate_; 445 std::unique_ptr<ProxyDelegate> proxy_delegate_; 446 std::unique_ptr<CookieStore> cookie_store_; 447 std::unique_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_; 448 std::unique_ptr<CertVerifier> cert_verifier_; 449 std::unique_ptr<CTPolicyEnforcer> ct_policy_enforcer_; 450 std::unique_ptr<SCTAuditingDelegate> sct_auditing_delegate_; 451 std::unique_ptr<QuicContext> quic_context_; 452 std::unique_ptr<ClientSocketFactory> client_socket_factory_ = nullptr; 453 #if BUILDFLAG(ENABLE_REPORTING) 454 std::unique_ptr<ReportingService> reporting_service_; 455 std::unique_ptr<ReportingPolicy> reporting_policy_; 456 bool network_error_logging_enabled_ = false; 457 std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service_; 458 std::unique_ptr<PersistentReportingAndNelStore> 459 persistent_reporting_and_nel_store_; 460 #endif // BUILDFLAG(ENABLE_REPORTING) 461 std::unique_ptr<HttpServerProperties> http_server_properties_; 462 std::map<std::string, std::unique_ptr<URLRequestJobFactory::ProtocolHandler>> 463 protocol_handlers_; 464 465 raw_ptr<ClientSocketFactory> client_socket_factory_raw_ = nullptr; 466 }; 467 468 } // namespace net 469 470 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ 471