| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/quota/usage_tracker.h" | 5 #include "webkit/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> |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) { | 247 ClientUsageTracker* UsageTracker::GetClientTracker(QuotaClient::ID client_id) { |
| 248 ClientTrackerMap::iterator found = client_tracker_map_.find(client_id); | 248 ClientTrackerMap::iterator found = client_tracker_map_.find(client_id); |
| 249 if (found != client_tracker_map_.end()) | 249 if (found != client_tracker_map_.end()) |
| 250 return found->second; | 250 return found->second; |
| 251 return NULL; | 251 return NULL; |
| 252 } | 252 } |
| 253 | 253 |
| 254 void UsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) { | 254 void UsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) { |
| 255 if (client_tracker_map_.size() == 0) { | 255 if (client_tracker_map_.size() == 0) { |
| 256 // No clients registered. | 256 // No clients registered. |
| 257 callback.Run(type_, 0, 0); | 257 callback.Run(0, 0); |
| 258 return; | 258 return; |
| 259 } | 259 } |
| 260 if (global_usage_callbacks_.Add(callback)) { | 260 if (global_usage_callbacks_.Add(callback)) { |
| 261 // This is the first call. Asks each ClientUsageTracker to collect | 261 // This is the first call. Asks each ClientUsageTracker to collect |
| 262 // usage information. | 262 // usage information. |
| 263 global_usage_.pending_clients = client_tracker_map_.size(); | 263 global_usage_.pending_clients = client_tracker_map_.size(); |
| 264 global_usage_.usage = 0; | 264 global_usage_.usage = 0; |
| 265 global_usage_.unlimited_usage = 0; | 265 global_usage_.unlimited_usage = 0; |
| 266 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); | 266 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); |
| 267 iter != client_tracker_map_.end(); | 267 iter != client_tracker_map_.end(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 282 } | 282 } |
| 283 if (host_usage_callbacks_.Add(host, callback)) { | 283 if (host_usage_callbacks_.Add(host, callback)) { |
| 284 // This is the first call for the given host. | 284 // This is the first call for the given host. |
| 285 DCHECK(outstanding_host_usage_.find(host) == outstanding_host_usage_.end()); | 285 DCHECK(outstanding_host_usage_.find(host) == outstanding_host_usage_.end()); |
| 286 outstanding_host_usage_[host].pending_clients = client_tracker_map_.size(); | 286 outstanding_host_usage_[host].pending_clients = client_tracker_map_.size(); |
| 287 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); | 287 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); |
| 288 iter != client_tracker_map_.end(); | 288 iter != client_tracker_map_.end(); |
| 289 ++iter) { | 289 ++iter) { |
| 290 iter->second->GetHostUsage(host, | 290 iter->second->GetHostUsage(host, |
| 291 base::Bind(&UsageTracker::DidGetClientHostUsage, | 291 base::Bind(&UsageTracker::DidGetClientHostUsage, |
| 292 weak_factory_.GetWeakPtr(), host, type_)); | 292 weak_factory_.GetWeakPtr(), host)); |
| 293 } | 293 } |
| 294 } | 294 } |
| 295 } | 295 } |
| 296 | 296 |
| 297 void UsageTracker::UpdateUsageCache( | 297 void UsageTracker::UpdateUsageCache( |
| 298 QuotaClient::ID client_id, const GURL& origin, int64 delta) { | 298 QuotaClient::ID client_id, const GURL& origin, int64 delta) { |
| 299 ClientUsageTracker* client_tracker = GetClientTracker(client_id); | 299 ClientUsageTracker* client_tracker = GetClientTracker(client_id); |
| 300 DCHECK(client_tracker); | 300 DCHECK(client_tracker); |
| 301 client_tracker->UpdateUsageCache(origin, delta); | 301 client_tracker->UpdateUsageCache(origin, delta); |
| 302 } | 302 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 322 | 322 |
| 323 void UsageTracker::SetUsageCacheEnabled(QuotaClient::ID client_id, | 323 void UsageTracker::SetUsageCacheEnabled(QuotaClient::ID client_id, |
| 324 const GURL& origin, | 324 const GURL& origin, |
| 325 bool enabled) { | 325 bool enabled) { |
| 326 ClientUsageTracker* client_tracker = GetClientTracker(client_id); | 326 ClientUsageTracker* client_tracker = GetClientTracker(client_id); |
| 327 DCHECK(client_tracker); | 327 DCHECK(client_tracker); |
| 328 | 328 |
| 329 client_tracker->SetUsageCacheEnabled(origin, enabled); | 329 client_tracker->SetUsageCacheEnabled(origin, enabled); |
| 330 } | 330 } |
| 331 | 331 |
| 332 void UsageTracker::DidGetClientGlobalUsage(StorageType type, | 332 void UsageTracker::DidGetClientGlobalUsage(int64 usage, |
| 333 int64 usage, | |
| 334 int64 unlimited_usage) { | 333 int64 unlimited_usage) { |
| 335 DCHECK_EQ(type, type_); | |
| 336 global_usage_.usage += usage; | 334 global_usage_.usage += usage; |
| 337 global_usage_.unlimited_usage += unlimited_usage; | 335 global_usage_.unlimited_usage += unlimited_usage; |
| 338 if (--global_usage_.pending_clients == 0) { | 336 if (--global_usage_.pending_clients == 0) { |
| 339 // Defend against confusing inputs from clients. | 337 // Defend against confusing inputs from clients. |
| 340 if (global_usage_.usage < 0) | 338 if (global_usage_.usage < 0) |
| 341 global_usage_.usage = 0; | 339 global_usage_.usage = 0; |
| 342 // TODO(michaeln): The unlimited number is not trustworthy, it | 340 // TODO(michaeln): The unlimited number is not trustworthy, it |
| 343 // can get out of whack when apps are installed or uninstalled. | 341 // can get out of whack when apps are installed or uninstalled. |
| 344 if (global_usage_.unlimited_usage > global_usage_.usage) | 342 if (global_usage_.unlimited_usage > global_usage_.usage) |
| 345 global_usage_.unlimited_usage = global_usage_.usage; | 343 global_usage_.unlimited_usage = global_usage_.usage; |
| 346 else if (global_usage_.unlimited_usage < 0) | 344 else if (global_usage_.unlimited_usage < 0) |
| 347 global_usage_.unlimited_usage = 0; | 345 global_usage_.unlimited_usage = 0; |
| 348 | 346 |
| 349 // All the clients have returned their usage data. Dispatches the | 347 // All the clients have returned their usage data. Dispatches the |
| 350 // pending callbacks. | 348 // pending callbacks. |
| 351 global_usage_callbacks_.Run( | 349 global_usage_callbacks_.Run( |
| 352 MakeTuple(type, global_usage_.usage, | 350 MakeTuple(global_usage_.usage, global_usage_.unlimited_usage)); |
| 353 global_usage_.unlimited_usage)); | |
| 354 } | 351 } |
| 355 } | 352 } |
| 356 | 353 |
| 357 void UsageTracker::DidGetClientHostUsage(const std::string& host, | 354 void UsageTracker::DidGetClientHostUsage(const std::string& host, |
| 358 StorageType type, | |
| 359 int64 usage) { | 355 int64 usage) { |
| 360 DCHECK_EQ(type, type_); | |
| 361 TrackingInfo& info = outstanding_host_usage_[host]; | 356 TrackingInfo& info = outstanding_host_usage_[host]; |
| 362 info.usage += usage; | 357 info.usage += usage; |
| 363 if (--info.pending_clients == 0) { | 358 if (--info.pending_clients == 0) { |
| 364 // Defend against confusing inputs from clients. | 359 // Defend against confusing inputs from clients. |
| 365 if (info.usage < 0) | 360 if (info.usage < 0) |
| 366 info.usage = 0; | 361 info.usage = 0; |
| 367 // All the clients have returned their usage data. Dispatches the | 362 // All the clients have returned their usage data. Dispatches the |
| 368 // pending callbacks. | 363 // pending callbacks. |
| 369 host_usage_callbacks_.Run(host, MakeTuple(info.usage)); | 364 host_usage_callbacks_.Run(host, MakeTuple(info.usage)); |
| 370 outstanding_host_usage_.erase(host); | 365 outstanding_host_usage_.erase(host); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 390 special_storage_policy_->AddObserver(this); | 385 special_storage_policy_->AddObserver(this); |
| 391 } | 386 } |
| 392 | 387 |
| 393 ClientUsageTracker::~ClientUsageTracker() { | 388 ClientUsageTracker::~ClientUsageTracker() { |
| 394 if (special_storage_policy_) | 389 if (special_storage_policy_) |
| 395 special_storage_policy_->RemoveObserver(this); | 390 special_storage_policy_->RemoveObserver(this); |
| 396 } | 391 } |
| 397 | 392 |
| 398 void ClientUsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) { | 393 void ClientUsageTracker::GetGlobalUsage(const GlobalUsageCallback& callback) { |
| 399 if (global_usage_retrieved_ && non_cached_origins_by_host_.empty()) { | 394 if (global_usage_retrieved_ && non_cached_origins_by_host_.empty()) { |
| 400 callback.Run(type_, global_usage_, GetCachedGlobalUnlimitedUsage()); | 395 callback.Run(global_usage_, GetCachedGlobalUnlimitedUsage()); |
| 401 return; | 396 return; |
| 402 } | 397 } |
| 403 DCHECK(!global_usage_callback_.HasCallbacks()); | 398 DCHECK(!global_usage_callback_.HasCallbacks()); |
| 404 global_usage_callback_.Add(callback); | 399 global_usage_callback_.Add(callback); |
| 405 global_usage_task_ = new GatherGlobalUsageTask(tracker_, client_); | 400 global_usage_task_ = new GatherGlobalUsageTask(tracker_, client_); |
| 406 global_usage_task_->Start(); | 401 global_usage_task_->Start(); |
| 407 } | 402 } |
| 408 | 403 |
| 409 void ClientUsageTracker::GetHostUsage( | 404 void ClientUsageTracker::GetHostUsage( |
| 410 const std::string& host, const UsageCallback& callback) { | 405 const std::string& host, const UsageCallback& callback) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 int64 global_usage, | 543 int64 global_usage, |
| 549 int64 non_cached_global_usage, | 544 int64 non_cached_global_usage, |
| 550 const std::map<std::string, int64>& non_cached_host_usage) { | 545 const std::map<std::string, int64>& non_cached_host_usage) { |
| 551 DCHECK(global_usage_task_ != NULL); | 546 DCHECK(global_usage_task_ != NULL); |
| 552 global_usage_task_ = NULL; | 547 global_usage_task_ = NULL; |
| 553 // TODO(kinuko): Record when it has retrieved the global usage. | 548 // TODO(kinuko): Record when it has retrieved the global usage. |
| 554 global_usage_retrieved_ = true; | 549 global_usage_retrieved_ = true; |
| 555 | 550 |
| 556 DCHECK(global_usage_callback_.HasCallbacks()); | 551 DCHECK(global_usage_callback_.HasCallbacks()); |
| 557 global_usage_callback_.Run( | 552 global_usage_callback_.Run( |
| 558 MakeTuple(type_, global_usage, | 553 MakeTuple(global_usage, |
| 559 GetCachedGlobalUnlimitedUsage() + non_cached_global_usage)); | 554 GetCachedGlobalUnlimitedUsage() + non_cached_global_usage)); |
| 560 | 555 |
| 561 for (HostUsageCallbackMap::iterator iter = host_usage_callbacks_.Begin(); | 556 for (HostUsageCallbackMap::iterator iter = host_usage_callbacks_.Begin(); |
| 562 iter != host_usage_callbacks_.End(); ++iter) { | 557 iter != host_usage_callbacks_.End(); ++iter) { |
| 563 int64 host_usage = GetCachedHostUsage(iter->first); | 558 int64 host_usage = GetCachedHostUsage(iter->first); |
| 564 std::map<std::string, int64>::const_iterator found = | 559 std::map<std::string, int64>::const_iterator found = |
| 565 non_cached_host_usage.find(iter->first); | 560 non_cached_host_usage.find(iter->first); |
| 566 if (found != non_cached_host_usage.end()) | 561 if (found != non_cached_host_usage.end()) |
| 567 host_usage += found->second; | 562 host_usage += found->second; |
| 568 iter->second.Run(MakeTuple(host_usage)); | 563 iter->second.Run(MakeTuple(host_usage)); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 void ClientUsageTracker::NoopHostUsageCallback(int64 usage) {} | 643 void ClientUsageTracker::NoopHostUsageCallback(int64 usage) {} |
| 649 | 644 |
| 650 bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const { | 645 bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const { |
| 651 if (type_ == kStorageTypeSyncable) | 646 if (type_ == kStorageTypeSyncable) |
| 652 return false; | 647 return false; |
| 653 return special_storage_policy_.get() && | 648 return special_storage_policy_.get() && |
| 654 special_storage_policy_->IsStorageUnlimited(origin); | 649 special_storage_policy_->IsStorageUnlimited(origin); |
| 655 } | 650 } |
| 656 | 651 |
| 657 } // namespace quota | 652 } // namespace quota |
| OLD | NEW |