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 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |
6 #define COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 11 matching lines...) Expand all Loading... |
22 struct BidirectionalStreamRequestInfo; | 22 struct BidirectionalStreamRequestInfo; |
23 class SpdyHeaderBlock; | 23 class SpdyHeaderBlock; |
24 } // namespace net | 24 } // namespace net |
25 | 25 |
26 namespace cronet { | 26 namespace cronet { |
27 | 27 |
28 class CronetURLRequestContextAdapter; | 28 class CronetURLRequestContextAdapter; |
29 class IOBufferWithByteBuffer; | 29 class IOBufferWithByteBuffer; |
30 | 30 |
31 // An adapter from Java BidirectionalStream object to net::BidirectionalStream. | 31 // An adapter from Java BidirectionalStream object to net::BidirectionalStream. |
32 // Created and configured from a Java thread. Start, ReadData, WriteData and | 32 // Created and configured from a Java thread. Start, ReadData, WritevData and |
33 // Destroy can be called on any thread (including network thread), and post | 33 // Destroy can be called on any thread (including network thread), and post |
34 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to | 34 // calls to corresponding {Start|ReadData|WritevData|Destroy}OnNetworkThread to |
35 // the network thread. The object is always deleted on network thread. All | 35 // the network thread. The object is always deleted on network thread. All |
36 // callbacks into the Java BidirectionalStream are done on the network thread. | 36 // callbacks into the Java BidirectionalStream are done on the network thread. |
37 // Java BidirectionalStream is expected to initiate the next step like ReadData | 37 // Java BidirectionalStream is expected to initiate the next step like ReadData |
38 // or Destroy. Public methods can be called on any thread. | 38 // or Destroy. Public methods can be called on any thread. |
39 class CronetBidirectionalStreamAdapter | 39 class CronetBidirectionalStreamAdapter |
40 : public net::BidirectionalStream::Delegate { | 40 : public net::BidirectionalStream::Delegate { |
41 public: | 41 public: |
42 static bool RegisterJni(JNIEnv* env); | 42 static bool RegisterJni(JNIEnv* env); |
43 | 43 |
44 CronetBidirectionalStreamAdapter( | 44 CronetBidirectionalStreamAdapter( |
45 CronetURLRequestContextAdapter* context, | 45 CronetURLRequestContextAdapter* context, |
46 JNIEnv* env, | 46 JNIEnv* env, |
47 const base::android::JavaParamRef<jobject>& jbidi_stream); | 47 const base::android::JavaParamRef<jobject>& jbidi_stream, |
| 48 bool jdisable_auto_flush); |
48 ~CronetBidirectionalStreamAdapter() override; | 49 ~CronetBidirectionalStreamAdapter() override; |
49 | 50 |
50 // Validates method and headers, initializes and starts the request. If | 51 // Validates method and headers, initializes and starts the request. If |
51 // |jend_of_stream| is true, then stream is half-closed after sending header | 52 // |jend_of_stream| is true, then stream is half-closed after sending header |
52 // frame and no data is expected to be written. | 53 // frame and no data is expected to be written. |
53 // Returns 0 if request is valid and started successfully, | 54 // Returns 0 if request is valid and started successfully, |
54 // Returns -1 if |jmethod| is not valid HTTP method name. | 55 // Returns -1 if |jmethod| is not valid HTTP method name. |
55 // Returns position of invalid header value in |jheaders| if header name is | 56 // Returns position of invalid header value in |jheaders| if header name is |
56 // not valid. | 57 // not valid. |
57 jint Start(JNIEnv* env, | 58 jint Start(JNIEnv* env, |
58 const base::android::JavaParamRef<jobject>& jcaller, | 59 const base::android::JavaParamRef<jobject>& jcaller, |
59 const base::android::JavaParamRef<jstring>& jurl, | 60 const base::android::JavaParamRef<jstring>& jurl, |
60 jint jpriority, | 61 jint jpriority, |
61 const base::android::JavaParamRef<jstring>& jmethod, | 62 const base::android::JavaParamRef<jstring>& jmethod, |
62 const base::android::JavaParamRef<jobjectArray>& jheaders, | 63 const base::android::JavaParamRef<jobjectArray>& jheaders, |
63 jboolean jend_of_stream); | 64 jboolean jend_of_stream); |
64 | 65 |
65 // Reads more data into |jbyte_buffer| starting at |jposition| and not | 66 // Reads more data into |jbyte_buffer| starting at |jposition| and not |
66 // exceeding |jlimit|. Arguments are preserved to ensure that |jbyte_buffer| | 67 // exceeding |jlimit|. Arguments are preserved to ensure that |jbyte_buffer| |
67 // is not modified by the application during read. | 68 // is not modified by the application during read. |
68 jboolean ReadData(JNIEnv* env, | 69 jboolean ReadData(JNIEnv* env, |
69 const base::android::JavaParamRef<jobject>& jcaller, | 70 const base::android::JavaParamRef<jobject>& jcaller, |
70 const base::android::JavaParamRef<jobject>& jbyte_buffer, | 71 const base::android::JavaParamRef<jobject>& jbyte_buffer, |
71 jint jposition, | 72 jint jposition, |
72 jint jlimit); | 73 jint jlimit); |
73 | 74 |
74 // Writes more data from |jbyte_buffer| starting at |jposition| and ending at | 75 // Writes more data from |jbyte_buffers|. For the i_th buffer in |
75 // |jlimit|-1. Arguments are preserved to ensure that |jbyte_buffer| | 76 // |jbyte_buffers|, bytes to write start from i_th position in |jpositions| |
| 77 // and end at i_th limit in |jlimits|. |
| 78 // Arguments are preserved to ensure that |jbyte_buffer| |
76 // is not modified by the application during write. The |jend_of_stream| is | 79 // is not modified by the application during write. The |jend_of_stream| is |
77 // passed to remote to indicate end of stream. | 80 // passed to remote to indicate end of stream. |
78 jboolean WriteData(JNIEnv* env, | 81 jboolean WritevData( |
79 const base::android::JavaParamRef<jobject>& jcaller, | 82 JNIEnv* env, |
80 const base::android::JavaParamRef<jobject>& jbyte_buffer, | 83 const base::android::JavaParamRef<jobject>& jcaller, |
81 jint jposition, | 84 const base::android::JavaParamRef<jobjectArray>& jbyte_buffers, |
82 jint jlimit, | 85 const base::android::JavaParamRef<jintArray>& jpositions, |
83 jboolean jend_of_stream); | 86 const base::android::JavaParamRef<jintArray>& jlimits, |
| 87 jboolean jend_of_stream); |
84 | 88 |
85 // Releases all resources for the request and deletes the object itself. | 89 // Releases all resources for the request and deletes the object itself. |
86 // |jsend_on_canceled| indicates if Java onCanceled callback should be | 90 // |jsend_on_canceled| indicates if Java onCanceled callback should be |
87 // issued to indicate that no more callbacks will be issued. | 91 // issued to indicate that no more callbacks will be issued. |
88 void Destroy(JNIEnv* env, | 92 void Destroy(JNIEnv* env, |
89 const base::android::JavaParamRef<jobject>& jcaller, | 93 const base::android::JavaParamRef<jobject>& jcaller, |
90 jboolean jsend_on_canceled); | 94 jboolean jsend_on_canceled); |
91 | 95 |
92 private: | 96 private: |
| 97 typedef std::vector<scoped_refptr<IOBufferWithByteBuffer>> IOByteBufferList; |
93 // net::BidirectionalStream::Delegate implementations: | 98 // net::BidirectionalStream::Delegate implementations: |
94 void OnHeadersSent() override; | 99 void OnStreamReady() override; |
95 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; | 100 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; |
96 void OnDataRead(int bytes_read) override; | 101 void OnDataRead(int bytes_read) override; |
97 void OnDataSent() override; | 102 void OnDataSent() override; |
98 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; | 103 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; |
99 void OnFailed(int error) override; | 104 void OnFailed(int error) override; |
100 | 105 |
101 void StartOnNetworkThread( | 106 void StartOnNetworkThread( |
102 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); | 107 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); |
103 void ReadDataOnNetworkThread( | 108 void ReadDataOnNetworkThread( |
104 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | 109 scoped_refptr<IOBufferWithByteBuffer> read_buffer, |
105 int buffer_size); | 110 int buffer_size); |
106 void WriteDataOnNetworkThread( | 111 void WritevDataOnNetworkThread(const IOByteBufferList& buffers, |
107 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | 112 bool end_of_stream); |
108 int buffer_size, | |
109 bool end_of_stream); | |
110 void DestroyOnNetworkThread(bool send_on_canceled); | 113 void DestroyOnNetworkThread(bool send_on_canceled); |
111 // Gets headers as a Java array. | 114 // Gets headers as a Java array. |
112 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( | 115 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( |
113 JNIEnv* env, | 116 JNIEnv* env, |
114 const net::SpdyHeaderBlock& header_block); | 117 const net::SpdyHeaderBlock& header_block); |
115 | 118 |
116 CronetURLRequestContextAdapter* const context_; | 119 CronetURLRequestContextAdapter* const context_; |
117 | 120 |
118 // Java object that owns this CronetBidirectionalStreamAdapter. | 121 // Java object that owns this CronetBidirectionalStreamAdapter. |
119 base::android::ScopedJavaGlobalRef<jobject> owner_; | 122 base::android::ScopedJavaGlobalRef<jobject> owner_; |
| 123 const bool disable_auto_flush_; |
| 124 // Whether an end of stream flag is passed in through a write call. |
| 125 // Not applicable to HTTP methods that do not send data. |
| 126 bool write_end_of_stream_; |
120 | 127 |
121 scoped_refptr<IOBufferWithByteBuffer> read_buffer_; | 128 scoped_refptr<IOBufferWithByteBuffer> read_buffer_; |
122 scoped_refptr<IOBufferWithByteBuffer> write_buffer_; | 129 IOByteBufferList write_buffer_list_; |
123 std::unique_ptr<net::BidirectionalStream> bidi_stream_; | 130 std::unique_ptr<net::BidirectionalStream> bidi_stream_; |
124 | 131 |
125 // Whether BidirectionalStream::Delegate::OnFailed callback is invoked. | 132 // Whether BidirectionalStream::Delegate::OnFailed callback is invoked. |
126 bool stream_failed_; | 133 bool stream_failed_; |
127 | 134 |
128 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter); | 135 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter); |
129 }; | 136 }; |
130 | 137 |
131 } // namespace cronet | 138 } // namespace cronet |
132 | 139 |
133 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ | 140 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |
OLD | NEW |