Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / storage / browser / quota / usage_tracker.h
blob224faf83acaac5db10507eb2ea0bb0fa30056dcb
1 // Copyright 2013 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 STORAGE_BROWSER_QUOTA_USAGE_TRACKER_H_
6 #define STORAGE_BROWSER_QUOTA_USAGE_TRACKER_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "storage/browser/quota/quota_callbacks.h"
17 #include "storage/browser/quota/quota_client.h"
18 #include "storage/browser/quota/quota_task.h"
19 #include "storage/browser/quota/special_storage_policy.h"
20 #include "storage/browser/storage_browser_export.h"
21 #include "storage/common/quota/quota_types.h"
22 #include "url/gurl.h"
24 namespace storage {
26 class ClientUsageTracker;
27 class StorageMonitor;
29 // A helper class that gathers and tracks the amount of data stored in
30 // all quota clients.
31 // An instance of this class is created per storage type.
32 class STORAGE_EXPORT UsageTracker : public QuotaTaskObserver {
33 public:
34 UsageTracker(const QuotaClientList& clients, StorageType type,
35 SpecialStoragePolicy* special_storage_policy,
36 StorageMonitor* storage_monitor);
37 ~UsageTracker() override;
39 StorageType type() const { return type_; }
40 ClientUsageTracker* GetClientTracker(QuotaClient::ID client_id);
42 void GetGlobalLimitedUsage(const UsageCallback& callback);
43 void GetGlobalUsage(const GlobalUsageCallback& callback);
44 void GetHostUsage(const std::string& host, const UsageCallback& callback);
45 void UpdateUsageCache(QuotaClient::ID client_id,
46 const GURL& origin,
47 int64 delta);
48 void GetCachedHostsUsage(std::map<std::string, int64>* host_usage) const;
49 void GetCachedOrigins(std::set<GURL>* origins) const;
50 bool IsWorking() const {
51 return global_usage_callbacks_.HasCallbacks() ||
52 host_usage_callbacks_.HasAnyCallbacks();
55 void SetUsageCacheEnabled(QuotaClient::ID client_id,
56 const GURL& origin,
57 bool enabled);
59 private:
60 struct AccumulateInfo {
61 AccumulateInfo() : pending_clients(0), usage(0), unlimited_usage(0) {}
62 int pending_clients;
63 int64 usage;
64 int64 unlimited_usage;
67 typedef std::map<QuotaClient::ID, ClientUsageTracker*> ClientTrackerMap;
69 typedef CallbackQueue<UsageCallback, int64> UsageCallbackQueue;
70 typedef CallbackQueue<GlobalUsageCallback, int64, int64>
71 GlobalUsageCallbackQueue;
72 typedef CallbackQueueMap<UsageCallback, std::string, int64>
73 HostUsageCallbackMap;
75 friend class ClientUsageTracker;
76 void AccumulateClientGlobalLimitedUsage(AccumulateInfo* info,
77 int64 limited_usage);
78 void AccumulateClientGlobalUsage(AccumulateInfo* info,
79 int64 usage,
80 int64 unlimited_usage);
81 void AccumulateClientHostUsage(AccumulateInfo* info,
82 const std::string& host,
83 int64 usage);
85 const StorageType type_;
86 ClientTrackerMap client_tracker_map_;
88 UsageCallbackQueue global_limited_usage_callbacks_;
89 GlobalUsageCallbackQueue global_usage_callbacks_;
90 HostUsageCallbackMap host_usage_callbacks_;
92 StorageMonitor* storage_monitor_;
94 base::WeakPtrFactory<UsageTracker> weak_factory_;
95 DISALLOW_COPY_AND_ASSIGN(UsageTracker);
98 } // namespace storage
100 #endif // STORAGE_BROWSER_QUOTA_USAGE_TRACKER_H_