xref: /aosp_15_r20/external/cronet/net/http/bidirectional_stream_impl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
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_IMPL_H_
6 #define NET_HTTP_BIDIRECTIONAL_STREAM_IMPL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/memory/scoped_refptr.h"
14 #include "net/base/load_timing_info.h"
15 #include "net/base/net_export.h"
16 #include "net/socket/next_proto.h"
17 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
18 #include "net/traffic_annotation/network_traffic_annotation.h"
19 
20 namespace base {
21 class OneShotTimer;
22 }  // namespace base
23 
24 namespace net {
25 
26 class IOBuffer;
27 class NetLogWithSource;
28 struct BidirectionalStreamRequestInfo;
29 struct NetErrorDetails;
30 
31 // Exposes an interface to do HTTP/2 bidirectional streaming.
32 // Note that only one ReadData or SendData should be in flight until the
33 // operation completes synchronously or asynchronously.
34 // BidirectionalStreamImpl once created by HttpStreamFactory should be owned
35 // by BidirectionalStream.
36 class NET_EXPORT_PRIVATE BidirectionalStreamImpl {
37  public:
38   // Delegate to handle BidirectionalStreamImpl events.
39   class NET_EXPORT_PRIVATE Delegate {
40    public:
41     Delegate();
42 
43     Delegate(const Delegate&) = delete;
44     Delegate& operator=(const Delegate&) = delete;
45 
46     // Called when the stream is ready for reading and writing.
47     // The delegate may call BidirectionalStreamImpl::ReadData to start reading,
48     // call BidirectionalStreamImpl::SendData to send data,
49     // or call BidirectionalStreamImpl::Cancel to cancel the stream.
50     // The delegate should not call BidirectionalStreamImpl::Cancel
51     // during this callback.
52     // |request_headers_sent| if true, request headers have been sent. If false,
53     // SendRequestHeaders() needs to be explicitly called.
54     virtual void OnStreamReady(bool request_headers_sent) = 0;
55 
56     // Called when response headers are received.
57     // This is called at most once for the lifetime of a stream.
58     // The delegate may call BidirectionalStreamImpl::ReadData to start
59     // reading, call BidirectionalStreamImpl::SendData to send data,
60     // or call BidirectionalStreamImpl::Cancel to cancel the stream.
61     virtual void OnHeadersReceived(
62         const spdy::Http2HeaderBlock& response_headers) = 0;
63 
64     // Called when read is completed asynchronously. |bytes_read| specifies how
65     // much data is available.
66     // The delegate may call BidirectionalStreamImpl::ReadData to continue
67     // reading, call BidirectionalStreamImpl::SendData to send data,
68     // or call BidirectionalStreamImpl::Cancel to cancel the stream.
69     virtual void OnDataRead(int bytes_read) = 0;
70 
71     // Called when the entire buffer passed through SendData is sent.
72     // The delegate may call BidirectionalStreamImpl::ReadData to continue
73     // reading, or call BidirectionalStreamImpl::SendData to send data.
74     // The delegate should not call BidirectionalStreamImpl::Cancel
75     // during this callback.
76     virtual void OnDataSent() = 0;
77 
78     // Called when trailers are received. This is called as soon as trailers
79     // are received, which can happen before a read completes.
80     // The delegate is able to continue reading if there is no pending read and
81     // EOF has not been received, or to send data if there is no pending send.
82     virtual void OnTrailersReceived(const spdy::Http2HeaderBlock& trailers) = 0;
83 
84     // Called when an error occurred. Do not call into the stream after this
85     // point. No other delegate functions will be called after this.
86     virtual void OnFailed(int status) = 0;
87 
88    protected:
89     virtual ~Delegate();
90   };
91 
92   BidirectionalStreamImpl();
93 
94   BidirectionalStreamImpl(const BidirectionalStreamImpl&) = delete;
95   BidirectionalStreamImpl& operator=(const BidirectionalStreamImpl&) = delete;
96 
97   // |this| should not be destroyed during Delegate::OnHeadersSent or
98   // Delegate::OnDataSent.
99   virtual ~BidirectionalStreamImpl();
100 
101   // Starts the BidirectionalStreamImpl and sends request headers.
102   // |send_request_headers_automatically| if true, request headers will be sent
103   // automatically when stream is negotiated. If false, request headers will be
104   // sent only when SendRequestHeaders() is invoked or with next
105   // SendData/SendvData.
106   virtual void Start(const BidirectionalStreamRequestInfo* request_info,
107                      const NetLogWithSource& net_log,
108                      bool send_request_headers_automatically,
109                      BidirectionalStreamImpl::Delegate* delegate,
110                      std::unique_ptr<base::OneShotTimer> timer,
111                      const NetworkTrafficAnnotationTag& traffic_annotation) = 0;
112 
113   // Sends request headers to server.
114   // When |send_request_headers_automatically_| is
115   // false and OnStreamReady() is invoked with request_headers_sent = false,
116   // headers will be combined with next SendData/SendvData unless this
117   // method is called first, in which case headers will be sent separately
118   // without delay.
119   // (This method cannot be called when |send_request_headers_automatically_| is
120   // true nor when OnStreamReady() is invoked with request_headers_sent = true,
121   // since headers have been sent by the stream when stream is negotiated
122   // successfully.)
123   virtual void SendRequestHeaders() = 0;
124 
125   // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read,
126   // ERR_IO_PENDING if the read is to be completed asynchronously, or an error
127   // code if any error occurred. If returns 0, there is no more data to read.
128   // This should not be called before Delegate::OnHeadersReceived is invoked,
129   // and should not be called again unless it returns with number greater than
130   // 0 or until Delegate::OnDataRead is invoked.
131   virtual int ReadData(IOBuffer* buf, int buf_len) = 0;
132 
133   // Sends data. This should not be called be called before
134   // Delegate::OnHeadersSent is invoked, and should not be called again until
135   // Delegate::OnDataSent is invoked. If |end_stream| is true, the DATA frame
136   // will have an END_STREAM flag.
137   virtual void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers,
138                          const std::vector<int>& lengths,
139                          bool end_stream) = 0;
140 
141   // Returns the protocol used by this stream. If stream has not been
142   // established, return kProtoUnknown.
143   virtual NextProto GetProtocol() const = 0;
144 
145   // Total number of bytes received over the network of SPDY data, headers, and
146   // push_promise frames associated with this stream, including the size of
147   // frame headers, after SSL decryption and not including proxy overhead.
148   virtual int64_t GetTotalReceivedBytes() const = 0;
149 
150   // Total number of bytes sent over the network of SPDY frames associated with
151   // this stream, including the size of frame headers, before SSL encryption and
152   // not including proxy overhead. Note that some SPDY frames such as pings are
153   // not associated with any stream, and are not included in this value.
154   virtual int64_t GetTotalSentBytes() const = 0;
155 
156   // Populates the connection establishment part of |load_timing_info|, and
157   // socket reuse info. Return true if LoadTimingInfo is obtained successfully
158   // and false otherwise.
159   virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
160 
161   // Get the network error details this stream is encountering.
162   // Fills in |details| if it is available; leaves |details| unchanged if it
163   // is unavailable.
164   virtual void PopulateNetErrorDetails(NetErrorDetails* details) = 0;
165 };
166 
167 }  // namespace net
168 
169 #endif  // NET_HTTP_BIDIRECTIONAL_STREAM_IMPL_H_
170