| 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 10 matching lines...) Expand all Loading... |
| 21 namespace net { | 21 namespace net { |
| 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 // Convenient wrapper to hold Java references and data to represent the pending |
| 32 // data to be written. |
| 33 struct PendingWriteData { |
| 34 PendingWriteData(JNIEnv* env, |
| 35 jobjectArray jwrite_buffer_list, |
| 36 jintArray jwrite_buffer_pos_list, |
| 37 jintArray jwrite_buffer_limit_list, |
| 38 jboolean jwrite_end_of_stream); |
| 39 ~PendingWriteData(); |
| 40 |
| 41 // Arguments passed in from Java. Retain a global ref so they won't get GC-ed |
| 42 // until the corresponding onWriteCompleted is invoked. |
| 43 base::android::ScopedJavaGlobalRef<jobjectArray> jwrite_buffer_list; |
| 44 base::android::ScopedJavaGlobalRef<jintArray> jwrite_buffer_pos_list; |
| 45 base::android::ScopedJavaGlobalRef<jintArray> jwrite_buffer_limit_list; |
| 46 // A copy of the end of stream flag passed in from Java. |
| 47 jboolean jwrite_end_of_stream; |
| 48 // Every IOBuffer in |write_buffer_list| points to the memory owned by the |
| 49 // corresponding Java ByteBuffer in |jwrite_buffer_list|. |
| 50 std::vector<scoped_refptr<net::IOBuffer>> write_buffer_list; |
| 51 // A list of the length of each IOBuffer in |write_buffer_list|. |
| 52 std::vector<int> write_buffer_len_list; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(PendingWriteData); |
| 55 }; |
| 56 |
| 31 // An adapter from Java BidirectionalStream object to net::BidirectionalStream. | 57 // An adapter from Java BidirectionalStream object to net::BidirectionalStream. |
| 32 // Created and configured from a Java thread. Start, ReadData, WritevData and | 58 // Created and configured from a Java thread. Start, ReadData, WritevData and |
| 33 // Destroy can be called on any thread (including network thread), and post | 59 // Destroy can be called on any thread (including network thread), and post |
| 34 // calls to corresponding {Start|ReadData|WritevData|Destroy}OnNetworkThread to | 60 // calls to corresponding {Start|ReadData|WritevData|Destroy}OnNetworkThread to |
| 35 // the network thread. The object is always deleted on network thread. All | 61 // the network thread. The object is always deleted on network thread. All |
| 36 // callbacks into the Java BidirectionalStream are done on the network thread. | 62 // callbacks into the Java BidirectionalStream are done on the network thread. |
| 37 // Java BidirectionalStream is expected to initiate the next step like ReadData | 63 // Java BidirectionalStream is expected to initiate the next step like ReadData |
| 38 // or Destroy. Public methods can be called on any thread. | 64 // or Destroy. Public methods can be called on any thread. |
| 39 class CronetBidirectionalStreamAdapter | 65 class CronetBidirectionalStreamAdapter |
| 40 : public net::BidirectionalStream::Delegate { | 66 : public net::BidirectionalStream::Delegate { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 jboolean jend_of_stream); | 113 jboolean jend_of_stream); |
| 88 | 114 |
| 89 // Releases all resources for the request and deletes the object itself. | 115 // Releases all resources for the request and deletes the object itself. |
| 90 // |jsend_on_canceled| indicates if Java onCanceled callback should be | 116 // |jsend_on_canceled| indicates if Java onCanceled callback should be |
| 91 // issued to indicate that no more callbacks will be issued. | 117 // issued to indicate that no more callbacks will be issued. |
| 92 void Destroy(JNIEnv* env, | 118 void Destroy(JNIEnv* env, |
| 93 const base::android::JavaParamRef<jobject>& jcaller, | 119 const base::android::JavaParamRef<jobject>& jcaller, |
| 94 jboolean jsend_on_canceled); | 120 jboolean jsend_on_canceled); |
| 95 | 121 |
| 96 private: | 122 private: |
| 97 typedef std::vector<scoped_refptr<IOBufferWithByteBuffer>> | |
| 98 IOBufferWithByteBufferList; | |
| 99 // net::BidirectionalStream::Delegate implementations: | 123 // net::BidirectionalStream::Delegate implementations: |
| 100 void OnStreamReady() override; | 124 void OnStreamReady() override; |
| 101 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; | 125 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; |
| 102 void OnDataRead(int bytes_read) override; | 126 void OnDataRead(int bytes_read) override; |
| 103 void OnDataSent() override; | 127 void OnDataSent() override; |
| 104 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; | 128 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; |
| 105 void OnFailed(int error) override; | 129 void OnFailed(int error) override; |
| 106 | 130 |
| 107 void StartOnNetworkThread( | 131 void StartOnNetworkThread( |
| 108 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); | 132 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); |
| 109 void ReadDataOnNetworkThread( | 133 void ReadDataOnNetworkThread( |
| 110 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | 134 scoped_refptr<IOBufferWithByteBuffer> read_buffer, |
| 111 int buffer_size); | 135 int buffer_size); |
| 112 void WritevDataOnNetworkThread(const IOBufferWithByteBufferList& buffers, | 136 void WritevDataOnNetworkThread( |
| 113 bool end_of_stream); | 137 std::unique_ptr<PendingWriteData> pending_write_data); |
| 114 void DestroyOnNetworkThread(bool send_on_canceled); | 138 void DestroyOnNetworkThread(bool send_on_canceled); |
| 115 // Gets headers as a Java array. | 139 // Gets headers as a Java array. |
| 116 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( | 140 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( |
| 117 JNIEnv* env, | 141 JNIEnv* env, |
| 118 const net::SpdyHeaderBlock& header_block); | 142 const net::SpdyHeaderBlock& header_block); |
| 119 | 143 |
| 120 CronetURLRequestContextAdapter* const context_; | 144 CronetURLRequestContextAdapter* const context_; |
| 121 | 145 |
| 122 // Java object that owns this CronetBidirectionalStreamAdapter. | 146 // Java object that owns this CronetBidirectionalStreamAdapter. |
| 123 base::android::ScopedJavaGlobalRef<jobject> owner_; | 147 base::android::ScopedJavaGlobalRef<jobject> owner_; |
| 124 const bool disable_auto_flush_; | 148 const bool disable_auto_flush_; |
| 125 // Whether an end of stream flag is passed in through a write call. | |
| 126 // Not applicable to HTTP methods that do not send data. | |
| 127 bool write_end_of_stream_; | |
| 128 | 149 |
| 129 scoped_refptr<IOBufferWithByteBuffer> read_buffer_; | 150 scoped_refptr<IOBufferWithByteBuffer> read_buffer_; |
| 130 IOBufferWithByteBufferList write_buffer_list_; | 151 std::unique_ptr<PendingWriteData> pending_write_data_; |
| 131 std::unique_ptr<net::BidirectionalStream> bidi_stream_; | 152 std::unique_ptr<net::BidirectionalStream> bidi_stream_; |
| 132 | 153 |
| 133 // Whether BidirectionalStream::Delegate::OnFailed callback is invoked. | 154 // Whether BidirectionalStream::Delegate::OnFailed callback is invoked. |
| 134 bool stream_failed_; | 155 bool stream_failed_; |
| 135 | 156 |
| 136 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter); | 157 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter); |
| 137 }; | 158 }; |
| 138 | 159 |
| 139 } // namespace cronet | 160 } // namespace cronet |
| 140 | 161 |
| 141 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ | 162 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |
| OLD | NEW |