| 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/memory/mem_backend_impl.h" | 5 #include "net/disk_cache/memory/mem_backend_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/disk_cache/cache_util.h" | 10 #include "net/disk_cache/cache_util.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 return 0; | 22 return 0; |
| 23 | 23 |
| 24 return high_water - kCleanUpMargin; | 24 return high_water - kCleanUpMargin; |
| 25 } | 25 } |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 namespace disk_cache { | 29 namespace disk_cache { |
| 30 | 30 |
| 31 MemBackendImpl::MemBackendImpl(net::NetLog* net_log) | 31 MemBackendImpl::MemBackendImpl(net::NetLog* net_log) |
| 32 : max_size_(0), current_size_(0), net_log_(net_log) {} | 32 : max_size_(0), current_size_(0), net_log_(net_log), weak_factory_(this) { |
| 33 } |
| 33 | 34 |
| 34 MemBackendImpl::~MemBackendImpl() { | 35 MemBackendImpl::~MemBackendImpl() { |
| 35 EntryMap::iterator it = entries_.begin(); | 36 EntryMap::iterator it = entries_.begin(); |
| 36 while (it != entries_.end()) { | 37 while (it != entries_.end()) { |
| 37 it->second->Doom(); | 38 it->second->Doom(); |
| 38 it = entries_.begin(); | 39 it = entries_.begin(); |
| 39 } | 40 } |
| 40 DCHECK(!current_size_); | 41 DCHECK(!current_size_); |
| 41 } | 42 } |
| 42 | 43 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 174 } |
| 174 | 175 |
| 175 int MemBackendImpl::DoomEntriesSince(const base::Time initial_time, | 176 int MemBackendImpl::DoomEntriesSince(const base::Time initial_time, |
| 176 const CompletionCallback& callback) { | 177 const CompletionCallback& callback) { |
| 177 if (DoomEntriesSince(initial_time)) | 178 if (DoomEntriesSince(initial_time)) |
| 178 return net::OK; | 179 return net::OK; |
| 179 | 180 |
| 180 return net::ERR_FAILED; | 181 return net::ERR_FAILED; |
| 181 } | 182 } |
| 182 | 183 |
| 183 int MemBackendImpl::OpenNextEntry(void** iter, Entry** next_entry, | 184 class MemBackendImpl::MemIterator : public Backend::Iterator { |
| 184 const CompletionCallback& callback) { | 185 public: |
| 185 if (OpenNextEntry(iter, next_entry)) | 186 explicit MemIterator(base::WeakPtr<MemBackendImpl> backend) |
| 186 return net::OK; | 187 : backend_(backend), current_(NULL) { |
| 188 } |
| 187 | 189 |
| 188 return net::ERR_FAILED; | 190 virtual int OpenNextEntry(Entry** next_entry, |
| 189 } | 191 const CompletionCallback& callback) OVERRIDE { |
| 192 if (!backend_) |
| 193 return net::ERR_FAILED; |
| 190 | 194 |
| 191 void MemBackendImpl::EndEnumeration(void** iter) { | 195 MemEntryImpl* node = backend_->rankings_.GetNext(current_); |
| 192 *iter = NULL; | 196 // We should never return a child entry so iterate until we hit a parent |
| 197 // entry. |
| 198 while (node && node->type() != MemEntryImpl::kParentEntry) |
| 199 node = backend_->rankings_.GetNext(node); |
| 200 *next_entry = node; |
| 201 current_ = node; |
| 202 |
| 203 if (node) { |
| 204 node->Open(); |
| 205 return net::OK; |
| 206 } |
| 207 return net::ERR_FAILED; |
| 208 } |
| 209 |
| 210 private: |
| 211 base::WeakPtr<MemBackendImpl> backend_; |
| 212 MemEntryImpl* current_; |
| 213 }; |
| 214 |
| 215 scoped_ptr<Backend::Iterator> MemBackendImpl::CreateIterator() { |
| 216 return scoped_ptr<Backend::Iterator>( |
| 217 new MemIterator(weak_factory_.GetWeakPtr())); |
| 193 } | 218 } |
| 194 | 219 |
| 195 void MemBackendImpl::OnExternalCacheHit(const std::string& key) { | 220 void MemBackendImpl::OnExternalCacheHit(const std::string& key) { |
| 196 EntryMap::iterator it = entries_.find(key); | 221 EntryMap::iterator it = entries_.find(key); |
| 197 if (it != entries_.end()) { | 222 if (it != entries_.end()) { |
| 198 UpdateRank(it->second); | 223 UpdateRank(it->second); |
| 199 } | 224 } |
| 200 } | 225 } |
| 201 | 226 |
| 202 bool MemBackendImpl::OpenEntry(const std::string& key, Entry** entry) { | 227 bool MemBackendImpl::OpenEntry(const std::string& key, Entry** entry) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 // Get the entry in the front. | 305 // Get the entry in the front. |
| 281 Entry* entry = rankings_.GetNext(NULL); | 306 Entry* entry = rankings_.GetNext(NULL); |
| 282 | 307 |
| 283 // Break the loop when there are no more entries or the entry is too old. | 308 // Break the loop when there are no more entries or the entry is too old. |
| 284 if (!entry || entry->GetLastUsed() < initial_time) | 309 if (!entry || entry->GetLastUsed() < initial_time) |
| 285 return true; | 310 return true; |
| 286 entry->Doom(); | 311 entry->Doom(); |
| 287 } | 312 } |
| 288 } | 313 } |
| 289 | 314 |
| 290 bool MemBackendImpl::OpenNextEntry(void** iter, Entry** next_entry) { | |
| 291 MemEntryImpl* current = reinterpret_cast<MemEntryImpl*>(*iter); | |
| 292 MemEntryImpl* node = rankings_.GetNext(current); | |
| 293 // We should never return a child entry so iterate until we hit a parent | |
| 294 // entry. | |
| 295 while (node && node->type() != MemEntryImpl::kParentEntry) { | |
| 296 node = rankings_.GetNext(node); | |
| 297 } | |
| 298 *next_entry = node; | |
| 299 *iter = node; | |
| 300 | |
| 301 if (node) | |
| 302 node->Open(); | |
| 303 | |
| 304 return NULL != node; | |
| 305 } | |
| 306 | |
| 307 void MemBackendImpl::TrimCache(bool empty) { | 315 void MemBackendImpl::TrimCache(bool empty) { |
| 308 MemEntryImpl* next = rankings_.GetPrev(NULL); | 316 MemEntryImpl* next = rankings_.GetPrev(NULL); |
| 309 if (!next) | 317 if (!next) |
| 310 return; | 318 return; |
| 311 | 319 |
| 312 int target_size = empty ? 0 : LowWaterAdjust(max_size_); | 320 int target_size = empty ? 0 : LowWaterAdjust(max_size_); |
| 313 while (current_size_ > target_size && next) { | 321 while (current_size_ > target_size && next) { |
| 314 MemEntryImpl* node = next; | 322 MemEntryImpl* node = next; |
| 315 next = rankings_.GetPrev(next); | 323 next = rankings_.GetPrev(next); |
| 316 if (!node->InUse() || empty) { | 324 if (!node->InUse() || empty) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 328 if (current_size_ > max_size_) | 336 if (current_size_ > max_size_) |
| 329 TrimCache(false); | 337 TrimCache(false); |
| 330 } | 338 } |
| 331 | 339 |
| 332 void MemBackendImpl::SubstractStorageSize(int32 bytes) { | 340 void MemBackendImpl::SubstractStorageSize(int32 bytes) { |
| 333 current_size_ -= bytes; | 341 current_size_ -= bytes; |
| 334 DCHECK_GE(current_size_, 0); | 342 DCHECK_GE(current_size_, 0); |
| 335 } | 343 } |
| 336 | 344 |
| 337 } // namespace disk_cache | 345 } // namespace disk_cache |
| OLD | NEW |