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

Unified Diff: components/cronet/android/api/src/org/chromium/net/CronetEngine.java

Issue 1429863008: [Cronet] Remove JSON serialization of CronetEngine.Builder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove TextUtils.isEmpty where sensical Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..1bcebfde1675d12d6d44a493edec03aebff0da22 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,16 +32,50 @@ 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;
+ // Port of the server that supports QUIC.
+ final int mPort;
+ // Alternate protocol port.
+ 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;
+ 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;
+ setLibraryName("cronet");
enableLegacyMode(false);
enableQUIC(false);
enableHTTP2(true);
@@ -67,11 +98,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 +122,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 +139,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 +152,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 +166,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 +180,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 +196,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 +212,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 +243,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,
@@ -241,34 +306,47 @@ public abstract class CronetEngine {
*/
public Builder enableHttpCache(@HttpCacheSetting int cacheMode, long maxSize) {
if (cacheMode == HTTP_CACHE_DISK || cacheMode == HTTP_CACHE_DISK_NO_HTTP) {
- if (storagePath().isEmpty()) {
+ 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.
throw new IllegalArgumentException("Storage path must be set");
}
} else {
- if (!storagePath().isEmpty()) {
+ if (storagePath() != null) {
throw new IllegalArgumentException("Storage path must be empty");
xunjieli 2015/12/02 18:55:35 nit: maybe change "Storage path must be empty" ->
pauljensen 2015/12/03 18:51:45 Done.
}
}
- 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 +362,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 +377,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 +407,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;
}
}
@@ -661,7 +689,7 @@ public abstract class CronetEngine {
@Deprecated
public static CronetEngine createContext(Builder builder) {
CronetEngine cronetEngine = null;
- if (builder.getUserAgent().isEmpty()) {
+ if (builder.getUserAgent() == null) {
builder.setUserAgent(builder.getDefaultUserAgent());
}
if (!builder.legacyMode()) {
« no previous file with comments | « components/cronet.gypi ('k') | components/cronet/android/api/src/org/chromium/net/HttpUrlConnectionUrlRequestFactory.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698