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.support.annotation.IntDef; | 8 import android.support.annotation.IntDef; |
9 import android.util.Log; | 9 import android.util.Log; |
10 | 10 |
11 import org.json.JSONArray; | 11 import org.json.JSONArray; |
12 import org.json.JSONException; | 12 import org.json.JSONException; |
13 import org.json.JSONObject; | 13 import org.json.JSONObject; |
14 | 14 |
15 import java.io.File; | 15 import java.io.File; |
| 16 import java.io.IOException; |
16 import java.lang.annotation.Retention; | 17 import java.lang.annotation.Retention; |
17 import java.lang.annotation.RetentionPolicy; | 18 import java.lang.annotation.RetentionPolicy; |
18 import java.lang.reflect.Constructor; | 19 import java.lang.reflect.Constructor; |
19 import java.net.Proxy; | 20 import java.net.Proxy; |
20 import java.net.URL; | 21 import java.net.URL; |
21 import java.net.URLConnection; | 22 import java.net.URLConnection; |
22 import java.net.URLStreamHandlerFactory; | 23 import java.net.URLStreamHandlerFactory; |
23 import java.util.List; | 24 import java.util.List; |
24 import java.util.Map; | 25 import java.util.Map; |
25 import java.util.concurrent.Executor; | 26 import java.util.concurrent.Executor; |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 | 588 |
588 /** | 589 /** |
589 * Establishes a new connection to the resource specified by the {@link URL}
{@code url}. | 590 * Establishes a new connection to the resource specified by the {@link URL}
{@code url}. |
590 * <p> | 591 * <p> |
591 * <b>Note:</b> Cronet's {@link java.net.HttpURLConnection} implementation i
s subject to certain | 592 * <b>Note:</b> Cronet's {@link java.net.HttpURLConnection} implementation i
s subject to certain |
592 * limitations, see {@link #createURLStreamHandlerFactory} for details. | 593 * limitations, see {@link #createURLStreamHandlerFactory} for details. |
593 * | 594 * |
594 * @param url URL of resource to connect to. | 595 * @param url URL of resource to connect to. |
595 * @return an {@link java.net.HttpURLConnection} instance implemented by thi
s CronetEngine. | 596 * @return an {@link java.net.HttpURLConnection} instance implemented by thi
s CronetEngine. |
596 */ | 597 */ |
597 public abstract URLConnection openConnection(URL url); | 598 public abstract URLConnection openConnection(URL url) throws IOException; |
598 | 599 |
599 /** | 600 /** |
600 * Establishes a new connection to the resource specified by the {@link URL}
{@code url} | 601 * Establishes a new connection to the resource specified by the {@link URL}
{@code url} |
601 * using the given proxy. | 602 * using the given proxy. |
602 * <p> | 603 * <p> |
603 * <b>Note:</b> Cronet's {@link java.net.HttpURLConnection} implementation i
s subject to certain | 604 * <b>Note:</b> Cronet's {@link java.net.HttpURLConnection} implementation i
s subject to certain |
604 * limitations, see {@link #createURLStreamHandlerFactory} for details. | 605 * limitations, see {@link #createURLStreamHandlerFactory} for details. |
605 * | 606 * |
606 * @param url URL of resource to connect to. | 607 * @param url URL of resource to connect to. |
607 * @param proxy proxy to use when establishing connection. | 608 * @param proxy proxy to use when establishing connection. |
608 * @return an {@link java.net.HttpURLConnection} instance implemented by thi
s CronetEngine. | 609 * @return an {@link java.net.HttpURLConnection} instance implemented by thi
s CronetEngine. |
609 * @hide | 610 * @hide |
610 * @deprecated Marked as deprecated because @hide doesn't properly hide but | 611 * @deprecated Marked as deprecated because @hide doesn't properly hide but |
611 * javadocs are built with nodeprecated="yes". | 612 * javadocs are built with nodeprecated="yes". |
612 * TODO(pauljensen): Expose once implemented, http://crbug.com/41811
1 | 613 * TODO(pauljensen): Expose once implemented, http://crbug.com/41811
1 |
613 */ | 614 */ |
614 @SuppressWarnings("DepAnn") public abstract URLConnection openConnection(URL
url, Proxy proxy); | 615 @SuppressWarnings("DepAnn") |
| 616 public abstract URLConnection openConnection(URL url, Proxy proxy) throws IO
Exception; |
615 | 617 |
616 /** | 618 /** |
617 * Creates a {@link URLStreamHandlerFactory} to handle HTTP and HTTPS | 619 * Creates a {@link URLStreamHandlerFactory} to handle HTTP and HTTPS |
618 * traffic. An instance of this class can be installed via | 620 * traffic. An instance of this class can be installed via |
619 * {@link URL#setURLStreamHandlerFactory} thus using this CronetEngine by de
fault for | 621 * {@link URL#setURLStreamHandlerFactory} thus using this CronetEngine by de
fault for |
620 * all requests created via {@link URL#openConnection}. | 622 * all requests created via {@link URL#openConnection}. |
621 * <p> | 623 * <p> |
622 * Cronet does not use certain HTTP features provided via the system: | 624 * Cronet does not use certain HTTP features provided via the system: |
623 * <ul> | 625 * <ul> |
624 * <li>the HTTP cache installed via | 626 * <li>the HTTP cache installed via |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 @Deprecated | 659 @Deprecated |
658 public static CronetEngine createContext(Builder builder) { | 660 public static CronetEngine createContext(Builder builder) { |
659 CronetEngine cronetEngine = null; | 661 CronetEngine cronetEngine = null; |
660 if (builder.getUserAgent().isEmpty()) { | 662 if (builder.getUserAgent().isEmpty()) { |
661 builder.setUserAgent(builder.getDefaultUserAgent()); | 663 builder.setUserAgent(builder.getDefaultUserAgent()); |
662 } | 664 } |
663 if (!builder.legacyMode()) { | 665 if (!builder.legacyMode()) { |
664 cronetEngine = createCronetEngine(builder); | 666 cronetEngine = createCronetEngine(builder); |
665 } | 667 } |
666 if (cronetEngine == null) { | 668 if (cronetEngine == null) { |
667 // TODO(mef): Fallback to stub implementation. Once stub | 669 cronetEngine = new JavaCronetEngine(builder.getUserAgent()); |
668 // implementation is available merge with createCronetFactory. | |
669 cronetEngine = createCronetEngine(builder); | |
670 } | 670 } |
671 Log.i(TAG, "Using network stack: " + cronetEngine.getVersionString()); | 671 Log.i(TAG, "Using network stack: " + cronetEngine.getVersionString()); |
672 return cronetEngine; | 672 return cronetEngine; |
673 } | 673 } |
674 | 674 |
675 private static CronetEngine createCronetEngine(Builder builder) { | 675 private static CronetEngine createCronetEngine(Builder builder) { |
676 CronetEngine cronetEngine = null; | 676 CronetEngine cronetEngine = null; |
677 try { | 677 try { |
678 Class<? extends CronetEngine> engineClass = | 678 Class<? extends CronetEngine> engineClass = |
679 CronetEngine.class.getClassLoader() | 679 CronetEngine.class.getClassLoader() |
680 .loadClass(CRONET_URL_REQUEST_CONTEXT) | 680 .loadClass(CRONET_URL_REQUEST_CONTEXT) |
681 .asSubclass(CronetEngine.class); | 681 .asSubclass(CronetEngine.class); |
682 Constructor<? extends CronetEngine> constructor = | 682 Constructor<? extends CronetEngine> constructor = |
683 engineClass.getConstructor(Builder.class); | 683 engineClass.getConstructor(Builder.class); |
684 CronetEngine possibleEngine = constructor.newInstance(builder); | 684 CronetEngine possibleEngine = constructor.newInstance(builder); |
685 if (possibleEngine.isEnabled()) { | 685 if (possibleEngine.isEnabled()) { |
686 cronetEngine = possibleEngine; | 686 cronetEngine = possibleEngine; |
687 } | 687 } |
688 } catch (ClassNotFoundException e) { | 688 } catch (ClassNotFoundException e) { |
689 // Leave as null. | 689 // Leave as null. |
690 } catch (Exception e) { | 690 } catch (Exception e) { |
691 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_
REQUEST_CONTEXT, e); | 691 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_
REQUEST_CONTEXT, e); |
692 } | 692 } |
693 return cronetEngine; | 693 return cronetEngine; |
694 } | 694 } |
695 } | 695 } |
OLD | NEW |