File: devtools_durable_msg.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/devtools_durable_msg.h"

#include "base/functional/callback_helpers.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "net/base/io_buffer.h"
#include "net/filter/filter_source_stream.h"
#include "net/filter/source_stream.h"
#include "net/filter/source_stream_type.h"

namespace network {

class DurableMessageEncodedSourceStream : public net::SourceStream {
 public:
  explicit DurableMessageEncodedSourceStream(
      base::span<const uint8_t> encoded_bytes)
      : SourceStream(net::SourceStreamType::kNone),
        encoded_bytes_(encoded_bytes) {}

  int Read(net::IOBuffer* dest_buffer,
           int buffer_size,
           net::CompletionOnceCallback callback) override {
    size_t consume = std::min(base::checked_cast<size_t>(buffer_size),
                              encoded_bytes_.size());
    if (consume == 0) {
      return 0;
    }

    dest_buffer->span().copy_prefix_from(encoded_bytes_.take_first(consume));
    return base::checked_cast<int>(consume);
  }

  std::string Description() const override {
    return "DurableMessageEncodedSourceStream";
  }

  bool MayHaveMoreBytes() const override { return !encoded_bytes_.empty(); }

 private:
  base::raw_span<const uint8_t> encoded_bytes_;
};

DevtoolsDurableMessage::DevtoolsDurableMessage(
    std::string request_id,
    DevtoolsDurableMessageAccountingDelegate& accounting_delegate)
    : request_id_(std::move(request_id)),
      accounting_delegate_(accounting_delegate) {}

DevtoolsDurableMessage::~DevtoolsDurableMessage() {
  accounting_delegate_->WillRemoveBytes(*this);
}

void DevtoolsDurableMessage::AddBytes(base::span<const uint8_t> bytes,
                                      size_t encoded_byte_size) {
  CHECK(!is_complete_);
  base::WeakPtr<DevtoolsDurableMessage> self = GetWeakPtr();
  accounting_delegate_->WillAddBytes(*this, encoded_byte_size);
  if (!self) {
    return;
  }

  bytes_.insert(bytes_.end(), bytes.begin(), bytes.end());
  encoded_byte_size_ += encoded_byte_size;
}

mojo_base::BigBuffer DevtoolsDurableMessage::Retrieve() const {
  CHECK(is_complete_);

  if (client_decoding_types_.empty()) {
    return mojo_base::BigBuffer(bytes_);
  }

  // Stored data needs to be decoded before shipping out.
  std::unique_ptr<DurableMessageEncodedSourceStream> encoded_stream =
      std::make_unique<DurableMessageEncodedSourceStream>(bytes_);
  std::unique_ptr<net::SourceStream> decoding_stream =
      net::FilterSourceStream::CreateDecodingSourceStream(
          std::move(encoded_stream), client_decoding_types_);
  scoped_refptr<net::GrowableIOBuffer> decode_buffer =
      base::MakeRefCounted<net::GrowableIOBuffer>();
  // Set to encoded size initially.
  decode_buffer->SetCapacity(encoded_byte_size_);
  while (decoding_stream->MayHaveMoreBytes()) {
    if (decode_buffer->RemainingCapacity() == 0) {
      decode_buffer->SetCapacity(decode_buffer->capacity() * 2);
    }
    int result = decoding_stream->Read(decode_buffer.get(),
                                       decode_buffer->RemainingCapacity(),
                                       base::DoNothing());
    if (result > 0) {
      // Update the offset of the `decode_buffer` to reflect the new data.
      decode_buffer->DidConsume(result);
    } else {
      // If someone makes a FilterSourceStream that decompresses asynchronously
      // (on another thread, for example), then crash noisily.
      CHECK_NE(result, net::ERR_IO_PENDING);
    }
  }

  return mojo_base::BigBuffer(decode_buffer->span_before_offset());
}

void DevtoolsDurableMessage::MarkComplete() {
  is_complete_ = true;
}

}  // namespace network