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::MediaDeviceInfo::MediaDeviceInfo( | 25 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo( |
26 const std::string& id, | 26 const std::string& id, |
27 const string16& device_name, | 27 const string16& device_name, |
28 const FilePath::StringType& device_location) | 28 const FilePath::StringType& device_location) |
29 : unique_id(id), | 29 : device_id(id), |
30 name(device_name), | 30 name(device_name), |
31 location(device_location) { | 31 location(device_location) { |
32 } | 32 } |
33 | 33 |
34 SystemMonitor::SystemMonitor() | 34 SystemMonitor::SystemMonitor() |
35 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 35 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), |
36 devices_changed_observer_list_( | 36 devices_changed_observer_list_( |
37 new ObserverListThreadSafe<DevicesChangedObserver>()), | 37 new ObserverListThreadSafe<DevicesChangedObserver>()), |
38 battery_in_use_(false), | 38 battery_in_use_(false), |
39 suspended_(false) { | 39 suspended_(false) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 NotifySuspend(); | 89 NotifySuspend(); |
90 } | 90 } |
91 break; | 91 break; |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { | 95 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { |
96 NotifyDevicesChanged(device_type); | 96 NotifyDevicesChanged(device_type); |
97 } | 97 } |
98 | 98 |
99 void SystemMonitor::ProcessMediaDeviceAttached( | 99 void SystemMonitor::ProcessRemovableStorageAttached( |
100 const std::string& id, | 100 const std::string& id, |
101 const string16& name, | 101 const string16& name, |
102 const FilePath::StringType& location) { | 102 const FilePath::StringType& location) { |
103 MediaDeviceInfo info(id, name, location); | 103 if (ContainsKey(removable_storage_map_, id)) { |
104 if (ContainsKey(media_device_map_, id)) { | |
105 // This can happen if our unique id scheme fails. Ignore the incoming | 104 // This can happen if our unique id scheme fails. Ignore the incoming |
106 // non-unique attachment. | 105 // non-unique attachment. |
107 return; | 106 return; |
108 } | 107 } |
109 media_device_map_.insert(std::make_pair(id, info)); | 108 RemovableStorageInfo info(id, name, location); |
110 NotifyMediaDeviceAttached(id, name, location); | 109 removable_storage_map_.insert(std::make_pair(id, info)); |
| 110 NotifyRemovableStorageAttached(id, name, location); |
111 } | 111 } |
112 | 112 |
113 void SystemMonitor::ProcessMediaDeviceDetached(const std::string& id) { | 113 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) { |
114 MediaDeviceMap::iterator it = media_device_map_.find(id); | 114 RemovableStorageMap::iterator it = removable_storage_map_.find(id); |
115 if (it == media_device_map_.end()) | 115 if (it == removable_storage_map_.end()) |
116 return; | 116 return; |
117 media_device_map_.erase(it); | 117 removable_storage_map_.erase(it); |
118 NotifyMediaDeviceDetached(id); | 118 NotifyRemovableStorageDetached(id); |
119 } | 119 } |
120 | 120 |
121 std::vector<SystemMonitor::MediaDeviceInfo> | 121 std::vector<SystemMonitor::RemovableStorageInfo> |
122 SystemMonitor::GetAttachedMediaDevices() const { | 122 SystemMonitor::GetAttachedRemovableStorage() const { |
123 std::vector<MediaDeviceInfo> results; | 123 std::vector<RemovableStorageInfo> results; |
124 for (MediaDeviceMap::const_iterator it = media_device_map_.begin(); | 124 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); |
125 it != media_device_map_.end(); | 125 it != removable_storage_map_.end(); |
126 ++it) { | 126 ++it) { |
127 results.push_back(it->second); | 127 results.push_back(it->second); |
128 } | 128 } |
129 return results; | 129 return results; |
130 } | 130 } |
131 | 131 |
132 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { | 132 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { |
133 power_observer_list_->AddObserver(obs); | 133 power_observer_list_->AddObserver(obs); |
134 } | 134 } |
135 | 135 |
136 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { | 136 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { |
137 power_observer_list_->RemoveObserver(obs); | 137 power_observer_list_->RemoveObserver(obs); |
138 } | 138 } |
139 | 139 |
140 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { | 140 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { |
141 devices_changed_observer_list_->AddObserver(obs); | 141 devices_changed_observer_list_->AddObserver(obs); |
142 } | 142 } |
143 | 143 |
144 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { | 144 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { |
145 devices_changed_observer_list_->RemoveObserver(obs); | 145 devices_changed_observer_list_->RemoveObserver(obs); |
146 } | 146 } |
147 | 147 |
148 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { | 148 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { |
149 DVLOG(1) << "DevicesChanged with device type " << device_type; | 149 DVLOG(1) << "DevicesChanged with device type " << device_type; |
150 devices_changed_observer_list_->Notify( | 150 devices_changed_observer_list_->Notify( |
151 &DevicesChangedObserver::OnDevicesChanged, device_type); | 151 &DevicesChangedObserver::OnDevicesChanged, device_type); |
152 } | 152 } |
153 | 153 |
154 void SystemMonitor::NotifyMediaDeviceAttached( | 154 void SystemMonitor::NotifyRemovableStorageAttached( |
155 const std::string& id, | 155 const std::string& id, |
156 const string16& name, | 156 const string16& name, |
157 const FilePath::StringType& location) { | 157 const FilePath::StringType& location) { |
158 DVLOG(1) << "MediaDeviceAttached with name " << UTF16ToUTF8(name) | 158 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name) |
159 << " and id " << id; | 159 << " and id " << id; |
160 devices_changed_observer_list_->Notify( | 160 devices_changed_observer_list_->Notify( |
161 &DevicesChangedObserver::OnMediaDeviceAttached, id, name, location); | 161 &DevicesChangedObserver::OnRemovableStorageAttached, id, name, location); |
162 } | 162 } |
163 | 163 |
164 void SystemMonitor::NotifyMediaDeviceDetached(const std::string& id) { | 164 void SystemMonitor::NotifyRemovableStorageDetached(const std::string& id) { |
165 DVLOG(1) << "MediaDeviceDetached for id " << id; | 165 DVLOG(1) << "RemovableStorageDetached for id " << id; |
166 devices_changed_observer_list_->Notify( | 166 devices_changed_observer_list_->Notify( |
167 &DevicesChangedObserver::OnMediaDeviceDetached, id); | 167 &DevicesChangedObserver::OnRemovableStorageDetached, id); |
168 } | 168 } |
169 | 169 |
170 void SystemMonitor::NotifyPowerStateChange() { | 170 void SystemMonitor::NotifyPowerStateChange() { |
171 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 171 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
172 << " battery"; | 172 << " battery"; |
173 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, | 173 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, |
174 BatteryPower()); | 174 BatteryPower()); |
175 } | 175 } |
176 | 176 |
177 void SystemMonitor::NotifySuspend() { | 177 void SystemMonitor::NotifySuspend() { |
178 DVLOG(1) << "Power Suspending"; | 178 DVLOG(1) << "Power Suspending"; |
179 power_observer_list_->Notify(&PowerObserver::OnSuspend); | 179 power_observer_list_->Notify(&PowerObserver::OnSuspend); |
180 } | 180 } |
181 | 181 |
182 void SystemMonitor::NotifyResume() { | 182 void SystemMonitor::NotifyResume() { |
183 DVLOG(1) << "Power Resuming"; | 183 DVLOG(1) << "Power Resuming"; |
184 power_observer_list_->Notify(&PowerObserver::OnResume); | 184 power_observer_list_->Notify(&PowerObserver::OnResume); |
185 } | 185 } |
186 | 186 |
187 void SystemMonitor::BatteryCheck() { | 187 void SystemMonitor::BatteryCheck() { |
188 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 188 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
189 } | 189 } |
190 | 190 |
191 } // namespace base | 191 } // namespace base |
OLD | NEW |