bidirectional_stream_quic_impl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2016 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_QUIC_BIDIRECTIONAL_STREAM_QUIC_IMPL_H_
  5. #define NET_QUIC_BIDIRECTIONAL_STREAM_QUIC_IMPL_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "net/base/load_timing_info.h"
  12. #include "net/base/net_export.h"
  13. #include "net/http/bidirectional_stream_impl.h"
  14. #include "net/quic/quic_chromium_client_session.h"
  15. #include "net/quic/quic_chromium_client_stream.h"
  16. #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
  17. namespace base {
  18. class OneShotTimer;
  19. } // namespace base
  20. namespace net {
  21. struct BidirectionalStreamRequestInfo;
  22. class IOBuffer;
  23. class NET_EXPORT_PRIVATE BidirectionalStreamQuicImpl
  24. : public BidirectionalStreamImpl {
  25. public:
  26. explicit BidirectionalStreamQuicImpl(
  27. std::unique_ptr<QuicChromiumClientSession::Handle> session);
  28. BidirectionalStreamQuicImpl(const BidirectionalStreamQuicImpl&) = delete;
  29. BidirectionalStreamQuicImpl& operator=(const BidirectionalStreamQuicImpl&) =
  30. delete;
  31. ~BidirectionalStreamQuicImpl() override;
  32. // BidirectionalStreamImpl implementation:
  33. void Start(const BidirectionalStreamRequestInfo* request_info,
  34. const NetLogWithSource& net_log,
  35. bool send_request_headers_automatically,
  36. BidirectionalStreamImpl::Delegate* delegate,
  37. std::unique_ptr<base::OneShotTimer> timer,
  38. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  39. void SendRequestHeaders() override;
  40. int ReadData(IOBuffer* buffer, int buffer_len) override;
  41. void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers,
  42. const std::vector<int>& lengths,
  43. bool end_stream) override;
  44. NextProto GetProtocol() const override;
  45. int64_t GetTotalReceivedBytes() const override;
  46. int64_t GetTotalSentBytes() const override;
  47. bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
  48. void PopulateNetErrorDetails(NetErrorDetails* details) override;
  49. private:
  50. int WriteHeaders();
  51. void OnStreamReady(int rv);
  52. void OnSendDataComplete(int rv);
  53. void ReadInitialHeaders();
  54. void OnReadInitialHeadersComplete(int rv);
  55. void ReadTrailingHeaders();
  56. void OnReadTrailingHeadersComplete(int rv);
  57. void OnReadDataComplete(int rv);
  58. // Notifies the delegate of an error, clears |stream_| and |delegate_|,
  59. // and cancels any pending callbacks.
  60. void NotifyError(int error);
  61. // Notifies the delegate of an error, clears |stream_| and |delegate_|,
  62. // and cancels any pending callbacks. If |notify_delegate_later| is true
  63. // then the delegate will be notified asynchronously via a posted task,
  64. // otherwise the notification will be synchronous.
  65. void NotifyErrorImpl(int error, bool notify_delegate_later);
  66. // Notifies the delegate that the stream is ready.
  67. void NotifyStreamReady();
  68. // Resets the stream and ensures that |delegate_| won't be called back.
  69. void ResetStream();
  70. // Invokes OnFailure(error) on |delegate|.
  71. void NotifyFailure(BidirectionalStreamImpl::Delegate* delegate, int error);
  72. const std::unique_ptr<QuicChromiumClientSession::Handle> session_;
  73. std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
  74. raw_ptr<const BidirectionalStreamRequestInfo> request_info_ = nullptr;
  75. raw_ptr<BidirectionalStreamImpl::Delegate> delegate_ = nullptr;
  76. // Saves the response status if the stream is explicitly closed via OnError
  77. // or OnClose with an error. Once all buffered data has been returned, this
  78. // will be used as the final response.
  79. int response_status_ = OK;
  80. // The protocol that is negotiated.
  81. NextProto negotiated_protocol_ = kProtoUnknown;
  82. // Connect timing information for this stream. Populated when headers are
  83. // received.
  84. LoadTimingInfo::ConnectTiming connect_timing_;
  85. spdy::Http2HeaderBlock initial_headers_;
  86. spdy::Http2HeaderBlock trailing_headers_;
  87. // User provided read buffer for ReadData() response.
  88. scoped_refptr<IOBuffer> read_buffer_;
  89. int read_buffer_len_ = 0;
  90. // Number of bytes received by the headers stream on behalf of this stream.
  91. int64_t headers_bytes_received_ = 0;
  92. // Number of bytes sent by the headers stream on behalf of this stream.
  93. int64_t headers_bytes_sent_ = 0;
  94. // After |stream_| has been closed, this keeps track of the total number of
  95. // bytes received over the network for |stream_| while it was open.
  96. int64_t closed_stream_received_bytes_ = 0;
  97. // After |stream_| has been closed, this keeps track of the total number of
  98. // bytes sent over the network for |stream_| while it was open.
  99. int64_t closed_stream_sent_bytes_ = 0;
  100. // True if the stream is the first stream negotiated on the session. Set when
  101. // the stream was closed. If |stream_| is failed to be created, this takes on
  102. // the default value of false.
  103. bool closed_is_first_stream_ = false;
  104. // Indicates whether initial headers have been sent.
  105. bool has_sent_headers_ = false;
  106. // Whether to automatically send request headers when stream is negotiated.
  107. // If false, headers will not be sent until SendRequestHeaders() is called or
  108. // until next SendData/SendvData, during which QUIC will try to combine header
  109. // frame with data frame in the same packet if possible.
  110. bool send_request_headers_automatically_ = true;
  111. // True when callbacks to the delegate may be invoked synchronously.
  112. bool may_invoke_callbacks_ = true;
  113. base::WeakPtrFactory<BidirectionalStreamQuicImpl> weak_factory_{this};
  114. };
  115. } // namespace net
  116. #endif // NET_QUIC_BIDIRECTIONAL_STREAM_QUIC_IMPL_H_