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