| 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..08609a1c1d6a55ad7735870822ce94b4536985f2
|
| --- /dev/null
|
| +++ b/webkit/quota/quota_temporary_storage_evictor.cc
|
| @@ -0,0 +1,132 @@
|
| +// 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 <list>
|
| +#include <vector>
|
| +
|
| +#include "base/message_loop.h"
|
| +#include "base/task.h"
|
| +#include "googleurl/src/gurl.h"
|
| +#include "webkit/quota/quota_client.h"
|
| +
|
| +namespace quota {
|
| +
|
| +const double QuotaTemporaryStorageEvictor::kUsageRatioToBeEvicted = 0.7;
|
| +
|
| +QuotaTemporaryStorageEvictor::QuotaTemporaryStorageEvictor(
|
| + int64 target_available_space,
|
| + int64 delay_ms,
|
| + scoped_refptr<base::MessageLoopProxy> io_message_loop)
|
| + : physical_available_space_to_be_evicted(1000 * 1000 * 500),
|
| + quota_manager_(NULL),
|
| + target_available_space_(target_available_space),
|
| + delay_ms_(delay_ms),
|
| + repeated_eviction_(false),
|
| + io_message_loop_(io_message_loop),
|
| + callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
|
| +}
|
| +
|
| +QuotaTemporaryStorageEvictor::~QuotaTemporaryStorageEvictor() {
|
| + LOG(ERROR) << "destruct";
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::RegisterQuotaManagerOnIOThread(
|
| + QuotaEvictionHandler* quota_manager) {
|
| + LOG(ERROR) << "RegisterQuotaManagerOnIOThread";
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + quota_manager_ = quota_manager;
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::OnQuotaManagerDestroyedOnIOThread() {
|
| + LOG(ERROR) << "OnQuotaManagerDestroyedOnIOThread";
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + LOG(ERROR) << "OnQuotaManagerDestroyedOnIOThread 2";
|
| + quota_manager_ = NULL;
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::OnDeletionCompletedOnIOThread(
|
| + QuotaStatusCode status,
|
| + int64 usage,
|
| + int64 quota,
|
| + int64 physical_available_space) {
|
| + LOG(ERROR) << "OnDeletionCompletedOnIOThread";
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| +
|
| + if (status == kQuotaStatusOk &&
|
| + (usage > quota * kUsageRatioToBeEvicted ||
|
| + physical_available_space_to_be_evicted > physical_available_space)) {
|
| + // Delete another origin immediately.
|
| + io_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
|
| + this, &QuotaTemporaryStorageEvictor::Evict, true));
|
| + } else if (repeated_eviction_) {
|
| + // Post the next delayed task.
|
| + // TODO(dmikurube): How to avoid that this DelayedTask is called in tests?
|
| + io_message_loop_->PostDelayedTask(FROM_HERE, NewRunnableMethod(
|
| + this, &QuotaTemporaryStorageEvictor::Evict, false), delay_ms_);
|
| + }
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::CallDeleteOriginOnIOThread(
|
| + const GURL& origin) {
|
| + LOG(ERROR) << "CallDeleteOriginOnIOThread";
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + if (quota_manager_ != NULL) {
|
| + quota_manager_->DeleteOriginData(
|
| + origin, kStorageTypeTemporary, callback_factory_.NewCallback(
|
| + &QuotaTemporaryStorageEvictor::OnDeletionCompletedOnIOThread));
|
| + }
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::CallGetUsageAndQuotaOnIOThread() {
|
| + LOG(ERROR) << "CallGetUsageAndQuotaOnIOThread";
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + if (quota_manager_ != NULL) {
|
| + quota_manager_->GetUsageAndQuotaForEviction(callback_factory_.NewCallback(
|
| + &QuotaTemporaryStorageEvictor::OnDeletionCompletedOnIOThread));
|
| + }
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::Evict(bool delete_immediately) {
|
| + LOG(ERROR) << "Evict: immedeate?=" << delete_immediately;
|
| + if (delete_immediately) {
|
| + GURL origin;
|
| + // origin = manager_->GetLRUOriginExceptFor(/* fs_type?, */ in_use);
|
| + origin = GURL("http://www.example.com"); // test.
|
| +
|
| + if (origin.is_empty()) {
|
| + LOG(ERROR) << "empty";
|
| + if (repeated_eviction_) {
|
| + io_message_loop_->PostDelayedTask(FROM_HERE, NewRunnableMethod(
|
| + this, &QuotaTemporaryStorageEvictor::Evict, false), delay_ms_);
|
| + }
|
| + return;
|
| + }
|
| +
|
| + io_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
|
| + &QuotaTemporaryStorageEvictor::CallDeleteOriginOnIOThread, origin));
|
| + } else {
|
| + io_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
|
| + &QuotaTemporaryStorageEvictor::CallGetUsageAndQuotaOnIOThread));
|
| + }
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::Start() {
|
| + LOG(ERROR) << "Start";
|
| + io_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
|
| + this, &QuotaTemporaryStorageEvictor::Evict, false));
|
| +}
|
| +
|
| +void QuotaTemporaryStorageEvictor::DeleteOnCorrectThread() const {
|
| + LOG(ERROR) << "DeleteOnCorrectThread";
|
| + if (!io_message_loop_->BelongsToCurrentThread()) {
|
| + LOG(ERROR) << "io";
|
| + io_message_loop_->DeleteSoon(FROM_HERE, this);
|
| + return;
|
| + }
|
| + delete this;
|
| +}
|
| +
|
| +} // namespace quota
|
|
|