Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_quota_client.cc
blobb988a99e514d7fdb7c2b9fa2253de189521e596a
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"
7 #include <algorithm>
9 #include "base/bind.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"
22 #include "url/gurl.h"
24 using storage::StorageType;
26 namespace storage {
28 namespace {
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);
38 if (!quota_util)
39 return;
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);
52 if (!quota_util)
53 return;
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,
64 const GURL& origin,
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;
77 } // namespace
79 FileSystemQuotaClient::FileSystemQuotaClient(
80 FileSystemContext* file_system_context,
81 bool is_incognito)
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() {
93 delete this;
96 void FileSystemQuotaClient::GetOriginUsage(
97 const GURL& origin_url,
98 StorageType storage_type,
99 const GetUsageCallback& callback) {
100 DCHECK(!callback.is_null());
102 if (is_incognito_) {
103 // We don't support FileSystem in incognito mode yet.
104 callback.Run(0);
105 return;
108 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
109 DCHECK(type != kFileSystemTypeUnknown);
111 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type);
112 if (!quota_util) {
113 callback.Run(0);
114 return;
117 base::PostTaskAndReplyWithResult(
118 file_task_runner(),
119 FROM_HERE,
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_,
124 origin_url,
125 type),
126 callback);
129 void FileSystemQuotaClient::GetOriginsForType(
130 StorageType storage_type,
131 const GetOriginsCallback& callback) {
132 DCHECK(!callback.is_null());
134 if (is_incognito_) {
135 // We don't support FileSystem in incognito mode yet.
136 std::set<GURL> origins;
137 callback.Run(origins);
138 return;
141 std::set<GURL>* origins_ptr = new std::set<GURL>();
142 file_task_runner()->PostTaskAndReply(
143 FROM_HERE,
144 base::Bind(&GetOriginsForTypeOnFileTaskRunner,
145 file_system_context_,
146 storage_type,
147 base::Unretained(origins_ptr)),
148 base::Bind(&DidGetOrigins,
149 callback,
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());
159 if (is_incognito_) {
160 // We don't support FileSystem in incognito mode yet.
161 std::set<GURL> origins;
162 callback.Run(origins);
163 return;
166 std::set<GURL>* origins_ptr = new std::set<GURL>();
167 file_task_runner()->PostTaskAndReply(
168 FROM_HERE,
169 base::Bind(&GetOriginsForHostOnFileTaskRunner,
170 file_system_context_,
171 storage_type,
172 host,
173 base::Unretained(origins_ptr)),
174 base::Bind(&DidGetOrigins,
175 callback,
176 base::Owned(origins_ptr)));
179 void FileSystemQuotaClient::DeleteOriginData(
180 const GURL& origin,
181 StorageType type,
182 const DeletionCallback& callback) {
183 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type);
184 DCHECK(fs_type != kFileSystemTypeUnknown);
186 base::PostTaskAndReplyWithResult(
187 file_task_runner(),
188 FROM_HERE,
189 base::Bind(&DeleteOriginOnFileTaskRunner,
190 file_system_context_,
191 origin,
192 fs_type),
193 callback);
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