Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/UrlRequestContext.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/UrlRequestContext.java b/components/cronet/android/java/src/org/chromium/net/UrlRequestContext.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..701b6c148e24bf18129218e4c5c9c8a6ef5f976f |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/UrlRequestContext.java |
| @@ -0,0 +1,101 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net; |
| + |
| +import android.content.Context; |
| +import android.util.Log; |
| + |
| +import java.lang.reflect.Constructor; |
| +import java.util.concurrent.Executor; |
| + |
| +/** |
| + * A context for {@link UrlRequest}'s, which uses the best HTTP stack |
| + * available on the current platform. |
| + */ |
| +public abstract class UrlRequestContext { |
| + 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 |
| + * on the Executor's thread as well. |
| + * createRequest itself may be called on any thread. |
| + * @param url URL for the request. |
| + * @param listener Callback interface that gets called on different events. |
| + * @param executor Executor on which all callbacks will be called. |
|
mmenke
2014/11/03 17:13:16
Worth mentioning that Executor must not just run t
mef
2014/11/03 21:23:37
Could you elaborate? What's wrong with shutdown?
mmenke
2014/11/03 21:42:06
It can't be called on the network thread.
mef
2014/11/03 23:27:00
Done.
|
| + * @return new request. |
| + */ |
| + public abstract UrlRequest createRequest(String url, |
| + UrlRequestListener listener, Executor executor); |
| + |
| + /** |
| + * @return true if the context is enabled. |
| + */ |
| + public abstract boolean isEnabled(); |
| + |
| + /** |
| + * @return a human-readable name of the context. |
|
mmenke
2014/11/03 17:13:16
name of -> version string for
mef
2014/11/03 21:23:37
Done.
|
| + */ |
| + public abstract String getName(); |
|
mmenke
2014/11/03 17:13:16
I think getVersionString is much clearer. "Name"
mef
2014/11/03 21:23:37
Done.
|
| + |
| + /** |
| + * Shutdown the UrlRequestContext if there are no active requests, otherwise |
| + * throw an exception. |
| + */ |
| + public abstract void shutdown(); |
| + |
| + /** |
| + * Create context with given config. If config.legacyMode is true, or |
| + * native library is not available, then creates HttpUrlConnection-based |
| + * context. |
| + * @param context application context. |
| + * @param config context configuration. |
| + */ |
| + public static UrlRequestContext createContext(Context context, |
| + UrlRequestContextConfig config) { |
| + UrlRequestContext urlRequestContext = null; |
| + if (config.userAgent().isEmpty()) { |
| + config.setUserAgent(UserAgent.from(context)); |
| + } |
| + if (!config.legacyMode()) { |
| + urlRequestContext = createCronetContext(context, config); |
| + } |
| + if (urlRequestContext == null) { |
| + // TODO(mef): Fallback to stub implementation. Once stub |
| + // implementation is available merge with createCronetFactory. |
| + urlRequestContext = createCronetContext(context, config); |
| + } |
| + Log.i(TAG, "Using network stack: " + urlRequestContext.getName()); |
| + return urlRequestContext; |
| + } |
| + |
| + private static UrlRequestContext createCronetContext(Context context, |
| + UrlRequestContextConfig config) { |
| + UrlRequestContext urlRequestContext = null; |
| + try { |
| + Class<? extends UrlRequestContext> contextClass = |
| + UrlRequestContext.class.getClassLoader() |
| + .loadClass(CRONET_URL_REQUEST_CONTEXT) |
| + .asSubclass(UrlRequestContext.class); |
| + Constructor<? extends UrlRequestContext> constructor = |
| + contextClass.getConstructor( |
| + Context.class, UrlRequestContextConfig.class); |
| + UrlRequestContext cronetContext = |
| + constructor.newInstance(context, config); |
| + if (cronetContext.isEnabled()) { |
| + urlRequestContext = cronetContext; |
| + } |
| + } catch (ClassNotFoundException e) { |
| + // Leave as null. |
| + } catch (Exception e) { |
| + throw new IllegalStateException( |
| + "Cannot instantiate: " + CRONET_URL_REQUEST_CONTEXT, |
| + e); |
| + } |
| + return urlRequestContext; |
| + } |
| +} |