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 "storage/browser/quota/quota_manager.h" | 5 #include "storage/browser/quota/quota_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <functional> | 11 #include <functional> |
12 #include <limits> | 12 #include <limits> |
13 #include <memory> | 13 #include <memory> |
14 #include <utility> | 14 #include <utility> |
15 | 15 |
16 #include "base/barrier_closure.h" | |
16 #include "base/bind.h" | 17 #include "base/bind.h" |
17 #include "base/bind_helpers.h" | 18 #include "base/bind_helpers.h" |
18 #include "base/command_line.h" | 19 #include "base/command_line.h" |
19 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
20 #include "base/macros.h" | 21 #include "base/macros.h" |
21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
22 #include "base/numerics/safe_conversions.h" | 23 #include "base/numerics/safe_conversions.h" |
23 #include "base/profiler/scoped_tracker.h" | 24 #include "base/profiler/scoped_tracker.h" |
24 #include "base/sequenced_task_runner.h" | 25 #include "base/sequenced_task_runner.h" |
25 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
26 #include "base/strings/string_number_conversions.h" | 27 #include "base/strings/string_number_conversions.h" |
27 #include "base/sys_info.h" | 28 #include "base/sys_info.h" |
28 #include "base/task_runner_util.h" | 29 #include "base/task_runner_util.h" |
30 #include "base/threading/thread_task_runner_handle.h" | |
29 #include "base/time/time.h" | 31 #include "base/time/time.h" |
30 #include "base/trace_event/trace_event.h" | 32 #include "base/trace_event/trace_event.h" |
31 #include "net/base/url_util.h" | 33 #include "net/base/url_util.h" |
32 #include "storage/browser/quota/client_usage_tracker.h" | 34 #include "storage/browser/quota/client_usage_tracker.h" |
33 #include "storage/browser/quota/quota_manager_proxy.h" | 35 #include "storage/browser/quota/quota_manager_proxy.h" |
34 #include "storage/browser/quota/quota_temporary_storage_evictor.h" | 36 #include "storage/browser/quota/quota_temporary_storage_evictor.h" |
35 #include "storage/browser/quota/storage_monitor.h" | 37 #include "storage/browser/quota/storage_monitor.h" |
36 #include "storage/browser/quota/usage_tracker.h" | 38 #include "storage/browser/quota/usage_tracker.h" |
37 #include "storage/common/quota/quota_types.h" | 39 #include "storage/common/quota/quota_types.h" |
38 | 40 |
39 #define UMA_HISTOGRAM_MBYTES(name, sample) \ | |
40 UMA_HISTOGRAM_CUSTOM_COUNTS( \ | |
41 (name), static_cast<int>((sample) / kMBytes), \ | |
42 1, 10 * 1024 * 1024 /* 10TB */, 100) | |
43 | |
44 namespace storage { | 41 namespace storage { |
45 | 42 |
46 namespace { | 43 namespace { |
47 | 44 |
48 const int64_t kMBytes = 1024 * 1024; | 45 const int64_t kMBytes = 1024 * 1024; |
49 const int kMinutesInMilliSeconds = 60 * 1000; | 46 const int kMinutesInMilliSeconds = 60 * 1000; |
47 const int64_t kReportHistogramInterval = 60 * 60 * 1000; // 1 hour | |
48 const double kShouldRemainAvailableRatio = 0.1; // 10 percent | |
50 | 49 |
51 const int64_t kReportHistogramInterval = 60 * 60 * 1000; // 1 hour | 50 #define UMA_HISTOGRAM_MBYTES(name, sample) \ |
52 const double kTemporaryQuotaRatioToAvail = 1.0 / 3.0; // 33% | 51 UMA_HISTOGRAM_CUSTOM_COUNTS((name), static_cast<int>((sample) / kMBytes), 1, \ |
52 10 * 1024 * 1024 /* 10TB */, 100) | |
53 | 53 |
54 } // namespace | 54 } // namespace |
55 | 55 |
56 // Arbitrary for now, but must be reasonably small so that | |
57 // in-memory databases can fit. | |
58 // TODO(kinuko): Refer SysInfo::AmountOfPhysicalMemory() to determine this. | |
59 const int64_t QuotaManager::kIncognitoDefaultQuotaLimit = 100 * kMBytes; | |
60 | |
61 const int64_t QuotaManager::kNoLimit = INT64_MAX; | 56 const int64_t QuotaManager::kNoLimit = INT64_MAX; |
62 | 57 |
63 const int QuotaManager::kPerHostTemporaryPortion = 5; // 20% | |
64 | |
65 // Cap size for per-host persistent quota determined by the histogram. | 58 // Cap size for per-host persistent quota determined by the histogram. |
66 // This is a bit lax value because the histogram says nothing about per-host | 59 // This is a bit lax value because the histogram says nothing about per-host |
67 // persistent storage usage and we determined by global persistent storage | 60 // persistent storage usage and we determined by global persistent storage |
68 // usage that is less than 10GB for almost all users. | 61 // usage that is less than 10GB for almost all users. |
69 const int64_t QuotaManager::kPerHostPersistentQuotaLimit = 10 * 1024 * kMBytes; | 62 const int64_t QuotaManager::kPerHostPersistentQuotaLimit = 10 * 1024 * kMBytes; |
70 | 63 |
71 const char QuotaManager::kDatabaseName[] = "QuotaManager"; | |
72 | |
73 const int QuotaManager::kThresholdOfErrorsToBeBlacklisted = 3; | |
74 | |
75 // Preserve kMinimumPreserveForSystem disk space for system book-keeping | |
76 // when returning the quota to unlimited apps/extensions. | |
77 // TODO(kinuko): This should be like 10% of the actual disk space. | |
78 // For now we simply use a constant as getting the disk size needs | |
79 // platform-dependent code. (http://crbug.com/178976) | |
80 int64_t QuotaManager::kMinimumPreserveForSystem = 1024 * kMBytes; | |
81 | |
82 const int QuotaManager::kEvictionIntervalInMilliSeconds = | |
83 30 * kMinutesInMilliSeconds; | |
84 | |
85 const char QuotaManager::kTimeBetweenRepeatedOriginEvictionsHistogram[] = | |
86 "Quota.TimeBetweenRepeatedOriginEvictions"; | |
87 const char QuotaManager::kEvictedOriginAccessedCountHistogram[] = | |
88 "Quota.EvictedOriginAccessCount"; | |
89 const char QuotaManager::kEvictedOriginTimeSinceAccessHistogram[] = | |
90 "Quota.EvictedOriginTimeSinceAccess"; | |
91 | |
92 // Heuristics: assuming average cloud server allows a few Gigs storage | 64 // Heuristics: assuming average cloud server allows a few Gigs storage |
93 // on the server side and the storage needs to be shared for user data | 65 // on the server side and the storage needs to be shared for user data |
94 // and by multiple apps. | 66 // and by multiple apps. |
95 int64_t QuotaManager::kSyncableStorageDefaultHostQuota = 500 * kMBytes; | 67 int64_t QuotaManager::kSyncableStorageDefaultHostQuota = 500 * kMBytes; |
96 | 68 |
69 const char QuotaManager::kDatabaseName[] = "QuotaManager"; | |
70 | |
71 const int QuotaManager::kThresholdOfErrorsToBeBlacklisted = 3; | |
72 const int QuotaManager::kEvictionIntervalInMilliSeconds = | |
73 30 * kMinutesInMilliSeconds; | |
74 | |
75 const char QuotaManager::kDaysBetweenRepeatedOriginEvictionsHistogram[] = | |
76 "Quota.DaysBetweenRepeatedOriginEvictions"; | |
77 const char QuotaManager::kEvictedOriginAccessedCountHistogram[] = | |
78 "Quota.EvictedOriginAccessCount"; | |
79 const char QuotaManager::kEvictedOriginDaysSinceAccessHistogram[] = | |
80 "Quota.EvictedOriginDaysSinceAccess"; | |
81 | |
97 namespace { | 82 namespace { |
98 | 83 |
84 bool IsSupportedType(StorageType type) { | |
85 return type == kStorageTypeTemporary || type == kStorageTypePersistent || | |
86 type == kStorageTypeSyncable; | |
87 } | |
88 | |
89 bool IsSupportedIncognitoType(StorageType type) { | |
90 return type == kStorageTypeTemporary || type == kStorageTypePersistent; | |
91 } | |
92 | |
99 void CountOriginType(const std::set<GURL>& origins, | 93 void CountOriginType(const std::set<GURL>& origins, |
100 SpecialStoragePolicy* policy, | 94 SpecialStoragePolicy* policy, |
101 size_t* protected_origins, | 95 size_t* protected_origins, |
102 size_t* unlimited_origins) { | 96 size_t* unlimited_origins) { |
103 DCHECK(protected_origins); | 97 DCHECK(protected_origins); |
104 DCHECK(unlimited_origins); | 98 DCHECK(unlimited_origins); |
105 *protected_origins = 0; | 99 *protected_origins = 0; |
106 *unlimited_origins = 0; | 100 *unlimited_origins = 0; |
107 if (!policy) | 101 if (!policy) |
108 return; | 102 return; |
109 for (std::set<GURL>::const_iterator itr = origins.begin(); | 103 for (std::set<GURL>::const_iterator itr = origins.begin(); |
110 itr != origins.end(); | 104 itr != origins.end(); |
111 ++itr) { | 105 ++itr) { |
112 if (policy->IsStorageProtected(*itr)) | 106 if (policy->IsStorageProtected(*itr)) |
113 ++*protected_origins; | 107 ++*protected_origins; |
114 if (policy->IsStorageUnlimited(*itr)) | 108 if (policy->IsStorageUnlimited(*itr)) |
115 ++*unlimited_origins; | 109 ++*unlimited_origins; |
116 } | 110 } |
117 } | 111 } |
118 | 112 |
119 bool SetTemporaryGlobalOverrideQuotaOnDBThread(int64_t* new_quota, | |
120 QuotaDatabase* database) { | |
121 DCHECK(database); | |
122 if (!database->SetQuotaConfigValue( | |
123 QuotaDatabase::kTemporaryQuotaOverrideKey, *new_quota)) { | |
124 *new_quota = -1; | |
125 return false; | |
126 } | |
127 return true; | |
128 } | |
129 | |
130 bool GetPersistentHostQuotaOnDBThread(const std::string& host, | 113 bool GetPersistentHostQuotaOnDBThread(const std::string& host, |
131 int64_t* quota, | 114 int64_t* quota, |
132 QuotaDatabase* database) { | 115 QuotaDatabase* database) { |
133 DCHECK(database); | 116 DCHECK(database); |
134 database->GetHostQuota(host, kStorageTypePersistent, quota); | 117 database->GetHostQuota(host, kStorageTypePersistent, quota); |
135 return true; | 118 return true; |
136 } | 119 } |
137 | 120 |
138 bool SetPersistentHostQuotaOnDBThread(const std::string& host, | 121 bool SetPersistentHostQuotaOnDBThread(const std::string& host, |
139 int64_t* new_quota, | 122 int64_t* new_quota, |
140 QuotaDatabase* database) { | 123 QuotaDatabase* database) { |
141 DCHECK(database); | 124 DCHECK(database); |
142 if (database->SetHostQuota(host, kStorageTypePersistent, *new_quota)) | 125 if (database->SetHostQuota(host, kStorageTypePersistent, *new_quota)) |
143 return true; | 126 return true; |
144 *new_quota = 0; | 127 *new_quota = 0; |
145 return false; | 128 return false; |
146 } | 129 } |
147 | 130 |
148 bool InitializeOnDBThread(int64_t* temporary_quota_override, | |
149 int64_t* desired_available_space, | |
150 QuotaDatabase* database) { | |
151 DCHECK(database); | |
152 database->GetQuotaConfigValue(QuotaDatabase::kTemporaryQuotaOverrideKey, | |
153 temporary_quota_override); | |
154 database->GetQuotaConfigValue(QuotaDatabase::kDesiredAvailableSpaceKey, | |
155 desired_available_space); | |
156 return true; | |
157 } | |
158 | |
159 bool GetLRUOriginOnDBThread(StorageType type, | 131 bool GetLRUOriginOnDBThread(StorageType type, |
160 const std::set<GURL>& exceptions, | 132 const std::set<GURL>& exceptions, |
161 SpecialStoragePolicy* policy, | 133 SpecialStoragePolicy* policy, |
162 GURL* url, | 134 GURL* url, |
163 QuotaDatabase* database) { | 135 QuotaDatabase* database) { |
164 DCHECK(database); | 136 DCHECK(database); |
165 database->GetLRUOrigin(type, exceptions, policy, url); | 137 database->GetLRUOrigin(type, exceptions, policy, url); |
166 return true; | 138 return true; |
167 } | 139 } |
168 | 140 |
169 bool DeleteOriginInfoOnDBThread(const GURL& origin, | 141 bool DeleteOriginInfoOnDBThread(const GURL& origin, |
170 StorageType type, | 142 StorageType type, |
171 bool is_eviction, | 143 bool is_eviction, |
172 QuotaDatabase* database) { | 144 QuotaDatabase* database) { |
173 DCHECK(database); | 145 DCHECK(database); |
174 | 146 |
175 base::Time now = base::Time::Now(); | 147 base::Time now = base::Time::Now(); |
176 | 148 |
177 if (is_eviction) { | 149 if (is_eviction) { |
178 QuotaDatabase::OriginInfoTableEntry entry; | 150 QuotaDatabase::OriginInfoTableEntry entry; |
179 database->GetOriginInfo(origin, type, &entry); | 151 database->GetOriginInfo(origin, type, &entry); |
180 UMA_HISTOGRAM_COUNTS(QuotaManager::kEvictedOriginAccessedCountHistogram, | 152 UMA_HISTOGRAM_COUNTS(QuotaManager::kEvictedOriginAccessedCountHistogram, |
181 entry.used_count); | 153 entry.used_count); |
182 UMA_HISTOGRAM_LONG_TIMES( | 154 UMA_HISTOGRAM_COUNTS_1000( |
183 QuotaManager::kEvictedOriginTimeSinceAccessHistogram, | 155 QuotaManager::kEvictedOriginDaysSinceAccessHistogram, |
184 now - entry.last_access_time); | 156 (now - entry.last_access_time).InDays()); |
157 | |
185 } | 158 } |
186 | 159 |
187 if (!database->DeleteOriginInfo(origin, type)) | 160 if (!database->DeleteOriginInfo(origin, type)) |
188 return false; | 161 return false; |
189 | 162 |
190 // If the deletion is not due to an eviction, delete the entry in the eviction | 163 // If the deletion is not due to an eviction, delete the entry in the eviction |
191 // table as well due to privacy concerns. | 164 // table as well due to privacy concerns. |
192 if (!is_eviction) | 165 if (!is_eviction) |
193 return database->DeleteOriginLastEvictionTime(origin, type); | 166 return database->DeleteOriginLastEvictionTime(origin, type); |
194 | 167 |
195 base::Time last_eviction_time; | 168 base::Time last_eviction_time; |
196 database->GetOriginLastEvictionTime(origin, type, &last_eviction_time); | 169 database->GetOriginLastEvictionTime(origin, type, &last_eviction_time); |
197 | 170 |
198 if (last_eviction_time != base::Time()) { | 171 if (last_eviction_time != base::Time()) { |
199 UMA_HISTOGRAM_LONG_TIMES( | 172 UMA_HISTOGRAM_COUNTS_1000( |
200 QuotaManager::kTimeBetweenRepeatedOriginEvictionsHistogram, | 173 QuotaManager::kDaysBetweenRepeatedOriginEvictionsHistogram, |
201 now - last_eviction_time); | 174 (now - last_eviction_time).InDays()); |
202 } | 175 } |
203 | 176 |
204 return database->SetOriginLastEvictionTime(origin, type, now); | 177 return database->SetOriginLastEvictionTime(origin, type, now); |
205 } | 178 } |
206 | 179 |
207 bool InitializeTemporaryOriginsInfoOnDBThread(const std::set<GURL>* origins, | 180 bool BootstrapDatabaseOnDBThread(const std::set<GURL>* origins, |
208 QuotaDatabase* database) { | 181 QuotaDatabase* database) { |
209 DCHECK(database); | 182 DCHECK(database); |
210 if (database->IsOriginDatabaseBootstrapped()) | 183 if (database->IsOriginDatabaseBootstrapped()) |
211 return true; | 184 return true; |
212 | 185 |
213 // Register existing origins with 0 last time access. | 186 // Register existing origins with 0 last time access. |
214 if (database->RegisterInitialOriginInfo(*origins, kStorageTypeTemporary)) { | 187 if (database->RegisterInitialOriginInfo(*origins, kStorageTypeTemporary)) { |
215 database->SetOriginDatabaseBootstrapped(true); | 188 database->SetOriginDatabaseBootstrapped(true); |
216 return true; | 189 return true; |
217 } | 190 } |
218 return false; | 191 return false; |
219 } | 192 } |
220 | 193 |
221 bool UpdateAccessTimeOnDBThread(const GURL& origin, | 194 bool UpdateAccessTimeOnDBThread(const GURL& origin, |
222 StorageType type, | 195 StorageType type, |
223 base::Time accessed_time, | 196 base::Time accessed_time, |
224 QuotaDatabase* database) { | 197 QuotaDatabase* database) { |
225 DCHECK(database); | 198 DCHECK(database); |
226 return database->SetOriginLastAccessTime(origin, type, accessed_time); | 199 return database->SetOriginLastAccessTime(origin, type, accessed_time); |
227 } | 200 } |
228 | 201 |
229 bool UpdateModifiedTimeOnDBThread(const GURL& origin, | 202 bool UpdateModifiedTimeOnDBThread(const GURL& origin, |
230 StorageType type, | 203 StorageType type, |
231 base::Time modified_time, | 204 base::Time modified_time, |
232 QuotaDatabase* database) { | 205 QuotaDatabase* database) { |
233 DCHECK(database); | 206 DCHECK(database); |
234 return database->SetOriginLastModifiedTime(origin, type, modified_time); | 207 return database->SetOriginLastModifiedTime(origin, type, modified_time); |
235 } | 208 } |
236 | 209 |
237 int64_t CalculateTemporaryGlobalQuota(int64_t global_limited_usage, | |
238 int64_t available_space) { | |
239 DCHECK_GE(global_limited_usage, 0); | |
240 int64_t avail_space = available_space; | |
241 if (avail_space < | |
242 std::numeric_limits<int64_t>::max() - global_limited_usage) { | |
243 // We basically calculate the temporary quota by | |
244 // [available_space + space_used_for_temp] * kTempQuotaRatio, | |
245 // but make sure we'll have no overflow. | |
246 avail_space += global_limited_usage; | |
247 } | |
248 int64_t pool_size = avail_space * kTemporaryQuotaRatioToAvail; | |
249 UMA_HISTOGRAM_MBYTES("Quota.GlobalTemporaryPoolSize", pool_size); | |
250 return pool_size; | |
251 } | |
252 | |
253 void DispatchTemporaryGlobalQuotaCallback( | |
254 const QuotaCallback& callback, | |
255 QuotaStatusCode status, | |
256 const UsageAndQuota& usage_and_quota) { | |
257 if (status != kQuotaStatusOk) { | |
258 callback.Run(status, 0); | |
259 return; | |
260 } | |
261 | |
262 callback.Run(status, CalculateTemporaryGlobalQuota( | |
263 usage_and_quota.global_limited_usage, | |
264 usage_and_quota.available_disk_space)); | |
265 } | |
266 | |
267 int64_t CalculateQuotaWithDiskSpace(int64_t available_disk_space, | |
268 int64_t usage, | |
269 int64_t quota) { | |
270 if (available_disk_space < QuotaManager::kMinimumPreserveForSystem) { | |
271 LOG(WARNING) | |
272 << "Running out of disk space for profile." | |
273 << " QuotaManager starts forbidding further quota consumption."; | |
274 return usage; | |
275 } | |
276 | |
277 if (quota < usage) { | |
278 // No more space; cap the quota to the current usage. | |
279 return usage; | |
280 } | |
281 | |
282 available_disk_space -= QuotaManager::kMinimumPreserveForSystem; | |
283 if (available_disk_space < quota - usage) | |
284 return available_disk_space + usage; | |
285 | |
286 return quota; | |
287 } | |
288 | |
289 int64_t CalculateTemporaryHostQuota(int64_t host_usage, | |
290 int64_t global_quota, | |
291 int64_t global_limited_usage) { | |
292 DCHECK_GE(global_limited_usage, 0); | |
293 int64_t host_quota = global_quota / QuotaManager::kPerHostTemporaryPortion; | |
294 if (global_limited_usage > global_quota) | |
295 host_quota = std::min(host_quota, host_usage); | |
296 return host_quota; | |
297 } | |
298 | |
299 void DispatchUsageAndQuotaForWebApps( | |
300 StorageType type, | |
301 bool is_incognito, | |
302 bool is_unlimited, | |
303 bool can_query_disk_size, | |
304 const QuotaManager::GetUsageAndQuotaCallback& callback, | |
305 QuotaStatusCode status, | |
306 const UsageAndQuota& usage_and_quota) { | |
307 if (status != kQuotaStatusOk) { | |
308 callback.Run(status, 0, 0); | |
309 return; | |
310 } | |
311 | |
312 int64_t usage = usage_and_quota.usage; | |
313 int64_t quota = usage_and_quota.quota; | |
314 | |
315 if (type == kStorageTypeTemporary && !is_unlimited) { | |
316 quota = CalculateTemporaryHostQuota( | |
317 usage, quota, usage_and_quota.global_limited_usage); | |
318 } | |
319 | |
320 if (is_incognito) { | |
321 quota = std::min(quota, QuotaManager::kIncognitoDefaultQuotaLimit); | |
322 callback.Run(status, usage, quota); | |
323 return; | |
324 } | |
325 | |
326 // For apps with unlimited permission or can_query_disk_size is true (and not | |
327 // in incognito mode). | |
328 // We assume we can expose the actual disk size for them and cap the quota by | |
329 // the available disk space. | |
330 if (is_unlimited || can_query_disk_size) { | |
331 quota = CalculateQuotaWithDiskSpace( | |
332 usage_and_quota.available_disk_space, | |
333 usage, quota); | |
334 } | |
335 | |
336 callback.Run(status, usage, quota); | |
337 | |
338 if (type == kStorageTypeTemporary && !is_unlimited) | |
339 UMA_HISTOGRAM_MBYTES("Quota.QuotaForOrigin", quota); | |
340 } | |
341 | |
342 } // namespace | 210 } // namespace |
343 | 211 |
344 UsageAndQuota::UsageAndQuota() | 212 class QuotaManager::UsageAndQuotaHelper : public QuotaTask { |
345 : usage(0), | |
346 global_limited_usage(0), | |
347 quota(0), | |
348 available_disk_space(0) { | |
349 } | |
350 | |
351 UsageAndQuota::UsageAndQuota(int64_t usage, | |
352 int64_t global_limited_usage, | |
353 int64_t quota, | |
354 int64_t available_disk_space) | |
355 : usage(usage), | |
356 global_limited_usage(global_limited_usage), | |
357 quota(quota), | |
358 available_disk_space(available_disk_space) {} | |
359 | |
360 class UsageAndQuotaCallbackDispatcher | |
361 : public QuotaTask, | |
362 public base::SupportsWeakPtr<UsageAndQuotaCallbackDispatcher> { | |
363 public: | 213 public: |
364 explicit UsageAndQuotaCallbackDispatcher(QuotaManager* manager) | 214 UsageAndQuotaHelper(QuotaManager* manager, |
215 const GURL& origin, | |
216 StorageType type, | |
217 bool is_unlimited, | |
218 bool is_incognito, | |
219 const UsageAndQuotaCallback& callback) | |
365 : QuotaTask(manager), | 220 : QuotaTask(manager), |
366 has_usage_(false), | 221 origin_(origin), |
367 has_global_limited_usage_(false), | 222 callback_(callback), |
368 has_quota_(false), | 223 type_(type), |
369 has_available_disk_space_(false), | 224 is_unlimited_(is_unlimited), |
370 status_(kQuotaStatusUnknown), | 225 is_incognito_(is_incognito), |
371 usage_and_quota_(-1, -1, -1, -1), | 226 weak_factory_(this) {} |
372 waiting_callbacks_(1) {} | 227 |
373 | 228 protected: |
374 ~UsageAndQuotaCallbackDispatcher() override {} | 229 void Run() override { |
375 | 230 // Start the async process of gathering the info we need. |
376 void WaitForResults(const QuotaManager::UsageAndQuotaCallback& callback) { | 231 // Gather 4 pieces of info before computing an answer: |
377 callback_ = callback; | 232 // settings, device_storage_capacity, host_usage, and host_quota. |
378 Start(); | 233 base::Closure barrier = base::BarrierClosure( |
379 } | 234 4, base::Bind(&UsageAndQuotaHelper::OnBarrierComplete, |
380 | 235 weak_factory_.GetWeakPtr())); |
381 void set_usage(int64_t usage) { | 236 |
382 usage_and_quota_.usage = usage; | 237 std::string host = net::GetHostOrSpecFromURL(origin_); |
383 has_usage_ = true; | 238 |
384 } | 239 manager()->GetQuotaSettings(base::Bind(&UsageAndQuotaHelper::OnGotSettings, |
385 | 240 weak_factory_.GetWeakPtr(), |
386 void set_global_limited_usage(int64_t global_limited_usage) { | 241 barrier)); |
387 usage_and_quota_.global_limited_usage = global_limited_usage; | 242 manager()->GetStorageCapacity( |
388 has_global_limited_usage_ = true; | 243 base::Bind(&UsageAndQuotaHelper::OnGotCapacity, |
389 } | 244 weak_factory_.GetWeakPtr(), barrier)); |
390 | 245 manager()->GetHostUsage(host, type_, |
391 void set_quota(int64_t quota) { | 246 base::Bind(&UsageAndQuotaHelper::OnGotHostUsage, |
392 usage_and_quota_.quota = quota; | 247 weak_factory_.GetWeakPtr(), barrier)); |
393 has_quota_ = true; | 248 |
394 } | 249 // Determine host_quota differently depending on type. |
395 | 250 if (is_unlimited_) { |
396 void set_available_disk_space(int64_t available_disk_space) { | 251 SetDesiredHostQuota(barrier, kQuotaStatusOk, kNoLimit); |
397 usage_and_quota_.available_disk_space = available_disk_space; | 252 } else if (type_ == kStorageTypeSyncable) { |
398 has_available_disk_space_ = true; | 253 SetDesiredHostQuota(barrier, kQuotaStatusOk, |
399 } | 254 kSyncableStorageDefaultHostQuota); |
400 | 255 } else if (type_ == kStorageTypePersistent) { |
401 UsageCallback GetHostUsageCallback() { | 256 manager()->GetPersistentHostQuota( |
402 ++waiting_callbacks_; | 257 host, base::Bind(&UsageAndQuotaHelper::SetDesiredHostQuota, |
403 has_usage_ = true; | 258 weak_factory_.GetWeakPtr(), barrier)); |
404 return base::Bind(&UsageAndQuotaCallbackDispatcher::DidGetHostUsage, | 259 } else { |
405 AsWeakPtr()); | 260 DCHECK_EQ(kStorageTypeTemporary, type_); |
406 } | 261 // For temporary storge, OnGotSettings will set the host quota. |
407 | 262 } |
408 UsageCallback GetGlobalLimitedUsageCallback() { | 263 } |
409 ++waiting_callbacks_; | 264 |
410 has_global_limited_usage_ = true; | 265 void Aborted() override { |
411 return base::Bind( | 266 weak_factory_.InvalidateWeakPtrs(); |
412 &UsageAndQuotaCallbackDispatcher::DidGetGlobalLimitedUsage, | 267 callback_.Run(kQuotaErrorAbort, 0, 0); |
413 AsWeakPtr()); | 268 DeleteSoon(); |
414 } | 269 } |
415 | 270 |
416 QuotaCallback GetQuotaCallback() { | 271 void Completed() override { |
417 ++waiting_callbacks_; | 272 weak_factory_.InvalidateWeakPtrs(); |
418 has_quota_ = true; | 273 |
419 return base::Bind(&UsageAndQuotaCallbackDispatcher::DidGetQuota, | 274 // Constrain the desired |host_quota| to something that fits. |
420 AsWeakPtr()); | 275 // If available space is too low, cap usage at current levels. |
421 } | 276 // If it's close to being too low, cap growth to avoid it getting too low. |
422 | 277 int64_t host_quota = |
423 QuotaCallback GetAvailableSpaceCallback() { | 278 std::min(desired_host_quota_, |
424 ++waiting_callbacks_; | 279 host_usage_ + |
425 has_available_disk_space_ = true; | 280 std::max(INT64_C(0), available_space_ - |
426 return base::Bind(&UsageAndQuotaCallbackDispatcher::DidGetAvailableSpace, | 281 settings_.must_remain_available)); |
427 AsWeakPtr()); | 282 callback_.Run(kQuotaStatusOk, host_usage_, host_quota); |
283 if (type_ == kStorageTypeTemporary && !is_incognito_ && !is_unlimited_) { | |
284 UMA_HISTOGRAM_MBYTES("Quota.QuotaForOrigin", host_quota); | |
285 UMA_HISTOGRAM_PERCENTAGE("Quota.PercentUsedByOrigin", | |
286 std::min(100, static_cast<int>((host_usage_ * 100) / host_quota))); | |
287 } | |
288 DeleteSoon(); | |
428 } | 289 } |
429 | 290 |
430 private: | 291 private: |
431 void DidGetHostUsage(int64_t usage) { | 292 QuotaManager* manager() const { |
432 if (status_ == kQuotaStatusUnknown) | 293 return static_cast<QuotaManager*>(observer()); |
433 status_ = kQuotaStatusOk; | 294 } |
434 usage_and_quota_.usage = usage; | 295 |
435 CheckCompleted(); | 296 void OnGotSettings(const base::Closure& barrier_closure, |
436 } | 297 const QuotaSettings& settings) { |
437 | 298 settings_ = settings; |
438 void DidGetGlobalLimitedUsage(int64_t limited_usage) { | 299 barrier_closure.Run(); |
439 if (status_ == kQuotaStatusUnknown) | 300 if (type_ == kStorageTypeTemporary && !is_unlimited_) { |
440 status_ = kQuotaStatusOk; | 301 SetDesiredHostQuota(barrier_closure, kQuotaStatusOk, |
441 usage_and_quota_.global_limited_usage = limited_usage; | 302 settings.per_host_quota); |
442 CheckCompleted(); | 303 } |
443 } | 304 } |
444 | 305 |
445 void DidGetQuota(QuotaStatusCode status, int64_t quota) { | 306 void OnGotCapacity(const base::Closure& barrier_closure, |
446 if (status_ == kQuotaStatusUnknown || status_ == kQuotaStatusOk) | 307 int64_t total_space, |
447 status_ = status; | 308 int64_t available_space) { |
448 usage_and_quota_.quota = quota; | 309 total_space_ = total_space; |
449 CheckCompleted(); | 310 available_space_ = available_space; |
450 } | 311 barrier_closure.Run(); |
451 | 312 } |
452 void DidGetAvailableSpace(QuotaStatusCode status, int64_t space) { | 313 |
453 // crbug.com/349708 | 314 void OnGotHostUsage(const base::Closure& barrier_closure, int64_t usage) { |
454 TRACE_EVENT0( | 315 host_usage_ = usage; |
455 "io", "UsageAndQuotaCallbackDispatcher::DidGetAvailableSpace"); | 316 barrier_closure.Run(); |
456 | 317 } |
457 DCHECK_GE(space, 0); | 318 |
458 if (status_ == kQuotaStatusUnknown || status_ == kQuotaStatusOk) | 319 void SetDesiredHostQuota(const base::Closure& barrier_closure, |
459 status_ = status; | 320 QuotaStatusCode status, |
460 usage_and_quota_.available_disk_space = space; | 321 int64_t quota) { |
461 CheckCompleted(); | 322 desired_host_quota_ = quota; |
462 } | 323 barrier_closure.Run(); |
463 | 324 } |
325 | |
326 void OnBarrierComplete() { CallCompleted(); } | |
327 | |
328 GURL origin_; | |
329 QuotaManager::UsageAndQuotaCallback callback_; | |
330 StorageType type_; | |
331 bool is_unlimited_; | |
332 bool is_incognito_; | |
333 int64_t available_space_ = 0; | |
334 int64_t total_space_ = 0; | |
335 int64_t desired_host_quota_ = 0; | |
336 int64_t host_usage_ = 0; | |
337 QuotaSettings settings_; | |
338 base::WeakPtrFactory<UsageAndQuotaHelper> weak_factory_; | |
339 DISALLOW_COPY_AND_ASSIGN(UsageAndQuotaHelper); | |
340 }; | |
341 | |
342 // Helper to asychronously gather information needed at the start of an | |
343 // eviction round. | |
344 class QuotaManager::EvictionRoundInfoHelper : public QuotaTask { | |
345 public: | |
346 EvictionRoundInfoHelper(QuotaManager* manager, | |
347 const EvictionRoundInfoCallback& callback) | |
348 : QuotaTask(manager), callback_(callback), weak_factory_(this) {} | |
349 | |
350 protected: | |
464 void Run() override { | 351 void Run() override { |
465 // We initialize waiting_callbacks to 1 so that we won't run | 352 // Gather 2 pieces of info before deciding if we need to get GlobalUsage: |
466 // the completion callback until here even some of the callbacks | 353 // settings and device_storage_capacity. |
467 // are dispatched synchronously. | 354 base::Closure barrier = base::BarrierClosure( |
468 CheckCompleted(); | 355 2, base::Bind(&EvictionRoundInfoHelper::OnBarrierComplete, |
356 weak_factory_.GetWeakPtr())); | |
357 | |
358 manager()->GetQuotaSettings( | |
359 base::Bind(&EvictionRoundInfoHelper::OnGotSettings, | |
360 weak_factory_.GetWeakPtr(), barrier)); | |
361 manager()->GetStorageCapacity( | |
362 base::Bind(&EvictionRoundInfoHelper::OnGotCapacity, | |
363 weak_factory_.GetWeakPtr(), barrier)); | |
469 } | 364 } |
470 | 365 |
471 void Aborted() override { | 366 void Aborted() override { |
472 callback_.Run(kQuotaErrorAbort, UsageAndQuota()); | 367 weak_factory_.InvalidateWeakPtrs(); |
368 callback_.Run(kQuotaErrorAbort, QuotaSettings(), 0, 0, 0, 0, false); | |
473 DeleteSoon(); | 369 DeleteSoon(); |
474 } | 370 } |
475 | 371 |
476 void Completed() override { | 372 void Completed() override { |
477 // crbug.com/349708 | 373 weak_factory_.InvalidateWeakPtrs(); |
478 TRACE_EVENT0("io", "UsageAndQuotaCallbackDispatcher::Completed"); | 374 callback_.Run(kQuotaStatusOk, settings_, |
479 | 375 available_space_, total_space_, should_remain_available_, |
480 DCHECK(!has_usage_ || usage_and_quota_.usage >= 0); | 376 global_usage_, global_usage_is_complete_); |
481 DCHECK(!has_global_limited_usage_ || | 377 DeleteSoon(); |
482 usage_and_quota_.global_limited_usage >= 0); | 378 } |
483 DCHECK(!has_quota_ || usage_and_quota_.quota >= 0); | 379 |
484 DCHECK(!has_available_disk_space_ || | 380 private: |
485 usage_and_quota_.available_disk_space >= 0); | 381 QuotaManager* manager() const { |
486 | 382 return static_cast<QuotaManager*>(observer()); |
487 callback_.Run(status_, usage_and_quota_); | 383 } |
488 DeleteSoon(); | 384 |
489 } | 385 void OnGotSettings(const base::Closure& barrier_closure, |
490 | 386 const QuotaSettings& settings) { |
491 void CheckCompleted() { | 387 settings_ = settings; |
492 if (--waiting_callbacks_ <= 0) | 388 barrier_closure.Run(); |
389 } | |
390 | |
391 void OnGotCapacity(const base::Closure& barrier_closure, | |
392 int64_t total_space, | |
393 int64_t available_space) { | |
394 total_space_ = total_space; | |
395 available_space_ = available_space; | |
396 barrier_closure.Run(); | |
397 } | |
398 | |
399 void OnBarrierComplete() { | |
400 should_remain_available_ = std::max( | |
401 settings_.must_remain_available, | |
402 static_cast<int64_t>(total_space_ * kShouldRemainAvailableRatio)); | |
403 | |
404 // Avoid computing the full current_usage when there's no pressure. | |
405 int64_t consumed_space = total_space_ - available_space_; | |
406 if (consumed_space < settings_.pool_size && | |
407 available_space_ > should_remain_available_) { | |
408 DCHECK(!global_usage_is_complete_); | |
409 global_usage_ = | |
410 manager()->GetUsageTracker(kStorageTypeTemporary)->GetCachedUsage(); | |
493 CallCompleted(); | 411 CallCompleted(); |
494 } | 412 return; |
495 | 413 } |
496 // For sanity checks, they're checked only when DCHECK is on. | 414 manager()->GetGlobalUsage( |
497 bool has_usage_; | 415 kStorageTypeTemporary, |
498 bool has_global_limited_usage_; | 416 base::Bind(&EvictionRoundInfoHelper::OnGotGlobalUsage, |
499 bool has_quota_; | 417 weak_factory_.GetWeakPtr())); |
500 bool has_available_disk_space_; | 418 } |
501 | 419 |
502 QuotaStatusCode status_; | 420 void OnGotGlobalUsage(int64_t usage, int64_t unlimited_usage) { |
503 UsageAndQuota usage_and_quota_; | 421 global_usage_ = std::max(INT64_C(0), usage - unlimited_usage); |
504 QuotaManager::UsageAndQuotaCallback callback_; | 422 global_usage_is_complete_ = true; |
505 int waiting_callbacks_; | 423 UMA_HISTOGRAM_PERCENTAGE("Quota.PercentUsedForTemporaryStorage", |
506 | 424 static_cast<int>((global_usage_ * 100) / total_space_)); |
507 DISALLOW_COPY_AND_ASSIGN(UsageAndQuotaCallbackDispatcher); | 425 CallCompleted(); |
426 } | |
427 | |
428 EvictionRoundInfoCallback callback_; | |
429 QuotaSettings settings_; | |
430 int64_t available_space_ = 0; | |
431 int64_t total_space_ = 0; | |
432 int64_t should_remain_available_ = 0; | |
433 int64_t global_usage_ = 0; | |
434 bool global_usage_is_complete_ = false; | |
435 base::WeakPtrFactory<EvictionRoundInfoHelper> weak_factory_; | |
436 DISALLOW_COPY_AND_ASSIGN(EvictionRoundInfoHelper); | |
508 }; | 437 }; |
509 | 438 |
510 class QuotaManager::GetUsageInfoTask : public QuotaTask { | 439 class QuotaManager::GetUsageInfoTask : public QuotaTask { |
511 public: | 440 public: |
512 GetUsageInfoTask( | 441 GetUsageInfoTask( |
513 QuotaManager* manager, | 442 QuotaManager* manager, |
514 const GetUsageInfoCallback& callback) | 443 const GetUsageInfoCallback& callback) |
515 : QuotaTask(manager), | 444 : QuotaTask(manager), |
516 callback_(callback), | 445 callback_(callback), |
517 weak_factory_(this) { | 446 weak_factory_(this) { |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 OriginInfoTableEntries entries_; | 788 OriginInfoTableEntries entries_; |
860 }; | 789 }; |
861 | 790 |
862 // QuotaManager --------------------------------------------------------------- | 791 // QuotaManager --------------------------------------------------------------- |
863 | 792 |
864 QuotaManager::QuotaManager( | 793 QuotaManager::QuotaManager( |
865 bool is_incognito, | 794 bool is_incognito, |
866 const base::FilePath& profile_path, | 795 const base::FilePath& profile_path, |
867 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, | 796 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, |
868 const scoped_refptr<base::SequencedTaskRunner>& db_thread, | 797 const scoped_refptr<base::SequencedTaskRunner>& db_thread, |
869 const scoped_refptr<SpecialStoragePolicy>& special_storage_policy) | 798 const scoped_refptr<SpecialStoragePolicy>& special_storage_policy, |
799 const GetQuotaSettingsFunc& get_settings_function) | |
870 : is_incognito_(is_incognito), | 800 : is_incognito_(is_incognito), |
871 profile_path_(profile_path), | 801 profile_path_(profile_path), |
872 proxy_(new QuotaManagerProxy(this, io_thread)), | 802 proxy_(new QuotaManagerProxy(this, io_thread)), |
873 db_disabled_(false), | 803 db_disabled_(false), |
874 eviction_disabled_(false), | 804 eviction_disabled_(false), |
875 io_thread_(io_thread), | 805 io_thread_(io_thread), |
876 db_thread_(db_thread), | 806 db_thread_(db_thread), |
807 get_settings_function_(get_settings_function), | |
877 is_getting_eviction_origin_(false), | 808 is_getting_eviction_origin_(false), |
878 temporary_quota_initialized_(false), | |
879 temporary_quota_override_(-1), | |
880 special_storage_policy_(special_storage_policy), | 809 special_storage_policy_(special_storage_policy), |
881 get_volume_info_fn_(&QuotaManager::GetVolumeInfo), | 810 get_volume_info_fn_(&QuotaManager::GetVolumeInfo), |
882 storage_monitor_(new StorageMonitor(this)), | 811 storage_monitor_(new StorageMonitor(this)), |
883 weak_factory_(this) {} | 812 weak_factory_(this) { |
813 DCHECK_EQ(settings_.refresh_interval, base::TimeDelta::Max()); | |
814 if (!get_settings_function.is_null()) { | |
815 // Reset the interval to ensure we use the get_settings_function | |
816 // the first times settings_ is needed. | |
817 settings_.refresh_interval = base::TimeDelta(); | |
818 get_settings_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
819 } | |
820 } | |
821 | |
822 void QuotaManager::SetQuotaSettings(const QuotaSettings& settings) { | |
823 settings_ = settings; | |
824 settings_timestamp_ = base::TimeTicks::Now(); | |
825 } | |
884 | 826 |
885 void QuotaManager::GetUsageInfo(const GetUsageInfoCallback& callback) { | 827 void QuotaManager::GetUsageInfo(const GetUsageInfoCallback& callback) { |
886 LazyInitialize(); | 828 LazyInitialize(); |
887 GetUsageInfoTask* get_usage_info = new GetUsageInfoTask(this, callback); | 829 GetUsageInfoTask* get_usage_info = new GetUsageInfoTask(this, callback); |
888 get_usage_info->Start(); | 830 get_usage_info->Start(); |
889 } | 831 } |
890 | 832 |
891 void QuotaManager::GetUsageAndQuotaForWebApps( | 833 void QuotaManager::GetUsageAndQuotaForWebApps( |
892 const GURL& origin, | 834 const GURL& origin, |
893 StorageType type, | 835 StorageType type, |
894 const GetUsageAndQuotaCallback& callback) { | 836 const UsageAndQuotaCallback& callback) { |
895 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. | 837 DCHECK(origin == origin.GetOrigin()); |
896 tracked_objects::ScopedTracker tracking_profile( | 838 if (!IsSupportedType(type) || |
897 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 839 (is_incognito_ && !IsSupportedIncognitoType(type))) { |
898 "477117 QuotaManager::GetUsageAndQuotaForWebApps")); | |
899 if (type != kStorageTypeTemporary && | |
900 type != kStorageTypePersistent && | |
901 type != kStorageTypeSyncable) { | |
902 callback.Run(kQuotaErrorNotSupported, 0, 0); | 840 callback.Run(kQuotaErrorNotSupported, 0, 0); |
903 return; | 841 return; |
904 } | 842 } |
905 | |
906 DCHECK(origin == origin.GetOrigin()); | |
907 LazyInitialize(); | 843 LazyInitialize(); |
908 | 844 UsageAndQuotaHelper* helper = new UsageAndQuotaHelper( |
909 bool unlimited = IsStorageUnlimited(origin, type); | 845 this, origin, type, IsStorageUnlimited(origin, type), is_incognito_, |
910 bool can_query_disk_size = CanQueryDiskSize(origin); | 846 callback); |
911 | 847 helper->Start(); |
912 UsageAndQuotaCallbackDispatcher* dispatcher = | |
913 new UsageAndQuotaCallbackDispatcher(this); | |
914 | |
915 if (unlimited) { | |
916 dispatcher->set_quota(kNoLimit); | |
917 } else { | |
918 if (type == kStorageTypeTemporary) { | |
919 GetUsageTracker(type)->GetGlobalLimitedUsage( | |
920 dispatcher->GetGlobalLimitedUsageCallback()); | |
921 GetTemporaryGlobalQuota(dispatcher->GetQuotaCallback()); | |
922 } else if (type == kStorageTypePersistent) { | |
923 GetPersistentHostQuota(net::GetHostOrSpecFromURL(origin), | |
924 dispatcher->GetQuotaCallback()); | |
925 } else { | |
926 dispatcher->set_quota(kSyncableStorageDefaultHostQuota); | |
927 } | |
928 } | |
929 | |
930 DCHECK(GetUsageTracker(type)); | |
931 GetUsageTracker(type)->GetHostUsage(net::GetHostOrSpecFromURL(origin), | |
932 dispatcher->GetHostUsageCallback()); | |
933 | |
934 if (!is_incognito_ && (unlimited || can_query_disk_size)) | |
935 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback()); | |
936 | |
937 dispatcher->WaitForResults(base::Bind( | |
938 &DispatchUsageAndQuotaForWebApps, | |
939 type, is_incognito_, unlimited, can_query_disk_size, | |
940 callback)); | |
941 } | 848 } |
942 | 849 |
943 void QuotaManager::GetUsageAndQuota( | 850 void QuotaManager::GetUsageAndQuota(const GURL& origin, |
944 const GURL& origin, StorageType type, | 851 StorageType type, |
945 const GetUsageAndQuotaCallback& callback) { | 852 const UsageAndQuotaCallback& callback) { |
946 DCHECK(origin == origin.GetOrigin()); | 853 DCHECK(origin == origin.GetOrigin()); |
947 | 854 |
948 if (IsStorageUnlimited(origin, type)) { | 855 if (IsStorageUnlimited(origin, type)) { |
856 // TODO(michaeln): This seems like a non-obvious odd behavior, probably for | |
857 // apps/extensions, but it would be good to elimiate this special case. | |
949 callback.Run(kQuotaStatusOk, 0, kNoLimit); | 858 callback.Run(kQuotaStatusOk, 0, kNoLimit); |
950 return; | 859 return; |
951 } | 860 } |
952 | 861 |
953 GetUsageAndQuotaForWebApps(origin, type, callback); | 862 GetUsageAndQuotaForWebApps(origin, type, callback); |
954 } | 863 } |
955 | 864 |
956 void QuotaManager::NotifyStorageAccessed( | 865 void QuotaManager::NotifyStorageAccessed( |
957 QuotaClient::ID client_id, | 866 QuotaClient::ID client_id, |
958 const GURL& origin, StorageType type) { | 867 const GURL& origin, StorageType type) { |
(...skipping 25 matching lines...) Expand all Loading... | |
984 | 893 |
985 void QuotaManager::SetUsageCacheEnabled(QuotaClient::ID client_id, | 894 void QuotaManager::SetUsageCacheEnabled(QuotaClient::ID client_id, |
986 const GURL& origin, | 895 const GURL& origin, |
987 StorageType type, | 896 StorageType type, |
988 bool enabled) { | 897 bool enabled) { |
989 LazyInitialize(); | 898 LazyInitialize(); |
990 DCHECK(GetUsageTracker(type)); | 899 DCHECK(GetUsageTracker(type)); |
991 GetUsageTracker(type)->SetUsageCacheEnabled(client_id, origin, enabled); | 900 GetUsageTracker(type)->SetUsageCacheEnabled(client_id, origin, enabled); |
992 } | 901 } |
993 | 902 |
994 void QuotaManager::SetTemporaryStorageEvictionPolicy( | |
995 std::unique_ptr<QuotaEvictionPolicy> policy) { | |
996 temporary_storage_eviction_policy_ = std::move(policy); | |
997 } | |
998 | |
999 void QuotaManager::DeleteOriginData(const GURL& origin, | 903 void QuotaManager::DeleteOriginData(const GURL& origin, |
1000 StorageType type, | 904 StorageType type, |
1001 int quota_client_mask, | 905 int quota_client_mask, |
1002 const StatusCallback& callback) { | 906 const StatusCallback& callback) { |
1003 DeleteOriginDataInternal(origin, type, quota_client_mask, false, callback); | 907 DeleteOriginDataInternal(origin, type, quota_client_mask, false, callback); |
1004 } | 908 } |
1005 | 909 |
1006 void QuotaManager::DeleteHostData(const std::string& host, | 910 void QuotaManager::DeleteHostData(const std::string& host, |
1007 StorageType type, | 911 StorageType type, |
1008 int quota_client_mask, | 912 int quota_client_mask, |
1009 const StatusCallback& callback) { | 913 const StatusCallback& callback) { |
1010 LazyInitialize(); | 914 LazyInitialize(); |
1011 | |
1012 if (host.empty() || clients_.empty()) { | 915 if (host.empty() || clients_.empty()) { |
1013 callback.Run(kQuotaStatusOk); | 916 callback.Run(kQuotaStatusOk); |
1014 return; | 917 return; |
1015 } | 918 } |
1016 | 919 |
1017 HostDataDeleter* deleter = | 920 HostDataDeleter* deleter = |
1018 new HostDataDeleter(this, host, type, quota_client_mask, callback); | 921 new HostDataDeleter(this, host, type, quota_client_mask, callback); |
1019 deleter->Start(); | 922 deleter->Start(); |
1020 } | 923 } |
1021 | 924 |
1022 void QuotaManager::GetAvailableSpace(const AvailableSpaceCallback& callback) { | |
1023 if (!available_space_callbacks_.Add(callback)) | |
1024 return; | |
1025 // crbug.com/349708 | |
1026 TRACE_EVENT0("io", "QuotaManager::GetAvailableSpace"); | |
1027 | |
1028 PostTaskAndReplyWithResult( | |
1029 db_thread_.get(), | |
1030 FROM_HERE, | |
1031 base::Bind(&QuotaManager::CallGetAmountOfFreeDiskSpace, | |
1032 get_volume_info_fn_, profile_path_), | |
1033 base::Bind(&QuotaManager::DidGetAvailableSpace, | |
1034 weak_factory_.GetWeakPtr())); | |
1035 } | |
1036 | |
1037 void QuotaManager::GetTemporaryGlobalQuota(const QuotaCallback& callback) { | |
1038 LazyInitialize(); | |
1039 if (!temporary_quota_initialized_) { | |
1040 db_initialization_callbacks_.Add(base::Bind( | |
1041 &QuotaManager::GetTemporaryGlobalQuota, | |
1042 weak_factory_.GetWeakPtr(), callback)); | |
1043 return; | |
1044 } | |
1045 | |
1046 if (temporary_quota_override_ > 0) { | |
1047 callback.Run(kQuotaStatusOk, temporary_quota_override_); | |
1048 return; | |
1049 } | |
1050 | |
1051 UsageAndQuotaCallbackDispatcher* dispatcher = | |
1052 new UsageAndQuotaCallbackDispatcher(this); | |
1053 GetUsageTracker(kStorageTypeTemporary)-> | |
1054 GetGlobalLimitedUsage(dispatcher->GetGlobalLimitedUsageCallback()); | |
1055 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback()); | |
1056 dispatcher->WaitForResults( | |
1057 base::Bind(&DispatchTemporaryGlobalQuotaCallback, callback)); | |
1058 } | |
1059 | |
1060 void QuotaManager::SetTemporaryGlobalOverrideQuota( | |
1061 int64_t new_quota, | |
1062 const QuotaCallback& callback) { | |
1063 LazyInitialize(); | |
1064 | |
1065 if (new_quota < 0) { | |
1066 if (!callback.is_null()) | |
1067 callback.Run(kQuotaErrorInvalidModification, -1); | |
1068 return; | |
1069 } | |
1070 | |
1071 if (db_disabled_) { | |
1072 if (!callback.is_null()) | |
1073 callback.Run(kQuotaErrorInvalidAccess, -1); | |
1074 return; | |
1075 } | |
1076 | |
1077 int64_t* new_quota_ptr = new int64_t(new_quota); | |
1078 PostTaskAndReplyWithResultForDBThread( | |
1079 FROM_HERE, | |
1080 base::Bind(&SetTemporaryGlobalOverrideQuotaOnDBThread, | |
1081 base::Unretained(new_quota_ptr)), | |
1082 base::Bind(&QuotaManager::DidSetTemporaryGlobalOverrideQuota, | |
1083 weak_factory_.GetWeakPtr(), | |
1084 callback, | |
1085 base::Owned(new_quota_ptr))); | |
1086 } | |
1087 | |
1088 void QuotaManager::GetPersistentHostQuota(const std::string& host, | 925 void QuotaManager::GetPersistentHostQuota(const std::string& host, |
1089 const QuotaCallback& callback) { | 926 const QuotaCallback& callback) { |
1090 LazyInitialize(); | 927 LazyInitialize(); |
1091 if (host.empty()) { | 928 if (host.empty()) { |
1092 // This could happen if we are called on file:///. | 929 // This could happen if we are called on file:///. |
1093 // TODO(kinuko) We may want to respect --allow-file-access-from-files | 930 // TODO(kinuko) We may want to respect --allow-file-access-from-files |
1094 // command line switch. | 931 // command line switch. |
1095 callback.Run(kQuotaStatusOk, 0); | 932 callback.Run(kQuotaStatusOk, 0); |
1096 return; | 933 return; |
1097 } | 934 } |
(...skipping 21 matching lines...) Expand all Loading... | |
1119 // This could happen if we are called on file:///. | 956 // This could happen if we are called on file:///. |
1120 callback.Run(kQuotaErrorNotSupported, 0); | 957 callback.Run(kQuotaErrorNotSupported, 0); |
1121 return; | 958 return; |
1122 } | 959 } |
1123 | 960 |
1124 if (new_quota < 0) { | 961 if (new_quota < 0) { |
1125 callback.Run(kQuotaErrorInvalidModification, -1); | 962 callback.Run(kQuotaErrorInvalidModification, -1); |
1126 return; | 963 return; |
1127 } | 964 } |
1128 | 965 |
1129 if (kPerHostPersistentQuotaLimit < new_quota) { | 966 // Cap the requested size at the per-host quota limit. |
1130 // Cap the requested size at the per-host quota limit. | 967 new_quota = std::min(new_quota, kPerHostPersistentQuotaLimit); |
1131 new_quota = kPerHostPersistentQuotaLimit; | |
1132 } | |
1133 | 968 |
1134 if (db_disabled_) { | 969 if (db_disabled_) { |
1135 callback.Run(kQuotaErrorInvalidAccess, -1); | 970 callback.Run(kQuotaErrorInvalidAccess, -1); |
1136 return; | 971 return; |
1137 } | 972 } |
1138 | 973 |
1139 int64_t* new_quota_ptr = new int64_t(new_quota); | 974 int64_t* new_quota_ptr = new int64_t(new_quota); |
1140 PostTaskAndReplyWithResultForDBThread( | 975 PostTaskAndReplyWithResultForDBThread( |
1141 FROM_HERE, | 976 FROM_HERE, |
1142 base::Bind(&SetPersistentHostQuotaOnDBThread, | 977 base::Bind(&SetPersistentHostQuotaOnDBThread, |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1282 QuotaManager::EvictionContext::EvictionContext() | 1117 QuotaManager::EvictionContext::EvictionContext() |
1283 : evicted_type(kStorageTypeUnknown) { | 1118 : evicted_type(kStorageTypeUnknown) { |
1284 } | 1119 } |
1285 | 1120 |
1286 QuotaManager::EvictionContext::~EvictionContext() { | 1121 QuotaManager::EvictionContext::~EvictionContext() { |
1287 } | 1122 } |
1288 | 1123 |
1289 void QuotaManager::LazyInitialize() { | 1124 void QuotaManager::LazyInitialize() { |
1290 DCHECK(io_thread_->BelongsToCurrentThread()); | 1125 DCHECK(io_thread_->BelongsToCurrentThread()); |
1291 if (database_) { | 1126 if (database_) { |
1292 // Initialization seems to be done already. | 1127 // Already initialized. |
1293 return; | 1128 return; |
1294 } | 1129 } |
1295 | 1130 |
1296 // Use an empty path to open an in-memory only databse for incognito. | 1131 // Use an empty path to open an in-memory only databse for incognito. |
1297 database_.reset(new QuotaDatabase(is_incognito_ ? base::FilePath() : | 1132 database_.reset(new QuotaDatabase(is_incognito_ ? base::FilePath() : |
1298 profile_path_.AppendASCII(kDatabaseName))); | 1133 profile_path_.AppendASCII(kDatabaseName))); |
1299 | 1134 |
1300 temporary_usage_tracker_.reset(new UsageTracker( | 1135 temporary_usage_tracker_.reset(new UsageTracker( |
1301 clients_, kStorageTypeTemporary, special_storage_policy_.get(), | 1136 clients_, kStorageTypeTemporary, special_storage_policy_.get(), |
1302 storage_monitor_.get())); | 1137 storage_monitor_.get())); |
1303 persistent_usage_tracker_.reset(new UsageTracker( | 1138 persistent_usage_tracker_.reset(new UsageTracker( |
1304 clients_, kStorageTypePersistent, special_storage_policy_.get(), | 1139 clients_, kStorageTypePersistent, special_storage_policy_.get(), |
1305 storage_monitor_.get())); | 1140 storage_monitor_.get())); |
1306 syncable_usage_tracker_.reset(new UsageTracker( | 1141 syncable_usage_tracker_.reset(new UsageTracker( |
1307 clients_, kStorageTypeSyncable, special_storage_policy_.get(), | 1142 clients_, kStorageTypeSyncable, special_storage_policy_.get(), |
1308 storage_monitor_.get())); | 1143 storage_monitor_.get())); |
1309 | 1144 |
1310 int64_t* temporary_quota_override = new int64_t(-1); | 1145 if (!is_incognito_) { |
1311 int64_t* desired_available_space = new int64_t(-1); | 1146 histogram_timer_.Start( |
1147 FROM_HERE, base::TimeDelta::FromMilliseconds(kReportHistogramInterval), | |
1148 this, &QuotaManager::ReportHistogram); | |
1149 } | |
1150 | |
1151 base::PostTaskAndReplyWithResult( | |
1152 db_thread_.get(), FROM_HERE, | |
1153 base::Bind(&QuotaDatabase::IsOriginDatabaseBootstrapped, | |
1154 base::Unretained(database_.get())), | |
1155 base::Bind(&QuotaManager::FinishLazyInitialize, | |
1156 weak_factory_.GetWeakPtr())); | |
1157 } | |
1158 | |
1159 void QuotaManager::FinishLazyInitialize(bool is_database_bootstrapped) { | |
1160 is_database_bootstrapped_ = is_database_bootstrapped; | |
1161 StartEviction(); | |
1162 } | |
1163 | |
1164 void QuotaManager::BootstrapDatabaseForEviction( | |
1165 const GetOriginCallback& did_get_origin_callback, | |
1166 int64_t usage, | |
1167 int64_t unlimited_usage) { | |
1168 // The usage cache should be fully populated now so we can | |
1169 // seed the database with origins we know about. | |
1170 std::set<GURL>* origins = new std::set<GURL>; | |
1171 temporary_usage_tracker_->GetCachedOrigins(origins); | |
1312 PostTaskAndReplyWithResultForDBThread( | 1172 PostTaskAndReplyWithResultForDBThread( |
1313 FROM_HERE, | 1173 FROM_HERE, base::Bind(&BootstrapDatabaseOnDBThread, base::Owned(origins)), |
1314 base::Bind(&InitializeOnDBThread, | 1174 base::Bind(&QuotaManager::DidBootstrapDatabase, |
1315 base::Unretained(temporary_quota_override), | 1175 weak_factory_.GetWeakPtr(), did_get_origin_callback)); |
1316 base::Unretained(desired_available_space)), | 1176 } |
1317 base::Bind(&QuotaManager::DidInitialize, | 1177 |
1318 weak_factory_.GetWeakPtr(), | 1178 void QuotaManager::DidBootstrapDatabase( |
1319 base::Owned(temporary_quota_override), | 1179 const GetOriginCallback& did_get_origin_callback, |
1320 base::Owned(desired_available_space))); | 1180 bool success) { |
1181 is_database_bootstrapped_ = success; | |
1182 DidDatabaseWork(success); | |
1183 GetLRUOrigin(kStorageTypeTemporary, did_get_origin_callback); | |
1321 } | 1184 } |
1322 | 1185 |
1323 void QuotaManager::RegisterClient(QuotaClient* client) { | 1186 void QuotaManager::RegisterClient(QuotaClient* client) { |
1324 DCHECK(!database_.get()); | 1187 DCHECK(!database_.get()); |
1325 clients_.push_back(client); | 1188 clients_.push_back(client); |
1326 } | 1189 } |
1327 | 1190 |
1328 UsageTracker* QuotaManager::GetUsageTracker(StorageType type) const { | 1191 UsageTracker* QuotaManager::GetUsageTracker(StorageType type) const { |
1329 switch (type) { | 1192 switch (type) { |
1330 case kStorageTypeTemporary: | 1193 case kStorageTypeTemporary: |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1405 base::Bind(&DumpOriginInfoTableHelper::DumpOriginInfoTableOnDBThread, | 1268 base::Bind(&DumpOriginInfoTableHelper::DumpOriginInfoTableOnDBThread, |
1406 base::Unretained(helper)), | 1269 base::Unretained(helper)), |
1407 base::Bind(&DumpOriginInfoTableHelper::DidDumpOriginInfoTable, | 1270 base::Bind(&DumpOriginInfoTableHelper::DidDumpOriginInfoTable, |
1408 base::Owned(helper), | 1271 base::Owned(helper), |
1409 weak_factory_.GetWeakPtr(), | 1272 weak_factory_.GetWeakPtr(), |
1410 callback)); | 1273 callback)); |
1411 } | 1274 } |
1412 | 1275 |
1413 void QuotaManager::StartEviction() { | 1276 void QuotaManager::StartEviction() { |
1414 DCHECK(!temporary_storage_evictor_.get()); | 1277 DCHECK(!temporary_storage_evictor_.get()); |
1278 if (eviction_disabled_) | |
1279 return; | |
1415 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( | 1280 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( |
1416 this, kEvictionIntervalInMilliSeconds)); | 1281 this, kEvictionIntervalInMilliSeconds)); |
1417 if (desired_available_space_ >= 0) | |
1418 temporary_storage_evictor_->set_min_available_disk_space_to_start_eviction( | |
1419 desired_available_space_); | |
1420 temporary_storage_evictor_->Start(); | 1282 temporary_storage_evictor_->Start(); |
1421 } | 1283 } |
1422 | 1284 |
1423 void QuotaManager::DeleteOriginFromDatabase(const GURL& origin, | 1285 void QuotaManager::DeleteOriginFromDatabase(const GURL& origin, |
1424 StorageType type, | 1286 StorageType type, |
1425 bool is_eviction) { | 1287 bool is_eviction) { |
1426 LazyInitialize(); | 1288 LazyInitialize(); |
1427 if (db_disabled_) | 1289 if (db_disabled_) |
1428 return; | 1290 return; |
1429 | 1291 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1595 const GetOriginCallback& callback) { | 1457 const GetOriginCallback& callback) { |
1596 LazyInitialize(); | 1458 LazyInitialize(); |
1597 // This must not be called while there's an in-flight task. | 1459 // This must not be called while there's an in-flight task. |
1598 DCHECK(!is_getting_eviction_origin_); | 1460 DCHECK(!is_getting_eviction_origin_); |
1599 is_getting_eviction_origin_ = true; | 1461 is_getting_eviction_origin_ = true; |
1600 | 1462 |
1601 GetOriginCallback did_get_origin_callback = | 1463 GetOriginCallback did_get_origin_callback = |
1602 base::Bind(&QuotaManager::DidGetEvictionOrigin, | 1464 base::Bind(&QuotaManager::DidGetEvictionOrigin, |
1603 weak_factory_.GetWeakPtr(), callback); | 1465 weak_factory_.GetWeakPtr(), callback); |
1604 | 1466 |
1605 if (type == kStorageTypeTemporary && temporary_storage_eviction_policy_) { | 1467 if (!is_database_bootstrapped_ && !eviction_disabled_) { |
1606 std::map<GURL, int64_t> usage_map; | 1468 // Once bootstrapped, GetLRUOrigin will be called. |
1607 // The cached origins are populated by the prior call to | 1469 GetGlobalUsage( |
1608 // GetUsageAndQuotaForEviction(). | 1470 kStorageTypeTemporary, |
1609 GetUsageTracker(kStorageTypeTemporary)->GetCachedOriginsUsage(&usage_map); | 1471 base::Bind(&QuotaManager::BootstrapDatabaseForEviction, |
1610 | 1472 weak_factory_.GetWeakPtr(), did_get_origin_callback)); |
1611 temporary_storage_eviction_policy_->GetEvictionOrigin( | |
1612 special_storage_policy_, GetEvictionOriginExceptions(extra_exceptions), | |
1613 usage_map, global_quota, did_get_origin_callback); | |
1614 | |
1615 return; | 1473 return; |
1616 } | 1474 } |
1617 | 1475 |
1618 // TODO(calamity): convert LRU origin retrieval into a QuotaEvictionPolicy. | |
1619 GetLRUOrigin(type, did_get_origin_callback); | 1476 GetLRUOrigin(type, did_get_origin_callback); |
1620 } | 1477 } |
1621 | 1478 |
1622 void QuotaManager::EvictOriginData(const GURL& origin, | 1479 void QuotaManager::EvictOriginData(const GURL& origin, |
1623 StorageType type, | 1480 StorageType type, |
1624 const EvictOriginDataCallback& callback) { | 1481 const StatusCallback& callback) { |
1625 DCHECK(io_thread_->BelongsToCurrentThread()); | 1482 DCHECK(io_thread_->BelongsToCurrentThread()); |
1626 DCHECK_EQ(type, kStorageTypeTemporary); | 1483 DCHECK_EQ(type, kStorageTypeTemporary); |
1627 | 1484 |
1628 eviction_context_.evicted_origin = origin; | 1485 eviction_context_.evicted_origin = origin; |
1629 eviction_context_.evicted_type = type; | 1486 eviction_context_.evicted_type = type; |
1630 eviction_context_.evict_origin_data_callback = callback; | 1487 eviction_context_.evict_origin_data_callback = callback; |
1631 | 1488 |
1632 DeleteOriginDataInternal(origin, type, QuotaClient::kAllClientsMask, true, | 1489 DeleteOriginDataInternal(origin, type, QuotaClient::kAllClientsMask, true, |
1633 base::Bind(&QuotaManager::DidOriginDataEvicted, | 1490 base::Bind(&QuotaManager::DidOriginDataEvicted, |
1634 weak_factory_.GetWeakPtr())); | 1491 weak_factory_.GetWeakPtr())); |
1635 } | 1492 } |
1636 | 1493 |
1637 void QuotaManager::GetUsageAndQuotaForEviction( | 1494 void QuotaManager::GetEvictionRoundInfo( |
1638 const UsageAndQuotaCallback& callback) { | 1495 const EvictionRoundInfoCallback& callback) { |
1639 // crbug.com/349708 | |
1640 TRACE_EVENT0("io", "QuotaManager::GetUsageAndQuotaForEviction"); | |
1641 | |
1642 DCHECK(io_thread_->BelongsToCurrentThread()); | 1496 DCHECK(io_thread_->BelongsToCurrentThread()); |
1643 LazyInitialize(); | 1497 LazyInitialize(); |
1644 | 1498 EvictionRoundInfoHelper* helper = new EvictionRoundInfoHelper(this, callback); |
1645 UsageAndQuotaCallbackDispatcher* dispatcher = | 1499 helper->Start(); |
1646 new UsageAndQuotaCallbackDispatcher(this); | |
1647 GetUsageTracker(kStorageTypeTemporary) | |
1648 ->GetGlobalLimitedUsage(dispatcher->GetGlobalLimitedUsageCallback()); | |
1649 GetTemporaryGlobalQuota(dispatcher->GetQuotaCallback()); | |
1650 GetAvailableSpace(dispatcher->GetAvailableSpaceCallback()); | |
1651 dispatcher->WaitForResults(callback); | |
1652 } | |
1653 | |
1654 void QuotaManager::AsyncGetVolumeInfo( | |
1655 const VolumeInfoCallback& callback) { | |
1656 DCHECK(io_thread_->BelongsToCurrentThread()); | |
1657 uint64_t* available_space = new uint64_t(0); | |
1658 uint64_t* total_space = new uint64_t(0); | |
1659 PostTaskAndReplyWithResult( | |
1660 db_thread_.get(), | |
1661 FROM_HERE, | |
1662 base::Bind(get_volume_info_fn_, | |
1663 profile_path_, | |
1664 base::Unretained(available_space), | |
1665 base::Unretained(total_space)), | |
1666 base::Bind(&QuotaManager::DidGetVolumeInfo, | |
1667 weak_factory_.GetWeakPtr(), | |
1668 callback, | |
1669 base::Owned(available_space), | |
1670 base::Owned(total_space))); | |
1671 } | |
1672 | |
1673 void QuotaManager::DidGetVolumeInfo( | |
1674 const VolumeInfoCallback& callback, | |
1675 uint64_t* available_space, uint64_t* total_space, bool success) { | |
1676 DCHECK(io_thread_->BelongsToCurrentThread()); | |
1677 callback.Run(success, *available_space, *total_space); | |
1678 } | 1500 } |
1679 | 1501 |
1680 void QuotaManager::GetLRUOrigin(StorageType type, | 1502 void QuotaManager::GetLRUOrigin(StorageType type, |
1681 const GetOriginCallback& callback) { | 1503 const GetOriginCallback& callback) { |
1682 LazyInitialize(); | 1504 LazyInitialize(); |
1683 // This must not be called while there's an in-flight task. | 1505 // This must not be called while there's an in-flight task. |
1684 DCHECK(lru_origin_callback_.is_null()); | 1506 DCHECK(lru_origin_callback_.is_null()); |
1685 lru_origin_callback_ = callback; | 1507 lru_origin_callback_ = callback; |
1686 if (db_disabled_) { | 1508 if (db_disabled_) { |
1687 lru_origin_callback_.Run(GURL()); | 1509 lru_origin_callback_.Run(GURL()); |
1688 lru_origin_callback_.Reset(); | 1510 lru_origin_callback_.Reset(); |
1689 return; | 1511 return; |
1690 } | 1512 } |
1691 | 1513 |
1692 GURL* url = new GURL; | 1514 GURL* url = new GURL; |
1693 PostTaskAndReplyWithResultForDBThread( | 1515 PostTaskAndReplyWithResultForDBThread( |
1694 FROM_HERE, base::Bind(&GetLRUOriginOnDBThread, type, | 1516 FROM_HERE, base::Bind(&GetLRUOriginOnDBThread, type, |
1695 GetEvictionOriginExceptions(std::set<GURL>()), | 1517 GetEvictionOriginExceptions(std::set<GURL>()), |
1696 base::RetainedRef(special_storage_policy_), | 1518 base::RetainedRef(special_storage_policy_), |
1697 base::Unretained(url)), | 1519 base::Unretained(url)), |
1698 base::Bind(&QuotaManager::DidGetLRUOrigin, weak_factory_.GetWeakPtr(), | 1520 base::Bind(&QuotaManager::DidGetLRUOrigin, weak_factory_.GetWeakPtr(), |
1699 base::Owned(url))); | 1521 base::Owned(url))); |
1700 } | 1522 } |
1701 | 1523 |
1702 void QuotaManager::DidSetTemporaryGlobalOverrideQuota( | |
1703 const QuotaCallback& callback, | |
1704 const int64_t* new_quota, | |
1705 bool success) { | |
1706 QuotaStatusCode status = kQuotaErrorInvalidAccess; | |
1707 DidDatabaseWork(success); | |
1708 if (success) { | |
1709 temporary_quota_override_ = *new_quota; | |
1710 status = kQuotaStatusOk; | |
1711 } | |
1712 | |
1713 if (callback.is_null()) | |
1714 return; | |
1715 | |
1716 callback.Run(status, *new_quota); | |
1717 } | |
1718 | |
1719 void QuotaManager::DidGetPersistentHostQuota(const std::string& host, | 1524 void QuotaManager::DidGetPersistentHostQuota(const std::string& host, |
1720 const int64_t* quota, | 1525 const int64_t* quota, |
1721 bool success) { | 1526 bool success) { |
1722 DidDatabaseWork(success); | 1527 DidDatabaseWork(success); |
1723 persistent_host_quota_callbacks_.Run(host, kQuotaStatusOk, *quota); | 1528 persistent_host_quota_callbacks_.Run( |
1529 host, kQuotaStatusOk, std::min(*quota, kPerHostPersistentQuotaLimit)); | |
1724 } | 1530 } |
1725 | 1531 |
1726 void QuotaManager::DidSetPersistentHostQuota(const std::string& host, | 1532 void QuotaManager::DidSetPersistentHostQuota(const std::string& host, |
1727 const QuotaCallback& callback, | 1533 const QuotaCallback& callback, |
1728 const int64_t* new_quota, | 1534 const int64_t* new_quota, |
1729 bool success) { | 1535 bool success) { |
1730 DidDatabaseWork(success); | 1536 DidDatabaseWork(success); |
1731 callback.Run(success ? kQuotaStatusOk : kQuotaErrorInvalidAccess, *new_quota); | 1537 callback.Run(success ? kQuotaStatusOk : kQuotaErrorInvalidAccess, *new_quota); |
1732 } | 1538 } |
1733 | 1539 |
1734 void QuotaManager::DidInitialize(int64_t* temporary_quota_override, | |
1735 int64_t* desired_available_space, | |
1736 bool success) { | |
1737 temporary_quota_override_ = *temporary_quota_override; | |
1738 desired_available_space_ = *desired_available_space; | |
1739 temporary_quota_initialized_ = true; | |
1740 DidDatabaseWork(success); | |
1741 | |
1742 if (!is_incognito_) { | |
1743 histogram_timer_.Start(FROM_HERE, | |
1744 base::TimeDelta::FromMilliseconds( | |
1745 kReportHistogramInterval), | |
1746 this, &QuotaManager::ReportHistogram); | |
1747 } | |
1748 | |
1749 db_initialization_callbacks_.Run(); | |
1750 GetTemporaryGlobalQuota( | |
1751 base::Bind(&QuotaManager::DidGetInitialTemporaryGlobalQuota, | |
1752 weak_factory_.GetWeakPtr(), base::TimeTicks::Now())); | |
1753 } | |
1754 | |
1755 void QuotaManager::DidGetLRUOrigin(const GURL* origin, | 1540 void QuotaManager::DidGetLRUOrigin(const GURL* origin, |
1756 bool success) { | 1541 bool success) { |
1757 DidDatabaseWork(success); | 1542 DidDatabaseWork(success); |
1758 | 1543 |
1759 lru_origin_callback_.Run(*origin); | 1544 lru_origin_callback_.Run(*origin); |
1760 lru_origin_callback_.Reset(); | 1545 lru_origin_callback_.Reset(); |
1761 } | 1546 } |
1762 | 1547 |
1763 void QuotaManager::DidGetInitialTemporaryGlobalQuota( | 1548 namespace { |
1764 base::TimeTicks start_ticks, | 1549 void DidGetSettingsThreadAdapter(base::TaskRunner* task_runner, |
1765 QuotaStatusCode status, | 1550 const OptionalQuotaSettingsCallback& callback, |
1766 int64_t quota_unused) { | 1551 base::Optional<QuotaSettings> settings) { |
1767 UMA_HISTOGRAM_LONG_TIMES( | 1552 task_runner->PostTask(FROM_HERE, base::Bind(callback, std::move(settings))); |
1768 "Quota.TimeToInitializeGlobalQuota", | 1553 } |
1769 base::TimeTicks::Now() - start_ticks); | 1554 } // namespace |
1770 | 1555 |
1771 if (eviction_disabled_) | 1556 void QuotaManager::GetQuotaSettings(const QuotaSettingsCallback& callback) { |
1557 if (base::TimeTicks::Now() - settings_timestamp_ < | |
1558 settings_.refresh_interval) { | |
1559 callback.Run(settings_); | |
1560 return; | |
1561 } | |
1562 | |
1563 if (!settings_callbacks_.Add(callback)) | |
1772 return; | 1564 return; |
1773 | 1565 |
1774 std::set<GURL>* origins = new std::set<GURL>; | 1566 // We invoke our clients GetQuotaSettingsFunc on the |
1775 temporary_usage_tracker_->GetCachedOrigins(origins); | 1567 // UI thread and plumb the resulting value back to this thread. |
1776 // This will call the StartEviction() when initial origin registration | 1568 get_settings_task_runner_->PostTask( |
1777 // is completed. | |
1778 PostTaskAndReplyWithResultForDBThread( | |
1779 FROM_HERE, | 1569 FROM_HERE, |
1780 base::Bind(&InitializeTemporaryOriginsInfoOnDBThread, | 1570 base::Bind( |
1781 base::Owned(origins)), | 1571 get_settings_function_, |
1782 base::Bind(&QuotaManager::DidInitializeTemporaryOriginsInfo, | 1572 base::Bind( |
1573 &DidGetSettingsThreadAdapter, | |
1574 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), | |
1575 base::Bind(&QuotaManager::DidGetSettings, | |
1576 weak_factory_.GetWeakPtr(), base::TimeTicks::Now())))); | |
1577 } | |
1578 | |
1579 void QuotaManager::DidGetSettings(base::TimeTicks start_ticks, | |
1580 base::Optional<QuotaSettings> settings) { | |
1581 if (!settings) { | |
1582 settings = settings_; | |
1583 settings->refresh_interval = base::TimeDelta::FromMinutes(1); | |
1584 } | |
1585 SetQuotaSettings(*settings); | |
1586 settings_callbacks_.Run(*settings); | |
1587 UMA_HISTOGRAM_MBYTES("Quota.GlobalTemporaryPoolSize", settings->pool_size); | |
1588 UMA_HISTOGRAM_LONG_TIMES("Quota.TimeToGetSettings", | |
1589 base::TimeTicks::Now() - start_ticks); | |
1590 LOG_IF(WARNING, settings->pool_size == 0) | |
1591 << "No storage quota provided in QuotaSettings."; | |
1592 } | |
1593 | |
1594 void QuotaManager::GetStorageCapacity(const StorageCapacityCallback& callback) { | |
1595 if (!storage_capacity_callbacks_.Add(callback)) | |
1596 return; | |
1597 if (is_incognito_) { | |
1598 GetQuotaSettings( | |
1599 base::Bind(&QuotaManager::ContinueIncognitoGetStorageCapacity, | |
1600 weak_factory_.GetWeakPtr())); | |
1601 return; | |
1602 } | |
1603 base::PostTaskAndReplyWithResult( | |
1604 db_thread_.get(), FROM_HERE, | |
1605 base::Bind(&QuotaManager::CallGetVolumeInfo, get_volume_info_fn_, | |
1606 profile_path_), | |
1607 base::Bind(&QuotaManager::DidGetStorageCapacity, | |
1783 weak_factory_.GetWeakPtr())); | 1608 weak_factory_.GetWeakPtr())); |
1784 } | 1609 } |
1785 | 1610 |
1786 void QuotaManager::DidInitializeTemporaryOriginsInfo(bool success) { | 1611 void QuotaManager::ContinueIncognitoGetStorageCapacity( |
1787 DidDatabaseWork(success); | 1612 const QuotaSettings& settings) { |
1788 if (success) | 1613 int64_t current_usage = |
1789 StartEviction(); | 1614 GetUsageTracker(kStorageTypeTemporary)->GetCachedUsage(); |
1615 current_usage += GetUsageTracker(kStorageTypePersistent)->GetCachedUsage(); | |
1616 int64_t available_space = | |
1617 std::max(INT64_C(0), settings.pool_size - current_usage); | |
1618 DidGetStorageCapacity(std::make_pair(settings.pool_size, available_space)); | |
1790 } | 1619 } |
1791 | 1620 |
1792 void QuotaManager::DidGetAvailableSpace(int64_t space) { | 1621 void QuotaManager::DidGetStorageCapacity( |
1793 // crbug.com/349708 | 1622 const std::pair<int64_t, int64_t>& total_and_available) { |
1794 TRACE_EVENT1("io", "QuotaManager::DidGetAvailableSpace", | 1623 storage_capacity_callbacks_.Run(total_and_available.first, |
1795 "n_callbacks", available_space_callbacks_.size()); | 1624 total_and_available.second); |
1796 | |
1797 available_space_callbacks_.Run(kQuotaStatusOk, space); | |
1798 } | 1625 } |
1799 | 1626 |
1800 void QuotaManager::DidDatabaseWork(bool success) { | 1627 void QuotaManager::DidDatabaseWork(bool success) { |
1801 db_disabled_ = !success; | 1628 db_disabled_ = !success; |
1802 } | 1629 } |
1803 | 1630 |
1804 void QuotaManager::DeleteOnCorrectThread() const { | 1631 void QuotaManager::DeleteOnCorrectThread() const { |
1805 if (!io_thread_->BelongsToCurrentThread() && | 1632 if (!io_thread_->BelongsToCurrentThread() && |
1806 io_thread_->DeleteSoon(FROM_HERE, this)) { | 1633 io_thread_->DeleteSoon(FROM_HERE, this)) { |
1807 return; | 1634 return; |
1808 } | 1635 } |
1809 delete this; | 1636 delete this; |
1810 } | 1637 } |
1811 | 1638 |
1812 void QuotaManager::PostTaskAndReplyWithResultForDBThread( | 1639 void QuotaManager::PostTaskAndReplyWithResultForDBThread( |
1813 const tracked_objects::Location& from_here, | 1640 const tracked_objects::Location& from_here, |
1814 const base::Callback<bool(QuotaDatabase*)>& task, | 1641 const base::Callback<bool(QuotaDatabase*)>& task, |
1815 const base::Callback<void(bool)>& reply) { | 1642 const base::Callback<void(bool)>& reply) { |
1816 // Deleting manager will post another task to DB thread to delete | 1643 // Deleting manager will post another task to DB thread to delete |
1817 // |database_|, therefore we can be sure that database_ is alive when this | 1644 // |database_|, therefore we can be sure that database_ is alive when this |
1818 // task runs. | 1645 // task runs. |
1819 base::PostTaskAndReplyWithResult( | 1646 base::PostTaskAndReplyWithResult( |
1820 db_thread_.get(), | 1647 db_thread_.get(), |
1821 from_here, | 1648 from_here, |
1822 base::Bind(task, base::Unretained(database_.get())), | 1649 base::Bind(task, base::Unretained(database_.get())), |
1823 reply); | 1650 reply); |
1824 } | 1651 } |
1825 | 1652 |
1826 // static | 1653 // static |
1827 int64_t QuotaManager::CallGetAmountOfFreeDiskSpace( | 1654 std::pair<int64_t, int64_t> QuotaManager::CallGetVolumeInfo( |
1828 GetVolumeInfoFn get_volume_info_fn, | 1655 GetVolumeInfoFn get_volume_info_fn, |
1829 const base::FilePath& profile_path) { | 1656 const base::FilePath& path) { |
1830 // crbug.com/349708 | 1657 // crbug.com/349708 |
1831 TRACE_EVENT0("io", "CallSystemGetAmountOfFreeDiskSpace"); | 1658 TRACE_EVENT0("io", "CallGetVolumeInfo"); |
1832 if (!base::CreateDirectory(profile_path)) { | 1659 if (!base::CreateDirectory(path)) { |
1833 LOG(WARNING) << "Create directory failed for path" << profile_path.value(); | 1660 LOG(WARNING) << "Create directory failed for path" << path.value(); |
1834 return 0; | 1661 return std::make_pair<int64_t, int64_t>(0, 0); |
1835 } | 1662 } |
1836 uint64_t available, total; | 1663 std::pair<int64_t, int64_t> total_and_available = get_volume_info_fn(path); |
michaeln
2017/02/01 00:13:42
@chris, i'll change this to a std::tuple<> like we
| |
1837 if (!get_volume_info_fn(profile_path, &available, &total)) { | 1664 if (total_and_available.first < 0 || total_and_available.second < 0) { |
1838 return 0; | 1665 LOG(WARNING) << "Unable to get volume info: " << path.value(); |
1666 return std::make_pair<int64_t, int64_t>(0, 0); | |
1839 } | 1667 } |
1840 UMA_HISTOGRAM_MBYTES("Quota.AvailableDiskSpace", available); | 1668 UMA_HISTOGRAM_MBYTES("Quota.TotalDiskSpace", total_and_available.first); |
1841 UMA_HISTOGRAM_MBYTES("Quota.TotalDiskSpace", total); | 1669 UMA_HISTOGRAM_MBYTES("Quota.AvailableDiskSpace", total_and_available.second); |
1842 return static_cast<int64_t>(available); | 1670 UMA_HISTOGRAM_PERCENTAGE( |
1671 "Quota.PercentDiskAvailable", | |
1672 static_cast<int>((total_and_available.second * 100) / | |
1673 total_and_available.first)); | |
1674 return total_and_available; | |
1843 } | 1675 } |
1844 | 1676 |
1845 //static | 1677 // static |
1846 bool QuotaManager::GetVolumeInfo(const base::FilePath& path, | 1678 std::pair<int64_t, int64_t> QuotaManager::GetVolumeInfo( |
1847 uint64_t* available_space, | 1679 const base::FilePath& path) { |
1848 uint64_t* total_size) { | 1680 return std::make_pair(base::SysInfo::AmountOfTotalDiskSpace(path), |
1849 // Inspired by similar code in the base::SysInfo class. | 1681 base::SysInfo::AmountOfFreeDiskSpace(path)); |
1850 base::ThreadRestrictions::AssertIOAllowed(); | |
1851 | |
1852 int64_t available = base::SysInfo::AmountOfFreeDiskSpace(path); | |
1853 if (available < 0) | |
1854 return false; | |
1855 int64_t total = base::SysInfo::AmountOfTotalDiskSpace(path); | |
1856 if (total < 0) | |
1857 return false; | |
1858 | |
1859 *available_space = static_cast<uint64_t>(available); | |
1860 *total_size = static_cast<uint64_t>(total); | |
1861 return true; | |
1862 } | 1682 } |
1863 | 1683 |
1864 } // namespace storage | 1684 } // namespace storage |
OLD | NEW |