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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// 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.
#include "third_party/blink/renderer/platform/loader/testing/replaying_bytes_consumer.h"
#include "base/task/single_thread_task_runner.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink {
ReplayingBytesConsumer::ReplayingBytesConsumer(
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: task_runner_(std::move(task_runner)) {}
ReplayingBytesConsumer::~ReplayingBytesConsumer() {}
BytesConsumer::Result ReplayingBytesConsumer::BeginRead(
base::span<const char>& buffer) {
DCHECK(!is_in_two_phase_read_);
++notification_token_;
if (commands_.empty()) {
switch (state_) {
case BytesConsumer::InternalState::kWaiting:
return Result::kShouldWait;
case BytesConsumer::InternalState::kClosed:
return Result::kDone;
case BytesConsumer::InternalState::kErrored:
return Result::kError;
}
}
const Command& command = commands_[0];
switch (command.GetName()) {
case Command::kDataAndDone:
case Command::kData:
DCHECK_LE(offset_, command.Body().size());
buffer = base::span(command.Body()).subspan(offset_);
is_in_two_phase_read_ = true;
return Result::kOk;
case Command::kDone:
commands_.pop_front();
Close();
return Result::kDone;
case Command::kError: {
Error e(String::FromUTF8(base::as_byte_span(command.Body())));
commands_.pop_front();
MakeErrored(std::move(e));
return Result::kError;
}
case Command::kWait:
commands_.pop_front();
state_ = InternalState::kWaiting;
task_runner_->PostTask(
FROM_HERE, WTF::BindOnce(&ReplayingBytesConsumer::NotifyAsReadable,
WrapPersistent(this), notification_token_));
return Result::kShouldWait;
}
NOTREACHED();
}
BytesConsumer::Result ReplayingBytesConsumer::EndRead(size_t read) {
DCHECK(is_in_two_phase_read_);
DCHECK(!commands_.empty());
is_in_two_phase_read_ = false;
const Command& command = commands_[0];
const auto name = command.GetName();
DCHECK(name == Command::kData || name == Command::kDataAndDone);
offset_ += read;
DCHECK_LE(offset_, command.Body().size());
if (offset_ < command.Body().size())
return Result::kOk;
offset_ = 0;
commands_.pop_front();
if (name == Command::kData)
return Result::kOk;
Close();
return Result::kDone;
}
void ReplayingBytesConsumer::SetClient(Client* client) {
DCHECK(!client_);
DCHECK(client);
client_ = client;
++notification_token_;
}
void ReplayingBytesConsumer::ClearClient() {
DCHECK(client_);
client_ = nullptr;
++notification_token_;
}
void ReplayingBytesConsumer::Cancel() {
Close();
is_cancelled_ = true;
}
BytesConsumer::PublicState ReplayingBytesConsumer::GetPublicState() const {
return GetPublicStateFromInternalState(state_);
}
BytesConsumer::Error ReplayingBytesConsumer::GetError() const {
return error_;
}
void ReplayingBytesConsumer::NotifyAsReadable(int notification_token) {
if (notification_token_ != notification_token) {
// The notification is cancelled.
return;
}
DCHECK(client_);
DCHECK_NE(InternalState::kClosed, state_);
DCHECK_NE(InternalState::kErrored, state_);
client_->OnStateChange();
}
void ReplayingBytesConsumer::Close() {
commands_.clear();
offset_ = 0;
state_ = InternalState::kClosed;
++notification_token_;
}
void ReplayingBytesConsumer::MakeErrored(const Error& e) {
commands_.clear();
offset_ = 0;
error_ = e;
state_ = InternalState::kErrored;
++notification_token_;
}
void ReplayingBytesConsumer::Trace(Visitor* visitor) const {
visitor->Trace(client_);
BytesConsumer::Trace(visitor);
}
} // namespace blink
|