Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/quota/quota_temporary_storage_evictor.h" | |
| 6 | |
| 7 #include "googleurl/src/gurl.h" | |
| 8 | |
| 9 namespace quota { | |
| 10 | |
| 11 const double QuotaTemporaryStorageEvictor::kUsageRatioToStartEviction = 0.7; | |
| 12 const int64 QuotaTemporaryStorageEvictor:: | |
| 13 kDefaultMinAvailableDiskSpaceToStartEviction = 1000 * 1000 * 500; | |
| 14 | |
| 15 QuotaTemporaryStorageEvictor::QuotaTemporaryStorageEvictor( | |
| 16 QuotaEvictionHandler* quota_eviction_handler, | |
| 17 int64 interval_ms, | |
| 18 scoped_refptr<base::MessageLoopProxy> io_thread) | |
| 19 : min_available_disk_space_to_start_eviction_( | |
| 20 kDefaultMinAvailableDiskSpaceToStartEviction), | |
| 21 quota_eviction_handler_(quota_eviction_handler), | |
| 22 interval_ms_(interval_ms), | |
| 23 repeated_eviction_(false), | |
| 24 io_thread_(io_thread), | |
| 25 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 26 DCHECK(quota_eviction_handler); | |
| 27 } | |
| 28 | |
| 29 QuotaTemporaryStorageEvictor::~QuotaTemporaryStorageEvictor() { | |
| 30 } | |
| 31 | |
| 32 void QuotaTemporaryStorageEvictor::Start() { | |
| 33 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 34 StartEvictionTimerWithDelay(0); | |
| 35 } | |
| 36 | |
| 37 void QuotaTemporaryStorageEvictor::StartEvictionTimerWithDelay(int delay_ms) { | |
| 38 if (timer_.IsRunning()) | |
| 39 return; | |
| 40 timer_.Start(base::TimeDelta::FromMilliseconds(delay_ms), this, | |
| 41 &QuotaTemporaryStorageEvictor::ConsiderEviction); | |
| 42 } | |
| 43 | |
| 44 void QuotaTemporaryStorageEvictor::ConsiderEviction() { | |
| 45 // Get usage and disk space, then continue. | |
| 46 quota_eviction_handler_->GetUsageAndQuotaForEviction(callback_factory_. | |
| 47 NewCallback( | |
| 48 &QuotaTemporaryStorageEvictor::OnGotUsageAndQuotaForEviction)); | |
| 49 } | |
| 50 | |
| 51 void QuotaTemporaryStorageEvictor::OnGotUsageAndQuotaForEviction( | |
| 52 QuotaStatusCode status, | |
| 53 int64 usage, | |
| 54 int64 quota, | |
| 55 int64 available_disk_space) { | |
| 56 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 57 | |
| 58 if (status == kQuotaStatusOk && | |
| 59 (usage > quota * kUsageRatioToStartEviction || | |
| 60 min_available_disk_space_to_start_eviction_ > available_disk_space)) { | |
| 61 // Space is getting tight. Get the least recently used origin and continue. | |
| 62 quota_eviction_handler_->GetLRUOrigin(kStorageTypeTemporary, | |
| 63 callback_factory_.NewCallback( | |
| 64 &QuotaTemporaryStorageEvictor::OnGotLRUOrigin)); | |
| 65 } else if (repeated_eviction_) { | |
| 66 // No action required, sleep for a while and check again later. | |
| 67 StartEvictionTimerWithDelay(interval_ms_); | |
| 68 } | |
| 69 | |
| 70 // TODO(dmikurube): Add simple stats on # of {error, eviction, skipped}. | |
|
kinuko
2011/05/24 12:46:48
super-nit: no need to declare 'simple' in a TODO c
Dai Mikurube (NOT FULLTIME)
2011/05/25 05:26:31
Done.
| |
| 71 // TODO(dmikurube): Add error handling for the case status != kQuotaStatusOk. | |
| 72 } | |
| 73 | |
| 74 void QuotaTemporaryStorageEvictor::OnGotLRUOrigin(const GURL& origin) { | |
| 75 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 76 | |
| 77 if (origin.is_empty()) { | |
| 78 if (repeated_eviction_) | |
| 79 StartEvictionTimerWithDelay(interval_ms_); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 quota_eviction_handler_->EvictOriginData(origin, kStorageTypeTemporary, | |
| 84 callback_factory_.NewCallback( | |
| 85 &QuotaTemporaryStorageEvictor::OnEvictionComplete)); | |
| 86 } | |
| 87 | |
| 88 void QuotaTemporaryStorageEvictor::OnEvictionComplete( | |
| 89 QuotaStatusCode status) { | |
| 90 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 91 | |
| 92 // We many need to get rid of more space so reconsider immediately. | |
| 93 ConsiderEviction(); | |
| 94 } | |
| 95 | |
| 96 } // namespace quota | |
| OLD | NEW |