[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 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 | |||||
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_ | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 16 | |
17 | #include <string> | ||||
[email protected] | 1575e3d | 2014-05-03 22:21:44 | [diff] [blame] | 18 | #include <vector> |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 19 | |
20 | #include "base/basictypes.h" | ||||
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 21 | #include "base/files/file_path.h" |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 22 | #include "base/memory/ref_counted.h" |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 23 | #include "base/memory/scoped_ptr.h" |
24 | #include "build/build_config.h" | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 25 | #include "net/base/net_export.h" |
26 | |||||
27 | namespace net { | ||||
28 | |||||
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 29 | class FtpTransactionFactory; |
[email protected] | 72d8220d | 2013-11-27 15:31:21 | [diff] [blame] | 30 | class HostResolver; |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 31 | class HostMappingRules; |
[email protected] | 1575e3d | 2014-05-03 22:21:44 | [diff] [blame] | 32 | class HttpAuthHandlerFactory; |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 33 | class ProxyConfigService; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 34 | class URLRequestContext; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 35 | class NetworkDelegate; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 36 | |
37 | class NET_EXPORT URLRequestContextBuilder { | ||||
38 | public: | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 39 | struct NET_EXPORT HttpCacheParams { |
40 | enum Type { | ||||
41 | IN_MEMORY, | ||||
42 | DISK, | ||||
43 | }; | ||||
44 | |||||
45 | HttpCacheParams(); | ||||
46 | ~HttpCacheParams(); | ||||
47 | |||||
[email protected] | 393e5ec6 | 2012-07-09 19:08:28 | [diff] [blame] | 48 | // The type of HTTP cache. Default is IN_MEMORY. |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 49 | Type type; |
50 | |||||
51 | // The max size of the cache in bytes. Default is algorithmically determined | ||||
52 | // based off available disk space. | ||||
53 | int max_size; | ||||
54 | |||||
55 | // The cache path (when type is DISK). | ||||
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 56 | base::FilePath path; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 57 | }; |
58 | |||||
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 59 | struct NET_EXPORT HttpNetworkSessionParams { |
60 | HttpNetworkSessionParams(); | ||||
61 | ~HttpNetworkSessionParams(); | ||||
62 | |||||
63 | // These fields mirror those in net::HttpNetworkSession::Params; | ||||
64 | bool ignore_certificate_errors; | ||||
65 | HostMappingRules* host_mapping_rules; | ||||
66 | bool http_pipelining_enabled; | ||||
67 | uint16 testing_fixed_http_port; | ||||
68 | uint16 testing_fixed_https_port; | ||||
69 | std::string trusted_spdy_proxy; | ||||
70 | }; | ||||
71 | |||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 72 | URLRequestContextBuilder(); |
73 | ~URLRequestContextBuilder(); | ||||
74 | |||||
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 75 | void set_proxy_config_service(ProxyConfigService* proxy_config_service); |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 76 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 77 | // Call these functions to specify hard-coded Accept-Language |
78 | // or User-Agent header values for all requests that don't | ||||
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 79 | // have the headers already set. |
80 | void set_accept_language(const std::string& accept_language) { | ||||
81 | accept_language_ = accept_language; | ||||
82 | } | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 83 | void set_user_agent(const std::string& user_agent) { |
84 | user_agent_ = user_agent; | ||||
85 | } | ||||
86 | |||||
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 87 | // Control support for data:// requests. By default it's disabled. |
88 | void set_data_enabled(bool enable) { | ||||
89 | data_enabled_ = enable; | ||||
90 | } | ||||
91 | |||||
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 92 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 93 | // Control support for file:// requests. By default it's disabled. |
94 | void set_file_enabled(bool enable) { | ||||
95 | file_enabled_ = enable; | ||||
96 | } | ||||
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 97 | #endif |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 98 | |
99 | #if !defined(DISABLE_FTP_SUPPORT) | ||||
100 | // Control support for ftp:// requests. By default it's disabled. | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 101 | void set_ftp_enabled(bool enable) { |
102 | ftp_enabled_ = enable; | ||||
103 | } | ||||
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 104 | #endif |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 105 | |
[email protected] | 72d8220d | 2013-11-27 15:31:21 | [diff] [blame] | 106 | // By default host_resolver is constructed with CreateDefaultResolver. |
107 | void set_host_resolver(HostResolver* host_resolver) { | ||||
108 | host_resolver_.reset(host_resolver); | ||||
109 | } | ||||
110 | |||||
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 111 | // Uses BasicNetworkDelegate by default. Note that calling Build will unset |
112 | // any custom delegate in builder, so this must be called each time before | ||||
113 | // Build is called. | ||||
114 | void set_network_delegate(NetworkDelegate* delegate) { | ||||
115 | network_delegate_.reset(delegate); | ||||
116 | } | ||||
117 | |||||
[email protected] | 1575e3d | 2014-05-03 22:21:44 | [diff] [blame] | 118 | |
119 | // Adds additional auth handler factories to be used in addition to what is | ||||
120 | // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme| | ||||
121 | // and |factory| are provided. The builder takes ownership of the factory and | ||||
122 | // Build() must be called after this method. | ||||
123 | void add_http_auth_handler_factory(const std::string& scheme, | ||||
124 | net::HttpAuthHandlerFactory* factory) { | ||||
125 | extra_http_auth_handlers_.push_back(SchemeFactory(scheme, factory)); | ||||
126 | } | ||||
127 | |||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 128 | // By default HttpCache is enabled with a default constructed HttpCacheParams. |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 129 | void EnableHttpCache(const HttpCacheParams& params) { |
[email protected] | ff316f1 | 2014-01-19 00:08:20 | [diff] [blame] | 130 | http_cache_enabled_ = true; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 131 | http_cache_params_ = params; |
132 | } | ||||
133 | |||||
134 | void DisableHttpCache() { | ||||
[email protected] | ff316f1 | 2014-01-19 00:08:20 | [diff] [blame] | 135 | http_cache_enabled_ = false; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 136 | http_cache_params_ = HttpCacheParams(); |
137 | } | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 138 | |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 139 | // Override default net::HttpNetworkSession::Params settings. |
140 | void set_http_network_session_params( | ||||
141 | const HttpNetworkSessionParams& http_network_session_params) { | ||||
142 | http_network_session_params_ = http_network_session_params; | ||||
143 | } | ||||
144 | |||||
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 145 | URLRequestContext* Build(); |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 146 | |
147 | private: | ||||
[email protected] | 1575e3d | 2014-05-03 22:21:44 | [diff] [blame] | 148 | |
149 | struct SchemeFactory { | ||||
150 | SchemeFactory(const std::string& scheme, | ||||
151 | net::HttpAuthHandlerFactory* factory); | ||||
152 | ~SchemeFactory(); | ||||
153 | |||||
154 | std::string scheme; | ||||
155 | net::HttpAuthHandlerFactory* factory; | ||||
156 | }; | ||||
157 | |||||
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 158 | std::string accept_language_; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 159 | std::string user_agent_; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 160 | // Include support for data:// requests. |
161 | bool data_enabled_; | ||||
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 162 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 163 | // Include support for file:// requests. |
164 | bool file_enabled_; | ||||
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 165 | #endif |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 166 | #if !defined(DISABLE_FTP_SUPPORT) |
167 | // Include support for ftp:// requests. | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 168 | bool ftp_enabled_; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 169 | #endif |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 170 | bool http_cache_enabled_; |
171 | HttpCacheParams http_cache_params_; | ||||
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 172 | HttpNetworkSessionParams http_network_session_params_; |
[email protected] | 72d8220d | 2013-11-27 15:31:21 | [diff] [blame] | 173 | scoped_ptr<HostResolver> host_resolver_; |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 174 | scoped_ptr<ProxyConfigService> proxy_config_service_; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 175 | scoped_ptr<NetworkDelegate> network_delegate_; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 176 | scoped_ptr<FtpTransactionFactory> ftp_transaction_factory_; |
[email protected] | 1575e3d | 2014-05-03 22:21:44 | [diff] [blame] | 177 | std::vector<SchemeFactory> extra_http_auth_handlers_; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 178 | |
179 | DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder); | ||||
180 | }; | ||||
181 | |||||
182 | } // namespace net | ||||
183 | |||||
184 | #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ |