#include "net/disk_cache/simple/simple_entry_impl.h"
#include <algorithm>
#include <cstring>
#include <limits>
#include <optional>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/trace_event.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/prioritized_task_runner.h"
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "net/disk_cache/net_log_parameters.h"
#include "net/disk_cache/simple/simple_backend_impl.h"
#include "net/disk_cache/simple/simple_histogram_enums.h"
#include "net/disk_cache/simple/simple_histogram_macros.h"
#include "net/disk_cache/simple/simple_index.h"
#include "net/disk_cache/simple/simple_net_log_parameters.h"
#include "net/disk_cache/simple/simple_synchronous_entry.h"
#include "net/disk_cache/simple/simple_util.h"
#include "net/log/net_log.h"
#include "net/log/net_log_source_type.h"
#include "third_party/zlib/zlib.h"
namespace disk_cache {
namespace {
constexpr int64_t kMaxSparseDataSizeDivisor = 10;
constexpr int64_t kStream1SizeLimit = ((1LL << 38) - (1LL << 32)) / 2 -
(1LL << 32) - sizeof(SimpleFileHeader) -
sizeof(SimpleFileEOF);
OpenEntryIndexEnum ComputeIndexState(SimpleBackendImpl* backend,
uint64_t entry_hash) {
if (!backend->index()->initialized())
return INDEX_NOEXIST;
if (backend->index()->Has(entry_hash))
return INDEX_HIT;
return INDEX_MISS;
}
void RecordOpenEntryIndexState(net::CacheType cache_type,
OpenEntryIndexEnum state) {
SIMPLE_CACHE_UMA(ENUMERATION, "OpenEntryIndexState", cache_type, state,
INDEX_MAX);
}
void RecordHeaderSize(net::CacheType cache_type, int size) {
SIMPLE_CACHE_UMA(COUNTS_10000, "HeaderSize", cache_type, size);
}
void InvokeCallbackIfBackendIsAlive(
const base::WeakPtr<SimpleBackendImpl>& backend,
net::CompletionOnceCallback completion_callback,
int result) {
DCHECK(!completion_callback.is_null());
if (!backend.get())
return;
std::move(completion_callback).Run(result);
}
void InvokeEntryResultCallbackIfBackendIsAlive(
const base::WeakPtr<SimpleBackendImpl>& backend,
EntryResultCallback completion_callback,
EntryResult result) {
DCHECK(!completion_callback.is_null());
if (!backend.get())
return;
std::move(completion_callback).Run(std::move(result));
}
int PostToCallbackIfNeeded(bool sync_possible,
net::CompletionOnceCallback callback,
int rv) {
if (!sync_possible && !callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), rv));
return net::ERR_IO_PENDING;
} else {
return rv;
}
}
}
using base::OnceClosure;
using base::FilePath;
using base::Time;
using base::TaskRunner;
class SimpleEntryImpl::ScopedOperationRunner {
public:
explicit ScopedOperationRunner(SimpleEntryImpl* entry) : entry_(entry) {
}
~ScopedOperationRunner() {
entry_->RunNextOperationIfNeeded();
}
private:
const raw_ptr<SimpleEntryImpl> entry_;
};
SimpleEntryImpl::ActiveEntryProxy::~ActiveEntryProxy() = default;
SimpleEntryImpl::SimpleEntryImpl(
net::CacheType cache_type,
const FilePath& path,
scoped_refptr<BackendCleanupTracker> cleanup_tracker,
const uint64_t entry_hash,
OperationsMode operations_mode,
SimpleBackendImpl* backend,
SimpleFileTracker* file_tracker,
scoped_refptr<BackendFileOperationsFactory> file_operations_factory,
net::NetLog* net_log,
uint32_t entry_priority)
: cleanup_tracker_(std::move(cleanup_tracker)),
backend_(backend->AsWeakPtr()),
file_tracker_(file_tracker),
file_operations_factory_(std::move(file_operations_factory)),
cache_type_(cache_type),
path_(path),
entry_hash_(entry_hash),
use_optimistic_operations_(operations_mode == OPTIMISTIC_OPERATIONS),
last_used_(Time::Now()),
prioritized_task_runner_(backend_->prioritized_task_runner()),
net_log_(
net::NetLogWithSource::Make(net_log,
net::NetLogSourceType::DISK_CACHE_ENTRY)),
stream_0_data_(base::MakeRefCounted<net::GrowableIOBuffer>()),
entry_priority_(entry_priority) {
static_assert(std::tuple_size<decltype(data_size_)>() ==
std::tuple_size<decltype(crc32s_end_offset_)>(),
"arrays should be the same size");
static_assert(std::tuple_size<decltype(data_size_)>() ==
std::tuple_size<decltype(crc32s_)>(),
"arrays should be the same size");
static_assert(std::tuple_size<decltype(data_size_)>() ==
std::tuple_size<decltype(have_written_)>(),
"arrays should be the same size");
ResetEntry();
NetLogSimpleEntryConstruction(net_log_,
net::NetLogEventType::SIMPLE_CACHE_ENTRY,
net::NetLogEventPhase::BEGIN, this);
}
void SimpleEntryImpl::SetActiveEntryProxy(
std::unique_ptr<ActiveEntryProxy> active_entry_proxy) {
DCHECK(!active_entry_proxy_);
active_entry_proxy_ = std::move(active_entry_proxy);
}
EntryResult SimpleEntryImpl::OpenEntry(EntryResultCallback callback) {
DCHECK(backend_.get());
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_CALL);
OpenEntryIndexEnum index_state =
ComputeIndexState(backend_.get(), entry_hash_);
RecordOpenEntryIndexState(cache_type_, index_state);
if (index_state == INDEX_MISS) {
net_log_.AddEventWithNetErrorCode(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_END, net::ERR_FAILED);
return EntryResult::MakeError(net::ERR_FAILED);
}
pending_operations_.push(SimpleEntryOperation::OpenOperation(
this, SimpleEntryOperation::ENTRY_NEEDS_CALLBACK, std::move(callback)));
RunNextOperationIfNeeded();
return EntryResult::MakeError(net::ERR_IO_PENDING);
}
EntryResult SimpleEntryImpl::CreateEntry(EntryResultCallback callback) {
DCHECK(backend_.get());
DCHECK_EQ(entry_hash_, simple_util::GetEntryHashKey(*key_));
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_CREATE_CALL);
EntryResult result = EntryResult::MakeError(net::ERR_IO_PENDING);
if (use_optimistic_operations_ &&
state_ == STATE_UNINITIALIZED && pending_operations_.size() == 0) {
net_log_.AddEvent(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_CREATE_OPTIMISTIC);
ReturnEntryToCaller();
result = EntryResult::MakeCreated(this);
pending_operations_.push(SimpleEntryOperation::CreateOperation(
this, SimpleEntryOperation::ENTRY_ALREADY_RETURNED,
EntryResultCallback()));
if (optimistic_create_pending_doom_state_ != CREATE_NORMAL) {
CHECK_EQ(CREATE_OPTIMISTIC_PENDING_DOOM,
optimistic_create_pending_doom_state_);
state_ = STATE_IO_PENDING;
}
} else {
pending_operations_.push(SimpleEntryOperation::CreateOperation(
this, SimpleEntryOperation::ENTRY_NEEDS_CALLBACK, std::move(callback)));
}
backend_->index()->Insert(entry_hash_);
RunNextOperationIfNeeded();
return result;
}
EntryResult SimpleEntryImpl::OpenOrCreateEntry(EntryResultCallback callback) {
DCHECK(backend_.get());
DCHECK_EQ(entry_hash_, simple_util::GetEntryHashKey(*key_));
net_log_.AddEvent(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_OR_CREATE_CALL);
OpenEntryIndexEnum index_state =
ComputeIndexState(backend_.get(), entry_hash_);
RecordOpenEntryIndexState(cache_type_, index_state);
TRACE_EVENT("disk_cache", "SimpleEntryImpl::OpenOrCreateEntry", "index_state",
index_state);
EntryResult result = EntryResult::MakeError(net::ERR_IO_PENDING);
if (index_state == INDEX_MISS && use_optimistic_operations_ &&
state_ == STATE_UNINITIALIZED && pending_operations_.size() == 0) {
net_log_.AddEvent(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_CREATE_OPTIMISTIC);
ReturnEntryToCaller();
result = EntryResult::MakeCreated(this);
pending_operations_.push(SimpleEntryOperation::OpenOrCreateOperation(
this, index_state, SimpleEntryOperation::ENTRY_ALREADY_RETURNED,
EntryResultCallback()));
CHECK_EQ(CREATE_NORMAL, optimistic_create_pending_doom_state_);
} else {
pending_operations_.push(SimpleEntryOperation::OpenOrCreateOperation(
this, index_state, SimpleEntryOperation::ENTRY_NEEDS_CALLBACK,
std::move(callback)));
}
backend_->index()->Insert(entry_hash_);
RunNextOperationIfNeeded();
return result;
}
net::Error SimpleEntryImpl::DoomEntry(net::CompletionOnceCallback callback) {
if (doom_state_ != DOOM_NONE)
return net::OK;
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_DOOM_CALL);
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_DOOM_BEGIN);
MarkAsDoomed(DOOM_QUEUED);
if (backend_.get()) {
if (optimistic_create_pending_doom_state_ == CREATE_NORMAL) {
post_doom_waiting_ = backend_->OnDoomStart(entry_hash_);
} else {
CHECK_EQ(STATE_IO_PENDING, state_);
CHECK_EQ(CREATE_OPTIMISTIC_PENDING_DOOM,
optimistic_create_pending_doom_state_);
optimistic_create_pending_doom_state_ =
CREATE_OPTIMISTIC_PENDING_DOOM_FOLLOWED_BY_DOOM;
}
}
pending_operations_.push(
SimpleEntryOperation::DoomOperation(this, std::move(callback)));
RunNextOperationIfNeeded();
return net::ERR_IO_PENDING;
}
void SimpleEntryImpl::SetCreatePendingDoom() {
CHECK_EQ(CREATE_NORMAL, optimistic_create_pending_doom_state_);
optimistic_create_pending_doom_state_ = CREATE_OPTIMISTIC_PENDING_DOOM;
}
void SimpleEntryImpl::NotifyDoomBeforeCreateComplete() {
CHECK_EQ(STATE_IO_PENDING, state_);
CHECK_NE(CREATE_NORMAL, optimistic_create_pending_doom_state_);
if (backend_.get() && optimistic_create_pending_doom_state_ ==
CREATE_OPTIMISTIC_PENDING_DOOM_FOLLOWED_BY_DOOM)
post_doom_waiting_ = backend_->OnDoomStart(entry_hash_);
state_ = STATE_UNINITIALIZED;
optimistic_create_pending_doom_state_ = CREATE_NORMAL;
RunNextOperationIfNeeded();
}
void SimpleEntryImpl::SetKey(const std::string& key) {
key_ = key;
net_log_.AddEventWithStringParams(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_SET_KEY, "key", key);
}
void SimpleEntryImpl::Doom() {
DoomEntry(CompletionOnceCallback());
}
void SimpleEntryImpl::Close() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK_LT(0, open_count_);
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_CLOSE_CALL);
if (--open_count_ > 0) {
DCHECK(!HasOneRef());
Release();
return;
}
pending_operations_.push(SimpleEntryOperation::CloseOperation(this));
DCHECK(!HasOneRef());
Release();
RunNextOperationIfNeeded();
}
std::string SimpleEntryImpl::GetKey() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return *key_;
}
Time SimpleEntryImpl::GetLastUsed() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(cache_type_ != net::APP_CACHE);
return last_used_;
}
int64_t SimpleEntryImpl::GetDataSize(int stream_index) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_LE(0, data_size_[stream_index]);
return data_size_[stream_index];
}
int SimpleEntryImpl::ReadData(int stream_index,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (net_log_.IsCapturing()) {
NetLogReadWriteData(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_CALL,
net::NetLogEventPhase::NONE, stream_index, offset, buf_len, false);
}
if (stream_index < 0 || stream_index >= kSimpleEntryStreamCount ||
offset < 0 || buf_len < 0) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_END,
net::NetLogEventPhase::NONE, net::ERR_INVALID_ARGUMENT);
}
return net::ERR_INVALID_ARGUMENT;
}
bool alone_in_queue =
pending_operations_.size() == 0 && state_ == STATE_READY;
if (alone_in_queue) {
return ReadDataInternal( true, stream_index, offset,
buf, buf_len, std::move(callback));
}
pending_operations_.push(SimpleEntryOperation::ReadOperation(
this, stream_index, offset, buf_len, buf, std::move(callback)));
RunNextOperationIfNeeded();
return net::ERR_IO_PENDING;
}
int SimpleEntryImpl::WriteData(int stream_index,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
bool truncate) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (net_log_.IsCapturing()) {
NetLogReadWriteData(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_CALL,
net::NetLogEventPhase::NONE, stream_index, offset, buf_len, truncate);
}
if (stream_index < 0 || stream_index >= kSimpleEntryStreamCount ||
offset < 0 || buf_len < 0 || (!buf && buf_len != 0)) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_END,
net::NetLogEventPhase::NONE, net::ERR_INVALID_ARGUMENT);
}
return net::ERR_INVALID_ARGUMENT;
}
int64_t end_offset;
if (!base::CheckAdd(offset, buf_len).AssignIfValid(&end_offset) ||
(backend_.get() && end_offset > backend_->MaxFileSize()) ||
end_offset > kStream1SizeLimit) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_END,
net::NetLogEventPhase::NONE, net::ERR_FAILED);
}
return net::ERR_FAILED;
}
if (stream_index == 0 && end_offset > std::numeric_limits<int32_t>::max()) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_END,
net::NetLogEventPhase::NONE, net::ERR_INVALID_ARGUMENT);
}
return net::ERR_INVALID_ARGUMENT;
}
ScopedOperationRunner operation_runner(this);
if (stream_index == 0 && state_ == STATE_READY &&
pending_operations_.size() == 0) {
state_ = STATE_IO_PENDING;
SetStream0Data(buf, base::checked_cast<int32_t>(offset), buf_len, truncate);
state_ = STATE_READY;
return buf_len;
}
const bool optimistic =
(use_optimistic_operations_ && state_ == STATE_READY &&
pending_operations_.size() == 0);
CompletionOnceCallback op_callback;
scoped_refptr<net::IOBuffer> op_buf;
int ret_value = net::ERR_FAILED;
if (!optimistic) {
op_buf = buf;
op_callback = std::move(callback);
ret_value = net::ERR_IO_PENDING;
} else {
if (buf) {
op_buf = base::MakeRefCounted<net::IOBufferWithSize>(buf_len);
op_buf->span().copy_from(buf->first(static_cast<unsigned>(buf_len)));
}
op_callback = CompletionOnceCallback();
ret_value = buf_len;
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_OPTIMISTIC,
net::NetLogEventPhase::NONE, buf_len);
}
}
pending_operations_.push(SimpleEntryOperation::WriteOperation(
this, stream_index, offset, buf_len, op_buf.get(), truncate, optimistic,
std::move(op_callback)));
return ret_value;
}
int SimpleEntryImpl::ReadSparseData(int64_t offset,
net::IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (net_log_.IsCapturing()) {
NetLogSparseOperation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_SPARSE_CALL,
net::NetLogEventPhase::NONE, offset, buf_len);
}
if (offset < 0 || buf_len < 0) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_SPARSE_END,
net::NetLogEventPhase::NONE, net::ERR_INVALID_ARGUMENT);
}
return net::ERR_INVALID_ARGUMENT;
}
size_t length = std::min(static_cast<int64_t>(buf_len),
std::numeric_limits<int64_t>::max() - offset);
ScopedOperationRunner operation_runner(this);
pending_operations_.push(SimpleEntryOperation::ReadSparseOperation(
this, static_cast<uint64_t>(offset), length, buf, std::move(callback)));
return net::ERR_IO_PENDING;
}
int SimpleEntryImpl::WriteSparseData(int64_t offset,
net::IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (net_log_.IsCapturing()) {
NetLogSparseOperation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_SPARSE_CALL,
net::NetLogEventPhase::NONE, offset, buf_len);
}
if (offset < 0 || buf_len < 0 || !base::CheckAdd(offset, buf_len).IsValid()) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_SPARSE_END,
net::NetLogEventPhase::NONE, net::ERR_INVALID_ARGUMENT);
}
return net::ERR_INVALID_ARGUMENT;
}
ScopedOperationRunner operation_runner(this);
pending_operations_.push(SimpleEntryOperation::WriteSparseOperation(
this, static_cast<uint64_t>(offset), static_cast<size_t>(buf_len), buf,
std::move(callback)));
return net::ERR_IO_PENDING;
}
RangeResult SimpleEntryImpl::GetAvailableRange(int64_t offset,
int len,
RangeResultCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (offset < 0 || len < 0)
return RangeResult(net::ERR_INVALID_ARGUMENT);
size_t length = std::min(static_cast<int64_t>(len),
std::numeric_limits<int64_t>::max() - offset);
ScopedOperationRunner operation_runner(this);
pending_operations_.push(SimpleEntryOperation::GetAvailableRangeOperation(
this, static_cast<uint64_t>(offset), length, std::move(callback)));
return RangeResult(net::ERR_IO_PENDING);
}
bool SimpleEntryImpl::CouldBeSparse() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return true;
}
void SimpleEntryImpl::CancelSparseIO() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
net::Error SimpleEntryImpl::ReadyForSparseIO(CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return net::OK;
}
void SimpleEntryImpl::SetEntryInMemoryData(uint8_t data) {
if (backend_) {
backend_->index()->SetEntryInMemoryData(entry_hash_, data);
}
}
void SimpleEntryImpl::SetLastUsedTimeForTest(base::Time time) {
last_used_ = time;
backend_->index()->SetLastUsedTimeForTest(entry_hash_, time);
}
void SimpleEntryImpl::SetPriority(uint32_t entry_priority) {
entry_priority_ = entry_priority;
}
SimpleEntryImpl::~SimpleEntryImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(0U, pending_operations_.size());
net_log_.EndEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY);
}
void SimpleEntryImpl::PostClientCallback(net::CompletionOnceCallback callback,
int result) {
if (callback.is_null())
return;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&InvokeCallbackIfBackendIsAlive, backend_,
std::move(callback), result));
}
void SimpleEntryImpl::PostClientCallback(EntryResultCallback callback,
EntryResult result) {
if (callback.is_null())
return;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&InvokeEntryResultCallbackIfBackendIsAlive, backend_,
std::move(callback), std::move(result)));
}
void SimpleEntryImpl::ResetEntry() {
state_ = doom_state_ == DOOM_COMPLETED ? STATE_FAILURE : STATE_UNINITIALIZED;
std::ranges::fill(crc32s_end_offset_, 0);
std::ranges::fill(crc32s_, 0);
std::ranges::fill(have_written_, 0);
std::ranges::fill(data_size_, 0);
}
void SimpleEntryImpl::ReturnEntryToCaller() {
DCHECK(backend_);
++open_count_;
AddRef();
}
void SimpleEntryImpl::ReturnEntryToCallerAsync(bool is_open,
EntryResultCallback callback) {
DCHECK(!callback.is_null());
++open_count_;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&SimpleEntryImpl::FinishReturnEntryToCallerAsync, this,
is_open, std::move(callback)));
}
void SimpleEntryImpl::FinishReturnEntryToCallerAsync(
bool is_open,
EntryResultCallback callback) {
AddRef();
if (!backend_.get()) {
Close();
return;
}
std::move(callback).Run(is_open ? EntryResult::MakeOpened(this)
: EntryResult::MakeCreated(this));
}
void SimpleEntryImpl::MarkAsDoomed(DoomState new_state) {
DCHECK_NE(DOOM_NONE, new_state);
doom_state_ = new_state;
if (!backend_.get())
return;
backend_->index()->Remove(entry_hash_);
active_entry_proxy_.reset();
}
void SimpleEntryImpl::RunNextOperationIfNeeded() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) {
SimpleEntryOperation operation = std::move(pending_operations_.front());
pending_operations_.pop();
switch (operation.type()) {
case SimpleEntryOperation::TYPE_OPEN:
OpenEntryInternal(operation.entry_result_state(),
operation.ReleaseEntryResultCallback());
break;
case SimpleEntryOperation::TYPE_CREATE:
CreateEntryInternal(operation.entry_result_state(),
operation.ReleaseEntryResultCallback());
break;
case SimpleEntryOperation::TYPE_OPEN_OR_CREATE:
OpenOrCreateEntryInternal(operation.index_state(),
operation.entry_result_state(),
operation.ReleaseEntryResultCallback());
break;
case SimpleEntryOperation::TYPE_CLOSE:
CloseInternal();
break;
case SimpleEntryOperation::TYPE_READ:
ReadDataInternal( false, operation.index(),
operation.offset(), operation.buf(),
operation.length(), operation.ReleaseCallback());
break;
case SimpleEntryOperation::TYPE_WRITE:
WriteDataInternal(operation.index(), operation.offset(),
operation.buf(), operation.length(),
operation.ReleaseCallback(), operation.truncate());
break;
case SimpleEntryOperation::TYPE_READ_SPARSE:
ReadSparseDataInternal(operation.sparse_offset(), operation.buf(),
operation.sparse_length(),
operation.ReleaseCallback());
break;
case SimpleEntryOperation::TYPE_WRITE_SPARSE:
WriteSparseDataInternal(operation.sparse_offset(), operation.buf(),
operation.sparse_length(),
operation.ReleaseCallback());
break;
case SimpleEntryOperation::TYPE_GET_AVAILABLE_RANGE:
GetAvailableRangeInternal(operation.sparse_offset(),
operation.sparse_length(),
operation.ReleaseRangeResultCalback());
break;
case SimpleEntryOperation::TYPE_DOOM:
DoomEntryInternal(operation.ReleaseCallback());
break;
default:
NOTREACHED();
}
}
}
void SimpleEntryImpl::OpenEntryInternal(
SimpleEntryOperation::EntryResultState result_state,
EntryResultCallback callback) {
ScopedOperationRunner operation_runner(this);
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_BEGIN);
DCHECK_EQ(SimpleEntryOperation::ENTRY_NEEDS_CALLBACK, result_state);
if (state_ == STATE_READY) {
ReturnEntryToCallerAsync( true, std::move(callback));
NetLogSimpleEntryCreation(net_log_,
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_END,
net::NetLogEventPhase::NONE, this, net::OK);
return;
}
if (state_ == STATE_FAILURE) {
PostClientCallback(std::move(callback),
EntryResult::MakeError(net::ERR_FAILED));
NetLogSimpleEntryCreation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_END,
net::NetLogEventPhase::NONE, this, net::ERR_FAILED);
return;
}
DCHECK_EQ(STATE_UNINITIALIZED, state_);
DCHECK(!synchronous_entry_);
state_ = STATE_IO_PENDING;
const base::TimeTicks start_time = base::TimeTicks::Now();
auto results = std::make_unique<SimpleEntryCreationResults>(
SimpleEntryStat(last_used_, data_size_, sparse_data_size_));
uint32_t trailer_prefetch_size = 0;
base::Time last_used_time;
if (SimpleBackendImpl* backend = backend_.get()) {
if (cache_type_ == net::APP_CACHE) {
trailer_prefetch_size =
backend->index()->GetTrailerPrefetchSize(entry_hash_);
} else {
last_used_time = backend->index()->GetLastUsedTime(entry_hash_);
}
}
base::OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::OpenEntry, cache_type_, path_, key_, entry_hash_,
file_tracker_, file_operations_factory_->CreateUnbound(),
trailer_prefetch_size, results.get());
base::OnceClosure reply = base::BindOnce(
&SimpleEntryImpl::CreationOperationComplete, this, result_state,
std::move(callback), start_time, last_used_time, std::move(results),
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_END);
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::CreateEntryInternal(
SimpleEntryOperation::EntryResultState result_state,
EntryResultCallback callback) {
ScopedOperationRunner operation_runner(this);
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_CREATE_BEGIN);
if (state_ != STATE_UNINITIALIZED) {
NetLogSimpleEntryCreation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_CREATE_END,
net::NetLogEventPhase::NONE, this, net::ERR_FAILED);
DCHECK_EQ(SimpleEntryOperation::ENTRY_NEEDS_CALLBACK, result_state);
PostClientCallback(std::move(callback),
EntryResult::MakeError(net::ERR_FAILED));
return;
}
DCHECK_EQ(STATE_UNINITIALIZED, state_);
DCHECK(!synchronous_entry_);
state_ = STATE_IO_PENDING;
last_used_ = base::Time::Now();
const base::TimeTicks start_time = base::TimeTicks::Now();
auto results = std::make_unique<SimpleEntryCreationResults>(
SimpleEntryStat(last_used_, data_size_, sparse_data_size_));
OnceClosure task =
base::BindOnce(&SimpleSynchronousEntry::CreateEntry, cache_type_, path_,
*key_, entry_hash_, file_tracker_,
file_operations_factory_->CreateUnbound(), results.get());
OnceClosure reply = base::BindOnce(
&SimpleEntryImpl::CreationOperationComplete, this, result_state,
std::move(callback), start_time, base::Time(), std::move(results),
net::NetLogEventType::SIMPLE_CACHE_ENTRY_CREATE_END);
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::OpenOrCreateEntryInternal(
OpenEntryIndexEnum index_state,
SimpleEntryOperation::EntryResultState result_state,
EntryResultCallback callback) {
ScopedOperationRunner operation_runner(this);
net_log_.AddEvent(
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_OR_CREATE_BEGIN);
bool optimistic_create =
(result_state == SimpleEntryOperation::ENTRY_ALREADY_RETURNED);
DCHECK(!optimistic_create || state_ == STATE_UNINITIALIZED);
if (state_ == STATE_READY) {
ReturnEntryToCallerAsync( true, std::move(callback));
NetLogSimpleEntryCreation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_OR_CREATE_END,
net::NetLogEventPhase::NONE, this, net::OK);
return;
}
if (state_ == STATE_FAILURE) {
PostClientCallback(std::move(callback),
EntryResult::MakeError(net::ERR_FAILED));
NetLogSimpleEntryCreation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_OR_CREATE_END,
net::NetLogEventPhase::NONE, this, net::ERR_FAILED);
return;
}
DCHECK_EQ(STATE_UNINITIALIZED, state_);
DCHECK(!synchronous_entry_);
state_ = STATE_IO_PENDING;
const base::TimeTicks start_time = base::TimeTicks::Now();
auto results = std::make_unique<SimpleEntryCreationResults>(
SimpleEntryStat(last_used_, data_size_, sparse_data_size_));
uint32_t trailer_prefetch_size = 0;
base::Time last_used_time;
if (SimpleBackendImpl* backend = backend_.get()) {
if (cache_type_ == net::APP_CACHE) {
trailer_prefetch_size =
backend->index()->GetTrailerPrefetchSize(entry_hash_);
} else {
last_used_time = backend->index()->GetLastUsedTime(entry_hash_);
}
}
base::OnceClosure task =
base::BindOnce(&SimpleSynchronousEntry::OpenOrCreateEntry, cache_type_,
path_, *key_, entry_hash_, index_state, optimistic_create,
file_tracker_, file_operations_factory_->CreateUnbound(),
trailer_prefetch_size, results.get());
base::OnceClosure reply = base::BindOnce(
&SimpleEntryImpl::CreationOperationComplete, this, result_state,
std::move(callback), start_time, last_used_time, std::move(results),
net::NetLogEventType::SIMPLE_CACHE_ENTRY_OPEN_OR_CREATE_END);
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::CloseInternal() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (open_count_ != 0) {
return;
}
typedef SimpleSynchronousEntry::CRCRecord CRCRecord;
auto crc32s_to_write = std::make_unique<std::vector<CRCRecord>>();
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_CLOSE_BEGIN);
if (state_ == STATE_READY) {
DCHECK(synchronous_entry_);
state_ = STATE_IO_PENDING;
for (int i = 0; i < kSimpleEntryStreamCount; ++i) {
if (have_written_[i]) {
if (GetDataSize(i) == crc32s_end_offset_[i]) {
int32_t crc = GetDataSize(i) == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i];
crc32s_to_write->push_back(CRCRecord(i, true, crc));
} else {
crc32s_to_write->push_back(CRCRecord(i, false, 0));
}
}
}
} else {
DCHECK(STATE_UNINITIALIZED == state_ || STATE_FAILURE == state_);
}
auto results = std::make_unique<SimpleEntryCloseResults>();
if (synchronous_entry_) {
OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::Close, base::Unretained(synchronous_entry_),
SimpleEntryStat(last_used_, data_size_, sparse_data_size_),
std::move(crc32s_to_write), base::RetainedRef(stream_0_data_),
results.get());
OnceClosure reply = base::BindOnce(&SimpleEntryImpl::CloseOperationComplete,
this, std::move(results));
synchronous_entry_ = nullptr;
prioritized_task_runner_->PostTaskAndReply(
FROM_HERE, std::move(task), std::move(reply), entry_priority_);
} else {
CloseOperationComplete(std::move(results));
}
}
int SimpleEntryImpl::ReadDataInternal(bool sync_possible,
int stream_index,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScopedOperationRunner operation_runner(this);
if (net_log_.IsCapturing()) {
NetLogReadWriteData(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_BEGIN,
net::NetLogEventPhase::NONE, stream_index, offset, buf_len, false);
}
if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(net_log_,
net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_END,
net::NetLogEventPhase::NONE, net::ERR_FAILED);
}
return PostToCallbackIfNeeded(sync_possible, std::move(callback),
net::ERR_FAILED);
}
DCHECK_EQ(STATE_READY, state_);
if (offset >= GetDataSize(stream_index) || !buf_len) {
return PostToCallbackIfNeeded(sync_possible, std::move(callback), 0);
}
buf_len = std::min(static_cast<int64_t>(buf_len),
GetDataSize(stream_index) - offset);
if (stream_index == 0) {
CHECK_LE(GetDataSize(stream_index), std::numeric_limits<int32_t>::max());
state_ = STATE_IO_PENDING;
ReadFromBuffer(stream_0_data_.get(), static_cast<size_t>(offset), buf_len,
buf);
state_ = STATE_READY;
return PostToCallbackIfNeeded(sync_possible, std::move(callback), buf_len);
}
if (stream_index == 1) {
if (stream_1_prefetch_data_) {
CHECK_EQ(stream_1_prefetch_data_->size(), GetDataSize(1));
CHECK_LE(GetDataSize(stream_index), std::numeric_limits<int32_t>::max());
state_ = STATE_IO_PENDING;
ReadFromBuffer(stream_1_prefetch_data_.get(), static_cast<size_t>(offset),
buf_len, buf);
state_ = STATE_READY;
return PostToCallbackIfNeeded(sync_possible, std::move(callback),
buf_len);
}
}
state_ = STATE_IO_PENDING;
if (doom_state_ == DOOM_NONE && backend_.get())
backend_->index()->UseIfExists(entry_hash_);
SimpleSynchronousEntry::ReadRequest read_req(stream_index, offset, buf_len);
if (crc32s_end_offset_[stream_index] == offset) {
read_req.request_update_crc = true;
read_req.previous_crc32 =
offset == 0 ? crc32(0, Z_NULL, 0) : crc32s_[stream_index];
read_req.request_verify_crc = !have_written_[stream_index];
}
auto result = std::make_unique<SimpleSynchronousEntry::ReadResult>();
auto entry_stat = std::make_unique<SimpleEntryStat>(last_used_, data_size_,
sparse_data_size_);
OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::ReadData, base::Unretained(synchronous_entry_),
read_req, entry_stat.get(), base::RetainedRef(buf), result.get());
OnceClosure reply = base::BindOnce(
&SimpleEntryImpl::ReadOperationComplete, this, stream_index, offset,
std::move(callback), std::move(entry_stat), std::move(result));
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
return net::ERR_IO_PENDING;
}
void SimpleEntryImpl::WriteDataInternal(int stream_index,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback,
bool truncate) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScopedOperationRunner operation_runner(this);
if (net_log_.IsCapturing()) {
NetLogReadWriteData(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_BEGIN,
net::NetLogEventPhase::NONE, stream_index, offset, buf_len, truncate);
}
if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_END,
net::NetLogEventPhase::NONE, net::ERR_FAILED);
}
if (!callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), net::ERR_FAILED));
}
return;
}
DCHECK_EQ(STATE_READY, state_);
if (stream_index == 0) {
state_ = STATE_IO_PENDING;
SetStream0Data(buf, base::checked_cast<int32_t>(offset), buf_len, truncate);
state_ = STATE_READY;
if (!callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), buf_len));
}
return;
}
if (buf_len == 0) {
int64_t data_size = data_size_[stream_index];
if (truncate ? (offset == data_size) : (offset <= data_size)) {
if (!callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), 0));
}
return;
}
}
state_ = STATE_IO_PENDING;
if (doom_state_ == DOOM_NONE && backend_.get())
backend_->index()->UseIfExists(entry_hash_);
if (stream_index == 1)
stream_1_prefetch_data_ = nullptr;
bool request_update_crc = false;
uint32_t initial_crc = 0;
if (offset < crc32s_end_offset_[stream_index]) {
crc32s_end_offset_[stream_index] = 0;
}
if (crc32s_end_offset_[stream_index] == offset) {
request_update_crc = true;
initial_crc = (offset != 0) ? crc32s_[stream_index] : crc32(0, Z_NULL, 0);
}
auto entry_stat = std::make_unique<SimpleEntryStat>(last_used_, data_size_,
sparse_data_size_);
if (truncate) {
data_size_[stream_index] = offset + buf_len;
} else {
data_size_[stream_index] = std::max(offset + buf_len,
GetDataSize(stream_index));
}
auto write_result = std::make_unique<SimpleSynchronousEntry::WriteResult>();
last_used_ = base::Time::Now();
have_written_[stream_index] = true;
if (stream_index == 1)
have_written_[0] = true;
OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::WriteData, base::Unretained(synchronous_entry_),
SimpleSynchronousEntry::WriteRequest(
stream_index, offset, buf_len, initial_crc, truncate,
doom_state_ != DOOM_NONE, request_update_crc),
base::Unretained(buf), entry_stat.get(), write_result.get());
OnceClosure reply =
base::BindOnce(&SimpleEntryImpl::WriteOperationComplete, this,
stream_index, std::move(callback), std::move(entry_stat),
std::move(write_result), base::RetainedRef(buf));
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::ReadSparseDataInternal(
int64_t sparse_offset,
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScopedOperationRunner operation_runner(this);
if (net_log_.IsCapturing()) {
NetLogSparseOperation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_SPARSE_BEGIN,
net::NetLogEventPhase::NONE, sparse_offset, buf_len);
}
if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_SPARSE_END,
net::NetLogEventPhase::NONE, net::ERR_FAILED);
}
if (!callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), net::ERR_FAILED));
}
return;
}
DCHECK_EQ(STATE_READY, state_);
state_ = STATE_IO_PENDING;
auto result = std::make_unique<int>();
auto last_used = std::make_unique<base::Time>();
OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::ReadSparseData,
base::Unretained(synchronous_entry_),
SimpleSynchronousEntry::SparseRequest(sparse_offset, buf_len),
base::RetainedRef(buf), last_used.get(), result.get());
OnceClosure reply = base::BindOnce(
&SimpleEntryImpl::ReadSparseOperationComplete, this, std::move(callback),
std::move(last_used), std::move(result));
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::WriteSparseDataInternal(
int64_t sparse_offset,
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScopedOperationRunner operation_runner(this);
if (net_log_.IsCapturing()) {
NetLogSparseOperation(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_SPARSE_BEGIN,
net::NetLogEventPhase::NONE, sparse_offset, buf_len);
}
if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_SPARSE_END,
net::NetLogEventPhase::NONE, net::ERR_FAILED);
}
if (!callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), net::ERR_FAILED));
}
return;
}
DCHECK_EQ(STATE_READY, state_);
state_ = STATE_IO_PENDING;
uint64_t max_sparse_data_size = std::numeric_limits<int64_t>::max();
if (backend_.get()) {
uint64_t max_cache_size = backend_->index()->max_size();
max_sparse_data_size = max_cache_size / kMaxSparseDataSizeDivisor;
}
auto entry_stat = std::make_unique<SimpleEntryStat>(last_used_, data_size_,
sparse_data_size_);
last_used_ = base::Time::Now();
auto result = std::make_unique<int>();
OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::WriteSparseData,
base::Unretained(synchronous_entry_),
SimpleSynchronousEntry::SparseRequest(sparse_offset, buf_len),
base::RetainedRef(buf), max_sparse_data_size, entry_stat.get(),
result.get());
OnceClosure reply = base::BindOnce(
&SimpleEntryImpl::WriteSparseOperationComplete, this, std::move(callback),
std::move(entry_stat), std::move(result));
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::GetAvailableRangeInternal(int64_t sparse_offset,
int len,
RangeResultCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScopedOperationRunner operation_runner(this);
if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
if (!callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), RangeResult(net::ERR_FAILED)));
}
return;
}
DCHECK_EQ(STATE_READY, state_);
state_ = STATE_IO_PENDING;
auto result = std::make_unique<RangeResult>();
OnceClosure task = base::BindOnce(
&SimpleSynchronousEntry::GetAvailableRange,
base::Unretained(synchronous_entry_),
SimpleSynchronousEntry::SparseRequest(sparse_offset, len), result.get());
OnceClosure reply =
base::BindOnce(&SimpleEntryImpl::GetAvailableRangeOperationComplete, this,
std::move(callback), std::move(result));
prioritized_task_runner_->PostTaskAndReply(FROM_HERE, std::move(task),
std::move(reply), entry_priority_);
}
void SimpleEntryImpl::DoomEntryInternal(net::CompletionOnceCallback callback) {
if (doom_state_ == DOOM_COMPLETED) {
DoomOperationComplete(std::move(callback), state_, net::OK);
return;
}
if (!backend_) {
prioritized_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&SimpleSynchronousEntry::TruncateEntryFiles, path_,
entry_hash_, file_operations_factory_->CreateUnbound()),
base::BindOnce(&SimpleEntryImpl::DoomOperationComplete, this,
std::move(callback),
STATE_FAILURE),
entry_priority_);
state_ = STATE_IO_PENDING;
return;
}
if (synchronous_entry_) {
prioritized_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&SimpleSynchronousEntry::Doom,
base::Unretained(synchronous_entry_)),
base::BindOnce(&SimpleEntryImpl::DoomOperationComplete, this,
std::move(callback), state_),
entry_priority_);
} else {
DCHECK_EQ(STATE_UNINITIALIZED, state_);
prioritized_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&SimpleSynchronousEntry::DeleteEntryFiles, path_,
cache_type_, entry_hash_,
file_operations_factory_->CreateUnbound()),
base::BindOnce(&SimpleEntryImpl::DoomOperationComplete, this,
std::move(callback), state_),
entry_priority_);
}
state_ = STATE_IO_PENDING;
}
void SimpleEntryImpl::CreationOperationComplete(
SimpleEntryOperation::EntryResultState result_state,
EntryResultCallback completion_callback,
const base::TimeTicks& start_time,
const base::Time index_last_used_time,
std::unique_ptr<SimpleEntryCreationResults> in_results,
net::NetLogEventType end_event_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(state_, STATE_IO_PENDING);
DCHECK(in_results);
ScopedOperationRunner operation_runner(this);
if (in_results->result != net::OK) {
if (in_results->result != net::ERR_FILE_EXISTS) {
if (backend_)
backend_->index()->Remove(entry_hash_);
}
net_log_.AddEventWithNetErrorCode(end_event_type, net::ERR_FAILED);
PostClientCallback(std::move(completion_callback),
EntryResult::MakeError(net::ERR_FAILED));
ResetEntry();
return;
}
if (in_results->created) {
for (bool& have_written : have_written_)
have_written = true;
}
if (backend_ && doom_state_ == DOOM_NONE)
backend_->index()->Insert(entry_hash_);
synchronous_entry_ = in_results->sync_entry;
for (int stream = 0; stream < 2; ++stream) {
const SimpleStreamPrefetchData& prefetched =
in_results->stream_prefetch_data[stream];
if (prefetched.data.get()) {
if (stream == 0)
stream_0_data_ = prefetched.data;
else
stream_1_prefetch_data_ = prefetched.data;
crc32s_[stream] = prefetched.stream_crc32;
crc32s_end_offset_[stream] = in_results->entry_stat.data_size(stream);
}
}
if (!key_.has_value()) {
SetKey(*synchronous_entry_->key());
} else {
DCHECK_EQ(*key_, *synchronous_entry_->key());
}
if (!index_last_used_time.is_null())
in_results->entry_stat.set_last_used(index_last_used_time);
UpdateDataFromEntryStat(in_results->entry_stat);
if (cache_type_ == net::APP_CACHE && backend_.get() && backend_->index()) {
backend_->index()->SetTrailerPrefetchSize(
entry_hash_, in_results->computed_trailer_prefetch_size);
}
SIMPLE_CACHE_UMA(TIMES,
"EntryCreationTime", cache_type_,
(base::TimeTicks::Now() - start_time));
net_log_.AddEvent(end_event_type);
const bool created = in_results->created;
in_results = nullptr;
state_ = STATE_READY;
if (result_state == SimpleEntryOperation::ENTRY_NEEDS_CALLBACK) {
ReturnEntryToCallerAsync(!created, std::move(completion_callback));
}
}
void SimpleEntryImpl::UpdateStateAfterOperationComplete(
const SimpleEntryStat& entry_stat,
int result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(synchronous_entry_);
DCHECK_EQ(STATE_IO_PENDING, state_);
if (result < 0) {
state_ = STATE_FAILURE;
MarkAsDoomed(DOOM_COMPLETED);
} else {
UpdateDataFromEntryStat(entry_stat);
state_ = STATE_READY;
}
}
void SimpleEntryImpl::EntryOperationComplete(
net::CompletionOnceCallback completion_callback,
const SimpleEntryStat& entry_stat,
int result) {
UpdateStateAfterOperationComplete(entry_stat, result);
if (!completion_callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(completion_callback), result));
}
RunNextOperationIfNeeded();
}
void SimpleEntryImpl::ReadOperationComplete(
int stream_index,
int64_t offset,
net::CompletionOnceCallback completion_callback,
std::unique_ptr<SimpleEntryStat> entry_stat,
std::unique_ptr<SimpleSynchronousEntry::ReadResult> read_result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(synchronous_entry_);
DCHECK_EQ(STATE_IO_PENDING, state_);
DCHECK(read_result);
int result = read_result->result;
if (read_result->crc_updated) {
if (result > 0) {
DCHECK_EQ(crc32s_end_offset_[stream_index], offset);
crc32s_end_offset_[stream_index] += result;
crc32s_[stream_index] = read_result->updated_crc32;
}
}
if (result < 0) {
crc32s_end_offset_[stream_index] = 0;
}
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(net_log_,
net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_END,
net::NetLogEventPhase::NONE, result);
}
EntryOperationComplete(std::move(completion_callback), *entry_stat, result);
}
void SimpleEntryImpl::WriteOperationComplete(
int stream_index,
net::CompletionOnceCallback completion_callback,
std::unique_ptr<SimpleEntryStat> entry_stat,
std::unique_ptr<SimpleSynchronousEntry::WriteResult> write_result,
net::IOBuffer* buf) {
int result = write_result->result;
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(net_log_,
net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_END,
net::NetLogEventPhase::NONE, result);
}
if (result < 0)
crc32s_end_offset_[stream_index] = 0;
if (result > 0 && write_result->crc_updated) {
crc32s_end_offset_[stream_index] += result;
crc32s_[stream_index] = write_result->updated_crc32;
}
EntryOperationComplete(std::move(completion_callback), *entry_stat, result);
}
void SimpleEntryImpl::ReadSparseOperationComplete(
net::CompletionOnceCallback completion_callback,
std::unique_ptr<base::Time> last_used,
std::unique_ptr<int> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(synchronous_entry_);
DCHECK(result);
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_SPARSE_END,
net::NetLogEventPhase::NONE, *result);
}
SimpleEntryStat entry_stat(*last_used, data_size_, sparse_data_size_);
EntryOperationComplete(std::move(completion_callback), entry_stat, *result);
}
void SimpleEntryImpl::WriteSparseOperationComplete(
net::CompletionOnceCallback completion_callback,
std::unique_ptr<SimpleEntryStat> entry_stat,
std::unique_ptr<int> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(synchronous_entry_);
DCHECK(result);
if (net_log_.IsCapturing()) {
NetLogReadWriteComplete(
net_log_, net::NetLogEventType::SIMPLE_CACHE_ENTRY_WRITE_SPARSE_END,
net::NetLogEventPhase::NONE, *result);
}
EntryOperationComplete(std::move(completion_callback), *entry_stat, *result);
}
void SimpleEntryImpl::GetAvailableRangeOperationComplete(
RangeResultCallback completion_callback,
std::unique_ptr<RangeResult> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(synchronous_entry_);
DCHECK(result);
SimpleEntryStat entry_stat(last_used_, data_size_, sparse_data_size_);
UpdateStateAfterOperationComplete(entry_stat, result->net_error);
if (!completion_callback.is_null()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(completion_callback), *result));
}
RunNextOperationIfNeeded();
}
void SimpleEntryImpl::DoomOperationComplete(
net::CompletionOnceCallback callback,
State state_to_restore,
int result) {
state_ = state_to_restore;
doom_state_ = DOOM_COMPLETED;
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_DOOM_END);
PostClientCallback(std::move(callback), result);
RunNextOperationIfNeeded();
if (post_doom_waiting_) {
post_doom_waiting_->OnOperationComplete(entry_hash_);
post_doom_waiting_ = nullptr;
}
}
void SimpleEntryImpl::CloseOperationComplete(
std::unique_ptr<SimpleEntryCloseResults> in_results) {
DCHECK(!synchronous_entry_);
DCHECK_EQ(0, open_count_);
DCHECK(STATE_IO_PENDING == state_ || STATE_FAILURE == state_ ||
STATE_UNINITIALIZED == state_);
net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_CLOSE_END);
if (cache_type_ == net::APP_CACHE &&
in_results->estimated_trailer_prefetch_size > 0 && backend_.get() &&
backend_->index()) {
backend_->index()->SetTrailerPrefetchSize(
entry_hash_, in_results->estimated_trailer_prefetch_size);
}
ResetEntry();
RunNextOperationIfNeeded();
}
void SimpleEntryImpl::UpdateDataFromEntryStat(
const SimpleEntryStat& entry_stat) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(synchronous_entry_);
CHECK_EQ(state_, STATE_IO_PENDING);
last_used_ = entry_stat.last_used();
for (int i = 0; i < kSimpleEntryStreamCount; ++i) {
data_size_[i] = entry_stat.data_size(i);
}
sparse_data_size_ = entry_stat.sparse_data_size();
SimpleBackendImpl* backend_ptr = backend_.get();
if (doom_state_ == DOOM_NONE && backend_ptr) {
backend_ptr->index()->UpdateEntrySize(entry_hash_, GetDiskUsage());
}
}
uint64_t SimpleEntryImpl::GetDiskUsage() const {
uint64_t file_size = 0;
for (int64_t data_size : data_size_) {
file_size += base::checked_cast<uint64_t>(
simple_util::GetFileSizeFromDataSize(key_->size(), data_size));
}
file_size += sparse_data_size_;
return file_size;
}
void SimpleEntryImpl::ReadFromBuffer(net::GrowableIOBuffer* in_buf,
size_t offset,
int buf_len,
net::IOBuffer* out_buf) {
DCHECK_GE(buf_len, 0);
out_buf->span().copy_prefix_from(
in_buf->span().subspan(offset, base::checked_cast<size_t>(buf_len)));
UpdateDataFromEntryStat(
SimpleEntryStat(base::Time::Now(), data_size_, sparse_data_size_));
}
void SimpleEntryImpl::SetStream0Data(net::IOBuffer* buf,
int offset,
int buf_len,
bool truncate) {
CHECK_LE(GetDataSize(0), std::numeric_limits<int32_t>::max());
CHECK_LE(0, GetDataSize(0));
CHECK_LE(0, offset);
CHECK_LE(0, buf_len);
size_t u_offset = static_cast<size_t>(offset);
have_written_[0] = true;
size_t data_size = static_cast<size_t>(GetDataSize(0));
if (offset == 0 && truncate) {
stream_0_data_->SetCapacity(buf_len);
if (buf_len) {
stream_0_data_->span().copy_from(
buf->first(base::checked_cast<size_t>(buf_len)));
}
data_size_[0] = buf_len;
} else {
const size_t buffer_size =
truncate ? u_offset + buf_len : std::max(u_offset + buf_len, data_size);
stream_0_data_->SetCapacity(base::checked_cast<int>(buffer_size));
const size_t fill_size = u_offset <= data_size ? 0 : u_offset - data_size;
if (fill_size > 0) {
std::ranges::fill(stream_0_data_->span().subspan(data_size, fill_size),
0);
}
if (buf) {
stream_0_data_->span().subspan(u_offset).copy_prefix_from(
buf->first(base::checked_cast<size_t>(buf_len)));
}
data_size_[0] = buffer_size;
}
RecordHeaderSize(cache_type_, data_size_[0]);
base::Time modification_time = base::Time::Now();
crc32s_end_offset_[0] = 0;
UpdateDataFromEntryStat(
SimpleEntryStat(modification_time, data_size_, sparse_data_size_));
}
}