Chromium Code Reviews| 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..4cf94476c94284f8b2877b67331e984b5481dd00 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, |
|
mmenke
2014/10/06 18:28:45
Should have a comment here.
mmenke
2014/10/06 18:28:45
Don't think we really get anything by having two f
mef
2014/10/07 00:45:18
Done.
mef
2014/10/21 21:27:47
Done.
|
| + String userAgent, HttpUrlRequestFactoryConfig config) { |
| + UrlRequestFactory factory = null; |
| + if (config.userAgent().isEmpty()) { |
| + 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 |
|
mmenke
2014/10/06 18:28:45
nit: +.
mmenke
2014/10/06 18:28:45
Should we even have this case for now, or just thr
mef
2014/10/21 21:27:47
Done.
|
| + } catch (Exception e) { |
| + throw new IllegalStateException( |
| + "Cannot instantiate: " + |
| + CRONET_URL_REQUEST_CONTEXT, |
| + e); |
| + } |
| + return factory; |
| + } |
| } |