| 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_index.h" | 5 #include "net/disk_cache/simple/simple_index.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 return false; | 137 return false; |
| 138 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); | 138 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); |
| 139 entry_size_ = static_cast<int32_t>(tmp_entry_size); | 139 entry_size_ = static_cast<int32_t>(tmp_entry_size); |
| 140 return true; | 140 return true; |
| 141 } | 141 } |
| 142 | 142 |
| 143 SimpleIndex::SimpleIndex( | 143 SimpleIndex::SimpleIndex( |
| 144 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, | 144 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, |
| 145 SimpleIndexDelegate* delegate, | 145 SimpleIndexDelegate* delegate, |
| 146 net::CacheType cache_type, | 146 net::CacheType cache_type, |
| 147 scoped_ptr<SimpleIndexFile> index_file) | 147 std::unique_ptr<SimpleIndexFile> index_file) |
| 148 : delegate_(delegate), | 148 : delegate_(delegate), |
| 149 cache_type_(cache_type), | 149 cache_type_(cache_type), |
| 150 cache_size_(0), | 150 cache_size_(0), |
| 151 max_size_(0), | 151 max_size_(0), |
| 152 high_watermark_(0), | 152 high_watermark_(0), |
| 153 low_watermark_(0), | 153 low_watermark_(0), |
| 154 eviction_in_progress_(false), | 154 eviction_in_progress_(false), |
| 155 initialized_(false), | 155 initialized_(false), |
| 156 init_method_(INITIALIZE_METHOD_MAX), | 156 init_method_(INITIALIZE_METHOD_MAX), |
| 157 index_file_(std::move(index_file)), | 157 index_file_(std::move(index_file)), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 175 DCHECK(io_thread_checker_.CalledOnValidThread()); | 175 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 176 | 176 |
| 177 #if defined(OS_ANDROID) | 177 #if defined(OS_ANDROID) |
| 178 if (base::android::IsVMInitialized()) { | 178 if (base::android::IsVMInitialized()) { |
| 179 app_status_listener_.reset(new base::android::ApplicationStatusListener( | 179 app_status_listener_.reset(new base::android::ApplicationStatusListener( |
| 180 base::Bind(&SimpleIndex::OnApplicationStateChange, AsWeakPtr()))); | 180 base::Bind(&SimpleIndex::OnApplicationStateChange, AsWeakPtr()))); |
| 181 } | 181 } |
| 182 #endif | 182 #endif |
| 183 | 183 |
| 184 SimpleIndexLoadResult* load_result = new SimpleIndexLoadResult(); | 184 SimpleIndexLoadResult* load_result = new SimpleIndexLoadResult(); |
| 185 scoped_ptr<SimpleIndexLoadResult> load_result_scoped(load_result); | 185 std::unique_ptr<SimpleIndexLoadResult> load_result_scoped(load_result); |
| 186 base::Closure reply = base::Bind( | 186 base::Closure reply = base::Bind( |
| 187 &SimpleIndex::MergeInitializingSet, | 187 &SimpleIndex::MergeInitializingSet, |
| 188 AsWeakPtr(), | 188 AsWeakPtr(), |
| 189 base::Passed(&load_result_scoped)); | 189 base::Passed(&load_result_scoped)); |
| 190 index_file_->LoadIndexEntries(cache_mtime, reply, load_result); | 190 index_file_->LoadIndexEntries(cache_mtime, reply, load_result); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void SimpleIndex::SetMaxSize(uint64_t max_bytes) { | 193 void SimpleIndex::SetMaxSize(uint64_t max_bytes) { |
| 194 // Zero size means use the default. | 194 // Zero size means use the default. |
| 195 if (max_bytes) { | 195 if (max_bytes) { |
| 196 max_size_ = max_bytes; | 196 max_size_ = max_bytes; |
| 197 high_watermark_ = max_size_ - max_size_ / kEvictionMarginDivisor; | 197 high_watermark_ = max_size_ - max_size_ / kEvictionMarginDivisor; |
| 198 low_watermark_ = max_size_ - 2 * (max_size_ / kEvictionMarginDivisor); | 198 low_watermark_ = max_size_ - 2 * (max_size_ / kEvictionMarginDivisor); |
| 199 } | 199 } |
| 200 } | 200 } |
| 201 | 201 |
| 202 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { | 202 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { |
| 203 DCHECK(io_thread_checker_.CalledOnValidThread()); | 203 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 204 if (initialized_) | 204 if (initialized_) |
| 205 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); | 205 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); |
| 206 else | 206 else |
| 207 to_run_when_initialized_.push_back(task); | 207 to_run_when_initialized_.push_back(task); |
| 208 return net::ERR_IO_PENDING; | 208 return net::ERR_IO_PENDING; |
| 209 } | 209 } |
| 210 | 210 |
| 211 scoped_ptr<SimpleIndex::HashList> SimpleIndex::GetEntriesBetween( | 211 std::unique_ptr<SimpleIndex::HashList> SimpleIndex::GetEntriesBetween( |
| 212 base::Time initial_time, base::Time end_time) { | 212 base::Time initial_time, |
| 213 base::Time end_time) { |
| 213 DCHECK_EQ(true, initialized_); | 214 DCHECK_EQ(true, initialized_); |
| 214 | 215 |
| 215 if (!initial_time.is_null()) | 216 if (!initial_time.is_null()) |
| 216 initial_time -= EntryMetadata::GetLowerEpsilonForTimeComparisons(); | 217 initial_time -= EntryMetadata::GetLowerEpsilonForTimeComparisons(); |
| 217 if (end_time.is_null()) | 218 if (end_time.is_null()) |
| 218 end_time = base::Time::Max(); | 219 end_time = base::Time::Max(); |
| 219 else | 220 else |
| 220 end_time += EntryMetadata::GetUpperEpsilonForTimeComparisons(); | 221 end_time += EntryMetadata::GetUpperEpsilonForTimeComparisons(); |
| 221 const base::Time extended_end_time = | 222 const base::Time extended_end_time = |
| 222 end_time.is_null() ? base::Time::Max() : end_time; | 223 end_time.is_null() ? base::Time::Max() : end_time; |
| 223 DCHECK(extended_end_time >= initial_time); | 224 DCHECK(extended_end_time >= initial_time); |
| 224 scoped_ptr<HashList> ret_hashes(new HashList()); | 225 std::unique_ptr<HashList> ret_hashes(new HashList()); |
| 225 for (EntrySet::iterator it = entries_set_.begin(), end = entries_set_.end(); | 226 for (EntrySet::iterator it = entries_set_.begin(), end = entries_set_.end(); |
| 226 it != end; ++it) { | 227 it != end; ++it) { |
| 227 EntryMetadata& metadata = it->second; | 228 EntryMetadata& metadata = it->second; |
| 228 base::Time entry_time = metadata.GetLastUsedTime(); | 229 base::Time entry_time = metadata.GetLastUsedTime(); |
| 229 if (initial_time <= entry_time && entry_time < extended_end_time) | 230 if (initial_time <= entry_time && entry_time < extended_end_time) |
| 230 ret_hashes->push_back(it->first); | 231 ret_hashes->push_back(it->first); |
| 231 } | 232 } |
| 232 return ret_hashes; | 233 return ret_hashes; |
| 233 } | 234 } |
| 234 | 235 |
| 235 scoped_ptr<SimpleIndex::HashList> SimpleIndex::GetAllHashes() { | 236 std::unique_ptr<SimpleIndex::HashList> SimpleIndex::GetAllHashes() { |
| 236 return GetEntriesBetween(base::Time(), base::Time()); | 237 return GetEntriesBetween(base::Time(), base::Time()); |
| 237 } | 238 } |
| 238 | 239 |
| 239 int32_t SimpleIndex::GetEntryCount() const { | 240 int32_t SimpleIndex::GetEntryCount() const { |
| 240 // TODO(pasko): return a meaningful initial estimate before initialized. | 241 // TODO(pasko): return a meaningful initial estimate before initialized. |
| 241 return entries_set_.size(); | 242 return entries_set_.size(); |
| 242 } | 243 } |
| 243 | 244 |
| 244 uint64_t SimpleIndex::GetCacheSize() const { | 245 uint64_t SimpleIndex::GetCacheSize() const { |
| 245 DCHECK(initialized_); | 246 DCHECK(initialized_); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 int64_t entry_size) { | 389 int64_t entry_size) { |
| 389 // Update the total cache size with the new entry size. | 390 // Update the total cache size with the new entry size. |
| 390 DCHECK(io_thread_checker_.CalledOnValidThread()); | 391 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 391 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); | 392 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); |
| 392 cache_size_ -= (*it)->second.GetEntrySize(); | 393 cache_size_ -= (*it)->second.GetEntrySize(); |
| 393 cache_size_ += entry_size; | 394 cache_size_ += entry_size; |
| 394 (*it)->second.SetEntrySize(entry_size); | 395 (*it)->second.SetEntrySize(entry_size); |
| 395 } | 396 } |
| 396 | 397 |
| 397 void SimpleIndex::MergeInitializingSet( | 398 void SimpleIndex::MergeInitializingSet( |
| 398 scoped_ptr<SimpleIndexLoadResult> load_result) { | 399 std::unique_ptr<SimpleIndexLoadResult> load_result) { |
| 399 DCHECK(io_thread_checker_.CalledOnValidThread()); | 400 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 400 DCHECK(load_result->did_load); | 401 DCHECK(load_result->did_load); |
| 401 | 402 |
| 402 EntrySet* index_file_entries = &load_result->entries; | 403 EntrySet* index_file_entries = &load_result->entries; |
| 403 | 404 |
| 404 for (base::hash_set<uint64_t>::const_iterator it = removed_entries_.begin(); | 405 for (base::hash_set<uint64_t>::const_iterator it = removed_entries_.begin(); |
| 405 it != removed_entries_.end(); ++it) { | 406 it != removed_entries_.end(); ++it) { |
| 406 index_file_entries->erase(*it); | 407 index_file_entries->erase(*it); |
| 407 } | 408 } |
| 408 removed_entries_.clear(); | 409 removed_entries_.clear(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 start - last_write_to_disk_); | 480 start - last_write_to_disk_); |
| 480 } | 481 } |
| 481 } | 482 } |
| 482 last_write_to_disk_ = start; | 483 last_write_to_disk_ = start; |
| 483 | 484 |
| 484 index_file_->WriteToDisk(entries_set_, cache_size_, | 485 index_file_->WriteToDisk(entries_set_, cache_size_, |
| 485 start, app_on_background_, base::Closure()); | 486 start, app_on_background_, base::Closure()); |
| 486 } | 487 } |
| 487 | 488 |
| 488 } // namespace disk_cache | 489 } // namespace disk_cache |
| OLD | NEW |