Chromium Code Reviews| Index: net/http/http_cache_data_access.cc |
| diff --git a/net/http/http_cache_data_access.cc b/net/http/http_cache_data_access.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f8251c896b36f8aa9c46e05ee123842112878f00 |
| --- /dev/null |
| +++ b/net/http/http_cache_data_access.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <memory> |
| +#include <utility> |
| +#include "base/callback_helpers.h" |
| +#include "net/disk_cache/disk_cache.h" |
| +#include "net/http/http_cache_data_access.h" |
| +#include "net/http/http_transaction.h" |
| + |
| +namespace net { |
| + |
| +HttpCache::DataAccess::DataAccess( |
| + std::unique_ptr<HttpTransaction> network_transaction, |
| + ActiveEntry* entry) |
| + : entry_(entry), weak_factory_(this) { |
| + network_transaction_ = std::move(network_transaction); |
| + io_callback_ = |
| + base::Bind(&DataAccess::OnIOComplete, weak_factory_.GetWeakPtr()); |
| +} |
| + |
| +HttpCache::DataAccess::~DataAccess() {} |
| + |
| +int HttpCache::DataAccess::DoLoop(int result) { |
| + DCHECK(next_state_ != STATE_NONE); |
| + |
| + int rv = result; |
| + |
| + do { |
| + State state = next_state_; |
| + next_state_ = STATE_NONE; |
| + switch (state) { |
| + case STATE_NETWORK_READ: |
| + DCHECK_EQ(OK, rv); |
| + rv = DoNetworkRead(); |
| + break; |
| + case STATE_NETWORK_READ_COMPLETE: |
| + rv = DoNetworkReadComplete(rv); |
| + break; |
| + case STATE_CACHE_WRITE_DATA: |
| + rv = DoCacheWriteData(rv); |
| + break; |
| + case STATE_CACHE_WRITE_DATA_COMPLETE: |
| + rv = DoCacheWriteDataComplete(rv); |
| + break; |
| + default: |
| + NOTREACHED() << "bad state"; |
| + rv = ERR_FAILED; |
| + break; |
| + } |
| + } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| + |
| + if (rv != ERR_IO_PENDING && !callback_.is_null()) { |
| + read_buf_ = NULL; // Release the buffer before invoking the callback. |
| + base::ResetAndReturn(&callback_).Run(rv); |
| + } |
| + |
| + return rv; |
| +} |
| + |
| +int HttpCache::DataAccess::Read(scoped_refptr<IOBuffer> buf, |
| + int buf_len, |
| + const CompletionCallback& callback) { |
| + DCHECK_EQ(next_state_, STATE_NONE); |
| + DCHECK(buf); |
| + DCHECK_GT(buf_len, 0); |
| + DCHECK(!callback.is_null()); |
| + DCHECK(callback_.is_null()); |
|
Randy Smith (Not in Mondays)
2017/01/19 00:53:42
Both the state invariants and argument invariants
Randy Smith (Not in Mondays)
2017/01/19 00:53:42
Suggestion: Group these by state invariants (i.e.
shivanisha
2017/01/25 19:46:13
N/A since DataAccess class no longer exists.
|
| + |
| + read_buf_ = std::move(buf); |
|
Randy Smith (Not in Mondays)
2017/01/19 00:53:42
Should some assertion about read_buf_ being null b
shivanisha
2017/01/25 19:46:13
N/A since DataAccess class no longer exists.
|
| + io_buf_len_ = buf_len; |
| + |
| + next_state_ = STATE_NETWORK_READ; |
| + int rv = DoLoop(OK); |
| + |
| + if (rv == ERR_IO_PENDING) { |
| + DCHECK(callback_.is_null()); |
| + callback_ = callback; |
| + } |
| + return rv; |
| +} |
| + |
| +int HttpCache::DataAccess::DoNetworkRead() { |
| + next_state_ = STATE_NETWORK_READ_COMPLETE; |
| + return network_transaction_->Read(read_buf_.get(), io_buf_len_, io_callback_); |
| +} |
| + |
| +int HttpCache::DataAccess::DoNetworkReadComplete(int result) { |
| + return result; |
|
Randy Smith (Not in Mondays)
2017/01/19 00:53:42
Worthwhile nulling out read_buf_ and maybe io_buf_
shivanisha
2017/01/25 19:46:13
N/A since DataAccess class no longer exists.
|
| +} |
| + |
| +int HttpCache::DataAccess::CacheWrite(scoped_refptr<IOBuffer> buf, |
| + int write_len, |
| + const CompletionCallback& callback) { |
| + DCHECK_EQ(next_state_, STATE_NONE); |
| + DCHECK(buf); |
| + DCHECK_GE(write_len, 0); |
| + DCHECK(!callback.is_null()); |
| + DCHECK(callback_.is_null()); |
| + |
| + read_buf_ = std::move(buf); |
| + next_state_ = STATE_CACHE_WRITE_DATA; |
| + int rv = DoLoop(write_len); |
| + |
| + if (rv == ERR_IO_PENDING) { |
| + DCHECK(callback_.is_null()); |
| + callback_ = callback; |
| + } |
| + |
| + return rv; |
| +} |
| + |
| +int HttpCache::DataAccess::DoCacheWriteData(int num_bytes) { |
| + next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE; |
| + write_len_ = num_bytes; |
| + int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| + return entry_->disk_entry->WriteData(kResponseContentIndex, current_size, |
| + read_buf_.get(), num_bytes, io_callback_, |
| + true); |
| +} |
| + |
| +int HttpCache::DataAccess::DoCacheWriteDataComplete(int result) { |
| + return result; |
| +} |
| + |
| +void HttpCache::DataAccess::OnIOComplete(int result) { |
| + DoLoop(result); |
| +} |
| + |
| +} // namespace net |