Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/UrlRequestContextConfig.java

Issue 1363723002: [Cronet] Create Builders, rename UrlRequestContext to CronetEngine (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update Ben's tests Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import org.json.JSONArray;
8 import org.json.JSONException; 7 import org.json.JSONException;
9 import org.json.JSONObject;
10
11 import java.io.File;
12 8
13 /** 9 /**
14 * A config for UrlRequestContext, which allows runtime configuration of 10 * A config for CronetEngine, which allows runtime configuration of
15 * UrlRequestContext. 11 * CronetEngine.
12 * @deprecated use {@link CronetEngine.Builder} instead.
16 */ 13 */
17 public class UrlRequestContextConfig { 14 public class UrlRequestContextConfig extends CronetEngine.Builder {
18 private final JSONObject mConfig;
19
20 /**
21 * Default config enables SPDY, disables QUIC, SDCH and http cache.
22 */
23 public UrlRequestContextConfig() { 15 public UrlRequestContextConfig() {
24 mConfig = new JSONObject(); 16 // Context will be passed in later when the ChromiumUrlRequestFactory
25 enableLegacyMode(false); 17 // or ChromiumUrlRequestContext is created.
26 enableQUIC(false); 18 super(null);
27 enableHTTP2(true);
28 enableSDCH(false);
29 enableHttpCache(HTTP_CACHE_DISABLED, 0);
30 } 19 }
31 20
32 /**
33 * Creates a config from a JSON string, which was serialized using
34 * {@link #toString}.
35 */
36 public UrlRequestContextConfig(String json) throws JSONException { 21 public UrlRequestContextConfig(String json) throws JSONException {
37 mConfig = new JSONObject(json); 22 // Context will be passed in later when the ChromiumUrlRequestFactory
38 } 23 // or ChromiumUrlRequestContext is created.
39 24 super(null, json);
40 /**
41 * Overrides the user-agent header for all requests.
42 * @return the config to facilitate chaining.
43 */
44 public UrlRequestContextConfig setUserAgent(String userAgent) {
45 return putString(UrlRequestContextConfigList.USER_AGENT, userAgent);
46 }
47
48 String userAgent() {
49 return mConfig.optString(UrlRequestContextConfigList.USER_AGENT);
50 }
51
52 /**
53 * Sets directory for HTTP Cache and Cookie Storage. The directory must
54 * exist.
55 * @param value path to existing directory.
56 * @return the config to facilitate chaining.
57 */
58 public UrlRequestContextConfig setStoragePath(String value) {
59 if (!new File(value).isDirectory()) {
60 throw new IllegalArgumentException(
61 "Storage path must be set to existing directory");
62 }
63
64 return putString(UrlRequestContextConfigList.STORAGE_PATH, value);
65 }
66
67 String storagePath() {
68 return mConfig.optString(UrlRequestContextConfigList.STORAGE_PATH);
69 }
70
71 /**
72 * Sets whether falling back to implementation based on system's
73 * {@link java.net.HttpURLConnection} implementation is enabled.
74 * Defaults to disabled.
75 * @return the config to facilitate chaining.
76 * @deprecated Not supported by the new API.
77 */
78 @Deprecated
79 public UrlRequestContextConfig enableLegacyMode(boolean value) {
80 return putBoolean(UrlRequestContextConfigList.ENABLE_LEGACY_MODE,
81 value);
82 }
83
84 boolean legacyMode() {
85 return mConfig.optBoolean(
86 UrlRequestContextConfigList.ENABLE_LEGACY_MODE);
87 }
88
89 /**
90 * Overrides the name of the native library backing Cronet.
91 * @return the config to facilitate chaining.
92 */
93 UrlRequestContextConfig setLibraryName(String libName) {
94 return putString(UrlRequestContextConfigList.NATIVE_LIBRARY_NAME,
95 libName);
96 }
97
98 String libraryName() {
99 return mConfig.optString(
100 UrlRequestContextConfigList.NATIVE_LIBRARY_NAME, "cronet");
101 }
102
103 /**
104 * Sets whether <a href="https://www.chromium.org/quic">QUIC</a> protocol
105 * is enabled. Defaults to disabled.
106 * @return the config to facilitate chaining.
107 */
108 public UrlRequestContextConfig enableQUIC(boolean value) {
109 return putBoolean(UrlRequestContextConfigList.ENABLE_QUIC, value);
110 }
111
112 /**
113 * Sets whether <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a>
114 * protocol is enabled. Defaults to enabled.
115 * @return the config to facilitate chaining.
116 */
117 public UrlRequestContextConfig enableHTTP2(boolean value) {
118 return putBoolean(UrlRequestContextConfigList.ENABLE_SPDY, value);
119 }
120
121 /**
122 * Sets whether
123 * <a
124 * href="https://lists.w3.org/Archives/Public/ietf-http-wg/2008JulSep/att-04 41/Shared_Dictionary_Compression_over_HTTP.pdf">
125 * SDCH</a> compression is enabled. Defaults to disabled.
126 * @return the config to facilitate chaining.
127 */
128 public UrlRequestContextConfig enableSDCH(boolean value) {
129 return putBoolean(UrlRequestContextConfigList.ENABLE_SDCH, value);
130 }
131
132 /**
133 * Enables
134 * <a href="https://developer.chrome.com/multidevice/data-compression">Data
135 * Reduction Proxy</a>. Defaults to disabled.
136 * @param key key to use when authenticating with the proxy.
137 * @return the config to facilitate chaining.
138 */
139 public UrlRequestContextConfig enableDataReductionProxy(String key) {
140 return (putString(
141 UrlRequestContextConfigList.DATA_REDUCTION_PROXY_KEY, key));
142 }
143
144 /**
145 * Overrides
146 * <a href="https://developer.chrome.com/multidevice/data-compression">
147 * Data Reduction Proxy</a> configuration parameters with a primary
148 * proxy name, fallback proxy name, and a secure proxy check URL. Proxies
149 * are specified as [scheme://]host[:port]. Used for testing.
150 * @param primaryProxy the primary data reduction proxy to use.
151 * @param fallbackProxy a fallback data reduction proxy to use.
152 * @param secureProxyCheckUrl a URL to fetch to determine if using a secure
153 * proxy is allowed.
154 * @return the config to facilitate chaining.
155 * @hide
156 */
157 public UrlRequestContextConfig setDataReductionProxyOptions(
158 String primaryProxy,
159 String fallbackProxy,
160 String secureProxyCheckUrl) {
161 if (primaryProxy.isEmpty() || fallbackProxy.isEmpty()
162 || secureProxyCheckUrl.isEmpty()) {
163 throw new IllegalArgumentException(
164 "Primary and fallback proxies and check url must be set");
165 }
166 putString(UrlRequestContextConfigList.DATA_REDUCTION_PRIMARY_PROXY,
167 primaryProxy);
168 putString(UrlRequestContextConfigList.DATA_REDUCTION_FALLBACK_PROXY,
169 fallbackProxy);
170 putString(UrlRequestContextConfigList
171 .DATA_REDUCTION_SECURE_PROXY_CHECK_URL, secureProxyCheckUrl);
172 return this;
173 }
174
175 /**
176 * Setting to disable HTTP cache. Some data may still be temporarily stored in memory.
177 * Passed to {@link #enableHttpCache}.
178 */
179 public static final int HTTP_CACHE_DISABLED = 0;
180
181 /**
182 * Setting to enable in-memory HTTP cache, including HTTP data.
183 * Passed to {@link #enableHttpCache}.
184 */
185 public static final int HTTP_CACHE_IN_MEMORY = 1;
186
187 /**
188 * Setting to enable on-disk cache, excluding HTTP data.
189 * {@link #setStoragePath} must be called prior to passing this constant to
190 * {@link #enableHttpCache}.
191 */
192 public static final int HTTP_CACHE_DISK_NO_HTTP = 2;
193
194 /**
195 * Setting to enable on-disk cache, including HTTP data.
196 * {@link #setStoragePath} must be called prior to passing this constant to
197 * {@link #enableHttpCache}.
198 */
199 public static final int HTTP_CACHE_DISK = 3;
200
201 /**
202 * Enables or disables caching of HTTP data and other information like QUIC
203 * server information.
204 * @param cacheMode control location and type of cached data.
205 * @param maxSize maximum size used to cache data (advisory and maybe
206 * exceeded at times).
207 * @return the config to facilitate chaining.
208 */
209 public UrlRequestContextConfig enableHttpCache(int cacheMode, long maxSize) {
210 if (cacheMode == HTTP_CACHE_DISK || cacheMode == HTTP_CACHE_DISK_NO_HTTP ) {
211 if (storagePath().isEmpty()) {
212 throw new IllegalArgumentException("Storage path must be set");
213 }
214 } else {
215 if (!storagePath().isEmpty()) {
216 throw new IllegalArgumentException(
217 "Storage path must be empty");
218 }
219 }
220 putBoolean(UrlRequestContextConfigList.LOAD_DISABLE_CACHE,
221 cacheMode == HTTP_CACHE_DISABLED || cacheMode == HTTP_CACHE_DISK _NO_HTTP);
222 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE, maxSize);
223
224 switch (cacheMode) {
225 case HTTP_CACHE_DISABLED:
226 return putString(UrlRequestContextConfigList.HTTP_CACHE,
227 UrlRequestContextConfigList.HTTP_CACHE_DISABLED);
228 case HTTP_CACHE_DISK_NO_HTTP:
229 case HTTP_CACHE_DISK:
230 return putString(UrlRequestContextConfigList.HTTP_CACHE,
231 UrlRequestContextConfigList.HTTP_CACHE_DISK);
232
233 case HTTP_CACHE_IN_MEMORY:
234 return putString(UrlRequestContextConfigList.HTTP_CACHE,
235 UrlRequestContextConfigList.HTTP_CACHE_MEMORY);
236 }
237 return this;
238 }
239
240 /**
241 * Adds hint that {@code host} supports QUIC.
242 * Note that {@link #enableHttpCache enableHttpCache}
243 * ({@link HttpCache#DISK DISK}) is needed to take advantage of 0-RTT
244 * connection establishment between sessions.
245 *
246 * @param host hostname of the server that supports QUIC.
247 * @param port host of the server that supports QUIC.
248 * @param alternatePort alternate port to use for QUIC.
249 * @return the config to facilitate chaining.
250 */
251 public UrlRequestContextConfig addQuicHint(String host,
252 int port,
253 int alternatePort) {
254 if (host.contains("/")) {
255 throw new IllegalArgumentException("Illegal QUIC Hint Host: "
256 + host);
257 }
258 try {
259 JSONArray quicHints = mConfig.optJSONArray(
260 UrlRequestContextConfigList.QUIC_HINTS);
261 if (quicHints == null) {
262 quicHints = new JSONArray();
263 mConfig.put(UrlRequestContextConfigList.QUIC_HINTS, quicHints);
264 }
265
266 JSONObject hint = new JSONObject();
267 hint.put(UrlRequestContextConfigList.QUIC_HINT_HOST, host);
268 hint.put(UrlRequestContextConfigList.QUIC_HINT_PORT, port);
269 hint.put(UrlRequestContextConfigList.QUIC_HINT_ALT_PORT,
270 alternatePort);
271 quicHints.put(hint);
272 } catch (JSONException e) {
273 // Intentionally do nothing.
274 }
275 return this;
276 }
277
278 /**
279 * Sets experimental QUIC connection options, overwriting any pre-existing
280 * options. List of options is subject to change.
281 *
282 * @param quicConnectionOptions comma-separated QUIC options (for example
283 * "PACE,IW10") to use if QUIC is enabled.
284 * @return the config to facilitate chaining.
285 */
286 public UrlRequestContextConfig setExperimentalQuicConnectionOptions(
287 String quicConnectionOptions) {
288 return putString(UrlRequestContextConfigList.QUIC_OPTIONS,
289 quicConnectionOptions);
290 }
291
292 /**
293 * Get JSON string representation of the config.
294 */
295 @Override
296 public String toString() {
297 return mConfig.toString();
298 }
299
300 /**
301 * Sets a boolean value in the config. Returns a reference to the same
302 * config object, so you can chain put calls together.
303 * @return the config to facilitate chaining.
304 */
305 private UrlRequestContextConfig putBoolean(String key, boolean value) {
306 try {
307 mConfig.put(key, value);
308 } catch (JSONException e) {
309 // Intentionally do nothing.
310 }
311 return this;
312 }
313
314 /**
315 * Sets a long value in the config. Returns a reference to the same
316 * config object, so you can chain put calls together.
317 * @return the config to facilitate chaining.
318 */
319 private UrlRequestContextConfig putLong(String key, long value) {
320 try {
321 mConfig.put(key, value);
322 } catch (JSONException e) {
323 // Intentionally do nothing.
324 }
325 return this;
326 }
327
328 /**
329 * Sets a string value in the config. Returns a reference to the same
330 * config object, so you can chain put calls together.
331 * @return the config to facilitate chaining.
332 */
333 private UrlRequestContextConfig putString(String key, String value) {
334 try {
335 mConfig.put(key, value);
336 } catch (JSONException e) {
337 // Intentionally do nothing.
338 }
339 return this;
340 } 25 }
341 } 26 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698