| OLD | NEW |
| 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.impl; | 5 package org.chromium.net.impl; |
| 6 | 6 |
| 7 import org.chromium.base.Log; | 7 import org.chromium.base.Log; |
| 8 import org.chromium.base.VisibleForTesting; | 8 import org.chromium.base.VisibleForTesting; |
| 9 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
| 10 import org.chromium.base.annotations.JNINamespace; | 10 import org.chromium.base.annotations.JNINamespace; |
| 11 import org.chromium.base.annotations.NativeClassQualifiedName; | 11 import org.chromium.base.annotations.NativeClassQualifiedName; |
| 12 import org.chromium.net.BidirectionalStream; | 12 import org.chromium.net.BidirectionalStream; |
| 13 import org.chromium.net.CronetException; | 13 import org.chromium.net.CronetException; |
| 14 import org.chromium.net.Preconditions; | 14 import org.chromium.net.ExperimentalBidirectionalStream; |
| 15 import org.chromium.net.QuicException; | 15 import org.chromium.net.QuicException; |
| 16 import org.chromium.net.RequestFinishedInfo; | 16 import org.chromium.net.RequestFinishedInfo; |
| 17 import org.chromium.net.RequestPriority; | 17 import org.chromium.net.RequestPriority; |
| 18 import org.chromium.net.UrlRequestException; | 18 import org.chromium.net.UrlRequestException; |
| 19 import org.chromium.net.UrlResponseInfo; | 19 import org.chromium.net.UrlResponseInfo; |
| 20 | 20 |
| 21 import java.nio.ByteBuffer; | 21 import java.nio.ByteBuffer; |
| 22 import java.util.AbstractMap; | 22 import java.util.AbstractMap; |
| 23 import java.util.ArrayList; | 23 import java.util.ArrayList; |
| 24 import java.util.Arrays; | 24 import java.util.Arrays; |
| 25 import java.util.Collection; | 25 import java.util.Collection; |
| 26 import java.util.LinkedList; | 26 import java.util.LinkedList; |
| 27 import java.util.List; | 27 import java.util.List; |
| 28 import java.util.Map; | 28 import java.util.Map; |
| 29 import java.util.concurrent.Executor; | 29 import java.util.concurrent.Executor; |
| 30 import java.util.concurrent.RejectedExecutionException; | 30 import java.util.concurrent.RejectedExecutionException; |
| 31 | 31 |
| 32 import javax.annotation.concurrent.GuardedBy; | 32 import javax.annotation.concurrent.GuardedBy; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * {@link BidirectionalStream} implementation using Chromium network stack. | 35 * {@link BidirectionalStream} implementation using Chromium network stack. |
| 36 * All @CalledByNative methods are called on the native network thread | 36 * All @CalledByNative methods are called on the native network thread |
| 37 * and post tasks with callback calls onto Executor. Upon returning from callbac
k, the native | 37 * and post tasks with callback calls onto Executor. Upon returning from callbac
k, the native |
| 38 * stream is called on Executor thread and posts native tasks to the native netw
ork thread. | 38 * stream is called on Executor thread and posts native tasks to the native netw
ork thread. |
| 39 */ | 39 */ |
| 40 @JNINamespace("cronet") | 40 @JNINamespace("cronet") |
| 41 @VisibleForTesting | 41 @VisibleForTesting |
| 42 public class CronetBidirectionalStream extends BidirectionalStream { | 42 public class CronetBidirectionalStream extends ExperimentalBidirectionalStream { |
| 43 /** | 43 /** |
| 44 * States of BidirectionalStream are tracked in mReadState and mWriteState. | 44 * States of BidirectionalStream are tracked in mReadState and mWriteState. |
| 45 * The write state is separated out as it changes independently of the read
state. | 45 * The write state is separated out as it changes independently of the read
state. |
| 46 * There is one initial state: State.NOT_STARTED. There is one normal final
state: | 46 * There is one initial state: State.NOT_STARTED. There is one normal final
state: |
| 47 * State.SUCCESS, reached after State.READING_DONE and State.WRITING_DONE. T
here are two | 47 * State.SUCCESS, reached after State.READING_DONE and State.WRITING_DONE. T
here are two |
| 48 * exceptional final states: State.CANCELED and State.ERROR, which can be re
ached from | 48 * exceptional final states: State.CANCELED and State.ERROR, which can be re
ached from |
| 49 * any other non-final state. | 49 * any other non-final state. |
| 50 */ | 50 */ |
| 51 private enum State { | 51 private enum State { |
| 52 /* Initial state, stream not started. */ | 52 /* Initial state, stream not started. */ |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 if (maybeOnSucceeded) { | 216 if (maybeOnSucceeded) { |
| 217 maybeOnSucceededOnExecutor(); | 217 maybeOnSucceededOnExecutor(); |
| 218 } | 218 } |
| 219 } catch (Exception e) { | 219 } catch (Exception e) { |
| 220 onCallbackException(e); | 220 onCallbackException(e); |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url
, | 225 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url
, |
| 226 @BidirectionalStream.Builder.StreamPriority int priority, Callback c
allback, | 226 @BidirectionalStreamBuilderImpl.StreamPriority int priority, Callbac
k callback, |
| 227 Executor executor, String httpMethod, List<Map.Entry<String, String>
> requestHeaders, | 227 Executor executor, String httpMethod, List<Map.Entry<String, String>
> requestHeaders, |
| 228 boolean delayRequestHeadersUntilNextFlush, Collection<Object> reques
tAnnotations) { | 228 boolean delayRequestHeadersUntilNextFlush, Collection<Object> reques
tAnnotations) { |
| 229 mRequestContext = requestContext; | 229 mRequestContext = requestContext; |
| 230 mInitialUrl = url; | 230 mInitialUrl = url; |
| 231 mInitialPriority = convertStreamPriority(priority); | 231 mInitialPriority = convertStreamPriority(priority); |
| 232 mCallback = callback; | 232 mCallback = callback; |
| 233 mExecutor = executor; | 233 mExecutor = executor; |
| 234 mInitialMethod = httpMethod; | 234 mInitialMethod = httpMethod; |
| 235 mRequestHeaders = stringsFromHeaderList(requestHeaders); | 235 mRequestHeaders = stringsFromHeaderList(requestHeaders); |
| 236 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilNextFlush; | 236 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilNextFlush; |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 String headersArray[] = new String[headersList.size() * 2]; | 668 String headersArray[] = new String[headersList.size() * 2]; |
| 669 int i = 0; | 669 int i = 0; |
| 670 for (Map.Entry<String, String> requestHeader : headersList) { | 670 for (Map.Entry<String, String> requestHeader : headersList) { |
| 671 headersArray[i++] = requestHeader.getKey(); | 671 headersArray[i++] = requestHeader.getKey(); |
| 672 headersArray[i++] = requestHeader.getValue(); | 672 headersArray[i++] = requestHeader.getValue(); |
| 673 } | 673 } |
| 674 return headersArray; | 674 return headersArray; |
| 675 } | 675 } |
| 676 | 676 |
| 677 private static int convertStreamPriority( | 677 private static int convertStreamPriority( |
| 678 @BidirectionalStream.Builder.StreamPriority int priority) { | 678 @BidirectionalStreamBuilderImpl.StreamPriority int priority) { |
| 679 switch (priority) { | 679 switch (priority) { |
| 680 case Builder.STREAM_PRIORITY_IDLE: | 680 case Builder.STREAM_PRIORITY_IDLE: |
| 681 return RequestPriority.IDLE; | 681 return RequestPriority.IDLE; |
| 682 case Builder.STREAM_PRIORITY_LOWEST: | 682 case Builder.STREAM_PRIORITY_LOWEST: |
| 683 return RequestPriority.LOWEST; | 683 return RequestPriority.LOWEST; |
| 684 case Builder.STREAM_PRIORITY_LOW: | 684 case Builder.STREAM_PRIORITY_LOW: |
| 685 return RequestPriority.LOW; | 685 return RequestPriority.LOW; |
| 686 case Builder.STREAM_PRIORITY_MEDIUM: | 686 case Builder.STREAM_PRIORITY_MEDIUM: |
| 687 return RequestPriority.MEDIUM; | 687 return RequestPriority.MEDIUM; |
| 688 case Builder.STREAM_PRIORITY_HIGHEST: | 688 case Builder.STREAM_PRIORITY_HIGHEST: |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 792 private native boolean nativeReadData( | 792 private native boolean nativeReadData( |
| 793 long nativePtr, ByteBuffer byteBuffer, int position, int limit); | 793 long nativePtr, ByteBuffer byteBuffer, int position, int limit); |
| 794 | 794 |
| 795 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 795 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 796 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers
, int[] positions, | 796 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers
, int[] positions, |
| 797 int[] limits, boolean endOfStream); | 797 int[] limits, boolean endOfStream); |
| 798 | 798 |
| 799 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 799 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 800 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); | 800 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); |
| 801 } | 801 } |
| OLD | NEW |