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