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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 if (maybeOnSucceeded) { | 215 if (maybeOnSucceeded) { |
216 maybeOnSucceededOnExecutor(); | 216 maybeOnSucceededOnExecutor(); |
217 } | 217 } |
218 } catch (Exception e) { | 218 } catch (Exception e) { |
219 onCallbackException(e); | 219 onCallbackException(e); |
220 } | 220 } |
221 } | 221 } |
222 } | 222 } |
223 | 223 |
224 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url
, | 224 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url
, |
225 @BidirectionalStream.Builder.StreamPriority int priority, Callback c
allback, | 225 @BidirectionalStreamBuilderImpl.StreamPriority int priority, Callbac
k callback, |
226 Executor executor, String httpMethod, List<Map.Entry<String, String>
> requestHeaders, | 226 Executor executor, String httpMethod, List<Map.Entry<String, String>
> requestHeaders, |
227 boolean delayRequestHeadersUntilNextFlush, Collection<Object> reques
tAnnotations) { | 227 boolean delayRequestHeadersUntilNextFlush, Collection<Object> reques
tAnnotations) { |
228 mRequestContext = requestContext; | 228 mRequestContext = requestContext; |
229 mInitialUrl = url; | 229 mInitialUrl = url; |
230 mInitialPriority = convertStreamPriority(priority); | 230 mInitialPriority = convertStreamPriority(priority); |
231 mCallback = callback; | 231 mCallback = callback; |
232 mExecutor = executor; | 232 mExecutor = executor; |
233 mInitialMethod = httpMethod; | 233 mInitialMethod = httpMethod; |
234 mRequestHeaders = stringsFromHeaderList(requestHeaders); | 234 mRequestHeaders = stringsFromHeaderList(requestHeaders); |
235 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilNextFlush; | 235 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilNextFlush; |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 String headersArray[] = new String[headersList.size() * 2]; | 640 String headersArray[] = new String[headersList.size() * 2]; |
641 int i = 0; | 641 int i = 0; |
642 for (Map.Entry<String, String> requestHeader : headersList) { | 642 for (Map.Entry<String, String> requestHeader : headersList) { |
643 headersArray[i++] = requestHeader.getKey(); | 643 headersArray[i++] = requestHeader.getKey(); |
644 headersArray[i++] = requestHeader.getValue(); | 644 headersArray[i++] = requestHeader.getValue(); |
645 } | 645 } |
646 return headersArray; | 646 return headersArray; |
647 } | 647 } |
648 | 648 |
649 private static int convertStreamPriority( | 649 private static int convertStreamPriority( |
650 @BidirectionalStream.Builder.StreamPriority int priority) { | 650 @BidirectionalStreamBuilderImpl.StreamPriority int priority) { |
651 switch (priority) { | 651 switch (priority) { |
652 case Builder.STREAM_PRIORITY_IDLE: | 652 case Builder.STREAM_PRIORITY_IDLE: |
653 return RequestPriority.IDLE; | 653 return RequestPriority.IDLE; |
654 case Builder.STREAM_PRIORITY_LOWEST: | 654 case Builder.STREAM_PRIORITY_LOWEST: |
655 return RequestPriority.LOWEST; | 655 return RequestPriority.LOWEST; |
656 case Builder.STREAM_PRIORITY_LOW: | 656 case Builder.STREAM_PRIORITY_LOW: |
657 return RequestPriority.LOW; | 657 return RequestPriority.LOW; |
658 case Builder.STREAM_PRIORITY_MEDIUM: | 658 case Builder.STREAM_PRIORITY_MEDIUM: |
659 return RequestPriority.MEDIUM; | 659 return RequestPriority.MEDIUM; |
660 case Builder.STREAM_PRIORITY_HIGHEST: | 660 case Builder.STREAM_PRIORITY_HIGHEST: |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 private native boolean nativeReadData( | 771 private native boolean nativeReadData( |
772 long nativePtr, ByteBuffer byteBuffer, int position, int limit); | 772 long nativePtr, ByteBuffer byteBuffer, int position, int limit); |
773 | 773 |
774 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 774 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
775 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers
, int[] positions, | 775 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers
, int[] positions, |
776 int[] limits, boolean endOfStream); | 776 int[] limits, boolean endOfStream); |
777 | 777 |
778 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 778 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
779 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); | 779 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); |
780 } | 780 } |
OLD | NEW |