Chromium Code Reviews| 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 struct WriteBuffers { | |
| 135 WriteBuffers(); | |
| 136 ~WriteBuffers(); | |
| 137 | |
| 138 // Clears Write Buffers list. | |
| 139 void Clear(); | |
| 140 | |
| 141 // Appends |buffer| of |buffer_size| length to the end of buffer list. | |
| 142 void AppendBuffer(const scoped_refptr<net::IOBuffer>& buffer, | |
| 143 int buffer_size); | |
| 144 | |
| 145 // Returns true of Write Buffers list is empty. | |
| 146 bool Empty() const; | |
| 147 | |
| 148 // Every IOBuffer in |write_buffer_list| points to the memory owned by the | |
| 149 // application. | |
| 150 std::vector<scoped_refptr<net::IOBuffer>> write_buffer_list; | |
|
xunjieli
2016/06/14 14:29:34
Since we have AppendBuffer, these member variables
mef
2016/06/14 19:54:30
Done.
| |
| 151 // A list of the length of each IOBuffer in |write_buffer_list|. | |
| 152 std::vector<int> write_buffer_len_list; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(WriteBuffers); | |
| 155 }; | |
| 156 | |
| 119 // net::BidirectionalStream::Delegate implementations: | 157 // net::BidirectionalStream::Delegate implementations: |
| 120 void OnStreamReady(bool request_headers_sent) override; | 158 void OnStreamReady(bool request_headers_sent) override; |
| 121 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; | 159 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; |
| 122 void OnDataRead(int bytes_read) override; | 160 void OnDataRead(int bytes_read) override; |
| 123 void OnDataSent() override; | 161 void OnDataSent() override; |
| 124 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; | 162 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; |
| 125 void OnFailed(int error) override; | 163 void OnFailed(int error) override; |
| 126 // Helper method to derive OnSucceeded. | 164 // Helper method to derive OnSucceeded. |
| 127 void MaybeOnSucceded(); | 165 void MaybeOnSucceded(); |
| 128 | 166 |
| 129 void StartOnNetworkThread( | 167 void StartOnNetworkThread( |
| 130 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); | 168 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info); |
| 131 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | 169 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, |
| 132 int buffer_size); | 170 int buffer_size); |
| 133 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | 171 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, |
| 134 int buffer_size, | 172 int buffer_size, |
| 135 bool end_of_stream); | 173 bool end_of_stream); |
| 174 void FlushOnNetworkThread(); | |
| 136 void CancelOnNetworkThread(); | 175 void CancelOnNetworkThread(); |
| 137 void DestroyOnNetworkThread(); | 176 void DestroyOnNetworkThread(); |
| 138 | 177 |
| 139 // Read state is tracking reading flow. Only accessed on network thread. | 178 // Read state is tracking reading flow. Only accessed on network thread. |
| 140 // / <--- READING <--- \ | 179 // / <--- READING <--- \ |
| 141 // | | | 180 // | | |
| 142 // \ / | 181 // \ / |
| 143 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS | 182 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS |
| 144 State read_state_; | 183 State read_state_; |
| 145 | 184 |
| 146 // Write state is tracking writing flow. Only accessed on network thread. | 185 // Write state is tracking writing flow. Only accessed on network thread. |
| 147 // / <--- WRITING <--- \ | 186 // / <--- WRITING <--- \ |
| 148 // | | | 187 // | | |
| 149 // \ / | 188 // \ / |
| 150 // NOT_STARTED -> STARTED --> WAITING_FOR_WRITE -> WRITING_DONE -> SUCCESS | 189 // NOT_STARTED -> STARTED --> WAITING_FOR_FLUSH -> WRITING_DONE -> SUCCESS |
| 151 State write_state_; | 190 State write_state_; |
| 152 | 191 |
| 153 bool write_end_of_stream_; | 192 bool write_end_of_stream_; |
| 193 bool request_headers_sent_; | |
| 194 | |
| 195 bool disable_auto_flush_; | |
| 196 bool delay_headers_until_flush_; | |
| 154 | 197 |
| 155 CronetEnvironment* const environment_; | 198 CronetEnvironment* const environment_; |
| 156 | 199 |
| 157 scoped_refptr<net::WrappedIOBuffer> read_buffer_; | 200 scoped_refptr<net::WrappedIOBuffer> read_buffer_; |
| 158 scoped_refptr<net::WrappedIOBuffer> write_buffer_; | 201 |
| 202 // Write data that is pending the flush. | |
| 203 std::unique_ptr<WriteBuffers> pending_write_data_; | |
| 204 // Write data that is sending. | |
| 205 std::unique_ptr<WriteBuffers> sending_write_data_; | |
| 206 | |
| 159 std::unique_ptr<net::BidirectionalStream> bidi_stream_; | 207 std::unique_ptr<net::BidirectionalStream> bidi_stream_; |
| 160 Delegate* delegate_; | 208 Delegate* delegate_; |
| 161 | 209 |
| 162 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); | 210 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); |
| 163 }; | 211 }; |
| 164 | 212 |
| 165 } // namespace cronet | 213 } // namespace cronet |
| 166 | 214 |
| 167 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | 215 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ |
| OLD | NEW |