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

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

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebased onto Charles change + Paul's Comments Created 4 years, 2 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 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.os.SystemClock; 7 import android.os.SystemClock;
8 import android.support.annotation.Nullable; 8 import android.support.annotation.Nullable;
9 import android.util.Log;
10 9
10 import org.chromium.base.Log;
11 import org.chromium.base.VisibleForTesting; 11 import org.chromium.base.VisibleForTesting;
12 import org.chromium.base.annotations.CalledByNative; 12 import org.chromium.base.annotations.CalledByNative;
13 import org.chromium.base.annotations.JNIAdditionalImport; 13 import org.chromium.base.annotations.JNIAdditionalImport;
14 import org.chromium.base.annotations.JNINamespace; 14 import org.chromium.base.annotations.JNINamespace;
15 import org.chromium.base.annotations.NativeClassQualifiedName; 15 import org.chromium.base.annotations.NativeClassQualifiedName;
16 import org.chromium.net.InlineExecutionProhibitedException; 16 import org.chromium.net.InlineExecutionProhibitedException;
17 import org.chromium.net.Preconditions;
18 import org.chromium.net.QuicException; 17 import org.chromium.net.QuicException;
19 import org.chromium.net.RequestFinishedInfo; 18 import org.chromium.net.RequestFinishedInfo;
20 import org.chromium.net.RequestPriority; 19 import org.chromium.net.RequestPriority;
21 import org.chromium.net.UploadDataProvider; 20 import org.chromium.net.UploadDataProvider;
22 import org.chromium.net.UrlRequest; 21 import org.chromium.net.UrlRequest;
23 import org.chromium.net.UrlRequestException; 22 import org.chromium.net.UrlRequestException;
24 import org.chromium.net.UrlResponseInfo; 23 import org.chromium.net.UrlResponseInfo;
25 24
26 import java.nio.ByteBuffer; 25 import java.nio.ByteBuffer;
27 import java.util.AbstractMap; 26 import java.util.AbstractMap;
(...skipping 12 matching lines...) Expand all
40 * All @CallByNative methods are called on native network thread 39 * All @CallByNative methods are called on native network thread
41 * and post tasks with listener calls onto Executor. Upon return from listener 40 * and post tasks with listener calls onto Executor. Upon return from listener
42 * callback native request adapter is called on executive thread and posts 41 * callback native request adapter is called on executive thread and posts
43 * native tasks to native network thread. Because Cancel could be called from 42 * native tasks to native network thread. Because Cancel could be called from
44 * any thread it is protected by mUrlRequestAdapterLock. 43 * any thread it is protected by mUrlRequestAdapterLock.
45 */ 44 */
46 @JNINamespace("cronet") 45 @JNINamespace("cronet")
47 // Qualifies UrlRequest.StatusListener which is used in onStatus, a JNI method. 46 // Qualifies UrlRequest.StatusListener which is used in onStatus, a JNI method.
48 @JNIAdditionalImport(UrlRequest.class) 47 @JNIAdditionalImport(UrlRequest.class)
49 @VisibleForTesting 48 @VisibleForTesting
50 public final class CronetUrlRequest implements UrlRequest { 49 public final class CronetUrlRequest extends UrlRequestBase {
51 private static final RequestFinishedInfo.Metrics EMPTY_METRICS = 50 private static final RequestFinishedInfo.Metrics EMPTY_METRICS =
52 new CronetMetrics(null, null, null, null); 51 new CronetMetrics(null, null, null, null);
53 private final boolean mAllowDirectExecutor; 52 private final boolean mAllowDirectExecutor;
54 53
55 /* Native adapter object, owned by UrlRequest. */ 54 /* Native adapter object, owned by UrlRequest. */
56 @GuardedBy("mUrlRequestAdapterLock") 55 @GuardedBy("mUrlRequestAdapterLock")
57 private long mUrlRequestAdapter; 56 private long mUrlRequestAdapter;
58 57
59 @GuardedBy("mUrlRequestAdapterLock") 58 @GuardedBy("mUrlRequestAdapterLock")
60 private boolean mStarted = false; 59 private boolean mStarted = false;
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in onCompl ete method", e); 633 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception in onCompl ete method", e);
635 } 634 }
636 } 635 }
637 }; 636 };
638 postTaskToExecutor(task); 637 postTaskToExecutor(task);
639 } 638 }
640 639
641 /** 640 /**
642 * Called when error has occured, no callbacks will be called afterwards. 641 * Called when error has occured, no callbacks will be called afterwards.
643 * 642 *
644 * @param errorCode error code from {@link UrlRequestException.ERROR_LISTENE R_EXCEPTION_THROWN 643 * @param errorCode Error code represented by {@code UrlRequestError} that s hould be mapped
645 * UrlRequestException.ERROR_*}. 644 * to one of {@link UrlRequestException#ERROR_LISTENER_EXCE PTION_THROWN
645 * UrlRequestException.ERROR_*}.
646 * @param nativeError native net error code. 646 * @param nativeError native net error code.
647 * @param errorString textual representation of the error code. 647 * @param errorString textual representation of the error code.
648 * @param receivedBytesCount number of bytes received. 648 * @param receivedBytesCount number of bytes received.
649 */ 649 */
650 @SuppressWarnings("unused") 650 @SuppressWarnings("unused")
651 @CalledByNative 651 @CalledByNative
652 private void onError(int errorCode, int nativeError, int nativeQuicError, St ring errorString, 652 private void onError(int errorCode, int nativeError, int nativeQuicError, St ring errorString,
653 long receivedBytesCount) { 653 long receivedBytesCount) {
654 if (mResponseInfo != null) { 654 if (mResponseInfo != null) {
655 mResponseInfo.setReceivedBytesCount( 655 mResponseInfo.setReceivedBytesCount(
656 mReceivedBytesCountFromRedirects + receivedBytesCount); 656 mReceivedBytesCountFromRedirects + receivedBytesCount);
657 } 657 }
658 if (errorCode == UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED) { 658 if (errorCode == UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED) {
659 failWithException(new QuicException( 659 failWithException(new QuicException(
660 "Exception in CronetUrlRequest: " + errorString, nativeError , nativeQuicError)); 660 "Exception in CronetUrlRequest: " + errorString, nativeError , nativeQuicError));
661 } else { 661 } else {
662 int javaError = mapUrlRequestErrorToApiErrorCode(errorCode);
662 failWithException(new UrlRequestException( 663 failWithException(new UrlRequestException(
663 "Exception in CronetUrlRequest: " + errorString, errorCode, nativeError)); 664 "Exception in CronetUrlRequest: " + errorString, javaError, nativeError));
664 } 665 }
665 } 666 }
666 667
667 /** 668 /**
668 * Called when request is canceled, no callbacks will be called afterwards. 669 * Called when request is canceled, no callbacks will be called afterwards.
669 */ 670 */
670 @SuppressWarnings("unused") 671 @SuppressWarnings("unused")
671 @CalledByNative 672 @CalledByNative
672 private void onCanceled() { 673 private void onCanceled() {
673 Runnable task = new Runnable() { 674 Runnable task = new Runnable() {
(...skipping 12 matching lines...) Expand all
686 /** 687 /**
687 * Called by the native code when request status is fetched from the 688 * Called by the native code when request status is fetched from the
688 * native stack. 689 * native stack.
689 */ 690 */
690 @SuppressWarnings("unused") 691 @SuppressWarnings("unused")
691 @CalledByNative 692 @CalledByNative
692 private void onStatus(final UrlRequest.StatusListener listener, final int lo adState) { 693 private void onStatus(final UrlRequest.StatusListener listener, final int lo adState) {
693 Runnable task = new Runnable() { 694 Runnable task = new Runnable() {
694 @Override 695 @Override
695 public void run() { 696 public void run() {
696 listener.onStatus(UrlRequest.Status.convertLoadState(loadState)) ; 697 listener.onStatus(UrlRequestBase.Status.convertLoadState(loadSta te));
697 } 698 }
698 }; 699 };
699 postTaskToExecutor(task); 700 postTaskToExecutor(task);
700 } 701 }
701 702
702 private RequestFinishedInfo getRequestFinishedInfo() { 703 private RequestFinishedInfo getRequestFinishedInfo() {
703 // TODO(mgersh): fill in real values for finishedReason and exception 704 // TODO(mgersh): fill in real values for finishedReason and exception
704 return new RequestFinishedInfo(mInitialUrl, mRequestAnnotations, 705 return new RequestFinishedInfo(mInitialUrl, mRequestAnnotations,
705 (mRequestMetricsAccumulator != null ? mRequestMetricsAccumulator .getRequestMetrics() 706 (mRequestMetricsAccumulator != null ? mRequestMetricsAccumulator .getRequestMetrics()
706 : EMPTY_METRICS), 707 : EMPTY_METRICS),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 } 742 }
742 } 743 }
743 744
744 /** Enforces prohibition of direct execution. */ 745 /** Enforces prohibition of direct execution. */
745 void checkCallingThread() { 746 void checkCallingThread() {
746 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr entThread())) { 747 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr entThread())) {
747 throw new InlineExecutionProhibitedException(); 748 throw new InlineExecutionProhibitedException();
748 } 749 }
749 } 750 }
750 751
752 private int mapUrlRequestErrorToApiErrorCode(int errorCode) {
753 switch (errorCode) {
754 case UrlRequestError.LISTENER_EXCEPTION_THROWN:
755 return UrlRequestException.ERROR_LISTENER_EXCEPTION_THROWN;
756 case UrlRequestError.HOSTNAME_NOT_RESOLVED:
757 return UrlRequestException.ERROR_HOSTNAME_NOT_RESOLVED;
758 case UrlRequestError.INTERNET_DISCONNECTED:
759 return UrlRequestException.ERROR_INTERNET_DISCONNECTED;
760 case UrlRequestError.NETWORK_CHANGED:
761 return UrlRequestException.ERROR_NETWORK_CHANGED;
762 case UrlRequestError.TIMED_OUT:
763 return UrlRequestException.ERROR_TIMED_OUT;
764 case UrlRequestError.CONNECTION_CLOSED:
765 return UrlRequestException.ERROR_CONNECTION_CLOSED;
766 case UrlRequestError.CONNECTION_TIMED_OUT:
767 return UrlRequestException.ERROR_CONNECTION_TIMED_OUT;
768 case UrlRequestError.CONNECTION_REFUSED:
769 return UrlRequestException.ERROR_CONNECTION_REFUSED;
770 case UrlRequestError.CONNECTION_RESET:
771 return UrlRequestException.ERROR_CONNECTION_RESET;
772 case UrlRequestError.ADDRESS_UNREACHABLE:
773 return UrlRequestException.ERROR_ADDRESS_UNREACHABLE;
774 case UrlRequestError.QUIC_PROTOCOL_FAILED:
775 return UrlRequestException.ERROR_QUIC_PROTOCOL_FAILED;
776 case UrlRequestError.OTHER:
777 return UrlRequestException.ERROR_OTHER;
778 default:
779 Log.e(CronetUrlRequestContext.LOG_TAG, "Unknown error code: " + errorCode);
780 return errorCode;
781 }
782 }
783
751 // Native methods are implemented in cronet_url_request_adapter.cc. 784 // Native methods are implemented in cronet_url_request_adapter.cc.
752 785
753 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter , String url, 786 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter , String url,
754 int priority, boolean disableCache, boolean disableConnectionMigrati on); 787 int priority, boolean disableCache, boolean disableConnectionMigrati on);
755 788
756 @NativeClassQualifiedName("CronetURLRequestAdapter") 789 @NativeClassQualifiedName("CronetURLRequestAdapter")
757 private native boolean nativeSetHttpMethod(long nativePtr, String method); 790 private native boolean nativeSetHttpMethod(long nativePtr, String method);
758 791
759 @NativeClassQualifiedName("CronetURLRequestAdapter") 792 @NativeClassQualifiedName("CronetURLRequestAdapter")
760 private native boolean nativeAddRequestHeader(long nativePtr, String name, S tring value); 793 private native boolean nativeAddRequestHeader(long nativePtr, String name, S tring value);
761 794
762 @NativeClassQualifiedName("CronetURLRequestAdapter") 795 @NativeClassQualifiedName("CronetURLRequestAdapter")
763 private native void nativeStart(long nativePtr); 796 private native void nativeStart(long nativePtr);
764 797
765 @NativeClassQualifiedName("CronetURLRequestAdapter") 798 @NativeClassQualifiedName("CronetURLRequestAdapter")
766 private native void nativeFollowDeferredRedirect(long nativePtr); 799 private native void nativeFollowDeferredRedirect(long nativePtr);
767 800
768 @NativeClassQualifiedName("CronetURLRequestAdapter") 801 @NativeClassQualifiedName("CronetURLRequestAdapter")
769 private native boolean nativeReadData( 802 private native boolean nativeReadData(
770 long nativePtr, ByteBuffer byteBuffer, int position, int capacity); 803 long nativePtr, ByteBuffer byteBuffer, int position, int capacity);
771 804
772 @NativeClassQualifiedName("CronetURLRequestAdapter") 805 @NativeClassQualifiedName("CronetURLRequestAdapter")
773 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); 806 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled);
774 807
775 @NativeClassQualifiedName("CronetURLRequestAdapter") 808 @NativeClassQualifiedName("CronetURLRequestAdapter")
776 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener); 809 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener);
777 } 810 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698