xref: /aosp_15_r20/external/cronet/net/spdy/bidirectional_stream_spdy_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_SPDY_BIDIRECTIONAL_STREAM_SPDY_IMPL_H_
6 #define NET_SPDY_BIDIRECTIONAL_STREAM_SPDY_IMPL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/memory/raw_ptr.h"
14 #include "base/memory/scoped_refptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "net/base/load_timing_info.h"
17 #include "net/base/net_export.h"
18 #include "net/http/bidirectional_stream_impl.h"
19 #include "net/http/bidirectional_stream_request_info.h"
20 #include "net/http/http_request_info.h"
21 #include "net/log/net_log_source.h"
22 #include "net/spdy/spdy_read_queue.h"
23 #include "net/spdy/spdy_session.h"
24 #include "net/spdy/spdy_stream.h"
25 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
26 
27 namespace base {
28 class OneShotTimer;
29 }  // namespace base
30 
31 namespace net {
32 
33 class IOBuffer;
34 class NetLogWithSource;
35 
36 class NET_EXPORT_PRIVATE BidirectionalStreamSpdyImpl
37     : public BidirectionalStreamImpl,
38       public SpdyStream::Delegate {
39  public:
40   BidirectionalStreamSpdyImpl(const base::WeakPtr<SpdySession>& spdy_session,
41                               NetLogSource source_dependency);
42 
43   BidirectionalStreamSpdyImpl(const BidirectionalStreamSpdyImpl&) = delete;
44   BidirectionalStreamSpdyImpl& operator=(const BidirectionalStreamSpdyImpl&) =
45       delete;
46 
47   ~BidirectionalStreamSpdyImpl() override;
48 
49   // BidirectionalStreamImpl implementation:
50   void Start(const BidirectionalStreamRequestInfo* request_info,
51              const NetLogWithSource& net_log,
52              bool send_request_headers_automatically,
53              BidirectionalStreamImpl::Delegate* delegate,
54              std::unique_ptr<base::OneShotTimer> timer,
55              const NetworkTrafficAnnotationTag& traffic_annotation) override;
56   void SendRequestHeaders() override;
57   int ReadData(IOBuffer* buf, int buf_len) override;
58   void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers,
59                  const std::vector<int>& lengths,
60                  bool end_stream) override;
61   NextProto GetProtocol() const override;
62   int64_t GetTotalReceivedBytes() const override;
63   int64_t GetTotalSentBytes() const override;
64   bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
65   void PopulateNetErrorDetails(NetErrorDetails* details) override;
66 
67   // SpdyStream::Delegate implementation:
68   void OnHeadersSent() override;
69   void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& headers) override;
70   void OnHeadersReceived(
71       const spdy::Http2HeaderBlock& response_headers) override;
72   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
73   void OnDataSent() override;
74   void OnTrailers(const spdy::Http2HeaderBlock& trailers) override;
75   void OnClose(int status) override;
76   bool CanGreaseFrameType() const override;
77   NetLogSource source_dependency() const override;
78 
79  private:
80   int SendRequestHeadersHelper();
81   void OnStreamInitialized(int rv);
82   // Notifies delegate of an error.
83   void NotifyError(int rv);
84   void ResetStream();
85   void ScheduleBufferedRead();
86   void DoBufferedRead();
87   bool ShouldWaitForMoreBufferedData() const;
88   // Handles the case where stream is closed when SendData()/SendvData() is
89   // called. Return true if stream is closed.
90   bool MaybeHandleStreamClosedInSendData();
91 
92   const base::WeakPtr<SpdySession> spdy_session_;
93   raw_ptr<const BidirectionalStreamRequestInfo> request_info_ = nullptr;
94   raw_ptr<BidirectionalStreamImpl::Delegate> delegate_ = nullptr;
95   std::unique_ptr<base::OneShotTimer> timer_;
96   SpdyStreamRequest stream_request_;
97   base::WeakPtr<SpdyStream> stream_;
98   const NetLogSource source_dependency_;
99 
100   NextProto negotiated_protocol_ = kProtoUnknown;
101 
102   // Buffers the data as it arrives asynchronously from the stream.
103   SpdyReadQueue read_data_queue_;
104   // Whether received more data has arrived since started waiting.
105   bool more_read_data_pending_ = false;
106   // User provided read buffer for ReadData() response.
107   scoped_refptr<IOBuffer> read_buffer_;
108   int read_buffer_len_ = 0;
109 
110   // Whether client has written the end of stream flag in request headers or
111   // in SendData()/SendvData().
112   bool written_end_of_stream_ = false;
113   // Whether a SendData() or SendvData() is pending.
114   bool write_pending_ = false;
115 
116   // Whether OnClose has been invoked.
117   bool stream_closed_ = false;
118   // Status reported in OnClose.
119   int closed_stream_status_ = ERR_FAILED;
120   // After |stream_| has been closed, this keeps track of the total number of
121   // bytes received over the network for |stream_| while it was open.
122   int64_t closed_stream_received_bytes_ = 0;
123   // After |stream_| has been closed, this keeps track of the total number of
124   // bytes sent over the network for |stream_| while it was open.
125   int64_t closed_stream_sent_bytes_ = 0;
126   // True if |stream_| has LoadTimingInfo when it is closed.
127   bool closed_has_load_timing_info_ = false;
128   // LoadTimingInfo populated when |stream_| is closed.
129   LoadTimingInfo closed_load_timing_info_;
130 
131   // This is the combined buffer of buffers passed in through SendvData.
132   // Keep a reference here so it is alive until OnDataSent is invoked.
133   scoped_refptr<IOBuffer> pending_combined_buffer_;
134 
135   base::WeakPtrFactory<BidirectionalStreamSpdyImpl> weak_factory_{this};
136 };
137 
138 }  // namespace net
139 
140 #endif  // NET_SPDY_BIDIRECTIONAL_STREAM_SPDY_IMPL_H_
141