xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/qpack/qpack_decoder_stream_sender.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2018 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 
5 #include "quiche/quic/core/qpack/qpack_decoder_stream_sender.h"
6 
7 #include <cstddef>
8 #include <limits>
9 #include <string>
10 #include <utility>
11 
12 #include "absl/strings/string_view.h"
13 #include "quiche/quic/core/qpack/qpack_instructions.h"
14 #include "quiche/quic/platform/api/quic_flag_utils.h"
15 #include "quiche/quic/platform/api/quic_logging.h"
16 
17 namespace quic {
18 
QpackDecoderStreamSender()19 QpackDecoderStreamSender::QpackDecoderStreamSender()
20     : delegate_(nullptr),
21       // None of the instructions sent by the QpackDecoderStreamSender
22       // are strings, so huffman encoding is not relevant.
23       instruction_encoder_(HuffmanEncoding::kEnabled) {}
24 
SendInsertCountIncrement(uint64_t increment)25 void QpackDecoderStreamSender::SendInsertCountIncrement(uint64_t increment) {
26   instruction_encoder_.Encode(
27       QpackInstructionWithValues::InsertCountIncrement(increment), &buffer_);
28 }
29 
SendHeaderAcknowledgement(QuicStreamId stream_id)30 void QpackDecoderStreamSender::SendHeaderAcknowledgement(
31     QuicStreamId stream_id) {
32   instruction_encoder_.Encode(
33       QpackInstructionWithValues::HeaderAcknowledgement(stream_id), &buffer_);
34 }
35 
SendStreamCancellation(QuicStreamId stream_id)36 void QpackDecoderStreamSender::SendStreamCancellation(QuicStreamId stream_id) {
37   instruction_encoder_.Encode(
38       QpackInstructionWithValues::StreamCancellation(stream_id), &buffer_);
39 }
40 
Flush()41 void QpackDecoderStreamSender::Flush() {
42   if (buffer_.empty() || delegate_ == nullptr) {
43     return;
44   }
45   if (GetQuicRestartFlag(quic_opport_bundle_qpack_decoder_data4)) {
46     QUIC_RESTART_FLAG_COUNT_N(quic_opport_bundle_qpack_decoder_data4, 3, 4);
47     // Swap buffer_ before calling WriteStreamData, which might result in a
48     // reentrant call to `Flush()`.
49     std::string copy;
50     std::swap(copy, buffer_);
51     delegate_->WriteStreamData(copy);
52     return;
53   }
54   delegate_->WriteStreamData(buffer_);
55   buffer_.clear();
56 }
57 
58 }  // namespace quic
59