OLD | NEW |
---|---|
1 // Copyright 2015 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 android.content.Context; | 7 import android.content.Context; |
8 import android.support.annotation.IntDef; | 8 import android.support.annotation.IntDef; |
9 import android.text.TextUtils; | |
9 import android.util.Log; | 10 import android.util.Log; |
10 | 11 |
11 import org.json.JSONArray; | |
12 import org.json.JSONException; | |
13 import org.json.JSONObject; | |
14 | |
15 import java.io.File; | 12 import java.io.File; |
16 import java.lang.annotation.Retention; | 13 import java.lang.annotation.Retention; |
17 import java.lang.annotation.RetentionPolicy; | 14 import java.lang.annotation.RetentionPolicy; |
18 import java.lang.reflect.Constructor; | 15 import java.lang.reflect.Constructor; |
19 import java.net.Proxy; | 16 import java.net.Proxy; |
20 import java.net.URL; | 17 import java.net.URL; |
21 import java.net.URLConnection; | 18 import java.net.URLConnection; |
22 import java.net.URLStreamHandlerFactory; | 19 import java.net.URLStreamHandlerFactory; |
20 import java.util.LinkedList; | |
23 import java.util.List; | 21 import java.util.List; |
24 import java.util.Map; | 22 import java.util.Map; |
25 import java.util.concurrent.Executor; | 23 import java.util.concurrent.Executor; |
26 | 24 |
27 /** | 25 /** |
28 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack | 26 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack |
29 * available on the current platform. | 27 * available on the current platform. |
30 */ | 28 */ |
31 public abstract class CronetEngine { | 29 public abstract class CronetEngine { |
32 /** | 30 /** |
33 * A builder for {@link CronetEngine}s, which allows runtime configuration o f | 31 * A builder for {@link CronetEngine}s, which allows runtime configuration o f |
34 * {@code CronetEngine}. Configuration options are set on the builder and | 32 * {@code CronetEngine}. Configuration options are set on the builder and |
35 * then {@link #build} is called to create the {@code CronetEngine}. | 33 * then {@link #build} is called to create the {@code CronetEngine}. |
36 */ | 34 */ |
37 public static class Builder { | 35 public static class Builder { |
38 private final JSONObject mConfig; | 36 // A hint that a host supports QUIC. |
37 static class QuicHint { | |
38 // The host. | |
39 final String mHost; | |
40 // Port of the server that supports QUIC. | |
41 final int mPort; | |
42 // Alternate protocol port. | |
43 final int mAlternatePort; | |
44 | |
45 QuicHint(String host, int port, int alternatePort) { | |
46 mHost = host; | |
47 mPort = port; | |
48 mAlternatePort = alternatePort; | |
49 } | |
50 } | |
51 | |
52 // Private fields are simply storage of configuration for the resulting CronetEngine. | |
53 // See setters below for verbose descriptions. | |
39 private final Context mContext; | 54 private final Context mContext; |
55 private final List<QuicHint> mQuicHints = new LinkedList<QuicHint>(); | |
56 private String mUserAgent; | |
57 private String mStoragePath; | |
58 private boolean mLegacyModeEnabled; | |
59 private String mLibraryName; | |
60 private boolean mQuicEnabled; | |
61 private boolean mHttp2Enabled; | |
62 private boolean mSdchEnabled; | |
63 private String mDataReductionProxyKey; | |
64 private String mDataReductionProxyPrimaryProxy; | |
65 private String mDataReductionProxyFallbackProxy; | |
66 private String mDataReductionProxySecureProxyCheckUrl; | |
67 private boolean mDisableCache; | |
68 private int mHttpCacheMode; | |
69 private long mHttpCacheMaxSize; | |
70 private String mExperimentalOptions; | |
71 private long mMockCertVerifier; | |
40 | 72 |
41 /** | 73 /** |
42 * Default config enables SPDY, disables QUIC, SDCH and HTTP cache. | 74 * Default config enables SPDY, disables QUIC, SDCH and HTTP cache. |
43 * @param context Android {@link Context} for engine to use. | 75 * @param context Android {@link Context} for engine to use. |
44 */ | 76 */ |
45 public Builder(Context context) { | 77 public Builder(Context context) { |
46 mConfig = new JSONObject(); | |
47 mContext = context; | 78 mContext = context; |
79 setLibraryName("cronet"); | |
48 enableLegacyMode(false); | 80 enableLegacyMode(false); |
49 enableQUIC(false); | 81 enableQUIC(false); |
50 enableHTTP2(true); | 82 enableHTTP2(true); |
51 enableSDCH(false); | 83 enableSDCH(false); |
52 enableHttpCache(HTTP_CACHE_DISABLED, 0); | 84 enableHttpCache(HTTP_CACHE_DISABLED, 0); |
53 } | 85 } |
54 | 86 |
55 /** | 87 /** |
56 * Constructs a User-Agent string including Cronet version, and | 88 * Constructs a User-Agent string including Cronet version, and |
57 * application name and version. | 89 * application name and version. |
58 * | 90 * |
59 * @return User-Agent string. | 91 * @return User-Agent string. |
60 */ | 92 */ |
61 public String getDefaultUserAgent() { | 93 public String getDefaultUserAgent() { |
62 return UserAgent.from(mContext); | 94 return UserAgent.from(mContext); |
63 } | 95 } |
64 | 96 |
65 /** | 97 /** |
66 * Overrides the User-Agent header for all requests. | 98 * Overrides the User-Agent header for all requests. |
67 * @return the builder to facilitate chaining. | 99 * @return the builder to facilitate chaining. |
68 */ | 100 */ |
69 public Builder setUserAgent(String userAgent) { | 101 public Builder setUserAgent(String userAgent) { |
70 return putString(CronetEngineBuilderList.USER_AGENT, userAgent); | 102 mUserAgent = userAgent; |
103 return this; | |
71 } | 104 } |
72 | 105 |
73 String getUserAgent() { | 106 String getUserAgent() { |
74 return mConfig.optString(CronetEngineBuilderList.USER_AGENT); | 107 return mUserAgent; |
75 } | 108 } |
76 | 109 |
77 /** | 110 /** |
78 * Sets directory for HTTP Cache and Cookie Storage. The directory must | 111 * Sets directory for HTTP Cache and Cookie Storage. The directory must |
79 * exist. | 112 * exist. |
80 * <p> | 113 * <p> |
81 * <b>NOTE:</b> Do not use the same storage directory with more than one | 114 * <b>NOTE:</b> Do not use the same storage directory with more than one |
82 * {@code CronetEngine} at a time. Access to the storage directory does | 115 * {@code CronetEngine} at a time. Access to the storage directory does |
83 * not support concurrent access by multiple {@code CronetEngine}s. | 116 * not support concurrent access by multiple {@code CronetEngine}s. |
84 * | 117 * |
85 * @param value path to existing directory. | 118 * @param value path to existing directory. |
86 * @return the builder to facilitate chaining. | 119 * @return the builder to facilitate chaining. |
87 */ | 120 */ |
88 public Builder setStoragePath(String value) { | 121 public Builder setStoragePath(String value) { |
89 if (!new File(value).isDirectory()) { | 122 if (!new File(value).isDirectory()) { |
90 throw new IllegalArgumentException( | 123 throw new IllegalArgumentException( |
91 "Storage path must be set to existing directory"); | 124 "Storage path must be set to existing directory"); |
92 } | 125 } |
93 | 126 mStoragePath = value; |
94 return putString(CronetEngineBuilderList.STORAGE_PATH, value); | 127 return this; |
95 } | 128 } |
96 | 129 |
97 String storagePath() { | 130 String storagePath() { |
98 return mConfig.optString(CronetEngineBuilderList.STORAGE_PATH); | 131 return mStoragePath; |
99 } | 132 } |
100 | 133 |
101 /** | 134 /** |
102 * Sets whether falling back to implementation based on system's | 135 * Sets whether falling back to implementation based on system's |
103 * {@link java.net.HttpURLConnection} implementation is enabled. | 136 * {@link java.net.HttpURLConnection} implementation is enabled. |
104 * Defaults to disabled. | 137 * Defaults to disabled. |
105 * @return the builder to facilitate chaining. | 138 * @return the builder to facilitate chaining. |
106 * @deprecated Not supported by the new API. | 139 * @deprecated Not supported by the new API. |
107 */ | 140 */ |
108 @Deprecated | 141 @Deprecated |
109 public Builder enableLegacyMode(boolean value) { | 142 public Builder enableLegacyMode(boolean value) { |
110 return putBoolean(CronetEngineBuilderList.ENABLE_LEGACY_MODE, value) ; | 143 mLegacyModeEnabled = value; |
144 return this; | |
111 } | 145 } |
112 | 146 |
113 boolean legacyMode() { | 147 boolean legacyMode() { |
114 return mConfig.optBoolean(CronetEngineBuilderList.ENABLE_LEGACY_MODE ); | 148 return mLegacyModeEnabled; |
115 } | 149 } |
116 | 150 |
117 /** | 151 /** |
118 * Overrides the name of the native library backing Cronet. | 152 * Overrides the name of the native library backing Cronet. |
119 * @return the builder to facilitate chaining. | 153 * @return the builder to facilitate chaining. |
120 */ | 154 */ |
121 Builder setLibraryName(String libName) { | 155 Builder setLibraryName(String libName) { |
122 return putString(CronetEngineBuilderList.NATIVE_LIBRARY_NAME, libNam e); | 156 mLibraryName = libName; |
157 return this; | |
123 } | 158 } |
124 | 159 |
125 String libraryName() { | 160 String libraryName() { |
126 return mConfig.optString(CronetEngineBuilderList.NATIVE_LIBRARY_NAME , "cronet"); | 161 return mLibraryName; |
127 } | 162 } |
128 | 163 |
129 /** | 164 /** |
130 * Sets whether <a href="https://www.chromium.org/quic">QUIC</a> protoco l | 165 * Sets whether <a href="https://www.chromium.org/quic">QUIC</a> protoco l |
131 * is enabled. Defaults to disabled. | 166 * is enabled. Defaults to disabled. |
132 * @return the builder to facilitate chaining. | 167 * @return the builder to facilitate chaining. |
133 */ | 168 */ |
134 public Builder enableQUIC(boolean value) { | 169 public Builder enableQUIC(boolean value) { |
135 return putBoolean(CronetEngineBuilderList.ENABLE_QUIC, value); | 170 mQuicEnabled = value; |
171 return this; | |
172 } | |
173 | |
174 boolean quicEnabled() { | |
175 return mQuicEnabled; | |
136 } | 176 } |
137 | 177 |
138 /** | 178 /** |
139 * Sets whether <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> | 179 * Sets whether <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> |
140 * protocol is enabled. Defaults to enabled. | 180 * protocol is enabled. Defaults to enabled. |
141 * @return the builder to facilitate chaining. | 181 * @return the builder to facilitate chaining. |
142 */ | 182 */ |
143 public Builder enableHTTP2(boolean value) { | 183 public Builder enableHTTP2(boolean value) { |
144 return putBoolean(CronetEngineBuilderList.ENABLE_SPDY, value); | 184 mHttp2Enabled = value; |
185 return this; | |
186 } | |
187 | |
188 boolean http2Enabled() { | |
189 return mHttp2Enabled; | |
145 } | 190 } |
146 | 191 |
147 /** | 192 /** |
148 * Sets whether | 193 * Sets whether |
149 * <a | 194 * <a |
150 * href="https://lists.w3.org/Archives/Public/ietf-http-wg/2008JulSep/at t-0441/Shared_Dictionary_Compression_over_HTTP.pdf"> | 195 * href="https://lists.w3.org/Archives/Public/ietf-http-wg/2008JulSep/at t-0441/Shared_Dictionary_Compression_over_HTTP.pdf"> |
151 * SDCH</a> compression is enabled. Defaults to disabled. | 196 * SDCH</a> compression is enabled. Defaults to disabled. |
152 * @return the builder to facilitate chaining. | 197 * @return the builder to facilitate chaining. |
153 */ | 198 */ |
154 public Builder enableSDCH(boolean value) { | 199 public Builder enableSDCH(boolean value) { |
155 return putBoolean(CronetEngineBuilderList.ENABLE_SDCH, value); | 200 mSdchEnabled = value; |
201 return this; | |
202 } | |
203 | |
204 boolean sdchEnabled() { | |
205 return mSdchEnabled; | |
156 } | 206 } |
157 | 207 |
158 /** | 208 /** |
159 * Enables | 209 * Enables |
160 * <a href="https://developer.chrome.com/multidevice/data-compression">D ata | 210 * <a href="https://developer.chrome.com/multidevice/data-compression">D ata |
161 * Reduction Proxy</a>. Defaults to disabled. | 211 * Reduction Proxy</a>. Defaults to disabled. |
162 * @param key key to use when authenticating with the proxy. | 212 * @param key key to use when authenticating with the proxy. |
163 * @return the builder to facilitate chaining. | 213 * @return the builder to facilitate chaining. |
164 */ | 214 */ |
165 public Builder enableDataReductionProxy(String key) { | 215 public Builder enableDataReductionProxy(String key) { |
166 return (putString(CronetEngineBuilderList.DATA_REDUCTION_PROXY_KEY, key)); | 216 mDataReductionProxyKey = key; |
217 return this; | |
218 } | |
219 | |
220 String dataReductionProxyKey() { | |
221 return mDataReductionProxyKey; | |
167 } | 222 } |
168 | 223 |
169 /** | 224 /** |
170 * Overrides | 225 * Overrides |
171 * <a href="https://developer.chrome.com/multidevice/data-compression"> | 226 * <a href="https://developer.chrome.com/multidevice/data-compression"> |
172 * Data Reduction Proxy</a> configuration parameters with a primary | 227 * Data Reduction Proxy</a> configuration parameters with a primary |
173 * proxy name, fallback proxy name, and a secure proxy check URL. Proxie s | 228 * proxy name, fallback proxy name, and a secure proxy check URL. Proxie s |
174 * are specified as [scheme://]host[:port]. Used for testing. | 229 * are specified as [scheme://]host[:port]. Used for testing. |
175 * @param primaryProxy the primary data reduction proxy to use. | 230 * @param primaryProxy the primary data reduction proxy to use. |
176 * @param fallbackProxy a fallback data reduction proxy to use. | 231 * @param fallbackProxy a fallback data reduction proxy to use. |
177 * @param secureProxyCheckUrl a URL to fetch to determine if using a sec ure | 232 * @param secureProxyCheckUrl a URL to fetch to determine if using a sec ure |
178 * proxy is allowed. | 233 * proxy is allowed. |
179 * @return the builder to facilitate chaining. | 234 * @return the builder to facilitate chaining. |
180 * @hide | 235 * @hide |
181 * @deprecated Marked as deprecated because @hide doesn't properly hide but | 236 * @deprecated Marked as deprecated because @hide doesn't properly hide but |
182 * javadocs are built with nodeprecated="yes". | 237 * javadocs are built with nodeprecated="yes". |
183 */ | 238 */ |
184 @SuppressWarnings("DepAnn") | 239 @SuppressWarnings("DepAnn") |
185 public Builder setDataReductionProxyOptions( | 240 public Builder setDataReductionProxyOptions( |
186 String primaryProxy, String fallbackProxy, String secureProxyChe ckUrl) { | 241 String primaryProxy, String fallbackProxy, String secureProxyChe ckUrl) { |
187 if (primaryProxy.isEmpty() || fallbackProxy.isEmpty() | 242 if (primaryProxy.isEmpty() || fallbackProxy.isEmpty() |
188 || secureProxyCheckUrl.isEmpty()) { | 243 || secureProxyCheckUrl.isEmpty()) { |
189 throw new IllegalArgumentException( | 244 throw new IllegalArgumentException( |
190 "Primary and fallback proxies and check url must be set" ); | 245 "Primary and fallback proxies and check url must be set" ); |
191 } | 246 } |
192 putString(CronetEngineBuilderList.DATA_REDUCTION_PRIMARY_PROXY, prim aryProxy); | 247 mDataReductionProxyPrimaryProxy = primaryProxy; |
193 putString(CronetEngineBuilderList.DATA_REDUCTION_FALLBACK_PROXY, fal lbackProxy); | 248 mDataReductionProxyFallbackProxy = fallbackProxy; |
194 putString(CronetEngineBuilderList.DATA_REDUCTION_SECURE_PROXY_CHECK_ URL, | 249 mDataReductionProxySecureProxyCheckUrl = secureProxyCheckUrl; |
195 secureProxyCheckUrl); | |
196 return this; | 250 return this; |
197 } | 251 } |
198 | 252 |
253 String dataReductionProxyPrimaryProxy() { | |
254 return mDataReductionProxyPrimaryProxy; | |
255 } | |
256 | |
257 String dataReductionProxyFallbackProxy() { | |
258 return mDataReductionProxyFallbackProxy; | |
259 } | |
260 | |
261 String dataReductionProxySecureProxyCheckUrl() { | |
262 return mDataReductionProxySecureProxyCheckUrl; | |
263 } | |
264 | |
199 /** @deprecated not really deprecated but hidden. */ | 265 /** @deprecated not really deprecated but hidden. */ |
200 @IntDef({ | 266 @IntDef({ |
201 HTTP_CACHE_DISABLED, HTTP_CACHE_IN_MEMORY, HTTP_CACHE_DISK_NO_HT TP, HTTP_CACHE_DISK, | 267 HTTP_CACHE_DISABLED, HTTP_CACHE_IN_MEMORY, HTTP_CACHE_DISK_NO_HT TP, HTTP_CACHE_DISK, |
202 }) | 268 }) |
203 @Retention(RetentionPolicy.SOURCE) | 269 @Retention(RetentionPolicy.SOURCE) |
204 @SuppressWarnings("DepAnn") | 270 @SuppressWarnings("DepAnn") |
205 public @interface HttpCacheSetting {} | 271 public @interface HttpCacheSetting {} |
206 | 272 |
207 /** | 273 /** |
208 * Setting to disable HTTP cache. Some data may still be temporarily sto red in memory. | 274 * Setting to disable HTTP cache. Some data may still be temporarily sto red in memory. |
(...skipping 25 matching lines...) Expand all Loading... | |
234 * Enables or disables caching of HTTP data and other information like Q UIC | 300 * Enables or disables caching of HTTP data and other information like Q UIC |
235 * server information. | 301 * server information. |
236 * @param cacheMode control location and type of cached data. Must be on e of | 302 * @param cacheMode control location and type of cached data. Must be on e of |
237 * {@link #HTTP_CACHE_DISABLED HTTP_CACHE_*}. | 303 * {@link #HTTP_CACHE_DISABLED HTTP_CACHE_*}. |
238 * @param maxSize maximum size in bytes used to cache data (advisory and maybe | 304 * @param maxSize maximum size in bytes used to cache data (advisory and maybe |
239 * exceeded at times). | 305 * exceeded at times). |
240 * @return the builder to facilitate chaining. | 306 * @return the builder to facilitate chaining. |
241 */ | 307 */ |
242 public Builder enableHttpCache(@HttpCacheSetting int cacheMode, long max Size) { | 308 public Builder enableHttpCache(@HttpCacheSetting int cacheMode, long max Size) { |
243 if (cacheMode == HTTP_CACHE_DISK || cacheMode == HTTP_CACHE_DISK_NO_ HTTP) { | 309 if (cacheMode == HTTP_CACHE_DISK || cacheMode == HTTP_CACHE_DISK_NO_ HTTP) { |
244 if (storagePath().isEmpty()) { | 310 if (TextUtils.isEmpty(storagePath())) { |
xunjieli
2015/12/02 13:06:46
I don't understand why we need TextUtils.isEmpty.
pauljensen
2015/12/02 18:11:09
Done. I was using TextUtils.isEmpty() to match th
| |
245 throw new IllegalArgumentException("Storage path must be set "); | 311 throw new IllegalArgumentException("Storage path must be set "); |
246 } | 312 } |
247 } else { | 313 } else { |
248 if (!storagePath().isEmpty()) { | 314 if (!TextUtils.isEmpty(storagePath())) { |
249 throw new IllegalArgumentException("Storage path must be emp ty"); | 315 throw new IllegalArgumentException("Storage path must be emp ty"); |
250 } | 316 } |
251 } | 317 } |
252 putBoolean(CronetEngineBuilderList.LOAD_DISABLE_CACHE, | 318 mDisableCache = |
253 cacheMode == HTTP_CACHE_DISABLED || cacheMode == HTTP_CACHE_ DISK_NO_HTTP); | 319 (cacheMode == HTTP_CACHE_DISABLED || cacheMode == HTTP_CACHE _DISK_NO_HTTP); |
254 putLong(CronetEngineBuilderList.HTTP_CACHE_MAX_SIZE, maxSize); | 320 mHttpCacheMaxSize = maxSize; |
255 | 321 |
256 switch (cacheMode) { | 322 switch (cacheMode) { |
257 case HTTP_CACHE_DISABLED: | 323 case HTTP_CACHE_DISABLED: |
258 return putString(CronetEngineBuilderList.HTTP_CACHE, | 324 mHttpCacheMode = HttpCacheType.DISABLED; |
259 CronetEngineBuilderList.HTTP_CACHE_DISABLED); | 325 break; |
260 case HTTP_CACHE_DISK_NO_HTTP: | 326 case HTTP_CACHE_DISK_NO_HTTP: |
261 case HTTP_CACHE_DISK: | 327 case HTTP_CACHE_DISK: |
262 return putString(CronetEngineBuilderList.HTTP_CACHE, | 328 mHttpCacheMode = HttpCacheType.DISK; |
263 CronetEngineBuilderList.HTTP_CACHE_DISK); | 329 break; |
264 | |
265 case HTTP_CACHE_IN_MEMORY: | 330 case HTTP_CACHE_IN_MEMORY: |
266 return putString(CronetEngineBuilderList.HTTP_CACHE, | 331 mHttpCacheMode = HttpCacheType.MEMORY; |
267 CronetEngineBuilderList.HTTP_CACHE_MEMORY); | 332 break; |
333 default: | |
334 throw new IllegalArgumentException("Unknown cache mode"); | |
268 } | 335 } |
269 return this; | 336 return this; |
270 } | 337 } |
271 | 338 |
339 boolean cacheDisabled() { | |
340 return mDisableCache; | |
341 } | |
342 | |
343 long httpCacheMaxSize() { | |
344 return mHttpCacheMaxSize; | |
345 } | |
346 | |
347 int httpCacheMode() { | |
348 return mHttpCacheMode; | |
349 } | |
350 | |
272 /** | 351 /** |
273 * Adds hint that {@code host} supports QUIC. | 352 * Adds hint that {@code host} supports QUIC. |
274 * Note that {@link #enableHttpCache enableHttpCache} | 353 * Note that {@link #enableHttpCache enableHttpCache} |
275 * ({@link #HTTP_CACHE_DISK}) is needed to take advantage of 0-RTT | 354 * ({@link #HTTP_CACHE_DISK}) is needed to take advantage of 0-RTT |
276 * connection establishment between sessions. | 355 * connection establishment between sessions. |
277 * | 356 * |
278 * @param host hostname of the server that supports QUIC. | 357 * @param host hostname of the server that supports QUIC. |
279 * @param port host of the server that supports QUIC. | 358 * @param port host of the server that supports QUIC. |
280 * @param alternatePort alternate port to use for QUIC. | 359 * @param alternatePort alternate port to use for QUIC. |
281 * @return the builder to facilitate chaining. | 360 * @return the builder to facilitate chaining. |
282 */ | 361 */ |
283 public Builder addQuicHint(String host, int port, int alternatePort) { | 362 public Builder addQuicHint(String host, int port, int alternatePort) { |
284 if (host.contains("/")) { | 363 if (host.contains("/")) { |
285 throw new IllegalArgumentException("Illegal QUIC Hint Host: " + host); | 364 throw new IllegalArgumentException("Illegal QUIC Hint Host: " + host); |
286 } | 365 } |
287 try { | 366 mQuicHints.add(new QuicHint(host, port, alternatePort)); |
288 JSONArray quicHints = mConfig.optJSONArray(CronetEngineBuilderLi st.QUIC_HINTS); | 367 return this; |
289 if (quicHints == null) { | 368 } |
290 quicHints = new JSONArray(); | |
291 mConfig.put(CronetEngineBuilderList.QUIC_HINTS, quicHints); | |
292 } | |
293 | 369 |
294 JSONObject hint = new JSONObject(); | 370 List<QuicHint> quicHints() { |
295 hint.put(CronetEngineBuilderList.QUIC_HINT_HOST, host); | 371 return mQuicHints; |
296 hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); | |
297 hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePo rt); | |
298 quicHints.put(hint); | |
299 } catch (JSONException e) { | |
300 // Intentionally do nothing. | |
301 } | |
302 return this; | |
303 } | 372 } |
304 | 373 |
305 /** | 374 /** |
306 * Sets experimental options to be used in Cronet. | 375 * Sets experimental options to be used in Cronet. |
307 * | 376 * |
308 * @param options JSON formatted experimental options. | 377 * @param options JSON formatted experimental options. |
309 * @return the builder to facilitate chaining. | 378 * @return the builder to facilitate chaining. |
310 */ | 379 */ |
311 public Builder setExperimentalOptions(String options) { | 380 public Builder setExperimentalOptions(String options) { |
312 return putString(CronetEngineBuilderList.EXPERIMENTAL_OPTIONS, optio ns); | 381 mExperimentalOptions = options; |
382 return this; | |
383 } | |
384 | |
385 String experimentalOptions() { | |
386 return mExperimentalOptions; | |
313 } | 387 } |
314 | 388 |
315 /** | 389 /** |
316 * Sets a native MockCertVerifier for testing. | 390 * Sets a native MockCertVerifier for testing. |
317 */ | 391 */ |
318 Builder setMockCertVerifierForTesting(long mockCertVerifier) { | 392 Builder setMockCertVerifierForTesting(long mockCertVerifier) { |
319 return putString( | 393 mMockCertVerifier = mockCertVerifier; |
320 CronetEngineBuilderList.MOCK_CERT_VERIFIER, String.valueOf(m ockCertVerifier)); | 394 return this; |
395 } | |
396 | |
397 long mockCertVerifier() { | |
398 return mMockCertVerifier; | |
321 } | 399 } |
322 | 400 |
323 /** | 401 /** |
324 * Gets a JSON string representation of the builder. | |
325 */ | |
326 String toJSONString() { | |
327 return mConfig.toString(); | |
328 } | |
329 | |
330 /** | |
331 * Returns {@link Context} for builder. | 402 * Returns {@link Context} for builder. |
332 * | 403 * |
333 * @return {@link Context} for builder. | 404 * @return {@link Context} for builder. |
334 */ | 405 */ |
335 Context getContext() { | 406 Context getContext() { |
336 return mContext; | 407 return mContext; |
337 } | 408 } |
338 | 409 |
339 /** | 410 /** |
340 * Sets a boolean value in the config. Returns a reference to the same | |
341 * config object, so you can chain put calls together. | |
342 * @return the builder to facilitate chaining. | |
343 */ | |
344 private Builder putBoolean(String key, boolean value) { | |
345 try { | |
346 mConfig.put(key, value); | |
347 } catch (JSONException e) { | |
348 // Intentionally do nothing. | |
349 } | |
350 return this; | |
351 } | |
352 | |
353 /** | |
354 * Sets a long value in the config. Returns a reference to the same | |
355 * config object, so you can chain put calls together. | |
356 * @return the builder to facilitate chaining. | |
357 */ | |
358 private Builder putLong(String key, long value) { | |
359 try { | |
360 mConfig.put(key, value); | |
361 } catch (JSONException e) { | |
362 // Intentionally do nothing. | |
363 } | |
364 return this; | |
365 } | |
366 | |
367 /** | |
368 * Sets a string value in the config. Returns a reference to the same | |
369 * config object, so you can chain put calls together. | |
370 * @return the builder to facilitate chaining. | |
371 */ | |
372 private Builder putString(String key, String value) { | |
373 try { | |
374 mConfig.put(key, value); | |
375 } catch (JSONException e) { | |
376 // Intentionally do nothing. | |
377 } | |
378 return this; | |
379 } | |
380 | |
381 /** | |
382 * Build a {@link CronetEngine} using this builder's configuration. | 411 * Build a {@link CronetEngine} using this builder's configuration. |
383 */ | 412 */ |
384 public CronetEngine build() { | 413 public CronetEngine build() { |
385 CronetEngine engine = createContext(this); | 414 CronetEngine engine = createContext(this); |
386 // Clear MOCK_CERT_VERIFIER reference if there is any, since | 415 // Clear MOCK_CERT_VERIFIER reference if there is any, since |
387 // the ownership has been transferred to the engine. | 416 // the ownership has been transferred to the engine. |
388 mConfig.remove(CronetEngineBuilderList.MOCK_CERT_VERIFIER); | 417 mMockCertVerifier = 0; |
389 return engine; | 418 return engine; |
390 } | 419 } |
391 } | 420 } |
392 | 421 |
393 private static final String TAG = "UrlRequestFactory"; | 422 private static final String TAG = "UrlRequestFactory"; |
394 private static final String CRONET_URL_REQUEST_CONTEXT = | 423 private static final String CRONET_URL_REQUEST_CONTEXT = |
395 "org.chromium.net.CronetUrlRequestContext"; | 424 "org.chromium.net.CronetUrlRequestContext"; |
396 | 425 |
397 /** | 426 /** |
398 * Creates a {@link UrlRequest} object. All callbacks will | 427 * Creates a {@link UrlRequest} object. All callbacks will |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
654 /** | 683 /** |
655 * Creates a {@link CronetEngine} with the given {@link Builder}. | 684 * Creates a {@link CronetEngine} with the given {@link Builder}. |
656 * | 685 * |
657 * @param builder builder to used for creating the CronetEngine instance. | 686 * @param builder builder to used for creating the CronetEngine instance. |
658 * @return the created CronetEngine instance. | 687 * @return the created CronetEngine instance. |
659 * @deprecated Use {@link CronetEngine.Builder}. | 688 * @deprecated Use {@link CronetEngine.Builder}. |
660 */ | 689 */ |
661 @Deprecated | 690 @Deprecated |
662 public static CronetEngine createContext(Builder builder) { | 691 public static CronetEngine createContext(Builder builder) { |
663 CronetEngine cronetEngine = null; | 692 CronetEngine cronetEngine = null; |
664 if (builder.getUserAgent().isEmpty()) { | 693 if (TextUtils.isEmpty(builder.getUserAgent())) { |
xunjieli
2015/12/02 13:06:46
Same here. Can we do null test? If the embedder wa
pauljensen
2015/12/02 18:11:08
Done. I was using TextUtils.isEmpty() to match th
| |
665 builder.setUserAgent(builder.getDefaultUserAgent()); | 694 builder.setUserAgent(builder.getDefaultUserAgent()); |
666 } | 695 } |
667 if (!builder.legacyMode()) { | 696 if (!builder.legacyMode()) { |
668 cronetEngine = createCronetEngine(builder); | 697 cronetEngine = createCronetEngine(builder); |
669 } | 698 } |
670 if (cronetEngine == null) { | 699 if (cronetEngine == null) { |
671 // TODO(mef): Fallback to stub implementation. Once stub | 700 // TODO(mef): Fallback to stub implementation. Once stub |
672 // implementation is available merge with createCronetFactory. | 701 // implementation is available merge with createCronetFactory. |
673 cronetEngine = createCronetEngine(builder); | 702 cronetEngine = createCronetEngine(builder); |
674 } | 703 } |
(...skipping 15 matching lines...) Expand all Loading... | |
690 cronetEngine = possibleEngine; | 719 cronetEngine = possibleEngine; |
691 } | 720 } |
692 } catch (ClassNotFoundException e) { | 721 } catch (ClassNotFoundException e) { |
693 // Leave as null. | 722 // Leave as null. |
694 } catch (Exception e) { | 723 } catch (Exception e) { |
695 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); | 724 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e); |
696 } | 725 } |
697 return cronetEngine; | 726 return cronetEngine; |
698 } | 727 } |
699 } | 728 } |
OLD | NEW |