| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.impl; | 5 package org.chromium.net.impl; |
| 6 | 6 |
| 7 import android.util.Log; | 7 import org.chromium.base.Log; |
| 8 | |
| 9 import org.chromium.base.VisibleForTesting; | 8 import org.chromium.base.VisibleForTesting; |
| 10 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
| 11 import org.chromium.base.annotations.JNIAdditionalImport; | 10 import org.chromium.base.annotations.JNIAdditionalImport; |
| 12 import org.chromium.base.annotations.JNINamespace; | 11 import org.chromium.base.annotations.JNINamespace; |
| 13 import org.chromium.base.annotations.NativeClassQualifiedName; | 12 import org.chromium.base.annotations.NativeClassQualifiedName; |
| 14 import org.chromium.net.InlineExecutionProhibitedException; | 13 import org.chromium.net.InlineExecutionProhibitedException; |
| 15 import org.chromium.net.Preconditions; | |
| 16 import org.chromium.net.QuicException; | 14 import org.chromium.net.QuicException; |
| 17 import org.chromium.net.RequestFinishedInfo; | 15 import org.chromium.net.RequestFinishedInfo; |
| 18 import org.chromium.net.RequestPriority; | 16 import org.chromium.net.RequestPriority; |
| 19 import org.chromium.net.UploadDataProvider; | 17 import org.chromium.net.UploadDataProvider; |
| 20 import org.chromium.net.UrlRequest; | 18 import org.chromium.net.UrlRequest; |
| 21 import org.chromium.net.UrlRequestException; | 19 import org.chromium.net.UrlRequestException; |
| 22 import org.chromium.net.UrlResponseInfo; | 20 import org.chromium.net.UrlResponseInfo; |
| 23 | 21 |
| 24 import java.nio.ByteBuffer; | 22 import java.nio.ByteBuffer; |
| 25 import java.util.AbstractMap; | 23 import java.util.AbstractMap; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 38 * All @CallByNative methods are called on native network thread | 36 * All @CallByNative methods are called on native network thread |
| 39 * and post tasks with listener calls onto Executor. Upon return from listener | 37 * and post tasks with listener calls onto Executor. Upon return from listener |
| 40 * callback native request adapter is called on executive thread and posts | 38 * callback native request adapter is called on executive thread and posts |
| 41 * native tasks to native network thread. Because Cancel could be called from | 39 * native tasks to native network thread. Because Cancel could be called from |
| 42 * any thread it is protected by mUrlRequestAdapterLock. | 40 * any thread it is protected by mUrlRequestAdapterLock. |
| 43 */ | 41 */ |
| 44 @JNINamespace("cronet") | 42 @JNINamespace("cronet") |
| 45 // Qualifies UrlRequest.StatusListener which is used in onStatus, a JNI method. | 43 // Qualifies UrlRequest.StatusListener which is used in onStatus, a JNI method. |
| 46 @JNIAdditionalImport(UrlRequest.class) | 44 @JNIAdditionalImport(UrlRequest.class) |
| 47 @VisibleForTesting | 45 @VisibleForTesting |
| 48 public final class CronetUrlRequest implements UrlRequest { | 46 public final class CronetUrlRequest extends UrlRequestBase { |
| 49 private final boolean mAllowDirectExecutor; | 47 private final boolean mAllowDirectExecutor; |
| 50 | 48 |
| 51 /* Native adapter object, owned by UrlRequest. */ | 49 /* Native adapter object, owned by UrlRequest. */ |
| 52 @GuardedBy("mUrlRequestAdapterLock") | 50 @GuardedBy("mUrlRequestAdapterLock") |
| 53 private long mUrlRequestAdapter; | 51 private long mUrlRequestAdapter; |
| 54 | 52 |
| 55 @GuardedBy("mUrlRequestAdapterLock") | 53 @GuardedBy("mUrlRequestAdapterLock") |
| 56 private boolean mStarted = false; | 54 private boolean mStarted = false; |
| 57 @GuardedBy("mUrlRequestAdapterLock") | 55 @GuardedBy("mUrlRequestAdapterLock") |
| 58 private boolean mWaitingOnRedirect = false; | 56 private boolean mWaitingOnRedirect = false; |
| (...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in onCompl
ete method", e); | 620 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in onCompl
ete method", e); |
| 623 } | 621 } |
| 624 } | 622 } |
| 625 }; | 623 }; |
| 626 postTaskToExecutor(task); | 624 postTaskToExecutor(task); |
| 627 } | 625 } |
| 628 | 626 |
| 629 /** | 627 /** |
| 630 * Called when error has occured, no callbacks will be called afterwards. | 628 * Called when error has occured, no callbacks will be called afterwards. |
| 631 * | 629 * |
| 632 * @param errorCode error code from {@link UrlRequestException.ERROR_LISTENE
R_EXCEPTION_THROWN | 630 * @param errorCode Error code represented by {@code UrlRequestError} that s
hould be mapped |
| 633 * UrlRequestException.ERROR_*}. | 631 * to one of {@link UrlRequestException#ERROR_LISTENER_EXCE
PTION_THROWN |
| 632 * UrlRequestException.ERROR_*}. |
| 634 * @param nativeError native net error code. | 633 * @param nativeError native net error code. |
| 635 * @param errorString textual representation of the error code. | 634 * @param errorString textual representation of the error code. |
| 636 * @param receivedBytesCount number of bytes received. | 635 * @param receivedBytesCount number of bytes received. |
| 637 */ | 636 */ |
| 638 @SuppressWarnings("unused") | 637 @SuppressWarnings("unused") |
| 639 @CalledByNative | 638 @CalledByNative |
| 640 private void onError(int errorCode, int nativeError, int nativeQuicError, St
ring errorString, | 639 private void onError(int errorCode, int nativeError, int nativeQuicError, St
ring errorString, |
| 641 long receivedBytesCount) { | 640 long receivedBytesCount) { |
| 642 mFinishedReason = RequestFinishedInfo.FAILED; | 641 mFinishedReason = RequestFinishedInfo.FAILED; |
| 643 if (mResponseInfo != null) { | 642 if (mResponseInfo != null) { |
| 644 mResponseInfo.setReceivedBytesCount( | 643 mResponseInfo.setReceivedBytesCount( |
| 645 mReceivedBytesCountFromRedirects + receivedBytesCount); | 644 mReceivedBytesCountFromRedirects + receivedBytesCount); |
| 646 } | 645 } |
| 647 if (errorCode == UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED) { | 646 if (errorCode == UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED) { |
| 648 failWithException(new QuicException( | 647 failWithException(new QuicException( |
| 649 "Exception in CronetUrlRequest: " + errorString, nativeError
, nativeQuicError)); | 648 "Exception in CronetUrlRequest: " + errorString, nativeError
, nativeQuicError)); |
| 650 } else { | 649 } else { |
| 650 int javaError = mapUrlRequestErrorToApiErrorCode(errorCode); |
| 651 failWithException(new UrlRequestException( | 651 failWithException(new UrlRequestException( |
| 652 "Exception in CronetUrlRequest: " + errorString, errorCode,
nativeError)); | 652 "Exception in CronetUrlRequest: " + errorString, javaError,
nativeError)); |
| 653 } | 653 } |
| 654 } | 654 } |
| 655 | 655 |
| 656 /** | 656 /** |
| 657 * Called when request is canceled, no callbacks will be called afterwards. | 657 * Called when request is canceled, no callbacks will be called afterwards. |
| 658 */ | 658 */ |
| 659 @SuppressWarnings("unused") | 659 @SuppressWarnings("unused") |
| 660 @CalledByNative | 660 @CalledByNative |
| 661 private void onCanceled() { | 661 private void onCanceled() { |
| 662 mFinishedReason = RequestFinishedInfo.CANCELED; | 662 mFinishedReason = RequestFinishedInfo.CANCELED; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 676 /** | 676 /** |
| 677 * Called by the native code when request status is fetched from the | 677 * Called by the native code when request status is fetched from the |
| 678 * native stack. | 678 * native stack. |
| 679 */ | 679 */ |
| 680 @SuppressWarnings("unused") | 680 @SuppressWarnings("unused") |
| 681 @CalledByNative | 681 @CalledByNative |
| 682 private void onStatus(final UrlRequest.StatusListener listener, final int lo
adState) { | 682 private void onStatus(final UrlRequest.StatusListener listener, final int lo
adState) { |
| 683 Runnable task = new Runnable() { | 683 Runnable task = new Runnable() { |
| 684 @Override | 684 @Override |
| 685 public void run() { | 685 public void run() { |
| 686 listener.onStatus(UrlRequest.Status.convertLoadState(loadState))
; | 686 listener.onStatus(convertLoadState(loadState)); |
| 687 } | 687 } |
| 688 }; | 688 }; |
| 689 postTaskToExecutor(task); | 689 postTaskToExecutor(task); |
| 690 } | 690 } |
| 691 | 691 |
| 692 /** | 692 /** |
| 693 * Called by the native code to report metrics. | 693 * Called by the native code to report metrics. |
| 694 */ | 694 */ |
| 695 @SuppressWarnings("unused") | 695 @SuppressWarnings("unused") |
| 696 @CalledByNative | 696 @CalledByNative |
| (...skipping 20 matching lines...) Expand all Loading... |
| 717 mResponseInfo, mException); | 717 mResponseInfo, mException); |
| 718 } | 718 } |
| 719 | 719 |
| 720 /** Enforces prohibition of direct execution. */ | 720 /** Enforces prohibition of direct execution. */ |
| 721 void checkCallingThread() { | 721 void checkCallingThread() { |
| 722 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr
entThread())) { | 722 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr
entThread())) { |
| 723 throw new InlineExecutionProhibitedException(); | 723 throw new InlineExecutionProhibitedException(); |
| 724 } | 724 } |
| 725 } | 725 } |
| 726 | 726 |
| 727 private int mapUrlRequestErrorToApiErrorCode(int errorCode) { |
| 728 switch (errorCode) { |
| 729 case UrlRequestError.LISTENER_EXCEPTION_THROWN: |
| 730 return UrlRequestException.ERROR_LISTENER_EXCEPTION_THROWN; |
| 731 case UrlRequestError.HOSTNAME_NOT_RESOLVED: |
| 732 return UrlRequestException.ERROR_HOSTNAME_NOT_RESOLVED; |
| 733 case UrlRequestError.INTERNET_DISCONNECTED: |
| 734 return UrlRequestException.ERROR_INTERNET_DISCONNECTED; |
| 735 case UrlRequestError.NETWORK_CHANGED: |
| 736 return UrlRequestException.ERROR_NETWORK_CHANGED; |
| 737 case UrlRequestError.TIMED_OUT: |
| 738 return UrlRequestException.ERROR_TIMED_OUT; |
| 739 case UrlRequestError.CONNECTION_CLOSED: |
| 740 return UrlRequestException.ERROR_CONNECTION_CLOSED; |
| 741 case UrlRequestError.CONNECTION_TIMED_OUT: |
| 742 return UrlRequestException.ERROR_CONNECTION_TIMED_OUT; |
| 743 case UrlRequestError.CONNECTION_REFUSED: |
| 744 return UrlRequestException.ERROR_CONNECTION_REFUSED; |
| 745 case UrlRequestError.CONNECTION_RESET: |
| 746 return UrlRequestException.ERROR_CONNECTION_RESET; |
| 747 case UrlRequestError.ADDRESS_UNREACHABLE: |
| 748 return UrlRequestException.ERROR_ADDRESS_UNREACHABLE; |
| 749 case UrlRequestError.QUIC_PROTOCOL_FAILED: |
| 750 return UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED; |
| 751 case UrlRequestError.OTHER: |
| 752 return UrlRequestException.ERROR_OTHER; |
| 753 default: |
| 754 Log.e(CronetUrlRequestContext.LOG_TAG, "Unknown error code: " +
errorCode); |
| 755 return errorCode; |
| 756 } |
| 757 } |
| 758 |
| 727 // Native methods are implemented in cronet_url_request_adapter.cc. | 759 // Native methods are implemented in cronet_url_request_adapter.cc. |
| 728 | 760 |
| 729 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter
, String url, | 761 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter
, String url, |
| 730 int priority, boolean disableCache, boolean disableConnectionMigrati
on, | 762 int priority, boolean disableCache, boolean disableConnectionMigrati
on, |
| 731 boolean enableMetrics); | 763 boolean enableMetrics); |
| 732 | 764 |
| 733 @NativeClassQualifiedName("CronetURLRequestAdapter") | 765 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 734 private native boolean nativeSetHttpMethod(long nativePtr, String method); | 766 private native boolean nativeSetHttpMethod(long nativePtr, String method); |
| 735 | 767 |
| 736 @NativeClassQualifiedName("CronetURLRequestAdapter") | 768 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 737 private native boolean nativeAddRequestHeader(long nativePtr, String name, S
tring value); | 769 private native boolean nativeAddRequestHeader(long nativePtr, String name, S
tring value); |
| 738 | 770 |
| 739 @NativeClassQualifiedName("CronetURLRequestAdapter") | 771 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 740 private native void nativeStart(long nativePtr); | 772 private native void nativeStart(long nativePtr); |
| 741 | 773 |
| 742 @NativeClassQualifiedName("CronetURLRequestAdapter") | 774 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 743 private native void nativeFollowDeferredRedirect(long nativePtr); | 775 private native void nativeFollowDeferredRedirect(long nativePtr); |
| 744 | 776 |
| 745 @NativeClassQualifiedName("CronetURLRequestAdapter") | 777 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 746 private native boolean nativeReadData( | 778 private native boolean nativeReadData( |
| 747 long nativePtr, ByteBuffer byteBuffer, int position, int capacity); | 779 long nativePtr, ByteBuffer byteBuffer, int position, int capacity); |
| 748 | 780 |
| 749 @NativeClassQualifiedName("CronetURLRequestAdapter") | 781 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 750 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); | 782 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); |
| 751 | 783 |
| 752 @NativeClassQualifiedName("CronetURLRequestAdapter") | 784 @NativeClassQualifiedName("CronetURLRequestAdapter") |
| 753 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene
r listener); | 785 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene
r listener); |
| 754 } | 786 } |
| OLD | NEW |