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 : 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 SimpleIndex::HashList* entry_list = | 483 virtual int OpenNextEntry(Entry** next_entry, |
487 static_cast<SimpleIndex::HashList*>(*iter); | 484 const CompletionCallback& callback) OVERRIDE { |
488 delete entry_list; | 485 CompletionCallback open_next_entry_impl = |
489 *iter = NULL; | 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, | |
pasko
2014/09/12 08:10:26
This has a lot in common with SimpleBackendImpl::G
gavinp
2014/09/12 14:15:32
You're right, that would have been a good way to p
| |
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())); | |
490 } | 547 } |
491 | 548 |
492 void SimpleBackendImpl::GetStats( | 549 void SimpleBackendImpl::GetStats( |
493 std::vector<std::pair<std::string, std::string> >* stats) { | 550 std::vector<std::pair<std::string, std::string> >* stats) { |
494 std::pair<std::string, std::string> item; | 551 std::pair<std::string, std::string> item; |
495 item.first = "Cache type"; | 552 item.first = "Cache type"; |
496 item.second = "Simple Cache"; | 553 item.second = "Simple Cache"; |
497 stats->push_back(item); | 554 stats->push_back(item); |
498 } | 555 } |
499 | 556 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
607 return active_it->second->DoomEntry(callback); | 664 return active_it->second->DoomEntry(callback); |
608 | 665 |
609 // 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 |
610 // call to DoomEntries() to delete this entry. | 667 // call to DoomEntries() to delete this entry. |
611 std::vector<uint64> entry_hash_vector; | 668 std::vector<uint64> entry_hash_vector; |
612 entry_hash_vector.push_back(entry_hash); | 669 entry_hash_vector.push_back(entry_hash); |
613 DoomEntries(&entry_hash_vector, callback); | 670 DoomEntries(&entry_hash_vector, callback); |
614 return net::ERR_IO_PENDING; | 671 return net::ERR_IO_PENDING; |
615 } | 672 } |
616 | 673 |
617 void SimpleBackendImpl::GetNextEntryInIterator( | |
618 void** iter, | |
619 Entry** next_entry, | |
620 const CompletionCallback& callback, | |
621 int error_code) { | |
622 if (error_code != net::OK) { | |
623 callback.Run(error_code); | |
624 return; | |
625 } | |
626 if (*iter == NULL) { | |
627 *iter = index()->GetAllHashes().release(); | |
628 } | |
629 SimpleIndex::HashList* entry_list = | |
630 static_cast<SimpleIndex::HashList*>(*iter); | |
631 while (entry_list->size() > 0) { | |
632 uint64 entry_hash = entry_list->back(); | |
633 entry_list->pop_back(); | |
634 if (index()->Has(entry_hash)) { | |
635 *next_entry = NULL; | |
636 CompletionCallback continue_iteration = base::Bind( | |
637 &SimpleBackendImpl::CheckIterationReturnValue, | |
638 AsWeakPtr(), | |
639 iter, | |
640 next_entry, | |
641 callback); | |
642 int error_code_open = OpenEntryFromHash(entry_hash, | |
643 next_entry, | |
644 continue_iteration); | |
645 if (error_code_open == net::ERR_IO_PENDING) | |
646 return; | |
647 if (error_code_open != net::ERR_FAILED) { | |
648 callback.Run(error_code_open); | |
649 return; | |
650 } | |
651 } | |
652 } | |
653 callback.Run(net::ERR_FAILED); | |
654 } | |
655 | |
656 void SimpleBackendImpl::OnEntryOpenedFromHash( | 674 void SimpleBackendImpl::OnEntryOpenedFromHash( |
657 uint64 hash, | 675 uint64 hash, |
658 Entry** entry, | 676 Entry** entry, |
659 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 677 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
660 const CompletionCallback& callback, | 678 const CompletionCallback& callback, |
661 int error_code) { | 679 int error_code) { |
662 if (error_code != net::OK) { | 680 if (error_code != net::OK) { |
663 callback.Run(error_code); | 681 callback.Run(error_code); |
664 return; | 682 return; |
665 } | 683 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
699 simple_entry->Close(); | 717 simple_entry->Close(); |
700 final_code = net::ERR_FAILED; | 718 final_code = net::ERR_FAILED; |
701 } else { | 719 } else { |
702 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | 720 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); |
703 } | 721 } |
704 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | 722 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); |
705 } | 723 } |
706 callback.Run(final_code); | 724 callback.Run(final_code); |
707 } | 725 } |
708 | 726 |
709 void SimpleBackendImpl::CheckIterationReturnValue( | |
710 void** iter, | |
711 Entry** entry, | |
712 const CompletionCallback& callback, | |
713 int error_code) { | |
714 if (error_code == net::ERR_FAILED) { | |
715 OpenNextEntry(iter, entry, callback); | |
716 return; | |
717 } | |
718 callback.Run(error_code); | |
719 } | |
720 | |
721 void SimpleBackendImpl::DoomEntriesComplete( | 727 void SimpleBackendImpl::DoomEntriesComplete( |
722 scoped_ptr<std::vector<uint64> > entry_hashes, | 728 scoped_ptr<std::vector<uint64> > entry_hashes, |
723 const net::CompletionCallback& callback, | 729 const net::CompletionCallback& callback, |
724 int result) { | 730 int result) { |
725 std::for_each( | 731 std::for_each( |
726 entry_hashes->begin(), entry_hashes->end(), | 732 entry_hashes->begin(), entry_hashes->end(), |
727 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), | 733 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), |
728 this)); | 734 this)); |
729 callback.Run(result); | 735 callback.Run(result); |
730 } | 736 } |
731 | 737 |
732 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 738 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
733 if (g_sequenced_worker_pool) | 739 if (g_sequenced_worker_pool) |
734 g_sequenced_worker_pool->FlushForTesting(); | 740 g_sequenced_worker_pool->FlushForTesting(); |
735 } | 741 } |
736 | 742 |
737 } // namespace disk_cache | 743 } // namespace disk_cache |
OLD | NEW |