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