| 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, |
| 492 const CompletionCallback& callback, |
| 493 int index_initialization_error_code) { |
| 494 if (!backend_) { |
| 495 callback.Run(net::ERR_FAILED); |
| 496 return; |
| 497 } |
| 498 |
| 499 if (index_initialization_error_code != net::OK) { |
| 500 callback.Run(index_initialization_error_code); |
| 501 return; |
| 502 } |
| 503 |
| 504 if (!hashes_to_enumerate_) |
| 505 hashes_to_enumerate_ = backend_->index()->GetAllHashes().Pass(); |
| 506 |
| 507 while (!hashes_to_enumerate_->empty()) { |
| 508 uint64 entry_hash = hashes_to_enumerate_->back(); |
| 509 hashes_to_enumerate_->pop_back(); |
| 510 if (backend_->index()->Has(entry_hash)) { |
| 511 *next_entry = NULL; |
| 512 CompletionCallback continue_iteration = base::Bind( |
| 513 &SimpleIterator::CheckIterationReturnValue, |
| 514 weak_factory_.GetWeakPtr(), |
| 515 next_entry, |
| 516 callback); |
| 517 int error_code_open = backend_->OpenEntryFromHash(entry_hash, |
| 518 next_entry, |
| 519 continue_iteration); |
| 520 if (error_code_open == net::ERR_IO_PENDING) |
| 521 return; |
| 522 if (error_code_open != net::ERR_FAILED) { |
| 523 callback.Run(error_code_open); |
| 524 return; |
| 525 } |
| 526 } |
| 527 } |
| 528 callback.Run(net::ERR_FAILED); |
| 529 } |
| 530 |
| 531 void CheckIterationReturnValue(Entry** entry, |
| 532 const CompletionCallback& callback, |
| 533 int error_code) { |
| 534 if (error_code == net::ERR_FAILED) { |
| 535 OpenNextEntry(entry, callback); |
| 536 return; |
| 537 } |
| 538 callback.Run(error_code); |
| 539 } |
| 540 |
| 541 private: |
| 542 base::WeakPtr<SimpleBackendImpl> backend_; |
| 543 scoped_ptr<std::vector<uint64> > hashes_to_enumerate_; |
| 544 base::WeakPtrFactory<SimpleIterator> weak_factory_; |
| 545 }; |
| 546 |
| 547 scoped_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { |
| 548 return scoped_ptr<Iterator>(new SimpleIterator(AsWeakPtr())); |
| 490 } | 549 } |
| 491 | 550 |
| 492 void SimpleBackendImpl::GetStats( | 551 void SimpleBackendImpl::GetStats( |
| 493 std::vector<std::pair<std::string, std::string> >* stats) { | 552 std::vector<std::pair<std::string, std::string> >* stats) { |
| 494 std::pair<std::string, std::string> item; | 553 std::pair<std::string, std::string> item; |
| 495 item.first = "Cache type"; | 554 item.first = "Cache type"; |
| 496 item.second = "Simple Cache"; | 555 item.second = "Simple Cache"; |
| 497 stats->push_back(item); | 556 stats->push_back(item); |
| 498 } | 557 } |
| 499 | 558 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 return active_it->second->DoomEntry(callback); | 666 return active_it->second->DoomEntry(callback); |
| 608 | 667 |
| 609 // There's no pending dooms, nor any open entry. We can make a trivial | 668 // There's no pending dooms, nor any open entry. We can make a trivial |
| 610 // call to DoomEntries() to delete this entry. | 669 // call to DoomEntries() to delete this entry. |
| 611 std::vector<uint64> entry_hash_vector; | 670 std::vector<uint64> entry_hash_vector; |
| 612 entry_hash_vector.push_back(entry_hash); | 671 entry_hash_vector.push_back(entry_hash); |
| 613 DoomEntries(&entry_hash_vector, callback); | 672 DoomEntries(&entry_hash_vector, callback); |
| 614 return net::ERR_IO_PENDING; | 673 return net::ERR_IO_PENDING; |
| 615 } | 674 } |
| 616 | 675 |
| 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( | 676 void SimpleBackendImpl::OnEntryOpenedFromHash( |
| 657 uint64 hash, | 677 uint64 hash, |
| 658 Entry** entry, | 678 Entry** entry, |
| 659 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 679 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
| 660 const CompletionCallback& callback, | 680 const CompletionCallback& callback, |
| 661 int error_code) { | 681 int error_code) { |
| 662 if (error_code != net::OK) { | 682 if (error_code != net::OK) { |
| 663 callback.Run(error_code); | 683 callback.Run(error_code); |
| 664 return; | 684 return; |
| 665 } | 685 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 simple_entry->Close(); | 719 simple_entry->Close(); |
| 700 final_code = net::ERR_FAILED; | 720 final_code = net::ERR_FAILED; |
| 701 } else { | 721 } else { |
| 702 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | 722 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); |
| 703 } | 723 } |
| 704 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | 724 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); |
| 705 } | 725 } |
| 706 callback.Run(final_code); | 726 callback.Run(final_code); |
| 707 } | 727 } |
| 708 | 728 |
| 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( | 729 void SimpleBackendImpl::DoomEntriesComplete( |
| 722 scoped_ptr<std::vector<uint64> > entry_hashes, | 730 scoped_ptr<std::vector<uint64> > entry_hashes, |
| 723 const net::CompletionCallback& callback, | 731 const net::CompletionCallback& callback, |
| 724 int result) { | 732 int result) { |
| 725 std::for_each( | 733 std::for_each( |
| 726 entry_hashes->begin(), entry_hashes->end(), | 734 entry_hashes->begin(), entry_hashes->end(), |
| 727 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), | 735 std::bind1st(std::mem_fun(&SimpleBackendImpl::OnDoomComplete), |
| 728 this)); | 736 this)); |
| 729 callback.Run(result); | 737 callback.Run(result); |
| 730 } | 738 } |
| 731 | 739 |
| 732 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 740 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
| 733 if (g_sequenced_worker_pool) | 741 if (g_sequenced_worker_pool) |
| 734 g_sequenced_worker_pool->FlushForTesting(); | 742 g_sequenced_worker_pool->FlushForTesting(); |
| 735 } | 743 } |
| 736 | 744 |
| 737 } // namespace disk_cache | 745 } // namespace disk_cache |
| OLD | NEW |