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 content { 16 class StorageMonitorTestBase; 17 } 18 19 namespace quota { 20 21 class QuotaManager; 22 23 // This class dispatches storage events to observers of a common 24 // StorageObserver::Filter. 25 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageObserverList { 26 public: 27 StorageObserverList(); 28 virtual ~StorageObserverList(); 29 30 // Adds/removes an observer. 31 void AddObserver(StorageObserver* observer, 32 const StorageObserver::MonitorParams& params); 33 void RemoveObserver(StorageObserver* observer); 34 35 // Returns the number of observers. 36 int ObserverCount() const; 37 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); 42 43 // Dispatch an event to observers that require it. 44 void MaybeDispatchEvent(const StorageObserver::Event& event); 45 46 // Ensure the specified observer receives the next dispatched event. 47 void ScheduleUpdateForObserver(StorageObserver* observer); 48 49 private: 50 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE ObserverState { 51 GURL origin; 52 base::TimeTicks last_notification_time; 53 base::TimeDelta rate; 54 bool requires_update; 55 56 ObserverState(); 57 }; 58 typedef std::map<StorageObserver*, ObserverState> StorageObserverStateMap; 59 60 void DispatchPendingEvent(); 61 62 StorageObserverStateMap observers_; 63 base::OneShotTimer<StorageObserverList> notification_timer_; 64 StorageObserver::Event pending_event_; 65 66 friend class content::StorageMonitorTestBase; 67 68 DISALLOW_COPY_AND_ASSIGN(StorageObserverList); 69 }; 70 71 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 WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE HostStorageObservers { 75 public: 76 explicit HostStorageObservers(QuotaManager* quota_manager); 77 virtual ~HostStorageObservers(); 78 79 bool is_initialized() const { return initialized_; } 80 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; 87 88 // Handles a usage change. 89 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); 90 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); 98 99 QuotaManager* quota_manager_; 100 StorageObserverList observers_; 101 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_; 107 108 // Cached accumulated usage and quota for the host. 109 int64 cached_usage_; 110 int64 cached_quota_; 111 112 base::WeakPtrFactory<HostStorageObservers> weak_factory_; 113 114 friend class content::StorageMonitorTestBase; 115 116 DISALLOW_COPY_AND_ASSIGN(HostStorageObservers); 117 }; 118 119 120 // Manages the observers of a common storage type. 121 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageTypeObservers { 122 public: 123 explicit StorageTypeObservers(QuotaManager* quota_manager); 124 virtual ~StorageTypeObservers(); 125 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); 132 133 // Returns the observers of a specific host. 134 const HostStorageObservers* GetHostObservers(const std::string& host) const; 135 136 // Handles a usage change. 137 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); 138 139 private: 140 typedef std::map<std::string, HostStorageObservers*> HostObserversMap; 141 142 QuotaManager* quota_manager_; 143 HostObserversMap host_observers_map_; 144 145 DISALLOW_COPY_AND_ASSIGN(StorageTypeObservers); 146 }; 147 148 149 // Storage monitor manages observers and dispatches storage events to them. 150 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE StorageMonitor { 151 public: 152 explicit StorageMonitor(QuotaManager* quota_manager); 153 virtual ~StorageMonitor(); 154 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); 161 162 // Returns the observers of a specific storage type. 163 const StorageTypeObservers* GetStorageTypeObservers( 164 StorageType storage_type) const; 165 166 // Handles a usage change. 167 void NotifyUsageChange(const StorageObserver::Filter& filter, int64 delta); 168 169 private: 170 typedef std::map<StorageType, StorageTypeObservers*> StorageTypeObserversMap; 171 172 QuotaManager* quota_manager_; 173 StorageTypeObserversMap storage_type_observers_map_; 174 175 DISALLOW_COPY_AND_ASSIGN(StorageMonitor); 176 }; 177 178 } // namespace quota 179 180 #endif // WEBKIT_BROWSER_QUOTA_STORAGE_MONITOR_H_ 181