Chromium Code Reviews| 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} by going th rough | |
| 306 * all available {@link CronetProvider CronetProviders} and selecting on e | |
| 307 * with the highest priority. | |
|
pauljensen
2017/01/20 17:13:35
Please explain "highest priority"
kapishnikov
2017/01/20 21:48:38
Since an enabled provider should never return null
| |
| 308 * | |
| 309 * @param context Android Context to use. | |
| 310 * @return the created {@code ICronetEngineBuilder}. | |
| 311 */ | |
| 312 private static ICronetEngineBuilder createBuilderDelegate(Context contex t) { | |
| 313 ICronetEngineBuilder mBuilderDelegate; | |
|
pauljensen
2017/01/20 17:13:35
local variables should not have "m" prefixes, remo
kapishnikov
2017/01/20 21:48:38
Deleted.
| |
| 314 List<CronetProvider> providerList = | |
| 315 getEnabledCronetProviders(context, CronetProviders.getInstan ce()); | |
| 316 | |
| 317 // Try to load implementation using available providers. Assuming th at | |
|
mef
2017/01/19 22:57:46
nit: Assuming -> Assume
kapishnikov
2017/01/20 16:21:53
Done.
| |
| 318 // providers in the beginning of the list have higher priority. | |
| 319 ICronetEngineBuilder builderDelegate = null; | |
| 320 for (CronetProvider provider : providerList) { | |
| 321 Builder builder = provider.createBuilder(); | |
| 322 if (builder != null) { | |
|
pauljensen
2017/01/20 17:13:35
We should use either isEnabled() or createBuilder(
kapishnikov
2017/01/20 21:48:38
Agree. createBuilder() now throws an exception tha
| |
| 323 builderDelegate = builder.mBuilderDelegate; | |
| 324 break; | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 if (builderDelegate == null) { | |
| 329 throw new RuntimeException("Unable to load any Cronet implementa tion." | |
| 330 + " The list of enabled providers: " + providerList); | |
| 331 } | |
| 332 | |
| 333 mBuilderDelegate = builderDelegate; | |
| 334 return mBuilderDelegate; | |
| 335 } | |
| 336 | |
| 337 /** | |
| 338 * Returns the list of available and enabled {@link CronetProvider}. The returned list | |
| 339 * is sorted based on the provider versions and types. | |
| 340 * | |
| 341 * @param context Android Context to use. | |
| 342 * @return the sorted list of enabled providers. | |
| 343 * @throws RuntimeException is the list of providers is empty or all of the providers | |
| 344 * are disabled. | |
| 345 * | |
| 346 * {@hide} | |
|
pauljensen
2017/01/20 17:13:35
I don't think package private appears in javadocs,
kapishnikov
2017/01/20 21:48:38
Removed.
| |
| 347 */ | |
| 348 @VisibleForTesting | |
| 349 static List<CronetProvider> getEnabledCronetProviders( | |
|
mef
2017/01/19 22:57:46
Should this and below be moved into CronetProvider
kapishnikov
2017/01/20 16:21:53
I have a strong opinion that CronetProviders shoul
| |
| 350 Context context, CronetProviders providers) { | |
| 351 // Check that there is at least one available provider. | |
| 352 List<CronetProvider> providerList = providers.getAvailableProviders( context); | |
| 353 if (providerList.size() == 0) { | |
| 354 throw new RuntimeException("Unable to find any Cronet provider." | |
| 355 + " Have you included all necessary jars?"); | |
| 356 } | |
| 357 | |
| 358 // Exclude disabled providers from the list. | |
| 359 for (Iterator<CronetProvider> i = providerList.iterator(); i.hasNext ();) { | |
| 360 CronetProvider provider = i.next(); | |
| 361 if (!provider.isEnabled()) { | |
| 362 i.remove(); | |
| 363 } | |
| 364 } | |
| 365 | |
| 366 // Check that there is at least one enabled provider. | |
| 367 if (providerList.size() == 0) { | |
| 368 throw new RuntimeException("All found Cronet providers are disab led." | |
|
mef
2017/01/19 22:57:46
nit: found -> available
kapishnikov
2017/01/20 16:21:53
Done.
| |
| 369 + " A provider should be enabled before it can be used." ); | |
| 370 } | |
| 371 | |
| 372 // Sort providers based on version and type. | |
| 373 Collections.sort(providerList, new Comparator<CronetProvider>() { | |
| 374 @Override | |
| 375 public int compare(CronetProvider p1, CronetProvider p2) { | |
| 376 // The platform provider should always be at the end of the list. | |
|
mef
2017/01/19 22:57:46
Can PlatformProvider be just added to the end of t
kapishnikov
2017/01/20 16:21:53
We could extract PlatformProvider from the list, s
| |
| 377 if (CronetProviders.PROVIDER_NAME_PLATFORM.equals(p1.getName ())) { | |
| 378 return 1; | |
| 379 } | |
| 380 if (CronetProviders.PROVIDER_NAME_PLATFORM.equals(p2.getName ())) { | |
| 381 return -1; | |
| 382 } | |
| 383 // A provider with higher version should go first. | |
| 384 return -compareVersions(p1.getVersion(), p2.getVersion()); | |
|
mef
2017/01/19 22:57:46
Is it ok if compareVersions throws an exception?
kapishnikov
2017/01/20 16:21:53
It is a good question. Currently it will cause the
| |
| 385 } | |
| 386 }); | |
| 387 return providerList; | |
| 388 } | |
| 389 | |
| 390 /** | |
| 391 * Compares two strings that contain versions. The string should only co ntain | |
| 392 * dot-separated segments that contain an arbitrary number of digits dig its [0-9]. | |
| 393 * | |
| 394 * @param s1 the first string. | |
| 395 * @param s2 the second string. | |
| 396 * @return -1 if s1<s2, +1 if s1>s2 and 0 if s1=s2. If two versions are equal, the | |
| 397 * version with the higher number of segments is considered to b e higher. | |
| 398 * | |
| 399 * @throws IllegalArgumentException if any of the strings contains an il legal | |
| 400 * version number. | |
| 401 * | |
| 402 * {@hide} | |
|
pauljensen
2017/01/20 17:13:35
I don't think package private appears in javadocs,
kapishnikov
2017/01/20 21:48:38
Removed.
| |
| 403 */ | |
| 404 @VisibleForTesting | |
| 405 static int compareVersions(String s1, String s2) { | |
| 406 if (s1 == null || s2 == null) { | |
| 407 throw new IllegalArgumentException("The input values cannot be n ull"); | |
| 408 } | |
| 409 String[] s1segments = s1.split("\\."); | |
| 410 String[] s2segments = s2.split("\\."); | |
| 411 int index = 0; | |
| 412 while (s1segments.length > index && s2segments.length > index) { | |
|
pauljensen
2017/01/20 17:13:35
how about:
for (int i = 0; i < s1segments.length &
kapishnikov
2017/01/20 21:48:38
Neat. Done.
| |
| 413 try { | |
| 414 int s1segment = Integer.parseInt(s1segments[index]); | |
| 415 int s2segment = Integer.parseInt(s2segments[index]); | |
| 416 if (s1segment != s2segment) { | |
| 417 return Integer.signum(s1segment - s2segment); | |
| 418 } | |
| 419 index += 1; | |
| 420 } catch (NumberFormatException e) { | |
| 421 throw new IllegalArgumentException("Unable to convert versio n segments into" | |
| 422 + " integers: " + s1segments[index] + " & " + s2segments[index], | |
| 423 e); | |
| 424 } | |
| 425 } | |
| 426 return Integer.signum(s1segments.length - s2segments.length); | |
| 427 } | |
| 285 } | 428 } |
| 286 | 429 |
| 287 /** | 430 /** |
| 288 * @return a human-readable version string of the engine. | 431 * @return a human-readable version string of the engine. |
| 289 */ | 432 */ |
| 290 public abstract String getVersionString(); | 433 public abstract String getVersionString(); |
| 291 | 434 |
| 292 /** | 435 /** |
| 293 * Shuts down the {@link CronetEngine} if there are no active requests, | 436 * Shuts down the {@link CronetEngine} if there are no active requests, |
| 294 * otherwise throws an exception. | 437 * 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 | 542 * thread calling {@link Executor#execute} to prevent blocking networking |
| 400 * operations and causing exceptions during shutdown. | 543 * operations and causing exceptions during shutdown. |
| 401 * | 544 * |
| 402 * @param url URL for the generated requests. | 545 * @param url URL for the generated requests. |
| 403 * @param callback callback object that gets invoked on different events. | 546 * @param callback callback object that gets invoked on different events. |
| 404 * @param executor {@link Executor} on which all callbacks will be invoked. | 547 * @param executor {@link Executor} on which all callbacks will be invoked. |
| 405 */ | 548 */ |
| 406 public abstract UrlRequest.Builder newUrlRequestBuilder( | 549 public abstract UrlRequest.Builder newUrlRequestBuilder( |
| 407 String url, UrlRequest.Callback callback, Executor executor); | 550 String url, UrlRequest.Callback callback, Executor executor); |
| 408 } | 551 } |
| OLD | NEW |