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