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. |
| 26 * createRequest itself may be called on any thread. |
| 27 * @param url URL for the request. |
| 28 * @param listener Callback interface that gets called on different events. |
| 29 * @param executor Executor on which all callbacks will be called. |
| 30 * @return new request. |
| 31 */ |
| 32 public abstract UrlRequest createRequest(String url, |
| 33 UrlRequestListener listener, Executor executor); |
| 34 |
| 35 /** |
| 36 * @return true if the context is enabled. |
| 37 */ |
| 38 public abstract boolean isEnabled(); |
| 39 |
| 40 /** |
| 41 * @return a human-readable name of the context. |
| 42 */ |
| 43 public abstract String getName(); |
| 44 |
| 45 /** |
| 46 * Create context with given config. If config.legacyMode is true, or |
| 47 * native library is not available, then creates HttpUrlConnection-based |
| 48 * context. |
| 49 * @param context application context. |
| 50 * @param config context configuration. |
| 51 */ |
| 52 public static UrlRequestContext createContext(Context context, |
| 53 UrlRequestContextConfig config) { |
| 54 UrlRequestContext urlRequestContext = null; |
| 55 if (config.userAgent().isEmpty()) { |
| 56 config.setUserAgent(UserAgent.from(context)); |
| 57 } |
| 58 if (!config.legacyMode()) { |
| 59 urlRequestContext = createCronetContext(context, config); |
| 60 } |
| 61 if (urlRequestContext == null) { |
| 62 // TODO(mef): Fallback to stub implementation. Once stub |
| 63 // implementation is available merge with createCronetFactory. |
| 64 urlRequestContext = createCronetContext(context, config); |
| 65 } |
| 66 Log.i(TAG, "Using network stack: " + urlRequestContext.getName()); |
| 67 return urlRequestContext; |
| 68 } |
| 69 |
| 70 private static UrlRequestContext createCronetContext(Context context, |
| 71 UrlRequestContextConfig config) { |
| 72 UrlRequestContext urlRequestContext = null; |
| 73 try { |
| 74 Class<? extends UrlRequestContext> contextClass = |
| 75 UrlRequestContext.class.getClassLoader(). |
| 76 loadClass(CRONET_URL_REQUEST_CONTEXT). |
| 77 asSubclass(UrlRequestContext.class); |
| 78 Constructor<? extends UrlRequestContext> constructor = |
| 79 contextClass.getConstructor( |
| 80 Context.class, UrlRequestContextConfig.class); |
| 81 UrlRequestContext cronetContext = |
| 82 constructor.newInstance(context, config); |
| 83 if (cronetContext.isEnabled()) { |
| 84 urlRequestContext = cronetContext; |
| 85 } |
| 86 } catch (ClassNotFoundException e) { |
| 87 // Leave as null. |
| 88 } catch (Exception e) { |
| 89 throw new IllegalStateException( |
| 90 "Cannot instantiate: " + |
| 91 CRONET_URL_REQUEST_CONTEXT, |
| 92 e); |
| 93 } |
| 94 return urlRequestContext; |
| 95 } |
| 96 } |
OLD | NEW |