Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / webkit / quota / usage_tracker.h
blob185527248eafa669951fcb167e85d8f4f52a719c
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 WEBKIT_STORAGE_EXPORT 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 void SetUsageCacheEnabled(QuotaClient::ID client_id,
52 const GURL& origin,
53 bool enabled);
55 private:
56 struct TrackingInfo {
57 TrackingInfo() : pending_clients(0), usage(0), unlimited_usage(0) {}
58 int pending_clients;
59 int64 usage;
60 int64 unlimited_usage;
63 typedef std::map<QuotaClient::ID, ClientUsageTracker*> ClientTrackerMap;
65 friend class ClientUsageTracker;
66 void DidGetClientGlobalUsage(TrackingInfo* info,
67 int64 usage,
68 int64 unlimited_usage);
69 void DidGetClientHostUsage(TrackingInfo* info,
70 const std::string& host,
71 int64 usage);
73 const StorageType type_;
74 ClientTrackerMap client_tracker_map_;
76 GlobalUsageCallbackQueue global_usage_callbacks_;
77 HostUsageCallbackMap host_usage_callbacks_;
79 base::WeakPtrFactory<UsageTracker> weak_factory_;
80 DISALLOW_COPY_AND_ASSIGN(UsageTracker);
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.
85 class ClientUsageTracker : public SpecialStoragePolicy::Observer,
86 public base::NonThreadSafe,
87 public base::SupportsWeakPtr<ClientUsageTracker> {
88 public:
89 ClientUsageTracker(UsageTracker* tracker,
90 QuotaClient* client,
91 StorageType type,
92 SpecialStoragePolicy* special_storage_policy);
93 virtual ~ClientUsageTracker();
95 void GetGlobalUsage(const GlobalUsageCallback& callback);
96 void GetHostUsage(const std::string& host, const UsageCallback& callback);
97 void UpdateUsageCache(const GURL& origin, int64 delta);
98 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const;
99 void GetCachedOrigins(std::set<GURL>* origins) const;
100 int64 GetCachedOriginsUsage(const std::set<GURL>& origins,
101 std::vector<GURL>* origins_not_in_cache);
102 bool IsUsageCacheEnabledForOrigin(const GURL& origin) const;
103 void SetUsageCacheEnabled(const GURL& origin, bool enabled);
105 private:
106 typedef std::set<std::string> HostSet;
107 typedef std::map<GURL, int64> UsageMap;
108 typedef std::map<std::string, UsageMap> HostUsageMap;
109 typedef std::map<std::string, std::set<GURL> > OriginSetByHost;
111 class GatherUsageTaskBase;
112 class GatherGlobalUsageTask;
113 class GatherHostUsageTask;
115 // Methods used by our GatherUsage tasks, as a task makes progress
116 // origins and hosts are added incrementally to the cache.
117 void AddCachedOrigin(const GURL& origin, int64 usage);
118 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);
125 int64 GetCachedHostUsage(const std::string& host) const;
126 int64 GetCachedGlobalUnlimitedUsage();
127 bool GetCachedOriginUsage(const GURL& origin, int64* usage) const;
129 // SpecialStoragePolicy::Observer overrides
130 virtual void OnGranted(const GURL& origin, int change_flags) OVERRIDE;
131 virtual void OnRevoked(const GURL& origin, int change_flags) OVERRIDE;
132 virtual void OnCleared() OVERRIDE;
134 void NoopHostUsageCallback(int64 usage);
135 bool IsStorageUnlimited(const GURL& origin) const;
137 UsageTracker* tracker_;
138 QuotaClient* client_;
139 const StorageType type_;
141 int64 global_usage_;
142 int64 global_unlimited_usage_;
143 bool global_usage_retrieved_;
144 HostSet cached_hosts_;
145 HostUsageMap cached_usage_by_host_;
147 OriginSetByHost non_cached_origins_by_host_;
149 GatherGlobalUsageTask* global_usage_task_;
150 GlobalUsageCallbackQueue global_usage_callback_;
151 std::map<std::string, GatherHostUsageTask*> host_usage_tasks_;
152 HostUsageCallbackMap host_usage_callbacks_;
154 scoped_refptr<SpecialStoragePolicy> special_storage_policy_;
156 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker);
159 } // namespace quota
161 #endif // WEBKIT_QUOTA_USAGE_TRACKER_H_