Index: components/cronet/android/java/src/org/chromium/net/UrlRequestFactory.java |
diff --git a/components/cronet/android/java/src/org/chromium/net/UrlRequestFactory.java b/components/cronet/android/java/src/org/chromium/net/UrlRequestFactory.java |
index 2b08e7d99472e8a07a813d78446f6f608797ebb8..0d9a518eee946b15b78b9feb8891f36606e55086 100644 |
--- a/components/cronet/android/java/src/org/chromium/net/UrlRequestFactory.java |
+++ b/components/cronet/android/java/src/org/chromium/net/UrlRequestFactory.java |
@@ -4,13 +4,21 @@ |
package org.chromium.net; |
+import android.content.Context; |
+import android.util.Log; |
+ |
+import java.lang.reflect.Constructor; |
import java.util.concurrent.Executor; |
/** |
* A factory for {@link UrlRequest}'s, which uses the best HTTP stack |
* available on the current platform. |
*/ |
-public interface UrlRequestFactory { |
+public abstract class UrlRequestFactory { |
+ private static final String TAG = "UrlRequestFactory"; |
+ private static final String CRONET_URL_REQUEST_CONTEXT = |
+ "org.chromium.net.CronetUrlRequestContext"; |
+ |
/** |
* Creates an UrlRequest object. All UrlRequest functions must |
* be called on the Executor's thread, and all callbacks will be called |
@@ -23,4 +31,58 @@ public interface UrlRequestFactory { |
*/ |
public abstract UrlRequest createRequest(String url, |
UrlRequestListener listener, Executor executor); |
+ |
+ public static UrlRequestFactory createFactory(Context context, |
+ String userAgent, HttpUrlRequestFactoryConfig config) { |
+ UrlRequestFactory factory = null; |
+ if (config.userAgent().isEmpty()) |
xunjieli
2014/10/03 20:58:39
nit: brackets according to http://source.android.c
mef
2014/10/06 14:52:43
Done.
|
+ config.setUserAgent(UserAgent.from(context)); |
+ |
+ if (!config.legacyMode()) { |
+ factory = createCronetFactory(context, config); |
+ } |
+ if (factory == null) { |
+ // TODO(mef): Fallback to stub implementation. |
+ factory = createCronetFactory(context, config); |
+ } |
+ Log.i(TAG, "Using network stack: " + factory.getName()); |
+ return factory; |
+ } |
+ |
+ /** |
+ * @return true if the factory is enabled. |
+ */ |
+ public abstract boolean isEnabled(); |
+ |
+ /** |
+ * @return a human-readable name of the factory. |
+ */ |
+ public abstract String getName(); |
+ |
+ private static UrlRequestFactory createCronetFactory(Context context, |
+ HttpUrlRequestFactoryConfig config) { |
+ UrlRequestFactory factory = null; |
+ try { |
+ Class<? extends UrlRequestFactory> factoryClass = |
+ UrlRequestFactory.class.getClassLoader(). |
+ loadClass(CRONET_URL_REQUEST_CONTEXT). |
+ asSubclass(UrlRequestFactory.class); |
+ Constructor<? extends UrlRequestFactory> constructor = |
+ factoryClass.getConstructor( |
+ Context.class, HttpUrlRequestFactoryConfig.class); |
+ UrlRequestFactory cronetFactory = |
+ constructor.newInstance(context, config); |
+ if (cronetFactory.isEnabled()) { |
+ factory = cronetFactory; |
+ } |
+ } catch (ClassNotFoundException e) { |
+ // Leave as null |
+ } catch (Exception e) { |
+ throw new IllegalStateException( |
+ "Cannot instantiate: " + |
+ CRONET_URL_REQUEST_CONTEXT, |
+ e); |
+ } |
+ return factory; |
+ } |
} |