Index: webkit/quota/quota_temporary_storage_evictor.cc |
diff --git a/webkit/quota/quota_temporary_storage_evictor.cc b/webkit/quota/quota_temporary_storage_evictor.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9596c7aebdc8f025086e11f33e16832cd63b9d48 |
--- /dev/null |
+++ b/webkit/quota/quota_temporary_storage_evictor.cc |
@@ -0,0 +1,86 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "webkit/quota/quota_temporary_storage_evictor.h" |
+ |
+#include "googleurl/src/gurl.h" |
+ |
+namespace quota { |
+ |
+const double QuotaTemporaryStorageEvictor::kUsageRatioToStartEviction = 0.7; |
+ |
+QuotaTemporaryStorageEvictor::QuotaTemporaryStorageEvictor( |
+ QuotaEvictionHandler* quota_eviction_handler, |
+ int64 interval_ms, |
+ scoped_refptr<base::MessageLoopProxy> io_thread) |
+ : kAvailableDiskSpaceToStartEviction(1000 * 1000 * 500), |
+ quota_eviction_handler_(quota_eviction_handler), |
+ interval_ms_(interval_ms), |
+ repeated_eviction_(false), |
+ io_thread_(io_thread), |
+ callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+ DCHECK(quota_eviction_handler); |
+} |
+ |
+QuotaTemporaryStorageEvictor::~QuotaTemporaryStorageEvictor() { |
+} |
+ |
+void QuotaTemporaryStorageEvictor::OnEvictionCompleted( |
+ QuotaStatusCode status) { |
+ DCHECK(io_thread_->BelongsToCurrentThread()); |
+ |
+ // Check if more eviction is immediately required? |
+ quota_eviction_handler_->GetUsageAndQuotaForEviction(callback_factory_. |
+ NewCallback(&QuotaTemporaryStorageEvictor::EvictIfRequired)); |
+} |
+ |
+void QuotaTemporaryStorageEvictor::DoEvict(const GURL& origin) { |
+ DCHECK(io_thread_->BelongsToCurrentThread()); |
+ |
+ if (origin.is_empty()) { |
+ if (repeated_eviction_) |
+ MayStartEviction(interval_ms_); |
+ return; |
+ } |
+ |
+ quota_eviction_handler_->EvictOriginData(origin, kStorageTypeTemporary, |
+ callback_factory_.NewCallback( |
+ &QuotaTemporaryStorageEvictor::OnEvictionCompleted)); |
+} |
+ |
+void QuotaTemporaryStorageEvictor::EvictIfRequired( |
+ QuotaStatusCode status, |
+ int64 usage, |
+ int64 quota, |
+ int64 physical_available_space) { |
+ DCHECK(io_thread_->BelongsToCurrentThread()); |
+ |
+ if (status == kQuotaStatusOk && |
+ (usage > quota * kUsageRatioToStartEviction || |
+ kAvailableDiskSpaceToStartEviction > physical_available_space)) { |
+ quota_eviction_handler_->GetLRUOrigin(kStorageTypeTemporary, |
+ callback_factory_.NewCallback(&QuotaTemporaryStorageEvictor::DoEvict)); |
+ } else if (repeated_eviction_) { |
+ MayStartEviction(interval_ms_); |
+ } |
+} |
+ |
+void QuotaTemporaryStorageEvictor::GetUsageAndQuotaThenEvict() { |
+ quota_eviction_handler_->GetUsageAndQuotaForEviction(callback_factory_. |
+ NewCallback(&QuotaTemporaryStorageEvictor::EvictIfRequired)); |
+} |
+ |
+void QuotaTemporaryStorageEvictor::MayStartEviction(int interval_ms) { |
+ if (timer_.IsRunning()) |
+ return; |
+ timer_.Start(base::TimeDelta::FromMilliseconds(interval_ms), this, |
+ &QuotaTemporaryStorageEvictor::GetUsageAndQuotaThenEvict); |
+} |
+ |
+void QuotaTemporaryStorageEvictor::Start() { |
+ DCHECK(io_thread_->BelongsToCurrentThread()); |
+ MayStartEviction(0); |
+} |
+ |
+} // namespace quota |