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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 iter != client_tracker_map_.end(); | 250 iter != client_tracker_map_.end(); |
251 ++iter) { | 251 ++iter) { |
252 iter->second->GetGlobalUsage( | 252 iter->second->GetGlobalUsage( |
253 base::Bind(&UsageTracker::DidGetClientGlobalUsage, | 253 base::Bind(&UsageTracker::DidGetClientGlobalUsage, |
254 weak_factory_.GetWeakPtr())); | 254 weak_factory_.GetWeakPtr())); |
255 } | 255 } |
256 } | 256 } |
257 } | 257 } |
258 | 258 |
259 void UsageTracker::GetHostUsage( | 259 void UsageTracker::GetHostUsage( |
260 const std::string& host, const HostUsageCallback& callback) { | 260 const std::string& host, const UsageCallback& callback) { |
261 if (client_tracker_map_.size() == 0) { | 261 if (client_tracker_map_.size() == 0) { |
262 // No clients registered. | 262 // No clients registered. |
263 callback.Run(host, type_, 0); | 263 callback.Run(0); |
264 return; | 264 return; |
265 } | 265 } |
266 if (host_usage_callbacks_.Add(host, callback)) { | 266 if (host_usage_callbacks_.Add(host, callback)) { |
267 // This is the first call for the given host. | 267 // This is the first call for the given host. |
268 DCHECK(outstanding_host_usage_.find(host) == outstanding_host_usage_.end()); | 268 DCHECK(outstanding_host_usage_.find(host) == outstanding_host_usage_.end()); |
269 outstanding_host_usage_[host].pending_clients = client_tracker_map_.size(); | 269 outstanding_host_usage_[host].pending_clients = client_tracker_map_.size(); |
270 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); | 270 for (ClientTrackerMap::iterator iter = client_tracker_map_.begin(); |
271 iter != client_tracker_map_.end(); | 271 iter != client_tracker_map_.end(); |
272 ++iter) { | 272 ++iter) { |
273 iter->second->GetHostUsage(host, | 273 iter->second->GetHostUsage(host, |
274 base::Bind(&UsageTracker::DidGetClientHostUsage, | 274 base::Bind(&UsageTracker::DidGetClientHostUsage, |
275 weak_factory_.GetWeakPtr())); | 275 weak_factory_.GetWeakPtr(), host, type_)); |
276 } | 276 } |
277 } | 277 } |
278 } | 278 } |
279 | 279 |
280 void UsageTracker::UpdateUsageCache( | 280 void UsageTracker::UpdateUsageCache( |
281 QuotaClient::ID client_id, const GURL& origin, int64 delta) { | 281 QuotaClient::ID client_id, const GURL& origin, int64 delta) { |
282 ClientUsageTracker* client_tracker = GetClientTracker(client_id); | 282 ClientUsageTracker* client_tracker = GetClientTracker(client_id); |
283 DCHECK(client_tracker); | 283 DCHECK(client_tracker); |
284 client_tracker->UpdateUsageCache(origin, delta); | 284 client_tracker->UpdateUsageCache(origin, delta); |
285 } | 285 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 int64 usage) { | 332 int64 usage) { |
333 DCHECK_EQ(type, type_); | 333 DCHECK_EQ(type, type_); |
334 TrackingInfo& info = outstanding_host_usage_[host]; | 334 TrackingInfo& info = outstanding_host_usage_[host]; |
335 info.usage += usage; | 335 info.usage += usage; |
336 if (--info.pending_clients == 0) { | 336 if (--info.pending_clients == 0) { |
337 // Defend against confusing inputs from clients. | 337 // Defend against confusing inputs from clients. |
338 if (info.usage < 0) | 338 if (info.usage < 0) |
339 info.usage = 0; | 339 info.usage = 0; |
340 // All the clients have returned their usage data. Dispatches the | 340 // All the clients have returned their usage data. Dispatches the |
341 // pending callbacks. | 341 // pending callbacks. |
342 host_usage_callbacks_.Run(host, host, type, info.usage); | 342 host_usage_callbacks_.Run(host, info.usage); |
343 outstanding_host_usage_.erase(host); | 343 outstanding_host_usage_.erase(host); |
344 } | 344 } |
345 } | 345 } |
346 | 346 |
347 // ClientUsageTracker ---------------------------------------------------- | 347 // ClientUsageTracker ---------------------------------------------------- |
348 | 348 |
349 ClientUsageTracker::ClientUsageTracker( | 349 ClientUsageTracker::ClientUsageTracker( |
350 UsageTracker* tracker, QuotaClient* client, StorageType type, | 350 UsageTracker* tracker, QuotaClient* client, StorageType type, |
351 SpecialStoragePolicy* special_storage_policy) | 351 SpecialStoragePolicy* special_storage_policy) |
352 : tracker_(tracker), | 352 : tracker_(tracker), |
(...skipping 21 matching lines...) Expand all Loading... |
374 callback.Run(type_, global_usage_, GetCachedGlobalUnlimitedUsage()); | 374 callback.Run(type_, global_usage_, GetCachedGlobalUnlimitedUsage()); |
375 return; | 375 return; |
376 } | 376 } |
377 DCHECK(!global_usage_callback_.HasCallbacks()); | 377 DCHECK(!global_usage_callback_.HasCallbacks()); |
378 global_usage_callback_.Add(callback); | 378 global_usage_callback_.Add(callback); |
379 global_usage_task_ = new GatherGlobalUsageTask(tracker_, client_); | 379 global_usage_task_ = new GatherGlobalUsageTask(tracker_, client_); |
380 global_usage_task_->Start(); | 380 global_usage_task_->Start(); |
381 } | 381 } |
382 | 382 |
383 void ClientUsageTracker::GetHostUsage( | 383 void ClientUsageTracker::GetHostUsage( |
384 const std::string& host, const HostUsageCallback& callback) { | 384 const std::string& host, const UsageCallback& callback) { |
385 HostSet::const_iterator found = cached_hosts_.find(host); | 385 HostSet::const_iterator found = cached_hosts_.find(host); |
386 if (found != cached_hosts_.end()) { | 386 if (found != cached_hosts_.end()) { |
387 // TODO(kinuko): Drop host_usage_map_ cache periodically. | 387 // TODO(kinuko): Drop host_usage_map_ cache periodically. |
388 callback.Run(host, type_, GetCachedHostUsage(host)); | 388 callback.Run(GetCachedHostUsage(host)); |
389 return; | 389 return; |
390 } | 390 } |
391 if (!host_usage_callbacks_.Add(host, callback) || global_usage_task_) | 391 if (!host_usage_callbacks_.Add(host, callback) || global_usage_task_) |
392 return; | 392 return; |
393 GatherHostUsageTask* task = new GatherHostUsageTask(tracker_, client_, host); | 393 GatherHostUsageTask* task = new GatherHostUsageTask(tracker_, client_, host); |
394 host_usage_tasks_[host] = task; | 394 host_usage_tasks_[host] = task; |
395 task->Start(); | 395 task->Start(); |
396 } | 396 } |
397 | 397 |
398 void ClientUsageTracker::UpdateUsageCache( | 398 void ClientUsageTracker::UpdateUsageCache( |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 global_usage_task_ = NULL; | 462 global_usage_task_ = NULL; |
463 // TODO(kinuko): Record when it has retrieved the global usage. | 463 // TODO(kinuko): Record when it has retrieved the global usage. |
464 global_usage_retrieved_ = true; | 464 global_usage_retrieved_ = true; |
465 | 465 |
466 DCHECK(global_usage_callback_.HasCallbacks()); | 466 DCHECK(global_usage_callback_.HasCallbacks()); |
467 global_usage_callback_.Run(type_, global_usage_, | 467 global_usage_callback_.Run(type_, global_usage_, |
468 GetCachedGlobalUnlimitedUsage()); | 468 GetCachedGlobalUnlimitedUsage()); |
469 | 469 |
470 for (HostUsageCallbackMap::iterator iter = host_usage_callbacks_.Begin(); | 470 for (HostUsageCallbackMap::iterator iter = host_usage_callbacks_.Begin(); |
471 iter != host_usage_callbacks_.End(); ++iter) { | 471 iter != host_usage_callbacks_.End(); ++iter) { |
472 iter->second.Run(iter->first, type_, GetCachedHostUsage(iter->first)); | 472 iter->second.Run(GetCachedHostUsage(iter->first)); |
473 } | 473 } |
474 host_usage_callbacks_.Clear(); | 474 host_usage_callbacks_.Clear(); |
475 } | 475 } |
476 | 476 |
477 void ClientUsageTracker::GatherHostUsageComplete(const std::string& host) { | 477 void ClientUsageTracker::GatherHostUsageComplete(const std::string& host) { |
478 DCHECK(host_usage_tasks_.find(host) != host_usage_tasks_.end()); | 478 DCHECK(host_usage_tasks_.find(host) != host_usage_tasks_.end()); |
479 host_usage_tasks_.erase(host); | 479 host_usage_tasks_.erase(host); |
480 host_usage_callbacks_.Run(host, host, type_, GetCachedHostUsage(host)); | 480 host_usage_callbacks_.Run(host, GetCachedHostUsage(host)); |
481 } | 481 } |
482 | 482 |
483 int64 ClientUsageTracker::GetCachedHostUsage(const std::string& host) const { | 483 int64 ClientUsageTracker::GetCachedHostUsage(const std::string& host) const { |
484 HostUsageMap::const_iterator found = cached_usage_.find(host); | 484 HostUsageMap::const_iterator found = cached_usage_.find(host); |
485 if (found == cached_usage_.end()) | 485 if (found == cached_usage_.end()) |
486 return 0; | 486 return 0; |
487 | 487 |
488 int64 usage = 0; | 488 int64 usage = 0; |
489 const UsageMap& map = found->second; | 489 const UsageMap& map = found->second; |
490 for (UsageMap::const_iterator iter = map.begin(); | 490 for (UsageMap::const_iterator iter = map.begin(); |
(...skipping 18 matching lines...) Expand all Loading... |
509 global_unlimited_usage_is_valid_ = true; | 509 global_unlimited_usage_is_valid_ = true; |
510 } | 510 } |
511 return global_unlimited_usage_; | 511 return global_unlimited_usage_; |
512 } | 512 } |
513 | 513 |
514 void ClientUsageTracker::OnSpecialStoragePolicyChanged() { | 514 void ClientUsageTracker::OnSpecialStoragePolicyChanged() { |
515 DCHECK(CalledOnValidThread()); | 515 DCHECK(CalledOnValidThread()); |
516 global_unlimited_usage_is_valid_ = false; | 516 global_unlimited_usage_is_valid_ = false; |
517 } | 517 } |
518 | 518 |
519 void ClientUsageTracker::NoopHostUsageCallback( | 519 void ClientUsageTracker::NoopHostUsageCallback(int64 usage) {} |
520 const std::string& host, StorageType type, int64 usage) { | |
521 } | |
522 | 520 |
523 bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const { | 521 bool ClientUsageTracker::IsStorageUnlimited(const GURL& origin) const { |
524 if (type_ == kStorageTypeSyncable) | 522 if (type_ == kStorageTypeSyncable) |
525 return false; | 523 return false; |
526 return special_storage_policy_.get() && | 524 return special_storage_policy_.get() && |
527 special_storage_policy_->IsStorageUnlimited(origin); | 525 special_storage_policy_->IsStorageUnlimited(origin); |
528 } | 526 } |
529 | 527 |
530 } // namespace quota | 528 } // namespace quota |
OLD | NEW |