| 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" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 static SystemMonitor* g_system_monitor = NULL; | 17 static SystemMonitor* g_system_monitor = NULL; |
| 18 | 18 |
| 19 #if defined(ENABLE_BATTERY_MONITORING) | 19 #if defined(ENABLE_BATTERY_MONITORING) |
| 20 // The amount of time (in ms) to wait before running the initial | 20 // The amount of time (in ms) to wait before running the initial |
| 21 // battery check. | 21 // battery check. |
| 22 static int kDelayedBatteryCheckMs = 10 * 1000; | 22 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 23 #endif // defined(ENABLE_BATTERY_MONITORING) | 23 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 24 | 24 |
| 25 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() { | |
| 26 } | |
| 27 | |
| 28 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo( | |
| 29 const std::string& id, | |
| 30 const string16& device_name, | |
| 31 const FilePath::StringType& device_location) | |
| 32 : device_id(id), | |
| 33 name(device_name), | |
| 34 location(device_location) { | |
| 35 } | |
| 36 | |
| 37 SystemMonitor::SystemMonitor() | 25 SystemMonitor::SystemMonitor() |
| 38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 26 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), |
| 39 devices_changed_observer_list_( | 27 devices_changed_observer_list_( |
| 40 new ObserverListThreadSafe<DevicesChangedObserver>()), | 28 new ObserverListThreadSafe<DevicesChangedObserver>()), |
| 41 battery_in_use_(false), | 29 battery_in_use_(false), |
| 42 suspended_(false) { | 30 suspended_(false) { |
| 43 DCHECK(!g_system_monitor); | 31 DCHECK(!g_system_monitor); |
| 44 g_system_monitor = this; | 32 g_system_monitor = this; |
| 45 | 33 |
| 46 DCHECK(MessageLoop::current()); | 34 DCHECK(MessageLoop::current()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 NotifySuspend(); | 80 NotifySuspend(); |
| 93 } | 81 } |
| 94 break; | 82 break; |
| 95 } | 83 } |
| 96 } | 84 } |
| 97 | 85 |
| 98 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { | 86 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { |
| 99 NotifyDevicesChanged(device_type); | 87 NotifyDevicesChanged(device_type); |
| 100 } | 88 } |
| 101 | 89 |
| 102 void SystemMonitor::ProcessRemovableStorageAttached( | |
| 103 const std::string& id, | |
| 104 const string16& name, | |
| 105 const FilePath::StringType& location) { | |
| 106 { | |
| 107 base::AutoLock lock(removable_storage_lock_); | |
| 108 if (ContainsKey(removable_storage_map_, id)) { | |
| 109 // This can happen if our unique id scheme fails. Ignore the incoming | |
| 110 // non-unique attachment. | |
| 111 return; | |
| 112 } | |
| 113 RemovableStorageInfo info(id, name, location); | |
| 114 removable_storage_map_.insert(std::make_pair(id, info)); | |
| 115 } | |
| 116 NotifyRemovableStorageAttached(id, name, location); | |
| 117 } | |
| 118 | |
| 119 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) { | |
| 120 { | |
| 121 base::AutoLock lock(removable_storage_lock_); | |
| 122 RemovableStorageMap::iterator it = removable_storage_map_.find(id); | |
| 123 if (it == removable_storage_map_.end()) | |
| 124 return; | |
| 125 removable_storage_map_.erase(it); | |
| 126 } | |
| 127 NotifyRemovableStorageDetached(id); | |
| 128 } | |
| 129 | |
| 130 std::vector<SystemMonitor::RemovableStorageInfo> | |
| 131 SystemMonitor::GetAttachedRemovableStorage() const { | |
| 132 std::vector<RemovableStorageInfo> results; | |
| 133 | |
| 134 base::AutoLock lock(removable_storage_lock_); | |
| 135 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); | |
| 136 it != removable_storage_map_.end(); | |
| 137 ++it) { | |
| 138 results.push_back(it->second); | |
| 139 } | |
| 140 return results; | |
| 141 } | |
| 142 | |
| 143 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { | 90 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { |
| 144 power_observer_list_->AddObserver(obs); | 91 power_observer_list_->AddObserver(obs); |
| 145 } | 92 } |
| 146 | 93 |
| 147 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { | 94 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { |
| 148 power_observer_list_->RemoveObserver(obs); | 95 power_observer_list_->RemoveObserver(obs); |
| 149 } | 96 } |
| 150 | 97 |
| 151 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { | 98 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { |
| 152 devices_changed_observer_list_->AddObserver(obs); | 99 devices_changed_observer_list_->AddObserver(obs); |
| 153 } | 100 } |
| 154 | 101 |
| 155 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { | 102 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { |
| 156 devices_changed_observer_list_->RemoveObserver(obs); | 103 devices_changed_observer_list_->RemoveObserver(obs); |
| 157 } | 104 } |
| 158 | 105 |
| 159 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { | 106 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { |
| 160 DVLOG(1) << "DevicesChanged with device type " << device_type; | 107 DVLOG(1) << "DevicesChanged with device type " << device_type; |
| 161 devices_changed_observer_list_->Notify( | 108 devices_changed_observer_list_->Notify( |
| 162 &DevicesChangedObserver::OnDevicesChanged, device_type); | 109 &DevicesChangedObserver::OnDevicesChanged, device_type); |
| 163 } | 110 } |
| 164 | 111 |
| 165 void SystemMonitor::NotifyRemovableStorageAttached( | |
| 166 const std::string& id, | |
| 167 const string16& name, | |
| 168 const FilePath::StringType& location) { | |
| 169 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name) | |
| 170 << " and id " << id; | |
| 171 devices_changed_observer_list_->Notify( | |
| 172 &DevicesChangedObserver::OnRemovableStorageAttached, id, name, location); | |
| 173 } | |
| 174 | |
| 175 void SystemMonitor::NotifyRemovableStorageDetached(const std::string& id) { | |
| 176 DVLOG(1) << "RemovableStorageDetached for id " << id; | |
| 177 devices_changed_observer_list_->Notify( | |
| 178 &DevicesChangedObserver::OnRemovableStorageDetached, id); | |
| 179 } | |
| 180 | |
| 181 void SystemMonitor::NotifyPowerStateChange() { | 112 void SystemMonitor::NotifyPowerStateChange() { |
| 182 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 113 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| 183 << " battery"; | 114 << " battery"; |
| 184 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, | 115 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, |
| 185 BatteryPower()); | 116 BatteryPower()); |
| 186 } | 117 } |
| 187 | 118 |
| 188 void SystemMonitor::NotifySuspend() { | 119 void SystemMonitor::NotifySuspend() { |
| 189 DVLOG(1) << "Power Suspending"; | 120 DVLOG(1) << "Power Suspending"; |
| 190 power_observer_list_->Notify(&PowerObserver::OnSuspend); | 121 power_observer_list_->Notify(&PowerObserver::OnSuspend); |
| 191 } | 122 } |
| 192 | 123 |
| 193 void SystemMonitor::NotifyResume() { | 124 void SystemMonitor::NotifyResume() { |
| 194 DVLOG(1) << "Power Resuming"; | 125 DVLOG(1) << "Power Resuming"; |
| 195 power_observer_list_->Notify(&PowerObserver::OnResume); | 126 power_observer_list_->Notify(&PowerObserver::OnResume); |
| 196 } | 127 } |
| 197 | 128 |
| 198 void SystemMonitor::BatteryCheck() { | 129 void SystemMonitor::BatteryCheck() { |
| 199 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 130 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 200 } | 131 } |
| 201 | 132 |
| 202 } // namespace base | 133 } // namespace base |
| OLD | NEW |