Implement QuotaTemporaryStorageEvictor.

It's based on http://codereview.chromium.org/7029009/.

BUG=61676
TEST=QuotaTemporaryStorageEvictorTest.*

Review URL: http://codereview.chromium.org/7002024

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86597 0039d316-1c4b-4281-b951-d872f2087c98
parent c4784992
...@@ -38,6 +38,8 @@ class QuotaManagerProxy; ...@@ -38,6 +38,8 @@ class QuotaManagerProxy;
// An interface called by QuotaTemporaryStorageEvictor. // An interface called by QuotaTemporaryStorageEvictor.
class QuotaEvictionHandler { class QuotaEvictionHandler {
public: public:
virtual ~QuotaEvictionHandler() {}
typedef Callback1<const GURL&>::Type GetLRUOriginCallback; typedef Callback1<const GURL&>::Type GetLRUOriginCallback;
typedef Callback1<QuotaStatusCode>::Type EvictOriginDataCallback; typedef Callback1<QuotaStatusCode>::Type EvictOriginDataCallback;
typedef Callback4<QuotaStatusCode, typedef Callback4<QuotaStatusCode,
...@@ -59,9 +61,6 @@ class QuotaEvictionHandler { ...@@ -59,9 +61,6 @@ class QuotaEvictionHandler {
virtual void GetUsageAndQuotaForEviction( virtual void GetUsageAndQuotaForEviction(
GetUsageAndQuotaForEvictionCallback* callback) = 0; GetUsageAndQuotaForEvictionCallback* callback) = 0;
protected:
virtual ~QuotaEvictionHandler() {}
}; };
// The quota manager class. This class is instantiated per profile and // The quota manager class. This class is instantiated per profile and
......
// 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 "base/message_loop_proxy.h"
#include "googleurl/src/gurl.h"
#include "webkit/quota/quota_manager.h"
namespace quota {
const double QuotaTemporaryStorageEvictor::kUsageRatioToStartEviction = 0.7;
const int64 QuotaTemporaryStorageEvictor::
kDefaultMinAvailableDiskSpaceToStartEviction = 1000 * 1000 * 500;
QuotaTemporaryStorageEvictor::QuotaTemporaryStorageEvictor(
QuotaEvictionHandler* quota_eviction_handler,
int64 interval_ms,
scoped_refptr<base::MessageLoopProxy> io_thread)
: min_available_disk_space_to_start_eviction_(
kDefaultMinAvailableDiskSpaceToStartEviction),
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::Start() {
DCHECK(io_thread_->BelongsToCurrentThread());
StartEvictionTimerWithDelay(0);
}
void QuotaTemporaryStorageEvictor::StartEvictionTimerWithDelay(int delay_ms) {
if (timer_.IsRunning())
return;
timer_.Start(base::TimeDelta::FromMilliseconds(delay_ms), this,
&QuotaTemporaryStorageEvictor::ConsiderEviction);
}
void QuotaTemporaryStorageEvictor::ConsiderEviction() {
// Get usage and disk space, then continue.
quota_eviction_handler_->GetUsageAndQuotaForEviction(callback_factory_.
NewCallback(
&QuotaTemporaryStorageEvictor::OnGotUsageAndQuotaForEviction));
}
void QuotaTemporaryStorageEvictor::OnGotUsageAndQuotaForEviction(
QuotaStatusCode status,
int64 usage,
int64 quota,
int64 available_disk_space) {
DCHECK(io_thread_->BelongsToCurrentThread());
if (status == kQuotaStatusOk &&
(usage > quota * kUsageRatioToStartEviction ||
min_available_disk_space_to_start_eviction_ > available_disk_space)) {
// Space is getting tight. Get the least recently used origin and continue.
quota_eviction_handler_->GetLRUOrigin(kStorageTypeTemporary,
callback_factory_.NewCallback(
&QuotaTemporaryStorageEvictor::OnGotLRUOrigin));
} else if (repeated_eviction_) {
// No action required, sleep for a while and check again later.
StartEvictionTimerWithDelay(interval_ms_);
}
// TODO(dmikurube): Add stats on # of {error, eviction, skipped}.
// TODO(dmikurube): Add error handling for the case status != kQuotaStatusOk.
}
void QuotaTemporaryStorageEvictor::OnGotLRUOrigin(const GURL& origin) {
DCHECK(io_thread_->BelongsToCurrentThread());
if (origin.is_empty()) {
if (repeated_eviction_)
StartEvictionTimerWithDelay(interval_ms_);
return;
}
quota_eviction_handler_->EvictOriginData(origin, kStorageTypeTemporary,
callback_factory_.NewCallback(
&QuotaTemporaryStorageEvictor::OnEvictionComplete));
}
void QuotaTemporaryStorageEvictor::OnEvictionComplete(
QuotaStatusCode status) {
DCHECK(io_thread_->BelongsToCurrentThread());
// We many need to get rid of more space so reconsider immediately.
ConsiderEviction();
}
} // namespace quota
// 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.
#ifndef WEBKIT_QUOTA_QUOTA_TEMPORARY_STORAGE_EVICTOR_H_
#define WEBKIT_QUOTA_QUOTA_TEMPORARY_STORAGE_EVICTOR_H_
#pragma once
#include "base/memory/scoped_callback_factory.h"
#include "base/timer.h"
#include "webkit/quota/quota_types.h"
class GURL;
namespace base {
class MessageLoopProxy;
}
namespace quota {
class QuotaEvictionHandler;
class QuotaTemporaryStorageEvictor {
public:
QuotaTemporaryStorageEvictor(
QuotaEvictionHandler* quota_eviction_handler,
int64 interval_ms,
scoped_refptr<base::MessageLoopProxy> io_thread);
virtual ~QuotaTemporaryStorageEvictor();
void Start();
bool repeated_eviction() const { return repeated_eviction_; }
void set_repeated_eviction(bool repeated_eviction) {
repeated_eviction_ = repeated_eviction;
}
private:
friend class QuotaTemporaryStorageEvictorTest;
void StartEvictionTimerWithDelay(int delay_ms);
void ConsiderEviction();
void OnGotUsageAndQuotaForEviction(
QuotaStatusCode status,
int64 usage,
int64 quota,
int64 available_disk_space);
void OnGotLRUOrigin(const GURL& origin);
void OnEvictionComplete(QuotaStatusCode status);
static const double kUsageRatioToStartEviction;
static const int64 kDefaultMinAvailableDiskSpaceToStartEviction;
const int64 min_available_disk_space_to_start_eviction_;
// Not owned; quota_eviction_handler owns us.
QuotaEvictionHandler* quota_eviction_handler_;
int64 interval_ms_;
bool repeated_eviction_;
scoped_refptr<base::MessageLoopProxy> io_thread_;
base::OneShotTimer<QuotaTemporaryStorageEvictor> timer_;
base::ScopedCallbackFactory<QuotaTemporaryStorageEvictor> callback_factory_;
DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictor);
};
} // namespace quota
#endif // WEBKIT_QUOTA_QUOTA_TEMPORARY_STORAGE_EVICTOR_H_
This diff is collapsed.
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
'quota_manager.h', 'quota_manager.h',
'quota_task.cc', 'quota_task.cc',
'quota_task.h', 'quota_task.h',
'quota_temporary_storage_evictor.cc',
'quota_temporary_storage_evictor.h',
'quota_types.h', 'quota_types.h',
'special_storage_policy.cc', 'special_storage_policy.cc',
'special_storage_policy.h', 'special_storage_policy.h',
......
...@@ -438,6 +438,7 @@ ...@@ -438,6 +438,7 @@
'../../quota/mock_storage_client.h', '../../quota/mock_storage_client.h',
'../../quota/quota_database_unittest.cc', '../../quota/quota_database_unittest.cc',
'../../quota/quota_manager_unittest.cc', '../../quota/quota_manager_unittest.cc',
'../../quota/quota_temporary_storage_evictor_unittest.cc',
'../webcore_unit_tests/BMPImageDecoder_unittest.cpp', '../webcore_unit_tests/BMPImageDecoder_unittest.cpp',
'../webcore_unit_tests/ICOImageDecoder_unittest.cpp', '../webcore_unit_tests/ICOImageDecoder_unittest.cpp',
'event_listener_unittest.cc', 'event_listener_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment