url_request_context_builder.h 19 KB

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