Chromium Code Reviews| Index: components/cronet/android/api/src/org/chromium/net/CronetEngine.java |
| diff --git a/components/cronet/android/api/src/org/chromium/net/CronetEngine.java b/components/cronet/android/api/src/org/chromium/net/CronetEngine.java |
| index 6e7d1e0940ccc2fa4668cc24373cd866e90866ea..92e1782f4e499f551c70052ed6c91a5be1f1008b 100644 |
| --- a/components/cronet/android/api/src/org/chromium/net/CronetEngine.java |
| +++ b/components/cronet/android/api/src/org/chromium/net/CronetEngine.java |
| @@ -8,10 +8,6 @@ import android.content.Context; |
| import android.support.annotation.IntDef; |
| import android.util.Log; |
| -import org.json.JSONArray; |
| -import org.json.JSONException; |
| -import org.json.JSONObject; |
| - |
| import java.io.File; |
| import java.lang.annotation.Retention; |
| import java.lang.annotation.RetentionPolicy; |
| @@ -20,6 +16,7 @@ import java.net.Proxy; |
| import java.net.URL; |
| import java.net.URLConnection; |
| import java.net.URLStreamHandlerFactory; |
| +import java.util.LinkedList; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.concurrent.Executor; |
| @@ -35,15 +32,48 @@ public abstract class CronetEngine { |
| * then {@link #build} is called to create the {@code CronetEngine}. |
| */ |
| public static class Builder { |
| - private final JSONObject mConfig; |
| + // A hint that a host supports QUIC. |
| + static class QuicHint { |
| + // The host. |
| + final String mHost; |
| + // 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.
|
| + final int mPort; |
| + // Another port on which to use QUIC. |
|
xunjieli
2015/12/01 17:30:38
Same here.
pauljensen
2015/12/02 03:50:01
Done.
|
| + final int mAlternatePort; |
| + |
| + QuicHint(String host, int port, int alternatePort) { |
| + mHost = host; |
| + mPort = port; |
| + mAlternatePort = alternatePort; |
| + } |
| + } |
| + |
| + // Private fields are simply storage of configuration for the resulting CronetEngine. |
| + // See setters below for verbose descriptions. |
| private final Context mContext; |
| + private final List<QuicHint> mQuicHints = new LinkedList<QuicHint>(); |
| + private String mUserAgent = ""; |
| + private String mStoragePath = ""; |
| + private boolean mLegacyModeEnabled; |
| + 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
|
| + private boolean mQuicEnabled; |
| + private boolean mHttp2Enabled; |
| + private boolean mSdchEnabled; |
| + private String mDataReductionProxyKey; |
| + private String mDataReductionProxyPrimaryProxy; |
| + private String mDataReductionProxyFallbackProxy; |
| + private String mDataReductionProxySecureProxyCheckUrl; |
| + private boolean mDisableCache; |
| + private int mHttpCacheMode; |
| + private long mHttpCacheMaxSize; |
| + private String mExperimentalOptions; |
| + private long mMockCertVerifier; |
| /** |
| * Default config enables SPDY, disables QUIC, SDCH and HTTP cache. |
| * @param context Android {@link Context} for engine to use. |
| */ |
| public Builder(Context context) { |
| - mConfig = new JSONObject(); |
| mContext = context; |
| enableLegacyMode(false); |
| enableQUIC(false); |
| @@ -67,11 +97,12 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| public Builder setUserAgent(String userAgent) { |
| - return putString(CronetEngineBuilderList.USER_AGENT, userAgent); |
| + mUserAgent = userAgent; |
| + return this; |
| } |
| String getUserAgent() { |
| - return mConfig.optString(CronetEngineBuilderList.USER_AGENT); |
| + return mUserAgent; |
| } |
| /** |
| @@ -90,12 +121,12 @@ public abstract class CronetEngine { |
| throw new IllegalArgumentException( |
| "Storage path must be set to existing directory"); |
| } |
| - |
| - return putString(CronetEngineBuilderList.STORAGE_PATH, value); |
| + mStoragePath = value; |
| + return this; |
| } |
| String storagePath() { |
| - return mConfig.optString(CronetEngineBuilderList.STORAGE_PATH); |
| + return mStoragePath; |
| } |
| /** |
| @@ -107,11 +138,12 @@ public abstract class CronetEngine { |
| */ |
| @Deprecated |
| public Builder enableLegacyMode(boolean value) { |
| - return putBoolean(CronetEngineBuilderList.ENABLE_LEGACY_MODE, value); |
| + mLegacyModeEnabled = value; |
| + return this; |
| } |
| boolean legacyMode() { |
| - return mConfig.optBoolean(CronetEngineBuilderList.ENABLE_LEGACY_MODE); |
| + return mLegacyModeEnabled; |
| } |
| /** |
| @@ -119,11 +151,12 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| Builder setLibraryName(String libName) { |
| - return putString(CronetEngineBuilderList.NATIVE_LIBRARY_NAME, libName); |
| + mLibraryName = libName; |
| + return this; |
| } |
| String libraryName() { |
| - return mConfig.optString(CronetEngineBuilderList.NATIVE_LIBRARY_NAME, "cronet"); |
| + return mLibraryName; |
| } |
| /** |
| @@ -132,7 +165,12 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| public Builder enableQUIC(boolean value) { |
| - return putBoolean(CronetEngineBuilderList.ENABLE_QUIC, value); |
| + mQuicEnabled = value; |
| + return this; |
| + } |
| + |
| + boolean quicEnabled() { |
| + return mQuicEnabled; |
| } |
| /** |
| @@ -141,7 +179,12 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| public Builder enableHTTP2(boolean value) { |
| - return putBoolean(CronetEngineBuilderList.ENABLE_SPDY, value); |
| + mHttp2Enabled = value; |
| + return this; |
| + } |
| + |
| + boolean http2Enabled() { |
| + return mHttp2Enabled; |
| } |
| /** |
| @@ -152,7 +195,12 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| public Builder enableSDCH(boolean value) { |
| - return putBoolean(CronetEngineBuilderList.ENABLE_SDCH, value); |
| + mSdchEnabled = value; |
| + return this; |
| + } |
| + |
| + boolean sdchEnabled() { |
| + return mSdchEnabled; |
| } |
| /** |
| @@ -163,7 +211,12 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| public Builder enableDataReductionProxy(String key) { |
| - return (putString(CronetEngineBuilderList.DATA_REDUCTION_PROXY_KEY, key)); |
| + mDataReductionProxyKey = key; |
| + return this; |
| + } |
| + |
| + String dataReductionProxyKey() { |
| + return mDataReductionProxyKey; |
| } |
| /** |
| @@ -189,13 +242,24 @@ public abstract class CronetEngine { |
| throw new IllegalArgumentException( |
| "Primary and fallback proxies and check url must be set"); |
| } |
| - putString(CronetEngineBuilderList.DATA_REDUCTION_PRIMARY_PROXY, primaryProxy); |
| - putString(CronetEngineBuilderList.DATA_REDUCTION_FALLBACK_PROXY, fallbackProxy); |
| - putString(CronetEngineBuilderList.DATA_REDUCTION_SECURE_PROXY_CHECK_URL, |
| - secureProxyCheckUrl); |
| + mDataReductionProxyPrimaryProxy = primaryProxy; |
| + mDataReductionProxyFallbackProxy = fallbackProxy; |
| + mDataReductionProxySecureProxyCheckUrl = secureProxyCheckUrl; |
| return this; |
| } |
| + String dataReductionProxyPrimaryProxy() { |
| + return mDataReductionProxyPrimaryProxy; |
| + } |
| + |
| + String dataReductionProxyFallbackProxy() { |
| + return mDataReductionProxyFallbackProxy; |
| + } |
| + |
| + String dataReductionProxySecureProxyCheckUrl() { |
| + return mDataReductionProxySecureProxyCheckUrl; |
| + } |
| + |
| /** @deprecated not really deprecated but hidden. */ |
| @IntDef({ |
| HTTP_CACHE_DISABLED, HTTP_CACHE_IN_MEMORY, HTTP_CACHE_DISK_NO_HTTP, HTTP_CACHE_DISK, |
| @@ -249,26 +313,39 @@ public abstract class CronetEngine { |
| throw new IllegalArgumentException("Storage path must be empty"); |
| } |
| } |
| - putBoolean(CronetEngineBuilderList.LOAD_DISABLE_CACHE, |
| - cacheMode == HTTP_CACHE_DISABLED || cacheMode == HTTP_CACHE_DISK_NO_HTTP); |
| - putLong(CronetEngineBuilderList.HTTP_CACHE_MAX_SIZE, maxSize); |
| + mDisableCache = |
| + (cacheMode == HTTP_CACHE_DISABLED || cacheMode == HTTP_CACHE_DISK_NO_HTTP); |
| + mHttpCacheMaxSize = maxSize; |
| switch (cacheMode) { |
| case HTTP_CACHE_DISABLED: |
| - return putString(CronetEngineBuilderList.HTTP_CACHE, |
| - CronetEngineBuilderList.HTTP_CACHE_DISABLED); |
| + mHttpCacheMode = HttpCacheType.DISABLED; |
| + break; |
| case HTTP_CACHE_DISK_NO_HTTP: |
| case HTTP_CACHE_DISK: |
| - return putString(CronetEngineBuilderList.HTTP_CACHE, |
| - CronetEngineBuilderList.HTTP_CACHE_DISK); |
| - |
| + mHttpCacheMode = HttpCacheType.DISK; |
| + break; |
| case HTTP_CACHE_IN_MEMORY: |
| - return putString(CronetEngineBuilderList.HTTP_CACHE, |
| - CronetEngineBuilderList.HTTP_CACHE_MEMORY); |
| + mHttpCacheMode = HttpCacheType.MEMORY; |
| + break; |
| + default: |
| + throw new IllegalArgumentException("Unknown cache mode"); |
| } |
| return this; |
| } |
| + boolean cacheDisabled() { |
| + return mDisableCache; |
| + } |
| + |
| + long httpCacheMaxSize() { |
| + return mHttpCacheMaxSize; |
| + } |
| + |
| + int httpCacheMode() { |
| + return mHttpCacheMode; |
| + } |
| + |
| /** |
| * Adds hint that {@code host} supports QUIC. |
| * Note that {@link #enableHttpCache enableHttpCache} |
| @@ -284,24 +361,14 @@ public abstract class CronetEngine { |
| if (host.contains("/")) { |
| throw new IllegalArgumentException("Illegal QUIC Hint Host: " + host); |
| } |
| - try { |
| - JSONArray quicHints = mConfig.optJSONArray(CronetEngineBuilderList.QUIC_HINTS); |
| - if (quicHints == null) { |
| - quicHints = new JSONArray(); |
| - mConfig.put(CronetEngineBuilderList.QUIC_HINTS, quicHints); |
| - } |
| - |
| - JSONObject hint = new JSONObject(); |
| - hint.put(CronetEngineBuilderList.QUIC_HINT_HOST, host); |
| - hint.put(CronetEngineBuilderList.QUIC_HINT_PORT, port); |
| - hint.put(CronetEngineBuilderList.QUIC_HINT_ALT_PORT, alternatePort); |
| - quicHints.put(hint); |
| - } catch (JSONException e) { |
| - // Intentionally do nothing. |
| - } |
| + mQuicHints.add(new QuicHint(host, port, alternatePort)); |
| return this; |
| } |
| + List<QuicHint> quicHints() { |
| + return mQuicHints; |
| + } |
| + |
| /** |
| * Sets experimental options to be used in Cronet. |
| * |
| @@ -309,22 +376,24 @@ public abstract class CronetEngine { |
| * @return the builder to facilitate chaining. |
| */ |
| public Builder setExperimentalOptions(String options) { |
| - return putString(CronetEngineBuilderList.EXPERIMENTAL_OPTIONS, options); |
| + mExperimentalOptions = options; |
| + return this; |
| + } |
| + |
| + String experimentalOptions() { |
| + return mExperimentalOptions; |
| } |
| /** |
| * Sets a native MockCertVerifier for testing. |
| */ |
| Builder setMockCertVerifierForTesting(long mockCertVerifier) { |
| - return putString( |
| - CronetEngineBuilderList.MOCK_CERT_VERIFIER, String.valueOf(mockCertVerifier)); |
| + mMockCertVerifier = mockCertVerifier; |
| + return this; |
| } |
| - /** |
| - * Gets a JSON string representation of the builder. |
| - */ |
| - String toJSONString() { |
| - return mConfig.toString(); |
| + long mockCertVerifier() { |
| + return mMockCertVerifier; |
| } |
| /** |
| @@ -337,55 +406,13 @@ public abstract class CronetEngine { |
| } |
| /** |
| - * Sets a boolean value in the config. Returns a reference to the same |
| - * config object, so you can chain put calls together. |
| - * @return the builder to facilitate chaining. |
| - */ |
| - private Builder putBoolean(String key, boolean value) { |
| - try { |
| - mConfig.put(key, value); |
| - } catch (JSONException e) { |
| - // Intentionally do nothing. |
| - } |
| - return this; |
| - } |
| - |
| - /** |
| - * Sets a long value in the config. Returns a reference to the same |
| - * config object, so you can chain put calls together. |
| - * @return the builder to facilitate chaining. |
| - */ |
| - private Builder putLong(String key, long value) { |
| - try { |
| - mConfig.put(key, value); |
| - } catch (JSONException e) { |
| - // Intentionally do nothing. |
| - } |
| - return this; |
| - } |
| - |
| - /** |
| - * Sets a string value in the config. Returns a reference to the same |
| - * config object, so you can chain put calls together. |
| - * @return the builder to facilitate chaining. |
| - */ |
| - private Builder putString(String key, String value) { |
| - try { |
| - mConfig.put(key, value); |
| - } catch (JSONException e) { |
| - // Intentionally do nothing. |
| - } |
| - return this; |
| - } |
| - |
| - /** |
| * Build a {@link CronetEngine} using this builder's configuration. |
| */ |
| public CronetEngine build() { |
| CronetEngine engine = createContext(this); |
| // Clear MOCK_CERT_VERIFIER reference if there is any, since |
| // the ownership has been transferred to the engine. |
| - mConfig.remove(CronetEngineBuilderList.MOCK_CERT_VERIFIER); |
| + mMockCertVerifier = 0; |
| return engine; |
| } |
| } |