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

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

Powered by Google App Engine
This is Rietveld 408576698