OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.util.Log; |
| 9 |
| 10 import java.lang.reflect.Constructor; |
| 11 import java.util.concurrent.Executor; |
| 12 |
| 13 /** |
| 14 * A context for {@link UrlRequest}'s, which uses the best HTTP stack |
| 15 * available on the current platform. |
| 16 */ |
| 17 public abstract class UrlRequestContext { |
| 18 private static final String TAG = "UrlRequestFactory"; |
| 19 private static final String CRONET_URL_REQUEST_CONTEXT = |
| 20 "org.chromium.net.CronetUrlRequestContext"; |
| 21 |
| 22 /** |
| 23 * Creates an UrlRequest object. All UrlRequest functions must |
| 24 * be called on the Executor's thread, and all callbacks will be called |
| 25 * on the Executor's thread as well. Executor must not run tasks on the |
| 26 * current thread to prevent network jank and exception during shutdown. |
| 27 * |
| 28 * createRequest itself may be called on any thread. |
| 29 * @param url URL for the request. |
| 30 * @param listener Callback interface that gets called on different events. |
| 31 * @param executor Executor on which all callbacks will be called. |
| 32 * @return new request. |
| 33 */ |
| 34 public abstract UrlRequest createRequest(String url, |
| 35 UrlRequestListener listener, Executor executor); |
| 36 |
| 37 /** |
| 38 * @return true if the context is enabled. |
| 39 */ |
| 40 public abstract boolean isEnabled(); |
| 41 |
| 42 /** |
| 43 * @return a human-readable version string of the context. |
| 44 */ |
| 45 public abstract String getVersionString(); |
| 46 |
| 47 /** |
| 48 * Shuts down the UrlRequestContext if there are no active requests, |
| 49 * otherwise throws an exception. |
| 50 * Cannot be called on network thread - the thread Cronet calls into |
| 51 * Executor on (which is different from the thread the Executor invokes |
| 52 * callbacks on). May block until all the Context's resources have been |
| 53 * cleaned up. |
| 54 */ |
| 55 public abstract void shutdown(); |
| 56 |
| 57 /** |
| 58 * Create context with given config. If config.legacyMode is true, or |
| 59 * native library is not available, then creates HttpUrlConnection-based |
| 60 * context. |
| 61 * @param context application context. |
| 62 * @param config context configuration. |
| 63 */ |
| 64 public static UrlRequestContext createContext(Context context, |
| 65 UrlRequestContextConfig config) { |
| 66 UrlRequestContext urlRequestContext = null; |
| 67 if (config.userAgent().isEmpty()) { |
| 68 config.setUserAgent(UserAgent.from(context)); |
| 69 } |
| 70 if (!config.legacyMode()) { |
| 71 urlRequestContext = createCronetContext(context, config); |
| 72 } |
| 73 if (urlRequestContext == null) { |
| 74 // TODO(mef): Fallback to stub implementation. Once stub |
| 75 // implementation is available merge with createCronetFactory. |
| 76 urlRequestContext = createCronetContext(context, config); |
| 77 } |
| 78 Log.i(TAG, "Using network stack: " |
| 79 + urlRequestContext.getVersionString()); |
| 80 return urlRequestContext; |
| 81 } |
| 82 |
| 83 private static UrlRequestContext createCronetContext(Context context, |
| 84 UrlRequestContextConfig config) { |
| 85 UrlRequestContext urlRequestContext = null; |
| 86 try { |
| 87 Class<? extends UrlRequestContext> contextClass = |
| 88 UrlRequestContext.class.getClassLoader() |
| 89 .loadClass(CRONET_URL_REQUEST_CONTEXT) |
| 90 .asSubclass(UrlRequestContext.class); |
| 91 Constructor<? extends UrlRequestContext> constructor = |
| 92 contextClass.getConstructor( |
| 93 Context.class, UrlRequestContextConfig.class); |
| 94 UrlRequestContext cronetContext = |
| 95 constructor.newInstance(context, config); |
| 96 if (cronetContext.isEnabled()) { |
| 97 urlRequestContext = cronetContext; |
| 98 } |
| 99 } catch (ClassNotFoundException e) { |
| 100 // Leave as null. |
| 101 } catch (Exception e) { |
| 102 throw new IllegalStateException( |
| 103 "Cannot instantiate: " + CRONET_URL_REQUEST_CONTEXT, |
| 104 e); |
| 105 } |
| 106 return urlRequestContext; |
| 107 } |
| 108 } |
OLD | NEW |