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

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

Issue 2626523003: Cronet: a framework for providing alternative Cronet implementations (Closed)
Patch Set: Added context to getName() & getVersion(). 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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; 7 import android.content.Context;
8 import android.net.http.HttpResponseCache; 8 import android.net.http.HttpResponseCache;
9 import android.support.annotation.Nullable;
10 import android.util.Log;
9 11
10 import java.io.IOException; 12 import java.io.IOException;
11 import java.net.URL; 13 import java.net.URL;
12 import java.net.URLConnection; 14 import java.net.URLConnection;
13 import java.net.URLStreamHandlerFactory; 15 import java.net.URLStreamHandlerFactory;
16 import java.util.ArrayList;
14 import java.util.Date; 17 import java.util.Date;
18 import java.util.List;
15 import java.util.Set; 19 import java.util.Set;
16 import java.util.concurrent.Executor; 20 import java.util.concurrent.Executor;
17 21
18 import javax.net.ssl.HttpsURLConnection; 22 import javax.net.ssl.HttpsURLConnection;
19 /** 23 /**
20 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack 24 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack
21 * available on the current platform. An instance of this class can be created 25 * available on the current platform. An instance of this class can be created
22 * using {@link Builder}. 26 * using {@link Builder}.
23 */ 27 */
24 public abstract class CronetEngine { 28 public abstract class CronetEngine {
29 static final String TAG = "CronetApi";
pauljensen 2017/01/11 16:48:12 private
pauljensen 2017/01/11 16:48:12 "CronetApi" -> CronetEngine.class.getSimpleName()
kapishnikov 2017/01/17 22:29:19 Gone in the latest CL.
kapishnikov 2017/01/17 22:29:19 Gone in the latest CL.
30
25 /** 31 /**
26 * A builder for {@link CronetEngine}s, which allows runtime configuration o f 32 * A builder for {@link CronetEngine}s, which allows runtime configuration o f
27 * {@code CronetEngine}. Configuration options are set on the builder and 33 * {@code CronetEngine}. Configuration options are set on the builder and
28 * then {@link #build} is called to create the {@code CronetEngine}. 34 * then {@link #build} is called to create the {@code CronetEngine}.
29 */ 35 */
30 // NOTE(kapishnikov): In order to avoid breaking the existing API clients, a ll future methods 36 // NOTE(kapishnikov): In order to avoid breaking the existing API clients, a ll future methods
31 // added to this class and other API classes must have default implementatio n. 37 // added to this class and other API classes must have default implementatio n.
32 public static class Builder { 38 public static class Builder {
33 /** 39 /**
34 * A class which provides a method for loading the cronet native library . Apps needing to 40 * A class which provides a method for loading the cronet native library . Apps needing to
(...skipping 21 matching lines...) Expand all
56 * {@link CronetEngine}. The default configuration enables HTTP/2 and 62 * {@link CronetEngine}. The default configuration enables HTTP/2 and
57 * disables QUIC, SDCH and the HTTP cache. 63 * disables QUIC, SDCH and the HTTP cache.
58 * 64 *
59 * @param context Android {@link Context}, which is used by 65 * @param context Android {@link Context}, which is used by
60 * {@link Builder} to retrieve the application 66 * {@link Builder} to retrieve the application
61 * context. A reference to only the application 67 * context. A reference to only the application
62 * context will be kept, so as to avoid extending 68 * context will be kept, so as to avoid extending
63 * the lifetime of {@code context} unnecessarily. 69 * the lifetime of {@code context} unnecessarily.
64 */ 70 */
65 public Builder(Context context) { 71 public Builder(Context context) {
66 mBuilderDelegate = ImplLoader.load(context); 72 CronetImplProviderFinder finder = new CronetImplProviderFinder(conte xt);
73 List<CronetImplProvider> providers = finder.findProviders();
74
75 // Default provider selector.
76 CronetImplProviderSelector selector = new CronetImplProviderSelector () {
77 @Override
78 @Nullable
79 public CronetImplProvider select(List<CronetImplProvider> provid ers) {
80 if (providers.size() > 0) {
81 return providers.get(0);
82 }
83 return null;
84 }
85 };
86
87 CronetImplProvider provider = selector.select(providers);
88 if (provider != null) {
89 mBuilderDelegate = provider.load(context);
90 } else {
91 throw new RuntimeException("Unable to find suitable CronetImplPr ovider."
92 + " Have you included all required jars?");
93 }
67 } 94 }
68 95
69 /** 96 /**
70 * Constructs a User-Agent string including application name and version , 97 * Constructs a User-Agent string including application name and version ,
71 * system build version, model and id, and Cronet version. 98 * system build version, model and id, and Cronet version.
72 * 99 *
73 * @return User-Agent string. 100 * @return User-Agent string.
74 */ 101 */
75 public String getDefaultUserAgent() { 102 public String getDefaultUserAgent() {
76 return mBuilderDelegate.getDefaultUserAgent(); 103 return mBuilderDelegate.getDefaultUserAgent();
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 * {@code executor}'s threads. {@code executor} must not run tasks on the 425 * {@code executor}'s threads. {@code executor} must not run tasks on the
399 * thread calling {@link Executor#execute} to prevent blocking networking 426 * thread calling {@link Executor#execute} to prevent blocking networking
400 * operations and causing exceptions during shutdown. 427 * operations and causing exceptions during shutdown.
401 * 428 *
402 * @param url URL for the generated requests. 429 * @param url URL for the generated requests.
403 * @param callback callback object that gets invoked on different events. 430 * @param callback callback object that gets invoked on different events.
404 * @param executor {@link Executor} on which all callbacks will be invoked. 431 * @param executor {@link Executor} on which all callbacks will be invoked.
405 */ 432 */
406 public abstract UrlRequest.Builder newUrlRequestBuilder( 433 public abstract UrlRequest.Builder newUrlRequestBuilder(
407 String url, UrlRequest.Callback callback, Executor executor); 434 String url, UrlRequest.Callback callback, Executor executor);
435
436 /**
437 * Searches for the available CronetImplProvider implementations. The {@link #findProviders()}
438 * method returns the list of availiable providers in the following order (i f present):
439 * <ul>
440 * <li>Class name specified as "CronetProviderClassName" application string resource.</li>
441 * <li>The default Cronet implementation</li>
442 * </ul>
443 */
444 private static class CronetImplProviderFinder {
445 /**
446 * Name of the default {@link CronetImplProvider} class.
447 */
448 private static final String DEFAULT_CRONET_IMPL_PROVIDER_CLASS =
449 "org.chromium.net.impl.DefaultCronetImplProvider";
450
451 /**
452 * The key in the app string resource file that is be searched
453 * for an alternative implementation.
454 */
455 private static final String RES_KEY_CRONET_IMPL_CLASS = "CronetProviderC lassName";
456
457 private final Context mContext;
458
459 private CronetImplProviderFinder(Context context) {
460 mContext = context;
461 }
462
463 /**
464 * Returns the found list of available providers.
465 *
466 * @return the list of providers.
467 */
468 private List<CronetImplProvider> findProviders() {
469 List<CronetImplProvider> providers = new ArrayList<>();
470 addCronetProviderFromResourceFile(providers);
471 addCronetProviderImplByClassName(DEFAULT_CRONET_IMPL_PROVIDER_CLASS, providers);
472 return providers;
pauljensen 2017/01/11 16:48:13 We need to filter out providers that implement a v
kapishnikov 2017/01/17 22:29:19 We are going to use API_LEVEL that is introduced i
473 }
474
475 /**
476 * Adds a new provider referenced by the class name to the end of the li st.
477 *
478 * @param className the class name of the provider that shoul dbe instan tiated.
479 * @param providers the list of providers to add the new provider to.
480 * @return true if the provider was added.
481 */
482 private static boolean addCronetProviderImplByClassName(
483 String className, List<CronetImplProvider> providers) {
484 ClassLoader loader = CronetImplProviderFinder.class.getClassLoader() ;
485 Class<? extends CronetImplProvider> providerClass;
pauljensen 2017/01/11 16:48:12 move inside try block (variables should always hav
kapishnikov 2017/01/17 22:29:19 Done. See CronetProviders.java
486 try {
487 providerClass = loader.loadClass(className).asSubclass(CronetImp lProvider.class);
488 providers.add(providerClass.newInstance());
pauljensen 2017/01/11 16:48:13 how about moving this line outside the try block?
kapishnikov 2017/01/17 22:29:19 Kept it here for readability.
489 return true;
490 } catch (Exception ex) {
pauljensen 2017/01/11 16:48:12 catching all exceptions is not recommended, how ab
kapishnikov 2017/01/17 22:29:19 Changed to multiple catches. We cannot catch multi
491 // Class is absent in the class path. That is expected for
492 // some deployment configurations.
493 return false;
494 }
495 }
496
497 /**
498 * Adds a provider specified in the app resource file to the end of the provider list.
499 *
500 * @param providers the list of providers to add the new provider to.
501 * @return true if the provider was added.
502 */
503 private boolean addCronetProviderFromResourceFile(List<CronetImplProvide r> providers) {
504 String packageName = mContext.getPackageName();
505 int resId = mContext.getResources().getIdentifier(
506 RES_KEY_CRONET_IMPL_CLASS, "string", packageName);
pauljensen 2017/01/11 16:48:13 packageName->mContext.getPackageName()
kapishnikov 2017/01/17 22:29:19 Done.
507 // Resource not found
508 if (resId == 0) {
509 // The resource wasn't included in the app; therefore, there is nothing to add.
510 return false;
511 }
512 String className = mContext.getResources().getString(resId);
513 CronetImplProvider provider;
mef 2017/01/10 23:30:03 Should it call addCronetProviderImplByClassName(cl
kapishnikov 2017/01/17 22:29:19 Done. See CronetProviders.java
514 try {
515 Class<? extends CronetImplProvider> cl =
516 Class.forName(className).asSubclass(CronetImplProvider.c lass);
517 provider = cl.newInstance();
518 } catch (Exception e) {
519 Log.e(TAG, "Unable to instantiate Cronet implementation class " + className
520 + " that is listed as " + RES_KEY_CRONET_IMPL_CL ASS
521 + " in the app string resource file",
522 e);
523 return false;
524 }
525
526 providers.add(provider);
527 return true;
528 }
529 }
408 } 530 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698