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 "storage/browser/fileapi/file_system_quota_client.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner_util.h"
17 #include "net/base/net_util.h"
18 #include "storage/browser/fileapi/file_system_context.h"
19 #include "storage/browser/fileapi/file_system_usage_cache.h"
20 #include "storage/browser/fileapi/sandbox_file_system_backend.h"
21 #include "storage/common/fileapi/file_system_util.h"
24 using storage::StorageType
;
30 void GetOriginsForTypeOnFileTaskRunner(
31 FileSystemContext
* context
,
32 StorageType storage_type
,
33 std::set
<GURL
>* origins_ptr
) {
34 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
35 DCHECK(type
!= kFileSystemTypeUnknown
);
37 FileSystemQuotaUtil
* quota_util
= context
->GetQuotaUtil(type
);
40 quota_util
->GetOriginsForTypeOnFileTaskRunner(type
, origins_ptr
);
43 void GetOriginsForHostOnFileTaskRunner(
44 FileSystemContext
* context
,
45 StorageType storage_type
,
46 const std::string
& host
,
47 std::set
<GURL
>* origins_ptr
) {
48 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
49 DCHECK(type
!= kFileSystemTypeUnknown
);
51 FileSystemQuotaUtil
* quota_util
= context
->GetQuotaUtil(type
);
54 quota_util
->GetOriginsForHostOnFileTaskRunner(type
, host
, origins_ptr
);
57 void DidGetOrigins(const storage::QuotaClient::GetOriginsCallback
& callback
,
58 std::set
<GURL
>* origins_ptr
) {
59 callback
.Run(*origins_ptr
);
62 storage::QuotaStatusCode
DeleteOriginOnFileTaskRunner(
63 FileSystemContext
* context
,
65 FileSystemType type
) {
66 FileSystemBackend
* provider
= context
->GetFileSystemBackend(type
);
67 if (!provider
|| !provider
->GetQuotaUtil())
68 return storage::kQuotaErrorNotSupported
;
69 base::File::Error result
=
70 provider
->GetQuotaUtil()->DeleteOriginDataOnFileTaskRunner(
71 context
, context
->quota_manager_proxy(), origin
, type
);
72 if (result
== base::File::FILE_OK
)
73 return storage::kQuotaStatusOk
;
74 return storage::kQuotaErrorInvalidModification
;
79 FileSystemQuotaClient::FileSystemQuotaClient(
80 FileSystemContext
* file_system_context
,
82 : file_system_context_(file_system_context
),
83 is_incognito_(is_incognito
) {
86 FileSystemQuotaClient::~FileSystemQuotaClient() {}
88 storage::QuotaClient::ID
FileSystemQuotaClient::id() const {
89 return storage::QuotaClient::kFileSystem
;
92 void FileSystemQuotaClient::OnQuotaManagerDestroyed() {
96 void FileSystemQuotaClient::GetOriginUsage(
97 const GURL
& origin_url
,
98 StorageType storage_type
,
99 const GetUsageCallback
& callback
) {
100 DCHECK(!callback
.is_null());
103 // We don't support FileSystem in incognito mode yet.
108 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
109 DCHECK(type
!= kFileSystemTypeUnknown
);
111 FileSystemQuotaUtil
* quota_util
= file_system_context_
->GetQuotaUtil(type
);
117 base::PostTaskAndReplyWithResult(
120 // It is safe to pass Unretained(quota_util) since context owns it.
121 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileTaskRunner
,
122 base::Unretained(quota_util
),
123 file_system_context_
,
129 void FileSystemQuotaClient::GetOriginsForType(
130 StorageType storage_type
,
131 const GetOriginsCallback
& callback
) {
132 DCHECK(!callback
.is_null());
135 // We don't support FileSystem in incognito mode yet.
136 std::set
<GURL
> origins
;
137 callback
.Run(origins
);
141 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
142 file_task_runner()->PostTaskAndReply(
144 base::Bind(&GetOriginsForTypeOnFileTaskRunner
,
145 file_system_context_
,
147 base::Unretained(origins_ptr
)),
148 base::Bind(&DidGetOrigins
,
150 base::Owned(origins_ptr
)));
153 void FileSystemQuotaClient::GetOriginsForHost(
154 StorageType storage_type
,
155 const std::string
& host
,
156 const GetOriginsCallback
& callback
) {
157 DCHECK(!callback
.is_null());
160 // We don't support FileSystem in incognito mode yet.
161 std::set
<GURL
> origins
;
162 callback
.Run(origins
);
166 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
167 file_task_runner()->PostTaskAndReply(
169 base::Bind(&GetOriginsForHostOnFileTaskRunner
,
170 file_system_context_
,
173 base::Unretained(origins_ptr
)),
174 base::Bind(&DidGetOrigins
,
176 base::Owned(origins_ptr
)));
179 void FileSystemQuotaClient::DeleteOriginData(
182 const DeletionCallback
& callback
) {
183 FileSystemType fs_type
= QuotaStorageTypeToFileSystemType(type
);
184 DCHECK(fs_type
!= kFileSystemTypeUnknown
);
186 base::PostTaskAndReplyWithResult(
189 base::Bind(&DeleteOriginOnFileTaskRunner
,
190 file_system_context_
,
196 bool FileSystemQuotaClient::DoesSupport(
197 storage::StorageType storage_type
) const {
198 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
199 DCHECK(type
!= kFileSystemTypeUnknown
);
200 return file_system_context_
->IsSandboxFileSystem(type
);
203 base::SequencedTaskRunner
* FileSystemQuotaClient::file_task_runner() const {
204 return file_system_context_
->default_file_task_runner();
207 } // namespace storage