Chromium Code Reviews| 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. | |
|
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.
| |
| 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. | |
|
mmenke
2014/11/03 17:13:16
name of -> version string for
mef
2014/11/03 21:23:37
Done.
| |
| 42 */ | |
| 43 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.
| |
| 44 | |
| 45 /** | |
| 46 * Shutdown the UrlRequestContext if there are no active requests, otherwise | |
| 47 * throw an exception. | |
| 48 */ | |
| 49 public abstract void shutdown(); | |
| 50 | |
| 51 /** | |
| 52 * Create context with given config. If config.legacyMode is true, or | |
| 53 * native library is not available, then creates HttpUrlConnection-based | |
| 54 * context. | |
| 55 * @param context application context. | |
| 56 * @param config context configuration. | |
| 57 */ | |
| 58 public static UrlRequestContext createContext(Context context, | |
| 59 UrlRequestContextConfig config) { | |
| 60 UrlRequestContext urlRequestContext = null; | |
| 61 if (config.userAgent().isEmpty()) { | |
| 62 config.setUserAgent(UserAgent.from(context)); | |
| 63 } | |
| 64 if (!config.legacyMode()) { | |
| 65 urlRequestContext = createCronetContext(context, config); | |
| 66 } | |
| 67 if (urlRequestContext == null) { | |
| 68 // TODO(mef): Fallback to stub implementation. Once stub | |
| 69 // implementation is available merge with createCronetFactory. | |
| 70 urlRequestContext = createCronetContext(context, config); | |
| 71 } | |
| 72 Log.i(TAG, "Using network stack: " + urlRequestContext.getName()); | |
| 73 return urlRequestContext; | |
| 74 } | |
| 75 | |
| 76 private static UrlRequestContext createCronetContext(Context context, | |
| 77 UrlRequestContextConfig config) { | |
| 78 UrlRequestContext urlRequestContext = null; | |
| 79 try { | |
| 80 Class<? extends UrlRequestContext> contextClass = | |
| 81 UrlRequestContext.class.getClassLoader() | |
| 82 .loadClass(CRONET_URL_REQUEST_CONTEXT) | |
| 83 .asSubclass(UrlRequestContext.class); | |
| 84 Constructor<? extends UrlRequestContext> constructor = | |
| 85 contextClass.getConstructor( | |
| 86 Context.class, UrlRequestContextConfig.class); | |
| 87 UrlRequestContext cronetContext = | |
| 88 constructor.newInstance(context, config); | |
| 89 if (cronetContext.isEnabled()) { | |
| 90 urlRequestContext = cronetContext; | |
| 91 } | |
| 92 } catch (ClassNotFoundException e) { | |
| 93 // Leave as null. | |
| 94 } catch (Exception e) { | |
| 95 throw new IllegalStateException( | |
| 96 "Cannot instantiate: " + CRONET_URL_REQUEST_CONTEXT, | |
| 97 e); | |
| 98 } | |
| 99 return urlRequestContext; | |
| 100 } | |
| 101 } | |
| OLD | NEW |