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

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

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

Powered by Google App Engine
This is Rietveld 408576698