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/bind.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.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"
20 #include "storage/browser/fileapi/file_system_context.h"
21 #include "storage/browser/fileapi/file_system_quota_util.h"
22 #include "storage/browser/fileapi/file_system_usage_cache.h"
23 #include "storage/browser/fileapi/sandbox_file_system_backend.h"
24 #include "storage/common/fileapi/file_system_util.h"
27 using storage::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
);
60 void DidGetOrigins(const storage::QuotaClient::GetOriginsCallback
& callback
,
61 std::set
<GURL
>* origins_ptr
) {
62 callback
.Run(*origins_ptr
);
65 storage::QuotaStatusCode
DeleteOriginOnFileTaskRunner(
66 FileSystemContext
* context
,
68 FileSystemType type
) {
69 FileSystemBackend
* provider
= context
->GetFileSystemBackend(type
);
70 if (!provider
|| !provider
->GetQuotaUtil())
71 return storage::kQuotaErrorNotSupported
;
72 base::File::Error result
=
73 provider
->GetQuotaUtil()->DeleteOriginDataOnFileTaskRunner(
74 context
, context
->quota_manager_proxy(), origin
, type
);
75 if (result
== base::File::FILE_OK
)
76 return storage::kQuotaStatusOk
;
77 return storage::kQuotaErrorInvalidModification
;
82 FileSystemQuotaClient::FileSystemQuotaClient(
83 FileSystemContext
* file_system_context
,
85 : file_system_context_(file_system_context
),
86 is_incognito_(is_incognito
) {
89 FileSystemQuotaClient::~FileSystemQuotaClient() {}
91 storage::QuotaClient::ID
FileSystemQuotaClient::id() const {
92 return storage::QuotaClient::kFileSystem
;
95 void FileSystemQuotaClient::OnQuotaManagerDestroyed() {
99 void FileSystemQuotaClient::GetOriginUsage(
100 const GURL
& origin_url
,
101 StorageType storage_type
,
102 const GetUsageCallback
& callback
) {
103 DCHECK(!callback
.is_null());
106 // We don't support FileSystem in incognito mode yet.
111 FileSystemType type
= QuotaStorageTypeToFileSystemType(storage_type
);
112 DCHECK(type
!= kFileSystemTypeUnknown
);
114 FileSystemQuotaUtil
* quota_util
= file_system_context_
->GetQuotaUtil(type
);
120 base::PostTaskAndReplyWithResult(
123 // It is safe to pass Unretained(quota_util) since context owns it.
124 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileTaskRunner
,
125 base::Unretained(quota_util
),
126 file_system_context_
,
132 void FileSystemQuotaClient::GetOriginsForType(
133 StorageType storage_type
,
134 const GetOriginsCallback
& callback
) {
135 DCHECK(!callback
.is_null());
138 // We don't support FileSystem in incognito mode yet.
139 std::set
<GURL
> origins
;
140 callback
.Run(origins
);
144 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
145 file_task_runner()->PostTaskAndReply(
147 base::Bind(&GetOriginsForTypeOnFileTaskRunner
,
148 file_system_context_
,
150 base::Unretained(origins_ptr
)),
151 base::Bind(&DidGetOrigins
,
153 base::Owned(origins_ptr
)));
156 void FileSystemQuotaClient::GetOriginsForHost(
157 StorageType storage_type
,
158 const std::string
& host
,
159 const GetOriginsCallback
& callback
) {
160 DCHECK(!callback
.is_null());
163 // We don't support FileSystem in incognito mode yet.
164 std::set
<GURL
> origins
;
165 callback
.Run(origins
);
169 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
170 file_task_runner()->PostTaskAndReply(
172 base::Bind(&GetOriginsForHostOnFileTaskRunner
,
173 file_system_context_
,
176 base::Unretained(origins_ptr
)),
177 base::Bind(&DidGetOrigins
,
179 base::Owned(origins_ptr
)));
182 void FileSystemQuotaClient::DeleteOriginData(
185 const DeletionCallback
& callback
) {
186 FileSystemType fs_type
= QuotaStorageTypeToFileSystemType(type
);
187 DCHECK(fs_type
!= kFileSystemTypeUnknown
);
189 base::PostTaskAndReplyWithResult(
192 base::Bind(&DeleteOriginOnFileTaskRunner
,
193 file_system_context_
,
199 bool FileSystemQuotaClient::DoesSupport(
200 storage::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 storage