[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 1 | // Copyright 2014 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 | #ifndef COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ |
| 6 | #define COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ |
| 7 | |
dcheng | fe3745e624 | 2016-04-21 23:49:58 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 9 | #include <string> |
Donna Wu | 7ea51c2 | 2017-06-20 08:34:52 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 11 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 12 | #include "base/macros.h" |
xunjieli | da7f7702 | 2016-03-28 16:36:36 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 14 | #include "base/time/time.h" |
mgersh | cc6ae89 | 2017-06-20 22:33:53 | [diff] [blame] | 15 | #include "base/values.h" |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 16 | #include "net/base/hash_value.h" |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 17 | #include "net/cert/cert_verifier.h" |
mmenke | cf3cb2a | 2017-07-07 20:48:44 | [diff] [blame] | 18 | #include "net/http/http_network_session.h" |
tbansal | e11aa36 | 2017-07-04 15:59:36 | [diff] [blame] | 19 | #include "net/nqe/effective_connection_type.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 20 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 21 | |
| 22 | namespace net { |
xunjieli | 013145f | 2015-10-20 23:20:11 | [diff] [blame] | 23 | class CertVerifier; |
Victor Vasiliev | a1e66d7 | 2019-12-05 17:55:38 | [diff] [blame] | 24 | struct QuicParams; |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 25 | class URLRequestContextBuilder; |
| 26 | } // namespace net |
| 27 | |
| 28 | namespace cronet { |
| 29 | |
| 30 | // Common configuration parameters used by Cronet to configure |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 31 | // URLRequestContext. |
mgersh | cc6ae89 | 2017-06-20 22:33:53 | [diff] [blame] | 32 | // TODO(mgersh): This shouldn't be a struct, and experimental option parsing |
| 33 | // should be kept more separate from applying the configuration. |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 34 | struct URLRequestContextConfig { |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 35 | // Type of HTTP cache. |
kapishnikov | aa8f338c | 2016-10-28 16:17:32 | [diff] [blame] | 36 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net.impl |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 37 | enum HttpCacheType { |
| 38 | // No HTTP cache. |
| 39 | DISABLED, |
| 40 | // HTTP cache persisted to disk. |
| 41 | DISK, |
| 42 | // HTTP cache kept in memory. |
| 43 | MEMORY, |
| 44 | }; |
| 45 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 46 | // App-provided hint that server supports QUIC. |
| 47 | struct QuicHint { |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 48 | QuicHint(const std::string& host, int port, int alternate_port); |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 49 | ~QuicHint(); |
| 50 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 51 | // Host name of the server that supports QUIC. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 52 | const std::string host; |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 53 | // Port of the server that supports QUIC. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 54 | const int port; |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 55 | // Alternate protocol port. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 56 | const int alternate_port; |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | DISALLOW_COPY_AND_ASSIGN(QuicHint); |
| 60 | }; |
| 61 | |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 62 | // Public-Key-Pinning configuration structure. |
| 63 | struct Pkp { |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 64 | Pkp(const std::string& host, |
| 65 | bool include_subdomains, |
| 66 | const base::Time& expiration_date); |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 67 | ~Pkp(); |
| 68 | |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 69 | // Host name. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 70 | const std::string host; |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 71 | // Pin hashes (currently SHA256 only). |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 72 | net::HashValueVector pin_hashes; |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 73 | // Indicates whether the pinning should apply to the pinned host subdomains. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 74 | const bool include_subdomains; |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 75 | // Expiration date for the pins. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 76 | const base::Time expiration_date; |
kapishnikov | df5ccab | 2015-12-03 18:38:50 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | DISALLOW_COPY_AND_ASSIGN(Pkp); |
| 80 | }; |
| 81 | |
Douglas Creager | a220947e | 2018-08-23 20:08:53 | [diff] [blame] | 82 | // Simulated headers, used to preconfigure the Reporting API and Network Error |
| 83 | // Logging before receiving those actual configuration headers from the |
| 84 | // origins. |
| 85 | struct PreloadedNelAndReportingHeader { |
| 86 | PreloadedNelAndReportingHeader(const url::Origin& origin, |
| 87 | std::string value); |
| 88 | ~PreloadedNelAndReportingHeader(); |
| 89 | |
| 90 | // Origin that is "sending" this header. |
| 91 | const url::Origin origin; |
| 92 | |
| 93 | // Value of the header that is "sent". |
| 94 | const std::string value; |
| 95 | }; |
| 96 | |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 97 | URLRequestContextConfig( |
| 98 | // Enable QUIC. |
| 99 | bool enable_quic, |
mef | c5da571 | 2016-02-09 20:14:23 | [diff] [blame] | 100 | // QUIC User Agent ID. |
| 101 | const std::string& quic_user_agent_id, |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 102 | // Enable SPDY. |
| 103 | bool enable_spdy, |
xunjieli | 186d2bf | 2017-04-18 13:45:47 | [diff] [blame] | 104 | // Enable Brotli. |
| 105 | bool enable_brotli, |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 106 | // Type of http cache. |
| 107 | HttpCacheType http_cache, |
| 108 | // Max size of http cache in bytes. |
| 109 | int http_cache_max_size, |
| 110 | // Disable caching for HTTP responses. Other information may be stored in |
| 111 | // the cache. |
| 112 | bool load_disable_cache, |
| 113 | // Storage path for http cache and cookie storage. |
| 114 | const std::string& storage_path, |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 115 | // Accept-Language request header field. |
| 116 | const std::string& accept_language, |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 117 | // User-Agent request header field. |
| 118 | const std::string& user_agent, |
| 119 | // JSON encoded experimental options. |
| 120 | const std::string& experimental_options, |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 121 | // MockCertVerifier to use for testing purposes. |
tbansal | 7018e2a | 2016-06-25 00:40:39 | [diff] [blame] | 122 | std::unique_ptr<net::CertVerifier> mock_cert_verifier, |
| 123 | // Enable network quality estimator. |
kapishnikov | 385aa42 | 2016-07-01 20:53:02 | [diff] [blame] | 124 | bool enable_network_quality_estimator, |
| 125 | // Enable bypassing of public key pinning for local trust anchors |
Paul Jensen | 6a1ea3a | 2018-08-24 14:46:41 | [diff] [blame] | 126 | bool bypass_public_key_pinning_for_local_trust_anchors, |
| 127 | // Optional network thread priority. |
| 128 | // On Android, corresponds to android.os.Process.setThreadPriority() |
| 129 | // values. On iOS, corresponds to NSThread::setThreadPriority values. Do |
| 130 | // not specify for other targets. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 131 | absl::optional<double> network_thread_priority); |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 132 | ~URLRequestContextConfig(); |
| 133 | |
xunjieli | d67295e | 2017-03-16 21:05:41 | [diff] [blame] | 134 | // Configures |context_builder| based on |this|. |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 135 | void ConfigureURLRequestContextBuilder( |
Matt Mueller | de5dadf | 2019-11-27 20:11:58 | [diff] [blame] | 136 | net::URLRequestContextBuilder* context_builder); |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 137 | |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 138 | // Enable QUIC. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 139 | const bool enable_quic; |
mef | c5da571 | 2016-02-09 20:14:23 | [diff] [blame] | 140 | // QUIC User Agent ID. |
| 141 | const std::string quic_user_agent_id; |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 142 | // Enable SPDY. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 143 | const bool enable_spdy; |
xunjieli | 186d2bf | 2017-04-18 13:45:47 | [diff] [blame] | 144 | // Enable Brotli. |
| 145 | const bool enable_brotli; |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 146 | // Type of http cache. |
| 147 | const HttpCacheType http_cache; |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 148 | // Max size of http cache in bytes. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 149 | const int http_cache_max_size; |
mef | bb4f45c | 2015-01-12 18:03:25 | [diff] [blame] | 150 | // Disable caching for HTTP responses. Other information may be stored in |
| 151 | // the cache. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 152 | const bool load_disable_cache; |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 153 | // Storage path for http cache and cookie storage. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 154 | const std::string storage_path; |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 155 | // Accept-Language request header field. |
| 156 | const std::string accept_language; |
mef | d190710 | 2014-11-07 17:46:48 | [diff] [blame] | 157 | // User-Agent request header field. |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 158 | const std::string user_agent; |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 159 | |
xunjieli | 013145f | 2015-10-20 23:20:11 | [diff] [blame] | 160 | // Certificate verifier for testing. |
dcheng | fe3745e624 | 2016-04-21 23:49:58 | [diff] [blame] | 161 | std::unique_ptr<net::CertVerifier> mock_cert_verifier; |
xunjieli | 013145f | 2015-10-20 23:20:11 | [diff] [blame] | 162 | |
tbansal | e11aa36 | 2017-07-04 15:59:36 | [diff] [blame] | 163 | // Enable Network Quality Estimator (NQE). |
tbansal | 7018e2a | 2016-06-25 00:40:39 | [diff] [blame] | 164 | const bool enable_network_quality_estimator; |
| 165 | |
kapishnikov | 385aa42 | 2016-07-01 20:53:02 | [diff] [blame] | 166 | // Enable public key pinning bypass for local trust anchors. |
| 167 | const bool bypass_public_key_pinning_for_local_trust_anchors; |
| 168 | |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 169 | // App-provided list of servers that support QUIC. |
Donna Wu | 7ea51c2 | 2017-06-20 08:34:52 | [diff] [blame] | 170 | std::vector<std::unique_ptr<QuicHint>> quic_hints; |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 171 | |
| 172 | // The list of public key pins. |
Donna Wu | 7ea51c2 | 2017-06-20 08:34:52 | [diff] [blame] | 173 | std::vector<std::unique_ptr<Pkp>> pkp_list; |
pauljensen | 9041eb3c | 2015-12-09 12:29:01 | [diff] [blame] | 174 | |
mgersh | ac5f75a | 2017-06-28 16:53:08 | [diff] [blame] | 175 | // Enable DNS cache persistence. |
| 176 | bool enable_host_cache_persistence = false; |
| 177 | |
| 178 | // Minimum time in milliseconds between writing the HostCache contents to |
| 179 | // prefs. Only relevant when |enable_host_cache_persistence| is true. |
| 180 | int host_cache_persistence_delay_ms = 60000; |
| 181 | |
xunjieli | d67295e | 2017-03-16 21:05:41 | [diff] [blame] | 182 | // Experimental options that are recognized by the config parser. |
Lei Zhang | 724aa16b | 2021-04-15 20:56:58 | [diff] [blame] | 183 | std::unique_ptr<base::DictionaryValue> effective_experimental_options; |
xunjieli | d67295e | 2017-03-16 21:05:41 | [diff] [blame] | 184 | |
tbansal | e11aa36 | 2017-07-04 15:59:36 | [diff] [blame] | 185 | // If set, forces NQE to return the set value as the effective connection |
| 186 | // type. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 187 | absl::optional<net::EffectiveConnectionType> |
tbansal | e11aa36 | 2017-07-04 15:59:36 | [diff] [blame] | 188 | nqe_forced_effective_connection_type; |
| 189 | |
Douglas Creager | a220947e | 2018-08-23 20:08:53 | [diff] [blame] | 190 | // Preloaded Report-To headers, to preconfigure the Reporting API. |
| 191 | std::vector<PreloadedNelAndReportingHeader> preloaded_report_to_headers; |
| 192 | |
| 193 | // Preloaded NEL headers, to preconfigure Network Error Logging. |
| 194 | std::vector<PreloadedNelAndReportingHeader> preloaded_nel_headers; |
| 195 | |
Paul Jensen | 6a1ea3a | 2018-08-24 14:46:41 | [diff] [blame] | 196 | // Optional network thread priority. |
| 197 | // On Android, corresponds to android.os.Process.setThreadPriority() values. |
| 198 | // On iOS, corresponds to NSThread::setThreadPriority values. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 199 | const absl::optional<double> network_thread_priority; |
Paul Jensen | 6a1ea3a | 2018-08-24 14:46:41 | [diff] [blame] | 200 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 201 | private: |
mgersh | cc6ae89 | 2017-06-20 22:33:53 | [diff] [blame] | 202 | // Parses experimental options and makes appropriate changes to settings in |
| 203 | // the URLRequestContextConfig and URLRequestContextBuilder. |
| 204 | void ParseAndSetExperimentalOptions( |
| 205 | net::URLRequestContextBuilder* context_builder, |
Victor Vasiliev | a1e66d7 | 2019-12-05 17:55:38 | [diff] [blame] | 206 | net::HttpNetworkSession::Params* session_params, |
| 207 | net::QuicParams* quic_params); |
mgersh | cc6ae89 | 2017-06-20 22:33:53 | [diff] [blame] | 208 | |
tbansal | e11aa36 | 2017-07-04 15:59:36 | [diff] [blame] | 209 | // Experimental options encoded as a string in a JSON format containing |
| 210 | // experiments and their corresponding configuration options. The format |
| 211 | // is a JSON object with the name of the experiment as the key, and the |
| 212 | // configuration options as the value. An example: |
mmenke | cf3cb2a | 2017-07-07 20:48:44 | [diff] [blame] | 213 | // {"experiment1": {"option1": "option_value1", "option2": |
| 214 | // "option_value2", |
tbansal | e11aa36 | 2017-07-04 15:59:36 | [diff] [blame] | 215 | // ...}, "experiment2: {"option3", "option_value3", ...}, ...} |
| 216 | const std::string experimental_options; |
| 217 | |
mef | c71361c | 2014-09-16 14:48:56 | [diff] [blame] | 218 | DISALLOW_COPY_AND_ASSIGN(URLRequestContextConfig); |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 219 | }; |
| 220 | |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 221 | // Stores intermediate state for URLRequestContextConfig. Initializes with |
| 222 | // (mostly) sane defaults, then the appropriate member variables can be |
| 223 | // modified, and it can be finalized with Build(). |
| 224 | struct URLRequestContextConfigBuilder { |
| 225 | URLRequestContextConfigBuilder(); |
| 226 | ~URLRequestContextConfigBuilder(); |
| 227 | |
| 228 | // Finalize state into a URLRequestContextConfig. Must only be called once, |
| 229 | // as once |mock_cert_verifier| is moved into a URLRequestContextConfig, it |
| 230 | // cannot be used again. |
| 231 | std::unique_ptr<URLRequestContextConfig> Build(); |
| 232 | |
| 233 | // Enable QUIC. |
Renjie Tang | 1c71894 | 2020-06-11 23:05:38 | [diff] [blame] | 234 | bool enable_quic = true; |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 235 | // QUIC User Agent ID. |
| 236 | std::string quic_user_agent_id = ""; |
| 237 | // Enable SPDY. |
| 238 | bool enable_spdy = true; |
xunjieli | 186d2bf | 2017-04-18 13:45:47 | [diff] [blame] | 239 | // Enable Brotli. |
| 240 | bool enable_brotli = false; |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 241 | // Type of http cache. |
| 242 | URLRequestContextConfig::HttpCacheType http_cache = |
| 243 | URLRequestContextConfig::DISABLED; |
| 244 | // Max size of http cache in bytes. |
| 245 | int http_cache_max_size = 0; |
| 246 | // Disable caching for HTTP responses. Other information may be stored in |
| 247 | // the cache. |
| 248 | bool load_disable_cache = false; |
| 249 | // Storage path for http cache and cookie storage. |
| 250 | std::string storage_path = ""; |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 251 | // Accept-Language request header field. |
| 252 | std::string accept_language = ""; |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 253 | // User-Agent request header field. |
| 254 | std::string user_agent = ""; |
| 255 | // Experimental options encoded as a string in a JSON format containing |
| 256 | // experiments and their corresponding configuration options. The format |
| 257 | // is a JSON object with the name of the experiment as the key, and the |
| 258 | // configuration options as the value. An example: |
| 259 | // {"experiment1": {"option1": "option_value1", "option2": "option_value2", |
| 260 | // ...}, "experiment2: {"option3", "option_value3", ...}, ...} |
| 261 | std::string experimental_options = "{}"; |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 262 | |
| 263 | // Certificate verifier for testing. |
Lei Zhang | 5bb12d9 | 2021-04-15 09:54:56 | [diff] [blame] | 264 | std::unique_ptr<net::CertVerifier> mock_cert_verifier; |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 265 | |
| 266 | // Enable network quality estimator. |
| 267 | bool enable_network_quality_estimator = false; |
| 268 | |
| 269 | // Enable public key pinning bypass for local trust anchors. |
| 270 | bool bypass_public_key_pinning_for_local_trust_anchors = true; |
| 271 | |
Paul Jensen | 6a1ea3a | 2018-08-24 14:46:41 | [diff] [blame] | 272 | // Optional network thread priority. |
| 273 | // On Android, corresponds to android.os.Process.setThreadPriority() values. |
| 274 | // On iOS, corresponds to NSThread::setThreadPriority values. |
| 275 | // Do not specify for other targets. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 276 | absl::optional<double> network_thread_priority; |
Paul Jensen | 6a1ea3a | 2018-08-24 14:46:41 | [diff] [blame] | 277 | |
lilyhoughton | 14e2a1f1 | 2017-01-11 14:50:27 | [diff] [blame] | 278 | private: |
| 279 | DISALLOW_COPY_AND_ASSIGN(URLRequestContextConfigBuilder); |
| 280 | }; |
| 281 | |
[email protected] | 94de3e0 | 2014-06-17 00:09:51 | [diff] [blame] | 282 | } // namespace cronet |
| 283 | |
| 284 | #endif // COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ |