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 struct HttpRequestInfo; | |
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 an error occurs. E.g. when a connection to the server cannot | |
41 // be established. | |
42 virtual void OnFailed(Error error) = 0; | |
43 | |
44 // Called when the request headers have been sent. | |
45 virtual void OnRequestHeadersSent() = 0; | |
46 | |
47 // Called when response headers are received. | |
48 virtual void OnHeaders(const SpdyHeaderBlock& response_headers) = 0; | |
49 | |
50 // Called when read is completed asynchronously. |bytes_read| specifies how | |
51 // much data is available. | |
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. No other delegate functions will be | |
61 // called after this. |status| is a net error code or OK. | |
mef
2015/11/24 20:03:40
If |status| is a net error code, then how is it di
xunjieli
2015/11/25 16:58:58
Done. Good point. I think we should combine these
| |
62 virtual void OnClose(int status) = 0; | |
63 | |
64 protected: | |
65 virtual ~Delegate() {} | |
66 | |
67 private: | |
68 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
69 }; | |
70 | |
71 // Parts of |request_info| are ignored (e.g. |upload_data_stream| and most | |
72 // load flags). | |
73 BidirectionalStream(const HttpRequestInfo& request_info, | |
74 RequestPriority priority, | |
75 HttpNetworkSession* session, | |
76 Delegate* delegate); | |
77 | |
78 // Destroys the helper and cancels any pending request. | |
79 ~BidirectionalStream() override; | |
80 | |
81 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, | |
82 // or ERR_IO_PENDING if the read is to be completed asynchronously. | |
83 // There should not be called again unless it IO completes synchronously or | |
84 // until OnReadCompleted is invoked. | |
85 int ReadData(IOBuffer* buf, int buf_len); | |
86 | |
87 // Sends data. This should not be called again until OnDataSent is invoked. | |
88 void SendData(IOBuffer* data, int length, bool end_stream); | |
89 | |
90 // If there is |stream_request_|, cancel it. If |stream_job_| is established, | |
91 // cancel it. | |
92 void Cancel(); | |
93 | |
94 // TODO(xunjieli): implement a method to do flow control, and a method to ping | |
95 // remote end point. | |
96 | |
97 private: | |
98 // BidirectionalStreamJob::Delegate implementation: | |
99 | |
100 void OnFailed(Error error) override; | |
101 | |
102 void OnRequestHeadersSent() override; | |
103 | |
104 void OnHeaders(const SpdyHeaderBlock& response_headers) override; | |
105 | |
106 void OnReadCompleted(int bytes_read) override; | |
107 | |
108 void OnDataSent() override; | |
109 | |
110 void OnTrailers(const SpdyHeaderBlock& trailers) override; | |
111 | |
112 void OnClose(int status) override; | |
113 | |
114 // HttpStreamRequest::Delegate implementation: | |
115 | |
116 void OnStreamReady(const SSLConfig& used_ssl_config, | |
117 const ProxyInfo& used_proxy_info, | |
118 HttpStream* stream) override; | |
119 void OnBidirectionalStreamJobReady(const SSLConfig& used_ssl_config, | |
120 const ProxyInfo& used_proxy_info, | |
121 BidirectionalStreamJob* stream) override; | |
122 void OnWebSocketHandshakeStreamReady( | |
123 const SSLConfig& used_ssl_config, | |
124 const ProxyInfo& used_proxy_info, | |
125 WebSocketHandshakeStreamBase* stream) override; | |
126 void OnStreamFailed(int status, | |
127 const SSLConfig& used_ssl_config, | |
128 SSLFailureState ssl_failure_state) override; | |
129 void OnCertificateError(int status, | |
130 const SSLConfig& used_ssl_config, | |
131 const SSLInfo& ssl_info) override; | |
132 void OnNeedsProxyAuth(const HttpResponseInfo& response_info, | |
133 const SSLConfig& used_ssl_config, | |
134 const ProxyInfo& used_proxy_info, | |
135 HttpAuthController* auth_controller) override; | |
136 void OnNeedsClientAuth(const SSLConfig& used_ssl_config, | |
137 SSLCertRequestInfo* cert_info) override; | |
138 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info, | |
139 const SSLConfig& used_ssl_config, | |
140 const ProxyInfo& used_proxy_info, | |
141 HttpStream* stream) override; | |
142 | |
143 const HttpRequestInfo request_info_; | |
144 RequestPriority priority_; | |
145 BoundNetLog net_log_; | |
146 | |
147 Delegate* delegate_; | |
148 | |
149 scoped_ptr<HttpStreamRequest> stream_request_; | |
150 scoped_ptr<BidirectionalStreamJob> stream_job_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); | |
153 }; | |
154 | |
155 } // namespace net | |
156 | |
157 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ | |
OLD | NEW |