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