xref: /aosp_15_r20/external/cronet/components/cronet/url_request_context_config.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <optional>
10 #include <string>
11 #include <vector>
12 
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "net/base/hash_value.h"
16 #include "net/base/network_handle.h"
17 #include "net/cert/cert_verifier.h"
18 #include "net/nqe/effective_connection_type.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   std::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 std::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 
ExperimentalOptionsParsingIsAllowedToFailURLRequestContextConfig178   static bool ExperimentalOptionsParsingIsAllowedToFail() {
179     return DCHECK_IS_ON();
180   }
181 
182   static std::unique_ptr<URLRequestContextConfig> CreateURLRequestContextConfig(
183       // Enable QUIC.
184       bool enable_quic,
185       // Enable SPDY.
186       bool enable_spdy,
187       // Enable Brotli.
188       bool enable_brotli,
189       // Type of http cache.
190       HttpCacheType http_cache,
191       // Max size of http cache in bytes.
192       int http_cache_max_size,
193       // Disable caching for HTTP responses. Other information may be stored in
194       // the cache.
195       bool load_disable_cache,
196       // Storage path for http cache and cookie storage.
197       const std::string& storage_path,
198       // Accept-Language request header field.
199       const std::string& accept_language,
200       // User-Agent request header field.
201       const std::string& user_agent,
202       // JSON encoded experimental options.
203       const std::string& unparsed_experimental_options,
204       // MockCertVerifier to use for testing purposes.
205       std::unique_ptr<net::CertVerifier> mock_cert_verifier,
206       // Enable network quality estimator.
207       bool enable_network_quality_estimator,
208       // Enable bypassing of public key pinning for local trust anchors
209       bool bypass_public_key_pinning_for_local_trust_anchors,
210       // Optional network thread priority.
211       // On Android, corresponds to android.os.Process.setThreadPriority()
212       // values. Do not specify for other targets.
213       std::optional<double> network_thread_priority);
214 
215  private:
216   URLRequestContextConfig(
217       // Enable QUIC.
218       bool enable_quic,
219       // Enable SPDY.
220       bool enable_spdy,
221       // Enable Brotli.
222       bool enable_brotli,
223       // Type of http cache.
224       HttpCacheType http_cache,
225       // Max size of http cache in bytes.
226       int http_cache_max_size,
227       // Disable caching for HTTP responses. Other information may be stored in
228       // the cache.
229       bool load_disable_cache,
230       // Storage path for http cache and cookie storage.
231       const std::string& storage_path,
232       // Accept-Language request header field.
233       const std::string& accept_language,
234       // User-Agent request header field.
235       const std::string& user_agent,
236       // Parsed experimental options.
237       base::Value::Dict experimental_options,
238       // MockCertVerifier to use for testing purposes.
239       std::unique_ptr<net::CertVerifier> mock_cert_verifier,
240       // Enable network quality estimator.
241       bool enable_network_quality_estimator,
242       // Enable bypassing of public key pinning for local trust anchors
243       bool bypass_public_key_pinning_for_local_trust_anchors,
244       // Optional network thread priority.
245       // On Android, corresponds to android.os.Process.setThreadPriority()
246       // values. Do not specify for other targets.
247       std::optional<double> network_thread_priority);
248 
249   // Parses experimental options from their JSON format to the format used
250   // internally.
251   // Returns an empty optional if the operation was unsuccessful.
252   static std::optional<base::Value::Dict> ParseExperimentalOptions(
253       std::string unparsed_experimental_options);
254 
255   // Makes appropriate changes to settings in |this|.
256   void SetContextConfigExperimentalOptions();
257 
258   // Makes appropriate changes to settings in the URLRequestContextBuilder.
259   void SetContextBuilderExperimentalOptions(
260       net::URLRequestContextBuilder* context_builder,
261       net::HttpNetworkSessionParams* session_params,
262       net::QuicParams* quic_params,
263       net::handles::NetworkHandle bound_network);
264 };
265 
266 // Stores intermediate state for URLRequestContextConfig.  Initializes with
267 // (mostly) sane defaults, then the appropriate member variables can be
268 // modified, and it can be finalized with Build().
269 struct URLRequestContextConfigBuilder {
270   URLRequestContextConfigBuilder();
271 
272   URLRequestContextConfigBuilder(const URLRequestContextConfigBuilder&) =
273       delete;
274   URLRequestContextConfigBuilder& operator=(
275       const URLRequestContextConfigBuilder&) = delete;
276 
277   ~URLRequestContextConfigBuilder();
278 
279   // Finalize state into a URLRequestContextConfig.  Must only be called once,
280   // as once |mock_cert_verifier| is moved into a URLRequestContextConfig, it
281   // cannot be used again.
282   std::unique_ptr<URLRequestContextConfig> Build();
283 
284   // Enable QUIC.
285   bool enable_quic = true;
286   // Enable SPDY.
287   bool enable_spdy = true;
288   // Enable Brotli.
289   bool enable_brotli = false;
290   // Type of http cache.
291   URLRequestContextConfig::HttpCacheType http_cache =
292       URLRequestContextConfig::DISABLED;
293   // Max size of http cache in bytes.
294   int http_cache_max_size = 0;
295   // Disable caching for HTTP responses. Other information may be stored in
296   // the cache.
297   bool load_disable_cache = false;
298   // Storage path for http cache and cookie storage.
299   std::string storage_path = "";
300   // Accept-Language request header field.
301   std::string accept_language = "";
302   // User-Agent request header field.
303   std::string user_agent = "";
304   // Experimental options encoded as a string in a JSON format containing
305   // experiments and their corresponding configuration options. The format
306   // is a JSON object with the name of the experiment as the key, and the
307   // configuration options as the value. An example:
308   //   {"experiment1": {"option1": "option_value1", "option2": "option_value2",
309   //    ...}, "experiment2: {"option3", "option_value3", ...}, ...}
310   std::string experimental_options = "{}";
311 
312   // Certificate verifier for testing.
313   std::unique_ptr<net::CertVerifier> mock_cert_verifier;
314 
315   // Enable network quality estimator.
316   bool enable_network_quality_estimator = false;
317 
318   // Enable public key pinning bypass for local trust anchors.
319   bool bypass_public_key_pinning_for_local_trust_anchors = true;
320 
321   // Optional network thread priority.
322   // On Android, corresponds to android.os.Process.setThreadPriority() values.
323   // Do not specify for other targets.
324   std::optional<double> network_thread_priority;
325 };
326 
327 }  // namespace cronet
328 
329 #endif  // COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_
330