1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "webkit/browser/fileapi/file_system_quota_client.h"
10 #include "base/bind.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/task_runner_util.h"
19 #include "net/base/net_util.h"
21 #include "webkit/browser/fileapi/file_system_context.h"
22 #include "webkit/browser/fileapi/file_system_quota_util.h"
23 #include "webkit/browser/fileapi/file_system_usage_cache.h"
24 #include "webkit/browser/fileapi/sandbox_file_system_backend.h"
25 #include "webkit/common/fileapi/file_system_util.h"
27 using quota::StorageType
;
33 void GetOriginsForTypeOnFileTaskRunner(
34 FileSystemContext
* context
,
35 StorageType storage_type
,
36 std::set
<GURL
>* origins_ptr
) {
37 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
38 DCHECK(type
!= kFileSystemTypeUnknown
);
40 FileSystemQuotaUtil
* quota_util
= context
->GetQuotaUtil(type
);
43 quota_util
->GetOriginsForTypeOnFileTaskRunner(type
, origins_ptr
);
46 void GetOriginsForHostOnFileTaskRunner(
47 FileSystemContext
* context
,
48 StorageType storage_type
,
49 const std::string
& host
,
50 std::set
<GURL
>* origins_ptr
) {
51 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
52 DCHECK(type
!= kFileSystemTypeUnknown
);
54 FileSystemQuotaUtil
* quota_util
= context
->GetQuotaUtil(type
);
57 quota_util
->GetOriginsForHostOnFileTaskRunner(type
, host
, origins_ptr
);
61 const quota::QuotaClient::GetOriginsCallback
& callback
,
62 std::set
<GURL
>* origins_ptr
) {
63 callback
.Run(*origins_ptr
);
66 quota::QuotaStatusCode
DeleteOriginOnFileTaskRunner(
67 FileSystemContext
* context
,
69 FileSystemType type
) {
70 FileSystemBackend
* provider
= context
->GetFileSystemBackend(type
);
71 if (!provider
|| !provider
->GetQuotaUtil())
72 return quota::kQuotaErrorNotSupported
;
73 base::File::Error result
=
74 provider
->GetQuotaUtil()->DeleteOriginDataOnFileTaskRunner(
75 context
, context
->quota_manager_proxy(), origin
, type
);
76 if (result
== base::File::FILE_OK
)
77 return quota::kQuotaStatusOk
;
78 return quota::kQuotaErrorInvalidModification
;
83 FileSystemQuotaClient::FileSystemQuotaClient(
84 FileSystemContext
* file_system_context
,
86 : file_system_context_(file_system_context
),
87 is_incognito_(is_incognito
) {
90 FileSystemQuotaClient::~FileSystemQuotaClient() {}
92 quota::QuotaClient::ID
FileSystemQuotaClient::id() const {
93 return quota::QuotaClient::kFileSystem
;
96 void FileSystemQuotaClient::OnQuotaManagerDestroyed() {
100 void FileSystemQuotaClient::GetOriginUsage(
101 const GURL
& origin_url
,
102 StorageType storage_type
,
103 const GetUsageCallback
& callback
) {
104 DCHECK(!callback
.is_null());
107 // We don't support FileSystem in incognito mode yet.
112 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
113 DCHECK(type
!= kFileSystemTypeUnknown
);
115 FileSystemQuotaUtil
* quota_util
= file_system_context_
->GetQuotaUtil(type
);
121 base::PostTaskAndReplyWithResult(
124 // It is safe to pass Unretained(quota_util) since context owns it.
125 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileTaskRunner
,
126 base::Unretained(quota_util
),
127 file_system_context_
,
133 void FileSystemQuotaClient::GetOriginsForType(
134 StorageType storage_type
,
135 const GetOriginsCallback
& callback
) {
136 DCHECK(!callback
.is_null());
139 // We don't support FileSystem in incognito mode yet.
140 std::set
<GURL
> origins
;
141 callback
.Run(origins
);
145 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
146 file_task_runner()->PostTaskAndReply(
148 base::Bind(&GetOriginsForTypeOnFileTaskRunner
,
149 file_system_context_
,
151 base::Unretained(origins_ptr
)),
152 base::Bind(&DidGetOrigins
,
154 base::Owned(origins_ptr
)));
157 void FileSystemQuotaClient::GetOriginsForHost(
158 StorageType storage_type
,
159 const std::string
& host
,
160 const GetOriginsCallback
& callback
) {
161 DCHECK(!callback
.is_null());
164 // We don't support FileSystem in incognito mode yet.
165 std::set
<GURL
> origins
;
166 callback
.Run(origins
);
170 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
171 file_task_runner()->PostTaskAndReply(
173 base::Bind(&GetOriginsForHostOnFileTaskRunner
,
174 file_system_context_
,
177 base::Unretained(origins_ptr
)),
178 base::Bind(&DidGetOrigins
,
180 base::Owned(origins_ptr
)));
183 void FileSystemQuotaClient::DeleteOriginData(
186 const DeletionCallback
& callback
) {
187 FileSystemType fs_type
= QuotaStorageTypeToFileSystemType(type
);
188 DCHECK(fs_type
!= kFileSystemTypeUnknown
);
190 base::PostTaskAndReplyWithResult(
193 base::Bind(&DeleteOriginOnFileTaskRunner
,
194 file_system_context_
,
200 bool FileSystemQuotaClient::DoesSupport(quota::StorageType storage_type
) const {
201 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
202 DCHECK(type
!= kFileSystemTypeUnknown
);
203 return file_system_context_
->IsSandboxFileSystem(type
);
206 base::SequencedTaskRunner
* FileSystemQuotaClient::file_task_runner() const {
207 return file_system_context_
->default_file_task_runner();
210 } // namespace fileapi