1 // Copyright 2014 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_CLIENT_USAGE_TRACKER_H_
6 #define STORAGE_BROWSER_QUOTA_CLIENT_USAGE_TRACKER_H_
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 "storage/browser/quota/quota_callbacks.h"
18 #include "storage/browser/quota/quota_client.h"
19 #include "storage/browser/quota/quota_task.h"
20 #include "storage/browser/quota/special_storage_policy.h"
21 #include "storage/browser/storage_browser_export.h"
22 #include "storage/common/quota/quota_types.h"
30 // This class holds per-client usage tracking information and caches per-host
31 // usage data. An instance of this class is created per client.
32 class ClientUsageTracker
: public SpecialStoragePolicy::Observer
,
33 public base::NonThreadSafe
,
34 public base::SupportsWeakPtr
<ClientUsageTracker
> {
36 typedef base::Callback
<void(int64 limited_usage
,
37 int64 unlimited_usage
)> HostUsageAccumulator
;
38 typedef base::Callback
<void(const GURL
& origin
,
39 int64 usage
)> OriginUsageAccumulator
;
40 typedef std::map
<std::string
, std::set
<GURL
> > OriginSetByHost
;
42 ClientUsageTracker(UsageTracker
* tracker
,
45 SpecialStoragePolicy
* special_storage_policy
,
46 StorageMonitor
* storage_monitor
);
47 ~ClientUsageTracker() override
;
49 void GetGlobalLimitedUsage(const UsageCallback
& callback
);
50 void GetGlobalUsage(const GlobalUsageCallback
& callback
);
51 void GetHostUsage(const std::string
& host
, const UsageCallback
& callback
);
52 void UpdateUsageCache(const GURL
& origin
, int64 delta
);
53 void GetCachedHostsUsage(std::map
<std::string
, int64
>* host_usage
) const;
54 void GetCachedOrigins(std::set
<GURL
>* origins
) const;
55 int64
GetCachedOriginsUsage(const std::set
<GURL
>& origins
,
56 std::vector
<GURL
>* origins_not_in_cache
);
57 bool IsUsageCacheEnabledForOrigin(const GURL
& origin
) const;
58 void SetUsageCacheEnabled(const GURL
& origin
, bool enabled
);
61 typedef CallbackQueueMap
<HostUsageAccumulator
, std::string
, int64
, int64
>
62 HostUsageAccumulatorMap
;
64 typedef std::set
<std::string
> HostSet
;
65 typedef std::map
<GURL
, int64
> UsageMap
;
66 typedef std::map
<std::string
, UsageMap
> HostUsageMap
;
68 struct AccumulateInfo
{
71 int64 unlimited_usage
;
74 : pending_jobs(0), limited_usage(0), unlimited_usage(0) {}
77 void AccumulateLimitedOriginUsage(AccumulateInfo
* info
,
78 const UsageCallback
& callback
,
80 void DidGetOriginsForGlobalUsage(const GlobalUsageCallback
& callback
,
81 const std::set
<GURL
>& origins
);
82 void AccumulateHostUsage(AccumulateInfo
* info
,
83 const GlobalUsageCallback
& callback
,
85 int64 unlimited_usage
);
87 void DidGetOriginsForHostUsage(const std::string
& host
,
88 const std::set
<GURL
>& origins
);
90 void GetUsageForOrigins(const std::string
& host
,
91 const std::set
<GURL
>& origins
);
92 void AccumulateOriginUsage(AccumulateInfo
* info
,
93 const std::string
& host
,
97 void DidGetHostUsageAfterUpdate(const GURL
& origin
, int64 usage
);
99 // Methods used by our GatherUsage tasks, as a task makes progress
100 // origins and hosts are added incrementally to the cache.
101 void AddCachedOrigin(const GURL
& origin
, int64 usage
);
102 void AddCachedHost(const std::string
& host
);
104 int64
GetCachedHostUsage(const std::string
& host
) const;
105 int64
GetCachedGlobalUnlimitedUsage();
106 bool GetCachedOriginUsage(const GURL
& origin
, int64
* usage
) const;
108 // SpecialStoragePolicy::Observer overrides
109 void OnGranted(const GURL
& origin
, int change_flags
) override
;
110 void OnRevoked(const GURL
& origin
, int change_flags
) override
;
111 void OnCleared() override
;
113 bool IsStorageUnlimited(const GURL
& origin
) const;
115 UsageTracker
* tracker_
;
116 QuotaClient
* client_
;
117 const StorageType type_
;
118 StorageMonitor
* storage_monitor_
;
120 int64 global_limited_usage_
;
121 int64 global_unlimited_usage_
;
122 bool global_usage_retrieved_
;
123 HostSet cached_hosts_
;
124 HostUsageMap cached_usage_by_host_
;
126 OriginSetByHost non_cached_limited_origins_by_host_
;
127 OriginSetByHost non_cached_unlimited_origins_by_host_
;
129 HostUsageAccumulatorMap host_usage_accumulators_
;
131 scoped_refptr
<SpecialStoragePolicy
> special_storage_policy_
;
133 DISALLOW_COPY_AND_ASSIGN(ClientUsageTracker
);
136 } // namespace storage
138 #endif // STORAGE_BROWSER_QUOTA_CLIENT_USAGE_TRACKER_H_