Chromium Code Reviews| Index: net/disk_cache/simple/simple_entry_impl.cc |
| diff --git a/net/disk_cache/simple/simple_entry_impl.cc b/net/disk_cache/simple/simple_entry_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5cf4a7e4e7b1fe24fc72a902dcdd4930203c17e9 |
| --- /dev/null |
| +++ b/net/disk_cache/simple/simple_entry_impl.cc |
| @@ -0,0 +1,292 @@ |
| +// Copyright (c) 2013 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 "net/disk_cache/simple/simple_entry_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/callback.h" |
| +#include "base/location.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "base/threading/worker_pool.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/disk_cache/simple/simple_synchronous_entry.h" |
| + |
| + |
|
rvargas (doing something else)
2013/02/13 01:48:46
remove extra line
gavinp
2013/02/14 15:29:55
Done.
|
| +namespace { |
| + |
| +typedef disk_cache::Entry::CompletionCallback CompletionCallback; |
| +typedef disk_cache::SimpleSynchronousEntry::SynchronousEntryCallback |
| + SynchronousEntryCallback; |
| + |
| +} // namespace |
| + |
| +namespace disk_cache { |
| + |
| +using base::FilePath; |
| +using base::MessageLoopProxy; |
| +using base::Time; |
| +using base::WeakPtr; |
| +using base::WorkerPool; |
| + |
| +// static |
| +int SimpleEntryImpl::OpenEntry(const FilePath& path, |
| + const std::string& key, |
| + Entry** entry, |
| + const CompletionCallback& callback) { |
| + SynchronousEntryCallback sync_entry_callback = |
| + base::Bind(&SimpleEntryImpl::CreationOperationComplete, callback, entry); |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::OpenEntry, path, key, |
| + MessageLoopProxy::current(), |
| + sync_entry_callback), |
| + true); |
| + return net::ERR_IO_PENDING; |
| +} |
| + |
| +// static |
| +int SimpleEntryImpl::CreateEntry(const FilePath& path, |
| + const std::string& key, |
| + Entry** entry, |
| + const CompletionCallback& callback) { |
| + SynchronousEntryCallback sync_entry_callback = |
| + base::Bind(&SimpleEntryImpl::CreationOperationComplete, callback, entry); |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::CreateEntry, path, |
| + key, MessageLoopProxy::current(), |
| + sync_entry_callback), |
| + true); |
| + return net::ERR_IO_PENDING; |
| +} |
| + |
| +// static |
| +int SimpleEntryImpl::DoomEntry(const FilePath& path, |
| + const std::string& key, |
| + const CompletionCallback& callback) { |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::DoomEntry, path, key, |
| + MessageLoopProxy::current(), callback), |
| + true); |
| + return net::ERR_IO_PENDING; |
| +} |
| + |
| +void SimpleEntryImpl::Doom() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Overlapping an asynchronous operation."; |
| + return; |
| + } |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::DoomAndClose, |
| + base::Unretained(synchronous_entry_)), |
| + true); |
| + synchronous_entry_ = NULL; |
| + has_been_doomed_ = true; |
| +} |
| + |
| +void SimpleEntryImpl::Close() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Overlapping an asynchronous operation."; |
| + delete this; |
| + return; |
| + } |
| + DCHECK(synchronous_entry_ || has_been_doomed_); |
| + if (!has_been_doomed_) { |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::Close, |
| + base::Unretained(synchronous_entry_)), |
| + true); |
| + synchronous_entry_ = NULL; |
| + } |
| + // Entry::Close() is expected to release this entry. See disk_cache.h for |
| + // details. |
| + delete this; |
| +} |
| + |
| +std::string SimpleEntryImpl::GetKey() const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + return key_; |
| +} |
| + |
| +Time SimpleEntryImpl::GetLastUsed() const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Synchronous operations overlapping an asynchronous " |
| + << "operation."; |
| + NOTREACHED(); |
|
rvargas (doing something else)
2013/02/13 01:48:46
nit: sounds like this should be removed (not a cod
gavinp
2013/02/14 15:29:55
Done. I replaced them with CHECK(false), which is
|
| + } |
| + return synchronous_entry_->last_used(); |
| +} |
| + |
| +Time SimpleEntryImpl::GetLastModified() const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Synchronous operations overlapping an asynchronous " |
| + << "operation."; |
| + NOTREACHED(); |
| + } |
| + return synchronous_entry_->last_modified(); |
| +} |
| + |
| +int32 SimpleEntryImpl::GetDataSize(int index) const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Synchronous operations overlapping an asynchronous " |
| + << "operation."; |
| + NOTREACHED(); |
| + } |
| + return synchronous_entry_->data_size(index); |
| +} |
| + |
| +int SimpleEntryImpl::ReadData(int index, |
| + int offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + // TODO(gavinp): Add support for overlapping reads. The net::HttpCache does |
| + // make overlapping read requests when multiple transactions access the same |
| + // entry as read only. |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Overlapping calls to ReadData."; |
| + NOTREACHED(); |
| + } |
| + synchronous_entry_in_use_by_worker_ = true; |
| + SynchronousEntryCallback sync_entry_callback = |
| + base::Bind(&SimpleEntryImpl::EntryOperationComplete, |
| + callback, weak_ptr_factory_.GetWeakPtr()); |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::ReadData, |
| + base::Unretained(synchronous_entry_), |
| + index, offset, scoped_refptr<IOBuffer>(buf), |
|
rvargas (doing something else)
2013/02/13 01:48:46
make_scoped_refptr (if you want to force a scoped
gavinp
2013/02/14 15:29:55
Done.
|
| + buf_len, sync_entry_callback), |
| + true); |
| + return net::ERR_IO_PENDING; |
| +} |
| + |
| +int SimpleEntryImpl::WriteData(int index, |
| + int offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback, |
| + bool truncate) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (synchronous_entry_in_use_by_worker_) { |
| + NOTIMPLEMENTED() << ": Overlapping calls to WriteData."; |
| + NOTREACHED(); |
| + } |
| + synchronous_entry_in_use_by_worker_ = true; |
| + SynchronousEntryCallback sync_entry_callback = |
| + base::Bind(&SimpleEntryImpl::EntryOperationComplete, |
| + callback, weak_ptr_factory_.GetWeakPtr()); |
| + WorkerPool::PostTask(FROM_HERE, |
| + base::Bind(&SimpleSynchronousEntry::WriteData, |
| + base::Unretained(synchronous_entry_), |
| + index, offset, scoped_refptr<IOBuffer>(buf), |
| + buf_len, sync_entry_callback, truncate), |
| + true); |
| + return net::ERR_IO_PENDING; |
| +} |
| + |
| +int SimpleEntryImpl::ReadSparseData(int64 offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback) { |
| + // TODO(gavinp): Determine if the simple backend should support sparse data. |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + NOTIMPLEMENTED(); |
| + return net::ERR_FAILED; |
| +} |
| + |
| +int SimpleEntryImpl::WriteSparseData(int64 offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback) { |
| + // TODO(gavinp): Determine if the simple backend should support sparse data. |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + NOTIMPLEMENTED(); |
| + return net::ERR_FAILED; |
| +} |
| + |
| +int SimpleEntryImpl::GetAvailableRange(int64 offset, |
| + int len, |
| + int64* start, |
| + const CompletionCallback& callback) { |
| + // TODO(gavinp): Determine if the simple backend should support sparse data. |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + NOTIMPLEMENTED(); |
| + return net::ERR_FAILED; |
| +} |
| + |
| +bool SimpleEntryImpl::CouldBeSparse() const { |
| + // TODO(gavinp): Determine if the simple backend should support sparse data. |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + return false; |
| +} |
| + |
| +void SimpleEntryImpl::CancelSparseIO() { |
| + // TODO(gavinp): Determine if the simple backend should support sparse data. |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
| + // TODO(gavinp): Determine if the simple backend should support sparse data. |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + NOTIMPLEMENTED(); |
| + return net::ERR_FAILED; |
| +} |
| + |
| +SimpleEntryImpl::SimpleEntryImpl( |
| + SimpleSynchronousEntry* synchronous_entry) |
| + : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| + key_(synchronous_entry->key()), |
| + synchronous_entry_(synchronous_entry), |
| + synchronous_entry_in_use_by_worker_(false), |
| + has_been_doomed_(false) { |
| + DCHECK(synchronous_entry); |
| +} |
| + |
| +SimpleEntryImpl::~SimpleEntryImpl() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(!synchronous_entry_) << "synchronous_entry_ = " << synchronous_entry_; |
|
rvargas (doing something else)
2013/02/13 01:48:46
In general I would not bother to add more logging
gavinp
2013/02/14 15:29:55
Done.
|
| +} |
| + |
| +// static |
| +void SimpleEntryImpl::CreationOperationComplete( |
| + const CompletionCallback& completion_callback, |
| + Entry** out_entry, |
| + SimpleSynchronousEntry* sync_entry, |
| + int result) { |
| + DCHECK_NE(net::ERR_IO_PENDING, result); |
| + |
| + if (result != net::OK) { |
| + DCHECK(!sync_entry) << "sync_entry = " << sync_entry; |
| + completion_callback.Run(result); |
| + return; |
| + } |
| + DCHECK(sync_entry); |
| + *out_entry = new SimpleEntryImpl(sync_entry); |
| + DCHECK(*out_entry); |
| + completion_callback.Run(net::OK); |
| +} |
| + |
| +// static |
| +void SimpleEntryImpl::EntryOperationComplete( |
| + const CompletionCallback& completion_callback, |
| + base::WeakPtr<SimpleEntryImpl> entry, |
| + SimpleSynchronousEntry* sync_entry, |
|
rvargas (doing something else)
2013/02/13 01:48:46
is it really worth passing sync_entry back just to
gavinp
2013/02/14 15:29:55
No. Removed.
|
| + int result) { |
| + DCHECK(sync_entry); |
| + if (entry) { |
| + DCHECK(entry->synchronous_entry_in_use_by_worker_); |
| + DCHECK_EQ(entry->synchronous_entry_, sync_entry); |
| + entry->synchronous_entry_in_use_by_worker_ = false; |
| + } |
| + completion_callback.Run(result); |
| +} |
| + |
| +} // namespace disk_cache |