Commit 23db99e0 authored by nhiroki@chromium.org's avatar nhiroki@chromium.org

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
parent 7446d88f
...@@ -7,11 +7,15 @@ ...@@ -7,11 +7,15 @@
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include "base/bind.h"
#include "base/file_path.h" #include "base/file_path.h"
#include "base/file_util.h" #include "base/file_util.h"
#include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/task_runner_util.h"
#include "googleurl/src/gurl.h" #include "googleurl/src/gurl.h"
#include "net/base/net_util.h" #include "net/base/net_util.h"
#include "webkit/fileapi/file_system_context.h" #include "webkit/fileapi/file_system_context.h"
...@@ -21,162 +25,57 @@ ...@@ -21,162 +25,57 @@
#include "webkit/fileapi/file_system_util.h" #include "webkit/fileapi/file_system_util.h"
#include "webkit/fileapi/sandbox_mount_point_provider.h" #include "webkit/fileapi/sandbox_mount_point_provider.h"
using base::SequencedTaskRunner;
using quota::QuotaThreadTask;
using quota::StorageType; using quota::StorageType;
namespace fileapi { namespace fileapi {
class FileSystemQuotaClient::GetOriginUsageTask : public QuotaThreadTask { namespace {
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_);
}
FileSystemQuotaClient* quota_client_; void GetOriginsForTypeOnFileThread(FileSystemContext* context,
scoped_refptr<FileSystemContext> file_system_context_; StorageType storage_type,
GURL origin_url_; std::set<GURL>* origins_ptr) {
FileSystemType type_; FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
int64 fs_usage_; DCHECK(type != kFileSystemTypeUnknown);
};
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() {}
// QuotaThreadTask: FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type);
virtual void RunOnTargetThread() OVERRIDE { if (!quota_util)
FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); return;
if (quota_util) quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr);
quota_util->GetOriginsForHostOnFileThread(type_, host_, &origins_); }
}
virtual void Completed() OVERRIDE { void GetOriginsForHostOnFileThread(FileSystemContext* context,
quota_client_->DidGetOriginsForHost(std::make_pair(type_, host_), origins_); StorageType storage_type,
} const std::string& host,
std::set<GURL>* origins_ptr) {
FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
DCHECK(type != kFileSystemTypeUnknown);
private: FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type);
FileSystemQuotaClient* quota_client_; if (!quota_util)
scoped_refptr<FileSystemContext> file_system_context_; return;
std::set<GURL> origins_; quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr);
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) {
}
protected: void DidGetOrigins(
virtual ~DeleteOriginTask() {} const base::Callback<void(const std::set<GURL>&, StorageType)>& callback,
std::set<GURL>* origins_ptr,
// QuotaThreadTask: StorageType storage_type) {
virtual void RunOnTargetThread() OVERRIDE { callback.Run(*origins_ptr, storage_type);
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;
}
virtual void Completed() OVERRIDE { quota::QuotaStatusCode DeleteOriginOnTargetThread(
callback_.Run(status_); 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: } // namespace
FileSystemContext* file_system_context_;
GURL origin_;
FileSystemType type_;
quota::QuotaStatusCode status_;
DeletionCallback callback_;
};
FileSystemQuotaClient::FileSystemQuotaClient( FileSystemQuotaClient::FileSystemQuotaClient(
FileSystemContext* file_system_context, FileSystemContext* file_system_context,
...@@ -210,12 +109,22 @@ void FileSystemQuotaClient::GetOriginUsage( ...@@ -210,12 +109,22 @@ void FileSystemQuotaClient::GetOriginUsage(
FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
DCHECK(type != kFileSystemTypeUnknown); DCHECK(type != kFileSystemTypeUnknown);
if (pending_usage_callbacks_.Add( FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type);
std::make_pair(type, origin_url.spec()), callback)) { if (!quota_util) {
scoped_refptr<GetOriginUsageTask> task( callback.Run(0);
new GetOriginUsageTask(this, origin_url, type)); return;
task->Start();
} }
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( void FileSystemQuotaClient::GetOriginsForType(
...@@ -223,21 +132,24 @@ void FileSystemQuotaClient::GetOriginsForType( ...@@ -223,21 +132,24 @@ void FileSystemQuotaClient::GetOriginsForType(
const GetOriginsCallback& callback) { const GetOriginsCallback& callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
std::set<GURL> origins;
if (is_incognito_) { if (is_incognito_) {
// We don't support FileSystem in incognito mode yet. // We don't support FileSystem in incognito mode yet.
std::set<GURL> origins;
callback.Run(origins, storage_type); callback.Run(origins, storage_type);
return; return;
} }
FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); std::set<GURL>* origins_ptr = new std::set<GURL>();
DCHECK(type != kFileSystemTypeUnknown); file_task_runner()->PostTaskAndReply(
FROM_HERE,
if (pending_origins_for_type_callbacks_.Add(type, callback)) { base::Bind(&GetOriginsForTypeOnFileThread,
scoped_refptr<GetOriginsForTypeTask> task( file_system_context_,
new GetOriginsForTypeTask(this, type)); storage_type,
task->Start(); base::Unretained(origins_ptr)),
} base::Bind(&DidGetOrigins,
callback,
base::Owned(origins_ptr),
storage_type));
} }
void FileSystemQuotaClient::GetOriginsForHost( void FileSystemQuotaClient::GetOriginsForHost(
...@@ -246,22 +158,25 @@ void FileSystemQuotaClient::GetOriginsForHost( ...@@ -246,22 +158,25 @@ void FileSystemQuotaClient::GetOriginsForHost(
const GetOriginsCallback& callback) { const GetOriginsCallback& callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
std::set<GURL> origins;
if (is_incognito_) { if (is_incognito_) {
// We don't support FileSystem in incognito mode yet. // We don't support FileSystem in incognito mode yet.
std::set<GURL> origins;
callback.Run(origins, storage_type); callback.Run(origins, storage_type);
return; return;
} }
FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); std::set<GURL>* origins_ptr = new std::set<GURL>();
DCHECK(type != kFileSystemTypeUnknown); file_task_runner()->PostTaskAndReply(
FROM_HERE,
if (pending_origins_for_host_callbacks_.Add( base::Bind(&GetOriginsForHostOnFileThread,
std::make_pair(type, host), callback)) { file_system_context_,
scoped_refptr<GetOriginsForHostTask> task( storage_type,
new GetOriginsForHostTask(this, type, host)); host,
task->Start(); base::Unretained(origins_ptr)),
} base::Bind(&DidGetOrigins,
callback,
base::Owned(origins_ptr),
storage_type));
} }
void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, void FileSystemQuotaClient::DeleteOriginData(const GURL& origin,
...@@ -269,31 +184,15 @@ void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, ...@@ -269,31 +184,15 @@ void FileSystemQuotaClient::DeleteOriginData(const GURL& origin,
const DeletionCallback& callback) { const DeletionCallback& callback) {
FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type);
DCHECK(fs_type != kFileSystemTypeUnknown); 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( base::PostTaskAndReplyWithResult(
const TypeAndHostOrOrigin& type_and_host, const std::set<GURL>& origins) { file_task_runner(),
DCHECK(pending_origins_for_host_callbacks_.HasCallbacks(type_and_host)); FROM_HERE,
pending_origins_for_host_callbacks_.Run(type_and_host, origins, base::Bind(&DeleteOriginOnTargetThread,
FileSystemTypeToQuotaStorageType(type_and_host.first)); file_system_context_,
origin,
fs_type),
callback);
} }
base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const {
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "webkit/fileapi/file_system_quota_util.h" #include "webkit/fileapi/file_system_quota_util.h"
#include "webkit/fileapi/file_system_types.h" #include "webkit/fileapi/file_system_types.h"
#include "webkit/quota/quota_client.h" #include "webkit/quota/quota_client.h"
#include "webkit/quota/quota_task.h"
namespace base { namespace base {
class SequencedTaskRunner; class SequencedTaskRunner;
...@@ -33,8 +32,7 @@ class FileSystemContext; ...@@ -33,8 +32,7 @@ class FileSystemContext;
// All of the public methods of this class are called by the quota manager // All of the public methods of this class are called by the quota manager
// (except for the constructor/destructor). // (except for the constructor/destructor).
class FILEAPI_EXPORT_PRIVATE FileSystemQuotaClient class FILEAPI_EXPORT_PRIVATE FileSystemQuotaClient
: public NON_EXPORTED_BASE(quota::QuotaClient), : public NON_EXPORTED_BASE(quota::QuotaClient) {
public quota::QuotaTaskObserver {
public: public:
FileSystemQuotaClient( FileSystemQuotaClient(
FileSystemContext* file_system_context, FileSystemContext* file_system_context,
...@@ -60,46 +58,12 @@ class FILEAPI_EXPORT_PRIVATE FileSystemQuotaClient ...@@ -60,46 +58,12 @@ class FILEAPI_EXPORT_PRIVATE FileSystemQuotaClient
const DeletionCallback& callback) OVERRIDE; const DeletionCallback& callback) OVERRIDE;
private: 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; base::SequencedTaskRunner* file_task_runner() const;
scoped_refptr<FileSystemContext> file_system_context_; scoped_refptr<FileSystemContext> file_system_context_;
bool is_incognito_; 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); DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaClient);
}; };
......
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