| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/extensions/updater/local_extension_cache.h" | 5 #include "chrome/browser/extensions/updater/local_extension_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // Delay between checks for flag file presence when waiting for the cache to | 22 // Delay between checks for flag file presence when waiting for the cache to |
| 23 // become ready. | 23 // become ready. |
| 24 const int64_t kCacheStatusPollingDelayMs = 1000; | 24 const int64_t kCacheStatusPollingDelayMs = 1000; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 const char LocalExtensionCache::kCacheReadyFlagFileName[] = ".initialized"; | 28 const char LocalExtensionCache::kCacheReadyFlagFileName[] = ".initialized"; |
| 29 | 29 |
| 30 LocalExtensionCache::LocalExtensionCache( | 30 LocalExtensionCache::LocalExtensionCache( |
| 31 const base::FilePath& cache_dir, | 31 const base::FilePath& cache_dir, |
| 32 size_t max_cache_size, | 32 uint64 max_cache_size, |
| 33 const base::TimeDelta& max_cache_age, | 33 const base::TimeDelta& max_cache_age, |
| 34 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) | 34 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner) |
| 35 : cache_dir_(cache_dir), | 35 : cache_dir_(cache_dir), |
| 36 max_cache_size_(max_cache_size), | 36 max_cache_size_(max_cache_size), |
| 37 min_cache_age_(base::Time::Now() - max_cache_age), | 37 min_cache_age_(base::Time::Now() - max_cache_age), |
| 38 backend_task_runner_(backend_task_runner), | 38 backend_task_runner_(backend_task_runner), |
| 39 state_(kUninitialized), | 39 state_(kUninitialized), |
| 40 weak_ptr_factory_(this), | 40 weak_ptr_factory_(this), |
| 41 cache_status_polling_delay_( | 41 cache_status_polling_delay_( |
| 42 base::TimeDelta::FromMilliseconds(kCacheStatusPollingDelayMs)) { | 42 base::TimeDelta::FromMilliseconds(kCacheStatusPollingDelayMs)) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 144 |
| 145 backend_task_runner_->PostTask( | 145 backend_task_runner_->PostTask( |
| 146 FROM_HERE, | 146 FROM_HERE, |
| 147 base::Bind(&LocalExtensionCache::BackendRemoveCacheEntry, | 147 base::Bind(&LocalExtensionCache::BackendRemoveCacheEntry, |
| 148 it->second.file_path)); | 148 it->second.file_path)); |
| 149 | 149 |
| 150 cached_extensions_.erase(it); | 150 cached_extensions_.erase(it); |
| 151 return true; | 151 return true; |
| 152 } | 152 } |
| 153 | 153 |
| 154 bool LocalExtensionCache::GetStatistics(uint64* cache_size, |
| 155 size_t* extensions_count) { |
| 156 if (state_ != kReady) |
| 157 return false; |
| 158 |
| 159 *cache_size = 0; |
| 160 for (CacheMap::iterator it = cached_extensions_.begin(); |
| 161 it != cached_extensions_.end(); ++it) { |
| 162 *cache_size += it->second.size; |
| 163 } |
| 164 *extensions_count = cached_extensions_.size(); |
| 165 |
| 166 return true; |
| 167 } |
| 168 |
| 154 void LocalExtensionCache::SetCacheStatusPollingDelayForTests( | 169 void LocalExtensionCache::SetCacheStatusPollingDelayForTests( |
| 155 const base::TimeDelta& delay) { | 170 const base::TimeDelta& delay) { |
| 156 cache_status_polling_delay_ = delay; | 171 cache_status_polling_delay_ = delay; |
| 157 } | 172 } |
| 158 | 173 |
| 159 void LocalExtensionCache::CheckCacheStatus(const base::Closure& callback) { | 174 void LocalExtensionCache::CheckCacheStatus(const base::Closure& callback) { |
| 160 if (state_ == kShutdown) { | 175 if (state_ == kShutdown) { |
| 161 callback.Run(); | 176 callback.Run(); |
| 162 return; | 177 return; |
| 163 } | 178 } |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 bool LocalExtensionCache::CompareCacheItemsAge(const CacheMap::iterator& lhs, | 433 bool LocalExtensionCache::CompareCacheItemsAge(const CacheMap::iterator& lhs, |
| 419 const CacheMap::iterator& rhs) { | 434 const CacheMap::iterator& rhs) { |
| 420 return lhs->second.last_used < rhs->second.last_used; | 435 return lhs->second.last_used < rhs->second.last_used; |
| 421 } | 436 } |
| 422 | 437 |
| 423 void LocalExtensionCache::CleanUp() { | 438 void LocalExtensionCache::CleanUp() { |
| 424 DCHECK_EQ(state_, kReady); | 439 DCHECK_EQ(state_, kReady); |
| 425 | 440 |
| 426 std::vector<CacheMap::iterator> items; | 441 std::vector<CacheMap::iterator> items; |
| 427 items.reserve(cached_extensions_.size()); | 442 items.reserve(cached_extensions_.size()); |
| 428 size_t total_size = 0; | 443 uint64_t total_size = 0; |
| 429 for (CacheMap::iterator it = cached_extensions_.begin(); | 444 for (CacheMap::iterator it = cached_extensions_.begin(); |
| 430 it != cached_extensions_.end(); ++it) { | 445 it != cached_extensions_.end(); ++it) { |
| 431 items.push_back(it); | 446 items.push_back(it); |
| 432 total_size += it->second.size; | 447 total_size += it->second.size; |
| 433 } | 448 } |
| 434 std::sort(items.begin(), items.end(), CompareCacheItemsAge); | 449 std::sort(items.begin(), items.end(), CompareCacheItemsAge); |
| 435 | 450 |
| 436 for (std::vector<CacheMap::iterator>::iterator it = items.begin(); | 451 for (std::vector<CacheMap::iterator>::iterator it = items.begin(); |
| 437 it != items.end(); ++it) { | 452 it != items.end(); ++it) { |
| 438 if ((*it)->second.last_used < min_cache_age_ || | 453 if ((*it)->second.last_used < min_cache_age_ || |
| 439 (max_cache_size_ && total_size > max_cache_size_)) { | 454 (max_cache_size_ && total_size > max_cache_size_)) { |
| 440 total_size -= (*it)->second.size; | 455 total_size -= (*it)->second.size; |
| 441 VLOG(1) << "Clean up cached extension id " << (*it)->first; | 456 VLOG(1) << "Clean up cached extension id " << (*it)->first; |
| 442 RemoveExtension((*it)->first); | 457 RemoveExtension((*it)->first); |
| 443 } | 458 } |
| 444 } | 459 } |
| 445 } | 460 } |
| 446 | 461 |
| 447 LocalExtensionCache::CacheItemInfo::CacheItemInfo( | 462 LocalExtensionCache::CacheItemInfo::CacheItemInfo( |
| 448 const std::string& version, | 463 const std::string& version, |
| 449 const base::Time& last_used, | 464 const base::Time& last_used, |
| 450 const size_t size, | 465 const size_t size, |
| 451 const base::FilePath& file_path) | 466 const base::FilePath& file_path) |
| 452 : version(version), last_used(last_used), size(size), file_path(file_path) { | 467 : version(version), last_used(last_used), size(size), file_path(file_path) { |
| 453 } | 468 } |
| 454 | 469 |
| 455 } // namespace extensions | 470 } // namespace extensions |
| OLD | NEW |