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