| 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 uint64 max_cache_size, | 32 size_t 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 | |
| 169 void LocalExtensionCache::SetCacheStatusPollingDelayForTests( | 154 void LocalExtensionCache::SetCacheStatusPollingDelayForTests( |
| 170 const base::TimeDelta& delay) { | 155 const base::TimeDelta& delay) { |
| 171 cache_status_polling_delay_ = delay; | 156 cache_status_polling_delay_ = delay; |
| 172 } | 157 } |
| 173 | 158 |
| 174 void LocalExtensionCache::CheckCacheStatus(const base::Closure& callback) { | 159 void LocalExtensionCache::CheckCacheStatus(const base::Closure& callback) { |
| 175 if (state_ == kShutdown) { | 160 if (state_ == kShutdown) { |
| 176 callback.Run(); | 161 callback.Run(); |
| 177 return; | 162 return; |
| 178 } | 163 } |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 bool LocalExtensionCache::CompareCacheItemsAge(const CacheMap::iterator& lhs, | 418 bool LocalExtensionCache::CompareCacheItemsAge(const CacheMap::iterator& lhs, |
| 434 const CacheMap::iterator& rhs) { | 419 const CacheMap::iterator& rhs) { |
| 435 return lhs->second.last_used < rhs->second.last_used; | 420 return lhs->second.last_used < rhs->second.last_used; |
| 436 } | 421 } |
| 437 | 422 |
| 438 void LocalExtensionCache::CleanUp() { | 423 void LocalExtensionCache::CleanUp() { |
| 439 DCHECK_EQ(state_, kReady); | 424 DCHECK_EQ(state_, kReady); |
| 440 | 425 |
| 441 std::vector<CacheMap::iterator> items; | 426 std::vector<CacheMap::iterator> items; |
| 442 items.reserve(cached_extensions_.size()); | 427 items.reserve(cached_extensions_.size()); |
| 443 uint64_t total_size = 0; | 428 size_t total_size = 0; |
| 444 for (CacheMap::iterator it = cached_extensions_.begin(); | 429 for (CacheMap::iterator it = cached_extensions_.begin(); |
| 445 it != cached_extensions_.end(); ++it) { | 430 it != cached_extensions_.end(); ++it) { |
| 446 items.push_back(it); | 431 items.push_back(it); |
| 447 total_size += it->second.size; | 432 total_size += it->second.size; |
| 448 } | 433 } |
| 449 std::sort(items.begin(), items.end(), CompareCacheItemsAge); | 434 std::sort(items.begin(), items.end(), CompareCacheItemsAge); |
| 450 | 435 |
| 451 for (std::vector<CacheMap::iterator>::iterator it = items.begin(); | 436 for (std::vector<CacheMap::iterator>::iterator it = items.begin(); |
| 452 it != items.end(); ++it) { | 437 it != items.end(); ++it) { |
| 453 if ((*it)->second.last_used < min_cache_age_ || | 438 if ((*it)->second.last_used < min_cache_age_ || |
| 454 (max_cache_size_ && total_size > max_cache_size_)) { | 439 (max_cache_size_ && total_size > max_cache_size_)) { |
| 455 total_size -= (*it)->second.size; | 440 total_size -= (*it)->second.size; |
| 456 VLOG(1) << "Clean up cached extension id " << (*it)->first; | 441 VLOG(1) << "Clean up cached extension id " << (*it)->first; |
| 457 RemoveExtension((*it)->first); | 442 RemoveExtension((*it)->first); |
| 458 } | 443 } |
| 459 } | 444 } |
| 460 } | 445 } |
| 461 | 446 |
| 462 LocalExtensionCache::CacheItemInfo::CacheItemInfo( | 447 LocalExtensionCache::CacheItemInfo::CacheItemInfo( |
| 463 const std::string& version, | 448 const std::string& version, |
| 464 const base::Time& last_used, | 449 const base::Time& last_used, |
| 465 const size_t size, | 450 const size_t size, |
| 466 const base::FilePath& file_path) | 451 const base::FilePath& file_path) |
| 467 : version(version), last_used(last_used), size(size), file_path(file_path) { | 452 : version(version), last_used(last_used), size(size), file_path(file_path) { |
| 468 } | 453 } |
| 469 | 454 |
| 470 } // namespace extensions | 455 } // namespace extensions |
| OLD | NEW |