Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_HTTP_BIDIRECTIONAL_STREAM_H_ | |
| 6 #define NET_HTTP_BIDIRECTIONAL_STREAM_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "net/base/request_priority.h" | |
| 11 #include "net/http/bidirectional_stream_job.h" | |
| 12 #include "net/http/http_stream_factory.h" | |
| 13 #include "net/ssl/ssl_config.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 class BoundNetLog; | |
| 18 class HttpAuthController; | |
| 19 class HttpNetworkSession; | |
| 20 class HttpStream; | |
| 21 class HttpStreamRequest; | |
| 22 class IOBuffer; | |
| 23 class ProxyInfo; | |
| 24 class SpdyHeaderBlock; | |
| 25 | |
| 26 // A class to do HTTP/2 bidirectional streaming. Note that only one | |
| 27 // ReadData or SendData should be in flight until the operation completes | |
| 28 // synchronously or asynchronously. The BidirectionalStream must be torn down | |
| 29 // before the HttpNetworkSession. | |
| 30 class NET_EXPORT BidirectionalStream : public BidirectionalStreamJob::Delegate, | |
| 31 public HttpStreamRequest::Delegate { | |
| 32 public: | |
| 33 // Delegate interface to get notified of success of failure. Callbacks will be | |
| 34 // invoked asynchronously. | |
| 35 // TODO(xunjieli): Surface the protocol negotiated. | |
| 36 class NET_EXPORT Delegate { | |
| 37 public: | |
| 38 Delegate() {} | |
| 39 | |
| 40 // Called when the request headers have been sent. | |
| 41 virtual void OnRequestHeadersSent() = 0; | |
| 42 | |
| 43 // Called when response headers are received. | |
| 44 // The delegate should call BidirectionalStream::ReadData to start reading | |
| 45 // or call BidirectionalStream::Cancel to cancel the stream. | |
| 46 virtual void OnHeaders(const SpdyHeaderBlock& response_headers) = 0; | |
| 47 | |
| 48 // Called when read is completed asynchronously. |bytes_read| specifies how | |
| 49 // much data is available. | |
| 50 // The delegate should call BidirectionalStream::ReadData to continue | |
| 51 // reading or call BidirectionalStream::Cancel to cancel the stream. | |
| 52 virtual void OnReadCompleted(int bytes_read) = 0; | |
| 53 | |
| 54 // Called when the entire buffer passed through SendData is sent. | |
| 55 virtual void OnDataSent() = 0; | |
| 56 | |
| 57 // Called when trailers are received. | |
| 58 virtual void OnTrailers(const SpdyHeaderBlock& trailers) = 0; | |
| 59 | |
| 60 // Called when the stream is closed or an error occurred. | |
| 61 // No other delegate functions will be called after this. | |
| 62 virtual void OnFailed(int error) = 0; | |
| 63 | |
| 64 protected: | |
| 65 virtual ~Delegate() {} | |
| 66 | |
| 67 private: | |
| 68 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
| 69 }; | |
| 70 | |
| 71 struct NET_EXPORT RequestInfo { | |
| 72 RequestInfo(); | |
| 73 ~RequestInfo(); | |
| 74 | |
| 75 // The requested URL. | |
| 76 GURL url; | |
| 77 | |
| 78 // The method to use (GET, POST, etc.). | |
| 79 std::string method; | |
| 80 | |
| 81 // Any extra request headers (including User-Agent). | |
| 82 HttpRequestHeaders extra_headers; | |
| 83 }; | |
| 84 | |
| 85 BidirectionalStream(const RequestInfo& request_info, | |
| 86 RequestPriority priority, | |
| 87 HttpNetworkSession* session, | |
| 88 Delegate* delegate); | |
| 89 | |
| 90 // Destroys the helper and cancels any pending request. | |
| 91 ~BidirectionalStream() override; | |
| 92 | |
| 93 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, | |
| 94 // or ERR_IO_PENDING if the read is to be completed asynchronously. | |
| 95 // There should not be called again unless it IO completes synchronously or | |
| 96 // until OnReadCompleted is invoked. | |
| 97 int ReadData(IOBuffer* buf, int buf_len); | |
| 98 | |
| 99 // Sends data. This should not be called again until OnDataSent is invoked. | |
| 100 void SendData(IOBuffer* data, int length, bool end_stream); | |
| 101 | |
| 102 // If there is |stream_request_|, cancel it. If |stream_job_| is established, | |
| 103 // cancel it. | |
| 104 void Cancel(); | |
| 105 | |
| 106 // TODO(xunjieli): implement a method to do flow control and a method to ping | |
|
mef
2015/12/07 21:18:07
Can we also add methods that expose some metrics f
xunjieli
2015/12/08 16:08:46
Done.
| |
| 107 // remote end point. | |
| 108 | |
| 109 private: | |
| 110 // BidirectionalStreamJob::Delegate implementation: | |
| 111 void OnRequestHeadersSent() override; | |
| 112 void OnHeaders(const SpdyHeaderBlock& response_headers) override; | |
| 113 void OnReadCompleted(int bytes_read) override; | |
| 114 void OnDataSent() override; | |
| 115 void OnTrailers(const SpdyHeaderBlock& trailers) override; | |
| 116 void OnFailed(int error) override; | |
| 117 | |
| 118 // HttpStreamRequest::Delegate implementation: | |
| 119 void OnStreamReady(const SSLConfig& used_ssl_config, | |
| 120 const ProxyInfo& used_proxy_info, | |
| 121 HttpStream* stream) override; | |
| 122 void OnBidirectionalStreamJobReady(const SSLConfig& used_ssl_config, | |
| 123 const ProxyInfo& used_proxy_info, | |
| 124 BidirectionalStreamJob* stream) override; | |
| 125 void OnWebSocketHandshakeStreamReady( | |
| 126 const SSLConfig& used_ssl_config, | |
| 127 const ProxyInfo& used_proxy_info, | |
| 128 WebSocketHandshakeStreamBase* stream) override; | |
| 129 void OnStreamFailed(int status, | |
| 130 const SSLConfig& used_ssl_config, | |
| 131 SSLFailureState ssl_failure_state) override; | |
| 132 void OnCertificateError(int status, | |
| 133 const SSLConfig& used_ssl_config, | |
| 134 const SSLInfo& ssl_info) override; | |
| 135 void OnNeedsProxyAuth(const HttpResponseInfo& response_info, | |
| 136 const SSLConfig& used_ssl_config, | |
| 137 const ProxyInfo& used_proxy_info, | |
| 138 HttpAuthController* auth_controller) override; | |
| 139 void OnNeedsClientAuth(const SSLConfig& used_ssl_config, | |
| 140 SSLCertRequestInfo* cert_info) override; | |
| 141 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info, | |
| 142 const SSLConfig& used_ssl_config, | |
| 143 const ProxyInfo& used_proxy_info, | |
| 144 HttpStream* stream) override; | |
| 145 | |
| 146 const RequestInfo request_info_; | |
| 147 RequestPriority priority_; | |
| 148 BoundNetLog net_log_; | |
| 149 | |
| 150 Delegate* delegate_; | |
| 151 | |
| 152 scoped_ptr<HttpStreamRequest> stream_request_; | |
| 153 scoped_ptr<BidirectionalStreamJob> stream_job_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); | |
| 156 }; | |
| 157 | |
| 158 } // namespace net | |
| 159 | |
| 160 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ | |
| OLD | NEW |