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 class SimpleBackendImpl::SimpleIterator FINAL : public Iterator { | 476 int SimpleBackendImpl::OpenNextEntry(void** iter, |
477 public: | 477 Entry** next_entry, |
478 explicit SimpleIterator(base::WeakPtr<SimpleBackendImpl> backend) | 478 const CompletionCallback& callback) { |
479 : backend_(backend), | 479 CompletionCallback get_next_entry = |
480 weak_factory_(this) { | 480 base::Bind(&SimpleBackendImpl::GetNextEntryInIterator, AsWeakPtr(), iter, |
481 } | 481 next_entry, callback); |
| 482 return index_->ExecuteWhenReady(get_next_entry); |
| 483 } |
482 | 484 |
483 // From Backend::Iterator: | 485 void SimpleBackendImpl::EndEnumeration(void** iter) { |
484 virtual int OpenNextEntry(Entry** next_entry, | 486 active_enumerations_.Remove(IteratorToEnumerationId(iter)); |
485 const CompletionCallback& callback) OVERRIDE { | 487 *iter = NULL; |
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())); | |
548 } | 488 } |
549 | 489 |
550 void SimpleBackendImpl::GetStats( | 490 void SimpleBackendImpl::GetStats( |
551 std::vector<std::pair<std::string, std::string> >* stats) { | 491 std::vector<std::pair<std::string, std::string> >* stats) { |
552 std::pair<std::string, std::string> item; | 492 std::pair<std::string, std::string> item; |
553 item.first = "Cache type"; | 493 item.first = "Cache type"; |
554 item.second = "Simple Cache"; | 494 item.second = "Simple Cache"; |
555 stats->push_back(item); | 495 stats->push_back(item); |
556 } | 496 } |
557 | 497 |
558 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { | 498 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { |
559 index_->UseIfExists(simple_util::GetEntryHashKey(key)); | 499 index_->UseIfExists(simple_util::GetEntryHashKey(key)); |
560 } | 500 } |
561 | 501 |
| 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 |
562 void SimpleBackendImpl::InitializeIndex(const CompletionCallback& callback, | 523 void SimpleBackendImpl::InitializeIndex(const CompletionCallback& callback, |
563 const DiskStatResult& result) { | 524 const DiskStatResult& result) { |
564 if (result.net_error == net::OK) { | 525 if (result.net_error == net::OK) { |
565 index_->SetMaxSize(result.max_size); | 526 index_->SetMaxSize(result.max_size); |
566 index_->Initialize(result.cache_dir_mtime); | 527 index_->Initialize(result.cache_dir_mtime); |
567 } | 528 } |
568 callback.Run(result.net_error); | 529 callback.Run(result.net_error); |
569 } | 530 } |
570 | 531 |
571 SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( | 532 SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 return active_it->second->DoomEntry(callback); | 626 return active_it->second->DoomEntry(callback); |
666 | 627 |
667 // There's no pending dooms, nor any open entry. We can make a trivial | 628 // There's no pending dooms, nor any open entry. We can make a trivial |
668 // call to DoomEntries() to delete this entry. | 629 // call to DoomEntries() to delete this entry. |
669 std::vector<uint64> entry_hash_vector; | 630 std::vector<uint64> entry_hash_vector; |
670 entry_hash_vector.push_back(entry_hash); | 631 entry_hash_vector.push_back(entry_hash); |
671 DoomEntries(&entry_hash_vector, callback); | 632 DoomEntries(&entry_hash_vector, callback); |
672 return net::ERR_IO_PENDING; | 633 return net::ERR_IO_PENDING; |
673 } | 634 } |
674 | 635 |
| 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 |
675 void SimpleBackendImpl::OnEntryOpenedFromHash( | 679 void SimpleBackendImpl::OnEntryOpenedFromHash( |
676 uint64 hash, | 680 uint64 hash, |
677 Entry** entry, | 681 Entry** entry, |
678 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 682 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
679 const CompletionCallback& callback, | 683 const CompletionCallback& callback, |
680 int error_code) { | 684 int error_code) { |
681 if (error_code != net::OK) { | 685 if (error_code != net::OK) { |
682 callback.Run(error_code); | 686 callback.Run(error_code); |
683 return; | 687 return; |
684 } | 688 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 simple_entry->Close(); | 722 simple_entry->Close(); |
719 final_code = net::ERR_FAILED; | 723 final_code = net::ERR_FAILED; |
720 } else { | 724 } else { |
721 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | 725 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); |
722 } | 726 } |
723 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | 727 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); |
724 } | 728 } |
725 callback.Run(final_code); | 729 callback.Run(final_code); |
726 } | 730 } |
727 | 731 |
| 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 |
728 void SimpleBackendImpl::DoomEntriesComplete( | 744 void SimpleBackendImpl::DoomEntriesComplete( |
729 scoped_ptr<std::vector<uint64> > entry_hashes, | 745 scoped_ptr<std::vector<uint64> > entry_hashes, |
730 const net::CompletionCallback& callback, | 746 const net::CompletionCallback& callback, |
731 int result) { | 747 int result) { |
732 std::for_each( | 748 std::for_each( |
733 entry_hashes->begin(), entry_hashes->end(), | 749 entry_hashes->begin(), entry_hashes->end(), |
734 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), | 750 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), |
735 this)); | 751 this)); |
736 callback.Run(result); | 752 callback.Run(result); |
737 } | 753 } |
738 | 754 |
739 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 755 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
740 if (g_sequenced_worker_pool) | 756 if (g_sequenced_worker_pool) |
741 g_sequenced_worker_pool->FlushForTesting(); | 757 g_sequenced_worker_pool->FlushForTesting(); |
742 } | 758 } |
743 | 759 |
744 } // namespace disk_cache | 760 } // namespace disk_cache |
OLD | NEW |