| 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 <memory> | 10 #include <memory> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 class NET_EXPORT Delegate { | 43 class NET_EXPORT Delegate { |
| 44 public: | 44 public: |
| 45 Delegate(); | 45 Delegate(); |
| 46 | 46 |
| 47 // Called when the stream is ready for writing and reading. This is called | 47 // Called when the stream is ready for writing and reading. This is called |
| 48 // at most once for the lifetime of a stream. | 48 // at most once for the lifetime of a stream. |
| 49 // The delegate may call BidirectionalStream::ReadData to start reading, | 49 // The delegate may call BidirectionalStream::ReadData to start reading, |
| 50 // or call BidirectionalStream::SendData to send data. | 50 // or call BidirectionalStream::SendData to send data. |
| 51 // The delegate should not call BidirectionalStream::Cancel | 51 // The delegate should not call BidirectionalStream::Cancel |
| 52 // during this callback. | 52 // during this callback. |
| 53 virtual void OnStreamReady() = 0; | 53 // |request_headers_sent| if true, request headers have been sent. If false, |
| 54 // SendRequestHeaders() needs to be explicitly called. |
| 55 virtual void OnStreamReady(bool request_headers_sent) = 0; |
| 54 | 56 |
| 55 // Called when headers are received. This is called at most once for the | 57 // Called when headers are received. This is called at most once for the |
| 56 // lifetime of a stream. | 58 // lifetime of a stream. |
| 57 // The delegate may call BidirectionalStream::ReadData to start reading, | 59 // The delegate may call BidirectionalStream::ReadData to start reading, |
| 58 // call BidirectionalStream::SendData to send data, | 60 // call BidirectionalStream::SendData to send data, |
| 59 // or call BidirectionalStream::Cancel to cancel the stream. | 61 // or call BidirectionalStream::Cancel to cancel the stream. |
| 60 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; | 62 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; |
| 61 | 63 |
| 62 // Called when a pending read is completed asynchronously. | 64 // Called when a pending read is completed asynchronously. |
| 63 // |bytes_read| specifies how much data is read. | 65 // |bytes_read| specifies how much data is read. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 87 virtual ~Delegate(); | 89 virtual ~Delegate(); |
| 88 | 90 |
| 89 private: | 91 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(Delegate); | 92 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 91 }; | 93 }; |
| 92 | 94 |
| 93 // Constructs a BidirectionalStream. |request_info| contains information about | 95 // Constructs a BidirectionalStream. |request_info| contains information about |
| 94 // the request, and must be non-NULL. |session| is the http network session | 96 // the request, and must be non-NULL. |session| is the http network session |
| 95 // with which this request will be made. |delegate| must be non-NULL. | 97 // with which this request will be made. |delegate| must be non-NULL. |
| 96 // |session| and |delegate| must outlive |this|. | 98 // |session| and |delegate| must outlive |this|. |
| 99 // |send_request_headers_automatically| if true, request headers will be sent |
| 100 // automatically when stream is negotiated. If false, request headers will be |
| 101 // sent only when SendRequestHeaders() is invoked or with |
| 102 // next SendData/SendvData. |
| 97 BidirectionalStream( | 103 BidirectionalStream( |
| 98 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 104 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 99 HttpNetworkSession* session, | 105 HttpNetworkSession* session, |
| 100 bool disable_auto_flush, | 106 bool send_request_headers_automatically, |
| 101 Delegate* delegate); | 107 Delegate* delegate); |
| 102 | 108 |
| 103 // Constructor that accepts a Timer, which can be used in tests to control | 109 // Constructor that accepts a Timer, which can be used in tests to control |
| 104 // the buffering of received data. | 110 // the buffering of received data. |
| 105 BidirectionalStream( | 111 BidirectionalStream( |
| 106 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 112 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 107 HttpNetworkSession* session, | 113 HttpNetworkSession* session, |
| 108 bool disable_auto_flush, | 114 bool send_request_headers_automatically, |
| 109 Delegate* delegate, | 115 Delegate* delegate, |
| 110 std::unique_ptr<base::Timer> timer); | 116 std::unique_ptr<base::Timer> timer); |
| 111 | 117 |
| 112 // Cancels |stream_request_| or |stream_impl_| if applicable. | 118 // Cancels |stream_request_| or |stream_impl_| if applicable. |
| 113 // |this| should not be destroyed during Delegate::OnHeadersSent or | 119 // |this| should not be destroyed during Delegate::OnHeadersSent or |
| 114 // Delegate::OnDataSent. | 120 // Delegate::OnDataSent. |
| 115 ~BidirectionalStream() override; | 121 ~BidirectionalStream() override; |
| 116 | 122 |
| 123 // Sends request headers to server. |
| 124 // When |send_request_headers_automatically_| is |
| 125 // false and OnStreamReady() is invoked with request_headers_sent = false, |
| 126 // headers will be combined with next SendData/SendvData unless this |
| 127 // method is called first, in which case headers will be sent separately |
| 128 // without delay. |
| 129 // (This method cannot be called when |send_request_headers_automatically_| is |
| 130 // true nor when OnStreamReady() is invoked with request_headers_sent = true, |
| 131 // since headers have been sent by the stream when stream is negotiated |
| 132 // successfully.) |
| 133 void SendRequestHeaders(); |
| 134 |
| 117 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, | 135 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, |
| 118 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an | 136 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an |
| 119 // error code if any error occurred. If returns 0, there is no more data to | 137 // error code if any error occurred. If returns 0, there is no more data to |
| 120 // read. This should not be called before Delegate::OnHeadersReceived is | 138 // read. This should not be called before Delegate::OnHeadersReceived is |
| 121 // invoked, and should not be called again unless it returns with number | 139 // invoked, and should not be called again unless it returns with number |
| 122 // greater than 0 or until Delegate::OnDataRead is invoked. | 140 // greater than 0 or until Delegate::OnDataRead is invoked. |
| 123 int ReadData(IOBuffer* buf, int buf_len); | 141 int ReadData(IOBuffer* buf, int buf_len); |
| 124 | 142 |
| 125 // Sends data. This should not be called before Delegate::OnHeadersSent is | 143 // Sends data. This should not be called before Delegate::OnHeadersSent is |
| 126 // invoked, and should not be called again until Delegate::OnDataSent is | 144 // invoked, and should not be called again until Delegate::OnDataSent is |
| (...skipping 27 matching lines...) Expand all Loading... |
| 154 // this stream, including the size of frame headers, before SSL encryption and | 172 // this stream, including the size of frame headers, before SSL encryption and |
| 155 // not including proxy overhead. Note that some SPDY frames such as pings are | 173 // not including proxy overhead. Note that some SPDY frames such as pings are |
| 156 // not associated with any stream, and are not included in this value. | 174 // not associated with any stream, and are not included in this value. |
| 157 int64_t GetTotalSentBytes() const; | 175 int64_t GetTotalSentBytes() const; |
| 158 | 176 |
| 159 // TODO(xunjieli): Implement a method to do flow control and a method to ping | 177 // TODO(xunjieli): Implement a method to do flow control and a method to ping |
| 160 // remote end point. | 178 // remote end point. |
| 161 | 179 |
| 162 private: | 180 private: |
| 163 // BidirectionalStreamImpl::Delegate implementation: | 181 // BidirectionalStreamImpl::Delegate implementation: |
| 164 void OnStreamReady() override; | 182 void OnStreamReady(bool request_headers_sent) override; |
| 165 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; | 183 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; |
| 166 void OnDataRead(int bytes_read) override; | 184 void OnDataRead(int bytes_read) override; |
| 167 void OnDataSent() override; | 185 void OnDataSent() override; |
| 168 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; | 186 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; |
| 169 void OnFailed(int error) override; | 187 void OnFailed(int error) override; |
| 170 | 188 |
| 171 // HttpStreamRequest::Delegate implementation: | 189 // HttpStreamRequest::Delegate implementation: |
| 172 void OnStreamReady(const SSLConfig& used_ssl_config, | 190 void OnStreamReady(const SSLConfig& used_ssl_config, |
| 173 const ProxyInfo& used_proxy_info, | 191 const ProxyInfo& used_proxy_info, |
| 174 HttpStream* stream) override; | 192 HttpStream* stream) override; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 197 const ProxyInfo& used_proxy_info, | 215 const ProxyInfo& used_proxy_info, |
| 198 HttpStream* stream) override; | 216 HttpStream* stream) override; |
| 199 void OnQuicBroken() override; | 217 void OnQuicBroken() override; |
| 200 | 218 |
| 201 // BidirectionalStreamRequestInfo used when requesting the stream. | 219 // BidirectionalStreamRequestInfo used when requesting the stream. |
| 202 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_; | 220 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_; |
| 203 const BoundNetLog net_log_; | 221 const BoundNetLog net_log_; |
| 204 | 222 |
| 205 HttpNetworkSession* session_; | 223 HttpNetworkSession* session_; |
| 206 | 224 |
| 207 bool disable_auto_flush_; | 225 bool send_request_headers_automatically_; |
| 226 // Whether request headers have been sent, as indicated in OnStreamReady() |
| 227 // callback. |
| 228 bool request_headers_sent_; |
| 229 |
| 208 Delegate* const delegate_; | 230 Delegate* const delegate_; |
| 209 | 231 |
| 210 // Timer used to buffer data received in short time-spans and send a single | 232 // Timer used to buffer data received in short time-spans and send a single |
| 211 // read completion notification. | 233 // read completion notification. |
| 212 std::unique_ptr<base::Timer> timer_; | 234 std::unique_ptr<base::Timer> timer_; |
| 213 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL | 235 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL |
| 214 // if the request has been canceled or completed. | 236 // if the request has been canceled or completed. |
| 215 std::unique_ptr<HttpStreamRequest> stream_request_; | 237 std::unique_ptr<HttpStreamRequest> stream_request_; |
| 216 // The underlying BidirectioanlStreamImpl used for this stream. It is | 238 // The underlying BidirectioanlStreamImpl used for this stream. It is |
| 217 // non-NULL, if the |stream_request_| successfully finishes. | 239 // non-NULL, if the |stream_request_| successfully finishes. |
| 218 std::unique_ptr<BidirectionalStreamImpl> stream_impl_; | 240 std::unique_ptr<BidirectionalStreamImpl> stream_impl_; |
| 219 | 241 |
| 220 // Buffer used for reading. | 242 // Buffer used for reading. |
| 221 scoped_refptr<IOBuffer> read_buffer_; | 243 scoped_refptr<IOBuffer> read_buffer_; |
| 222 // List of buffers used for writing. | 244 // List of buffers used for writing. |
| 223 std::vector<scoped_refptr<IOBuffer>> write_buffer_list_; | 245 std::vector<scoped_refptr<IOBuffer>> write_buffer_list_; |
| 224 // List of buffer length. | 246 // List of buffer length. |
| 225 std::vector<int> write_buffer_len_list_; | 247 std::vector<int> write_buffer_len_list_; |
| 226 | 248 |
| 227 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); | 249 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); |
| 228 }; | 250 }; |
| 229 | 251 |
| 230 } // namespace net | 252 } // namespace net |
| 231 | 253 |
| 232 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ | 254 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
| OLD | NEW |