| 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 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 if (result == net::OK) { | 188 if (result == net::OK) { |
| 189 SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index); | 189 SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index); |
| 190 } else { | 190 } else { |
| 191 SIMPLE_CACHE_UMA(TIMES, | 191 SIMPLE_CACHE_UMA(TIMES, |
| 192 "CreationToIndexFail", cache_type, creation_to_index); | 192 "CreationToIndexFail", cache_type, creation_to_index); |
| 193 } | 193 } |
| 194 } | 194 } |
| 195 | 195 |
| 196 } // namespace | 196 } // namespace |
| 197 | 197 |
| 198 class SimpleBackendImpl::ActiveEntryProxy |
| 199 : public SimpleEntryImpl::ActiveEntryProxy { |
| 200 public: |
| 201 virtual ~ActiveEntryProxy() { |
| 202 if (backend_) { |
| 203 DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_)); |
| 204 backend_->active_entries_.erase(entry_hash_); |
| 205 } |
| 206 } |
| 207 |
| 208 static scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> Create( |
| 209 int64 entry_hash, |
| 210 SimpleBackendImpl* backend) { |
| 211 scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> |
| 212 proxy(new ActiveEntryProxy(entry_hash, backend)); |
| 213 return proxy.Pass(); |
| 214 } |
| 215 |
| 216 private: |
| 217 ActiveEntryProxy(uint64 entry_hash, |
| 218 SimpleBackendImpl* backend) |
| 219 : entry_hash_(entry_hash), |
| 220 backend_(backend->AsWeakPtr()) {} |
| 221 |
| 222 uint64 entry_hash_; |
| 223 base::WeakPtr<SimpleBackendImpl> backend_; |
| 224 }; |
| 225 |
| 198 SimpleBackendImpl::SimpleBackendImpl( | 226 SimpleBackendImpl::SimpleBackendImpl( |
| 199 const FilePath& path, | 227 const FilePath& path, |
| 200 int max_bytes, | 228 int max_bytes, |
| 201 net::CacheType cache_type, | 229 net::CacheType cache_type, |
| 202 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, | 230 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, |
| 203 net::NetLog* net_log) | 231 net::NetLog* net_log) |
| 204 : path_(path), | 232 : path_(path), |
| 205 cache_type_(cache_type), | 233 cache_type_(cache_type), |
| 206 cache_thread_(cache_thread), | 234 cache_thread_(cache_thread), |
| 207 orig_max_size_(max_bytes), | 235 orig_max_size_(max_bytes), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 272 |
| 245 bool SimpleBackendImpl::SetMaxSize(int max_bytes) { | 273 bool SimpleBackendImpl::SetMaxSize(int max_bytes) { |
| 246 orig_max_size_ = max_bytes; | 274 orig_max_size_ = max_bytes; |
| 247 return index_->SetMaxSize(max_bytes); | 275 return index_->SetMaxSize(max_bytes); |
| 248 } | 276 } |
| 249 | 277 |
| 250 int SimpleBackendImpl::GetMaxFileSize() const { | 278 int SimpleBackendImpl::GetMaxFileSize() const { |
| 251 return index_->max_size() / kMaxFileRatio; | 279 return index_->max_size() / kMaxFileRatio; |
| 252 } | 280 } |
| 253 | 281 |
| 254 void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) { | |
| 255 active_entries_.erase(entry->entry_hash()); | |
| 256 } | |
| 257 | |
| 258 void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) { | 282 void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) { |
| 259 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. | 283 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. |
| 260 CHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); | 284 CHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); |
| 261 entries_pending_doom_.insert( | 285 entries_pending_doom_.insert( |
| 262 std::make_pair(entry_hash, std::vector<Closure>())); | 286 std::make_pair(entry_hash, std::vector<Closure>())); |
| 263 } | 287 } |
| 264 | 288 |
| 265 void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) { | 289 void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) { |
| 266 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. | 290 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. |
| 267 CHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); | 291 CHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 DCHECK(result.max_size); | 529 DCHECK(result.max_size); |
| 506 } | 530 } |
| 507 return result; | 531 return result; |
| 508 } | 532 } |
| 509 | 533 |
| 510 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( | 534 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( |
| 511 const uint64 entry_hash, | 535 const uint64 entry_hash, |
| 512 const std::string& key) { | 536 const std::string& key) { |
| 513 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); | 537 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); |
| 514 std::pair<EntryMap::iterator, bool> insert_result = | 538 std::pair<EntryMap::iterator, bool> insert_result = |
| 515 active_entries_.insert(std::make_pair(entry_hash, | 539 active_entries_.insert(EntryMap::value_type(entry_hash, NULL)); |
| 516 base::WeakPtr<SimpleEntryImpl>())); | |
| 517 EntryMap::iterator& it = insert_result.first; | 540 EntryMap::iterator& it = insert_result.first; |
| 518 if (insert_result.second) | 541 const bool did_insert = insert_result.second; |
| 519 DCHECK(!it->second.get()); | 542 if (did_insert) { |
| 520 if (!it->second.get()) { | 543 SimpleEntryImpl* entry = it->second = |
| 521 SimpleEntryImpl* entry = new SimpleEntryImpl( | 544 new SimpleEntryImpl(cache_type_, path_, entry_hash, |
| 522 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); | 545 entry_operations_mode_,this, net_log_); |
| 523 entry->SetKey(key); | 546 entry->SetKey(key); |
| 524 it->second = entry->AsWeakPtr(); | 547 entry->SetActiveEntryProxy(ActiveEntryProxy::Create(entry_hash, this)); |
| 525 } | 548 } |
| 526 DCHECK(it->second.get()); | 549 DCHECK(it->second); |
| 527 // It's possible, but unlikely, that we have an entry hash collision with a | 550 // It's possible, but unlikely, that we have an entry hash collision with a |
| 528 // currently active entry. | 551 // currently active entry. |
| 529 if (key != it->second->key()) { | 552 if (key != it->second->key()) { |
| 530 it->second->Doom(); | 553 it->second->Doom(); |
| 531 DCHECK_EQ(0U, active_entries_.count(entry_hash)); | 554 DCHECK_EQ(0U, active_entries_.count(entry_hash)); |
| 532 return CreateOrFindActiveEntry(entry_hash, key); | 555 return CreateOrFindActiveEntry(entry_hash, key); |
| 533 } | 556 } |
| 534 return make_scoped_refptr(it->second.get()); | 557 return make_scoped_refptr(it->second); |
| 535 } | 558 } |
| 536 | 559 |
| 537 int SimpleBackendImpl::OpenEntryFromHash(uint64 entry_hash, | 560 int SimpleBackendImpl::OpenEntryFromHash(uint64 entry_hash, |
| 538 Entry** entry, | 561 Entry** entry, |
| 539 const CompletionCallback& callback) { | 562 const CompletionCallback& callback) { |
| 540 base::hash_map<uint64, std::vector<Closure> >::iterator it = | 563 base::hash_map<uint64, std::vector<Closure> >::iterator it = |
| 541 entries_pending_doom_.find(entry_hash); | 564 entries_pending_doom_.find(entry_hash); |
| 542 if (it != entries_pending_doom_.end()) { | 565 if (it != entries_pending_doom_.end()) { |
| 543 Callback<int(const net::CompletionCallback&)> operation = | 566 Callback<int(const net::CompletionCallback&)> operation = |
| 544 base::Bind(&SimpleBackendImpl::OpenEntryFromHash, | 567 base::Bind(&SimpleBackendImpl::OpenEntryFromHash, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 Entry** entry, | 656 Entry** entry, |
| 634 scoped_refptr<SimpleEntryImpl> simple_entry, | 657 scoped_refptr<SimpleEntryImpl> simple_entry, |
| 635 const CompletionCallback& callback, | 658 const CompletionCallback& callback, |
| 636 int error_code) { | 659 int error_code) { |
| 637 if (error_code != net::OK) { | 660 if (error_code != net::OK) { |
| 638 callback.Run(error_code); | 661 callback.Run(error_code); |
| 639 return; | 662 return; |
| 640 } | 663 } |
| 641 DCHECK(*entry); | 664 DCHECK(*entry); |
| 642 std::pair<EntryMap::iterator, bool> insert_result = | 665 std::pair<EntryMap::iterator, bool> insert_result = |
| 643 active_entries_.insert(std::make_pair(hash, | 666 active_entries_.insert(EntryMap::value_type(hash, simple_entry)); |
| 644 base::WeakPtr<SimpleEntryImpl>())); | |
| 645 EntryMap::iterator& it = insert_result.first; | 667 EntryMap::iterator& it = insert_result.first; |
| 646 const bool did_insert = insert_result.second; | 668 const bool did_insert = insert_result.second; |
| 647 if (did_insert) { | 669 if (did_insert) { |
| 648 // There is no active entry corresponding to this hash. The entry created | 670 // There was no active entry corresponding to this hash. We've already put |
| 649 // is put in the map of active entries and returned to the caller. | 671 // the entry opened from hash in the |active_entries_|. We now provide the |
| 650 it->second = simple_entry->AsWeakPtr(); | 672 // proxy object to the entry. |
| 651 callback.Run(error_code); | 673 it->second->SetActiveEntryProxy(ActiveEntryProxy::Create(hash, this)); |
| 674 callback.Run(net::OK); |
| 652 } else { | 675 } else { |
| 653 // The entry was made active with the key while the creation from hash | 676 // The entry was made active while we waiting for the open from hash to |
| 654 // occurred. The entry created from hash needs to be closed, and the one | 677 // finish. The entry created from hash needs to be closed, and the one |
| 655 // coming from the key returned to the caller. | 678 // in |active_entries_| can be returned to the caller. |
| 656 simple_entry->Close(); | 679 simple_entry->Close(); |
| 657 it->second->OpenEntry(entry, callback); | 680 it->second->OpenEntry(entry, callback); |
| 658 } | 681 } |
| 659 } | 682 } |
| 660 | 683 |
| 661 void SimpleBackendImpl::OnEntryOpenedFromKey( | 684 void SimpleBackendImpl::OnEntryOpenedFromKey( |
| 662 const std::string key, | 685 const std::string key, |
| 663 Entry** entry, | 686 Entry** entry, |
| 664 scoped_refptr<SimpleEntryImpl> simple_entry, | 687 scoped_refptr<SimpleEntryImpl> simple_entry, |
| 665 const CompletionCallback& callback, | 688 const CompletionCallback& callback, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 this)); | 726 this)); |
| 704 callback.Run(result); | 727 callback.Run(result); |
| 705 } | 728 } |
| 706 | 729 |
| 707 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 730 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
| 708 if (g_sequenced_worker_pool) | 731 if (g_sequenced_worker_pool) |
| 709 g_sequenced_worker_pool->FlushForTesting(); | 732 g_sequenced_worker_pool->FlushForTesting(); |
| 710 } | 733 } |
| 711 | 734 |
| 712 } // namespace disk_cache | 735 } // namespace disk_cache |
| OLD | NEW |