Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / webkit / quota / usage_tracker.h
blobfc734663534722ad98b459fc500075d01d85136b
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef WEBKIT_QUOTA_USAGE_TRACKER_H_
6 #define WEBKIT_QUOTA_USAGE_TRACKER_H_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "googleurl/src/gurl.h"
18 #include "webkit/quota/quota_client.h"
19 #include "webkit/quota/quota_task.h"
20 #include "webkit/quota/quota_types.h"
21 #include "webkit/quota/special_storage_policy.h"
23 namespace quota {
25 class ClientUsageTracker;
27 // A helper class that gathers and tracks the amount of data stored in
28 // all quota clients.
29 // An instance of this class is created per storage type.
30 class UsageTracker : public QuotaTaskObserver {
31 public:
32 UsageTracker(const QuotaClientList& clients, StorageType type,
33 SpecialStoragePolicy* special_storage_policy);
34 virtual ~UsageTracker();
36 StorageType type() const { return type_; }
37 ClientUsageTracker* GetClientTracker(QuotaClient::ID client_id);
39 void GetGlobalUsage(const GlobalUsageCallback& callback);
40 void GetHostUsage(const std::string& host, const UsageCallback& callback);
41 void UpdateUsageCache(QuotaClient::ID client_id,
42 const GURL& origin,
43 int64 delta);
44 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const;
45 void GetCachedOrigins(std::set<GURL>* origins) const;
46 bool IsWorking() const {
47 return global_usage_callbacks_.HasCallbacks() ||
48 host_usage_callbacks_.HasAnyCallbacks();
51 private:
52 struct TrackingInfo {
53 TrackingInfo() : pending_clients(0), usage(0), unlimited_usage(0) {}
54 int pending_clients;
55 int64 usage;
56 int64 unlimited_usage;
59 typedef std::map<QuotaClient::ID, ClientUsageTracker*> ClientTrackerMap;
61 friend class ClientUsageTracker;
62 void DidGetClientGlobalUsage(StorageType type, int64 usage,
63 int64 unlimited_usage);
64 void DidGetClientHostUsage(const std::string& host,
65 StorageType type,
66 int64 usage);
68 const StorageType type_;
69 ClientTrackerMap client_tracker_map_;
70 TrackingInfo global_usage_;
71 std::map<std::string, TrackingInfo> outstanding_host_usage_;
73 GlobalUsageCallbackQueue global_usage_callbacks_;
74 HostUsageCallbackMap host_usage_callbacks_;
76 base::WeakPtrFactory<UsageTracker> weak_factory_;
77 DISALLOW_COPY_AND_ASSIGN(UsageTracker);
80 // This class holds per-client usage tracking information and caches per-host
81 // usage data. An instance of this class is created per client.
82 class ClientUsageTracker : public SpecialStoragePolicy::Observer,
83 public base::NonThreadSafe,
84 public base::SupportsWeakPtr<ClientUsageTracker> {
85 public:
86 ClientUsageTracker(UsageTracker* tracker,
87 QuotaClient* client,
88 StorageType type,
89 SpecialStoragePolicy* special_storage_policy);
90 virtual ~ClientUsageTracker();
92 void GetGlobalUsage(const GlobalUsageCallback& callback);
93 void GetHostUsage(const std::string& host, const UsageCallback& callback);
94 void UpdateUsageCache(const GURL& origin, int64 delta);
95 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const;
96 void GetCachedOrigins(std::set<GURL>* origins) const;
98 private:
99 typedef std::set<std::string> HostSet;
100 typedef std::map<GURL, int64> UsageMap;
101 typedef std::map<std::string, UsageMap> HostUsageMap;
103 class GatherUsageTaskBase;
104 class GatherGlobalUsageTask;
105 class GatherHostUsageTask;
107 // Methods used by our GatherUsage tasks, as a task makes progress
108 // origins and hosts are added incrementally to the cache.
109 void AddCachedOrigin(const GURL& origin, int64 usage);
110 void AddCachedHost(const std::string& host);
111 void GatherGlobalUsageComplete();
112 void GatherHostUsageComplete(const std::string& host);
114 int64 GetCachedHostUsage(const std::string& host) const;
115 int64 GetCachedGlobalUnlimitedUsage();
116 virtual void OnSpecialStoragePolicyChanged() OVERRIDE;
117 void NoopHostUsageCallback(int64 usage);
118 bool IsStorageUnlimited(const GURL& origin) const;
120 UsageTracker* tracker_;
121 QuotaClient* client_;
122 const StorageType type_;
124 int64 global_usage_;
125 int64 global_unlimited_usage_;
126 bool global_usage_retrieved_;
127 bool global_unlimited_usage_is_valid_;
128 HostSet cached_hosts_;
129 HostUsageMap cached_usage_;
131 GatherGlobalUsageTask* global_usage_task_;
132 GlobalUsageCallbackQueue global_usage_callback_;
133 std::map<std::string, GatherHostUsageTask*> host_usage_tasks_;
134 HostUsageCallbackMap host_usage_callbacks_;
136 scoped_refptr<SpecialStoragePolicy> special_storage_policy_;
138 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker);
141 } // namespace quota
143 #endif // WEBKIT_QUOTA_USAGE_TRACKER_H_