File: replaying_bytes_consumer.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (92 lines) | stat: -rw-r--r-- 2,924 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_TESTING_REPLAYING_BYTES_CONSUMER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_TESTING_REPLAYING_BYTES_CONSUMER_H_

#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/loader/fetch/bytes_consumer.h"
#include "third_party/blink/renderer/platform/wtf/deque.h"

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace blink {

// ReplayingBytesConsumer stores commands via |add| and replays the stored
// commands when read.
class ReplayingBytesConsumer final : public BytesConsumer {
 public:
  class Command final {
    DISALLOW_NEW();

   public:
    enum Name {
      kData,
      kDone,
      kError,
      kWait,
      kDataAndDone,
    };

    explicit Command(Name name) : name_(name) {}
    Command(Name name, const Vector<char>& body) : name_(name), body_(body) {}
    Command(Name name, base::span<const char> body) : name_(name) {
      body_.AppendSpan(body);
    }
    template <size_t N>
    Command(Name name, const char (&body)[N])
        : Command(name, base::span(body).template first<N - 1>()) {}
    Name GetName() const { return name_; }
    const Vector<char>& Body() const { return body_; }

   private:
    const Name name_;
    Vector<char> body_;
  };

  explicit ReplayingBytesConsumer(scoped_refptr<base::SingleThreadTaskRunner>);
  ~ReplayingBytesConsumer() override;

  // Add a command to this handle. This function must be called BEFORE
  // any BytesConsumer methods are called.
  void Add(const Command& command) { commands_.push_back(command); }

  Result BeginRead(base::span<const char>& buffer) override;
  Result EndRead(size_t read_size) override;

  void SetClient(Client*) override;
  void ClearClient() override;
  void Cancel() override;
  PublicState GetPublicState() const override;
  Error GetError() const override;
  String DebugName() const override { return "ReplayingBytesConsumer"; }

  bool IsCancelled() const { return is_cancelled_; }
  bool IsCommandsEmpty() { return commands_.empty(); }
  void TriggerOnStateChange() { client_->OnStateChange(); }

  void Trace(Visitor*) const override;

 private:
  void NotifyAsReadable(int notification_token);
  void Close();
  void MakeErrored(const Error&);

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  Member<BytesConsumer::Client> client_;
  InternalState state_ = InternalState::kWaiting;
  Deque<Command> commands_;
  size_t offset_ = 0;
  BytesConsumer::Error error_;
  int notification_token_ = 0;
  bool is_cancelled_ = false;
  bool is_in_two_phase_read_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_TESTING_REPLAYING_BYTES_CONSUMER_H_