| 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/usage_tracker.h" | 5 #include "storage/browser/quota/usage_tracker.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 UsageTracker::UsageTracker(const QuotaClientList& clients, | 26 UsageTracker::UsageTracker(const QuotaClientList& clients, |
| 27 StorageType type, | 27 StorageType type, |
| 28 SpecialStoragePolicy* special_storage_policy, | 28 SpecialStoragePolicy* special_storage_policy, |
| 29 StorageMonitor* storage_monitor) | 29 StorageMonitor* storage_monitor) |
| 30 : type_(type), | 30 : type_(type), |
| 31 storage_monitor_(storage_monitor), | 31 storage_monitor_(storage_monitor), |
| 32 weak_factory_(this) { | 32 weak_factory_(this) { |
| 33 for (QuotaClientList::const_iterator iter = clients.begin(); | 33 for (const auto& client : clients) { |
| 34 iter != clients.end(); | 34 if (client->DoesSupport(type)) { |
| 35 ++iter) { | 35 client_tracker_map_[client->id()] = |
| 36 if ((*iter)->DoesSupport(type)) { | 36 new ClientUsageTracker(this, client, type, special_storage_policy, |
| 37 client_tracker_map_[(*iter)->id()] = | |
| 38 new ClientUsageTracker(this, *iter, type, special_storage_policy, | |
| 39 storage_monitor_); | 37 storage_monitor_); |
| 40 } | 38 } |
| 41 } | 39 } |
| 42 } | 40 } |
| 43 | 41 |
| 44 UsageTracker::~UsageTracker() { | 42 UsageTracker::~UsageTracker() { |
| 45 STLDeleteValues(&client_tracker_map_); | 43 STLDeleteValues(&client_tracker_map_); |
| 46 } | 44 } |
| 47 | 45 |
| 48 ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) { | 46 ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) { |
| 49 ClientTrackerMap::iterator found = client_tracker_map_.find(client_id); | 47 ClientTrackerMap::iterator found = client_tracker_map_.find(client_id); |
| 50 if (found != client_tracker_map_.end()) | 48 if (found != client_tracker_map_.end()) |
| 51 return found->second; | 49 return found->second; |
| 52 return NULL; | 50 return nullptr; |
| 53 } | 51 } |
| 54 | 52 |
| 55 void UsageTracker::GetGlobalLimitedUsage(const UsageCallback& callback) { | 53 void UsageTracker::GetGlobalLimitedUsage(const UsageCallback& callback) { |
| 56 if (global_usage_callbacks_.HasCallbacks()) { | 54 if (global_usage_callbacks_.HasCallbacks()) { |
| 57 global_usage_callbacks_.Add(base::Bind( | 55 global_usage_callbacks_.Add(base::Bind( |
| 58 &DidGetGlobalUsageForLimitedGlobalUsage, callback)); | 56 &DidGetGlobalUsageForLimitedGlobalUsage, callback)); |
| 59 return; | 57 return; |
| 60 } | 58 } |
| 61 | 59 |
| 62 if (!global_limited_usage_callbacks_.Add(callback)) | 60 if (!global_limited_usage_callbacks_.Add(callback)) |
| 63 return; | 61 return; |
| 64 | 62 |
| 65 AccumulateInfo* info = new AccumulateInfo; | 63 AccumulateInfo* info = new AccumulateInfo; |
| 66 // Calling GetGlobalLimitedUsage(accumulator) may synchronously | 64 // Calling GetGlobalLimitedUsage(accumulator) may synchronously |
| 67 // return if the usage is cached, which may in turn dispatch | 65 // return if the usage is cached, which may in turn dispatch |
| 68 // the completion callback before we finish looping over | 66 // the completion callback before we finish looping over |
| 69 // all clients (because info->pending_clients may reach 0 | 67 // all clients (because info->pending_clients may reach 0 |
| 70 // during the loop). | 68 // during the loop). |
| 71 // To avoid this, we add one more pending client as a sentinel | 69 // To avoid this, we add one more pending client as a sentinel |
| 72 // and fire the sentinel callback at the end. | 70 // and fire the sentinel callback at the end. |
| 73 info->pending_clients = client_tracker_map_.size() + 1; | 71 info->pending_clients = client_tracker_map_.size() + 1; |
| 74 UsageCallback accumulator = base::Bind( | 72 UsageCallback accumulator = base::Bind( |
| 75 &UsageTracker::AccumulateClientGlobalLimitedUsage, | 73 &UsageTracker::AccumulateClientGlobalLimitedUsage, |
| 76 weak_factory_.GetWeakPtr(), base::Owned(info)); | 74 weak_factory_.GetWeakPtr(), base::Owned(info)); |
| 77 | 75 |
| 78 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); | 76 for (const auto& client_id_and_tracker : client_tracker_map_) |
| 79 iter != client_tracker_map_.end(); | 77 client_id_and_tracker.second->GetGlobalLimitedUsage(accumulator); |
| 80 ++iter) | |
| 81 iter->second->GetGlobalLimitedUsage(accumulator); | |
| 82 | 78 |
| 83 // Fire the sentinel as we've now called GetGlobalUsage for all clients. | 79 // Fire the sentinel as we've now called GetGlobalUsage for all clients. |
| 84 accumulator.Run(0); | 80 accumulator.Run(0); |
| 85 } | 81 } |
| 86 | 82 |
| 87 void UsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) { | 83 void UsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) { |
| 88 if (!global_usage_callbacks_.Add(callback)) | 84 if (!global_usage_callbacks_.Add(callback)) |
| 89 return; | 85 return; |
| 90 | 86 |
| 91 AccumulateInfo* info = new AccumulateInfo; | 87 AccumulateInfo* info = new AccumulateInfo; |
| 92 // Calling GetGlobalUsage(accumulator) may synchronously | 88 // Calling GetGlobalUsage(accumulator) may synchronously |
| 93 // return if the usage is cached, which may in turn dispatch | 89 // return if the usage is cached, which may in turn dispatch |
| 94 // the completion callback before we finish looping over | 90 // the completion callback before we finish looping over |
| 95 // all clients (because info->pending_clients may reach 0 | 91 // all clients (because info->pending_clients may reach 0 |
| 96 // during the loop). | 92 // during the loop). |
| 97 // To avoid this, we add one more pending client as a sentinel | 93 // To avoid this, we add one more pending client as a sentinel |
| 98 // and fire the sentinel callback at the end. | 94 // and fire the sentinel callback at the end. |
| 99 info->pending_clients = client_tracker_map_.size() + 1; | 95 info->pending_clients = client_tracker_map_.size() + 1; |
| 100 GlobalUsageCallback accumulator = base::Bind( | 96 GlobalUsageCallback accumulator = base::Bind( |
| 101 &UsageTracker::AccumulateClientGlobalUsage, weak_factory_.GetWeakPtr(), | 97 &UsageTracker::AccumulateClientGlobalUsage, weak_factory_.GetWeakPtr(), |
| 102 base::Owned(info)); | 98 base::Owned(info)); |
| 103 | 99 |
| 104 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); | 100 for (const auto& client_id_and_tracker : client_tracker_map_) |
| 105 iter != client_tracker_map_.end(); | 101 client_id_and_tracker.second->GetGlobalUsage(accumulator); |
| 106 ++iter) | |
| 107 iter->second->GetGlobalUsage(accumulator); | |
| 108 | 102 |
| 109 // Fire the sentinel as we've now called GetGlobalUsage for all clients. | 103 // Fire the sentinel as we've now called GetGlobalUsage for all clients. |
| 110 accumulator.Run(0, 0); | 104 accumulator.Run(0, 0); |
| 111 } | 105 } |
| 112 | 106 |
| 113 void UsageTracker::GetHostUsage(const std::string& host, | 107 void UsageTracker::GetHostUsage(const std::string& host, |
| 114 const UsageCallback& callback) { | 108 const UsageCallback& callback) { |
| 115 if (!host_usage_callbacks_.Add(host, callback)) | 109 if (!host_usage_callbacks_.Add(host, callback)) |
| 116 return; | 110 return; |
| 117 | 111 |
| 118 AccumulateInfo* info = new AccumulateInfo; | 112 AccumulateInfo* info = new AccumulateInfo; |
| 119 // Calling GetHostUsage(accumulator) may synchronously | 113 // Calling GetHostUsage(accumulator) may synchronously |
| 120 // return if the usage is cached, which may in turn dispatch | 114 // return if the usage is cached, which may in turn dispatch |
| 121 // the completion callback before we finish looping over | 115 // the completion callback before we finish looping over |
| 122 // all clients (because info->pending_clients may reach 0 | 116 // all clients (because info->pending_clients may reach 0 |
| 123 // during the loop). | 117 // during the loop). |
| 124 // To avoid this, we add one more pending client as a sentinel | 118 // To avoid this, we add one more pending client as a sentinel |
| 125 // and fire the sentinel callback at the end. | 119 // and fire the sentinel callback at the end. |
| 126 info->pending_clients = client_tracker_map_.size() + 1; | 120 info->pending_clients = client_tracker_map_.size() + 1; |
| 127 UsageCallback accumulator = base::Bind( | 121 UsageCallback accumulator = base::Bind( |
| 128 &UsageTracker::AccumulateClientHostUsage, weak_factory_.GetWeakPtr(), | 122 &UsageTracker::AccumulateClientHostUsage, weak_factory_.GetWeakPtr(), |
| 129 base::Owned(info), host); | 123 base::Owned(info), host); |
| 130 | 124 |
| 131 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); | 125 for (const auto& client_id_and_tracker : client_tracker_map_) |
| 132 iter != client_tracker_map_.end(); | 126 client_id_and_tracker.second->GetHostUsage(host, accumulator); |
| 133 ++iter) | |
| 134 iter->second->GetHostUsage(host, accumulator); | |
| 135 | 127 |
| 136 // Fire the sentinel as we've now called GetHostUsage for all clients. | 128 // Fire the sentinel as we've now called GetHostUsage for all clients. |
| 137 accumulator.Run(0); | 129 accumulator.Run(0); |
| 138 } | 130 } |
| 139 | 131 |
| 140 void UsageTracker::UpdateUsageCache( | 132 void UsageTracker::UpdateUsageCache( |
| 141 QuotaClient::ID client_id, const GURL& origin, int64 delta) { | 133 QuotaClient::ID client_id, const GURL& origin, int64 delta) { |
| 142 ClientUsageTracker* client_tracker = GetClientTracker(client_id); | 134 ClientUsageTracker* client_tracker = GetClientTracker(client_id); |
| 143 DCHECK(client_tracker); | 135 DCHECK(client_tracker); |
| 144 client_tracker->UpdateUsageCache(origin, delta); | 136 client_tracker->UpdateUsageCache(origin, delta); |
| 145 } | 137 } |
| 146 | 138 |
| 147 void UsageTracker::GetCachedHostsUsage( | 139 void UsageTracker::GetCachedHostsUsage( |
| 148 std::map<std::string, int64>* host_usage) const { | 140 std::map<std::string, int64>* host_usage) const { |
| 149 DCHECK(host_usage); | 141 DCHECK(host_usage); |
| 150 host_usage->clear(); | 142 host_usage->clear(); |
| 151 for (ClientTrackerMap::const_iterator iter = client_tracker_map_.begin(); | 143 for (const auto& client_id_and_tracker : client_tracker_map_) |
| 152 iter != client_tracker_map_.end(); ++iter) { | 144 client_id_and_tracker.second->GetCachedHostsUsage(host_usage); |
| 153 iter->second->GetCachedHostsUsage(host_usage); | |
| 154 } | |
| 155 } | 145 } |
| 156 | 146 |
| 157 void UsageTracker::GetCachedOrigins(std::set<GURL>* origins) const { | 147 void UsageTracker::GetCachedOrigins(std::set<GURL>* origins) const { |
| 158 DCHECK(origins); | 148 DCHECK(origins); |
| 159 origins->clear(); | 149 origins->clear(); |
| 160 for (ClientTrackerMap::const_iterator iter = client_tracker_map_.begin(); | 150 for (const auto& client_id_and_tracker : client_tracker_map_) |
| 161 iter != client_tracker_map_.end(); ++iter) { | 151 client_id_and_tracker.second->GetCachedOrigins(origins); |
| 162 iter->second->GetCachedOrigins(origins); | |
| 163 } | |
| 164 } | 152 } |
| 165 | 153 |
| 166 void UsageTracker::SetUsageCacheEnabled(QuotaClient::ID client_id, | 154 void UsageTracker::SetUsageCacheEnabled(QuotaClient::ID client_id, |
| 167 const GURL& origin, | 155 const GURL& origin, |
| 168 bool enabled) { | 156 bool enabled) { |
| 169 ClientUsageTracker* client_tracker = GetClientTracker(client_id); | 157 ClientUsageTracker* client_tracker = GetClientTracker(client_id); |
| 170 DCHECK(client_tracker); | 158 DCHECK(client_tracker); |
| 171 | 159 |
| 172 client_tracker->SetUsageCacheEnabled(origin, enabled); | 160 client_tracker->SetUsageCacheEnabled(origin, enabled); |
| 173 } | 161 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 // Defend against confusing inputs from clients. | 205 // Defend against confusing inputs from clients. |
| 218 if (info->usage < 0) | 206 if (info->usage < 0) |
| 219 info->usage = 0; | 207 info->usage = 0; |
| 220 | 208 |
| 221 // All the clients have returned their usage data. Dispatch the | 209 // All the clients have returned their usage data. Dispatch the |
| 222 // pending callbacks. | 210 // pending callbacks. |
| 223 host_usage_callbacks_.Run(host, MakeTuple(info->usage)); | 211 host_usage_callbacks_.Run(host, MakeTuple(info->usage)); |
| 224 } | 212 } |
| 225 | 213 |
| 226 } // namespace storage | 214 } // namespace storage |
| OLD | NEW |