| 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 if (maybeOnSucceeded) { | 217 if (maybeOnSucceeded) { |
| 218 maybeOnSucceededOnExecutor(); | 218 maybeOnSucceededOnExecutor(); |
| 219 } | 219 } |
| 220 } catch (Exception e) { | 220 } catch (Exception e) { |
| 221 onCallbackException(e); | 221 onCallbackException(e); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 | 225 |
| 226 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url
, | 226 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url
, |
| 227 @BidirectionalStream.Builder.StreamPriority int priority, Callback c
allback, | 227 @CronetEngineBase.StreamPriority int priority, Callback callback, Ex
ecutor executor, |
| 228 Executor executor, String httpMethod, List<Map.Entry<String, String>
> requestHeaders, | 228 String httpMethod, List<Map.Entry<String, String>> requestHeaders, |
| 229 boolean delayRequestHeadersUntilNextFlush, Collection<Object> reques
tAnnotations) { | 229 boolean delayRequestHeadersUntilNextFlush, Collection<Object> reques
tAnnotations) { |
| 230 mRequestContext = requestContext; | 230 mRequestContext = requestContext; |
| 231 mInitialUrl = url; | 231 mInitialUrl = url; |
| 232 mInitialPriority = convertStreamPriority(priority); | 232 mInitialPriority = convertStreamPriority(priority); |
| 233 mCallback = callback; | 233 mCallback = callback; |
| 234 mExecutor = executor; | 234 mExecutor = executor; |
| 235 mInitialMethod = httpMethod; | 235 mInitialMethod = httpMethod; |
| 236 mRequestHeaders = stringsFromHeaderList(requestHeaders); | 236 mRequestHeaders = stringsFromHeaderList(requestHeaders); |
| 237 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilNextFlush; | 237 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilNextFlush; |
| 238 mPendingData = new LinkedList<>(); | 238 mPendingData = new LinkedList<>(); |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 private static String[] stringsFromHeaderList(List<Map.Entry<String, String>
> headersList) { | 677 private static String[] stringsFromHeaderList(List<Map.Entry<String, String>
> headersList) { |
| 678 String headersArray[] = new String[headersList.size() * 2]; | 678 String headersArray[] = new String[headersList.size() * 2]; |
| 679 int i = 0; | 679 int i = 0; |
| 680 for (Map.Entry<String, String> requestHeader : headersList) { | 680 for (Map.Entry<String, String> requestHeader : headersList) { |
| 681 headersArray[i++] = requestHeader.getKey(); | 681 headersArray[i++] = requestHeader.getKey(); |
| 682 headersArray[i++] = requestHeader.getValue(); | 682 headersArray[i++] = requestHeader.getValue(); |
| 683 } | 683 } |
| 684 return headersArray; | 684 return headersArray; |
| 685 } | 685 } |
| 686 | 686 |
| 687 private static int convertStreamPriority( | 687 private static int convertStreamPriority(@CronetEngineBase.StreamPriority in
t priority) { |
| 688 @BidirectionalStream.Builder.StreamPriority int priority) { | |
| 689 switch (priority) { | 688 switch (priority) { |
| 690 case Builder.STREAM_PRIORITY_IDLE: | 689 case Builder.STREAM_PRIORITY_IDLE: |
| 691 return RequestPriority.IDLE; | 690 return RequestPriority.IDLE; |
| 692 case Builder.STREAM_PRIORITY_LOWEST: | 691 case Builder.STREAM_PRIORITY_LOWEST: |
| 693 return RequestPriority.LOWEST; | 692 return RequestPriority.LOWEST; |
| 694 case Builder.STREAM_PRIORITY_LOW: | 693 case Builder.STREAM_PRIORITY_LOW: |
| 695 return RequestPriority.LOW; | 694 return RequestPriority.LOW; |
| 696 case Builder.STREAM_PRIORITY_MEDIUM: | 695 case Builder.STREAM_PRIORITY_MEDIUM: |
| 697 return RequestPriority.MEDIUM; | 696 return RequestPriority.MEDIUM; |
| 698 case Builder.STREAM_PRIORITY_HIGHEST: | 697 case Builder.STREAM_PRIORITY_HIGHEST: |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 private native boolean nativeReadData( | 802 private native boolean nativeReadData( |
| 804 long nativePtr, ByteBuffer byteBuffer, int position, int limit); | 803 long nativePtr, ByteBuffer byteBuffer, int position, int limit); |
| 805 | 804 |
| 806 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 805 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 807 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers
, int[] positions, | 806 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers
, int[] positions, |
| 808 int[] limits, boolean endOfStream); | 807 int[] limits, boolean endOfStream); |
| 809 | 808 |
| 810 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 809 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 811 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); | 810 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); |
| 812 } | 811 } |
| OLD | NEW |