| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/blockfile/backend_impl.h" | 5 #include "net/disk_cache/blockfile/backend_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 } | 321 } |
| 322 block_files_.CloseFiles(); | 322 block_files_.CloseFiles(); |
| 323 FlushIndex(); | 323 FlushIndex(); |
| 324 index_ = NULL; | 324 index_ = NULL; |
| 325 ptr_factory_.InvalidateWeakPtrs(); | 325 ptr_factory_.InvalidateWeakPtrs(); |
| 326 done_.Signal(); | 326 done_.Signal(); |
| 327 } | 327 } |
| 328 | 328 |
| 329 // ------------------------------------------------------------------------ | 329 // ------------------------------------------------------------------------ |
| 330 | 330 |
| 331 int BackendImpl::OpenPrevEntry(void** iter, Entry** prev_entry, | |
| 332 const CompletionCallback& callback) { | |
| 333 DCHECK(!callback.is_null()); | |
| 334 background_queue_.OpenPrevEntry(iter, prev_entry, callback); | |
| 335 return net::ERR_IO_PENDING; | |
| 336 } | |
| 337 | |
| 338 int BackendImpl::SyncOpenEntry(const std::string& key, Entry** entry) { | 331 int BackendImpl::SyncOpenEntry(const std::string& key, Entry** entry) { |
| 339 DCHECK(entry); | 332 DCHECK(entry); |
| 340 *entry = OpenEntryImpl(key); | 333 *entry = OpenEntryImpl(key); |
| 341 return (*entry) ? net::OK : net::ERR_FAILED; | 334 return (*entry) ? net::OK : net::ERR_FAILED; |
| 342 } | 335 } |
| 343 | 336 |
| 344 int BackendImpl::SyncCreateEntry(const std::string& key, Entry** entry) { | 337 int BackendImpl::SyncCreateEntry(const std::string& key, Entry** entry) { |
| 345 DCHECK(entry); | 338 DCHECK(entry); |
| 346 *entry = CreateEntryImpl(key); | 339 *entry = CreateEntryImpl(key); |
| 347 return (*entry) ? net::OK : net::ERR_FAILED; | 340 return (*entry) ? net::OK : net::ERR_FAILED; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 entry->Release(); | 430 entry->Release(); |
| 438 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator. | 431 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator. |
| 439 } | 432 } |
| 440 } | 433 } |
| 441 | 434 |
| 442 int BackendImpl::SyncOpenNextEntry(void** iter, Entry** next_entry) { | 435 int BackendImpl::SyncOpenNextEntry(void** iter, Entry** next_entry) { |
| 443 *next_entry = OpenNextEntryImpl(iter); | 436 *next_entry = OpenNextEntryImpl(iter); |
| 444 return (*next_entry) ? net::OK : net::ERR_FAILED; | 437 return (*next_entry) ? net::OK : net::ERR_FAILED; |
| 445 } | 438 } |
| 446 | 439 |
| 447 int BackendImpl::SyncOpenPrevEntry(void** iter, Entry** prev_entry) { | |
| 448 *prev_entry = OpenPrevEntryImpl(iter); | |
| 449 return (*prev_entry) ? net::OK : net::ERR_FAILED; | |
| 450 } | |
| 451 | |
| 452 void BackendImpl::SyncEndEnumeration(void* iter) { | 440 void BackendImpl::SyncEndEnumeration(void* iter) { |
| 453 scoped_ptr<Rankings::Iterator> iterator( | 441 scoped_ptr<Rankings::Iterator> iterator( |
| 454 reinterpret_cast<Rankings::Iterator*>(iter)); | 442 reinterpret_cast<Rankings::Iterator*>(iter)); |
| 455 } | 443 } |
| 456 | 444 |
| 457 void BackendImpl::SyncOnExternalCacheHit(const std::string& key) { | 445 void BackendImpl::SyncOnExternalCacheHit(const std::string& key) { |
| 458 if (disabled_) | 446 if (disabled_) |
| 459 return; | 447 return; |
| 460 | 448 |
| 461 uint32 hash = base::Hash(key); | 449 uint32 hash = base::Hash(key); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 CACHE_UMA(AGE_MS, "CreateTime", 0, start); | 598 CACHE_UMA(AGE_MS, "CreateTime", 0, start); |
| 611 stats_.OnEvent(Stats::CREATE_HIT); | 599 stats_.OnEvent(Stats::CREATE_HIT); |
| 612 SIMPLE_STATS_COUNTER("disk_cache.miss"); | 600 SIMPLE_STATS_COUNTER("disk_cache.miss"); |
| 613 Trace("create entry hit "); | 601 Trace("create entry hit "); |
| 614 FlushIndex(); | 602 FlushIndex(); |
| 615 cache_entry->AddRef(); | 603 cache_entry->AddRef(); |
| 616 return cache_entry.get(); | 604 return cache_entry.get(); |
| 617 } | 605 } |
| 618 | 606 |
| 619 EntryImpl* BackendImpl::OpenNextEntryImpl(void** iter) { | 607 EntryImpl* BackendImpl::OpenNextEntryImpl(void** iter) { |
| 620 return OpenFollowingEntry(true, iter); | 608 if (disabled_) |
| 621 } | 609 return NULL; |
| 622 | 610 |
| 623 EntryImpl* BackendImpl::OpenPrevEntryImpl(void** iter) { | 611 DCHECK(iter); |
| 624 return OpenFollowingEntry(false, iter); | 612 |
| 613 const int kListsToSearch = 3; |
| 614 scoped_refptr<EntryImpl> entries[kListsToSearch]; |
| 615 scoped_ptr<Rankings::Iterator> iterator( |
| 616 reinterpret_cast<Rankings::Iterator*>(*iter)); |
| 617 *iter = NULL; |
| 618 |
| 619 if (!iterator.get()) { |
| 620 iterator.reset(new Rankings::Iterator(&rankings_)); |
| 621 bool ret = false; |
| 622 |
| 623 // Get an entry from each list. |
| 624 for (int i = 0; i < kListsToSearch; i++) { |
| 625 EntryImpl* temp = NULL; |
| 626 ret |= OpenFollowingEntryFromList(static_cast<Rankings::List>(i), |
| 627 &iterator->nodes[i], &temp); |
| 628 entries[i].swap(&temp); // The entry was already addref'd. |
| 629 } |
| 630 if (!ret) |
| 631 return NULL; |
| 632 } else { |
| 633 // Get the next entry from the last list, and the actual entries for the |
| 634 // elements on the other lists. |
| 635 for (int i = 0; i < kListsToSearch; i++) { |
| 636 EntryImpl* temp = NULL; |
| 637 if (iterator->list == i) { |
| 638 OpenFollowingEntryFromList( |
| 639 iterator->list, &iterator->nodes[i], &temp); |
| 640 } else { |
| 641 temp = GetEnumeratedEntry(iterator->nodes[i], |
| 642 static_cast<Rankings::List>(i)); |
| 643 } |
| 644 |
| 645 entries[i].swap(&temp); // The entry was already addref'd. |
| 646 } |
| 647 } |
| 648 |
| 649 int newest = -1; |
| 650 int oldest = -1; |
| 651 Time access_times[kListsToSearch]; |
| 652 for (int i = 0; i < kListsToSearch; i++) { |
| 653 if (entries[i].get()) { |
| 654 access_times[i] = entries[i]->GetLastUsed(); |
| 655 if (newest < 0) { |
| 656 DCHECK_LT(oldest, 0); |
| 657 newest = oldest = i; |
| 658 continue; |
| 659 } |
| 660 if (access_times[i] > access_times[newest]) |
| 661 newest = i; |
| 662 if (access_times[i] < access_times[oldest]) |
| 663 oldest = i; |
| 664 } |
| 665 } |
| 666 |
| 667 if (newest < 0 || oldest < 0) |
| 668 return NULL; |
| 669 |
| 670 EntryImpl* next_entry; |
| 671 next_entry = entries[newest].get(); |
| 672 iterator->list = static_cast<Rankings::List>(newest); |
| 673 *iter = iterator.release(); |
| 674 next_entry->AddRef(); |
| 675 return next_entry; |
| 625 } | 676 } |
| 626 | 677 |
| 627 bool BackendImpl::SetMaxSize(int max_bytes) { | 678 bool BackendImpl::SetMaxSize(int max_bytes) { |
| 628 COMPILE_ASSERT(sizeof(max_bytes) == sizeof(max_size_), unsupported_int_model); | 679 COMPILE_ASSERT(sizeof(max_bytes) == sizeof(max_size_), unsupported_int_model); |
| 629 if (max_bytes < 0) | 680 if (max_bytes < 0) |
| 630 return false; | 681 return false; |
| 631 | 682 |
| 632 // Zero size means use the default. | 683 // Zero size means use the default. |
| 633 if (!max_bytes) | 684 if (!max_bytes) |
| 634 return true; | 685 return true; |
| (...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1612 } | 1663 } |
| 1613 | 1664 |
| 1614 if (cache_entry.get() && (find_parent || !found)) | 1665 if (cache_entry.get() && (find_parent || !found)) |
| 1615 cache_entry = NULL; | 1666 cache_entry = NULL; |
| 1616 | 1667 |
| 1617 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp); | 1668 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp); |
| 1618 FlushIndex(); | 1669 FlushIndex(); |
| 1619 return tmp; | 1670 return tmp; |
| 1620 } | 1671 } |
| 1621 | 1672 |
| 1622 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. | 1673 bool BackendImpl::OpenFollowingEntryFromList(Rankings::List list, |
| 1623 EntryImpl* BackendImpl::OpenFollowingEntry(bool forward, void** iter) { | |
| 1624 if (disabled_) | |
| 1625 return NULL; | |
| 1626 | |
| 1627 DCHECK(iter); | |
| 1628 | |
| 1629 const int kListsToSearch = 3; | |
| 1630 scoped_refptr<EntryImpl> entries[kListsToSearch]; | |
| 1631 scoped_ptr<Rankings::Iterator> iterator( | |
| 1632 reinterpret_cast<Rankings::Iterator*>(*iter)); | |
| 1633 *iter = NULL; | |
| 1634 | |
| 1635 if (!iterator.get()) { | |
| 1636 iterator.reset(new Rankings::Iterator(&rankings_)); | |
| 1637 bool ret = false; | |
| 1638 | |
| 1639 // Get an entry from each list. | |
| 1640 for (int i = 0; i < kListsToSearch; i++) { | |
| 1641 EntryImpl* temp = NULL; | |
| 1642 ret |= OpenFollowingEntryFromList(forward, static_cast<Rankings::List>(i), | |
| 1643 &iterator->nodes[i], &temp); | |
| 1644 entries[i].swap(&temp); // The entry was already addref'd. | |
| 1645 } | |
| 1646 if (!ret) | |
| 1647 return NULL; | |
| 1648 } else { | |
| 1649 // Get the next entry from the last list, and the actual entries for the | |
| 1650 // elements on the other lists. | |
| 1651 for (int i = 0; i < kListsToSearch; i++) { | |
| 1652 EntryImpl* temp = NULL; | |
| 1653 if (iterator->list == i) { | |
| 1654 OpenFollowingEntryFromList(forward, iterator->list, | |
| 1655 &iterator->nodes[i], &temp); | |
| 1656 } else { | |
| 1657 temp = GetEnumeratedEntry(iterator->nodes[i], | |
| 1658 static_cast<Rankings::List>(i)); | |
| 1659 } | |
| 1660 | |
| 1661 entries[i].swap(&temp); // The entry was already addref'd. | |
| 1662 } | |
| 1663 } | |
| 1664 | |
| 1665 int newest = -1; | |
| 1666 int oldest = -1; | |
| 1667 Time access_times[kListsToSearch]; | |
| 1668 for (int i = 0; i < kListsToSearch; i++) { | |
| 1669 if (entries[i].get()) { | |
| 1670 access_times[i] = entries[i]->GetLastUsed(); | |
| 1671 if (newest < 0) { | |
| 1672 DCHECK_LT(oldest, 0); | |
| 1673 newest = oldest = i; | |
| 1674 continue; | |
| 1675 } | |
| 1676 if (access_times[i] > access_times[newest]) | |
| 1677 newest = i; | |
| 1678 if (access_times[i] < access_times[oldest]) | |
| 1679 oldest = i; | |
| 1680 } | |
| 1681 } | |
| 1682 | |
| 1683 if (newest < 0 || oldest < 0) | |
| 1684 return NULL; | |
| 1685 | |
| 1686 EntryImpl* next_entry; | |
| 1687 if (forward) { | |
| 1688 next_entry = entries[newest].get(); | |
| 1689 iterator->list = static_cast<Rankings::List>(newest); | |
| 1690 } else { | |
| 1691 next_entry = entries[oldest].get(); | |
| 1692 iterator->list = static_cast<Rankings::List>(oldest); | |
| 1693 } | |
| 1694 | |
| 1695 *iter = iterator.release(); | |
| 1696 next_entry->AddRef(); | |
| 1697 return next_entry; | |
| 1698 } | |
| 1699 | |
| 1700 bool BackendImpl::OpenFollowingEntryFromList(bool forward, Rankings::List list, | |
| 1701 CacheRankingsBlock** from_entry, | 1674 CacheRankingsBlock** from_entry, |
| 1702 EntryImpl** next_entry) { | 1675 EntryImpl** next_entry) { |
| 1703 if (disabled_) | 1676 if (disabled_) |
| 1704 return false; | 1677 return false; |
| 1705 | 1678 |
| 1706 if (!new_eviction_ && Rankings::NO_USE != list) | 1679 if (!new_eviction_ && Rankings::NO_USE != list) |
| 1707 return false; | 1680 return false; |
| 1708 | 1681 |
| 1709 Rankings::ScopedRankingsBlock rankings(&rankings_, *from_entry); | 1682 Rankings::ScopedRankingsBlock rankings(&rankings_, *from_entry); |
| 1710 CacheRankingsBlock* next_block = forward ? | 1683 CacheRankingsBlock* next_block = rankings_.GetNext(rankings.get(), list); |
| 1711 rankings_.GetNext(rankings.get(), list) : | |
| 1712 rankings_.GetPrev(rankings.get(), list); | |
| 1713 Rankings::ScopedRankingsBlock next(&rankings_, next_block); | 1684 Rankings::ScopedRankingsBlock next(&rankings_, next_block); |
| 1714 *from_entry = NULL; | 1685 *from_entry = NULL; |
| 1715 | 1686 |
| 1716 *next_entry = GetEnumeratedEntry(next.get(), list); | 1687 *next_entry = GetEnumeratedEntry(next.get(), list); |
| 1717 if (!*next_entry) | 1688 if (!*next_entry) |
| 1718 return false; | 1689 return false; |
| 1719 | 1690 |
| 1720 *from_entry = next.release(); | 1691 *from_entry = next.release(); |
| 1721 return true; | 1692 return true; |
| 1722 } | 1693 } |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2095 if (total_memory > kMaxBuffersSize || total_memory <= 0) | 2066 if (total_memory > kMaxBuffersSize || total_memory <= 0) |
| 2096 total_memory = kMaxBuffersSize; | 2067 total_memory = kMaxBuffersSize; |
| 2097 | 2068 |
| 2098 done = true; | 2069 done = true; |
| 2099 } | 2070 } |
| 2100 | 2071 |
| 2101 return static_cast<int>(total_memory); | 2072 return static_cast<int>(total_memory); |
| 2102 } | 2073 } |
| 2103 | 2074 |
| 2104 } // namespace disk_cache | 2075 } // namespace disk_cache |
| OLD | NEW |