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

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebase & Conflict Resolution Created 4 years, 1 month 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 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
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
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
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 19 matching lines...) Expand all
716 mResponseInfo, mException); 716 mResponseInfo, mException);
717 } 717 }
718 718
719 /** Enforces prohibition of direct execution. */ 719 /** Enforces prohibition of direct execution. */
720 void checkCallingThread() { 720 void checkCallingThread() {
721 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr entThread())) { 721 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr entThread())) {
722 throw new InlineExecutionProhibitedException(); 722 throw new InlineExecutionProhibitedException();
723 } 723 }
724 } 724 }
725 725
726 private int mapUrlRequestErrorToApiErrorCode(int errorCode) {
727 switch (errorCode) {
728 case UrlRequestError.LISTENER_EXCEPTION_THROWN:
729 return UrlRequestException.ERROR_LISTENER_EXCEPTION_THROWN;
730 case UrlRequestError.HOSTNAME_NOT_RESOLVED:
731 return UrlRequestException.ERROR_HOSTNAME_NOT_RESOLVED;
732 case UrlRequestError.INTERNET_DISCONNECTED:
733 return UrlRequestException.ERROR_INTERNET_DISCONNECTED;
734 case UrlRequestError.NETWORK_CHANGED:
735 return UrlRequestException.ERROR_NETWORK_CHANGED;
736 case UrlRequestError.TIMED_OUT:
737 return UrlRequestException.ERROR_TIMED_OUT;
738 case UrlRequestError.CONNECTION_CLOSED:
739 return UrlRequestException.ERROR_CONNECTION_CLOSED;
740 case UrlRequestError.CONNECTION_TIMED_OUT:
741 return UrlRequestException.ERROR_CONNECTION_TIMED_OUT;
742 case UrlRequestError.CONNECTION_REFUSED:
743 return UrlRequestException.ERROR_CONNECTION_REFUSED;
744 case UrlRequestError.CONNECTION_RESET:
745 return UrlRequestException.ERROR_CONNECTION_RESET;
746 case UrlRequestError.ADDRESS_UNREACHABLE:
747 return UrlRequestException.ERROR_ADDRESS_UNREACHABLE;
748 case UrlRequestError.QUIC_PROTOCOL_FAILED:
749 return UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED;
750 case UrlRequestError.OTHER:
751 return UrlRequestException.ERROR_OTHER;
752 default:
753 Log.e(CronetUrlRequestContext.LOG_TAG, "Unknown error code: " + errorCode);
754 return errorCode;
755 }
756 }
757
726 // Native methods are implemented in cronet_url_request_adapter.cc. 758 // Native methods are implemented in cronet_url_request_adapter.cc.
727 759
728 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter , String url, 760 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter , String url,
729 int priority, boolean disableCache, boolean disableConnectionMigrati on, 761 int priority, boolean disableCache, boolean disableConnectionMigrati on,
730 boolean enableMetrics); 762 boolean enableMetrics);
731 763
732 @NativeClassQualifiedName("CronetURLRequestAdapter") 764 @NativeClassQualifiedName("CronetURLRequestAdapter")
733 private native boolean nativeSetHttpMethod(long nativePtr, String method); 765 private native boolean nativeSetHttpMethod(long nativePtr, String method);
734 766
735 @NativeClassQualifiedName("CronetURLRequestAdapter") 767 @NativeClassQualifiedName("CronetURLRequestAdapter")
736 private native boolean nativeAddRequestHeader(long nativePtr, String name, S tring value); 768 private native boolean nativeAddRequestHeader(long nativePtr, String name, S tring value);
737 769
738 @NativeClassQualifiedName("CronetURLRequestAdapter") 770 @NativeClassQualifiedName("CronetURLRequestAdapter")
739 private native void nativeStart(long nativePtr); 771 private native void nativeStart(long nativePtr);
740 772
741 @NativeClassQualifiedName("CronetURLRequestAdapter") 773 @NativeClassQualifiedName("CronetURLRequestAdapter")
742 private native void nativeFollowDeferredRedirect(long nativePtr); 774 private native void nativeFollowDeferredRedirect(long nativePtr);
743 775
744 @NativeClassQualifiedName("CronetURLRequestAdapter") 776 @NativeClassQualifiedName("CronetURLRequestAdapter")
745 private native boolean nativeReadData( 777 private native boolean nativeReadData(
746 long nativePtr, ByteBuffer byteBuffer, int position, int capacity); 778 long nativePtr, ByteBuffer byteBuffer, int position, int capacity);
747 779
748 @NativeClassQualifiedName("CronetURLRequestAdapter") 780 @NativeClassQualifiedName("CronetURLRequestAdapter")
749 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); 781 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled);
750 782
751 @NativeClassQualifiedName("CronetURLRequestAdapter") 783 @NativeClassQualifiedName("CronetURLRequestAdapter")
752 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener); 784 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener);
753 } 785 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698