Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: net/disk_cache/simple/simple_backend_impl.cc

Issue 478573003: Connect SimpleCache Backend active_entries_ more closely to Entry lifetime. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove inline destructor Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 if (result == net::OK) { 188 if (result == net::OK) {
189 SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index); 189 SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index);
190 } else { 190 } else {
191 SIMPLE_CACHE_UMA(TIMES, 191 SIMPLE_CACHE_UMA(TIMES,
192 "CreationToIndexFail", cache_type, creation_to_index); 192 "CreationToIndexFail", cache_type, creation_to_index);
193 } 193 }
194 } 194 }
195 195
196 } // namespace 196 } // namespace
197 197
198 // Removes an entry from the Backend's |active_entries_| when the entry is done.
199 // Provided to each Entry as soon as it's added to |active_entries_|.
200 class SimpleBackendImpl::EntryDisconnector
201 : public SimpleEntryImpl::BackendDisconnector {
202 public:
203 EntryDisconnector(uint64 entry_hash,
204 SimpleBackendImpl* backend)
205 : entry_hash_(entry_hash),
206 backend_(backend->AsWeakPtr()) {}
207
208 virtual ~EntryDisconnector() {
209 if (backend_)
Deprecated (see juliatuttle) 2014/08/22 19:26:37 It makes me uncomfortable that we've lost the "mak
gavinp 2014/08/25 13:18:27 DCHECK added. It's not clear what we're losing tho
210 backend_->active_entries_.erase(entry_hash_);
211 }
212
213 private:
214 uint64 entry_hash_;
215 base::WeakPtr<SimpleBackendImpl> backend_;
216 };
217
198 SimpleBackendImpl::SimpleBackendImpl( 218 SimpleBackendImpl::SimpleBackendImpl(
199 const FilePath& path, 219 const FilePath& path,
200 int max_bytes, 220 int max_bytes,
201 net::CacheType cache_type, 221 net::CacheType cache_type,
202 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, 222 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
203 net::NetLog* net_log) 223 net::NetLog* net_log)
204 : path_(path), 224 : path_(path),
205 cache_type_(cache_type), 225 cache_type_(cache_type),
206 cache_thread_(cache_thread), 226 cache_thread_(cache_thread),
207 orig_max_size_(max_bytes), 227 orig_max_size_(max_bytes),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 264
245 bool SimpleBackendImpl::SetMaxSize(int max_bytes) { 265 bool SimpleBackendImpl::SetMaxSize(int max_bytes) {
246 orig_max_size_ = max_bytes; 266 orig_max_size_ = max_bytes;
247 return index_->SetMaxSize(max_bytes); 267 return index_->SetMaxSize(max_bytes);
248 } 268 }
249 269
250 int SimpleBackendImpl::GetMaxFileSize() const { 270 int SimpleBackendImpl::GetMaxFileSize() const {
251 return index_->max_size() / kMaxFileRatio; 271 return index_->max_size() / kMaxFileRatio;
252 } 272 }
253 273
254 void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) {
255 active_entries_.erase(entry->entry_hash());
256 }
257
258 void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) { 274 void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) {
259 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. 275 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed.
260 CHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); 276 CHECK_EQ(0u, entries_pending_doom_.count(entry_hash));
261 entries_pending_doom_.insert( 277 entries_pending_doom_.insert(
262 std::make_pair(entry_hash, std::vector<Closure>())); 278 std::make_pair(entry_hash, std::vector<Closure>()));
263 } 279 }
264 280
265 void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) { 281 void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) {
266 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed. 282 // TODO(ttuttle): Revert to DCHECK once http://crbug.com/317138 is fixed.
267 CHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); 283 CHECK_EQ(1u, entries_pending_doom_.count(entry_hash));
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 DCHECK(result.max_size); 521 DCHECK(result.max_size);
506 } 522 }
507 return result; 523 return result;
508 } 524 }
509 525
510 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( 526 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry(
511 const uint64 entry_hash, 527 const uint64 entry_hash,
512 const std::string& key) { 528 const std::string& key) {
513 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); 529 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key));
514 std::pair<EntryMap::iterator, bool> insert_result = 530 std::pair<EntryMap::iterator, bool> insert_result =
515 active_entries_.insert(std::make_pair(entry_hash, 531 active_entries_.insert(EntryMap::value_type(entry_hash, NULL));
Deprecated (see juliatuttle) 2014/08/22 19:26:38 Ooh, that's cute, using ::value_type. Nice.
516 base::WeakPtr<SimpleEntryImpl>()));
517 EntryMap::iterator& it = insert_result.first; 532 EntryMap::iterator& it = insert_result.first;
518 if (insert_result.second) 533 if (insert_result.second) {
519 DCHECK(!it->second.get()); 534 it->second = new SimpleEntryImpl(
Deprecated (see juliatuttle) 2014/08/22 19:26:37 I'd prefer if you had this named entry still and t
gavinp 2014/08/25 13:18:26 Done.
520 if (!it->second.get()) {
521 SimpleEntryImpl* entry = new SimpleEntryImpl(
522 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); 535 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_);
523 entry->SetKey(key); 536 scoped_ptr<EntryDisconnector>
524 it->second = entry->AsWeakPtr(); 537 entry_disconnector(new EntryDisconnector(entry_hash, this));
538 it->second->SetBackendDisconnector(
539 entry_disconnector.PassAs<SimpleEntryImpl::BackendDisconnector>());
540 it->second->SetKey(key);
Deprecated (see juliatuttle) 2014/08/22 19:26:38 Similarly, for no good reason, I'd prefer SetKey b
gavinp 2014/08/25 13:18:26 Done.
525 } 541 }
526 DCHECK(it->second.get());
Deprecated (see juliatuttle) 2014/08/22 19:26:38 Why not change to DCHECK(it->second)?
gavinp 2014/08/25 13:18:26 Done.
527 // It's possible, but unlikely, that we have an entry hash collision with a 542 // It's possible, but unlikely, that we have an entry hash collision with a
528 // currently active entry. 543 // currently active entry.
529 if (key != it->second->key()) { 544 if (key != it->second->key()) {
530 it->second->Doom(); 545 it->second->Doom();
531 DCHECK_EQ(0U, active_entries_.count(entry_hash)); 546 DCHECK_EQ(0U, active_entries_.count(entry_hash));
532 return CreateOrFindActiveEntry(entry_hash, key); 547 return CreateOrFindActiveEntry(entry_hash, key);
533 } 548 }
534 return make_scoped_refptr(it->second.get()); 549 return make_scoped_refptr(it->second);
535 } 550 }
536 551
537 int SimpleBackendImpl::OpenEntryFromHash(uint64 entry_hash, 552 int SimpleBackendImpl::OpenEntryFromHash(uint64 entry_hash,
538 Entry** entry, 553 Entry** entry,
539 const CompletionCallback& callback) { 554 const CompletionCallback& callback) {
540 base::hash_map<uint64, std::vector<Closure> >::iterator it = 555 base::hash_map<uint64, std::vector<Closure> >::iterator it =
541 entries_pending_doom_.find(entry_hash); 556 entries_pending_doom_.find(entry_hash);
542 if (it != entries_pending_doom_.end()) { 557 if (it != entries_pending_doom_.end()) {
543 Callback<int(const net::CompletionCallback&)> operation = 558 Callback<int(const net::CompletionCallback&)> operation =
544 base::Bind(&SimpleBackendImpl::OpenEntryFromHash, 559 base::Bind(&SimpleBackendImpl::OpenEntryFromHash,
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 Entry** entry, 648 Entry** entry,
634 scoped_refptr<SimpleEntryImpl> simple_entry, 649 scoped_refptr<SimpleEntryImpl> simple_entry,
635 const CompletionCallback& callback, 650 const CompletionCallback& callback,
636 int error_code) { 651 int error_code) {
637 if (error_code != net::OK) { 652 if (error_code != net::OK) {
638 callback.Run(error_code); 653 callback.Run(error_code);
639 return; 654 return;
640 } 655 }
641 DCHECK(*entry); 656 DCHECK(*entry);
642 std::pair<EntryMap::iterator, bool> insert_result = 657 std::pair<EntryMap::iterator, bool> insert_result =
643 active_entries_.insert(std::make_pair(hash, 658 active_entries_.insert(EntryMap::value_type(hash, simple_entry));
644 base::WeakPtr<SimpleEntryImpl>()));
645 EntryMap::iterator& it = insert_result.first; 659 EntryMap::iterator& it = insert_result.first;
646 const bool did_insert = insert_result.second; 660 const bool did_insert = insert_result.second;
647 if (did_insert) { 661 if (did_insert) {
648 // There is no active entry corresponding to this hash. The entry created 662 // There is no active entry corresponding to this hash. The entry created
649 // is put in the map of active entries and returned to the caller. 663 // is put in the map of active entries and returned to the caller.
650 it->second = simple_entry->AsWeakPtr(); 664 scoped_ptr<EntryDisconnector>
Deprecated (see juliatuttle) 2014/08/22 19:26:37 Can we abstract out "add this entry to the map usi
gavinp 2014/08/25 13:18:27 I considered it. That method would have weird argu
665 entry_disconnector(new EntryDisconnector(hash, this));
666 it->second->SetBackendDisconnector(
Deprecated (see juliatuttle) 2014/08/22 19:26:37 I'd like a comment here that explicitly points out
gavinp 2014/08/25 13:18:27 I've beefed up the comment on this branch.
667 entry_disconnector.PassAs<SimpleEntryImpl::BackendDisconnector>());
651 callback.Run(error_code); 668 callback.Run(error_code);
652 } else { 669 } else {
653 // The entry was made active with the key while the creation from hash 670 // The entry was made active with the key while the creation from hash
654 // occurred. The entry created from hash needs to be closed, and the one 671 // occurred. The entry created from hash needs to be closed, and the one
655 // coming from the key returned to the caller. 672 // coming from the key returned to the caller.
656 simple_entry->Close(); 673 simple_entry->Close();
657 it->second->OpenEntry(entry, callback); 674 it->second->OpenEntry(entry, callback);
658 } 675 }
659 } 676 }
660 677
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 this)); 720 this));
704 callback.Run(result); 721 callback.Run(result);
705 } 722 }
706 723
707 void SimpleBackendImpl::FlushWorkerPoolForTesting() { 724 void SimpleBackendImpl::FlushWorkerPoolForTesting() {
708 if (g_sequenced_worker_pool) 725 if (g_sequenced_worker_pool)
709 g_sequenced_worker_pool->FlushForTesting(); 726 g_sequenced_worker_pool->FlushForTesting();
710 } 727 }
711 728
712 } // namespace disk_cache 729 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698