1 // Copyright 2013 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/quota/quota_backend_impl.h"
10 #include "base/callback.h"
11 #include "base/logging.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "base/sequenced_task_runner.h"
14 #include "storage/browser/fileapi/file_system_usage_cache.h"
15 #include "storage/browser/quota/quota_client.h"
16 #include "storage/browser/quota/quota_manager_proxy.h"
17 #include "storage/common/fileapi/file_system_util.h"
21 QuotaBackendImpl::QuotaBackendImpl(
22 base::SequencedTaskRunner
* file_task_runner
,
23 ObfuscatedFileUtil
* obfuscated_file_util
,
24 FileSystemUsageCache
* file_system_usage_cache
,
25 storage::QuotaManagerProxy
* quota_manager_proxy
)
26 : file_task_runner_(file_task_runner
),
27 obfuscated_file_util_(obfuscated_file_util
),
28 file_system_usage_cache_(file_system_usage_cache
),
29 quota_manager_proxy_(quota_manager_proxy
),
30 weak_ptr_factory_(this) {
33 QuotaBackendImpl::~QuotaBackendImpl() {
36 void QuotaBackendImpl::ReserveQuota(const GURL
& origin
,
39 const ReserveQuotaCallback
& callback
) {
40 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
41 DCHECK(origin
.is_valid());
43 callback
.Run(base::File::FILE_OK
, 0);
46 DCHECK(quota_manager_proxy_
.get());
47 quota_manager_proxy_
->GetUsageAndQuota(
48 file_task_runner_
.get(),
50 FileSystemTypeToQuotaStorageType(type
),
51 base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota
,
52 weak_ptr_factory_
.GetWeakPtr(),
53 QuotaReservationInfo(origin
, type
, delta
),
57 void QuotaBackendImpl::ReleaseReservedQuota(const GURL
& origin
,
60 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
61 DCHECK(origin
.is_valid());
65 ReserveQuotaInternal(QuotaReservationInfo(origin
, type
, -size
));
68 void QuotaBackendImpl::CommitQuotaUsage(const GURL
& origin
,
71 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
72 DCHECK(origin
.is_valid());
75 ReserveQuotaInternal(QuotaReservationInfo(origin
, type
, delta
));
77 if (GetUsageCachePath(origin
, type
, &path
) != base::File::FILE_OK
)
79 bool result
= file_system_usage_cache_
->AtomicUpdateUsageByDelta(path
, delta
);
83 void QuotaBackendImpl::IncrementDirtyCount(const GURL
& origin
,
84 FileSystemType type
) {
85 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
86 DCHECK(origin
.is_valid());
88 if (GetUsageCachePath(origin
, type
, &path
) != base::File::FILE_OK
)
90 DCHECK(file_system_usage_cache_
);
91 file_system_usage_cache_
->IncrementDirty(path
);
94 void QuotaBackendImpl::DecrementDirtyCount(const GURL
& origin
,
95 FileSystemType type
) {
96 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
97 DCHECK(origin
.is_valid());
99 if (GetUsageCachePath(origin
, type
, &path
) != base::File::FILE_OK
)
101 DCHECK(file_system_usage_cache_
);
102 file_system_usage_cache_
->DecrementDirty(path
);
105 void QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota(
106 const QuotaReservationInfo
& info
,
107 const ReserveQuotaCallback
& callback
,
108 storage::QuotaStatusCode status
,
111 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
112 DCHECK(info
.origin
.is_valid());
115 if (status
!= storage::kQuotaStatusOk
) {
116 callback
.Run(base::File::FILE_ERROR_FAILED
, 0);
120 QuotaReservationInfo normalized_info
= info
;
121 if (info
.delta
> 0) {
123 base::saturated_cast
<int64
>(usage
+ static_cast<uint64
>(info
.delta
));
124 if (quota
< new_usage
)
126 normalized_info
.delta
= std::max(static_cast<int64
>(0), new_usage
- usage
);
129 ReserveQuotaInternal(normalized_info
);
130 if (callback
.Run(base::File::FILE_OK
, normalized_info
.delta
))
132 // The requester could not accept the reserved quota. Revert it.
133 ReserveQuotaInternal(
134 QuotaReservationInfo(normalized_info
.origin
,
135 normalized_info
.type
,
136 -normalized_info
.delta
));
139 void QuotaBackendImpl::ReserveQuotaInternal(const QuotaReservationInfo
& info
) {
140 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
141 DCHECK(info
.origin
.is_valid());
142 DCHECK(quota_manager_proxy_
.get());
143 quota_manager_proxy_
->NotifyStorageModified(
144 storage::QuotaClient::kFileSystem
,
146 FileSystemTypeToQuotaStorageType(info
.type
),
150 base::File::Error
QuotaBackendImpl::GetUsageCachePath(
153 base::FilePath
* usage_file_path
) {
154 DCHECK(file_task_runner_
->RunsTasksOnCurrentThread());
155 DCHECK(origin
.is_valid());
156 DCHECK(usage_file_path
);
157 base::File::Error error
= base::File::FILE_OK
;
159 SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType(
160 obfuscated_file_util_
, origin
, type
, &error
);
164 QuotaBackendImpl::QuotaReservationInfo::QuotaReservationInfo(
165 const GURL
& origin
, FileSystemType type
, int64 delta
)
166 : origin(origin
), type(type
), delta(delta
) {
169 QuotaBackendImpl::QuotaReservationInfo::~QuotaReservationInfo() {
172 } // namespace storage