Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/system_monitor/system_monitor.h" | 5 #include "base/system_monitor/system_monitor.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 : device_id(id), | 32 : device_id(id), |
| 33 name(device_name), | 33 name(device_name), |
| 34 location(device_location) { | 34 location(device_location) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 SystemMonitor::SystemMonitor() | 37 SystemMonitor::SystemMonitor() |
| 38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), |
| 39 devices_changed_observer_list_( | 39 devices_changed_observer_list_( |
| 40 new ObserverListThreadSafe<DevicesChangedObserver>()), | 40 new ObserverListThreadSafe<DevicesChangedObserver>()), |
| 41 battery_in_use_(false), | 41 battery_in_use_(false), |
| 42 suspended_(false) { | 42 suspended_(false), |
| 43 free_space_delegate_(NULL) { | |
| 43 DCHECK(!g_system_monitor); | 44 DCHECK(!g_system_monitor); |
| 44 g_system_monitor = this; | 45 g_system_monitor = this; |
| 45 | 46 |
| 46 DCHECK(MessageLoop::current()); | 47 DCHECK(MessageLoop::current()); |
| 47 #if defined(ENABLE_BATTERY_MONITORING) | 48 #if defined(ENABLE_BATTERY_MONITORING) |
| 48 delayed_battery_check_.Start(FROM_HERE, | 49 delayed_battery_check_.Start(FROM_HERE, |
| 49 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 50 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 50 &SystemMonitor::BatteryCheck); | 51 &SystemMonitor::BatteryCheck); |
| 51 #endif // defined(ENABLE_BATTERY_MONITORING) | 52 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 52 #if defined(OS_MACOSX) | 53 #if defined(OS_MACOSX) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 } | 115 } |
| 115 | 116 |
| 116 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) { | 117 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) { |
| 117 RemovableStorageMap::iterator it = removable_storage_map_.find(id); | 118 RemovableStorageMap::iterator it = removable_storage_map_.find(id); |
| 118 if (it == removable_storage_map_.end()) | 119 if (it == removable_storage_map_.end()) |
| 119 return; | 120 return; |
| 120 removable_storage_map_.erase(it); | 121 removable_storage_map_.erase(it); |
| 121 NotifyRemovableStorageDetached(id); | 122 NotifyRemovableStorageDetached(id); |
| 122 } | 123 } |
| 123 | 124 |
| 125 void SystemMonitor::ProcessStorageFreeSpaceMessage(const FilePath& path, | |
| 126 int64 available_capacity, | |
| 127 bool error) { | |
| 128 scoped_refptr<StorageFreeSpaceObserverList> observers; | |
| 129 { | |
| 130 base::AutoLock lock(free_space_observers_lock_); | |
| 131 StorageFreeSpaceObserverListMap::iterator it = | |
| 132 free_space_observer_list_map_.find(path); | |
| 133 if (it == free_space_observer_list_map_.end()) | |
| 134 return; | |
| 135 observers = it->second; | |
| 136 } | |
| 137 if (error) | |
| 138 NotifyStorageFreeSpaceChanged(observers, path, available_capacity); | |
| 139 else | |
| 140 NotifyStorageFreeSpaceWatchingError(observers, path); | |
| 141 } | |
| 142 | |
| 143 void SystemMonitor::set_storage_free_space_delegate( | |
| 144 StorageFreeSpaceDelegate* delegate) { | |
| 145 DCHECK(free_space_delegate_ == NULL || delegate == NULL); | |
| 146 free_space_delegate_ = delegate; | |
| 147 } | |
| 148 | |
| 124 std::vector<SystemMonitor::RemovableStorageInfo> | 149 std::vector<SystemMonitor::RemovableStorageInfo> |
| 125 SystemMonitor::GetAttachedRemovableStorage() const { | 150 SystemMonitor::GetAttachedRemovableStorage() const { |
| 126 std::vector<RemovableStorageInfo> results; | 151 std::vector<RemovableStorageInfo> results; |
| 127 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); | 152 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); |
| 128 it != removable_storage_map_.end(); | 153 it != removable_storage_map_.end(); |
| 129 ++it) { | 154 ++it) { |
| 130 results.push_back(it->second); | 155 results.push_back(it->second); |
| 131 } | 156 } |
| 132 return results; | 157 return results; |
| 133 } | 158 } |
| 134 | 159 |
| 135 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { | 160 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { |
| 136 power_observer_list_->AddObserver(obs); | 161 power_observer_list_->AddObserver(obs); |
| 137 } | 162 } |
| 138 | 163 |
| 139 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { | 164 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { |
| 140 power_observer_list_->RemoveObserver(obs); | 165 power_observer_list_->RemoveObserver(obs); |
| 141 } | 166 } |
| 142 | 167 |
| 143 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { | 168 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { |
| 144 devices_changed_observer_list_->AddObserver(obs); | 169 devices_changed_observer_list_->AddObserver(obs); |
| 145 } | 170 } |
| 146 | 171 |
| 147 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { | 172 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { |
| 148 devices_changed_observer_list_->RemoveObserver(obs); | 173 devices_changed_observer_list_->RemoveObserver(obs); |
| 149 } | 174 } |
| 150 | 175 |
| 176 void SystemMonitor::AddStorageFreeSpaceObserver(const FilePath& path, | |
| 177 StorageFreeSpaceObserver* obs) { | |
| 178 scoped_refptr<StorageFreeSpaceObserverList> observers; | |
| 179 { | |
| 180 base::AutoLock lock(free_space_observers_lock_); | |
| 181 | |
| 182 // Try to start watching the given storage |path| if the first observer | |
| 183 // will be added. | |
| 184 if (!ContainsKey(free_space_observer_map_, path)) { | |
| 185 free_space_delegate_->StartWatchingStorage(path); | |
|
vandebo (ex-Chrome)
2012/09/13 19:57:34
Can you put this on line 191?
| |
| 186 } | |
| 187 | |
| 188 StorageFreeSpaceObserverListMap::iterator iter = | |
| 189 free_space_observer_list_map_.find(path); | |
| 190 if (iter == free_space_observer_list_map_.end()) { | |
| 191 observers = new StorageFreeSpaceObserverList; | |
| 192 free_space_observer_list_map_.insert(std::make_pair(path, observers)); | |
| 193 } else { | |
| 194 observers = iter->second; | |
| 195 } | |
| 196 | |
| 197 free_space_observer_map_.insert(std::make_pair(path, obs)); | |
| 198 } | |
| 199 observers->AddObserver(obs); | |
| 200 } | |
| 201 | |
| 202 void SystemMonitor::RemoveStorageFreeSpaceObserver( | |
| 203 const FilePath& path, | |
| 204 StorageFreeSpaceObserver* obs) { | |
| 205 scoped_refptr<StorageFreeSpaceObserverList> observers; | |
| 206 { | |
| 207 base::AutoLock lock(free_space_observers_lock_); | |
| 208 StorageFreeSpaceObserverListMap::iterator it = | |
| 209 free_space_observer_list_map_.find(path); | |
| 210 // In case of no observer is interested in the given storage |path|. | |
| 211 if (it == free_space_observer_list_map_.end()) { | |
| 212 DCHECK(free_space_observer_map_.find(path) == | |
|
vandebo (ex-Chrome)
2012/09/13 19:57:34
This case shouldn't happen, right?
| |
| 213 free_space_observer_map_.end()); | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 // Remove the (path, obs) pair from |free_space_observer_map_|. | |
| 218 std::pair<StorageFreeSpaceObserverMap::iterator, | |
| 219 StorageFreeSpaceObserverMap::iterator> range_pair = | |
| 220 free_space_observer_map_.equal_range(path); | |
| 221 StorageFreeSpaceObserverMap::iterator mit; | |
| 222 for (mit = range_pair.first; mit != range_pair.second; ++mit) { | |
| 223 if (mit->second == obs) break; | |
| 224 } | |
| 225 free_space_observer_map_.erase(mit); | |
| 226 | |
| 227 // If the last observer is removed, stop watching the storage |path|. | |
| 228 if (!ContainsKey(free_space_observer_map_, path)) | |
| 229 free_space_delegate_->StopWatchingStorage(path); | |
| 230 | |
| 231 observers = it->second; | |
| 232 } | |
| 233 observers->RemoveObserver(obs); | |
| 234 } | |
| 235 | |
| 151 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { | 236 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { |
| 152 DVLOG(1) << "DevicesChanged with device type " << device_type; | 237 DVLOG(1) << "DevicesChanged with device type " << device_type; |
| 153 devices_changed_observer_list_->Notify( | 238 devices_changed_observer_list_->Notify( |
| 154 &DevicesChangedObserver::OnDevicesChanged, device_type); | 239 &DevicesChangedObserver::OnDevicesChanged, device_type); |
| 155 } | 240 } |
| 156 | 241 |
| 157 void SystemMonitor::NotifyRemovableStorageAttached( | 242 void SystemMonitor::NotifyRemovableStorageAttached( |
| 158 const std::string& id, | 243 const std::string& id, |
| 159 const string16& name, | 244 const string16& name, |
| 160 const FilePath::StringType& location) { | 245 const FilePath::StringType& location) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 180 void SystemMonitor::NotifySuspend() { | 265 void SystemMonitor::NotifySuspend() { |
| 181 DVLOG(1) << "Power Suspending"; | 266 DVLOG(1) << "Power Suspending"; |
| 182 power_observer_list_->Notify(&PowerObserver::OnSuspend); | 267 power_observer_list_->Notify(&PowerObserver::OnSuspend); |
| 183 } | 268 } |
| 184 | 269 |
| 185 void SystemMonitor::NotifyResume() { | 270 void SystemMonitor::NotifyResume() { |
| 186 DVLOG(1) << "Power Resuming"; | 271 DVLOG(1) << "Power Resuming"; |
| 187 power_observer_list_->Notify(&PowerObserver::OnResume); | 272 power_observer_list_->Notify(&PowerObserver::OnResume); |
| 188 } | 273 } |
| 189 | 274 |
| 275 void SystemMonitor::NotifyStorageFreeSpaceChanged( | |
| 276 scoped_refptr<StorageFreeSpaceObserverList> observers, | |
| 277 const FilePath& path, | |
| 278 int64 available_capacity) { | |
| 279 DVLOG(1) << "Free Space Change: " << path.value() << " " | |
| 280 << available_capacity << " bytes"; | |
| 281 if (observers.get()) | |
| 282 observers->Notify(&StorageFreeSpaceObserver::OnFreeSpaceChanged, | |
| 283 path, available_capacity); | |
| 284 } | |
| 285 | |
| 286 void SystemMonitor::NotifyStorageFreeSpaceWatchingError( | |
| 287 scoped_refptr<StorageFreeSpaceObserverList> observers, | |
| 288 const FilePath& path) { | |
| 289 DVLOG(1) << "Failed to watch free space change for " << path.value(); | |
| 290 if (observers.get()) | |
| 291 observers->Notify(&StorageFreeSpaceObserver::OnFreeSpaceWatchingError, | |
| 292 path); | |
| 293 } | |
| 294 | |
| 190 void SystemMonitor::BatteryCheck() { | 295 void SystemMonitor::BatteryCheck() { |
| 191 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 296 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 192 } | 297 } |
| 193 | 298 |
| 194 } // namespace base | 299 } // namespace base |
| OLD | NEW |