OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
| 7 import android.content.Context; |
| 8 import android.util.Log; |
| 9 |
| 10 import java.lang.reflect.Constructor; |
7 import java.util.concurrent.Executor; | 11 import java.util.concurrent.Executor; |
8 | 12 |
9 /** | 13 /** |
10 * A factory for {@link UrlRequest}'s, which uses the best HTTP stack | 14 * A factory for {@link UrlRequest}'s, which uses the best HTTP stack |
11 * available on the current platform. | 15 * available on the current platform. |
12 */ | 16 */ |
13 public interface UrlRequestFactory { | 17 public abstract class UrlRequestFactory { |
| 18 private static final String TAG = "UrlRequestFactory"; |
| 19 private static final String CRONET_URL_REQUEST_CONTEXT = |
| 20 "org.chromium.net.CronetUrlRequestContext"; |
| 21 |
14 /** | 22 /** |
15 * Creates an UrlRequest object. All UrlRequest functions must | 23 * Creates an UrlRequest object. All UrlRequest functions must |
16 * be called on the Executor's thread, and all callbacks will be called | 24 * be called on the Executor's thread, and all callbacks will be called |
17 * on the Executor's thread as well. | 25 * on the Executor's thread as well. |
18 * createRequest itself may be called on any thread. | 26 * createRequest itself may be called on any thread. |
19 * @param url URL for the request. | 27 * @param url URL for the request. |
20 * @param listener Callback interface that gets called on different events. | 28 * @param listener Callback interface that gets called on different events. |
21 * @param executor Executor on which all callbacks will be called. | 29 * @param executor Executor on which all callbacks will be called. |
22 * @return new request. | 30 * @return new request. |
23 */ | 31 */ |
24 public abstract UrlRequest createRequest(String url, | 32 public abstract UrlRequest createRequest(String url, |
25 UrlRequestListener listener, Executor executor); | 33 UrlRequestListener listener, Executor executor); |
| 34 |
| 35 /** |
| 36 * @return true if the factory is enabled. |
| 37 */ |
| 38 public abstract boolean isEnabled(); |
| 39 |
| 40 /** |
| 41 * @return a human-readable name of the factory. |
| 42 */ |
| 43 public abstract String getName(); |
| 44 |
| 45 /** |
| 46 * Create factory with given config. If config.legacyMode is true, or |
| 47 * native library is not available, then creates HttpUrlConnection-based |
| 48 * factory. |
| 49 * @param context application context. |
| 50 * @param config factory configuration. |
| 51 */ |
| 52 public static UrlRequestFactory createFactory(Context context, |
| 53 HttpUrlRequestFactoryConfig config) { |
| 54 UrlRequestFactory factory = null; |
| 55 if (config.userAgent().isEmpty()) { |
| 56 config.setUserAgent(UserAgent.from(context)); |
| 57 } |
| 58 if (!config.legacyMode()) { |
| 59 factory = createCronetFactory(context, config); |
| 60 } |
| 61 if (factory == null) { |
| 62 // TODO(mef): Fallback to stub implementation. Once stub |
| 63 // implementation is available merge with createCronetFactory. |
| 64 factory = createCronetFactory(context, config); |
| 65 } |
| 66 Log.i(TAG, "Using network stack: " + factory.getName()); |
| 67 return factory; |
| 68 } |
| 69 |
| 70 private static UrlRequestFactory createCronetFactory(Context context, |
| 71 HttpUrlRequestFactoryConfig config) { |
| 72 UrlRequestFactory factory = null; |
| 73 try { |
| 74 Class<? extends UrlRequestFactory> factoryClass = |
| 75 UrlRequestFactory.class.getClassLoader(). |
| 76 loadClass(CRONET_URL_REQUEST_CONTEXT). |
| 77 asSubclass(UrlRequestFactory.class); |
| 78 Constructor<? extends UrlRequestFactory> constructor = |
| 79 factoryClass.getConstructor( |
| 80 Context.class, HttpUrlRequestFactoryConfig.class); |
| 81 UrlRequestFactory cronetFactory = |
| 82 constructor.newInstance(context, config); |
| 83 if (cronetFactory.isEnabled()) { |
| 84 factory = cronetFactory; |
| 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 factory; |
| 95 } |
26 } | 96 } |
OLD | NEW |