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