| 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_GRPC_SUPPORT_BIDIRECTIONAL_STREAM_H_ |
| 6 #define COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | 6 #define COMPONENTS_GRPC_SUPPORT_BIDIRECTIONAL_STREAM_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "net/http/bidirectional_stream.h" | 15 #include "net/http/bidirectional_stream.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 class HttpRequestHeaders; | 19 class HttpRequestHeaders; |
| 19 class WrappedIOBuffer; | 20 class WrappedIOBuffer; |
| 20 } // namespace net | 21 } // namespace net |
| 21 | 22 |
| 22 namespace cronet { | 23 namespace grpc_support { |
| 23 | |
| 24 class CronetEnvironment; | |
| 25 | 24 |
| 26 // An adapter to net::BidirectionalStream. | 25 // An adapter to net::BidirectionalStream. |
| 27 // Created and configured from any thread. Start, ReadData, WriteData and | 26 // Created and configured from any thread. Start, ReadData, WriteData and |
| 28 // Destroy can be called on any thread (including network thread), and post | 27 // Destroy can be called on any thread (including network thread), and post |
| 29 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to | 28 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to |
| 30 // the network thread. The object is always deleted on network thread. All | 29 // the network thread. The object is always deleted on network thread. All |
| 31 // callbacks into the Delegate are done on the network thread. | 30 // callbacks into the Delegate are done on the network thread. |
| 32 // The app is expected to initiate the next step like ReadData or Destroy. | 31 // The app is expected to initiate the next step like ReadData or Destroy. |
| 33 // Public methods can be called on any thread. | 32 // Public methods can be called on any thread. |
| 34 class CronetBidirectionalStream : public net::BidirectionalStream::Delegate { | 33 class BidirectionalStream : public net::BidirectionalStream::Delegate { |
| 35 public: | 34 public: |
| 36 class Delegate { | 35 class Delegate { |
| 37 public: | 36 public: |
| 38 virtual void OnStreamReady() = 0; | 37 virtual void OnStreamReady() = 0; |
| 39 | 38 |
| 40 virtual void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers, | 39 virtual void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers, |
| 41 const char* negotiated_protocol) = 0; | 40 const char* negotiated_protocol) = 0; |
| 42 | 41 |
| 43 virtual void OnDataRead(char* data, int size) = 0; | 42 virtual void OnDataRead(char* data, int size) = 0; |
| 44 | 43 |
| 45 virtual void OnDataSent(const char* data) = 0; | 44 virtual void OnDataSent(const char* data) = 0; |
| 46 | 45 |
| 47 virtual void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) = 0; | 46 virtual void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) = 0; |
| 48 | 47 |
| 49 virtual void OnSucceeded() = 0; | 48 virtual void OnSucceeded() = 0; |
| 50 | 49 |
| 51 virtual void OnFailed(int error) = 0; | 50 virtual void OnFailed(int error) = 0; |
| 52 | 51 |
| 53 virtual void OnCanceled() = 0; | 52 virtual void OnCanceled() = 0; |
| 54 }; | 53 }; |
| 55 | 54 |
| 56 CronetBidirectionalStream(CronetEnvironment* environment, Delegate* delegate); | 55 BidirectionalStream(net::URLRequestContextGetter* request_context_getter, |
| 57 ~CronetBidirectionalStream() override; | 56 Delegate* delegate); |
| 57 ~BidirectionalStream() override; |
| 58 | 58 |
| 59 // Disables automatic flushing of each buffer passed to WriteData(). | 59 // Disables automatic flushing of each buffer passed to WriteData(). |
| 60 void disable_auto_flush(bool disable_auto_flush) { | 60 void disable_auto_flush(bool disable_auto_flush) { |
| 61 disable_auto_flush_ = disable_auto_flush; | 61 disable_auto_flush_ = disable_auto_flush; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Delays sending request headers until first call to Flush(). | 64 // Delays sending request headers until first call to Flush(). |
| 65 void delay_headers_until_flush(bool delay_headers_until_flush) { | 65 void delay_headers_until_flush(bool delay_headers_until_flush) { |
| 66 delay_headers_until_flush_ = delay_headers_until_flush; | 66 delay_headers_until_flush_ = delay_headers_until_flush; |
| 67 } | 67 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // Waiting for ReadData() to be called. | 112 // Waiting for ReadData() to be called. |
| 113 WAITING_FOR_READ, | 113 WAITING_FOR_READ, |
| 114 // Reading from the remote, OnDataRead callback will be invoked when done. | 114 // Reading from the remote, OnDataRead callback will be invoked when done. |
| 115 READING, | 115 READING, |
| 116 // There is no more data to read and stream is half-closed by the remote | 116 // There is no more data to read and stream is half-closed by the remote |
| 117 // side. | 117 // side. |
| 118 READING_DONE, | 118 READING_DONE, |
| 119 // Stream is canceled. | 119 // Stream is canceled. |
| 120 CANCELED, | 120 CANCELED, |
| 121 // Error has occured, stream is closed. | 121 // Error has occured, stream is closed. |
| 122 ERROR, | 122 ERR, |
| 123 // Reading and writing are done, and the stream is closed successfully. | 123 // Reading and writing are done, and the stream is closed successfully. |
| 124 SUCCESS, | 124 SUCCESS, |
| 125 // Waiting for Flush() to be called. | 125 // Waiting for Flush() to be called. |
| 126 WAITING_FOR_FLUSH, | 126 WAITING_FOR_FLUSH, |
| 127 // Writing to the remote, callback will be invoked when done. | 127 // Writing to the remote, callback will be invoked when done. |
| 128 WRITING, | 128 WRITING, |
| 129 // There is no more data to write and stream is half-closed by the local | 129 // There is no more data to write and stream is half-closed by the local |
| 130 // side. | 130 // side. |
| 131 WRITING_DONE, | 131 WRITING_DONE, |
| 132 }; | 132 }; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | 180 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, |
| 181 int buffer_size); | 181 int buffer_size); |
| 182 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | 182 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, |
| 183 int buffer_size, | 183 int buffer_size, |
| 184 bool end_of_stream); | 184 bool end_of_stream); |
| 185 void FlushOnNetworkThread(); | 185 void FlushOnNetworkThread(); |
| 186 void SendFlushingWriteData(); | 186 void SendFlushingWriteData(); |
| 187 void CancelOnNetworkThread(); | 187 void CancelOnNetworkThread(); |
| 188 void DestroyOnNetworkThread(); | 188 void DestroyOnNetworkThread(); |
| 189 | 189 |
| 190 bool IsOnNetworkThread(); |
| 191 void PostToNetworkThread(const tracked_objects::Location& from_here, |
| 192 const base::Closure& task); |
| 193 |
| 190 // Read state is tracking reading flow. Only accessed on network thread. | 194 // Read state is tracking reading flow. Only accessed on network thread. |
| 191 // / <--- READING <--- \ | 195 // | <--- READING <--- | |
| 192 // | | | 196 // | | |
| 193 // \ / | 197 // | | |
| 194 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS | 198 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS |
| 195 State read_state_; | 199 State read_state_; |
| 196 | 200 |
| 197 // Write state is tracking writing flow. Only accessed on network thread. | 201 // Write state is tracking writing flow. Only accessed on network thread. |
| 198 // / <--- WRITING <--- \ | 202 // | <--- WRITING <--- | |
| 199 // | | | 203 // | | |
| 200 // \ / | 204 // | | |
| 201 // NOT_STARTED -> STARTED --> WAITING_FOR_FLUSH -> WRITING_DONE -> SUCCESS | 205 // NOT_STARTED -> STARTED --> WAITING_FOR_FLUSH -> WRITING_DONE -> SUCCESS |
| 202 State write_state_; | 206 State write_state_; |
| 203 | 207 |
| 204 bool write_end_of_stream_; | 208 bool write_end_of_stream_; |
| 205 bool request_headers_sent_; | 209 bool request_headers_sent_; |
| 206 | 210 |
| 207 bool disable_auto_flush_; | 211 bool disable_auto_flush_; |
| 208 bool delay_headers_until_flush_; | 212 bool delay_headers_until_flush_; |
| 209 | 213 |
| 210 CronetEnvironment* const environment_; | 214 net::URLRequestContextGetter* const request_context_getter_; |
| 211 | 215 |
| 212 scoped_refptr<net::WrappedIOBuffer> read_buffer_; | 216 scoped_refptr<net::WrappedIOBuffer> read_buffer_; |
| 213 | 217 |
| 214 // Write data that is pending the flush. | 218 // Write data that is pending the flush. |
| 215 std::unique_ptr<WriteBuffers> pending_write_data_; | 219 std::unique_ptr<WriteBuffers> pending_write_data_; |
| 216 // Write data that is flushed, but not sending yet. | 220 // Write data that is flushed, but not sending yet. |
| 217 std::unique_ptr<WriteBuffers> flushing_write_data_; | 221 std::unique_ptr<WriteBuffers> flushing_write_data_; |
| 218 // Write data that is sending. | 222 // Write data that is sending. |
| 219 std::unique_ptr<WriteBuffers> sending_write_data_; | 223 std::unique_ptr<WriteBuffers> sending_write_data_; |
| 220 | 224 |
| 221 std::unique_ptr<net::BidirectionalStream> bidi_stream_; | 225 std::unique_ptr<net::BidirectionalStream> bidi_stream_; |
| 222 Delegate* delegate_; | 226 Delegate* delegate_; |
| 223 | 227 |
| 224 base::WeakPtr<CronetBidirectionalStream> weak_this_; | 228 base::WeakPtr<BidirectionalStream> weak_this_; |
| 225 base::WeakPtrFactory<CronetBidirectionalStream> weak_factory_; | 229 base::WeakPtrFactory<BidirectionalStream> weak_factory_; |
| 226 | 230 |
| 227 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); | 231 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); |
| 228 }; | 232 }; |
| 229 | 233 |
| 230 } // namespace cronet | 234 } // namespace grpc_support |
| 231 | 235 |
| 232 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | 236 #endif // COMPONENTS_GRPC_SUPPORT_BIDIRECTIONAL_STREAM_H_ |
| OLD | NEW |