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/quota/usage_tracker.h" | 5 #include "webkit/browser/quota/usage_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <deque> | 8 #include <deque> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 17 #include "webkit/browser/quota/storage_monitor.h" |
| 18 #include "webkit/browser/quota/storage_observer.h" |
17 | 19 |
18 namespace quota { | 20 namespace quota { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 typedef ClientUsageTracker::OriginUsageAccumulator OriginUsageAccumulator; | 24 typedef ClientUsageTracker::OriginUsageAccumulator OriginUsageAccumulator; |
23 typedef ClientUsageTracker::OriginSetByHost OriginSetByHost; | 25 typedef ClientUsageTracker::OriginSetByHost OriginSetByHost; |
24 | 26 |
25 void DidGetOriginUsage(const OriginUsageAccumulator& accumulator, | 27 void DidGetOriginUsage(const OriginUsageAccumulator& accumulator, |
26 const GURL& origin, | 28 const GURL& origin, |
27 int64 usage) { | 29 int64 usage) { |
28 accumulator.Run(origin, usage); | 30 accumulator.Run(origin, usage); |
29 } | 31 } |
30 | 32 |
31 void DidGetHostUsage(const UsageCallback& callback, | 33 void DidGetHostUsage(const UsageCallback& callback, |
32 int64 limited_usage, | 34 int64 limited_usage, |
33 int64 unlimited_usage) { | 35 int64 unlimited_usage) { |
34 DCHECK_GE(limited_usage, 0); | 36 DCHECK_GE(limited_usage, 0); |
35 DCHECK_GE(unlimited_usage, 0); | 37 DCHECK_GE(unlimited_usage, 0); |
36 callback.Run(limited_usage + unlimited_usage); | 38 callback.Run(limited_usage + unlimited_usage); |
37 } | 39 } |
38 | 40 |
39 void NoopHostUsageCallback(int64 usage) {} | |
40 | |
41 bool EraseOriginFromOriginSet(OriginSetByHost* origins_by_host, | 41 bool EraseOriginFromOriginSet(OriginSetByHost* origins_by_host, |
42 const std::string& host, | 42 const std::string& host, |
43 const GURL& origin) { | 43 const GURL& origin) { |
44 OriginSetByHost::iterator found = origins_by_host->find(host); | 44 OriginSetByHost::iterator found = origins_by_host->find(host); |
45 if (found == origins_by_host->end()) | 45 if (found == origins_by_host->end()) |
46 return false; | 46 return false; |
47 | 47 |
48 if (!found->second.erase(origin)) | 48 if (!found->second.erase(origin)) |
49 return false; | 49 return false; |
50 | 50 |
(...skipping 14 matching lines...) Expand all Loading... |
65 int64 global_unlimited_usage) { | 65 int64 global_unlimited_usage) { |
66 callback.Run(total_global_usage - global_unlimited_usage); | 66 callback.Run(total_global_usage - global_unlimited_usage); |
67 } | 67 } |
68 | 68 |
69 } // namespace | 69 } // namespace |
70 | 70 |
71 // UsageTracker ---------------------------------------------------------- | 71 // UsageTracker ---------------------------------------------------------- |
72 | 72 |
73 UsageTracker::UsageTracker(const QuotaClientList& clients, | 73 UsageTracker::UsageTracker(const QuotaClientList& clients, |
74 StorageType type, | 74 StorageType type, |
75 SpecialStoragePolicy* special_storage_policy) | 75 SpecialStoragePolicy* special_storage_policy, |
| 76 StorageMonitor* storage_monitor) |
76 : type_(type), | 77 : type_(type), |
| 78 storage_monitor_(storage_monitor), |
77 weak_factory_(this) { | 79 weak_factory_(this) { |
78 for (QuotaClientList::const_iterator iter = clients.begin(); | 80 for (QuotaClientList::const_iterator iter = clients.begin(); |
79 iter != clients.end(); | 81 iter != clients.end(); |
80 ++iter) { | 82 ++iter) { |
81 if ((*iter)->DoesSupport(type)) { | 83 if ((*iter)->DoesSupport(type)) { |
82 client_tracker_map_[(*iter)->id()] = | 84 client_tracker_map_[(*iter)->id()] = |
83 new ClientUsageTracker(this, *iter, type, special_storage_policy); | 85 new ClientUsageTracker(this, *iter, type, special_storage_policy, |
| 86 storage_monitor_); |
84 } | 87 } |
85 } | 88 } |
86 } | 89 } |
87 | 90 |
88 UsageTracker::~UsageTracker() { | 91 UsageTracker::~UsageTracker() { |
89 STLDeleteValues(&client_tracker_map_); | 92 STLDeleteValues(&client_tracker_map_); |
90 } | 93 } |
91 | 94 |
92 ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) { | 95 ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) { |
93 ClientTrackerMap::iterator found = client_tracker_map_.find(client_id); | 96 ClientTrackerMap::iterator found = client_tracker_map_.find(client_id); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 267 |
265 // All the clients have returned their usage data. Dispatch the | 268 // All the clients have returned their usage data. Dispatch the |
266 // pending callbacks. | 269 // pending callbacks. |
267 host_usage_callbacks_.Run(host, MakeTuple(info->usage)); | 270 host_usage_callbacks_.Run(host, MakeTuple(info->usage)); |
268 } | 271 } |
269 | 272 |
270 // ClientUsageTracker ---------------------------------------------------- | 273 // ClientUsageTracker ---------------------------------------------------- |
271 | 274 |
272 ClientUsageTracker::ClientUsageTracker( | 275 ClientUsageTracker::ClientUsageTracker( |
273 UsageTracker* tracker, QuotaClient* client, StorageType type, | 276 UsageTracker* tracker, QuotaClient* client, StorageType type, |
274 SpecialStoragePolicy* special_storage_policy) | 277 SpecialStoragePolicy* special_storage_policy, |
| 278 StorageMonitor* storage_monitor) |
275 : tracker_(tracker), | 279 : tracker_(tracker), |
276 client_(client), | 280 client_(client), |
277 type_(type), | 281 type_(type), |
| 282 storage_monitor_(storage_monitor), |
278 global_limited_usage_(0), | 283 global_limited_usage_(0), |
279 global_unlimited_usage_(0), | 284 global_unlimited_usage_(0), |
280 global_usage_retrieved_(false), | 285 global_usage_retrieved_(false), |
281 special_storage_policy_(special_storage_policy) { | 286 special_storage_policy_(special_storage_policy) { |
282 DCHECK(tracker_); | 287 DCHECK(tracker_); |
283 DCHECK(client_); | 288 DCHECK(client_); |
284 if (special_storage_policy_.get()) | 289 if (special_storage_policy_.get()) |
285 special_storage_policy_->AddObserver(this); | 290 special_storage_policy_->AddObserver(this); |
286 } | 291 } |
287 | 292 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 if (!IsUsageCacheEnabledForOrigin(origin)) | 362 if (!IsUsageCacheEnabledForOrigin(origin)) |
358 return; | 363 return; |
359 | 364 |
360 cached_usage_by_host_[host][origin] += delta; | 365 cached_usage_by_host_[host][origin] += delta; |
361 if (IsStorageUnlimited(origin)) | 366 if (IsStorageUnlimited(origin)) |
362 global_unlimited_usage_ += delta; | 367 global_unlimited_usage_ += delta; |
363 else | 368 else |
364 global_limited_usage_ += delta; | 369 global_limited_usage_ += delta; |
365 DCHECK_GE(cached_usage_by_host_[host][origin], 0); | 370 DCHECK_GE(cached_usage_by_host_[host][origin], 0); |
366 DCHECK_GE(global_limited_usage_, 0); | 371 DCHECK_GE(global_limited_usage_, 0); |
| 372 |
| 373 // Notify the usage monitor that usage has changed. The storage monitor may |
| 374 // be NULL during tests. |
| 375 if (storage_monitor_) { |
| 376 StorageObserver::Filter filter(type_, origin); |
| 377 storage_monitor_->NotifyUsageChange(filter, delta); |
| 378 } |
367 return; | 379 return; |
368 } | 380 } |
369 | 381 |
370 // We don't know about this host yet, so populate our cache for it. | 382 // We don't know about this host yet, so populate our cache for it. |
371 GetHostUsage(host, base::Bind(&NoopHostUsageCallback)); | 383 GetHostUsage(host, base::Bind(&ClientUsageTracker::DidGetHostUsageAfterUpdate, |
| 384 AsWeakPtr(), origin)); |
372 } | 385 } |
373 | 386 |
374 void ClientUsageTracker::GetCachedHostsUsage( | 387 void ClientUsageTracker::GetCachedHostsUsage( |
375 std::map<std::string, int64>* host_usage) const { | 388 std::map<std::string, int64>* host_usage) const { |
376 DCHECK(host_usage); | 389 DCHECK(host_usage); |
377 for (HostUsageMap::const_iterator host_iter = cached_usage_by_host_.begin(); | 390 for (HostUsageMap::const_iterator host_iter = cached_usage_by_host_.begin(); |
378 host_iter != cached_usage_by_host_.end(); host_iter++) { | 391 host_iter != cached_usage_by_host_.end(); host_iter++) { |
379 const std::string& host = host_iter->first; | 392 const std::string& host = host_iter->first; |
380 (*host_usage)[host] += GetCachedHostUsage(host); | 393 (*host_usage)[host] += GetCachedHostUsage(host); |
381 } | 394 } |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 AddCachedOrigin(origin, usage); | 555 AddCachedOrigin(origin, usage); |
543 } | 556 } |
544 if (--info->pending_jobs) | 557 if (--info->pending_jobs) |
545 return; | 558 return; |
546 | 559 |
547 AddCachedHost(host); | 560 AddCachedHost(host); |
548 host_usage_accumulators_.Run( | 561 host_usage_accumulators_.Run( |
549 host, MakeTuple(info->limited_usage, info->unlimited_usage)); | 562 host, MakeTuple(info->limited_usage, info->unlimited_usage)); |
550 } | 563 } |
551 | 564 |
| 565 void ClientUsageTracker::DidGetHostUsageAfterUpdate( |
| 566 const GURL& origin, int64 usage) { |
| 567 if (!storage_monitor_) |
| 568 return; |
| 569 |
| 570 StorageObserver::Filter filter(type_, origin); |
| 571 storage_monitor_->NotifyUsageChange(filter, 0); |
| 572 } |
| 573 |
552 void ClientUsageTracker::AddCachedOrigin( | 574 void ClientUsageTracker::AddCachedOrigin( |
553 const GURL& origin, int64 new_usage) { | 575 const GURL& origin, int64 new_usage) { |
554 DCHECK(IsUsageCacheEnabledForOrigin(origin)); | 576 DCHECK(IsUsageCacheEnabledForOrigin(origin)); |
555 | 577 |
556 std::string host = net::GetHostOrSpecFromURL(origin); | 578 std::string host = net::GetHostOrSpecFromURL(origin); |
557 int64* usage = &cached_usage_by_host_[host][origin]; | 579 int64* usage = &cached_usage_by_host_[host][origin]; |
558 int64 delta = new_usage - *usage; | 580 int64 delta = new_usage - *usage; |
559 *usage = new_usage; | 581 *usage = new_usage; |
560 if (delta) { | 582 if (delta) { |
561 if (IsStorageUnlimited(origin)) | 583 if (IsStorageUnlimited(origin)) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 } | 685 } |
664 | 686 |
665 bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const { | 687 bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const { |
666 if (type_ == kStorageTypeSyncable) | 688 if (type_ == kStorageTypeSyncable) |
667 return false; | 689 return false; |
668 return special_storage_policy_.get() && | 690 return special_storage_policy_.get() && |
669 special_storage_policy_->IsStorageUnlimited(origin); | 691 special_storage_policy_->IsStorageUnlimited(origin); |
670 } | 692 } |
671 | 693 |
672 } // namespace quota | 694 } // namespace quota |
OLD | NEW |