Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: components/cronet/android/api/src/org/chromium/net/CronetProviders.java

Issue 2626523003: Cronet: a framework for providing alternative Cronet implementations (Closed)
Patch Set: Fixed JavaDoc links Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.support.annotation.VisibleForTesting;
9 import android.util.Log;
10
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17 * Provides the list of available {@link CronetProvider} instances.
18 * <p/>
19 * <b>NOTE:</b> This class is for advanced users that want to select a particula r
20 * Cronet implementation. Most users should simply use {@code new} {@link
21 * CronetEngine.Builder#CronetEngine.Builder(android.content.Context)}.
pauljensen 2017/01/20 17:13:35 Might want to put this NOTE at the top.
kapishnikov 2017/01/20 21:48:38 See the answer to the similar comment.
22 */
23 public class CronetProviders {
24 private static final String TAG = CronetProvider.class.getSimpleName();
25
26 /**
27 * Name of the platform Cronet provider.
pauljensen 2017/01/20 17:13:36 I don't understand this. I don't know what "platf
kapishnikov 2017/01/20 21:48:38 Nice. Changed the docs. Regarding the naming, if t
pauljensen 2017/01/23 14:44:28 You didn't address my comment about "platform". I
kapishnikov 2017/01/23 18:16:52 Renamed to PROVIDER_NAME_FALLBACK & PROVIDER_NAME_
28 */
29 public static final String PROVIDER_NAME_PLATFORM = "Platform-Cronet-Provide r";
30
31 /**
32 * Name of the native platform provider.
mef 2017/01/19 22:57:46 nit: s/platform/Cronet/ or remove.
kapishnikov 2017/01/20 16:21:53 Done.
pauljensen 2017/01/20 17:13:36 I don't understand this. I don't know what "platf
kapishnikov 2017/01/20 21:48:38 Changed the docs similar to PROVIDER_NAME_PLATFORM
33 */
34 public static final String PROVIDER_NAME_NATIVE = "Native-Cronet-Provider";
35
36 /**
37 * The key in the app string resource file that is be searched
38 * for an alternative implementation.
39 */
40 private static final String RES_KEY_CRONET_IMPL_CLASS = "CronetProviderClass Name";
41
42 private static CronetProviders sInstance = new CronetProviders();
43
44 @VisibleForTesting
45 CronetProviders() {}
46
47 public static CronetProviders getInstance() {
pauljensen 2017/01/20 17:13:36 needs javadoc, or preferably just remove if we ins
kapishnikov 2017/01/20 21:48:38 If we make it static, it will be difficult to mock
48 return sInstance;
49 }
50
51 /**
52 * Name of the Java {@link CronetProvider} class.
53 */
54 private static final String JAVA_CRONET_PROVIDER_CLASS =
55 "org.chromium.net.impl.JavaCronetProvider";
56
57 /**
58 * Name of the native {@link CronetProvider} class.
59 */
60 private static final String NATIVE_CRONET_PROVIDER_CLASS =
61 "org.chromium.net.impl.NativeCronetProvider";
62
63 /**
64 * Returns the found list of available providers.
65 * The providers are added in the following order:
66 * <ul>
67 * <li>Class name specified as "CronetProviderClassName" application string resource.</li>
68 * <li>The default Cronet implementation</li>
69 * </ul>
70 *
71 * Some of the returned providers may be in a disabled state and should be e nabled by
72 * the invoker. See {@link CronetProvider#isEnabled()}.
73 *
74 * @return the list of available providers.
75 */
76 public List<CronetProvider> getAvailableProviders(Context context) {
pauljensen 2017/01/20 17:13:35 How about moving this to CronetProvider (after mak
pauljensen 2017/01/20 17:13:36 "Available" is confusing because we also use the t
kapishnikov 2017/01/20 21:48:38 Done.
77 List<CronetProvider> providers = new ArrayList<>();
78 addCronetProviderFromResourceFile(context, providers);
79 addCronetProviderImplByClassName(context, NATIVE_CRONET_PROVIDER_CLASS, providers, false);
80 addCronetProviderImplByClassName(context, JAVA_CRONET_PROVIDER_CLASS, pr oviders, false);
81 return providers;
82 }
83
84 /**
85 * Adds a new provider referenced by the class name to the end of the list.
86 *
87 * @param className the class name of the provider that should be instantiat ed.
88 * @param providers the list of providers to add the new provider to.
89 * @return {@code true} if the provider was added to the list; {@code false}
90 * if the provider couldn't be instantiated.
91 */
92 private static boolean addCronetProviderImplByClassName(
93 Context context, String className, List<CronetProvider> providers, b oolean logError) {
94 ClassLoader loader = CronetProviders.class.getClassLoader();
pauljensen 2017/01/20 17:13:35 perhaps this should use context.getClassLoader()?
kapishnikov 2017/01/20 21:48:38 This is a good point. I am not sure in what circum
95 try {
96 Class<? extends CronetProvider> providerClass =
97 loader.loadClass(className).asSubclass(CronetProvider.class) ;
98 Constructor<? extends CronetProvider> ctor =
99 providerClass.getConstructor(Context.class);
100 providers.add(ctor.newInstance(context));
101 return true;
102 } catch (InstantiationException e) {
103 logReflectiveOperationException(className, logError, e);
104 } catch (InvocationTargetException e) {
105 logReflectiveOperationException(className, logError, e);
106 } catch (NoSuchMethodException e) {
107 logReflectiveOperationException(className, logError, e);
108 } catch (IllegalAccessException e) {
109 logReflectiveOperationException(className, logError, e);
110 } catch (ClassNotFoundException e) {
111 logReflectiveOperationException(className, logError, e);
112 }
113 return false;
114 }
115
116 /**
117 * De-duplicates exception handling logic in {@link #addCronetProviderImplBy ClassName}.
118 * It should be removed when support of API Levels lower than 19 is deprecat ed.
119 */
120 private static void logReflectiveOperationException(
121 String className, boolean logError, Exception e) {
122 if (logError) {
123 Log.e(TAG, "Unable to load provider class: " + className, e);
124 } else {
125 Log.d(TAG, "Tried to load " + className + " provider class but it wa sn't"
126 + " included in the app classpath");
127 }
128 }
129
130 /**
131 * Adds a provider specified in the app resource file to the end of the prov ider list.
132 *
133 * @param providers the list of providers to add the new provider to.
134 * @return {@code true} if the provider was added to the list; {@code false}
135 * if the provider wasn't listed, couldn't be found or instantiated.
136 * @
pauljensen 2017/01/20 17:13:36 spurious @
kapishnikov 2017/01/20 21:48:38 Forgot to add @throws RuntimeException. Done.
137 */
138 private static boolean addCronetProviderFromResourceFile(
139 Context context, List<CronetProvider> providers) {
140 int resId = context.getResources().getIdentifier(
141 RES_KEY_CRONET_IMPL_CLASS, "string", context.getPackageName());
142 // Resource not found
143 if (resId == 0) {
144 // The resource wasn't included in the app; therefore, there is noth ing to add.
145 return false;
146 }
147 String className = context.getResources().getString(resId);
148
149 boolean added = addCronetProviderImplByClassName(context, className, pro viders, true);
150
151 if (!added) {
pauljensen 2017/01/20 17:13:36 now that added is used once you can combine these
kapishnikov 2017/01/20 21:48:38 Done.
152 throw new RuntimeException("Unable to instantiate Cronet implementat ion class "
153 + className + " that is listed as in the app string resource file under"
154 + RES_KEY_CRONET_IMPL_CLASS + " key");
155 }
156 return true;
157 }
158 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698