diff options
author | nhiroki@chromium.org <nhiroki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-07 06:49:24 +0000 |
---|---|---|
committer | nhiroki@chromium.org <nhiroki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-07 06:49:24 +0000 |
commit | 23db99e0200384a9e35bc69fbda7dc920749bf24 (patch) | |
tree | e6b8a67b2942e0089da22ed63a375bd757d27777 /webkit | |
parent | 7446d88f7b420d554b558cd329a1edfecf998510 (diff) | |
download | chromium_src-23db99e0200384a9e35bc69fbda7dc920749bf24.zip chromium_src-23db99e0200384a9e35bc69fbda7dc920749bf24.tar.gz chromium_src-23db99e0200384a9e35bc69fbda7dc920749bf24.tar.bz2 |
Refactors FileSystemQuotaClient using base::Callback.
BUG=139270
Review URL: https://chromiumcodereview.appspot.com/10828177
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150290 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/fileapi/file_system_quota_client.cc | 285 | ||||
-rw-r--r-- | webkit/fileapi/file_system_quota_client.h | 38 |
2 files changed, 93 insertions, 230 deletions
diff --git a/webkit/fileapi/file_system_quota_client.cc b/webkit/fileapi/file_system_quota_client.cc index bd1638d..16c8f5d 100644 --- a/webkit/fileapi/file_system_quota_client.cc +++ b/webkit/fileapi/file_system_quota_client.cc @@ -7,11 +7,15 @@ #include <algorithm> #include <set> +#include "base/bind.h" #include "base/file_path.h" #include "base/file_util.h" +#include "base/location.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" +#include "base/sequenced_task_runner.h" #include "base/single_thread_task_runner.h" +#include "base/task_runner_util.h" #include "googleurl/src/gurl.h" #include "net/base/net_util.h" #include "webkit/fileapi/file_system_context.h" @@ -21,162 +25,57 @@ #include "webkit/fileapi/file_system_util.h" #include "webkit/fileapi/sandbox_mount_point_provider.h" -using base::SequencedTaskRunner; -using quota::QuotaThreadTask; using quota::StorageType; namespace fileapi { -class FileSystemQuotaClient::GetOriginUsageTask : public QuotaThreadTask { - public: - GetOriginUsageTask( - FileSystemQuotaClient* quota_client, - const GURL& origin_url, - FileSystemType type) - : QuotaThreadTask(quota_client, quota_client->file_task_runner()), - quota_client_(quota_client), - origin_url_(origin_url), - type_(type), - fs_usage_(0) { - DCHECK(quota_client_); - file_system_context_ = quota_client_->file_system_context_; - } - - protected: - virtual ~GetOriginUsageTask() {} - - // QuotaThreadTask: - virtual void RunOnTargetThread() OVERRIDE { - FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); - if (quota_util) - fs_usage_ = quota_util->GetOriginUsageOnFileThread( - file_system_context_, origin_url_, type_); - } - - virtual void Completed() OVERRIDE { - quota_client_->DidGetOriginUsage(type_, origin_url_, fs_usage_); - } +namespace { - FileSystemQuotaClient* quota_client_; - scoped_refptr<FileSystemContext> file_system_context_; - GURL origin_url_; - FileSystemType type_; - int64 fs_usage_; -}; - -class FileSystemQuotaClient::GetOriginsForTypeTask : public QuotaThreadTask { - public: - GetOriginsForTypeTask( - FileSystemQuotaClient* quota_client, - FileSystemType type) - : QuotaThreadTask(quota_client, quota_client->file_task_runner()), - quota_client_(quota_client), - type_(type) { - DCHECK(quota_client_); - file_system_context_ = quota_client_->file_system_context_; - } - - protected: - virtual ~GetOriginsForTypeTask() {} - - // QuotaThreadTask: - virtual void RunOnTargetThread() OVERRIDE { - FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); - if (quota_util) - quota_util->GetOriginsForTypeOnFileThread(type_, &origins_); - } - - virtual void Completed() OVERRIDE { - quota_client_->DidGetOriginsForType(type_, origins_); - } - - private: - FileSystemQuotaClient* quota_client_; - scoped_refptr<FileSystemContext> file_system_context_; - std::set<GURL> origins_; - FileSystemType type_; -}; - -class FileSystemQuotaClient::GetOriginsForHostTask : public QuotaThreadTask { - public: - GetOriginsForHostTask( - FileSystemQuotaClient* quota_client, - FileSystemType type, - const std::string& host) - : QuotaThreadTask(quota_client, quota_client->file_task_runner()), - quota_client_(quota_client), - type_(type), - host_(host) { - DCHECK(quota_client_); - file_system_context_ = quota_client_->file_system_context_; - } - - protected: - virtual ~GetOriginsForHostTask() {} +void GetOriginsForTypeOnFileThread(FileSystemContext* context, + StorageType storage_type, + std::set<GURL>* origins_ptr) { + FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); + DCHECK(type != kFileSystemTypeUnknown); - // QuotaThreadTask: - virtual void RunOnTargetThread() OVERRIDE { - FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); - if (quota_util) - quota_util->GetOriginsForHostOnFileThread(type_, host_, &origins_); - } + FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); + if (!quota_util) + return; + quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr); +} - virtual void Completed() OVERRIDE { - quota_client_->DidGetOriginsForHost(std::make_pair(type_, host_), origins_); - } +void GetOriginsForHostOnFileThread(FileSystemContext* context, + StorageType storage_type, + const std::string& host, + std::set<GURL>* origins_ptr) { + FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); + DCHECK(type != kFileSystemTypeUnknown); - private: - FileSystemQuotaClient* quota_client_; - scoped_refptr<FileSystemContext> file_system_context_; - std::set<GURL> origins_; - FileSystemType type_; - std::string host_; -}; - -class FileSystemQuotaClient::DeleteOriginTask - : public QuotaThreadTask { - public: - DeleteOriginTask( - FileSystemQuotaClient* quota_client, - const GURL& origin, - FileSystemType type, - const DeletionCallback& callback) - : QuotaThreadTask(quota_client, quota_client->file_task_runner()), - file_system_context_(quota_client->file_system_context_), - origin_(origin), - type_(type), - status_(quota::kQuotaStatusUnknown), - callback_(callback) { - } + FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); + if (!quota_util) + return; + quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr); +} - protected: - virtual ~DeleteOriginTask() {} - - // QuotaThreadTask: - virtual void RunOnTargetThread() OVERRIDE { - base::PlatformFileError result = - file_system_context_->sandbox_provider()->DeleteOriginDataOnFileThread( - file_system_context_, - file_system_context_->quota_manager_proxy(), - origin_, - type_); - if (result == base::PLATFORM_FILE_OK) - status_ = quota::kQuotaStatusOk; - else - status_ = quota::kQuotaErrorInvalidModification; - } +void DidGetOrigins( + const base::Callback<void(const std::set<GURL>&, StorageType)>& callback, + std::set<GURL>* origins_ptr, + StorageType storage_type) { + callback.Run(*origins_ptr, storage_type); +} - virtual void Completed() OVERRIDE { - callback_.Run(status_); - } +quota::QuotaStatusCode DeleteOriginOnTargetThread( + FileSystemContext* context, + const GURL& origin, + FileSystemType type) { + base::PlatformFileError result = + context->sandbox_provider()->DeleteOriginDataOnFileThread( + context, context->quota_manager_proxy(), origin, type); + if (result == base::PLATFORM_FILE_OK) + return quota::kQuotaStatusOk; + return quota::kQuotaErrorInvalidModification; +} - private: - FileSystemContext* file_system_context_; - GURL origin_; - FileSystemType type_; - quota::QuotaStatusCode status_; - DeletionCallback callback_; -}; +} // namespace FileSystemQuotaClient::FileSystemQuotaClient( FileSystemContext* file_system_context, @@ -210,12 +109,22 @@ void FileSystemQuotaClient::GetOriginUsage( FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); DCHECK(type != kFileSystemTypeUnknown); - if (pending_usage_callbacks_.Add( - std::make_pair(type, origin_url.spec()), callback)) { - scoped_refptr<GetOriginUsageTask> task( - new GetOriginUsageTask(this, origin_url, type)); - task->Start(); + FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type); + if (!quota_util) { + callback.Run(0); + return; } + + base::PostTaskAndReplyWithResult( + file_task_runner(), + FROM_HERE, + // It is safe to pass Unretained(quota_util) since context owns it. + base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileThread, + base::Unretained(quota_util), + file_system_context_, + origin_url, + type), + callback); } void FileSystemQuotaClient::GetOriginsForType( @@ -223,21 +132,24 @@ void FileSystemQuotaClient::GetOriginsForType( const GetOriginsCallback& callback) { DCHECK(!callback.is_null()); - std::set<GURL> origins; if (is_incognito_) { // We don't support FileSystem in incognito mode yet. + std::set<GURL> origins; callback.Run(origins, storage_type); return; } - FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); - DCHECK(type != kFileSystemTypeUnknown); - - if (pending_origins_for_type_callbacks_.Add(type, callback)) { - scoped_refptr<GetOriginsForTypeTask> task( - new GetOriginsForTypeTask(this, type)); - task->Start(); - } + std::set<GURL>* origins_ptr = new std::set<GURL>(); + file_task_runner()->PostTaskAndReply( + FROM_HERE, + base::Bind(&GetOriginsForTypeOnFileThread, + file_system_context_, + storage_type, + base::Unretained(origins_ptr)), + base::Bind(&DidGetOrigins, + callback, + base::Owned(origins_ptr), + storage_type)); } void FileSystemQuotaClient::GetOriginsForHost( @@ -246,22 +158,25 @@ void FileSystemQuotaClient::GetOriginsForHost( const GetOriginsCallback& callback) { DCHECK(!callback.is_null()); - std::set<GURL> origins; if (is_incognito_) { // We don't support FileSystem in incognito mode yet. + std::set<GURL> origins; callback.Run(origins, storage_type); return; } - FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); - DCHECK(type != kFileSystemTypeUnknown); - - if (pending_origins_for_host_callbacks_.Add( - std::make_pair(type, host), callback)) { - scoped_refptr<GetOriginsForHostTask> task( - new GetOriginsForHostTask(this, type, host)); - task->Start(); - } + std::set<GURL>* origins_ptr = new std::set<GURL>(); + file_task_runner()->PostTaskAndReply( + FROM_HERE, + base::Bind(&GetOriginsForHostOnFileThread, + file_system_context_, + storage_type, + host, + base::Unretained(origins_ptr)), + base::Bind(&DidGetOrigins, + callback, + base::Owned(origins_ptr), + storage_type)); } void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, @@ -269,31 +184,15 @@ void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, const DeletionCallback& callback) { FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); DCHECK(fs_type != kFileSystemTypeUnknown); - scoped_refptr<DeleteOriginTask> task( - new DeleteOriginTask(this, origin, fs_type, callback)); - task->Start(); -} - -void FileSystemQuotaClient::DidGetOriginUsage( - FileSystemType type, const GURL& origin_url, int64 usage) { - TypeAndHostOrOrigin type_and_origin(std::make_pair( - type, origin_url.spec())); - DCHECK(pending_usage_callbacks_.HasCallbacks(type_and_origin)); - pending_usage_callbacks_.Run(type_and_origin, usage); -} - -void FileSystemQuotaClient::DidGetOriginsForType( - FileSystemType type, const std::set<GURL>& origins) { - DCHECK(pending_origins_for_type_callbacks_.HasCallbacks(type)); - pending_origins_for_type_callbacks_.Run(type, origins, - FileSystemTypeToQuotaStorageType(type)); -} -void FileSystemQuotaClient::DidGetOriginsForHost( - const TypeAndHostOrOrigin& type_and_host, const std::set<GURL>& origins) { - DCHECK(pending_origins_for_host_callbacks_.HasCallbacks(type_and_host)); - pending_origins_for_host_callbacks_.Run(type_and_host, origins, - FileSystemTypeToQuotaStorageType(type_and_host.first)); + base::PostTaskAndReplyWithResult( + file_task_runner(), + FROM_HERE, + base::Bind(&DeleteOriginOnTargetThread, + file_system_context_, + origin, + fs_type), + callback); } base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { diff --git a/webkit/fileapi/file_system_quota_client.h b/webkit/fileapi/file_system_quota_client.h index adca8a6..688a720 100644 --- a/webkit/fileapi/file_system_quota_client.h +++ b/webkit/fileapi/file_system_quota_client.h @@ -17,7 +17,6 @@ #include "webkit/fileapi/file_system_quota_util.h" #include "webkit/fileapi/file_system_types.h" #include "webkit/quota/quota_client.h" -#include "webkit/quota/quota_task.h" namespace base { class SequencedTaskRunner; @@ -33,8 +32,7 @@ class FileSystemContext; // All of the public methods of this class are called by the quota manager // (except for the constructor/destructor). class FILEAPI_EXPORT_PRIVATE FileSystemQuotaClient - : public NON_EXPORTED_BASE(quota::QuotaClient), - public quota::QuotaTaskObserver { + : public NON_EXPORTED_BASE(quota::QuotaClient) { public: FileSystemQuotaClient( FileSystemContext* file_system_context, @@ -60,46 +58,12 @@ class FILEAPI_EXPORT_PRIVATE FileSystemQuotaClient const DeletionCallback& callback) OVERRIDE; private: - class GetOriginUsageTask; - class GetOriginsTaskBase; - class GetOriginsForTypeTask; - class GetOriginsForHostTask; - class DeleteOriginTask; - - typedef std::pair<fileapi::FileSystemType, std::string> TypeAndHostOrOrigin; - typedef quota::CallbackQueueMap1<GetUsageCallback, - TypeAndHostOrOrigin, - int64 - > UsageCallbackMap; - typedef quota::CallbackQueueMap2<GetOriginsCallback, - fileapi::FileSystemType, - const std::set<GURL>&, - quota::StorageType - > OriginsForTypeCallbackMap; - typedef quota::CallbackQueueMap2<GetOriginsCallback, - TypeAndHostOrOrigin, - const std::set<GURL>&, - quota::StorageType - > OriginsForHostCallbackMap; - - void DidGetOriginUsage(fileapi::FileSystemType type, - const GURL& origin, int64 usage); - void DidGetOriginsForType(fileapi::FileSystemType type, - const std::set<GURL>& origins); - void DidGetOriginsForHost(const TypeAndHostOrOrigin& type_and_host, - const std::set<GURL>& origins); - base::SequencedTaskRunner* file_task_runner() const; scoped_refptr<FileSystemContext> file_system_context_; bool is_incognito_; - // Pending callbacks. - UsageCallbackMap pending_usage_callbacks_; - OriginsForTypeCallbackMap pending_origins_for_type_callbacks_; - OriginsForHostCallbackMap pending_origins_for_host_callbacks_; - DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaClient); }; |