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 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 if (it != entries_pending_doom_.end()) { | 383 if (it != entries_pending_doom_.end()) { |
384 Callback<int(const net::CompletionCallback&)> operation = | 384 Callback<int(const net::CompletionCallback&)> operation = |
385 base::Bind(&SimpleBackendImpl::OpenEntry, | 385 base::Bind(&SimpleBackendImpl::OpenEntry, |
386 base::Unretained(this), key, entry); | 386 base::Unretained(this), key, entry); |
387 it->second.push_back(base::Bind(&RunOperationAndCallback, | 387 it->second.push_back(base::Bind(&RunOperationAndCallback, |
388 operation, callback)); | 388 operation, callback)); |
389 return net::ERR_IO_PENDING; | 389 return net::ERR_IO_PENDING; |
390 } | 390 } |
391 scoped_refptr<SimpleEntryImpl> simple_entry = | 391 scoped_refptr<SimpleEntryImpl> simple_entry = |
392 CreateOrFindActiveEntry(entry_hash, key); | 392 CreateOrFindActiveEntry(entry_hash, key); |
393 CompletionCallback backend_callback = | 393 return simple_entry->OpenEntry(entry, callback); |
394 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, | |
395 AsWeakPtr(), | |
396 key, | |
397 entry, | |
398 simple_entry, | |
399 callback); | |
400 return simple_entry->OpenEntry(entry, backend_callback); | |
401 } | 394 } |
402 | 395 |
403 int SimpleBackendImpl::CreateEntry(const std::string& key, | 396 int SimpleBackendImpl::CreateEntry(const std::string& key, |
404 Entry** entry, | 397 Entry** entry, |
405 const CompletionCallback& callback) { | 398 const CompletionCallback& callback) { |
406 DCHECK_LT(0u, key.size()); | 399 DCHECK_LT(0u, key.size()); |
407 const uint64_t entry_hash = simple_util::GetEntryHashKey(key); | 400 const uint64_t entry_hash = simple_util::GetEntryHashKey(key); |
408 | 401 |
409 std::unordered_map<uint64_t, std::vector<Closure>>::iterator it = | 402 std::unordered_map<uint64_t, std::vector<Closure>>::iterator it = |
410 entries_pending_doom_.find(entry_hash); | 403 entries_pending_doom_.find(entry_hash); |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 callback.Run(net::OK); | 699 callback.Run(net::OK); |
707 } else { | 700 } else { |
708 // The entry was made active while we waiting for the open from hash to | 701 // The entry was made active while we waiting for the open from hash to |
709 // finish. The entry created from hash needs to be closed, and the one | 702 // finish. The entry created from hash needs to be closed, and the one |
710 // in |active_entries_| can be returned to the caller. | 703 // in |active_entries_| can be returned to the caller. |
711 simple_entry->Close(); | 704 simple_entry->Close(); |
712 it->second->OpenEntry(entry, callback); | 705 it->second->OpenEntry(entry, callback); |
713 } | 706 } |
714 } | 707 } |
715 | 708 |
716 void SimpleBackendImpl::OnEntryOpenedFromKey( | |
717 const std::string key, | |
718 Entry** entry, | |
719 const scoped_refptr<SimpleEntryImpl>& simple_entry, | |
720 const CompletionCallback& callback, | |
721 int error_code) { | |
722 int final_code = error_code; | |
723 if (final_code == net::OK) { | |
724 bool key_matches = key.compare(simple_entry->key()) == 0; | |
725 if (!key_matches) { | |
726 // TODO(clamy): Add a unit test to check this code path. | |
727 DLOG(WARNING) << "Key mismatch on open."; | |
728 simple_entry->Doom(); | |
729 simple_entry->Close(); | |
730 final_code = net::ERR_FAILED; | |
731 } else { | |
732 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | |
733 } | |
734 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | |
735 } | |
736 callback.Run(final_code); | |
737 } | |
738 | |
739 void SimpleBackendImpl::DoomEntriesComplete( | 709 void SimpleBackendImpl::DoomEntriesComplete( |
740 std::unique_ptr<std::vector<uint64_t>> entry_hashes, | 710 std::unique_ptr<std::vector<uint64_t>> entry_hashes, |
741 const net::CompletionCallback& callback, | 711 const net::CompletionCallback& callback, |
742 int result) { | 712 int result) { |
743 for (const uint64_t& entry_hash : *entry_hashes) | 713 for (const uint64_t& entry_hash : *entry_hashes) |
744 OnDoomComplete(entry_hash); | 714 OnDoomComplete(entry_hash); |
745 callback.Run(result); | 715 callback.Run(result); |
746 } | 716 } |
747 | 717 |
748 // static | 718 // static |
749 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 719 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
750 // We only need to do this if we there is an active task runner. | 720 // We only need to do this if we there is an active task runner. |
751 if (base::ThreadTaskRunnerHandle::IsSet()) | 721 if (base::ThreadTaskRunnerHandle::IsSet()) |
752 g_sequenced_worker_pool.Get().FlushForTesting(); | 722 g_sequenced_worker_pool.Get().FlushForTesting(); |
753 } | 723 } |
754 | 724 |
755 } // namespace disk_cache | 725 } // namespace disk_cache |
OLD | NEW |