OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef WEBKIT_BROWSER_QUOTA_STORAGE_MONITOR_H_ | |
6 #define WEBKIT_BROWSER_QUOTA_STORAGE_MONITOR_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/time/time.h" | |
12 #include "base/timer/timer.h" | |
13 #include "webkit/browser/quota/storage_observer.h" | |
14 | |
15 namespace quota { | |
16 | |
17 class QuotaManager; | |
18 | |
19 // This class dispatches storage events to observers of a common | |
20 // StorageObserver::Filter. | |
21 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageObserverList { | |
22 public: | |
23 StorageObserverList(); | |
24 virtual ~StorageObserverList(); | |
25 | |
26 // Adds/removes an observer. | |
27 virtual void AddObserver(StorageObserver* observer, | |
28 const StorageObserver::MonitorParams& params); | |
29 void RemoveObserver(StorageObserver* observer); | |
30 | |
31 // Returns the number of observers. | |
32 int ObserverCount() const; | |
33 | |
34 // Forwards a storage change to observers. The event may be dispatched | |
35 // immediately to an observer or after a delay, depending on the desired event | |
36 // rate of the observer. | |
37 void OnStorageChange(const StorageObserver::Event& event); | |
38 | |
39 // Returns a pending event or NULL if no event is pending (for testing). | |
40 const StorageObserver::Event* GetPendingEvent() const; | |
41 | |
42 // Returns the number of observers that need to be dispatched the latest | |
43 // event (for testing). | |
44 int GetRequiredUpdatesCount() const; | |
45 | |
46 protected: | |
47 // Dispatch an event to observers that require it. | |
48 void MaybeDispatchEvent(const StorageObserver::Event& event); | |
49 | |
50 // Ensure the specified observer receives the next dispatched event. | |
51 void ScheduleUpdateForObserver(StorageObserver* observer); | |
52 | |
53 private: | |
54 friend class StorageObserverListTest; | |
55 | |
56 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE ObserverState { | |
57 GURL origin; | |
58 base::Time last_notification_time; | |
tzik
2014/03/31 07:45:48
I think base::TimeTicks is better for this.
| |
59 base::TimeDelta rate; | |
60 bool requires_update; | |
61 | |
62 ObserverState(); | |
63 }; | |
64 typedef std::map<StorageObserver*, ObserverState> StorageObserverStateMap; | |
65 | |
66 void DispatchPendingEvent(); | |
67 | |
68 StorageObserverStateMap observers_; | |
69 base::OneShotTimer<StorageObserverList> notification_timer_; | |
70 StorageObserver::Event pending_event_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(StorageObserverList); | |
73 }; | |
74 | |
75 | |
76 // Manages the storage observers of a common host. Caches the usage and quota of | |
77 // the host to avoid accumulating for every change. | |
78 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE HostStorageObservers | |
79 : public StorageObserverList { | |
80 public: | |
81 HostStorageObservers(QuotaManager* quota_manager); | |
tzik
2014/03/31 06:54:16
explicit?
| |
82 virtual ~HostStorageObservers(); | |
83 | |
84 bool is_initialized() const { return initialized_; } | |
85 | |
86 // Adds an observer. | |
87 virtual void AddObserver( | |
88 StorageObserver* observer, | |
89 const StorageObserver::MonitorParams& params) OVERRIDE; | |
90 | |
91 // Handles a usage change. | |
92 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); | |
93 | |
94 private: | |
95 void StartInitialization(const StorageObserver::Filter& filter); | |
96 void GotHostUsageAndQuota(const StorageObserver::Filter& filter, | |
97 QuotaStatusCode status, | |
98 int64 usage, | |
99 int64 quota); | |
100 void DispatchEvent(const StorageObserver::Filter& filter, bool is_update); | |
101 | |
102 QuotaManager* quota_manager_; | |
103 base::WeakPtrFactory<HostStorageObservers> weak_factory_; | |
tzik
2014/03/31 06:54:16
please make this the last member of the class.
| |
104 | |
105 // Flags used during initialization of the cached properties. | |
106 bool initialized_; | |
107 bool initializing_; | |
108 bool event_occurred_before_init_; | |
109 | |
110 // Cached accumulated usage and quota for the host. | |
111 int64 cached_usage_; | |
112 int64 cached_quota_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(HostStorageObservers); | |
115 }; | |
116 | |
117 | |
118 // Manages the observers of a common storage type. | |
119 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageTypeObservers { | |
120 public: | |
121 StorageTypeObservers(QuotaManager* quota_manager); | |
122 virtual ~StorageTypeObservers(); | |
123 | |
124 // Adds and removes an observer. | |
125 void AddObserver(StorageObserver* observer, | |
126 const StorageObserver::MonitorParams& params); | |
127 void RemoveObserver(StorageObserver* observer); | |
128 void RemoveObserverForFilter(StorageObserver* observer, | |
129 const StorageObserver::Filter& filter); | |
130 | |
131 // Returns the observers of a specific host (for testing). | |
132 const HostStorageObservers* GetHostObservers(const std::string& host) const; | |
133 | |
134 // Handles a usage change. | |
135 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); | |
136 | |
137 private: | |
138 typedef std::map<std::string, HostStorageObservers*> HostObserversMap; | |
139 | |
140 QuotaManager* quota_manager_; | |
141 HostObserversMap host_observers_map_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(StorageTypeObservers); | |
144 }; | |
145 | |
146 | |
147 // Storage monitor manages observers and dispatches storage events to them. | |
148 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageMonitor { | |
149 public: | |
150 explicit StorageMonitor(QuotaManager* quota_manager); | |
151 virtual ~StorageMonitor(); | |
152 | |
153 // Adds and removes an observer. | |
154 void AddObserver(StorageObserver* observer, | |
155 const StorageObserver::MonitorParams& params); | |
156 void RemoveObserver(StorageObserver* observer); | |
157 void RemoveObserverForFilter(StorageObserver* observer, | |
158 const StorageObserver::Filter& filter); | |
159 | |
160 // Returns the observers of a specific storage type (for testing). | |
161 const StorageTypeObservers* GetStorageTypeObservers( | |
162 StorageType storage_type) const; | |
163 | |
164 // Handles a usage change. | |
165 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); | |
166 | |
167 private: | |
168 typedef std::map<StorageType, StorageTypeObservers*> StorageTypeObserversMap; | |
169 | |
170 QuotaManager* quota_manager_; | |
171 StorageTypeObserversMap storage_type_observers_map_; | |
172 | |
173 DISALLOW_COPY_AND_ASSIGN(StorageMonitor); | |
174 }; | |
175 | |
176 } // namespace quota | |
177 | |
178 #endif // WEBKIT_BROWSER_QUOTA_STORAGE_MONITOR_H_ | |
OLD | NEW |