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

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequest.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 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.impl;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.net.TrafficStats; 8 import android.net.TrafficStats;
9 import android.os.Build; 9 import android.os.Build;
10 import android.util.Log; 10
11 import org.chromium.base.Log;
12 import org.chromium.net.InlineExecutionProhibitedException;
13 import org.chromium.net.UploadDataProvider;
14 import org.chromium.net.UploadDataSink;
15 import org.chromium.net.UrlRequestException;
16 import org.chromium.net.UrlResponseInfo;
11 17
12 import java.io.Closeable; 18 import java.io.Closeable;
13 import java.io.IOException; 19 import java.io.IOException;
14 import java.io.OutputStream; 20 import java.io.OutputStream;
15 import java.net.HttpURLConnection; 21 import java.net.HttpURLConnection;
16 import java.net.URI; 22 import java.net.URI;
17 import java.net.URL; 23 import java.net.URL;
18 import java.nio.ByteBuffer; 24 import java.nio.ByteBuffer;
19 import java.nio.channels.Channels; 25 import java.nio.channels.Channels;
20 import java.nio.channels.ReadableByteChannel; 26 import java.nio.channels.ReadableByteChannel;
21 import java.nio.channels.WritableByteChannel; 27 import java.nio.channels.WritableByteChannel;
22 import java.util.AbstractMap.SimpleEntry; 28 import java.util.AbstractMap.SimpleEntry;
23 import java.util.ArrayList; 29 import java.util.ArrayList;
24 import java.util.Collections; 30 import java.util.Collections;
25 import java.util.List; 31 import java.util.List;
26 import java.util.Map; 32 import java.util.Map;
27 import java.util.TreeMap; 33 import java.util.TreeMap;
28 import java.util.concurrent.Executor; 34 import java.util.concurrent.Executor;
29 import java.util.concurrent.RejectedExecutionException; 35 import java.util.concurrent.RejectedExecutionException;
30 import java.util.concurrent.atomic.AtomicBoolean; 36 import java.util.concurrent.atomic.AtomicBoolean;
31 import java.util.concurrent.atomic.AtomicReference; 37 import java.util.concurrent.atomic.AtomicReference;
32 38
33 /** 39 /**
34 * Pure java UrlRequest, backed by {@link HttpURLConnection}. 40 * Pure java UrlRequest, backed by {@link HttpURLConnection}.
35 */ 41 */
36 @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) // TrafficStats only availabl e on ICS 42 @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) // TrafficStats only availabl e on ICS
37 final class JavaUrlRequest implements UrlRequest { 43 final class JavaUrlRequest extends UrlRequestBase {
38 private static final String X_ANDROID = "X-Android"; 44 private static final String X_ANDROID = "X-Android";
39 private static final String X_ANDROID_SELECTED_TRANSPORT = "X-Android-Select ed-Transport"; 45 private static final String X_ANDROID_SELECTED_TRANSPORT = "X-Android-Select ed-Transport";
40 private static final String TAG = "JavaUrlConnection"; 46 private static final String TAG = "JavaUrlConnection";
41 private static final int DEFAULT_UPLOAD_BUFFER_SIZE = 8192; 47 private static final int DEFAULT_UPLOAD_BUFFER_SIZE = 8192;
42 private static final int DEFAULT_CHUNK_LENGTH = DEFAULT_UPLOAD_BUFFER_SIZE; 48 private static final int DEFAULT_CHUNK_LENGTH = DEFAULT_UPLOAD_BUFFER_SIZE;
43 private static final String USER_AGENT = "User-Agent"; 49 private static final String USER_AGENT = "User-Agent";
44 private final AsyncUrlRequestCallback mCallbackAsync; 50 private final AsyncUrlRequestCallback mCallbackAsync;
45 private final Executor mExecutor; 51 private final Executor mExecutor;
46 private final String mUserAgent; 52 private final String mUserAgent;
47 private final Map<String, String> mRequestHeaders = 53 private final Map<String, String> mRequestHeaders =
(...skipping 27 matching lines...) Expand all
75 /** 81 /**
76 * Holds a subset of StatusValues - {@link State#STARTED} can represent 82 * Holds a subset of StatusValues - {@link State#STARTED} can represent
77 * {@link Status#SENDING_REQUEST} or {@link Status#WAITING_FOR_RESPONSE}. Wh ile the distinction 83 * {@link Status#SENDING_REQUEST} or {@link Status#WAITING_FOR_RESPONSE}. Wh ile the distinction
78 * isn't needed to implement the logic in this class, it is needed to implem ent 84 * isn't needed to implement the logic in this class, it is needed to implem ent
79 * {@link #getStatus(StatusListener)}. 85 * {@link #getStatus(StatusListener)}.
80 * 86 *
81 * <p>Concurrency notes - this value is not atomically updated with mState, so there is some 87 * <p>Concurrency notes - this value is not atomically updated with mState, so there is some
82 * risk that we'd get an inconsistent snapshot of both - however, it also ha ppens that this 88 * risk that we'd get an inconsistent snapshot of both - however, it also ha ppens that this
83 * value is only used with the STARTED state, so it's inconsequential. 89 * value is only used with the STARTED state, so it's inconsequential.
84 */ 90 */
85 @Status.StatusValues private volatile int mAdditionalStatusDetails = Status. INVALID; 91 @Status.StatusValues
92 private volatile int mAdditionalStatusDetails = Status.INVALID;
86 93
87 /* These change with redirects. */ 94 /* These change with redirects. */
88 private String mCurrentUrl; 95 private String mCurrentUrl;
89 private ReadableByteChannel mResponseChannel; 96 private ReadableByteChannel mResponseChannel;
90 private UrlResponseInfo mUrlResponseInfo; 97 private UrlResponseInfo mUrlResponseInfo;
91 private String mPendingRedirectUrl; 98 private String mPendingRedirectUrl;
92 /** 99 /**
93 * The happens-before edges created by the executor submission and AtomicRef erence setting are 100 * The happens-before edges created by the executor submission and AtomicRef erence setting are
94 * sufficient to guarantee the correct behavior of this field; however, this is an 101 * sufficient to guarantee the correct behavior of this field; however, this is an
95 * AtomicReference so that we can cleanly dispose of a new connection if we' re cancelled during 102 * AtomicReference so that we can cleanly dispose of a new connection if we' re cancelled during
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 @Override 355 @Override
349 public void onRewindError(Exception exception) { 356 public void onRewindError(Exception exception) {
350 enterUploadErrorState(exception); 357 enterUploadErrorState(exception);
351 } 358 }
352 359
353 void startRead() { 360 void startRead() {
354 mExecutor.execute(errorSetting(new CheckedRunnable() { 361 mExecutor.execute(errorSetting(new CheckedRunnable() {
355 @Override 362 @Override
356 public void run() throws Exception { 363 public void run() throws Exception {
357 if (mOutputChannel == null) { 364 if (mOutputChannel == null) {
358 mAdditionalStatusDetails = Status.CONNECTING; 365 mAdditionalStatusDetails = UrlRequestBase.Status.CONNECT ING;
359 mUrlConnection.connect(); 366 mUrlConnection.connect();
360 mAdditionalStatusDetails = Status.SENDING_REQUEST; 367 mAdditionalStatusDetails = Status.SENDING_REQUEST;
361 mUrlConnectionOutputStream = mUrlConnection.getOutputStr eam(); 368 mUrlConnectionOutputStream = mUrlConnection.getOutputStr eam();
362 mOutputChannel = Channels.newChannel(mUrlConnectionOutpu tStream); 369 mOutputChannel = Channels.newChannel(mUrlConnectionOutpu tStream);
363 } 370 }
364 mSinkState.set(SinkState.AWAITING_READ_RESULT); 371 mSinkState.set(SinkState.AWAITING_READ_RESULT);
365 executeOnUploadExecutor(new CheckedRunnable() { 372 executeOnUploadExecutor(new CheckedRunnable() {
366 @Override 373 @Override
367 public void run() throws Exception { 374 public void run() throws Exception {
368 mUploadProvider.read(OutputStreamDataSink.this, mBuf fer); 375 mUploadProvider.read(OutputStreamDataSink.this, mBuf fer);
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 public boolean isDone() { 756 public boolean isDone() {
750 State state = mState.get(); 757 State state = mState.get();
751 return state == State.COMPLETE | state == State.ERROR | state == State.C ANCELLED; 758 return state == State.COMPLETE | state == State.ERROR | state == State.C ANCELLED;
752 } 759 }
753 760
754 @Override 761 @Override
755 public void getStatus(StatusListener listener) { 762 public void getStatus(StatusListener listener) {
756 State state = mState.get(); 763 State state = mState.get();
757 int extraStatus = this.mAdditionalStatusDetails; 764 int extraStatus = this.mAdditionalStatusDetails;
758 765
759 @Status.StatusValues final int status; 766 @Status.StatusValues
767 final int status;
760 switch (state) { 768 switch (state) {
761 case ERROR: 769 case ERROR:
762 case COMPLETE: 770 case COMPLETE:
763 case CANCELLED: 771 case CANCELLED:
764 case NOT_STARTED: 772 case NOT_STARTED:
765 status = Status.INVALID; 773 status = Status.INVALID;
766 break; 774 break;
767 case STARTED: 775 case STARTED:
768 status = extraStatus; 776 status = extraStatus;
769 break; 777 break;
770 case REDIRECT_RECEIVED: 778 case REDIRECT_RECEIVED:
771 case AWAITING_FOLLOW_REDIRECT: 779 case AWAITING_FOLLOW_REDIRECT:
772 case AWAITING_READ: 780 case AWAITING_READ:
773 status = Status.IDLE; 781 status = Status.IDLE;
774 break; 782 break;
775 case READING: 783 case READING:
776 status = Status.READING_RESPONSE; 784 status = Status.READING_RESPONSE;
777 break; 785 break;
778 default: 786 default:
779 throw new IllegalStateException("Switch is exhaustive: " + state ); 787 throw new IllegalStateException("Switch is exhaustive: " + state );
780 } 788 }
781 789
782 mCallbackAsync.sendStatus(listener, status); 790 mCallbackAsync.sendStatus(listener, status);
783 } 791 }
784 792
785 /** This wrapper ensures that callbacks are always called on the correct exe cutor */ 793 /** This wrapper ensures that callbacks are always called on the correct exe cutor */
786 private final class AsyncUrlRequestCallback { 794 private final class AsyncUrlRequestCallback {
787 final UrlRequest.Callback mCallback; 795 final Callback mCallback;
788 final Executor mUserExecutor; 796 final Executor mUserExecutor;
789 final Executor mFallbackExecutor; 797 final Executor mFallbackExecutor;
790 798
791 AsyncUrlRequestCallback(Callback callback, final Executor userExecutor) { 799 AsyncUrlRequestCallback(Callback callback, final Executor userExecutor) {
792 this.mCallback = callback; 800 this.mCallback = callback;
793 if (mAllowDirectExecutor) { 801 if (mAllowDirectExecutor) {
794 this.mUserExecutor = userExecutor; 802 this.mUserExecutor = userExecutor;
795 this.mFallbackExecutor = null; 803 this.mFallbackExecutor = null;
796 } else { 804 } else {
797 mUserExecutor = new DirectPreventingExecutor(userExecutor); 805 mUserExecutor = new DirectPreventingExecutor(userExecutor);
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 // Can't throw directly from here, since the delegate execut or could catch this 965 // Can't throw directly from here, since the delegate execut or could catch this
958 // exception. 966 // exception.
959 mExecutedInline = new InlineExecutionProhibitedException(); 967 mExecutedInline = new InlineExecutionProhibitedException();
960 return; 968 return;
961 } 969 }
962 mCommand.run(); 970 mCommand.run();
963 } 971 }
964 } 972 }
965 } 973 }
966 } 974 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698