OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_QUOTA_USAGE_TRACKER_H_ | |
6 #define WEBKIT_QUOTA_USAGE_TRACKER_H_ | |
7 | |
8 #include <list> | |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 | |
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_callbacks.h" | |
19 #include "webkit/quota/quota_client.h" | |
20 #include "webkit/quota/quota_task.h" | |
21 #include "webkit/quota/quota_types.h" | |
22 #include "webkit/quota/special_storage_policy.h" | |
23 | |
24 namespace quota { | |
25 | |
26 class ClientUsageTracker; | |
27 | |
28 // A helper class that gathers and tracks the amount of data stored in | |
29 // all quota clients. | |
30 // An instance of this class is created per storage type. | |
31 class WEBKIT_STORAGE_EXPORT UsageTracker : public QuotaTaskObserver { | |
32 public: | |
33 UsageTracker(const QuotaClientList& clients, StorageType type, | |
34 SpecialStoragePolicy* special_storage_policy); | |
35 virtual ~UsageTracker(); | |
36 | |
37 StorageType type() const { return type_; } | |
38 ClientUsageTracker* GetClientTracker(QuotaClient::ID client_id); | |
39 | |
40 void GetGlobalLimitedUsage(const UsageCallback& callback); | |
41 void GetGlobalUsage(const GlobalUsageCallback& callback); | |
42 void GetHostUsage(const std::string& host, const UsageCallback& callback); | |
43 void UpdateUsageCache(QuotaClient::ID client_id, | |
44 const GURL& origin, | |
45 int64 delta); | |
46 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const; | |
47 void GetCachedOrigins(std::set<GURL>* origins) const; | |
48 bool IsWorking() const { | |
49 return global_usage_callbacks_.HasCallbacks() || | |
50 host_usage_callbacks_.HasAnyCallbacks(); | |
51 } | |
52 | |
53 void SetUsageCacheEnabled(QuotaClient::ID client_id, | |
54 const GURL& origin, | |
55 bool enabled); | |
56 | |
57 private: | |
58 struct AccumulateInfo { | |
59 AccumulateInfo() : pending_clients(0), usage(0), unlimited_usage(0) {} | |
60 int pending_clients; | |
61 int64 usage; | |
62 int64 unlimited_usage; | |
63 }; | |
64 | |
65 typedef std::map<QuotaClient::ID, ClientUsageTracker*> ClientTrackerMap; | |
66 | |
67 friend class ClientUsageTracker; | |
68 void AccumulateClientGlobalUsage(AccumulateInfo* info, | |
69 int64 usage, | |
70 int64 unlimited_usage); | |
71 void AccumulateClientHostUsage(AccumulateInfo* info, | |
72 const std::string& host, | |
73 int64 usage); | |
74 | |
75 const StorageType type_; | |
76 ClientTrackerMap client_tracker_map_; | |
77 | |
78 GlobalUsageCallbackQueue global_usage_callbacks_; | |
79 HostUsageCallbackMap host_usage_callbacks_; | |
80 | |
81 base::WeakPtrFactory<UsageTracker> weak_factory_; | |
82 DISALLOW_COPY_AND_ASSIGN(UsageTracker); | |
83 }; | |
84 | |
85 // This class holds per-client usage tracking information and caches per-host | |
86 // usage data. An instance of this class is created per client. | |
87 class ClientUsageTracker : public SpecialStoragePolicy::Observer, | |
88 public base::NonThreadSafe, | |
89 public base::SupportsWeakPtr<ClientUsageTracker> { | |
90 public: | |
91 typedef base::Callback<void(int64 cached_usage, | |
92 int64 non_cached_usage)> HostUsageAccumulator; | |
93 typedef base::Callback<void(const GURL& origin, | |
94 int64 usage)> OriginUsageAccumulator; | |
95 typedef std::map<std::string, std::set<GURL> > OriginSetByHost; | |
96 | |
97 ClientUsageTracker(UsageTracker* tracker, | |
98 QuotaClient* client, | |
99 StorageType type, | |
100 SpecialStoragePolicy* special_storage_policy); | |
101 virtual ~ClientUsageTracker(); | |
102 | |
103 void GetGlobalUsage(const GlobalUsageCallback& callback); | |
104 void GetHostUsage(const std::string& host, const UsageCallback& callback); | |
105 void UpdateUsageCache(const GURL& origin, int64 delta); | |
106 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const; | |
107 void GetCachedOrigins(std::set<GURL>* origins) const; | |
108 int64 GetCachedOriginsUsage(const std::set<GURL>& origins, | |
109 std::vector<GURL>* origins_not_in_cache); | |
110 bool IsUsageCacheEnabledForOrigin(const GURL& origin) const; | |
111 void SetUsageCacheEnabled(const GURL& origin, bool enabled); | |
112 | |
113 private: | |
114 typedef CallbackQueueMap<HostUsageAccumulator, std::string, | |
115 Tuple2<int64, int64> > HostUsageAccumulatorMap; | |
116 | |
117 typedef std::set<std::string> HostSet; | |
118 typedef std::map<GURL, int64> UsageMap; | |
119 typedef std::map<std::string, UsageMap> HostUsageMap; | |
120 | |
121 struct AccumulateInfo { | |
122 int pending_jobs; | |
123 int64 cached_usage; | |
124 int64 non_cached_usage; | |
125 | |
126 AccumulateInfo() : pending_jobs(0), cached_usage(0), non_cached_usage(0) {} | |
127 }; | |
128 | |
129 void DidGetOriginsForGlobalUsage(const GlobalUsageCallback& callback, | |
130 const std::set<GURL>& origins); | |
131 void AccumulateHostUsage(AccumulateInfo* info, | |
132 const GlobalUsageCallback& callback, | |
133 int64 cached_usage, | |
134 int64 non_cached_usage); | |
135 | |
136 void DidGetOriginsForHostUsage(const std::string& host, | |
137 const std::set<GURL>& origins); | |
138 | |
139 void GetUsageForOrigins(const std::string& host, | |
140 const std::set<GURL>& origins); | |
141 void AccumulateOriginUsage(AccumulateInfo* info, | |
142 const std::string& host, | |
143 const GURL& origin, | |
144 int64 usage); | |
145 | |
146 // Methods used by our GatherUsage tasks, as a task makes progress | |
147 // origins and hosts are added incrementally to the cache. | |
148 void AddCachedOrigin(const GURL& origin, int64 usage); | |
149 void AddCachedHost(const std::string& host); | |
150 | |
151 int64 GetCachedHostUsage(const std::string& host) const; | |
152 int64 GetCachedGlobalUnlimitedUsage(); | |
153 bool GetCachedOriginUsage(const GURL& origin, int64* usage) const; | |
154 | |
155 // SpecialStoragePolicy::Observer overrides | |
156 virtual void OnGranted(const GURL& origin, int change_flags) OVERRIDE; | |
157 virtual void OnRevoked(const GURL& origin, int change_flags) OVERRIDE; | |
158 virtual void OnCleared() OVERRIDE; | |
159 | |
160 bool IsStorageUnlimited(const GURL& origin) const; | |
161 | |
162 UsageTracker* tracker_; | |
163 QuotaClient* client_; | |
164 const StorageType type_; | |
165 | |
166 int64 global_limited_usage_; | |
167 int64 global_unlimited_usage_; | |
168 bool global_usage_retrieved_; | |
169 HostSet cached_hosts_; | |
170 HostUsageMap cached_usage_by_host_; | |
171 | |
172 OriginSetByHost non_cached_limited_origins_by_host_; | |
173 OriginSetByHost non_cached_unlimited_origins_by_host_; | |
174 | |
175 GlobalUsageCallbackQueue global_usage_callback_; | |
176 HostUsageAccumulatorMap host_usage_accumulators_; | |
177 | |
178 scoped_refptr<SpecialStoragePolicy> special_storage_policy_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker); | |
181 }; | |
182 | |
183 } // namespace quota | |
184 | |
185 #endif // WEBKIT_QUOTA_USAGE_TRACKER_H_ | |
OLD | NEW |