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 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 base::Bind(&SimpleBackendImpl::IndexReadyForDoom, AsWeakPtr(), | 466 base::Bind(&SimpleBackendImpl::IndexReadyForDoom, AsWeakPtr(), |
467 initial_time, end_time, callback)); | 467 initial_time, end_time, callback)); |
468 } | 468 } |
469 | 469 |
470 int SimpleBackendImpl::DoomEntriesSince( | 470 int SimpleBackendImpl::DoomEntriesSince( |
471 const Time initial_time, | 471 const Time initial_time, |
472 const CompletionCallback& callback) { | 472 const CompletionCallback& callback) { |
473 return DoomEntriesBetween(initial_time, Time(), callback); | 473 return DoomEntriesBetween(initial_time, Time(), callback); |
474 } | 474 } |
475 | 475 |
476 int SimpleBackendImpl::OpenNextEntry(void** iter, | 476 class SimpleBackendImpl::SimpleIterator FINAL : public Iterator { |
477 Entry** next_entry, | 477 public: |
478 const CompletionCallback& callback) { | 478 explicit SimpleIterator(base::WeakPtr<SimpleBackendImpl> backend) |
479 CompletionCallback get_next_entry = | 479 : backend_(backend), |
480 base::Bind(&SimpleBackendImpl::GetNextEntryInIterator, AsWeakPtr(), iter, | 480 weak_factory_(this) { |
481 next_entry, callback); | 481 } |
482 return index_->ExecuteWhenReady(get_next_entry); | |
483 } | |
484 | 482 |
485 void SimpleBackendImpl::EndEnumeration(void** iter) { | 483 // From Backend::Iterator: |
486 active_enumerations_.Remove(IteratorToEnumerationId(iter)); | 484 virtual int OpenNextEntry(Entry** next_entry, |
487 *iter = NULL; | 485 const CompletionCallback& callback) OVERRIDE { |
| 486 CompletionCallback open_next_entry_impl = |
| 487 base::Bind(&SimpleIterator::OpenNextEntryImpl, |
| 488 weak_factory_.GetWeakPtr(), next_entry, callback); |
| 489 return backend_->index_->ExecuteWhenReady(open_next_entry_impl); |
| 490 } |
| 491 |
| 492 void OpenNextEntryImpl(Entry** next_entry, |
| 493 const CompletionCallback& callback, |
| 494 int index_initialization_error_code) { |
| 495 if (!backend_) { |
| 496 callback.Run(net::ERR_FAILED); |
| 497 return; |
| 498 } |
| 499 if (index_initialization_error_code != net::OK) { |
| 500 callback.Run(index_initialization_error_code); |
| 501 return; |
| 502 } |
| 503 if (!hashes_to_enumerate_) |
| 504 hashes_to_enumerate_ = backend_->index()->GetAllHashes().Pass(); |
| 505 |
| 506 while (!hashes_to_enumerate_->empty()) { |
| 507 uint64 entry_hash = hashes_to_enumerate_->back(); |
| 508 hashes_to_enumerate_->pop_back(); |
| 509 if (backend_->index()->Has(entry_hash)) { |
| 510 *next_entry = NULL; |
| 511 CompletionCallback continue_iteration = base::Bind( |
| 512 &SimpleIterator::CheckIterationReturnValue, |
| 513 weak_factory_.GetWeakPtr(), |
| 514 next_entry, |
| 515 callback); |
| 516 int error_code_open = backend_->OpenEntryFromHash(entry_hash, |
| 517 next_entry, |
| 518 continue_iteration); |
| 519 if (error_code_open == net::ERR_IO_PENDING) |
| 520 return; |
| 521 if (error_code_open != net::ERR_FAILED) { |
| 522 callback.Run(error_code_open); |
| 523 return; |
| 524 } |
| 525 } |
| 526 } |
| 527 callback.Run(net::ERR_FAILED); |
| 528 } |
| 529 |
| 530 void CheckIterationReturnValue(Entry** entry, |
| 531 const CompletionCallback& callback, |
| 532 int error_code) { |
| 533 if (error_code == net::ERR_FAILED) { |
| 534 OpenNextEntry(entry, callback); |
| 535 return; |
| 536 } |
| 537 callback.Run(error_code); |
| 538 } |
| 539 |
| 540 private: |
| 541 base::WeakPtr<SimpleBackendImpl> backend_; |
| 542 scoped_ptr<std::vector<uint64> > hashes_to_enumerate_; |
| 543 base::WeakPtrFactory<SimpleIterator> weak_factory_; |
| 544 }; |
| 545 |
| 546 scoped_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { |
| 547 return scoped_ptr<Iterator>(new SimpleIterator(AsWeakPtr())); |
488 } | 548 } |
489 | 549 |
490 void SimpleBackendImpl::GetStats( | 550 void SimpleBackendImpl::GetStats( |
491 std::vector<std::pair<std::string, std::string> >* stats) { | 551 std::vector<std::pair<std::string, std::string> >* stats) { |
492 std::pair<std::string, std::string> item; | 552 std::pair<std::string, std::string> item; |
493 item.first = "Cache type"; | 553 item.first = "Cache type"; |
494 item.second = "Simple Cache"; | 554 item.second = "Simple Cache"; |
495 stats->push_back(item); | 555 stats->push_back(item); |
496 } | 556 } |
497 | 557 |
498 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { | 558 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { |
499 index_->UseIfExists(simple_util::GetEntryHashKey(key)); | 559 index_->UseIfExists(simple_util::GetEntryHashKey(key)); |
500 } | 560 } |
501 | 561 |
502 // static | |
503 SimpleBackendImpl::ActiveEnumerationMap::KeyType | |
504 SimpleBackendImpl::IteratorToEnumerationId(void** iter) { | |
505 COMPILE_ASSERT(sizeof(ptrdiff_t) >= sizeof(*iter), | |
506 integer_type_must_fit_ptr_type_for_cast_to_be_reversible); | |
507 const ptrdiff_t ptrdiff_enumeration_id = reinterpret_cast<ptrdiff_t>(*iter); | |
508 const ActiveEnumerationMap::KeyType enumeration_id = ptrdiff_enumeration_id; | |
509 DCHECK_EQ(enumeration_id, ptrdiff_enumeration_id); | |
510 return enumeration_id; | |
511 } | |
512 | |
513 // static | |
514 void* SimpleBackendImpl::EnumerationIdToIterator( | |
515 ActiveEnumerationMap::KeyType enumeration_id) { | |
516 const ptrdiff_t ptrdiff_enumeration_id = enumeration_id; | |
517 DCHECK_EQ(enumeration_id, ptrdiff_enumeration_id); | |
518 COMPILE_ASSERT(sizeof(ptrdiff_t) >= sizeof(void*), | |
519 integer_type_must_fit_ptr_type_for_cast_to_be_reversible); | |
520 return reinterpret_cast<void*>(ptrdiff_enumeration_id); | |
521 } | |
522 | |
523 void SimpleBackendImpl::InitializeIndex(const CompletionCallback& callback, | 562 void SimpleBackendImpl::InitializeIndex(const CompletionCallback& callback, |
524 const DiskStatResult& result) { | 563 const DiskStatResult& result) { |
525 if (result.net_error == net::OK) { | 564 if (result.net_error == net::OK) { |
526 index_->SetMaxSize(result.max_size); | 565 index_->SetMaxSize(result.max_size); |
527 index_->Initialize(result.cache_dir_mtime); | 566 index_->Initialize(result.cache_dir_mtime); |
528 } | 567 } |
529 callback.Run(result.net_error); | 568 callback.Run(result.net_error); |
530 } | 569 } |
531 | 570 |
532 SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( | 571 SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 return active_it->second->DoomEntry(callback); | 665 return active_it->second->DoomEntry(callback); |
627 | 666 |
628 // There's no pending dooms, nor any open entry. We can make a trivial | 667 // There's no pending dooms, nor any open entry. We can make a trivial |
629 // call to DoomEntries() to delete this entry. | 668 // call to DoomEntries() to delete this entry. |
630 std::vector<uint64> entry_hash_vector; | 669 std::vector<uint64> entry_hash_vector; |
631 entry_hash_vector.push_back(entry_hash); | 670 entry_hash_vector.push_back(entry_hash); |
632 DoomEntries(&entry_hash_vector, callback); | 671 DoomEntries(&entry_hash_vector, callback); |
633 return net::ERR_IO_PENDING; | 672 return net::ERR_IO_PENDING; |
634 } | 673 } |
635 | 674 |
636 void SimpleBackendImpl::GetNextEntryInIterator( | |
637 void** iter, | |
638 Entry** next_entry, | |
639 const CompletionCallback& callback, | |
640 int error_code) { | |
641 if (error_code != net::OK) { | |
642 callback.Run(error_code); | |
643 return; | |
644 } | |
645 std::vector<uint64>* entry_list = NULL; | |
646 if (*iter == NULL) { | |
647 const ActiveEnumerationMap::KeyType new_enumeration_id = | |
648 active_enumerations_.Add( | |
649 entry_list = index()->GetAllHashes().release()); | |
650 *iter = EnumerationIdToIterator(new_enumeration_id); | |
651 } else { | |
652 entry_list = active_enumerations_.Lookup(IteratorToEnumerationId(iter)); | |
653 } | |
654 while (entry_list->size() > 0) { | |
655 uint64 entry_hash = entry_list->back(); | |
656 entry_list->pop_back(); | |
657 if (index()->Has(entry_hash)) { | |
658 *next_entry = NULL; | |
659 CompletionCallback continue_iteration = base::Bind( | |
660 &SimpleBackendImpl::CheckIterationReturnValue, | |
661 AsWeakPtr(), | |
662 iter, | |
663 next_entry, | |
664 callback); | |
665 int error_code_open = OpenEntryFromHash(entry_hash, | |
666 next_entry, | |
667 continue_iteration); | |
668 if (error_code_open == net::ERR_IO_PENDING) | |
669 return; | |
670 if (error_code_open != net::ERR_FAILED) { | |
671 callback.Run(error_code_open); | |
672 return; | |
673 } | |
674 } | |
675 } | |
676 callback.Run(net::ERR_FAILED); | |
677 } | |
678 | |
679 void SimpleBackendImpl::OnEntryOpenedFromHash( | 675 void SimpleBackendImpl::OnEntryOpenedFromHash( |
680 uint64 hash, | 676 uint64 hash, |
681 Entry** entry, | 677 Entry** entry, |
682 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 678 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
683 const CompletionCallback& callback, | 679 const CompletionCallback& callback, |
684 int error_code) { | 680 int error_code) { |
685 if (error_code != net::OK) { | 681 if (error_code != net::OK) { |
686 callback.Run(error_code); | 682 callback.Run(error_code); |
687 return; | 683 return; |
688 } | 684 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 simple_entry->Close(); | 718 simple_entry->Close(); |
723 final_code = net::ERR_FAILED; | 719 final_code = net::ERR_FAILED; |
724 } else { | 720 } else { |
725 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | 721 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); |
726 } | 722 } |
727 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | 723 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); |
728 } | 724 } |
729 callback.Run(final_code); | 725 callback.Run(final_code); |
730 } | 726 } |
731 | 727 |
732 void SimpleBackendImpl::CheckIterationReturnValue( | |
733 void** iter, | |
734 Entry** entry, | |
735 const CompletionCallback& callback, | |
736 int error_code) { | |
737 if (error_code == net::ERR_FAILED) { | |
738 OpenNextEntry(iter, entry, callback); | |
739 return; | |
740 } | |
741 callback.Run(error_code); | |
742 } | |
743 | |
744 void SimpleBackendImpl::DoomEntriesComplete( | 728 void SimpleBackendImpl::DoomEntriesComplete( |
745 scoped_ptr<std::vector<uint64> > entry_hashes, | 729 scoped_ptr<std::vector<uint64> > entry_hashes, |
746 const net::CompletionCallback& callback, | 730 const net::CompletionCallback& callback, |
747 int result) { | 731 int result) { |
748 std::for_each( | 732 std::for_each( |
749 entry_hashes->begin(), entry_hashes->end(), | 733 entry_hashes->begin(), entry_hashes->end(), |
750 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), | 734 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), |
751 this)); | 735 this)); |
752 callback.Run(result); | 736 callback.Run(result); |
753 } | 737 } |
754 | 738 |
755 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 739 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
756 if (g_sequenced_worker_pool) | 740 if (g_sequenced_worker_pool) |
757 g_sequenced_worker_pool->FlushForTesting(); | 741 g_sequenced_worker_pool->FlushForTesting(); |
758 } | 742 } |
759 | 743 |
760 } // namespace disk_cache | 744 } // namespace disk_cache |
OLD | NEW |