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 // QUIC User Agent ID. 113 const std::string quic_user_agent_id; 114 // Enable SPDY. 115 const bool enable_spdy; 116 // Enable Brotli. 117 const bool enable_brotli; 118 // Type of http cache. 119 const HttpCacheType http_cache; 120 // Max size of http cache in bytes. 121 const int http_cache_max_size; 122 // Disable caching for HTTP responses. Other information may be stored in 123 // the cache. 124 const bool load_disable_cache; 125 // Storage path for http cache and cookie storage. 126 const std::string storage_path; 127 // Accept-Language request header field. 128 const std::string accept_language; 129 // User-Agent request header field. 130 const std::string user_agent; 131 132 // Certificate verifier for testing. 133 std::unique_ptr<net::CertVerifier> mock_cert_verifier; 134 135 // Enable Network Quality Estimator (NQE). 136 const bool enable_network_quality_estimator; 137 138 // Enable public key pinning bypass for local trust anchors. 139 const bool bypass_public_key_pinning_for_local_trust_anchors; 140 141 // App-provided list of servers that support QUIC. 142 std::vector<std::unique_ptr<QuicHint>> quic_hints; 143 144 // The list of public key pins. 145 std::vector<std::unique_ptr<Pkp>> pkp_list; 146 147 // Enable DNS cache persistence. 148 bool enable_host_cache_persistence = false; 149 150 // Minimum time in milliseconds between writing the HostCache contents to 151 // prefs. Only relevant when |enable_host_cache_persistence| is true. 152 int host_cache_persistence_delay_ms = 60000; 153 154 // Experimental options that are recognized by the config parser. 155 base::Value::Dict effective_experimental_options; 156 base::Value::Dict experimental_options; 157 158 // If set, forces NQE to return the set value as the effective connection 159 // type. 160 absl::optional<net::EffectiveConnectionType> 161 nqe_forced_effective_connection_type; 162 163 // Preloaded Report-To headers, to preconfigure the Reporting API. 164 std::vector<PreloadedNelAndReportingHeader> preloaded_report_to_headers; 165 166 // Preloaded NEL headers, to preconfigure Network Error Logging. 167 std::vector<PreloadedNelAndReportingHeader> preloaded_nel_headers; 168 169 // Optional network thread priority. 170 // On Android, corresponds to android.os.Process.setThreadPriority() values. 171 // On iOS, corresponds to NSThread::setThreadPriority values. 172 const absl::optional<double> network_thread_priority; 173 174 // Whether the connection status of active bidirectional streams should be 175 // monitored. 176 bool bidi_stream_detect_broken_connection; 177 // If |bidi_stream_detect_broken_connection_| is true, this suggests the 178 // period of the heartbeat signal. 179 base::TimeDelta heartbeat_interval; 180 181 // Whether Cronet Telemetry should be enabled or not. 182 bool enable_telemetry; 183 ExperimentalOptionsParsingIsAllowedToFailURLRequestContextConfig184 static bool ExperimentalOptionsParsingIsAllowedToFail() { 185 return DCHECK_IS_ON(); 186 } 187 188 static std::unique_ptr<URLRequestContextConfig> CreateURLRequestContextConfig( 189 // Enable QUIC. 190 bool enable_quic, 191 // QUIC User Agent ID. 192 const std::string& quic_user_agent_id, 193 // Enable SPDY. 194 bool enable_spdy, 195 // Enable Brotli. 196 bool enable_brotli, 197 // Type of http cache. 198 HttpCacheType http_cache, 199 // Max size of http cache in bytes. 200 int http_cache_max_size, 201 // Disable caching for HTTP responses. Other information may be stored in 202 // the cache. 203 bool load_disable_cache, 204 // Storage path for http cache and cookie storage. 205 const std::string& storage_path, 206 // Accept-Language request header field. 207 const std::string& accept_language, 208 // User-Agent request header field. 209 const std::string& user_agent, 210 // JSON encoded experimental options. 211 const std::string& unparsed_experimental_options, 212 // MockCertVerifier to use for testing purposes. 213 std::unique_ptr<net::CertVerifier> mock_cert_verifier, 214 // Enable network quality estimator. 215 bool enable_network_quality_estimator, 216 // Enable bypassing of public key pinning for local trust anchors 217 bool bypass_public_key_pinning_for_local_trust_anchors, 218 // Optional network thread priority. 219 // On Android, corresponds to android.os.Process.setThreadPriority() 220 // values. On iOS, corresponds to NSThread::setThreadPriority values. Do 221 // not specify for other targets. 222 absl::optional<double> network_thread_priority); 223 224 private: 225 URLRequestContextConfig( 226 // Enable QUIC. 227 bool enable_quic, 228 // QUIC User Agent ID. 229 const std::string& quic_user_agent_id, 230 // Enable SPDY. 231 bool enable_spdy, 232 // Enable Brotli. 233 bool enable_brotli, 234 // Type of http cache. 235 HttpCacheType http_cache, 236 // Max size of http cache in bytes. 237 int http_cache_max_size, 238 // Disable caching for HTTP responses. Other information may be stored in 239 // the cache. 240 bool load_disable_cache, 241 // Storage path for http cache and cookie storage. 242 const std::string& storage_path, 243 // Accept-Language request header field. 244 const std::string& accept_language, 245 // User-Agent request header field. 246 const std::string& user_agent, 247 // Parsed experimental options. 248 base::Value::Dict experimental_options, 249 // MockCertVerifier to use for testing purposes. 250 std::unique_ptr<net::CertVerifier> mock_cert_verifier, 251 // Enable network quality estimator. 252 bool enable_network_quality_estimator, 253 // Enable bypassing of public key pinning for local trust anchors 254 bool bypass_public_key_pinning_for_local_trust_anchors, 255 // Optional network thread priority. 256 // On Android, corresponds to android.os.Process.setThreadPriority() 257 // values. On iOS, corresponds to NSThread::setThreadPriority values. Do 258 // not specify for other targets. 259 absl::optional<double> network_thread_priority); 260 261 // Parses experimental options from their JSON format to the format used 262 // internally. 263 // Returns an empty optional if the operation was unsuccessful. 264 static absl::optional<base::Value::Dict> ParseExperimentalOptions( 265 std::string unparsed_experimental_options); 266 267 // Makes appropriate changes to settings in |this|. 268 void SetContextConfigExperimentalOptions(); 269 270 // Makes appropriate changes to settings in the URLRequestContextBuilder. 271 void SetContextBuilderExperimentalOptions( 272 net::URLRequestContextBuilder* context_builder, 273 net::HttpNetworkSessionParams* session_params, 274 net::QuicParams* quic_params, 275 net::handles::NetworkHandle bound_network); 276 }; 277 278 // Stores intermediate state for URLRequestContextConfig. Initializes with 279 // (mostly) sane defaults, then the appropriate member variables can be 280 // modified, and it can be finalized with Build(). 281 struct URLRequestContextConfigBuilder { 282 URLRequestContextConfigBuilder(); 283 284 URLRequestContextConfigBuilder(const URLRequestContextConfigBuilder&) = 285 delete; 286 URLRequestContextConfigBuilder& operator=( 287 const URLRequestContextConfigBuilder&) = delete; 288 289 ~URLRequestContextConfigBuilder(); 290 291 // Finalize state into a URLRequestContextConfig. Must only be called once, 292 // as once |mock_cert_verifier| is moved into a URLRequestContextConfig, it 293 // cannot be used again. 294 std::unique_ptr<URLRequestContextConfig> Build(); 295 296 // Enable QUIC. 297 bool enable_quic = true; 298 // QUIC User Agent ID. 299 std::string quic_user_agent_id = ""; 300 // Enable SPDY. 301 bool enable_spdy = true; 302 // Enable Brotli. 303 bool enable_brotli = false; 304 // Type of http cache. 305 URLRequestContextConfig::HttpCacheType http_cache = 306 URLRequestContextConfig::DISABLED; 307 // Max size of http cache in bytes. 308 int http_cache_max_size = 0; 309 // Disable caching for HTTP responses. Other information may be stored in 310 // the cache. 311 bool load_disable_cache = false; 312 // Storage path for http cache and cookie storage. 313 std::string storage_path = ""; 314 // Accept-Language request header field. 315 std::string accept_language = ""; 316 // User-Agent request header field. 317 std::string user_agent = ""; 318 // Experimental options encoded as a string in a JSON format containing 319 // experiments and their corresponding configuration options. The format 320 // is a JSON object with the name of the experiment as the key, and the 321 // configuration options as the value. An example: 322 // {"experiment1": {"option1": "option_value1", "option2": "option_value2", 323 // ...}, "experiment2: {"option3", "option_value3", ...}, ...} 324 std::string experimental_options = "{}"; 325 326 // Certificate verifier for testing. 327 std::unique_ptr<net::CertVerifier> mock_cert_verifier; 328 329 // Enable network quality estimator. 330 bool enable_network_quality_estimator = false; 331 332 // Enable public key pinning bypass for local trust anchors. 333 bool bypass_public_key_pinning_for_local_trust_anchors = true; 334 335 // Optional network thread priority. 336 // On Android, corresponds to android.os.Process.setThreadPriority() values. 337 // On iOS, corresponds to NSThread::setThreadPriority values. 338 // Do not specify for other targets. 339 absl::optional<double> network_thread_priority; 340 }; 341 342 } // namespace cronet 343 344 #endif // COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_ 345