| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 5 #ifndef NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
| 6 #define NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 6 #define NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 class HttpNetworkSession; | 22 class HttpNetworkSession; |
| 23 class HttpStream; | 23 class HttpStream; |
| 24 class HttpStreamRequest; | 24 class HttpStreamRequest; |
| 25 class IOBuffer; | 25 class IOBuffer; |
| 26 class ProxyInfo; | 26 class ProxyInfo; |
| 27 class SpdyHeaderBlock; | 27 class SpdyHeaderBlock; |
| 28 struct BidirectionalStreamRequestInfo; | 28 struct BidirectionalStreamRequestInfo; |
| 29 struct SSLConfig; | 29 struct SSLConfig; |
| 30 | 30 |
| 31 // A class to do HTTP/2 bidirectional streaming. Note that at most one each of | 31 // A class to do HTTP/2 bidirectional streaming. Note that at most one each of |
| 32 // ReadData or SendData should be in flight until the operation completes. | 32 // ReadData or SendData/SendvData should be in flight until the operation |
| 33 // The BidirectionalStream must be torn down before the HttpNetworkSession. | 33 // completes. The BidirectionalStream must be torn down before the |
| 34 // HttpNetworkSession. |
| 34 class NET_EXPORT BidirectionalStream | 35 class NET_EXPORT BidirectionalStream |
| 35 : public NON_EXPORTED_BASE(BidirectionalStreamImpl::Delegate), | 36 : public NON_EXPORTED_BASE(BidirectionalStreamImpl::Delegate), |
| 36 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) { | 37 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) { |
| 37 public: | 38 public: |
| 38 // Delegate interface to get notified of success of failure. Callbacks will be | 39 // Delegate interface to get notified of success of failure. Callbacks will be |
| 39 // invoked asynchronously. | 40 // invoked asynchronously. |
| 40 class NET_EXPORT Delegate { | 41 class NET_EXPORT Delegate { |
| 41 public: | 42 public: |
| 42 Delegate(); | 43 Delegate(); |
| 43 | 44 |
| 44 // Called when headers have been sent. This is called at most once for | 45 // Called when the stream is ready for writing and reading. This is called |
| 45 // the lifetime of a stream. | 46 // at most once for the lifetime of a stream. |
| 46 // The delegate may call BidirectionalStream::ReadData to start reading, | 47 // The delegate may call BidirectionalStream::ReadData to start reading, |
| 47 // or call BidirectionalStream::SendData to send data. | 48 // or call BidirectionalStream::SendData to send data. |
| 48 // The delegate should not call BidirectionalStream::Cancel | 49 // The delegate should not call BidirectionalStream::Cancel |
| 49 // during this callback. | 50 // during this callback. |
| 50 virtual void OnHeadersSent() = 0; | 51 virtual void OnStreamReady() = 0; |
| 51 | 52 |
| 52 // Called when headers are received. This is called at most once for the | 53 // Called when headers are received. This is called at most once for the |
| 53 // lifetime of a stream. | 54 // lifetime of a stream. |
| 54 // The delegate may call BidirectionalStream::ReadData to start reading, | 55 // The delegate may call BidirectionalStream::ReadData to start reading, |
| 55 // call BidirectionalStream::SendData to send data, | 56 // call BidirectionalStream::SendData to send data, |
| 56 // or call BidirectionalStream::Cancel to cancel the stream. | 57 // or call BidirectionalStream::Cancel to cancel the stream. |
| 57 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; | 58 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; |
| 58 | 59 |
| 59 // Called when a pending read is completed asynchronously. | 60 // Called when a pending read is completed asynchronously. |
| 60 // |bytes_read| specifies how much data is read. | 61 // |bytes_read| specifies how much data is read. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 86 private: | 87 private: |
| 87 DISALLOW_COPY_AND_ASSIGN(Delegate); | 88 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 // Constructs a BidirectionalStream. |request_info| contains information about | 91 // Constructs a BidirectionalStream. |request_info| contains information about |
| 91 // the request, and must be non-NULL. |session| is the http network session | 92 // the request, and must be non-NULL. |session| is the http network session |
| 92 // with which this request will be made. |delegate| must be non-NULL. | 93 // with which this request will be made. |delegate| must be non-NULL. |
| 93 // |session| and |delegate| must outlive |this|. | 94 // |session| and |delegate| must outlive |this|. |
| 94 BidirectionalStream(scoped_ptr<BidirectionalStreamRequestInfo> request_info, | 95 BidirectionalStream(scoped_ptr<BidirectionalStreamRequestInfo> request_info, |
| 95 HttpNetworkSession* session, | 96 HttpNetworkSession* session, |
| 97 bool disable_auto_flush, |
| 96 Delegate* delegate); | 98 Delegate* delegate); |
| 97 | 99 |
| 98 // Constructor that accepts a Timer, which can be used in tests to control | 100 // Constructor that accepts a Timer, which can be used in tests to control |
| 99 // the buffering of received data. | 101 // the buffering of received data. |
| 100 BidirectionalStream(scoped_ptr<BidirectionalStreamRequestInfo> request_info, | 102 BidirectionalStream(scoped_ptr<BidirectionalStreamRequestInfo> request_info, |
| 101 HttpNetworkSession* session, | 103 HttpNetworkSession* session, |
| 104 bool disable_auto_flush, |
| 102 Delegate* delegate, | 105 Delegate* delegate, |
| 103 scoped_ptr<base::Timer> timer); | 106 scoped_ptr<base::Timer> timer); |
| 104 | 107 |
| 105 // Cancels |stream_request_| or |stream_impl_| if applicable. | 108 // Cancels |stream_request_| or |stream_impl_| if applicable. |
| 106 // |this| should not be destroyed during Delegate::OnHeadersSent or | 109 // |this| should not be destroyed during Delegate::OnHeadersSent or |
| 107 // Delegate::OnDataSent. | 110 // Delegate::OnDataSent. |
| 108 ~BidirectionalStream() override; | 111 ~BidirectionalStream() override; |
| 109 | 112 |
| 110 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, | 113 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, |
| 111 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an | 114 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an |
| 112 // error code if any error occurred. If returns 0, there is no more data to | 115 // error code if any error occurred. If returns 0, there is no more data to |
| 113 // read. This should not be called before Delegate::OnHeadersReceived is | 116 // read. This should not be called before Delegate::OnHeadersReceived is |
| 114 // invoked, and should not be called again unless it returns with number | 117 // invoked, and should not be called again unless it returns with number |
| 115 // greater than 0 or until Delegate::OnDataRead is invoked. | 118 // greater than 0 or until Delegate::OnDataRead is invoked. |
| 116 int ReadData(IOBuffer* buf, int buf_len); | 119 int ReadData(IOBuffer* buf, int buf_len); |
| 117 | 120 |
| 118 // Sends data. This should not be called before Delegate::OnHeadersSent is | 121 // Sends data. This should not be called before Delegate::OnHeadersSent is |
| 119 // invoked, and should not be called again until Delegate::OnDataSent is | 122 // invoked, and should not be called again until Delegate::OnDataSent is |
| 120 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM | 123 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM |
| 121 // flag. | 124 // flag. |
| 122 void SendData(IOBuffer* data, int length, bool end_stream); | 125 void SendData(IOBuffer* data, int length, bool end_stream); |
| 123 | 126 |
| 127 // Same as SendData except this takes in a vector of IOBuffers. |
| 128 void SendvData(const std::vector<IOBuffer*>& buffers, |
| 129 const std::vector<int>& lengths, |
| 130 bool end_stream); |
| 131 |
| 124 // If |stream_request_| is non-NULL, cancel it. If |stream_impl_| is | 132 // If |stream_request_| is non-NULL, cancel it. If |stream_impl_| is |
| 125 // established, cancel it. No delegate method will be called after Cancel(). | 133 // established, cancel it. No delegate method will be called after Cancel(). |
| 126 // Any pending operations may or may not succeed. | 134 // Any pending operations may or may not succeed. |
| 127 void Cancel(); | 135 void Cancel(); |
| 128 | 136 |
| 129 // Returns the protocol used by this stream. If stream has not been | 137 // Returns the protocol used by this stream. If stream has not been |
| 130 // established, return kProtoUnknown. | 138 // established, return kProtoUnknown. |
| 131 NextProto GetProtocol() const; | 139 NextProto GetProtocol() const; |
| 132 | 140 |
| 133 // Total number of bytes received over the network of SPDY data, headers, and | 141 // Total number of bytes received over the network of SPDY data, headers, and |
| 134 // push_promise frames associated with this stream, including the size of | 142 // push_promise frames associated with this stream, including the size of |
| 135 // frame headers, after SSL decryption and not including proxy overhead. | 143 // frame headers, after SSL decryption and not including proxy overhead. |
| 136 // If stream has not been established, return 0. | 144 // If stream has not been established, return 0. |
| 137 int64_t GetTotalReceivedBytes() const; | 145 int64_t GetTotalReceivedBytes() const; |
| 138 | 146 |
| 139 // Total number of bytes sent over the network of SPDY frames associated with | 147 // Total number of bytes sent over the network of SPDY frames associated with |
| 140 // this stream, including the size of frame headers, before SSL encryption and | 148 // this stream, including the size of frame headers, before SSL encryption and |
| 141 // not including proxy overhead. Note that some SPDY frames such as pings are | 149 // not including proxy overhead. Note that some SPDY frames such as pings are |
| 142 // not associated with any stream, and are not included in this value. | 150 // not associated with any stream, and are not included in this value. |
| 143 int64_t GetTotalSentBytes() const; | 151 int64_t GetTotalSentBytes() const; |
| 144 | 152 |
| 145 // TODO(xunjieli): Implement a method to do flow control and a method to ping | 153 // TODO(xunjieli): Implement a method to do flow control and a method to ping |
| 146 // remote end point. | 154 // remote end point. |
| 147 | 155 |
| 148 private: | 156 private: |
| 149 // BidirectionalStreamImpl::Delegate implementation: | 157 // BidirectionalStreamImpl::Delegate implementation: |
| 150 void OnHeadersSent() override; | 158 void OnStreamReady() override; |
| 151 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; | 159 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; |
| 152 void OnDataRead(int bytes_read) override; | 160 void OnDataRead(int bytes_read) override; |
| 153 void OnDataSent() override; | 161 void OnDataSent() override; |
| 154 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; | 162 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; |
| 155 void OnFailed(int error) override; | 163 void OnFailed(int error) override; |
| 156 | 164 |
| 157 // HttpStreamRequest::Delegate implementation: | 165 // HttpStreamRequest::Delegate implementation: |
| 158 void OnStreamReady(const SSLConfig& used_ssl_config, | 166 void OnStreamReady(const SSLConfig& used_ssl_config, |
| 159 const ProxyInfo& used_proxy_info, | 167 const ProxyInfo& used_proxy_info, |
| 160 HttpStream* stream) override; | 168 HttpStream* stream) override; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 183 const ProxyInfo& used_proxy_info, | 191 const ProxyInfo& used_proxy_info, |
| 184 HttpStream* stream) override; | 192 HttpStream* stream) override; |
| 185 void OnQuicBroken() override; | 193 void OnQuicBroken() override; |
| 186 | 194 |
| 187 // BidirectionalStreamRequestInfo used when requesting the stream. | 195 // BidirectionalStreamRequestInfo used when requesting the stream. |
| 188 scoped_ptr<BidirectionalStreamRequestInfo> request_info_; | 196 scoped_ptr<BidirectionalStreamRequestInfo> request_info_; |
| 189 const BoundNetLog net_log_; | 197 const BoundNetLog net_log_; |
| 190 | 198 |
| 191 HttpNetworkSession* session_; | 199 HttpNetworkSession* session_; |
| 192 | 200 |
| 201 bool disable_auto_flush_; |
| 193 Delegate* const delegate_; | 202 Delegate* const delegate_; |
| 194 | 203 |
| 195 // Timer used to buffer data received in short time-spans and send a single | 204 // Timer used to buffer data received in short time-spans and send a single |
| 196 // read completion notification. | 205 // read completion notification. |
| 197 scoped_ptr<base::Timer> timer_; | 206 scoped_ptr<base::Timer> timer_; |
| 198 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL | 207 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL |
| 199 // if the request has been canceled or completed. | 208 // if the request has been canceled or completed. |
| 200 scoped_ptr<HttpStreamRequest> stream_request_; | 209 scoped_ptr<HttpStreamRequest> stream_request_; |
| 201 // The underlying BidirectioanlStreamImpl used for this stream. It is | 210 // The underlying BidirectioanlStreamImpl used for this stream. It is |
| 202 // non-NULL, if the |stream_request_| successfully finishes. | 211 // non-NULL, if the |stream_request_| successfully finishes. |
| 203 scoped_ptr<BidirectionalStreamImpl> stream_impl_; | 212 scoped_ptr<BidirectionalStreamImpl> stream_impl_; |
| 204 | 213 |
| 205 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); | 214 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); |
| 206 }; | 215 }; |
| 207 | 216 |
| 208 } // namespace net | 217 } // namespace net |
| 209 | 218 |
| 210 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 219 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
| OLD | NEW |