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