| 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 StorageType type); | |
| 132 void AccumulateHostUsage(AccumulateInfo* info, | |
| 133 const GlobalUsageCallback& callback, | |
| 134 int64 cached_usage, | |
| 135 int64 non_cached_usage); | |
| 136 | |
| 137 void DidGetOriginsForHostUsage(const std::string& host, | |
| 138 const std::set<GURL>& origins, | |
| 139 StorageType type); | |
| 140 | |
| 141 void GetUsageForOrigins(const std::string& host, | |
| 142 const std::set<GURL>& origins); | |
| 143 void AccumulateOriginUsage(AccumulateInfo* info, | |
| 144 const std::string& host, | |
| 145 const GURL& origin, | |
| 146 int64 usage); | |
| 147 | |
| 148 // Methods used by our GatherUsage tasks, as a task makes progress | |
| 149 // origins and hosts are added incrementally to the cache. | |
| 150 void AddCachedOrigin(const GURL& origin, int64 usage); | |
| 151 void AddCachedHost(const std::string& host); | |
| 152 | |
| 153 int64 GetCachedHostUsage(const std::string& host) const; | |
| 154 int64 GetCachedGlobalUnlimitedUsage(); | |
| 155 bool GetCachedOriginUsage(const GURL& origin, int64* usage) const; | |
| 156 | |
| 157 // SpecialStoragePolicy::Observer overrides | |
| 158 virtual void OnGranted(const GURL& origin, int change_flags) OVERRIDE; | |
| 159 virtual void OnRevoked(const GURL& origin, int change_flags) OVERRIDE; | |
| 160 virtual void OnCleared() OVERRIDE; | |
| 161 | |
| 162 bool IsStorageUnlimited(const GURL& origin) const; | |
| 163 | |
| 164 UsageTracker* tracker_; | |
| 165 QuotaClient* client_; | |
| 166 const StorageType type_; | |
| 167 | |
| 168 int64 global_limited_usage_; | |
| 169 int64 global_unlimited_usage_; | |
| 170 bool global_usage_retrieved_; | |
| 171 HostSet cached_hosts_; | |
| 172 HostUsageMap cached_usage_by_host_; | |
| 173 | |
| 174 OriginSetByHost non_cached_limited_origins_by_host_; | |
| 175 OriginSetByHost non_cached_unlimited_origins_by_host_; | |
| 176 | |
| 177 GlobalUsageCallbackQueue global_usage_callback_; | |
| 178 HostUsageAccumulatorMap host_usage_accumulators_; | |
| 179 | |
| 180 scoped_refptr<SpecialStoragePolicy> special_storage_policy_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker); | |
| 183 }; | |
| 184 | |
| 185 } // namespace quota | |
| 186 | |
| 187 #endif // WEBKIT_QUOTA_USAGE_TRACKER_H_ | |
| OLD | NEW |