1 // Copyright 2014 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 #ifndef COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ 6 #define COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/time/time.h" 13 #include "base/values.h" 14 #include "net/base/hash_value.h" 15 #include "net/base/network_handle.h" 16 #include "net/cert/cert_verifier.h" 17 #include "net/nqe/effective_connection_type.h" 18 #include "third_party/abseil-cpp/absl/types/optional.h" 19 #include "url/origin.h" 20 21 namespace net { 22 class CertVerifier; 23 struct HttpNetworkSessionParams; 24 struct QuicParams; 25 class URLRequestContextBuilder; 26 } // namespace net 27 28 namespace cronet { 29 30 // Common configuration parameters used by Cronet to configure 31 // URLRequestContext. 32 // TODO(mgersh): This shouldn't be a struct, and experimental option parsing 33 // should be kept more separate from applying the configuration. 34 struct URLRequestContextConfig { 35 // Type of HTTP cache. 36 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net.impl 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 46 // App-provided hint that server supports QUIC. 47 struct QuicHint { 48 QuicHint(const std::string& host, int port, int alternate_port); 49 50 QuicHint(const QuicHint&) = delete; 51 QuicHint& operator=(const QuicHint&) = delete; 52 53 ~QuicHint(); 54 55 // Host name of the server that supports QUIC. 56 const std::string host; 57 // Port of the server that supports QUIC. 58 const int port; 59 // Alternate protocol port. 60 const int alternate_port; 61 }; 62 63 // Public-Key-Pinning configuration structure. 64 struct Pkp { 65 Pkp(const std::string& host, 66 bool include_subdomains, 67 const base::Time& expiration_date); 68 69 Pkp(const Pkp&) = delete; 70 Pkp& operator=(const Pkp&) = delete; 71 72 ~Pkp(); 73 74 // Host name. 75 const std::string host; 76 // Pin hashes (currently SHA256 only). 77 net::HashValueVector pin_hashes; 78 // Indicates whether the pinning should apply to the pinned host subdomains. 79 const bool include_subdomains; 80 // Expiration date for the pins. 81 const base::Time expiration_date; 82 }; 83 84 // Simulated headers, used to preconfigure the Reporting API and Network Error 85 // Logging before receiving those actual configuration headers from the 86 // origins. 87 struct PreloadedNelAndReportingHeader { 88 PreloadedNelAndReportingHeader(const url::Origin& origin, 89 std::string value); 90 ~PreloadedNelAndReportingHeader(); 91 92 // Origin that is "sending" this header. 93 const url::Origin origin; 94 95 // Value of the header that is "sent". 96 const std::string value; 97 }; 98 99 URLRequestContextConfig(const URLRequestContextConfig&) = delete; 100 URLRequestContextConfig& operator=(const URLRequestContextConfig&) = delete; 101 102 ~URLRequestContextConfig(); 103 104 // Configures |context_builder| based on |this|. 105 void ConfigureURLRequestContextBuilder( 106 net::URLRequestContextBuilder* context_builder, 107 net::handles::NetworkHandle bound_network = 108 net::handles::kInvalidNetworkHandle); 109 110 // Enable QUIC. 111 const bool enable_quic; 112 // Enable SPDY. 113 const bool enable_spdy; 114 // Enable Brotli. 115 const bool enable_brotli; 116 // Type of http cache. 117 const HttpCacheType http_cache; 118 // Max size of http cache in bytes. 119 const int http_cache_max_size; 120 // Disable caching for HTTP responses. Other information may be stored in 121 // the cache. 122 const bool load_disable_cache; 123 // Storage path for http cache and cookie storage. 124 const std::string storage_path; 125 // Accept-Language request header field. 126 const std::string accept_language; 127 // User-Agent request header field. 128 const std::string user_agent; 129 130 // Certificate verifier for testing. 131 std::unique_ptr<net::CertVerifier> mock_cert_verifier; 132 133 // Enable Network Quality Estimator (NQE). 134 const bool enable_network_quality_estimator; 135 136 // Enable public key pinning bypass for local trust anchors. 137 const bool bypass_public_key_pinning_for_local_trust_anchors; 138 139 // App-provided list of servers that support QUIC. 140 std::vector<std::unique_ptr<QuicHint>> quic_hints; 141 142 // The list of public key pins. 143 std::vector<std::unique_ptr<Pkp>> pkp_list; 144 145 // Enable DNS cache persistence. 146 bool enable_host_cache_persistence = false; 147 148 // Minimum time in milliseconds between writing the HostCache contents to 149 // prefs. Only relevant when |enable_host_cache_persistence| is true. 150 int host_cache_persistence_delay_ms = 60000; 151 152 // Experimental options that are recognized by the config parser. 153 base::Value::Dict effective_experimental_options; 154 base::Value::Dict experimental_options; 155 156 // If set, forces NQE to return the set value as the effective connection 157 // type. 158 absl::optional<net::EffectiveConnectionType> 159 nqe_forced_effective_connection_type; 160 161 // Preloaded Report-To headers, to preconfigure the Reporting API. 162 std::vector<PreloadedNelAndReportingHeader> preloaded_report_to_headers; 163 164 // Preloaded NEL headers, to preconfigure Network Error Logging. 165 std::vector<PreloadedNelAndReportingHeader> preloaded_nel_headers; 166 167 // Optional network thread priority. 168 // On Android, corresponds to android.os.Process.setThreadPriority() values. 169 const absl::optional<double> network_thread_priority; 170 171 // Whether the connection status of active bidirectional streams should be 172 // monitored. 173 bool bidi_stream_detect_broken_connection; 174 // If |bidi_stream_detect_broken_connection_| is true, this suggests the 175 // period of the heartbeat signal. 176 base::TimeDelta heartbeat_interval; 177 178 // Whether Cronet Telemetry should be enabled or not. 179 bool enable_telemetry; 180 ExperimentalOptionsParsingIsAllowedToFailURLRequestContextConfig181 static bool ExperimentalOptionsParsingIsAllowedToFail() { 182 return DCHECK_IS_ON(); 183 } 184 185 static std::unique_ptr<URLRequestContextConfig> CreateURLRequestContextConfig( 186 // Enable QUIC. 187 bool enable_quic, 188 // Enable SPDY. 189 bool enable_spdy, 190 // Enable Brotli. 191 bool enable_brotli, 192 // Type of http cache. 193 HttpCacheType http_cache, 194 // Max size of http cache in bytes. 195 int http_cache_max_size, 196 // Disable caching for HTTP responses. Other information may be stored in 197 // the cache. 198 bool load_disable_cache, 199 // Storage path for http cache and cookie storage. 200 const std::string& storage_path, 201 // Accept-Language request header field. 202 const std::string& accept_language, 203 // User-Agent request header field. 204 const std::string& user_agent, 205 // JSON encoded experimental options. 206 const std::string& unparsed_experimental_options, 207 // MockCertVerifier to use for testing purposes. 208 std::unique_ptr<net::CertVerifier> mock_cert_verifier, 209 // Enable network quality estimator. 210 bool enable_network_quality_estimator, 211 // Enable bypassing of public key pinning for local trust anchors 212 bool bypass_public_key_pinning_for_local_trust_anchors, 213 // Optional network thread priority. 214 // On Android, corresponds to android.os.Process.setThreadPriority() 215 // values. Do not specify for other targets. 216 absl::optional<double> network_thread_priority); 217 218 private: 219 URLRequestContextConfig( 220 // Enable QUIC. 221 bool enable_quic, 222 // Enable SPDY. 223 bool enable_spdy, 224 // Enable Brotli. 225 bool enable_brotli, 226 // Type of http cache. 227 HttpCacheType http_cache, 228 // Max size of http cache in bytes. 229 int http_cache_max_size, 230 // Disable caching for HTTP responses. Other information may be stored in 231 // the cache. 232 bool load_disable_cache, 233 // Storage path for http cache and cookie storage. 234 const std::string& storage_path, 235 // Accept-Language request header field. 236 const std::string& accept_language, 237 // User-Agent request header field. 238 const std::string& user_agent, 239 // Parsed experimental options. 240 base::Value::Dict experimental_options, 241 // MockCertVerifier to use for testing purposes. 242 std::unique_ptr<net::CertVerifier> mock_cert_verifier, 243 // Enable network quality estimator. 244 bool enable_network_quality_estimator, 245 // Enable bypassing of public key pinning for local trust anchors 246 bool bypass_public_key_pinning_for_local_trust_anchors, 247 // Optional network thread priority. 248 // On Android, corresponds to android.os.Process.setThreadPriority() 249 // values. Do not specify for other targets. 250 absl::optional<double> network_thread_priority); 251 252 // Parses experimental options from their JSON format to the format used 253 // internally. 254 // Returns an empty optional if the operation was unsuccessful. 255 static absl::optional<base::Value::Dict> ParseExperimentalOptions( 256 std::string unparsed_experimental_options); 257 258 // Makes appropriate changes to settings in |this|. 259 void SetContextConfigExperimentalOptions(); 260 261 // Makes appropriate changes to settings in the URLRequestContextBuilder. 262 void SetContextBuilderExperimentalOptions( 263 net::URLRequestContextBuilder* context_builder, 264 net::HttpNetworkSessionParams* session_params, 265 net::QuicParams* quic_params, 266 net::handles::NetworkHandle bound_network); 267 }; 268 269 // Stores intermediate state for URLRequestContextConfig. Initializes with 270 // (mostly) sane defaults, then the appropriate member variables can be 271 // modified, and it can be finalized with Build(). 272 struct URLRequestContextConfigBuilder { 273 URLRequestContextConfigBuilder(); 274 275 URLRequestContextConfigBuilder(const URLRequestContextConfigBuilder&) = 276 delete; 277 URLRequestContextConfigBuilder& operator=( 278 const URLRequestContextConfigBuilder&) = delete; 279 280 ~URLRequestContextConfigBuilder(); 281 282 // Finalize state into a URLRequestContextConfig. Must only be called once, 283 // as once |mock_cert_verifier| is moved into a URLRequestContextConfig, it 284 // cannot be used again. 285 std::unique_ptr<URLRequestContextConfig> Build(); 286 287 // Enable QUIC. 288 bool enable_quic = true; 289 // Enable SPDY. 290 bool enable_spdy = true; 291 // Enable Brotli. 292 bool enable_brotli = false; 293 // Type of http cache. 294 URLRequestContextConfig::HttpCacheType http_cache = 295 URLRequestContextConfig::DISABLED; 296 // Max size of http cache in bytes. 297 int http_cache_max_size = 0; 298 // Disable caching for HTTP responses. Other information may be stored in 299 // the cache. 300 bool load_disable_cache = false; 301 // Storage path for http cache and cookie storage. 302 std::string storage_path = ""; 303 // Accept-Language request header field. 304 std::string accept_language = ""; 305 // User-Agent request header field. 306 std::string user_agent = ""; 307 // Experimental options encoded as a string in a JSON format containing 308 // experiments and their corresponding configuration options. The format 309 // is a JSON object with the name of the experiment as the key, and the 310 // configuration options as the value. An example: 311 // {"experiment1": {"option1": "option_value1", "option2": "option_value2", 312 // ...}, "experiment2: {"option3", "option_value3", ...}, ...} 313 std::string experimental_options = "{}"; 314 315 // Certificate verifier for testing. 316 std::unique_ptr<net::CertVerifier> mock_cert_verifier; 317 318 // Enable network quality estimator. 319 bool enable_network_quality_estimator = false; 320 321 // Enable public key pinning bypass for local trust anchors. 322 bool bypass_public_key_pinning_for_local_trust_anchors = true; 323 324 // Optional network thread priority. 325 // On Android, corresponds to android.os.Process.setThreadPriority() values. 326 // Do not specify for other targets. 327 absl::optional<double> network_thread_priority; 328 }; 329 330 } // namespace cronet 331 332 #endif // COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ 333