bidirectional_stream_impl.h 7.5 KB

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