OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/browser/fileapi/quota/quota_backend_impl.h" | 5 #include "storage/browser/fileapi/quota/quota_backend_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "webkit/browser/fileapi/file_system_usage_cache.h" | 14 #include "storage/browser/fileapi/file_system_usage_cache.h" |
15 #include "webkit/browser/quota/quota_client.h" | 15 #include "storage/browser/quota/quota_client.h" |
16 #include "webkit/browser/quota/quota_manager_proxy.h" | 16 #include "storage/browser/quota/quota_manager_proxy.h" |
17 #include "webkit/common/fileapi/file_system_util.h" | 17 #include "storage/common/fileapi/file_system_util.h" |
18 | 18 |
19 namespace fileapi { | 19 namespace storage { |
20 | 20 |
21 QuotaBackendImpl::QuotaBackendImpl( | 21 QuotaBackendImpl::QuotaBackendImpl( |
22 base::SequencedTaskRunner* file_task_runner, | 22 base::SequencedTaskRunner* file_task_runner, |
23 ObfuscatedFileUtil* obfuscated_file_util, | 23 ObfuscatedFileUtil* obfuscated_file_util, |
24 FileSystemUsageCache* file_system_usage_cache, | 24 FileSystemUsageCache* file_system_usage_cache, |
25 quota::QuotaManagerProxy* quota_manager_proxy) | 25 quota::QuotaManagerProxy* quota_manager_proxy) |
26 : file_task_runner_(file_task_runner), | 26 : file_task_runner_(file_task_runner), |
27 obfuscated_file_util_(obfuscated_file_util), | 27 obfuscated_file_util_(obfuscated_file_util), |
28 file_system_usage_cache_(file_system_usage_cache), | 28 file_system_usage_cache_(file_system_usage_cache), |
29 quota_manager_proxy_(quota_manager_proxy), | 29 quota_manager_proxy_(quota_manager_proxy), |
30 weak_ptr_factory_(this) { | 30 weak_ptr_factory_(this) { |
31 } | 31 } |
32 | 32 |
33 QuotaBackendImpl::~QuotaBackendImpl() { | 33 QuotaBackendImpl::~QuotaBackendImpl() { |
34 } | 34 } |
35 | 35 |
36 void QuotaBackendImpl::ReserveQuota(const GURL& origin, | 36 void QuotaBackendImpl::ReserveQuota(const GURL& origin, |
37 FileSystemType type, | 37 FileSystemType type, |
38 int64 delta, | 38 int64 delta, |
39 const ReserveQuotaCallback& callback) { | 39 const ReserveQuotaCallback& callback) { |
40 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 40 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
41 DCHECK(origin.is_valid()); | 41 DCHECK(origin.is_valid()); |
42 if (!delta) { | 42 if (!delta) { |
43 callback.Run(base::File::FILE_OK, 0); | 43 callback.Run(base::File::FILE_OK, 0); |
44 return; | 44 return; |
45 } | 45 } |
46 DCHECK(quota_manager_proxy_); | 46 DCHECK(quota_manager_proxy_); |
47 quota_manager_proxy_->GetUsageAndQuota( | 47 quota_manager_proxy_->GetUsageAndQuota( |
48 file_task_runner_, origin, FileSystemTypeToQuotaStorageType(type), | 48 file_task_runner_, |
| 49 origin, |
| 50 FileSystemTypeToQuotaStorageType(type), |
49 base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota, | 51 base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota, |
50 weak_ptr_factory_.GetWeakPtr(), | 52 weak_ptr_factory_.GetWeakPtr(), |
51 QuotaReservationInfo(origin, type, delta), callback)); | 53 QuotaReservationInfo(origin, type, delta), |
| 54 callback)); |
52 } | 55 } |
53 | 56 |
54 void QuotaBackendImpl::ReleaseReservedQuota(const GURL& origin, | 57 void QuotaBackendImpl::ReleaseReservedQuota(const GURL& origin, |
55 FileSystemType type, | 58 FileSystemType type, |
56 int64 size) { | 59 int64 size) { |
57 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 60 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
58 DCHECK(origin.is_valid()); | 61 DCHECK(origin.is_valid()); |
59 DCHECK_LE(0, size); | 62 DCHECK_LE(0, size); |
60 if (!size) | 63 if (!size) |
61 return; | 64 return; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 base::FilePath path; | 98 base::FilePath path; |
96 if (GetUsageCachePath(origin, type, &path) != base::File::FILE_OK) | 99 if (GetUsageCachePath(origin, type, &path) != base::File::FILE_OK) |
97 return; | 100 return; |
98 DCHECK(file_system_usage_cache_); | 101 DCHECK(file_system_usage_cache_); |
99 file_system_usage_cache_->DecrementDirty(path); | 102 file_system_usage_cache_->DecrementDirty(path); |
100 } | 103 } |
101 | 104 |
102 void QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota( | 105 void QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota( |
103 const QuotaReservationInfo& info, | 106 const QuotaReservationInfo& info, |
104 const ReserveQuotaCallback& callback, | 107 const ReserveQuotaCallback& callback, |
105 quota::QuotaStatusCode status, int64 usage, int64 quota) { | 108 quota::QuotaStatusCode status, |
| 109 int64 usage, |
| 110 int64 quota) { |
106 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 111 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
107 DCHECK(info.origin.is_valid()); | 112 DCHECK(info.origin.is_valid()); |
108 DCHECK_LE(0, usage); | 113 DCHECK_LE(0, usage); |
109 DCHECK_LE(0, quota); | 114 DCHECK_LE(0, quota); |
110 if (status != quota::kQuotaStatusOk) { | 115 if (status != quota::kQuotaStatusOk) { |
111 callback.Run(base::File::FILE_ERROR_FAILED, 0); | 116 callback.Run(base::File::FILE_ERROR_FAILED, 0); |
112 return; | 117 return; |
113 } | 118 } |
114 | 119 |
115 QuotaReservationInfo normalized_info = info; | 120 QuotaReservationInfo normalized_info = info; |
116 if (info.delta > 0) { | 121 if (info.delta > 0) { |
117 int64 new_usage = | 122 int64 new_usage = |
118 base::saturated_cast<int64>(usage + static_cast<uint64>(info.delta)); | 123 base::saturated_cast<int64>(usage + static_cast<uint64>(info.delta)); |
119 if (quota < new_usage) | 124 if (quota < new_usage) |
120 new_usage = quota; | 125 new_usage = quota; |
121 normalized_info.delta = std::max(static_cast<int64>(0), new_usage - usage); | 126 normalized_info.delta = std::max(static_cast<int64>(0), new_usage - usage); |
122 } | 127 } |
123 | 128 |
124 ReserveQuotaInternal(normalized_info); | 129 ReserveQuotaInternal(normalized_info); |
125 if (callback.Run(base::File::FILE_OK, normalized_info.delta)) | 130 if (callback.Run(base::File::FILE_OK, normalized_info.delta)) |
126 return; | 131 return; |
127 // The requester could not accept the reserved quota. Revert it. | 132 // The requester could not accept the reserved quota. Revert it. |
128 ReserveQuotaInternal( | 133 ReserveQuotaInternal(QuotaReservationInfo( |
129 QuotaReservationInfo(normalized_info.origin, | 134 normalized_info.origin, normalized_info.type, -normalized_info.delta)); |
130 normalized_info.type, | |
131 -normalized_info.delta)); | |
132 } | 135 } |
133 | 136 |
134 void QuotaBackendImpl::ReserveQuotaInternal(const QuotaReservationInfo& info) { | 137 void QuotaBackendImpl::ReserveQuotaInternal(const QuotaReservationInfo& info) { |
135 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 138 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
136 DCHECK(info.origin.is_valid()); | 139 DCHECK(info.origin.is_valid()); |
137 DCHECK(quota_manager_proxy_); | 140 DCHECK(quota_manager_proxy_); |
138 quota_manager_proxy_->NotifyStorageModified( | 141 quota_manager_proxy_->NotifyStorageModified( |
139 quota::QuotaClient::kFileSystem, info.origin, | 142 quota::QuotaClient::kFileSystem, |
140 FileSystemTypeToQuotaStorageType(info.type), info.delta); | 143 info.origin, |
| 144 FileSystemTypeToQuotaStorageType(info.type), |
| 145 info.delta); |
141 } | 146 } |
142 | 147 |
143 base::File::Error QuotaBackendImpl::GetUsageCachePath( | 148 base::File::Error QuotaBackendImpl::GetUsageCachePath( |
144 const GURL& origin, | 149 const GURL& origin, |
145 FileSystemType type, | 150 FileSystemType type, |
146 base::FilePath* usage_file_path) { | 151 base::FilePath* usage_file_path) { |
147 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 152 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
148 DCHECK(origin.is_valid()); | 153 DCHECK(origin.is_valid()); |
149 DCHECK(usage_file_path); | 154 DCHECK(usage_file_path); |
150 base::File::Error error = base::File::FILE_OK; | 155 base::File::Error error = base::File::FILE_OK; |
151 *usage_file_path = | 156 *usage_file_path = |
152 SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType( | 157 SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType( |
153 obfuscated_file_util_, origin, type, &error); | 158 obfuscated_file_util_, origin, type, &error); |
154 return error; | 159 return error; |
155 } | 160 } |
156 | 161 |
157 QuotaBackendImpl::QuotaReservationInfo::QuotaReservationInfo( | 162 QuotaBackendImpl::QuotaReservationInfo::QuotaReservationInfo( |
158 const GURL& origin, FileSystemType type, int64 delta) | 163 const GURL& origin, |
| 164 FileSystemType type, |
| 165 int64 delta) |
159 : origin(origin), type(type), delta(delta) { | 166 : origin(origin), type(type), delta(delta) { |
160 } | 167 } |
161 | 168 |
162 QuotaBackendImpl::QuotaReservationInfo::~QuotaReservationInfo() { | 169 QuotaBackendImpl::QuotaReservationInfo::~QuotaReservationInfo() { |
163 } | 170 } |
164 | 171 |
165 } // namespace fileapi | 172 } // namespace storage |
OLD | NEW |