| OLD | NEW |
| 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.VisibleForTesting; |
| 9 | 10 |
| 10 import java.io.IOException; | 11 import java.io.IOException; |
| 11 import java.net.URL; | 12 import java.net.URL; |
| 12 import java.net.URLConnection; | 13 import java.net.URLConnection; |
| 13 import java.net.URLStreamHandlerFactory; | 14 import java.net.URLStreamHandlerFactory; |
| 15 import java.util.Collections; |
| 16 import java.util.Comparator; |
| 14 import java.util.Date; | 17 import java.util.Date; |
| 18 import java.util.Iterator; |
| 19 import java.util.List; |
| 15 import java.util.Set; | 20 import java.util.Set; |
| 16 import java.util.concurrent.Executor; | 21 import java.util.concurrent.Executor; |
| 17 | 22 |
| 18 import javax.net.ssl.HttpsURLConnection; | 23 import javax.net.ssl.HttpsURLConnection; |
| 19 /** | 24 /** |
| 20 * An engine to process {@link UrlRequest}s, which uses the best HTTP stack | 25 * 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 | 26 * available on the current platform. An instance of this class can be created |
| 22 * using {@link Builder}. | 27 * using {@link Builder}. |
| 23 */ | 28 */ |
| 24 public abstract class CronetEngine { | 29 public abstract class CronetEngine { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 * {@link CronetEngine}. The default configuration enables HTTP/2 and | 61 * {@link CronetEngine}. The default configuration enables HTTP/2 and |
| 57 * disables QUIC, SDCH and the HTTP cache. | 62 * disables QUIC, SDCH and the HTTP cache. |
| 58 * | 63 * |
| 59 * @param context Android {@link Context}, which is used by | 64 * @param context Android {@link Context}, which is used by |
| 60 * {@link Builder} to retrieve the application | 65 * {@link Builder} to retrieve the application |
| 61 * context. A reference to only the application | 66 * context. A reference to only the application |
| 62 * context will be kept, so as to avoid extending | 67 * context will be kept, so as to avoid extending |
| 63 * the lifetime of {@code context} unnecessarily. | 68 * the lifetime of {@code context} unnecessarily. |
| 64 */ | 69 */ |
| 65 public Builder(Context context) { | 70 public Builder(Context context) { |
| 66 mBuilderDelegate = ImplLoader.load(context); | 71 this(createBuilderDelegate(context)); |
| 67 } | 72 } |
| 68 | 73 |
| 69 /** | 74 /** |
| 75 * Constructs {@link Builder} with a given delegate that provides the ac
tual implementation |
| 76 * of the {@code Builder} methods. This constructor is used only by the
internal |
| 77 * implementation. |
| 78 * |
| 79 * @param builderDelegate delegate that provides the actual implementati
on. |
| 80 * |
| 81 * {@hide} |
| 82 */ |
| 83 public Builder(ICronetEngineBuilder builderDelegate) { |
| 84 mBuilderDelegate = builderDelegate; |
| 85 } |
| 86 |
| 87 /** |
| 70 * Constructs a User-Agent string including application name and version
, | 88 * Constructs a User-Agent string including application name and version
, |
| 71 * system build version, model and id, and Cronet version. | 89 * system build version, model and id, and Cronet version. |
| 72 * | 90 * |
| 73 * @return User-Agent string. | 91 * @return User-Agent string. |
| 74 */ | 92 */ |
| 75 public String getDefaultUserAgent() { | 93 public String getDefaultUserAgent() { |
| 76 return mBuilderDelegate.getDefaultUserAgent(); | 94 return mBuilderDelegate.getDefaultUserAgent(); |
| 77 } | 95 } |
| 78 | 96 |
| 79 /** | 97 /** |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 return this; | 293 return this; |
| 276 } | 294 } |
| 277 | 295 |
| 278 /** | 296 /** |
| 279 * Build a {@link CronetEngine} using this builder's configuration. | 297 * Build a {@link CronetEngine} using this builder's configuration. |
| 280 * @return constructed {@link CronetEngine}. | 298 * @return constructed {@link CronetEngine}. |
| 281 */ | 299 */ |
| 282 public CronetEngine build() { | 300 public CronetEngine build() { |
| 283 return mBuilderDelegate.build(); | 301 return mBuilderDelegate.build(); |
| 284 } | 302 } |
| 303 |
| 304 /** |
| 305 * Creates an implementation of {@link ICronetEngineBuilder} that can be
used |
| 306 * to delegate the builder calls to. The method uses {@link CronetProvid
er} |
| 307 * to obtain the list of available providers. |
| 308 * |
| 309 * @param context Android Context to use. |
| 310 * @return the created {@code ICronetEngineBuilder}. |
| 311 */ |
| 312 private static ICronetEngineBuilder createBuilderDelegate(Context contex
t) { |
| 313 List<CronetProvider> providerList = |
| 314 getEnabledCronetProviders(context, CronetProvider.getAllProv
iders(context)); |
| 315 return providerList.get(0).createBuilder().mBuilderDelegate; |
| 316 } |
| 317 |
| 318 /** |
| 319 * Returns the list of available and enabled {@link CronetProvider}. The
returned list |
| 320 * is sorted based on the provider versions and types. |
| 321 * |
| 322 * @param context Android Context to use. |
| 323 * @return the sorted list of enabled providers. The list contains at le
ast one provider. |
| 324 * @throws RuntimeException is the list of providers is empty or all of
the providers |
| 325 * are disabled. |
| 326 */ |
| 327 @VisibleForTesting |
| 328 static List<CronetProvider> getEnabledCronetProviders( |
| 329 Context context, List<CronetProvider> providers) { |
| 330 // Check that there is at least one available provider. |
| 331 if (providers.size() == 0) { |
| 332 throw new RuntimeException("Unable to find any Cronet provider." |
| 333 + " Have you included all necessary jars?"); |
| 334 } |
| 335 |
| 336 // Exclude disabled providers from the list. |
| 337 for (Iterator<CronetProvider> i = providers.iterator(); i.hasNext();
) { |
| 338 CronetProvider provider = i.next(); |
| 339 if (!provider.isEnabled()) { |
| 340 i.remove(); |
| 341 } |
| 342 } |
| 343 |
| 344 // Check that there is at least one enabled provider. |
| 345 if (providers.size() == 0) { |
| 346 throw new RuntimeException("All available Cronet providers are d
isabled." |
| 347 + " A provider should be enabled before it can be used."
); |
| 348 } |
| 349 |
| 350 // Sort providers based on version and type. |
| 351 Collections.sort(providers, new Comparator<CronetProvider>() { |
| 352 @Override |
| 353 public int compare(CronetProvider p1, CronetProvider p2) { |
| 354 // The fallback provider should always be at the end of the
list. |
| 355 if (CronetProvider.PROVIDER_NAME_FALLBACK.equals(p1.getName(
))) { |
| 356 return 1; |
| 357 } |
| 358 if (CronetProvider.PROVIDER_NAME_FALLBACK.equals(p2.getName(
))) { |
| 359 return -1; |
| 360 } |
| 361 // A provider with higher version should go first. |
| 362 return -compareVersions(p1.getVersion(), p2.getVersion()); |
| 363 } |
| 364 }); |
| 365 return providers; |
| 366 } |
| 367 |
| 368 /** |
| 369 * Compares two strings that contain versions. The string should only co
ntain |
| 370 * dot-separated segments that contain an arbitrary number of digits dig
its [0-9]. |
| 371 * |
| 372 * @param s1 the first string. |
| 373 * @param s2 the second string. |
| 374 * @return -1 if s1<s2, +1 if s1>s2 and 0 if s1=s2. If two versions are
equal, the |
| 375 * version with the higher number of segments is considered to b
e higher. |
| 376 * |
| 377 * @throws IllegalArgumentException if any of the strings contains an il
legal |
| 378 * version number. |
| 379 */ |
| 380 @VisibleForTesting |
| 381 static int compareVersions(String s1, String s2) { |
| 382 if (s1 == null || s2 == null) { |
| 383 throw new IllegalArgumentException("The input values cannot be n
ull"); |
| 384 } |
| 385 String[] s1segments = s1.split("\\."); |
| 386 String[] s2segments = s2.split("\\."); |
| 387 for (int i = 0; i < s1segments.length && i < s2segments.length; i++)
{ |
| 388 try { |
| 389 int s1segment = Integer.parseInt(s1segments[i]); |
| 390 int s2segment = Integer.parseInt(s2segments[i]); |
| 391 if (s1segment != s2segment) { |
| 392 return Integer.signum(s1segment - s2segment); |
| 393 } |
| 394 } catch (NumberFormatException e) { |
| 395 throw new IllegalArgumentException("Unable to convert versio
n segments into" |
| 396 + " integers: " + s1segments[i] + " & " + s2
segments[i], |
| 397 e); |
| 398 } |
| 399 } |
| 400 return Integer.signum(s1segments.length - s2segments.length); |
| 401 } |
| 285 } | 402 } |
| 286 | 403 |
| 287 /** | 404 /** |
| 288 * @return a human-readable version string of the engine. | 405 * @return a human-readable version string of the engine. |
| 289 */ | 406 */ |
| 290 public abstract String getVersionString(); | 407 public abstract String getVersionString(); |
| 291 | 408 |
| 292 /** | 409 /** |
| 293 * Shuts down the {@link CronetEngine} if there are no active requests, | 410 * Shuts down the {@link CronetEngine} if there are no active requests, |
| 294 * otherwise throws an exception. | 411 * otherwise throws an exception. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 * thread calling {@link Executor#execute} to prevent blocking networking | 516 * thread calling {@link Executor#execute} to prevent blocking networking |
| 400 * operations and causing exceptions during shutdown. | 517 * operations and causing exceptions during shutdown. |
| 401 * | 518 * |
| 402 * @param url URL for the generated requests. | 519 * @param url URL for the generated requests. |
| 403 * @param callback callback object that gets invoked on different events. | 520 * @param callback callback object that gets invoked on different events. |
| 404 * @param executor {@link Executor} on which all callbacks will be invoked. | 521 * @param executor {@link Executor} on which all callbacks will be invoked. |
| 405 */ | 522 */ |
| 406 public abstract UrlRequest.Builder newUrlRequestBuilder( | 523 public abstract UrlRequest.Builder newUrlRequestBuilder( |
| 407 String url, UrlRequest.Callback callback, Executor executor); | 524 String url, UrlRequest.Callback callback, Executor executor); |
| 408 } | 525 } |
| OLD | NEW |