| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/simple/simple_backend_impl.h" | 5 #include "net/disk_cache/simple/simple_backend_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstdlib> | 8 #include <cstdlib> |
| 9 #include <functional> | 9 #include <functional> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" |
| 12 |
| 11 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 12 #include <sys/resource.h> | 14 #include <sys/resource.h> |
| 13 #endif | 15 #endif |
| 14 | 16 |
| 15 #include "base/bind.h" | 17 #include "base/bind.h" |
| 16 #include "base/callback.h" | 18 #include "base/callback.h" |
| 17 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
| 18 #include "base/lazy_instance.h" | 20 #include "base/lazy_instance.h" |
| 19 #include "base/location.h" | 21 #include "base/location.h" |
| 20 #include "base/macros.h" | 22 #include "base/macros.h" |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 class SimpleBackendImpl::ActiveEntryProxy | 204 class SimpleBackendImpl::ActiveEntryProxy |
| 203 : public SimpleEntryImpl::ActiveEntryProxy { | 205 : public SimpleEntryImpl::ActiveEntryProxy { |
| 204 public: | 206 public: |
| 205 ~ActiveEntryProxy() override { | 207 ~ActiveEntryProxy() override { |
| 206 if (backend_) { | 208 if (backend_) { |
| 207 DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_)); | 209 DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_)); |
| 208 backend_->active_entries_.erase(entry_hash_); | 210 backend_->active_entries_.erase(entry_hash_); |
| 209 } | 211 } |
| 210 } | 212 } |
| 211 | 213 |
| 212 static scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> Create( | 214 static std::unique_ptr<SimpleEntryImpl::ActiveEntryProxy> Create( |
| 213 int64_t entry_hash, | 215 int64_t entry_hash, |
| 214 SimpleBackendImpl* backend) { | 216 SimpleBackendImpl* backend) { |
| 215 scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> | 217 std::unique_ptr<SimpleEntryImpl::ActiveEntryProxy> proxy( |
| 216 proxy(new ActiveEntryProxy(entry_hash, backend)); | 218 new ActiveEntryProxy(entry_hash, backend)); |
| 217 return proxy; | 219 return proxy; |
| 218 } | 220 } |
| 219 | 221 |
| 220 private: | 222 private: |
| 221 ActiveEntryProxy(uint64_t entry_hash, SimpleBackendImpl* backend) | 223 ActiveEntryProxy(uint64_t entry_hash, SimpleBackendImpl* backend) |
| 222 : entry_hash_(entry_hash), backend_(backend->AsWeakPtr()) {} | 224 : entry_hash_(entry_hash), backend_(backend->AsWeakPtr()) {} |
| 223 | 225 |
| 224 uint64_t entry_hash_; | 226 uint64_t entry_hash_; |
| 225 base::WeakPtr<SimpleBackendImpl> backend_; | 227 base::WeakPtr<SimpleBackendImpl> backend_; |
| 226 }; | 228 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 243 } | 245 } |
| 244 | 246 |
| 245 SimpleBackendImpl::~SimpleBackendImpl() { | 247 SimpleBackendImpl::~SimpleBackendImpl() { |
| 246 index_->WriteToDisk(); | 248 index_->WriteToDisk(); |
| 247 } | 249 } |
| 248 | 250 |
| 249 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) { | 251 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) { |
| 250 worker_pool_ = g_sequenced_worker_pool.Get().GetTaskRunner(); | 252 worker_pool_ = g_sequenced_worker_pool.Get().GetTaskRunner(); |
| 251 | 253 |
| 252 index_.reset(new SimpleIndex( | 254 index_.reset(new SimpleIndex( |
| 253 base::ThreadTaskRunnerHandle::Get(), | 255 base::ThreadTaskRunnerHandle::Get(), this, cache_type_, |
| 254 this, | 256 base::WrapUnique(new SimpleIndexFile(cache_thread_, worker_pool_.get(), |
| 255 cache_type_, | 257 cache_type_, path_)))); |
| 256 make_scoped_ptr(new SimpleIndexFile( | |
| 257 cache_thread_, worker_pool_.get(), cache_type_, path_)))); | |
| 258 index_->ExecuteWhenReady( | 258 index_->ExecuteWhenReady( |
| 259 base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now())); | 259 base::Bind(&RecordIndexLoad, cache_type_, base::TimeTicks::Now())); |
| 260 | 260 |
| 261 PostTaskAndReplyWithResult( | 261 PostTaskAndReplyWithResult( |
| 262 cache_thread_.get(), | 262 cache_thread_.get(), |
| 263 FROM_HERE, | 263 FROM_HERE, |
| 264 base::Bind( | 264 base::Bind( |
| 265 &SimpleBackendImpl::InitCacheStructureOnDisk, path_, orig_max_size_), | 265 &SimpleBackendImpl::InitCacheStructureOnDisk, path_, orig_max_size_), |
| 266 base::Bind(&SimpleBackendImpl::InitializeIndex, | 266 base::Bind(&SimpleBackendImpl::InitializeIndex, |
| 267 AsWeakPtr(), | 267 AsWeakPtr(), |
| (...skipping 26 matching lines...) Expand all Loading... |
| 294 std::vector<Closure> to_run_closures; | 294 std::vector<Closure> to_run_closures; |
| 295 to_run_closures.swap(it->second); | 295 to_run_closures.swap(it->second); |
| 296 entries_pending_doom_.erase(it); | 296 entries_pending_doom_.erase(it); |
| 297 | 297 |
| 298 for (auto& closure : to_run_closures) | 298 for (auto& closure : to_run_closures) |
| 299 closure.Run(); | 299 closure.Run(); |
| 300 } | 300 } |
| 301 | 301 |
| 302 void SimpleBackendImpl::DoomEntries(std::vector<uint64_t>* entry_hashes, | 302 void SimpleBackendImpl::DoomEntries(std::vector<uint64_t>* entry_hashes, |
| 303 const net::CompletionCallback& callback) { | 303 const net::CompletionCallback& callback) { |
| 304 scoped_ptr<std::vector<uint64_t>> mass_doom_entry_hashes( | 304 std::unique_ptr<std::vector<uint64_t>> mass_doom_entry_hashes( |
| 305 new std::vector<uint64_t>()); | 305 new std::vector<uint64_t>()); |
| 306 mass_doom_entry_hashes->swap(*entry_hashes); | 306 mass_doom_entry_hashes->swap(*entry_hashes); |
| 307 | 307 |
| 308 std::vector<uint64_t> to_doom_individually_hashes; | 308 std::vector<uint64_t> to_doom_individually_hashes; |
| 309 | 309 |
| 310 // For each of the entry hashes, there are two cases: | 310 // For each of the entry hashes, there are two cases: |
| 311 // 1. The entry is either open or pending doom, and so it should be doomed | 311 // 1. The entry is either open or pending doom, and so it should be doomed |
| 312 // individually to avoid flakes. | 312 // individually to avoid flakes. |
| 313 // 2. The entry is not in use at all, so we can call | 313 // 2. The entry is not in use at all, so we can call |
| 314 // SimpleSynchronousEntry::DoomEntrySet and delete the files en masse. | 314 // SimpleSynchronousEntry::DoomEntrySet and delete the files en masse. |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 int error_code) { | 523 int error_code) { |
| 524 if (error_code == net::ERR_FAILED) { | 524 if (error_code == net::ERR_FAILED) { |
| 525 OpenNextEntry(entry, callback); | 525 OpenNextEntry(entry, callback); |
| 526 return; | 526 return; |
| 527 } | 527 } |
| 528 callback.Run(error_code); | 528 callback.Run(error_code); |
| 529 } | 529 } |
| 530 | 530 |
| 531 private: | 531 private: |
| 532 base::WeakPtr<SimpleBackendImpl> backend_; | 532 base::WeakPtr<SimpleBackendImpl> backend_; |
| 533 scoped_ptr<std::vector<uint64_t>> hashes_to_enumerate_; | 533 std::unique_ptr<std::vector<uint64_t>> hashes_to_enumerate_; |
| 534 base::WeakPtrFactory<SimpleIterator> weak_factory_; | 534 base::WeakPtrFactory<SimpleIterator> weak_factory_; |
| 535 }; | 535 }; |
| 536 | 536 |
| 537 scoped_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { | 537 std::unique_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { |
| 538 return scoped_ptr<Iterator>(new SimpleIterator(AsWeakPtr())); | 538 return std::unique_ptr<Iterator>(new SimpleIterator(AsWeakPtr())); |
| 539 } | 539 } |
| 540 | 540 |
| 541 void SimpleBackendImpl::GetStats(base::StringPairs* stats) { | 541 void SimpleBackendImpl::GetStats(base::StringPairs* stats) { |
| 542 std::pair<std::string, std::string> item; | 542 std::pair<std::string, std::string> item; |
| 543 item.first = "Cache type"; | 543 item.first = "Cache type"; |
| 544 item.second = "Simple Cache"; | 544 item.second = "Simple Cache"; |
| 545 stats->push_back(item); | 545 stats->push_back(item); |
| 546 } | 546 } |
| 547 | 547 |
| 548 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { | 548 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 559 } | 559 } |
| 560 | 560 |
| 561 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, | 561 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, |
| 562 Time end_time, | 562 Time end_time, |
| 563 const CompletionCallback& callback, | 563 const CompletionCallback& callback, |
| 564 int result) { | 564 int result) { |
| 565 if (result != net::OK) { | 565 if (result != net::OK) { |
| 566 callback.Run(result); | 566 callback.Run(result); |
| 567 return; | 567 return; |
| 568 } | 568 } |
| 569 scoped_ptr<std::vector<uint64_t>> removed_key_hashes( | 569 std::unique_ptr<std::vector<uint64_t>> removed_key_hashes( |
| 570 index_->GetEntriesBetween(initial_time, end_time).release()); | 570 index_->GetEntriesBetween(initial_time, end_time).release()); |
| 571 DoomEntries(removed_key_hashes.get(), callback); | 571 DoomEntries(removed_key_hashes.get(), callback); |
| 572 } | 572 } |
| 573 | 573 |
| 574 void SimpleBackendImpl::IndexReadyForSizeCalculation( | 574 void SimpleBackendImpl::IndexReadyForSizeCalculation( |
| 575 const CompletionCallback& callback, | 575 const CompletionCallback& callback, |
| 576 int result) { | 576 int result) { |
| 577 if (result == net::OK) | 577 if (result == net::OK) |
| 578 result = static_cast<int>(index_->GetCacheSize()); | 578 result = static_cast<int>(index_->GetCacheSize()); |
| 579 callback.Run(result); | 579 callback.Run(result); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); | 651 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); |
| 652 CompletionCallback backend_callback = | 652 CompletionCallback backend_callback = |
| 653 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromHash, | 653 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromHash, |
| 654 AsWeakPtr(), entry_hash, entry, simple_entry, callback); | 654 AsWeakPtr(), entry_hash, entry, simple_entry, callback); |
| 655 return simple_entry->OpenEntry(entry, backend_callback); | 655 return simple_entry->OpenEntry(entry, backend_callback); |
| 656 } | 656 } |
| 657 | 657 |
| 658 int SimpleBackendImpl::DoomEntryFromHash(uint64_t entry_hash, | 658 int SimpleBackendImpl::DoomEntryFromHash(uint64_t entry_hash, |
| 659 const CompletionCallback& callback) { | 659 const CompletionCallback& callback) { |
| 660 Entry** entry = new Entry*(); | 660 Entry** entry = new Entry*(); |
| 661 scoped_ptr<Entry*> scoped_entry(entry); | 661 std::unique_ptr<Entry*> scoped_entry(entry); |
| 662 | 662 |
| 663 base::hash_map<uint64_t, std::vector<Closure>>::iterator pending_it = | 663 base::hash_map<uint64_t, std::vector<Closure>>::iterator pending_it = |
| 664 entries_pending_doom_.find(entry_hash); | 664 entries_pending_doom_.find(entry_hash); |
| 665 if (pending_it != entries_pending_doom_.end()) { | 665 if (pending_it != entries_pending_doom_.end()) { |
| 666 Callback<int(const net::CompletionCallback&)> operation = | 666 Callback<int(const net::CompletionCallback&)> operation = |
| 667 base::Bind(&SimpleBackendImpl::DoomEntryFromHash, | 667 base::Bind(&SimpleBackendImpl::DoomEntryFromHash, |
| 668 base::Unretained(this), entry_hash); | 668 base::Unretained(this), entry_hash); |
| 669 pending_it->second.push_back(base::Bind(&RunOperationAndCallback, | 669 pending_it->second.push_back(base::Bind(&RunOperationAndCallback, |
| 670 operation, callback)); | 670 operation, callback)); |
| 671 return net::ERR_IO_PENDING; | 671 return net::ERR_IO_PENDING; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 final_code = net::ERR_FAILED; | 730 final_code = net::ERR_FAILED; |
| 731 } else { | 731 } else { |
| 732 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | 732 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); |
| 733 } | 733 } |
| 734 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | 734 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); |
| 735 } | 735 } |
| 736 callback.Run(final_code); | 736 callback.Run(final_code); |
| 737 } | 737 } |
| 738 | 738 |
| 739 void SimpleBackendImpl::DoomEntriesComplete( | 739 void SimpleBackendImpl::DoomEntriesComplete( |
| 740 scoped_ptr<std::vector<uint64_t>> entry_hashes, | 740 std::unique_ptr<std::vector<uint64_t>> entry_hashes, |
| 741 const net::CompletionCallback& callback, | 741 const net::CompletionCallback& callback, |
| 742 int result) { | 742 int result) { |
| 743 for (const uint64_t& entry_hash : *entry_hashes) | 743 for (const uint64_t& entry_hash : *entry_hashes) |
| 744 OnDoomComplete(entry_hash); | 744 OnDoomComplete(entry_hash); |
| 745 callback.Run(result); | 745 callback.Run(result); |
| 746 } | 746 } |
| 747 | 747 |
| 748 // static | 748 // static |
| 749 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 749 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
| 750 // We only need to do this if we there is an active task runner. | 750 // We only need to do this if we there is an active task runner. |
| 751 if (base::ThreadTaskRunnerHandle::IsSet()) | 751 if (base::ThreadTaskRunnerHandle::IsSet()) |
| 752 g_sequenced_worker_pool.Get().FlushForTesting(); | 752 g_sequenced_worker_pool.Get().FlushForTesting(); |
| 753 } | 753 } |
| 754 | 754 |
| 755 } // namespace disk_cache | 755 } // namespace disk_cache |
| OLD | NEW |