From af6588f8d723931a298c995fa97259bb7f7deb55 Mon Sep 17 00:00:00 2001 From: Zeno Albisser Date: Fri, 5 Dec 2014 15:04:29 +0100 Subject: BASELINE: Update chromium to 40.0.2214.28 and ninja to 1.5.3. Change-Id: I759465284fd64d59ad120219cbe257f7402c4181 Reviewed-by: Andras Becsi --- .../net/disk_cache/simple/simple_backend_impl.cc | 283 +++++++++++---------- 1 file changed, 155 insertions(+), 128 deletions(-) (limited to 'chromium/net/disk_cache/simple/simple_backend_impl.cc') diff --git a/chromium/net/disk_cache/simple/simple_backend_impl.cc b/chromium/net/disk_cache/simple/simple_backend_impl.cc index 5e8c3f2ffbf..6385a3763c2 100644 --- a/chromium/net/disk_cache/simple/simple_backend_impl.cc +++ b/chromium/net/disk_cache/simple/simple_backend_impl.cc @@ -14,15 +14,15 @@ #include "base/bind.h" #include "base/callback.h" -#include "base/file_util.h" +#include "base/files/file_util.h" #include "base/location.h" -#include "base/message_loop/message_loop_proxy.h" #include "base/metrics/field_trial.h" #include "base/metrics/histogram.h" #include "base/metrics/sparse_histogram.h" #include "base/single_thread_task_runner.h" #include "base/sys_info.h" #include "base/task_runner_util.h" +#include "base/thread_task_runner_handle.h" #include "base/threading/sequenced_worker_pool.h" #include "base/time/time.h" #include "net/base/net_errors.h" @@ -39,9 +39,7 @@ using base::Callback; using base::Closure; using base::FilePath; -using base::MessageLoopProxy; using base::SequencedWorkerPool; -using base::SingleThreadTaskRunner; using base::Time; using base::DirectoryExists; using base::CreateDirectory; @@ -197,19 +195,47 @@ void RecordIndexLoad(net::CacheType cache_type, } // namespace -SimpleBackendImpl::SimpleBackendImpl(const FilePath& path, - int max_bytes, - net::CacheType cache_type, - base::SingleThreadTaskRunner* cache_thread, - net::NetLog* net_log) +class SimpleBackendImpl::ActiveEntryProxy + : public SimpleEntryImpl::ActiveEntryProxy { + public: + ~ActiveEntryProxy() override { + if (backend_) { + DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_)); + backend_->active_entries_.erase(entry_hash_); + } + } + + static scoped_ptr Create( + int64 entry_hash, + SimpleBackendImpl* backend) { + scoped_ptr + proxy(new ActiveEntryProxy(entry_hash, backend)); + return proxy.Pass(); + } + + private: + ActiveEntryProxy(uint64 entry_hash, + SimpleBackendImpl* backend) + : entry_hash_(entry_hash), + backend_(backend->AsWeakPtr()) {} + + uint64 entry_hash_; + base::WeakPtr backend_; +}; + +SimpleBackendImpl::SimpleBackendImpl( + const FilePath& path, + int max_bytes, + net::CacheType cache_type, + const scoped_refptr& cache_thread, + net::NetLog* net_log) : path_(path), cache_type_(cache_type), cache_thread_(cache_thread), orig_max_size_(max_bytes), - entry_operations_mode_( - cache_type == net::DISK_CACHE ? - SimpleEntryImpl::OPTIMISTIC_OPERATIONS : - SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS), + entry_operations_mode_(cache_type == net::DISK_CACHE ? + SimpleEntryImpl::OPTIMISTIC_OPERATIONS : + SimpleEntryImpl::NON_OPTIMISTIC_OPERATIONS), net_log_(net_log) { MaybeHistogramFdLimit(cache_type_); } @@ -224,19 +250,22 @@ int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) { worker_pool_ = g_sequenced_worker_pool->GetTaskRunnerWithShutdownBehavior( SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); - index_.reset(new SimpleIndex(MessageLoopProxy::current(), this, cache_type_, - make_scoped_ptr(new SimpleIndexFile( - cache_thread_.get(), worker_pool_.get(), - cache_type_, path_)))); + index_.reset(new SimpleIndex( + base::ThreadTaskRunnerHandle::Get(), + this, + cache_type_, + make_scoped_ptr(new SimpleIndexFile( + cache_thread_, worker_pool_.get(), cache_type_, path_)))); index_->ExecuteWhenReady( base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now())); PostTaskAndReplyWithResult( - cache_thread_, + cache_thread_.get(), FROM_HERE, - base::Bind(&SimpleBackendImpl::InitCacheStructureOnDisk, path_, - orig_max_size_), - base::Bind(&SimpleBackendImpl::InitializeIndex, AsWeakPtr(), + base::Bind( + &SimpleBackendImpl::InitCacheStructureOnDisk, path_, orig_max_size_), + base::Bind(&SimpleBackendImpl::InitializeIndex, + AsWeakPtr(), completion_callback)); return net::ERR_IO_PENDING; } @@ -250,20 +279,14 @@ int SimpleBackendImpl::GetMaxFileSize() const { return index_->max_size() / kMaxFileRatio; } -void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) { - active_entries_.erase(entry->entry_hash()); -} - void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) { - // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. - CHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); + DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); entries_pending_doom_.insert( std::make_pair(entry_hash, std::vector())); } void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) { - // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. - CHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); + DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); base::hash_map >::iterator it = entries_pending_doom_.find(entry_hash); std::vector to_run_closures; @@ -289,11 +312,8 @@ void SimpleBackendImpl::DoomEntries(std::vector* entry_hashes, // SimpleSynchronousEntry::DoomEntrySet and delete the files en masse. for (int i = mass_doom_entry_hashes->size() - 1; i >= 0; --i) { const uint64 entry_hash = (*mass_doom_entry_hashes)[i]; - // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. - CHECK(active_entries_.count(entry_hash) == 0 || - entries_pending_doom_.count(entry_hash) == 0) - << "The entry 0x" << std::hex << entry_hash - << " is both active and pending doom."; + DCHECK(active_entries_.count(entry_hash) == 0 || + entries_pending_doom_.count(entry_hash) == 0); if (!active_entries_.count(entry_hash) && !entries_pending_doom_.count(entry_hash)) { continue; @@ -312,8 +332,7 @@ void SimpleBackendImpl::DoomEntries(std::vector* entry_hashes, it = to_doom_individually_hashes.begin(), end = to_doom_individually_hashes.end(); it != end; ++it) { const int doom_result = DoomEntryFromHash(*it, barrier_callback); - // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. - CHECK_EQ(net::ERR_IO_PENDING, doom_result); + DCHECK_EQ(net::ERR_IO_PENDING, doom_result); index_->Remove(*it); } @@ -328,13 +347,15 @@ void SimpleBackendImpl::DoomEntries(std::vector* entry_hashes, // base::Passed before mass_doom_entry_hashes.get(). std::vector* mass_doom_entry_hashes_ptr = mass_doom_entry_hashes.get(); - PostTaskAndReplyWithResult( - worker_pool_, FROM_HERE, - base::Bind(&SimpleSynchronousEntry::DoomEntrySet, - mass_doom_entry_hashes_ptr, path_), - base::Bind(&SimpleBackendImpl::DoomEntriesComplete, - AsWeakPtr(), base::Passed(&mass_doom_entry_hashes), - barrier_callback)); + PostTaskAndReplyWithResult(worker_pool_.get(), + FROM_HERE, + base::Bind(&SimpleSynchronousEntry::DoomEntrySet, + mass_doom_entry_hashes_ptr, + path_), + base::Bind(&SimpleBackendImpl::DoomEntriesComplete, + AsWeakPtr(), + base::Passed(&mass_doom_entry_hashes), + barrier_callback)); } net::CacheType SimpleBackendImpl::GetCacheType() const { @@ -446,20 +467,78 @@ int SimpleBackendImpl::DoomEntriesSince( return DoomEntriesBetween(initial_time, Time(), callback); } -int SimpleBackendImpl::OpenNextEntry(void** iter, - Entry** next_entry, - const CompletionCallback& callback) { - CompletionCallback get_next_entry = - base::Bind(&SimpleBackendImpl::GetNextEntryInIterator, AsWeakPtr(), iter, - next_entry, callback); - return index_->ExecuteWhenReady(get_next_entry); -} +class SimpleBackendImpl::SimpleIterator final : public Iterator { + public: + explicit SimpleIterator(base::WeakPtr backend) + : backend_(backend), + weak_factory_(this) { + } + + // From Backend::Iterator: + int OpenNextEntry(Entry** next_entry, + const CompletionCallback& callback) override { + CompletionCallback open_next_entry_impl = + base::Bind(&SimpleIterator::OpenNextEntryImpl, + weak_factory_.GetWeakPtr(), next_entry, callback); + return backend_->index_->ExecuteWhenReady(open_next_entry_impl); + } + + void OpenNextEntryImpl(Entry** next_entry, + const CompletionCallback& callback, + int index_initialization_error_code) { + if (!backend_) { + callback.Run(net::ERR_FAILED); + return; + } + if (index_initialization_error_code != net::OK) { + callback.Run(index_initialization_error_code); + return; + } + if (!hashes_to_enumerate_) + hashes_to_enumerate_ = backend_->index()->GetAllHashes().Pass(); + + while (!hashes_to_enumerate_->empty()) { + uint64 entry_hash = hashes_to_enumerate_->back(); + hashes_to_enumerate_->pop_back(); + if (backend_->index()->Has(entry_hash)) { + *next_entry = NULL; + CompletionCallback continue_iteration = base::Bind( + &SimpleIterator::CheckIterationReturnValue, + weak_factory_.GetWeakPtr(), + next_entry, + callback); + int error_code_open = backend_->OpenEntryFromHash(entry_hash, + next_entry, + continue_iteration); + if (error_code_open == net::ERR_IO_PENDING) + return; + if (error_code_open != net::ERR_FAILED) { + callback.Run(error_code_open); + return; + } + } + } + callback.Run(net::ERR_FAILED); + } + + void CheckIterationReturnValue(Entry** entry, + const CompletionCallback& callback, + int error_code) { + if (error_code == net::ERR_FAILED) { + OpenNextEntry(entry, callback); + return; + } + callback.Run(error_code); + } + + private: + base::WeakPtr backend_; + scoped_ptr > hashes_to_enumerate_; + base::WeakPtrFactory weak_factory_; +}; -void SimpleBackendImpl::EndEnumeration(void** iter) { - SimpleIndex::HashList* entry_list = - static_cast(*iter); - delete entry_list; - *iter = NULL; +scoped_ptr SimpleBackendImpl::CreateIterator() { + return scoped_ptr(new SimpleIterator(AsWeakPtr())); } void SimpleBackendImpl::GetStats( @@ -511,18 +590,17 @@ scoped_refptr SimpleBackendImpl::CreateOrFindActiveEntry( const std::string& key) { DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); std::pair insert_result = - active_entries_.insert(std::make_pair(entry_hash, - base::WeakPtr())); + active_entries_.insert(EntryMap::value_type(entry_hash, NULL)); EntryMap::iterator& it = insert_result.first; - if (insert_result.second) - DCHECK(!it->second.get()); - if (!it->second.get()) { - SimpleEntryImpl* entry = new SimpleEntryImpl( - cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); + const bool did_insert = insert_result.second; + if (did_insert) { + SimpleEntryImpl* entry = it->second = + new SimpleEntryImpl(cache_type_, path_, entry_hash, + entry_operations_mode_,this, net_log_); entry->SetKey(key); - it->second = entry->AsWeakPtr(); + entry->SetActiveEntryProxy(ActiveEntryProxy::Create(entry_hash, this)); } - DCHECK(it->second.get()); + DCHECK(it->second); // It's possible, but unlikely, that we have an entry hash collision with a // currently active entry. if (key != it->second->key()) { @@ -530,7 +608,7 @@ scoped_refptr SimpleBackendImpl::CreateOrFindActiveEntry( DCHECK_EQ(0U, active_entries_.count(entry_hash)); return CreateOrFindActiveEntry(entry_hash, key); } - return make_scoped_refptr(it->second.get()); + return make_scoped_refptr(it->second); } int SimpleBackendImpl::OpenEntryFromHash(uint64 entry_hash, @@ -588,49 +666,10 @@ int SimpleBackendImpl::DoomEntryFromHash(uint64 entry_hash, return net::ERR_IO_PENDING; } -void SimpleBackendImpl::GetNextEntryInIterator( - void** iter, - Entry** next_entry, - const CompletionCallback& callback, - int error_code) { - if (error_code != net::OK) { - callback.Run(error_code); - return; - } - if (*iter == NULL) { - *iter = index()->GetAllHashes().release(); - } - SimpleIndex::HashList* entry_list = - static_cast(*iter); - while (entry_list->size() > 0) { - uint64 entry_hash = entry_list->back(); - entry_list->pop_back(); - if (index()->Has(entry_hash)) { - *next_entry = NULL; - CompletionCallback continue_iteration = base::Bind( - &SimpleBackendImpl::CheckIterationReturnValue, - AsWeakPtr(), - iter, - next_entry, - callback); - int error_code_open = OpenEntryFromHash(entry_hash, - next_entry, - continue_iteration); - if (error_code_open == net::ERR_IO_PENDING) - return; - if (error_code_open != net::ERR_FAILED) { - callback.Run(error_code_open); - return; - } - } - } - callback.Run(net::ERR_FAILED); -} - void SimpleBackendImpl::OnEntryOpenedFromHash( uint64 hash, Entry** entry, - scoped_refptr simple_entry, + const scoped_refptr& simple_entry, const CompletionCallback& callback, int error_code) { if (error_code != net::OK) { @@ -639,19 +678,19 @@ void SimpleBackendImpl::OnEntryOpenedFromHash( } DCHECK(*entry); std::pair insert_result = - active_entries_.insert(std::make_pair(hash, - base::WeakPtr())); + active_entries_.insert(EntryMap::value_type(hash, simple_entry.get())); EntryMap::iterator& it = insert_result.first; const bool did_insert = insert_result.second; if (did_insert) { - // There is no active entry corresponding to this hash. The entry created - // is put in the map of active entries and returned to the caller. - it->second = simple_entry->AsWeakPtr(); - callback.Run(error_code); + // There was no active entry corresponding to this hash. We've already put + // the entry opened from hash in the |active_entries_|. We now provide the + // proxy object to the entry. + it->second->SetActiveEntryProxy(ActiveEntryProxy::Create(hash, this)); + callback.Run(net::OK); } else { - // The entry was made active with the key while the creation from hash - // occurred. The entry created from hash needs to be closed, and the one - // coming from the key returned to the caller. + // The entry was made active while we waiting for the open from hash to + // finish. The entry created from hash needs to be closed, and the one + // in |active_entries_| can be returned to the caller. simple_entry->Close(); it->second->OpenEntry(entry, callback); } @@ -660,7 +699,7 @@ void SimpleBackendImpl::OnEntryOpenedFromHash( void SimpleBackendImpl::OnEntryOpenedFromKey( const std::string key, Entry** entry, - scoped_refptr simple_entry, + const scoped_refptr& simple_entry, const CompletionCallback& callback, int error_code) { int final_code = error_code; @@ -680,18 +719,6 @@ void SimpleBackendImpl::OnEntryOpenedFromKey( callback.Run(final_code); } -void SimpleBackendImpl::CheckIterationReturnValue( - void** iter, - Entry** entry, - const CompletionCallback& callback, - int error_code) { - if (error_code == net::ERR_FAILED) { - OpenNextEntry(iter, entry, callback); - return; - } - callback.Run(error_code); -} - void SimpleBackendImpl::DoomEntriesComplete( scoped_ptr > entry_hashes, const net::CompletionCallback& callback, -- cgit v1.2.1