| 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) | |
| 20 // The amount of time (in ms) to wait before running the initial | |
| 21 // battery check. | |
| 22 static int kDelayedBatteryCheckMs = 10 * 1000; | |
| 23 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 24 | |
| 25 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() { | 19 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() { |
| 26 } | 20 } |
| 27 | 21 |
| 28 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo( | 22 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo( |
| 29 const std::string& id, | 23 const std::string& id, |
| 30 const string16& device_name, | 24 const string16& device_name, |
| 31 const FilePath::StringType& device_location) | 25 const FilePath::StringType& device_location) |
| 32 : device_id(id), | 26 : device_id(id), |
| 33 name(device_name), | 27 name(device_name), |
| 34 location(device_location) { | 28 location(device_location) { |
| 35 } | 29 } |
| 36 | 30 |
| 37 SystemMonitor::SystemMonitor() | 31 SystemMonitor::SystemMonitor() |
| 38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 32 : devices_changed_observer_list_( |
| 39 devices_changed_observer_list_( | 33 new ObserverListThreadSafe<DevicesChangedObserver>()) { |
| 40 new ObserverListThreadSafe<DevicesChangedObserver>()), | |
| 41 battery_in_use_(false), | |
| 42 suspended_(false) { | |
| 43 DCHECK(!g_system_monitor); | 34 DCHECK(!g_system_monitor); |
| 44 g_system_monitor = this; | 35 g_system_monitor = this; |
| 45 | |
| 46 DCHECK(MessageLoop::current()); | |
| 47 #if defined(ENABLE_BATTERY_MONITORING) | |
| 48 delayed_battery_check_.Start(FROM_HERE, | |
| 49 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | |
| 50 &SystemMonitor::BatteryCheck); | |
| 51 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 52 #if defined(OS_MACOSX) | |
| 53 PlatformInit(); | |
| 54 #endif | |
| 55 } | 36 } |
| 56 | 37 |
| 57 SystemMonitor::~SystemMonitor() { | 38 SystemMonitor::~SystemMonitor() { |
| 58 #if defined(OS_MACOSX) | |
| 59 PlatformDestroy(); | |
| 60 #endif | |
| 61 DCHECK_EQ(this, g_system_monitor); | 39 DCHECK_EQ(this, g_system_monitor); |
| 62 g_system_monitor = NULL; | 40 g_system_monitor = NULL; |
| 63 } | 41 } |
| 64 | 42 |
| 65 // static | 43 // static |
| 66 SystemMonitor* SystemMonitor::Get() { | 44 SystemMonitor* SystemMonitor::Get() { |
| 67 return g_system_monitor; | 45 return g_system_monitor; |
| 68 } | 46 } |
| 69 | 47 |
| 70 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | |
| 71 // Suppress duplicate notifications. Some platforms may | |
| 72 // send multiple notifications of the same event. | |
| 73 switch (event_id) { | |
| 74 case POWER_STATE_EVENT: | |
| 75 { | |
| 76 bool on_battery = IsBatteryPower(); | |
| 77 if (on_battery != battery_in_use_) { | |
| 78 battery_in_use_ = on_battery; | |
| 79 NotifyPowerStateChange(); | |
| 80 } | |
| 81 } | |
| 82 break; | |
| 83 case RESUME_EVENT: | |
| 84 if (suspended_) { | |
| 85 suspended_ = false; | |
| 86 NotifyResume(); | |
| 87 } | |
| 88 break; | |
| 89 case SUSPEND_EVENT: | |
| 90 if (!suspended_) { | |
| 91 suspended_ = true; | |
| 92 NotifySuspend(); | |
| 93 } | |
| 94 break; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { | 48 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { |
| 99 NotifyDevicesChanged(device_type); | 49 NotifyDevicesChanged(device_type); |
| 100 } | 50 } |
| 101 | 51 |
| 102 void SystemMonitor::ProcessRemovableStorageAttached( | 52 void SystemMonitor::ProcessRemovableStorageAttached( |
| 103 const std::string& id, | 53 const std::string& id, |
| 104 const string16& name, | 54 const string16& name, |
| 105 const FilePath::StringType& location) { | 55 const FilePath::StringType& location) { |
| 106 { | 56 { |
| 107 base::AutoLock lock(removable_storage_lock_); | 57 base::AutoLock lock(removable_storage_lock_); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 133 | 83 |
| 134 base::AutoLock lock(removable_storage_lock_); | 84 base::AutoLock lock(removable_storage_lock_); |
| 135 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); | 85 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); |
| 136 it != removable_storage_map_.end(); | 86 it != removable_storage_map_.end(); |
| 137 ++it) { | 87 ++it) { |
| 138 results.push_back(it->second); | 88 results.push_back(it->second); |
| 139 } | 89 } |
| 140 return results; | 90 return results; |
| 141 } | 91 } |
| 142 | 92 |
| 143 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { | |
| 144 power_observer_list_->AddObserver(obs); | |
| 145 } | |
| 146 | |
| 147 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { | |
| 148 power_observer_list_->RemoveObserver(obs); | |
| 149 } | |
| 150 | |
| 151 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { | 93 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { |
| 152 devices_changed_observer_list_->AddObserver(obs); | 94 devices_changed_observer_list_->AddObserver(obs); |
| 153 } | 95 } |
| 154 | 96 |
| 155 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { | 97 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { |
| 156 devices_changed_observer_list_->RemoveObserver(obs); | 98 devices_changed_observer_list_->RemoveObserver(obs); |
| 157 } | 99 } |
| 158 | 100 |
| 159 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { | 101 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { |
| 160 DVLOG(1) << "DevicesChanged with device type " << device_type; | 102 DVLOG(1) << "DevicesChanged with device type " << device_type; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 171 devices_changed_observer_list_->Notify( | 113 devices_changed_observer_list_->Notify( |
| 172 &DevicesChangedObserver::OnRemovableStorageAttached, id, name, location); | 114 &DevicesChangedObserver::OnRemovableStorageAttached, id, name, location); |
| 173 } | 115 } |
| 174 | 116 |
| 175 void SystemMonitor::NotifyRemovableStorageDetached(const std::string& id) { | 117 void SystemMonitor::NotifyRemovableStorageDetached(const std::string& id) { |
| 176 DVLOG(1) << "RemovableStorageDetached for id " << id; | 118 DVLOG(1) << "RemovableStorageDetached for id " << id; |
| 177 devices_changed_observer_list_->Notify( | 119 devices_changed_observer_list_->Notify( |
| 178 &DevicesChangedObserver::OnRemovableStorageDetached, id); | 120 &DevicesChangedObserver::OnRemovableStorageDetached, id); |
| 179 } | 121 } |
| 180 | 122 |
| 181 void SystemMonitor::NotifyPowerStateChange() { | |
| 182 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
| 183 << " battery"; | |
| 184 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, | |
| 185 BatteryPower()); | |
| 186 } | |
| 187 | |
| 188 void SystemMonitor::NotifySuspend() { | |
| 189 DVLOG(1) << "Power Suspending"; | |
| 190 power_observer_list_->Notify(&PowerObserver::OnSuspend); | |
| 191 } | |
| 192 | |
| 193 void SystemMonitor::NotifyResume() { | |
| 194 DVLOG(1) << "Power Resuming"; | |
| 195 power_observer_list_->Notify(&PowerObserver::OnResume); | |
| 196 } | |
| 197 | |
| 198 void SystemMonitor::BatteryCheck() { | |
| 199 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | |
| 200 } | |
| 201 | |
| 202 } // namespace base | 123 } // namespace base |
| OLD | NEW |