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_
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"
26 class ClientUsageTracker
;
29 // A helper class that gathers and tracks the amount of data stored in
31 // An instance of this class is created per storage type.
32 class STORAGE_EXPORT UsageTracker
: public QuotaTaskObserver
{
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
,
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
,
60 struct AccumulateInfo
{
61 AccumulateInfo() : pending_clients(0), usage(0), unlimited_usage(0) {}
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
>
75 friend class ClientUsageTracker
;
76 void AccumulateClientGlobalLimitedUsage(AccumulateInfo
* info
,
78 void AccumulateClientGlobalUsage(AccumulateInfo
* info
,
80 int64 unlimited_usage
);
81 void AccumulateClientHostUsage(AccumulateInfo
* info
,
82 const std::string
& host
,
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_