| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | 5 #ifndef COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ |
| 6 #define COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | 6 #define COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 13 #include "net/http/bidirectional_stream.h" | 14 #include "net/http/bidirectional_stream.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 class HttpRequestHeaders; | 17 class HttpRequestHeaders; |
| 17 class WrappedIOBuffer; | 18 class WrappedIOBuffer; |
| 18 } // namespace net | 19 } // namespace net |
| (...skipping 28 matching lines...) Expand all Loading... |
| 47 virtual void OnSucceeded() = 0; | 48 virtual void OnSucceeded() = 0; |
| 48 | 49 |
| 49 virtual void OnFailed(int error) = 0; | 50 virtual void OnFailed(int error) = 0; |
| 50 | 51 |
| 51 virtual void OnCanceled() = 0; | 52 virtual void OnCanceled() = 0; |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 CronetBidirectionalStream(CronetEnvironment* environment, Delegate* delegate); | 55 CronetBidirectionalStream(CronetEnvironment* environment, Delegate* delegate); |
| 55 ~CronetBidirectionalStream() override; | 56 ~CronetBidirectionalStream() override; |
| 56 | 57 |
| 58 // Disables automatic flushing of each buffer passed to WriteData(). |
| 59 void disable_auto_flush(bool disable_auto_flush) { |
| 60 disable_auto_flush_ = disable_auto_flush; |
| 61 } |
| 62 |
| 63 // Delays sending request headers until first call to Flush(). |
| 64 void delay_headers_until_flush(bool delay_headers_until_flush) { |
| 65 delay_headers_until_flush_ = delay_headers_until_flush; |
| 66 } |
| 67 |
| 57 // Validates method and headers, initializes and starts the request. If | 68 // Validates method and headers, initializes and starts the request. If |
| 58 // |end_of_stream| is true, then stream is half-closed after sending header | 69 // |end_of_stream| is true, then stream is half-closed after sending header |
| 59 // frame and no data is expected to be written. | 70 // frame and no data is expected to be written. |
| 60 // Returns 0 if request is valid and started successfully, | 71 // Returns 0 if request is valid and started successfully, |
| 61 // Returns -1 if |method| is not valid HTTP method name. | 72 // Returns -1 if |method| is not valid HTTP method name. |
| 62 // Returns position of invalid header value in |headers| if header name is | 73 // Returns position of invalid header value in |headers| if header name is |
| 63 // not valid. | 74 // not valid. |
| 64 int Start(const char* url, | 75 int Start(const char* url, |
| 65 int priority, | 76 int priority, |
| 66 const char* method, | 77 const char* method, |
| 67 const net::HttpRequestHeaders& headers, | 78 const net::HttpRequestHeaders& headers, |
| 68 bool end_of_stream); | 79 bool end_of_stream); |
| 69 | 80 |
| 70 // Reads more data into |buffer| up to |capacity| bytes. | 81 // Reads more data into |buffer| up to |capacity| bytes. |
| 71 bool ReadData(char* buffer, int capacity); | 82 bool ReadData(char* buffer, int capacity); |
| 72 | 83 |
| 73 // Writes |count| bytes of data from |buffer|. The |end_of_stream| is | 84 // Writes |count| bytes of data from |buffer|. The |end_of_stream| is |
| 74 // passed to remote to indicate end of stream. | 85 // passed to remote to indicate end of stream. |
| 75 bool WriteData(const char* buffer, int count, bool end_of_stream); | 86 bool WriteData(const char* buffer, int count, bool end_of_stream); |
| 76 | 87 |
| 88 // Sends buffers passed to WriteData(). |
| 89 void Flush(); |
| 90 |
| 77 // Cancels the request. The OnCanceled callback is invoked when request is | 91 // Cancels the request. The OnCanceled callback is invoked when request is |
| 78 // caneceled, and not other callbacks are invoked afterwards.. | 92 // caneceled, and not other callbacks are invoked afterwards.. |
| 79 void Cancel(); | 93 void Cancel(); |
| 80 | 94 |
| 81 // Releases all resources for the request and deletes the object itself. | 95 // Releases all resources for the request and deletes the object itself. |
| 82 void Destroy(); | 96 void Destroy(); |
| 83 | 97 |
| 84 private: | 98 private: |
| 85 // States of BidirectionalStream are tracked in |read_state_| and | 99 // States of BidirectionalStream are tracked in |read_state_| and |
| 86 // |write_state_|. | 100 // |write_state_|. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 100 READING, | 114 READING, |
| 101 // There is no more data to read and stream is half-closed by the remote | 115 // There is no more data to read and stream is half-closed by the remote |
| 102 // side. | 116 // side. |
| 103 READING_DONE, | 117 READING_DONE, |
| 104 // Stream is canceled. | 118 // Stream is canceled. |
| 105 CANCELED, | 119 CANCELED, |
| 106 // Error has occured, stream is closed. | 120 // Error has occured, stream is closed. |
| 107 ERROR, | 121 ERROR, |
| 108 // Reading and writing are done, and the stream is closed successfully. | 122 // Reading and writing are done, and the stream is closed successfully. |
| 109 SUCCESS, | 123 SUCCESS, |
| 110 // Waiting for WriteData() to be called. | 124 // Waiting for Flush() to be called. |
| 111 WAITING_FOR_WRITE, | 125 WAITING_FOR_FLUSH, |
| 112 // Writing to the remote, callback will be invoked when done. | 126 // Writing to the remote, callback will be invoked when done. |
| 113 WRITING, | 127 WRITING, |
| 114 // There is no more data to write and stream is half-closed by the local | 128 // There is no more data to write and stream is half-closed by the local |
| 115 // side. | 129 // side. |
| 116 WRITING_DONE, | 130 WRITING_DONE, |
| 117 }; | 131 }; |
| 118 | 132 |
| 133 // Container to hold buffers and sizes of the pending data to be written. |
| 134 class WriteBuffers { |
| 135 public: |
| 136 WriteBuffers(); |
| 137 ~WriteBuffers(); |
| 138 |
| 139 // Clears Write Buffers list. |
| 140 void Clear(); |
| 141 |
| 142 // Appends |buffer| of |buffer_size| length to the end of buffer list. |
| 143 void AppendBuffer(const scoped_refptr<net::IOBuffer>& buffer, |
| 144 int buffer_size); |
| 145 |
| 146 void MoveTo(WriteBuffers* target); |
| 147 |
| 148 // Returns true of Write Buffers list is empty. |
| 149 bool Empty() const; |
| 150 |
| 151 const std::vector<scoped_refptr<net::IOBuffer>>& buffers() const { |
| 152 return write_buffer_list; |
| 153 } |
| 154 |
| 155 const std::vector<int>& lengths() const { return write_buffer_len_list; } |
| 156 |
| 157 private: |
| 158 // Every IOBuffer in |write_buffer_list| points to the memory owned by the |
| 159 // application. |
| 160 std::vector<scoped_refptr<net::IOBuffer>> write_buffer_list; |
| 161 // A list of the length of each IOBuffer in |write_buffer_list|. |
| 162 std::vector<int> write_buffer_len_list; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(WriteBuffers); |
| 165 }; |
| 166 |
| 119 // net::BidirectionalStream::Delegate implementations: | 167 // net::BidirectionalStream::Delegate implementations: |
| 120 void OnStreamReady(bool request_headers_sent) override; | 168 void OnStreamReady(bool request_headers_sent) override; |
| 121 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; | 169 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; |
| 122 void OnDataRead(int bytes_read) override; | 170 void OnDataRead(int bytes_read) override; |
| 123 void OnDataSent() override; | 171 void OnDataSent() override; |
| 124 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; | 172 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; |
| 125 void OnFailed(int error) override; | 173 void OnFailed(int error) override; |
| 126 // Helper method to derive OnSucceeded. | 174 // Helper method to derive OnSucceeded. |
| 127 void MaybeOnSucceded(); | 175 void MaybeOnSucceded(); |
| 128 | 176 |
| 129 void StartOnNetworkThread( | 177 void StartOnNetworkThread( |
| 130 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); | 178 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); |
| 131 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | 179 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, |
| 132 int buffer_size); | 180 int buffer_size); |
| 133 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | 181 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, |
| 134 int buffer_size, | 182 int buffer_size, |
| 135 bool end_of_stream); | 183 bool end_of_stream); |
| 184 void FlushOnNetworkThread(); |
| 185 void SendFlushingWriteData(); |
| 136 void CancelOnNetworkThread(); | 186 void CancelOnNetworkThread(); |
| 137 void DestroyOnNetworkThread(); | 187 void DestroyOnNetworkThread(); |
| 138 | 188 |
| 139 // Read state is tracking reading flow. Only accessed on network thread. | 189 // Read state is tracking reading flow. Only accessed on network thread. |
| 140 // / <--- READING <--- \ | 190 // / <--- READING <--- \ |
| 141 // | | | 191 // | | |
| 142 // \ / | 192 // \ / |
| 143 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS | 193 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS |
| 144 State read_state_; | 194 State read_state_; |
| 145 | 195 |
| 146 // Write state is tracking writing flow. Only accessed on network thread. | 196 // Write state is tracking writing flow. Only accessed on network thread. |
| 147 // / <--- WRITING <--- \ | 197 // / <--- WRITING <--- \ |
| 148 // | | | 198 // | | |
| 149 // \ / | 199 // \ / |
| 150 // NOT_STARTED -> STARTED --> WAITING_FOR_WRITE -> WRITING_DONE -> SUCCESS | 200 // NOT_STARTED -> STARTED --> WAITING_FOR_FLUSH -> WRITING_DONE -> SUCCESS |
| 151 State write_state_; | 201 State write_state_; |
| 152 | 202 |
| 153 bool write_end_of_stream_; | 203 bool write_end_of_stream_; |
| 204 bool request_headers_sent_; |
| 205 |
| 206 bool disable_auto_flush_; |
| 207 bool delay_headers_until_flush_; |
| 154 | 208 |
| 155 CronetEnvironment* const environment_; | 209 CronetEnvironment* const environment_; |
| 156 | 210 |
| 157 scoped_refptr<net::WrappedIOBuffer> read_buffer_; | 211 scoped_refptr<net::WrappedIOBuffer> read_buffer_; |
| 158 scoped_refptr<net::WrappedIOBuffer> write_buffer_; | 212 |
| 213 // Write data that is pending the flush. |
| 214 std::unique_ptr<WriteBuffers> pending_write_data_; |
| 215 // Write data that is flushed, but not sending yet. |
| 216 std::unique_ptr<WriteBuffers> flushing_write_data_; |
| 217 // Write data that is sending. |
| 218 std::unique_ptr<WriteBuffers> sending_write_data_; |
| 219 |
| 159 std::unique_ptr<net::BidirectionalStream> bidi_stream_; | 220 std::unique_ptr<net::BidirectionalStream> bidi_stream_; |
| 160 Delegate* delegate_; | 221 Delegate* delegate_; |
| 161 | 222 |
| 162 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); | 223 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); |
| 163 }; | 224 }; |
| 164 | 225 |
| 165 } // namespace cronet | 226 } // namespace cronet |
| 166 | 227 |
| 167 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | 228 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ |
| OLD | NEW |