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 |
| 13 QuotaTemporaryStorageEvictor::QuotaTemporaryStorageEvictor( |
| 14 QuotaEvictionHandler* quota_eviction_handler, |
| 15 int64 interval_ms, |
| 16 scoped_refptr<base::MessageLoopProxy> io_thread) |
| 17 : kAvailableDiskSpaceToStartEviction(1000 * 1000 * 500), |
| 18 quota_eviction_handler_(quota_eviction_handler), |
| 19 interval_ms_(interval_ms), |
| 20 repeated_eviction_(false), |
| 21 io_thread_(io_thread), |
| 22 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 23 DCHECK(quota_eviction_handler); |
| 24 } |
| 25 |
| 26 QuotaTemporaryStorageEvictor::~QuotaTemporaryStorageEvictor() { |
| 27 } |
| 28 |
| 29 void QuotaTemporaryStorageEvictor::OnEvictionCompleted( |
| 30 QuotaStatusCode status) { |
| 31 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 32 |
| 33 // Check if more eviction is immediately required? |
| 34 quota_eviction_handler_->GetUsageAndQuotaForEviction(callback_factory_. |
| 35 NewCallback(&QuotaTemporaryStorageEvictor::EvictIfRequired)); |
| 36 } |
| 37 |
| 38 void QuotaTemporaryStorageEvictor::DoEvict(const GURL& origin) { |
| 39 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 40 |
| 41 if (origin.is_empty()) { |
| 42 if (repeated_eviction_) |
| 43 MayStartEviction(interval_ms_); |
| 44 return; |
| 45 } |
| 46 |
| 47 quota_eviction_handler_->EvictOriginData(origin, kStorageTypeTemporary, |
| 48 callback_factory_.NewCallback( |
| 49 &QuotaTemporaryStorageEvictor::OnEvictionCompleted)); |
| 50 } |
| 51 |
| 52 void QuotaTemporaryStorageEvictor::EvictIfRequired( |
| 53 QuotaStatusCode status, |
| 54 int64 usage, |
| 55 int64 quota, |
| 56 int64 physical_available_space) { |
| 57 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 58 |
| 59 if (status == kQuotaStatusOk && |
| 60 (usage > quota * kUsageRatioToStartEviction || |
| 61 kAvailableDiskSpaceToStartEviction > physical_available_space)) { |
| 62 quota_eviction_handler_->GetLRUOrigin(kStorageTypeTemporary, |
| 63 callback_factory_.NewCallback(&QuotaTemporaryStorageEvictor::DoEvict)); |
| 64 } else if (repeated_eviction_) { |
| 65 MayStartEviction(interval_ms_); |
| 66 } |
| 67 } |
| 68 |
| 69 void QuotaTemporaryStorageEvictor::GetUsageAndQuotaThenEvict() { |
| 70 quota_eviction_handler_->GetUsageAndQuotaForEviction(callback_factory_. |
| 71 NewCallback(&QuotaTemporaryStorageEvictor::EvictIfRequired)); |
| 72 } |
| 73 |
| 74 void QuotaTemporaryStorageEvictor::MayStartEviction(int interval_ms) { |
| 75 if (timer_.IsRunning()) |
| 76 return; |
| 77 timer_.Start(base::TimeDelta::FromMilliseconds(interval_ms), this, |
| 78 &QuotaTemporaryStorageEvictor::GetUsageAndQuotaThenEvict); |
| 79 } |
| 80 |
| 81 void QuotaTemporaryStorageEvictor::Start() { |
| 82 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 83 MayStartEviction(0); |
| 84 } |
| 85 |
| 86 } // namespace quota |
OLD | NEW |