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 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/optional.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "net/base/hash_value.h"
18 #include "net/cert/cert_verifier.h"
19 #include "net/http/http_network_session.h"
20 #include "net/nqe/effective_connection_type.h"
21 
22 namespace net {
23 class CertVerifier;
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     ~QuicHint();
50 
51     // Host name of the server that supports QUIC.
52     const std::string host;
53     // Port of the server that supports QUIC.
54     const int port;
55     // Alternate protocol port.
56     const int alternate_port;
57 
58    private:
59     DISALLOW_COPY_AND_ASSIGN(QuicHint);
60   };
61 
62   // Public-Key-Pinning configuration structure.
63   struct Pkp {
64     Pkp(const std::string& host,
65         bool include_subdomains,
66         const base::Time& expiration_date);
67     ~Pkp();
68 
69     // Host name.
70     const std::string host;
71     // Pin hashes (currently SHA256 only).
72     net::HashValueVector pin_hashes;
73     // Indicates whether the pinning should apply to the pinned host subdomains.
74     const bool include_subdomains;
75     // Expiration date for the pins.
76     const base::Time expiration_date;
77 
78    private:
79     DISALLOW_COPY_AND_ASSIGN(Pkp);
80   };
81 
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 
97   URLRequestContextConfig(
98       // Enable QUIC.
99       bool enable_quic,
100       // QUIC User Agent ID.
101       const std::string& quic_user_agent_id,
102       // Enable SPDY.
103       bool enable_spdy,
104       // Enable Brotli.
105       bool enable_brotli,
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,
115       // Accept-Language request header field.
116       const std::string& accept_language,
117       // User-Agent request header field.
118       const std::string& user_agent,
119       // JSON encoded experimental options.
120       const std::string& experimental_options,
121       // MockCertVerifier to use for testing purposes.
122       std::unique_ptr<net::CertVerifier> mock_cert_verifier,
123       // Enable network quality estimator.
124       bool enable_network_quality_estimator,
125       // Enable bypassing of public key pinning for local trust anchors
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.
131       base::Optional<double> network_thread_priority);
132   ~URLRequestContextConfig();
133 
134   // Configures |context_builder| based on |this|.
135   void ConfigureURLRequestContextBuilder(
136       net::URLRequestContextBuilder* context_builder);
137 
138   // Enable QUIC.
139   const bool enable_quic;
140   // QUIC User Agent ID.
141   const std::string quic_user_agent_id;
142   // Enable SPDY.
143   const bool enable_spdy;
144   // Enable Brotli.
145   const bool enable_brotli;
146   // Type of http cache.
147   const HttpCacheType http_cache;
148   // Max size of http cache in bytes.
149   const int http_cache_max_size;
150   // Disable caching for HTTP responses. Other information may be stored in
151   // the cache.
152   const bool load_disable_cache;
153   // Storage path for http cache and cookie storage.
154   const std::string storage_path;
155   // Accept-Language request header field.
156   const std::string accept_language;
157   // User-Agent request header field.
158   const std::string user_agent;
159 
160   // Certificate verifier for testing.
161   std::unique_ptr<net::CertVerifier> mock_cert_verifier;
162 
163   // Enable Network Quality Estimator (NQE).
164   const bool enable_network_quality_estimator;
165 
166   // Enable public key pinning bypass for local trust anchors.
167   const bool bypass_public_key_pinning_for_local_trust_anchors;
168 
169   // App-provided list of servers that support QUIC.
170   std::vector<std::unique_ptr<QuicHint>> quic_hints;
171 
172   // The list of public key pins.
173   std::vector<std::unique_ptr<Pkp>> pkp_list;
174 
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 
182   // Experimental options that are recognized by the config parser.
183   std::unique_ptr<base::DictionaryValue> effective_experimental_options =
184       nullptr;
185 
186   // If set, forces NQE to return the set value as the effective connection
187   // type.
188   base::Optional<net::EffectiveConnectionType>
189       nqe_forced_effective_connection_type;
190 
191   // Preloaded Report-To headers, to preconfigure the Reporting API.
192   std::vector<PreloadedNelAndReportingHeader> preloaded_report_to_headers;
193 
194   // Preloaded NEL headers, to preconfigure Network Error Logging.
195   std::vector<PreloadedNelAndReportingHeader> preloaded_nel_headers;
196 
197   // Optional network thread priority.
198   // On Android, corresponds to android.os.Process.setThreadPriority() values.
199   // On iOS, corresponds to NSThread::setThreadPriority values.
200   const base::Optional<double> network_thread_priority;
201 
202  private:
203   // Parses experimental options and makes appropriate changes to settings in
204   // the URLRequestContextConfig and URLRequestContextBuilder.
205   void ParseAndSetExperimentalOptions(
206       net::URLRequestContextBuilder* context_builder,
207       net::HttpNetworkSession::Params* session_params,
208       net::QuicParams* quic_params);
209 
210   // Experimental options encoded as a string in a JSON format containing
211   // experiments and their corresponding configuration options. The format
212   // is a JSON object with the name of the experiment as the key, and the
213   // configuration options as the value. An example:
214   //   {"experiment1": {"option1": "option_value1", "option2":
215   //   "option_value2",
216   //    ...}, "experiment2: {"option3", "option_value3", ...}, ...}
217   const std::string experimental_options;
218 
219   DISALLOW_COPY_AND_ASSIGN(URLRequestContextConfig);
220 };
221 
222 // Stores intermediate state for URLRequestContextConfig.  Initializes with
223 // (mostly) sane defaults, then the appropriate member variables can be
224 // modified, and it can be finalized with Build().
225 struct URLRequestContextConfigBuilder {
226   URLRequestContextConfigBuilder();
227   ~URLRequestContextConfigBuilder();
228 
229   // Finalize state into a URLRequestContextConfig.  Must only be called once,
230   // as once |mock_cert_verifier| is moved into a URLRequestContextConfig, it
231   // cannot be used again.
232   std::unique_ptr<URLRequestContextConfig> Build();
233 
234   // Enable QUIC.
235   bool enable_quic = true;
236   // QUIC User Agent ID.
237   std::string quic_user_agent_id = "";
238   // Enable SPDY.
239   bool enable_spdy = true;
240   // Enable Brotli.
241   bool enable_brotli = false;
242   // Type of http cache.
243   URLRequestContextConfig::HttpCacheType http_cache =
244       URLRequestContextConfig::DISABLED;
245   // Max size of http cache in bytes.
246   int http_cache_max_size = 0;
247   // Disable caching for HTTP responses. Other information may be stored in
248   // the cache.
249   bool load_disable_cache = false;
250   // Storage path for http cache and cookie storage.
251   std::string storage_path = "";
252   // Accept-Language request header field.
253   std::string accept_language = "";
254   // User-Agent request header field.
255   std::string user_agent = "";
256   // Experimental options encoded as a string in a JSON format containing
257   // experiments and their corresponding configuration options. The format
258   // is a JSON object with the name of the experiment as the key, and the
259   // configuration options as the value. An example:
260   //   {"experiment1": {"option1": "option_value1", "option2": "option_value2",
261   //    ...}, "experiment2: {"option3", "option_value3", ...}, ...}
262   std::string experimental_options = "{}";
263 
264   // Certificate verifier for testing.
265   std::unique_ptr<net::CertVerifier> mock_cert_verifier = nullptr;
266 
267   // Enable network quality estimator.
268   bool enable_network_quality_estimator = false;
269 
270   // Enable public key pinning bypass for local trust anchors.
271   bool bypass_public_key_pinning_for_local_trust_anchors = true;
272 
273   // Optional network thread priority.
274   // On Android, corresponds to android.os.Process.setThreadPriority() values.
275   // On iOS, corresponds to NSThread::setThreadPriority values.
276   // Do not specify for other targets.
277   base::Optional<double> network_thread_priority;
278 
279  private:
280   DISALLOW_COPY_AND_ASSIGN(URLRequestContextConfigBuilder);
281 };
282 
283 }  // namespace cronet
284 
285 #endif  // COMPONENTS_CRONET_URL_REQUEST_CONTEXT_CONFIG_H_
286