Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / storage / browser / quota / storage_monitor.h
blob650c7b1f7f74cdf14b146f8c2c9ab5d58202bb25
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_STORAGE_MONITOR_H_
6 #define STORAGE_BROWSER_QUOTA_STORAGE_MONITOR_H_
8 #include <map>
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "storage/browser/quota/storage_observer.h"
15 namespace content {
16 class StorageMonitorTestBase;
19 namespace storage {
21 class QuotaManager;
23 // This class dispatches storage events to observers of a common
24 // StorageObserver::Filter.
25 class STORAGE_EXPORT_PRIVATE StorageObserverList {
26 public:
27 StorageObserverList();
28 virtual ~StorageObserverList();
30 // Adds/removes an observer.
31 void AddObserver(StorageObserver* observer,
32 const StorageObserver::MonitorParams& params);
33 void RemoveObserver(StorageObserver* observer);
35 // Returns the number of observers.
36 int ObserverCount() const;
38 // Forwards a storage change to observers. The event may be dispatched
39 // immediately to an observer or after a delay, depending on the desired event
40 // rate of the observer.
41 void OnStorageChange(const StorageObserver::Event& event);
43 // Dispatch an event to observers that require it.
44 void MaybeDispatchEvent(const StorageObserver::Event& event);
46 // Ensure the specified observer receives the next dispatched event.
47 void ScheduleUpdateForObserver(StorageObserver* observer);
49 private:
50 struct STORAGE_EXPORT_PRIVATE ObserverState {
51 GURL origin;
52 base::TimeTicks last_notification_time;
53 base::TimeDelta rate;
54 bool requires_update;
56 ObserverState();
58 typedef std::map<StorageObserver*, ObserverState> StorageObserverStateMap;
60 void DispatchPendingEvent();
62 StorageObserverStateMap observers_;
63 base::OneShotTimer<StorageObserverList> notification_timer_;
64 StorageObserver::Event pending_event_;
66 friend class content::StorageMonitorTestBase;
68 DISALLOW_COPY_AND_ASSIGN(StorageObserverList);
72 // Manages the storage observers of a common host. Caches the usage and quota of
73 // the host to avoid accumulating for every change.
74 class STORAGE_EXPORT_PRIVATE HostStorageObservers {
75 public:
76 explicit HostStorageObservers(QuotaManager* quota_manager);
77 virtual ~HostStorageObservers();
79 bool is_initialized() const { return initialized_; }
81 // Adds/removes an observer.
82 void AddObserver(
83 StorageObserver* observer,
84 const StorageObserver::MonitorParams& params);
85 void RemoveObserver(StorageObserver* observer);
86 bool ContainsObservers() const;
88 // Handles a usage change.
89 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta);
91 private:
92 void StartInitialization(const StorageObserver::Filter& filter);
93 void GotHostUsageAndQuota(const StorageObserver::Filter& filter,
94 QuotaStatusCode status,
95 int64 usage,
96 int64 quota);
97 void DispatchEvent(const StorageObserver::Filter& filter, bool is_update);
99 QuotaManager* quota_manager_;
100 StorageObserverList observers_;
102 // Flags used during initialization of the cached properties.
103 bool initialized_;
104 bool initializing_;
105 bool event_occurred_before_init_;
106 int64 usage_deltas_during_init_;
108 // Cached accumulated usage and quota for the host.
109 int64 cached_usage_;
110 int64 cached_quota_;
112 base::WeakPtrFactory<HostStorageObservers> weak_factory_;
114 friend class content::StorageMonitorTestBase;
116 DISALLOW_COPY_AND_ASSIGN(HostStorageObservers);
120 // Manages the observers of a common storage type.
121 class STORAGE_EXPORT_PRIVATE StorageTypeObservers {
122 public:
123 explicit StorageTypeObservers(QuotaManager* quota_manager);
124 virtual ~StorageTypeObservers();
126 // Adds and removes an observer.
127 void AddObserver(StorageObserver* observer,
128 const StorageObserver::MonitorParams& params);
129 void RemoveObserver(StorageObserver* observer);
130 void RemoveObserverForFilter(StorageObserver* observer,
131 const StorageObserver::Filter& filter);
133 // Returns the observers of a specific host.
134 const HostStorageObservers* GetHostObservers(const std::string& host) const;
136 // Handles a usage change.
137 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta);
139 private:
140 typedef std::map<std::string, HostStorageObservers*> HostObserversMap;
142 QuotaManager* quota_manager_;
143 HostObserversMap host_observers_map_;
145 DISALLOW_COPY_AND_ASSIGN(StorageTypeObservers);
149 // Storage monitor manages observers and dispatches storage events to them.
150 class STORAGE_EXPORT_PRIVATE StorageMonitor {
151 public:
152 explicit StorageMonitor(QuotaManager* quota_manager);
153 virtual ~StorageMonitor();
155 // Adds and removes an observer.
156 void AddObserver(StorageObserver* observer,
157 const StorageObserver::MonitorParams& params);
158 void RemoveObserver(StorageObserver* observer);
159 void RemoveObserverForFilter(StorageObserver* observer,
160 const StorageObserver::Filter& filter);
162 // Returns the observers of a specific storage type.
163 const StorageTypeObservers* GetStorageTypeObservers(
164 StorageType storage_type) const;
166 // Handles a usage change.
167 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta);
169 private:
170 typedef std::map<StorageType, StorageTypeObservers*> StorageTypeObserversMap;
172 QuotaManager* quota_manager_;
173 StorageTypeObserversMap storage_type_observers_map_;
175 DISALLOW_COPY_AND_ASSIGN(StorageMonitor);
178 } // namespace storage
180 #endif // STORAGE_BROWSER_QUOTA_STORAGE_MONITOR_H_