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 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 // Dispatch an event to observers that require it. |
| 40 void MaybeDispatchEvent(const StorageObserver::Event& event); |
| 41 |
| 42 // Ensure the specified observer receives the next dispatched event. |
| 43 void ScheduleUpdateForObserver(StorageObserver* observer); |
| 44 |
| 45 private: |
| 46 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE ObserverState { |
| 47 GURL origin; |
| 48 base::TimeTicks last_notification_time; |
| 49 base::TimeDelta rate; |
| 50 bool requires_update; |
| 51 |
| 52 ObserverState(); |
| 53 }; |
| 54 typedef std::map<StorageObserver*, ObserverState> StorageObserverStateMap; |
| 55 |
| 56 void DispatchPendingEvent(); |
| 57 |
| 58 StorageObserverStateMap observers_; |
| 59 base::OneShotTimer<StorageObserverList> notification_timer_; |
| 60 StorageObserver::Event pending_event_; |
| 61 |
| 62 friend class StorageMonitorTestBase; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(StorageObserverList); |
| 65 }; |
| 66 |
| 67 |
| 68 // Manages the storage observers of a common host. Caches the usage and quota of |
| 69 // the host to avoid accumulating for every change. |
| 70 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE HostStorageObservers { |
| 71 public: |
| 72 explicit HostStorageObservers(QuotaManager* quota_manager); |
| 73 virtual ~HostStorageObservers(); |
| 74 |
| 75 bool is_initialized() const { return initialized_; } |
| 76 |
| 77 // Adds/removes an observer. |
| 78 void AddObserver( |
| 79 StorageObserver* observer, |
| 80 const StorageObserver::MonitorParams& params); |
| 81 void RemoveObserver(StorageObserver* observer); |
| 82 bool ContainsObservers() const; |
| 83 |
| 84 // Handles a usage change. |
| 85 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); |
| 86 |
| 87 private: |
| 88 void StartInitialization(const StorageObserver::Filter& filter); |
| 89 void GotHostUsageAndQuota(const StorageObserver::Filter& filter, |
| 90 QuotaStatusCode status, |
| 91 int64 usage, |
| 92 int64 quota); |
| 93 void DispatchEvent(const StorageObserver::Filter& filter, bool is_update); |
| 94 |
| 95 QuotaManager* quota_manager_; |
| 96 StorageObserverList observers_; |
| 97 |
| 98 // Flags used during initialization of the cached properties. |
| 99 bool initialized_; |
| 100 bool initializing_; |
| 101 bool event_occurred_before_init_; |
| 102 |
| 103 // Cached accumulated usage and quota for the host. |
| 104 int64 cached_usage_; |
| 105 int64 cached_quota_; |
| 106 |
| 107 base::WeakPtrFactory<HostStorageObservers> weak_factory_; |
| 108 |
| 109 friend class StorageMonitorTestBase; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(HostStorageObservers); |
| 112 }; |
| 113 |
| 114 |
| 115 // Manages the observers of a common storage type. |
| 116 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageTypeObservers { |
| 117 public: |
| 118 explicit StorageTypeObservers(QuotaManager* quota_manager); |
| 119 virtual ~StorageTypeObservers(); |
| 120 |
| 121 // Adds and removes an observer. |
| 122 void AddObserver(StorageObserver* observer, |
| 123 const StorageObserver::MonitorParams& params); |
| 124 void RemoveObserver(StorageObserver* observer); |
| 125 void RemoveObserverForFilter(StorageObserver* observer, |
| 126 const StorageObserver::Filter& filter); |
| 127 |
| 128 // Returns the observers of a specific host. |
| 129 const HostStorageObservers* GetHostObservers(const std::string& host) const; |
| 130 |
| 131 // Handles a usage change. |
| 132 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); |
| 133 |
| 134 private: |
| 135 typedef std::map<std::string, HostStorageObservers*> HostObserversMap; |
| 136 |
| 137 QuotaManager* quota_manager_; |
| 138 HostObserversMap host_observers_map_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(StorageTypeObservers); |
| 141 }; |
| 142 |
| 143 |
| 144 // Storage monitor manages observers and dispatches storage events to them. |
| 145 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageMonitor { |
| 146 public: |
| 147 explicit StorageMonitor(QuotaManager* quota_manager); |
| 148 virtual ~StorageMonitor(); |
| 149 |
| 150 // Adds and removes an observer. |
| 151 void AddObserver(StorageObserver* observer, |
| 152 const StorageObserver::MonitorParams& params); |
| 153 void RemoveObserver(StorageObserver* observer); |
| 154 void RemoveObserverForFilter(StorageObserver* observer, |
| 155 const StorageObserver::Filter& filter); |
| 156 |
| 157 // Returns the observers of a specific storage type. |
| 158 const StorageTypeObservers* GetStorageTypeObservers( |
| 159 StorageType storage_type) const; |
| 160 |
| 161 // Handles a usage change. |
| 162 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); |
| 163 |
| 164 private: |
| 165 typedef std::map<StorageType, StorageTypeObservers*> StorageTypeObserversMap; |
| 166 |
| 167 QuotaManager* quota_manager_; |
| 168 StorageTypeObserversMap storage_type_observers_map_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(StorageMonitor); |
| 171 }; |
| 172 |
| 173 } // namespace quota |
| 174 |
| 175 #endif // WEBKIT_BROWSER_QUOTA_STORAGE_MONITOR_H_ |
OLD | NEW |