OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef WEBKIT_QUOTA_USAGE_TRACKER_H_ | 5 #ifndef WEBKIT_QUOTA_USAGE_TRACKER_H_ |
6 #define WEBKIT_QUOTA_USAGE_TRACKER_H_ | 6 #define WEBKIT_QUOTA_USAGE_TRACKER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 bool IsWorking() const { | 46 bool IsWorking() const { |
47 return global_usage_callbacks_.HasCallbacks() || | 47 return global_usage_callbacks_.HasCallbacks() || |
48 host_usage_callbacks_.HasAnyCallbacks(); | 48 host_usage_callbacks_.HasAnyCallbacks(); |
49 } | 49 } |
50 | 50 |
51 void SetUsageCacheEnabled(QuotaClient::ID client_id, | 51 void SetUsageCacheEnabled(QuotaClient::ID client_id, |
52 const GURL& origin, | 52 const GURL& origin, |
53 bool enabled); | 53 bool enabled); |
54 | 54 |
55 private: | 55 private: |
56 struct TrackingInfo { | 56 struct AccumulateInfo { |
57 TrackingInfo() : pending_clients(0), usage(0), unlimited_usage(0) {} | 57 AccumulateInfo() : pending_clients(0), usage(0), unlimited_usage(0) {} |
58 int pending_clients; | 58 int pending_clients; |
59 int64 usage; | 59 int64 usage; |
60 int64 unlimited_usage; | 60 int64 unlimited_usage; |
61 }; | 61 }; |
62 | 62 |
63 typedef std::map<QuotaClient::ID, ClientUsageTracker*> ClientTrackerMap; | 63 typedef std::map<QuotaClient::ID, ClientUsageTracker*> ClientTrackerMap; |
64 | 64 |
65 friend class ClientUsageTracker; | 65 friend class ClientUsageTracker; |
66 void DidGetClientGlobalUsage(TrackingInfo* info, | 66 void AccumulateClientGlobalUsage(AccumulateInfo* info, |
67 int64 usage, | 67 int64 usage, |
68 int64 unlimited_usage); | 68 int64 unlimited_usage); |
69 void DidGetClientHostUsage(TrackingInfo* info, | 69 void AccumulateClientHostUsage(AccumulateInfo* info, |
70 const std::string& host, | 70 const std::string& host, |
71 int64 usage); | 71 int64 usage); |
72 | 72 |
73 const StorageType type_; | 73 const StorageType type_; |
74 ClientTrackerMap client_tracker_map_; | 74 ClientTrackerMap client_tracker_map_; |
75 | 75 |
76 GlobalUsageCallbackQueue global_usage_callbacks_; | 76 GlobalUsageCallbackQueue global_usage_callbacks_; |
77 HostUsageCallbackMap host_usage_callbacks_; | 77 HostUsageCallbackMap host_usage_callbacks_; |
78 | 78 |
79 base::WeakPtrFactory<UsageTracker> weak_factory_; | 79 base::WeakPtrFactory<UsageTracker> weak_factory_; |
80 DISALLOW_COPY_AND_ASSIGN(UsageTracker); | 80 DISALLOW_COPY_AND_ASSIGN(UsageTracker); |
81 }; | 81 }; |
82 | 82 |
83 // This class holds per-client usage tracking information and caches per-host | 83 // This class holds per-client usage tracking information and caches per-host |
84 // usage data. An instance of this class is created per client. | 84 // usage data. An instance of this class is created per client. |
85 class ClientUsageTracker : public SpecialStoragePolicy::Observer, | 85 class ClientUsageTracker : public SpecialStoragePolicy::Observer, |
86 public base::NonThreadSafe, | 86 public base::NonThreadSafe, |
87 public base::SupportsWeakPtr<ClientUsageTracker> { | 87 public base::SupportsWeakPtr<ClientUsageTracker> { |
88 public: | 88 public: |
| 89 typedef base::Callback<void(int64 cached_usage, |
| 90 int64 non_cached_usage)> HostUsageAccumulator; |
| 91 typedef base::Callback<void(const GURL& origin, |
| 92 int64 usage)> OriginUsageAccumulator; |
| 93 |
89 ClientUsageTracker(UsageTracker* tracker, | 94 ClientUsageTracker(UsageTracker* tracker, |
90 QuotaClient* client, | 95 QuotaClient* client, |
91 StorageType type, | 96 StorageType type, |
92 SpecialStoragePolicy* special_storage_policy); | 97 SpecialStoragePolicy* special_storage_policy); |
93 virtual ~ClientUsageTracker(); | 98 virtual ~ClientUsageTracker(); |
94 | 99 |
95 void GetGlobalUsage(const GlobalUsageCallback& callback); | 100 void GetGlobalUsage(const GlobalUsageCallback& callback); |
96 void GetHostUsage(const std::string& host, const UsageCallback& callback); | 101 void GetHostUsage(const std::string& host, const UsageCallback& callback); |
97 void UpdateUsageCache(const GURL& origin, int64 delta); | 102 void UpdateUsageCache(const GURL& origin, int64 delta); |
98 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const; | 103 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const; |
99 void GetCachedOrigins(std::set<GURL>* origins) const; | 104 void GetCachedOrigins(std::set<GURL>* origins) const; |
100 int64 GetCachedOriginsUsage(const std::set<GURL>& origins, | 105 int64 GetCachedOriginsUsage(const std::set<GURL>& origins, |
101 std::vector<GURL>* origins_not_in_cache); | 106 std::vector<GURL>* origins_not_in_cache); |
102 bool IsUsageCacheEnabledForOrigin(const GURL& origin) const; | 107 bool IsUsageCacheEnabledForOrigin(const GURL& origin) const; |
103 void SetUsageCacheEnabled(const GURL& origin, bool enabled); | 108 void SetUsageCacheEnabled(const GURL& origin, bool enabled); |
104 | 109 |
105 private: | 110 private: |
| 111 typedef CallbackQueueMap<HostUsageAccumulator, std::string, |
| 112 Tuple2<int64, int64> > HostUsageAccumulatorMap; |
| 113 |
106 typedef std::set<std::string> HostSet; | 114 typedef std::set<std::string> HostSet; |
107 typedef std::map<GURL, int64> UsageMap; | 115 typedef std::map<GURL, int64> UsageMap; |
108 typedef std::map<std::string, UsageMap> HostUsageMap; | 116 typedef std::map<std::string, UsageMap> HostUsageMap; |
109 typedef std::map<std::string, std::set<GURL> > OriginSetByHost; | 117 typedef std::map<std::string, std::set<GURL> > OriginSetByHost; |
110 | 118 |
111 class GatherUsageTaskBase; | 119 struct AccumulateInfo { |
112 class GatherGlobalUsageTask; | 120 int pending_jobs; |
113 class GatherHostUsageTask; | 121 int64 cached_usage; |
| 122 int64 non_cached_usage; |
| 123 |
| 124 AccumulateInfo() : pending_jobs(0), cached_usage(0), non_cached_usage(0) {} |
| 125 }; |
| 126 |
| 127 void DidGetOriginsForGlobalUsage(const GlobalUsageCallback& callback, |
| 128 const std::set<GURL>& origins, |
| 129 StorageType type); |
| 130 void AccumulateHostUsage(AccumulateInfo* info, |
| 131 const GlobalUsageCallback& callback, |
| 132 int64 cached_usage, |
| 133 int64 non_cached_usage); |
| 134 |
| 135 void DidGetOriginsForHostUsage(const std::string& host, |
| 136 const std::set<GURL>& origins, |
| 137 StorageType type); |
| 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); |
114 | 145 |
115 // Methods used by our GatherUsage tasks, as a task makes progress | 146 // Methods used by our GatherUsage tasks, as a task makes progress |
116 // origins and hosts are added incrementally to the cache. | 147 // origins and hosts are added incrementally to the cache. |
117 void AddCachedOrigin(const GURL& origin, int64 usage); | 148 void AddCachedOrigin(const GURL& origin, int64 usage); |
118 void AddCachedHost(const std::string& host); | 149 void AddCachedHost(const std::string& host); |
119 void GatherGlobalUsageComplete( | |
120 int64 global_usage, | |
121 int64 non_cached_global_usage, | |
122 const std::map<std::string, int64>& non_cached_usage_by_host); | |
123 void GatherHostUsageComplete(const std::string& host, int64 host_usage); | |
124 | 150 |
125 int64 GetCachedHostUsage(const std::string& host) const; | 151 int64 GetCachedHostUsage(const std::string& host) const; |
126 int64 GetCachedGlobalUnlimitedUsage(); | 152 int64 GetCachedGlobalUnlimitedUsage(); |
127 bool GetCachedOriginUsage(const GURL& origin, int64* usage) const; | 153 bool GetCachedOriginUsage(const GURL& origin, int64* usage) const; |
128 | 154 |
129 // SpecialStoragePolicy::Observer overrides | 155 // SpecialStoragePolicy::Observer overrides |
130 virtual void OnGranted(const GURL& origin, int change_flags) OVERRIDE; | 156 virtual void OnGranted(const GURL& origin, int change_flags) OVERRIDE; |
131 virtual void OnRevoked(const GURL& origin, int change_flags) OVERRIDE; | 157 virtual void OnRevoked(const GURL& origin, int change_flags) OVERRIDE; |
132 virtual void OnCleared() OVERRIDE; | 158 virtual void OnCleared() OVERRIDE; |
133 | 159 |
134 void NoopHostUsageCallback(int64 usage); | |
135 bool IsStorageUnlimited(const GURL& origin) const; | 160 bool IsStorageUnlimited(const GURL& origin) const; |
136 | 161 |
137 UsageTracker* tracker_; | 162 UsageTracker* tracker_; |
138 QuotaClient* client_; | 163 QuotaClient* client_; |
139 const StorageType type_; | 164 const StorageType type_; |
140 | 165 |
141 int64 global_usage_; | 166 int64 global_usage_; |
142 int64 global_unlimited_usage_; | 167 int64 global_unlimited_usage_; |
143 bool global_usage_retrieved_; | 168 bool global_usage_retrieved_; |
144 HostSet cached_hosts_; | 169 HostSet cached_hosts_; |
145 HostUsageMap cached_usage_by_host_; | 170 HostUsageMap cached_usage_by_host_; |
146 | 171 |
147 OriginSetByHost non_cached_origins_by_host_; | 172 OriginSetByHost non_cached_origins_by_host_; |
148 | 173 |
149 GatherGlobalUsageTask* global_usage_task_; | |
150 GlobalUsageCallbackQueue global_usage_callback_; | 174 GlobalUsageCallbackQueue global_usage_callback_; |
151 std::map<std::string, GatherHostUsageTask*> host_usage_tasks_; | 175 HostUsageAccumulatorMap host_usage_accumulators_; |
152 HostUsageCallbackMap host_usage_callbacks_; | |
153 | 176 |
154 scoped_refptr<SpecialStoragePolicy> special_storage_policy_; | 177 scoped_refptr<SpecialStoragePolicy> special_storage_policy_; |
155 | 178 |
156 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker); | 179 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker); |
157 }; | 180 }; |
158 | 181 |
159 } // namespace quota | 182 } // namespace quota |
160 | 183 |
161 #endif // WEBKIT_QUOTA_USAGE_TRACKER_H_ | 184 #endif // WEBKIT_QUOTA_USAGE_TRACKER_H_ |
OLD | NEW |