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 public static UrlRequestFactory createFactory(Context context, | |
36 String userAgent, HttpUrlRequestFactoryConfig config) { | |
37 UrlRequestFactory factory = null; | |
38 if (config.userAgent().isEmpty()) | |
39 config.setUserAgent(UserAgent.from(context)); | |
40 | |
41 if (!config.legacyMode()) { | |
42 factory = createCronetFactory(context, config); | |
43 } | |
44 if (factory == null) { | |
45 // TODO(mef): Fallback to stub implementation. | |
46 factory = createCronetFactory(context, config); | |
47 } | |
48 Log.i(TAG, "Using network stack: " + factory.getName()); | |
49 return factory; | |
50 } | |
51 | |
52 /** | |
53 * Returns true if the factory is enabled. | |
mmenke
2014/10/02 15:24:09
What does it mean for a factory to be enabled?
mmenke
2014/10/02 15:24:09
"@return True if..."
mef
2014/10/02 22:07:43
Done.
| |
54 */ | |
55 public abstract boolean isEnabled(); | |
56 | |
57 /** | |
58 * Returns a human-readable name of the factory. | |
mmenke
2014/10/02 15:24:09
@return
mef
2014/10/02 22:07:43
Done.
| |
59 */ | |
60 public abstract String getName(); | |
61 | |
62 private static UrlRequestFactory createCronetFactory(Context context, | |
63 HttpUrlRequestFactoryConfig config) { | |
64 UrlRequestFactory factory = null; | |
65 try { | |
66 Class<? extends UrlRequestFactory> factoryClass = | |
67 UrlRequestFactory.class.getClassLoader(). | |
68 loadClass(CRONET_URL_REQUEST_CONTEXT). | |
69 asSubclass(UrlRequestFactory.class); | |
70 Constructor<? extends UrlRequestFactory> constructor = | |
71 factoryClass.getConstructor( | |
72 Context.class, HttpUrlRequestFactoryConfig.class); | |
73 UrlRequestFactory cronetFactory = | |
74 constructor.newInstance(context, config); | |
75 if (cronetFactory.isEnabled()) { | |
76 factory = cronetFactory; | |
77 } | |
78 } catch (ClassNotFoundException e) { | |
79 // Leave as null | |
80 } catch (Exception e) { | |
81 throw new IllegalStateException( | |
82 "Cannot instantiate: " + | |
83 CRONET_URL_REQUEST_CONTEXT, | |
84 e); | |
85 } | |
86 return factory; | |
87 } | |
26 } | 88 } |
OLD | NEW |