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/power_monitor/power_monitor.h" |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/time.h" | |
13 #include "base/utf_string_conversions.h" | |
14 | 6 |
15 namespace base { | 7 namespace base { |
16 | 8 |
17 static SystemMonitor* g_system_monitor = NULL; | 9 static PowerMonitor* g_power_monitor = NULL; |
18 | 10 |
19 #if defined(ENABLE_BATTERY_MONITORING) | 11 #if defined(ENABLE_BATTERY_MONITORING) |
20 // The amount of time (in ms) to wait before running the initial | 12 // The amount of time (in ms) to wait before running the initial |
21 // battery check. | 13 // battery check. |
22 static int kDelayedBatteryCheckMs = 10 * 1000; | 14 static int kDelayedBatteryCheckMs = 10 * 1000; |
23 #endif // defined(ENABLE_BATTERY_MONITORING) | 15 #endif // defined(ENABLE_BATTERY_MONITORING) |
24 | 16 |
25 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() { | 17 PowerMonitor::PowerMonitor() |
26 } | 18 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
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() | |
38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | |
39 devices_changed_observer_list_( | |
40 new ObserverListThreadSafe<DevicesChangedObserver>()), | |
41 battery_in_use_(false), | 19 battery_in_use_(false), |
42 suspended_(false) { | 20 suspended_(false), |
43 DCHECK(!g_system_monitor); | 21 signaler_available_(true) { |
44 g_system_monitor = this; | 22 DCHECK(!g_power_monitor); |
23 g_power_monitor = this; | |
45 | 24 |
46 DCHECK(MessageLoop::current()); | 25 DCHECK(MessageLoop::current()); |
47 #if defined(ENABLE_BATTERY_MONITORING) | 26 #if defined(ENABLE_BATTERY_MONITORING) |
48 delayed_battery_check_.Start(FROM_HERE, | 27 delayed_battery_check_.Start(FROM_HERE, |
49 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 28 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
50 &SystemMonitor::BatteryCheck); | 29 &PowerMonitor::BatteryCheck); |
51 #endif // defined(ENABLE_BATTERY_MONITORING) | 30 #endif // defined(ENABLE_BATTERY_MONITORING) |
31 | |
52 #if defined(OS_MACOSX) | 32 #if defined(OS_MACOSX) |
53 PlatformInit(); | 33 PlatformInit(); |
54 #endif | 34 #endif |
55 } | 35 } |
56 | 36 |
57 SystemMonitor::~SystemMonitor() { | 37 PowerMonitor::~PowerMonitor() { |
58 #if defined(OS_MACOSX) | 38 #if defined(OS_MACOSX) |
59 PlatformDestroy(); | 39 PlatformDestroy(); |
60 #endif | 40 #endif |
61 DCHECK_EQ(this, g_system_monitor); | 41 DCHECK_EQ(this, g_power_monitor); |
62 g_system_monitor = NULL; | 42 g_power_monitor = NULL; |
63 } | 43 } |
64 | 44 |
65 // static | 45 // static |
66 SystemMonitor* SystemMonitor::Get() { | 46 PowerMonitor* PowerMonitor::Get() { |
67 return g_system_monitor; | 47 return g_power_monitor; |
68 } | 48 } |
69 | 49 |
70 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | 50 PowerMonitor::Signaler* PowerMonitor::GetSignalerOnce() { |
51 if (signaler_available_) { | |
willchan no longer on Chromium
2012/11/02 21:35:27
indentation off
Hongbo Min
2012/11/05 09:36:42
Done.
| |
52 signaler_available_ = false; | |
53 return new PowerMonitor::Signaler(); | |
54 } | |
55 | |
56 // Return NULL after the first time it get called. | |
57 return NULL; | |
58 } | |
59 | |
60 void PowerMonitor::AddObserver(PowerObserver* obs) { | |
61 observers_->AddObserver(obs); | |
62 } | |
63 | |
64 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | |
65 observers_->RemoveObserver(obs); | |
66 } | |
67 | |
68 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | |
71 // Suppress duplicate notifications. Some platforms may | 69 // Suppress duplicate notifications. Some platforms may |
72 // send multiple notifications of the same event. | 70 // send multiple notifications of the same event. |
73 switch (event_id) { | 71 switch (event_id) { |
74 case POWER_STATE_EVENT: | 72 case POWER_STATE_EVENT: |
75 { | 73 { |
76 bool on_battery = IsBatteryPower(); | 74 bool on_battery = IsBatteryPower(); |
77 if (on_battery != battery_in_use_) { | 75 if (on_battery != battery_in_use_) { |
78 battery_in_use_ = on_battery; | 76 battery_in_use_ = on_battery; |
79 NotifyPowerStateChange(); | 77 NotifyPowerStateChange(); |
80 } | 78 } |
81 } | 79 } |
82 break; | 80 break; |
83 case RESUME_EVENT: | 81 case RESUME_EVENT: |
84 if (suspended_) { | 82 if (suspended_) { |
85 suspended_ = false; | 83 suspended_ = false; |
86 NotifyResume(); | 84 NotifyResume(); |
87 } | 85 } |
88 break; | 86 break; |
89 case SUSPEND_EVENT: | 87 case SUSPEND_EVENT: |
90 if (!suspended_) { | 88 if (!suspended_) { |
91 suspended_ = true; | 89 suspended_ = true; |
92 NotifySuspend(); | 90 NotifySuspend(); |
93 } | 91 } |
94 break; | 92 break; |
95 } | 93 } |
96 } | 94 } |
97 | 95 |
98 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { | 96 void PowerMonitor::NotifyPowerStateChange() { |
99 NotifyDevicesChanged(device_type); | |
100 } | |
101 | |
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) { | |
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) { | |
152 devices_changed_observer_list_->AddObserver(obs); | |
153 } | |
154 | |
155 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { | |
156 devices_changed_observer_list_->RemoveObserver(obs); | |
157 } | |
158 | |
159 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { | |
160 DVLOG(1) << "DevicesChanged with device type " << device_type; | |
161 devices_changed_observer_list_->Notify( | |
162 &DevicesChangedObserver::OnDevicesChanged, device_type); | |
163 } | |
164 | |
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() { | |
182 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 97 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
183 << " battery"; | 98 << " battery"; |
184 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, | 99 observers_->Notify(&PowerObserver::OnPowerStateChange, |
185 BatteryPower()); | 100 BatteryPower()); |
186 } | 101 } |
187 | 102 |
188 void SystemMonitor::NotifySuspend() { | 103 void PowerMonitor::NotifySuspend() { |
189 DVLOG(1) << "Power Suspending"; | 104 DVLOG(1) << "Power Suspending"; |
190 power_observer_list_->Notify(&PowerObserver::OnSuspend); | 105 observers_->Notify(&PowerObserver::OnSuspend); |
191 } | 106 } |
192 | 107 |
193 void SystemMonitor::NotifyResume() { | 108 void PowerMonitor::NotifyResume() { |
194 DVLOG(1) << "Power Resuming"; | 109 DVLOG(1) << "Power Resuming"; |
195 power_observer_list_->Notify(&PowerObserver::OnResume); | 110 observers_->Notify(&PowerObserver::OnResume); |
196 } | 111 } |
197 | 112 |
198 void SystemMonitor::BatteryCheck() { | 113 void PowerMonitor::BatteryCheck() { |
199 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 114 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
115 } | |
116 | |
117 // PowerMonitor::Signaler | |
118 | |
119 PowerMonitor::Signaler::Signaler() { | |
120 } | |
121 | |
122 PowerMonitor::Signaler::~Signaler() { | |
123 } | |
124 | |
125 #if defined(OS_WIN) | |
126 void PowerMonitor::Signaler::ProcessWmPowerBroadcastMessage(int event) { | |
127 if (PowerMonitor::Get()) | |
128 PowerMonitor::Get()->ProcessWmPowerBroadcastMessage(event); | |
129 } | |
130 #endif | |
131 | |
132 void PowerMonitor::Signaler::ProcessPowerEvent(PowerEvent event) { | |
133 if (PowerMonitor::Get()) | |
134 PowerMonitor::Get()->ProcessPowerEvent(event); | |
200 } | 135 } |
201 | 136 |
202 } // namespace base | 137 } // namespace base |
138 | |
OLD | NEW |