diff options
author | nhiroki@chromium.org <nhiroki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-08 07:42:25 +0000 |
---|---|---|
committer | nhiroki@chromium.org <nhiroki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-08 07:42:25 +0000 |
commit | ed222dfc734a95dcfb33ba8d99fcf7841b28a24a (patch) | |
tree | 3c9d3b1b570f27826f7dc069d3517b8002179879 /webkit/browser/fileapi | |
parent | a07346b59280824c8623115ce2f396cca43e4a69 (diff) | |
download | chromium_src-ed222dfc734a95dcfb33ba8d99fcf7841b28a24a.zip chromium_src-ed222dfc734a95dcfb33ba8d99fcf7841b28a24a.tar.gz chromium_src-ed222dfc734a95dcfb33ba8d99fcf7841b28a24a.tar.bz2 |
Quota: Implement QuotaBackendImpl
This introduces QuotaBackendImpl working as bridge among FileSystemUsageCache,
QuotaManager and QuotaReservationManager. The backend will be created by
FileSystemContext and retained by QuotaReservationManager.
BUG=303443
TEST=content_unittests --gtest_filter=QuotaBackendImplTest.*
TEST=content_unittests --gtest_filter=QuotaReservationManagerTest.*
TBR=jochen@chromium.org
Review URL: https://codereview.chromium.org/61593002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233815 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/browser/fileapi')
10 files changed, 538 insertions, 54 deletions
diff --git a/webkit/browser/fileapi/obfuscated_file_util.h b/webkit/browser/fileapi/obfuscated_file_util.h index e25a99e..05d5530 100644 --- a/webkit/browser/fileapi/obfuscated_file_util.h +++ b/webkit/browser/fileapi/obfuscated_file_util.h @@ -230,6 +230,7 @@ class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE ObfuscatedFileUtil friend class ObfuscatedFileEnumerator; friend class ObfuscatedFileUtilTest; + friend class QuotaBackendImplTest; FRIEND_TEST_ALL_PREFIXES(ObfuscatedFileUtilTest, MaybeDropDatabasesAliveCase); FRIEND_TEST_ALL_PREFIXES(ObfuscatedFileUtilTest, MaybeDropDatabasesAlreadyDeletedCase); diff --git a/webkit/browser/fileapi/quota/quota_backend_impl.cc b/webkit/browser/fileapi/quota/quota_backend_impl.cc new file mode 100644 index 0000000..70dbd3d --- /dev/null +++ b/webkit/browser/fileapi/quota/quota_backend_impl.cc @@ -0,0 +1,148 @@ +// Copyright 2013 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/browser/fileapi/quota/quota_backend_impl.h" + +#include <string> + +#include "base/bind.h" +#include "base/callback.h" +#include "base/logging.h" +#include "base/sequenced_task_runner.h" +#include "webkit/browser/fileapi/file_system_usage_cache.h" +#include "webkit/browser/quota/quota_client.h" +#include "webkit/browser/quota/quota_manager.h" +#include "webkit/common/fileapi/file_system_util.h" + +namespace fileapi { + +QuotaBackendImpl::QuotaBackendImpl( + base::SequencedTaskRunner* file_task_runner, + ObfuscatedFileUtil* obfuscated_file_util, + FileSystemUsageCache* file_system_usage_cache, + quota::QuotaManagerProxy* quota_manager_proxy) + : file_task_runner_(file_task_runner), + obfuscated_file_util_(obfuscated_file_util), + file_system_usage_cache_(file_system_usage_cache), + quota_manager_proxy_(quota_manager_proxy), + weak_ptr_factory_(this) { +} + +QuotaBackendImpl::~QuotaBackendImpl() { +} + +void QuotaBackendImpl::ReserveQuota(const GURL& origin, + FileSystemType type, + int64 delta, + const ReserveQuotaCallback& callback) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + if (!delta) { + callback.Run(base::PLATFORM_FILE_OK); + return; + } + DCHECK(quota_manager_proxy_); + quota_manager_proxy_->GetUsageAndQuota( + file_task_runner_, origin, FileSystemTypeToQuotaStorageType(type), + base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota, + weak_ptr_factory_.GetWeakPtr(), + QuotaReservationInfo(origin, type, delta), callback)); +} + +void QuotaBackendImpl::ReleaseReservedQuota(const GURL& origin, + FileSystemType type, + int64 size) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + DCHECK_LE(0, size); + if (!size) + return; + ReserveQuotaInternal(QuotaReservationInfo(origin, type, -size)); +} + +void QuotaBackendImpl::CommitQuotaUsage(const GURL& origin, + FileSystemType type, + int64 delta) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + if (!delta) + return; + ReserveQuotaInternal(QuotaReservationInfo(origin, type, delta)); + base::FilePath path; + if (GetUsageCachePath(origin, type, &path) != base::PLATFORM_FILE_OK) + return; + bool result = file_system_usage_cache_->AtomicUpdateUsageByDelta(path, delta); + DCHECK(result); +} + +void QuotaBackendImpl::IncrementDirtyCount(const GURL& origin, + FileSystemType type) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + base::FilePath path; + if (GetUsageCachePath(origin, type, &path) != base::PLATFORM_FILE_OK) + return; + DCHECK(file_system_usage_cache_); + file_system_usage_cache_->IncrementDirty(path); +} + +void QuotaBackendImpl::DecrementDirtyCount(const GURL& origin, + FileSystemType type) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + base::FilePath path; + if (GetUsageCachePath(origin, type, &path) != base::PLATFORM_FILE_OK) + return; + DCHECK(file_system_usage_cache_); + file_system_usage_cache_->DecrementDirty(path); +} + +void QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota( + const QuotaReservationInfo& info, + const ReserveQuotaCallback& callback, + quota::QuotaStatusCode status, int64 usage, int64 quota) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + if (status != quota::kQuotaStatusOk) { + callback.Run(base::PLATFORM_FILE_ERROR_FAILED); + return; + } + + if (quota < usage + info.delta) { + callback.Run(base::PLATFORM_FILE_ERROR_NO_SPACE); + return; + } + + ReserveQuotaInternal(info); + if (callback.Run(base::PLATFORM_FILE_OK)) + return; + // The requester could not accept the reserved quota. Revert it. + ReserveQuotaInternal( + QuotaReservationInfo(info.origin, info.type, -info.delta)); +} + +void QuotaBackendImpl::ReserveQuotaInternal(const QuotaReservationInfo& info) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + DCHECK(quota_manager_proxy_); + quota_manager_proxy_->NotifyStorageModified( + quota::QuotaClient::kFileSystem, info.origin, + FileSystemTypeToQuotaStorageType(info.type), info.delta); +} + +base::PlatformFileError QuotaBackendImpl::GetUsageCachePath( + const GURL& origin, + FileSystemType type, + base::FilePath* usage_file_path) { + DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); + DCHECK(usage_file_path); + base::PlatformFileError error = base::PLATFORM_FILE_OK; + *usage_file_path = + SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType( + obfuscated_file_util_, origin, type, &error); + return error; +} + +QuotaBackendImpl::QuotaReservationInfo::QuotaReservationInfo( + const GURL& origin, FileSystemType type, int64 delta) + : origin(origin), type(type), delta(delta) { +} + +QuotaBackendImpl::QuotaReservationInfo::~QuotaReservationInfo() { +} + +} // namespace fileapi diff --git a/webkit/browser/fileapi/quota/quota_backend_impl.h b/webkit/browser/fileapi/quota/quota_backend_impl.h new file mode 100644 index 0000000..10c38e7 --- /dev/null +++ b/webkit/browser/fileapi/quota/quota_backend_impl.h @@ -0,0 +1,103 @@ +// Copyright 2013 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_BROWSER_FILEAPI_QUOTA_QUOTA_BACKEND_IMPL_H_ +#define WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_BACKEND_IMPL_H_ + +#include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" +#include "webkit/browser/fileapi/quota/quota_reservation_manager.h" +#include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h" +#include "webkit/browser/webkit_storage_browser_export.h" +#include "webkit/common/quota/quota_status_code.h" + +namespace base { +class SequencedTaskRunner; +} + +namespace quota { +class QuotaManagerProxy; +} + +namespace fileapi { + +class FileSystemUsageCache; +class ObfuscatedFileUtil; + +// An instance of this class is owned by QuotaReservationManager. +class WEBKIT_STORAGE_BROWSER_EXPORT QuotaBackendImpl + : public QuotaReservationManager::QuotaBackend { + public: + typedef QuotaReservationManager::ReserveQuotaCallback + ReserveQuotaCallback; + + QuotaBackendImpl(base::SequencedTaskRunner* file_task_runner, + ObfuscatedFileUtil* obfuscated_file_util, + FileSystemUsageCache* file_system_usage_cache, + quota::QuotaManagerProxy* quota_manager_proxy); + virtual ~QuotaBackendImpl(); + + // QuotaReservationManager::QuotaBackend overrides. + virtual void ReserveQuota( + const GURL& origin, + FileSystemType type, + int64 delta, + const ReserveQuotaCallback& callback) OVERRIDE; + virtual void ReleaseReservedQuota( + const GURL& origin, + FileSystemType type, + int64 size) OVERRIDE; + virtual void CommitQuotaUsage( + const GURL& origin, + FileSystemType type, + int64 delta) OVERRIDE; + virtual void IncrementDirtyCount( + const GURL& origin, + FileSystemType type) OVERRIDE; + virtual void DecrementDirtyCount( + const GURL& origin, + FileSystemType type) OVERRIDE; + + private: + friend class QuotaBackendImplTest; + + struct QuotaReservationInfo { + QuotaReservationInfo(const GURL& origin, FileSystemType type, int64 delta); + ~QuotaReservationInfo(); + + GURL origin; + FileSystemType type; + int64 delta; + }; + + void DidGetUsageAndQuotaForReserveQuota( + const QuotaReservationInfo& info, + const ReserveQuotaCallback& callback, + quota::QuotaStatusCode status, + int64 usage, + int64 quota); + + void ReserveQuotaInternal( + const QuotaReservationInfo& info); + base::PlatformFileError GetUsageCachePath( + const GURL& origin, + FileSystemType type, + base::FilePath* usage_file_path); + + scoped_refptr<base::SequencedTaskRunner> file_task_runner_; + + // Owned by SandboxFileSystemBackendDelegate. + ObfuscatedFileUtil* obfuscated_file_util_; + FileSystemUsageCache* file_system_usage_cache_; + + scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; + + base::WeakPtrFactory<QuotaBackendImpl> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(QuotaBackendImpl); +}; + +} // namespace fileapi + +#endif // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_BACKEND_IMPL_H_ diff --git a/webkit/browser/fileapi/quota/quota_backend_impl_unittest.cc b/webkit/browser/fileapi/quota/quota_backend_impl_unittest.cc new file mode 100644 index 0000000..bd1483a --- /dev/null +++ b/webkit/browser/fileapi/quota/quota_backend_impl_unittest.cc @@ -0,0 +1,243 @@ +// Copyright 2013 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/browser/fileapi/quota/quota_backend_impl.h" + +#include "base/files/scoped_temp_dir.h" +#include "base/message_loop/message_loop.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "webkit/browser/fileapi/file_system_usage_cache.h" +#include "webkit/browser/fileapi/obfuscated_file_util.h" +#include "webkit/browser/quota/quota_manager.h" + +namespace fileapi { + +namespace { + +const char kOrigin[] = "http://example.com"; + +bool DidReserveQuota(bool accepted, + base::PlatformFileError* error_out, + base::PlatformFileError error) { + DCHECK(error_out); + *error_out = error; + return accepted; +} + +class MockQuotaManagerProxy : public quota::QuotaManagerProxy { + public: + MockQuotaManagerProxy() + : QuotaManagerProxy(NULL, NULL), + storage_modified_count_(0), + usage_(0), quota_(0) {} + + // We don't mock them. + virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} + virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} + virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id, + const GURL& origin, + quota::StorageType type, + bool enabled) OVERRIDE {} + + virtual void NotifyStorageModified( + quota::QuotaClient::ID client_id, + const GURL& origin, + quota::StorageType type, + int64 delta) OVERRIDE { + ++storage_modified_count_; + usage_ += delta; + ASSERT_LT(usage_, quota_); + } + + virtual void GetUsageAndQuota( + base::SequencedTaskRunner* original_task_runner, + const GURL& origin, + quota::StorageType type, + const GetUsageAndQuotaCallback& callback) OVERRIDE { + callback.Run(quota::kQuotaStatusOk, usage_, quota_); + } + + int storage_modified_count() { return storage_modified_count_; } + int64 usage() { return usage_; } + void set_usage(int64 usage) { usage_ = usage; } + void set_quota(int64 quota) { quota_ = quota; } + + protected: + virtual ~MockQuotaManagerProxy() {} + + private: + int storage_modified_count_; + int64 usage_; + int64 quota_; + + DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerProxy); +}; + +} // namespace + +class QuotaBackendImplTest : public testing::Test { + public: + QuotaBackendImplTest() + : file_system_usage_cache_(file_task_runner()), + quota_manager_proxy_(new MockQuotaManagerProxy) {} + + virtual void SetUp() OVERRIDE { + ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); + file_util_.reset(ObfuscatedFileUtil::CreateForTesting( + NULL, data_dir_.path(), file_task_runner())); + backend_.reset(new QuotaBackendImpl(file_task_runner(), + file_util_.get(), + &file_system_usage_cache_, + quota_manager_proxy_.get())); + } + + virtual void TearDown() OVERRIDE { + backend_.reset(); + quota_manager_proxy_ = NULL; + file_util_.reset(); + message_loop_.RunUntilIdle(); + } + + protected: + void InitializeForOriginAndType(const GURL& origin, FileSystemType type) { + file_util_->InitOriginDatabase(origin, true /* create */); + ASSERT_TRUE(file_util_->origin_database_ != NULL); + + std::string type_string = + SandboxFileSystemBackendDelegate::GetTypeString(type); + base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; + base::FilePath path = file_util_->GetDirectoryForOriginAndType( + origin, type_string, true /* create */, &error); + ASSERT_EQ(base::PLATFORM_FILE_OK, error); + file_system_usage_cache_.UpdateUsage(GetUsageCachePath(origin, type), 0); + } + + base::SequencedTaskRunner* file_task_runner() { + return base::MessageLoopProxy::current().get(); + } + + base::FilePath GetUsageCachePath(const GURL& origin, FileSystemType type) { + base::FilePath path; + DCHECK_EQ(base::PLATFORM_FILE_OK, + backend_->GetUsageCachePath(origin, type, &path)); + DCHECK(!path.empty()); + return path; + } + + base::MessageLoop message_loop_; + base::ScopedTempDir data_dir_; + scoped_ptr<ObfuscatedFileUtil> file_util_; + FileSystemUsageCache file_system_usage_cache_; + scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; + scoped_ptr<QuotaBackendImpl> backend_; + + private: + DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); +}; + +TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { + FileSystemType type = fileapi::kFileSystemTypeTemporary; + InitializeForOriginAndType(GURL(kOrigin), type); + quota_manager_proxy_->set_quota(10000); + + const int64 kDelta1 = 1000; + base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; + backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, + base::Bind(&DidReserveQuota, true, &error)); + EXPECT_EQ(base::PLATFORM_FILE_OK, error); + EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); + + const int64 kDelta2 = -300; + error = base::PLATFORM_FILE_ERROR_FAILED; + backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, + base::Bind(&DidReserveQuota, true, &error)); + EXPECT_EQ(base::PLATFORM_FILE_OK, error); + EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); + + EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); +} + +TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { + FileSystemType type = fileapi::kFileSystemTypeTemporary; + InitializeForOriginAndType(GURL(kOrigin), type); + quota_manager_proxy_->set_quota(100); + + const int64 kDelta = 1000; + base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; + backend_->ReserveQuota(GURL(kOrigin), type, kDelta, + base::Bind(&DidReserveQuota, true, &error)); + EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, error); + EXPECT_EQ(0, quota_manager_proxy_->usage()); + + EXPECT_EQ(0, quota_manager_proxy_->storage_modified_count()); +} + +TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { + FileSystemType type = fileapi::kFileSystemTypeTemporary; + InitializeForOriginAndType(GURL(kOrigin), type); + quota_manager_proxy_->set_quota(10000); + + const int64 kDelta = 1000; + base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; + backend_->ReserveQuota(GURL(kOrigin), type, kDelta, + base::Bind(&DidReserveQuota, false, &error)); + EXPECT_EQ(base::PLATFORM_FILE_OK, error); + EXPECT_EQ(0, quota_manager_proxy_->usage()); + + EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); +} + +TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { + FileSystemType type = fileapi::kFileSystemTypeTemporary; + InitializeForOriginAndType(GURL(kOrigin), type); + const int64 kInitialUsage = 2000; + quota_manager_proxy_->set_usage(kInitialUsage); + quota_manager_proxy_->set_quota(10000); + + const int64 kSize = 1000; + backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); + EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); + + EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); +} + +TEST_F(QuotaBackendImplTest, CommitQuotaUsage) { + FileSystemType type = fileapi::kFileSystemTypeTemporary; + InitializeForOriginAndType(GURL(kOrigin), type); + quota_manager_proxy_->set_quota(10000); + base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); + + const int64 kDelta1 = 1000; + backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1); + EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); + int64 usage = 0; + EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); + EXPECT_EQ(kDelta1, usage); + + const int64 kDelta2 = -300; + backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2); + EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); + usage = 0; + EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); + EXPECT_EQ(kDelta1 + kDelta2, usage); + + EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); +} + +TEST_F(QuotaBackendImplTest, DirtyCount) { + FileSystemType type = fileapi::kFileSystemTypeTemporary; + InitializeForOriginAndType(GURL(kOrigin), type); + base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); + + backend_->IncrementDirtyCount(GURL(kOrigin), type); + uint32 dirty = 0; + ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); + EXPECT_EQ(1u, dirty); + + backend_->DecrementDirtyCount(GURL(kOrigin), type); + ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); + EXPECT_EQ(0u, dirty); +} + +} // namespace fileapi diff --git a/webkit/browser/fileapi/quota/quota_reservation_buffer.cc b/webkit/browser/fileapi/quota/quota_reservation_buffer.cc index c83073e..4a4e036 100644 --- a/webkit/browser/fileapi/quota/quota_reservation_buffer.cc +++ b/webkit/browser/fileapi/quota/quota_reservation_buffer.cc @@ -26,7 +26,7 @@ QuotaReservationBuffer::QuotaReservationBuffer( type_(type), reserved_quota_(0) { DCHECK(sequence_checker_.CalledOnValidSequencedThread()); - reservation_manager_->IncreaseDirtyCount(origin, type); + reservation_manager_->IncrementDirtyCount(origin, type); } scoped_refptr<QuotaReservation> QuotaReservationBuffer::CreateReservation() { @@ -49,10 +49,7 @@ void QuotaReservationBuffer::CommitFileGrowth(int64 quota_consumption, DCHECK(sequence_checker_.CalledOnValidSequencedThread()); if (!reservation_manager_) return; - - reservation_manager_->CommitQuotaUsage( - origin_, type_, usage_delta, - base::Bind(&IgnoreResult)); + reservation_manager_->CommitQuotaUsage(origin_, type_, usage_delta); DCHECK_LE(quota_consumption, reserved_quota_); if (quota_consumption > 0) { @@ -84,20 +81,20 @@ QuotaReservationBuffer::~QuotaReservationBuffer() { if (reserved_quota_ && reservation_manager_) { reservation_manager_->ReserveQuota( origin_, type_, -reserved_quota_, - base::Bind(&QuotaReservationBuffer::DecreaseDirtyCount, + base::Bind(&QuotaReservationBuffer::DecrementDirtyCount, reservation_manager_, origin_, type_)); } reservation_manager_->ReleaseReservationBuffer(this); } // static -bool QuotaReservationBuffer::DecreaseDirtyCount( +bool QuotaReservationBuffer::DecrementDirtyCount( base::WeakPtr<QuotaReservationManager> reservation_manager, const GURL& origin, FileSystemType type, base::PlatformFileError error) { if (error == base::PLATFORM_FILE_OK && reservation_manager) { - reservation_manager->DecreaseDirtyCount(origin, type); + reservation_manager->DecrementDirtyCount(origin, type); return true; } return false; diff --git a/webkit/browser/fileapi/quota/quota_reservation_buffer.h b/webkit/browser/fileapi/quota/quota_reservation_buffer.h index 1b27529..9a11fa5 100644 --- a/webkit/browser/fileapi/quota/quota_reservation_buffer.h +++ b/webkit/browser/fileapi/quota/quota_reservation_buffer.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef WEBKIT_BROWSER_FILEAPI_QUOTA_RESERVATION_BUFFER_H_ -#define WEBKIT_BROWSER_FILEAPI_QUOTA_RESERVATION_BUFFER_H_ +#ifndef WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_BUFFER_H_ +#define WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_BUFFER_H_ #include <map> @@ -56,7 +56,7 @@ class QuotaReservationBuffer : public base::RefCounted<QuotaReservationBuffer> { friend class base::RefCounted<QuotaReservationBuffer>; virtual ~QuotaReservationBuffer(); - static bool DecreaseDirtyCount( + static bool DecrementDirtyCount( base::WeakPtr<QuotaReservationManager> reservation_manager, const GURL& origin, FileSystemType type, @@ -83,4 +83,4 @@ class QuotaReservationBuffer : public base::RefCounted<QuotaReservationBuffer> { } // namespace fileapi -#endif // WEBKIT_BROWSER_FILEAPI_QUOTA_RESERVATION_BUFFER_H_ +#endif // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_BUFFER_H_ diff --git a/webkit/browser/fileapi/quota/quota_reservation_manager.cc b/webkit/browser/fileapi/quota/quota_reservation_manager.cc index 0a02940..7968b1f 100644 --- a/webkit/browser/fileapi/quota/quota_reservation_manager.cc +++ b/webkit/browser/fileapi/quota/quota_reservation_manager.cc @@ -9,8 +9,9 @@ namespace fileapi { -QuotaReservationManager::QuotaReservationManager(QuotaBackend* backend) - : backend_(backend), +QuotaReservationManager::QuotaReservationManager( + scoped_ptr<QuotaBackend> backend) + : backend_(backend.Pass()), weak_ptr_factory_(this) { DCHECK(sequence_checker_.CalledOnValidSequencedThread()); } @@ -37,19 +38,18 @@ void QuotaReservationManager::ReleaseReservedQuota( void QuotaReservationManager::CommitQuotaUsage( const GURL& origin, FileSystemType type, - int64 delta, - const StatusCallback& callback) { - backend_->CommitQuotaUsage(origin, type, delta, callback); + int64 delta) { + backend_->CommitQuotaUsage(origin, type, delta); } -void QuotaReservationManager::IncreaseDirtyCount(const GURL& origin, +void QuotaReservationManager::IncrementDirtyCount(const GURL& origin, FileSystemType type) { - backend_->IncreaseDirtyCount(origin, type); + backend_->IncrementDirtyCount(origin, type); } -void QuotaReservationManager::DecreaseDirtyCount(const GURL& origin, +void QuotaReservationManager::DecrementDirtyCount(const GURL& origin, FileSystemType type) { - backend_->DecreaseDirtyCount(origin, type); + backend_->DecrementDirtyCount(origin, type); } scoped_refptr<QuotaReservationBuffer> diff --git a/webkit/browser/fileapi/quota/quota_reservation_manager.h b/webkit/browser/fileapi/quota/quota_reservation_manager.h index 2e3ae93..07beacc 100644 --- a/webkit/browser/fileapi/quota/quota_reservation_manager.h +++ b/webkit/browser/fileapi/quota/quota_reservation_manager.h @@ -6,6 +6,7 @@ #define WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_ #include <map> +#include <utility> #include "base/basictypes.h" #include "base/callback_forward.h" @@ -25,13 +26,17 @@ class OpenFileHandleContext; class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { public: - typedef base::Callback<void(base::PlatformFileError error)> StatusCallback; + // Callback for ReserveQuota. When this callback returns false, ReserveQuota + // operation should be reverted. typedef base::Callback<bool(base::PlatformFileError error)> ReserveQuotaCallback; // An abstraction of backing quota system. - class QuotaBackend { + class WEBKIT_STORAGE_BROWSER_EXPORT QuotaBackend { public: + QuotaBackend() {} + virtual ~QuotaBackend() {} + // Reserves or reclaims |delta| of quota for |origin| and |type| pair. // Reserved quota should be counted as usage, but it should be on-memory // and be cleared by a browser restart. @@ -52,23 +57,18 @@ class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { // Invokes |callback| upon completion with an error code. virtual void CommitQuotaUsage(const GURL& origin, FileSystemType type, - int64 delta, - const StatusCallback& callback) = 0; + int64 delta) = 0; - virtual void IncreaseDirtyCount(const GURL& origin, + virtual void IncrementDirtyCount(const GURL& origin, FileSystemType type) = 0; - virtual void DecreaseDirtyCount(const GURL& origin, + virtual void DecrementDirtyCount(const GURL& origin, FileSystemType type) = 0; - protected: - QuotaBackend() {} - virtual ~QuotaBackend() {} - private: DISALLOW_COPY_AND_ASSIGN(QuotaBackend); }; - explicit QuotaReservationManager(QuotaBackend* backend); + explicit QuotaReservationManager(scoped_ptr<QuotaBackend> backend); ~QuotaReservationManager(); // The entry point of the quota reservation. Creates new reservation object @@ -83,6 +83,7 @@ class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { friend class QuotaReservation; friend class QuotaReservationBuffer; + friend class QuotaReservationManagerTest; void ReserveQuota(const GURL& origin, FileSystemType type, @@ -95,20 +96,17 @@ class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { void CommitQuotaUsage(const GURL& origin, FileSystemType type, - int64 delta, - const StatusCallback& callback); + int64 delta); - void IncreaseDirtyCount(const GURL& origin, FileSystemType type); - void DecreaseDirtyCount(const GURL& origin, FileSystemType type); + void IncrementDirtyCount(const GURL& origin, FileSystemType type); + void DecrementDirtyCount(const GURL& origin, FileSystemType type); scoped_refptr<QuotaReservationBuffer> GetReservationBuffer( const GURL& origin, FileSystemType type); void ReleaseReservationBuffer(QuotaReservationBuffer* reservation_pool); - - // Not owned. |backend_| should outlive QuotaReservationManager - QuotaBackend* backend_; + scoped_ptr<QuotaBackend> backend_; // Not owned. The destructor of ReservationBuffer should erase itself from // |reservation_buffers_| by calling ReleaseReservationBuffer. diff --git a/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc b/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc index 86590c7a..d9a521d 100644 --- a/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc +++ b/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc @@ -23,7 +23,6 @@ const FileSystemType kType = kFileSystemTypeTemporary; const int64 kInitialFileSize = 30; typedef QuotaReservationManager::ReserveQuotaCallback ReserveQuotaCallback; -typedef QuotaReservationManager::StatusCallback StatusCallback; class FakeBackend : public QuotaReservationManager::QuotaBackend { public: @@ -55,19 +54,16 @@ class FakeBackend : public QuotaReservationManager::QuotaBackend { virtual void CommitQuotaUsage(const GURL& origin, FileSystemType type, - int64 delta, - const StatusCallback& callback) OVERRIDE { + int64 delta) OVERRIDE { EXPECT_EQ(GURL(kOrigin), origin); EXPECT_EQ(kType, type); on_disk_usage_ += delta; - base::MessageLoopProxy::current()->PostTask( - FROM_HERE, base::Bind(callback, base::PLATFORM_FILE_OK)); } - virtual void IncreaseDirtyCount(const GURL& origin, - FileSystemType type) OVERRIDE {} - virtual void DecreaseDirtyCount(const GURL& origin, - FileSystemType type) OVERRIDE {} + virtual void IncrementDirtyCount(const GURL& origin, + FileSystemType type) OVERRIDE {} + virtual void DecrementDirtyCount(const GURL& origin, + FileSystemType type) OVERRIDE {} int64 on_memory_usage() { return on_memory_usage_; } int64 on_disk_usage() { return on_disk_usage_; } @@ -106,14 +102,12 @@ class QuotaReservationManagerTest : public testing::Test { file_path_ = work_dir_.path().Append(FILE_PATH_LITERAL("hoge")); SetFileSize(kInitialFileSize); - fake_backend_.reset(new FakeBackend); - reservation_manager_.reset(new QuotaReservationManager( - fake_backend_.get())); + scoped_ptr<QuotaReservationManager::QuotaBackend> backend(new FakeBackend); + reservation_manager_.reset(new QuotaReservationManager(backend.Pass())); } virtual void TearDown() OVERRIDE { reservation_manager_.reset(); - fake_backend_.reset(); } int64 GetFileSize() { @@ -140,7 +134,7 @@ class QuotaReservationManagerTest : public testing::Test { } FakeBackend* fake_backend() { - return fake_backend_.get(); + return static_cast<FakeBackend*>(reservation_manager_->backend_.get()); } QuotaReservationManager* reservation_manager() { @@ -155,7 +149,6 @@ class QuotaReservationManagerTest : public testing::Test { base::MessageLoop message_loop_; base::ScopedTempDir work_dir_; base::FilePath file_path_; - scoped_ptr<FakeBackend> fake_backend_; scoped_ptr<QuotaReservationManager> reservation_manager_; DISALLOW_COPY_AND_ASSIGN(QuotaReservationManagerTest); diff --git a/webkit/browser/fileapi/sandbox_file_system_backend_delegate.h b/webkit/browser/fileapi/sandbox_file_system_backend_delegate.h index c011a48..962797c 100644 --- a/webkit/browser/fileapi/sandbox_file_system_backend_delegate.h +++ b/webkit/browser/fileapi/sandbox_file_system_backend_delegate.h @@ -187,6 +187,7 @@ class WEBKIT_STORAGE_BROWSER_EXPORT SandboxFileSystemBackendDelegate private: friend class SandboxQuotaObserver; friend class SandboxFileSystemTestHelper; + friend class QuotaBackendImpl; FRIEND_TEST_ALL_PREFIXES(SandboxFileSystemBackendDelegateTest, IsAccessValid); // Performs API-specific validity checks on the given path |url|. |