1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/system_monitor/system_monitor.h"
#include <utility>
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/stl_util.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
namespace base {
static SystemMonitor* g_system_monitor = NULL;
#if defined(ENABLE_BATTERY_MONITORING)
// The amount of time (in ms) to wait before running the initial
// battery check.
static int kDelayedBatteryCheckMs = 10 * 1000;
#endif // defined(ENABLE_BATTERY_MONITORING)
SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() {
}
SystemMonitor::RemovableStorageInfo::RemovableStorageInfo(
const std::string& id,
const string16& device_name,
const FilePath::StringType& device_location)
: device_id(id),
name(device_name),
location(device_location) {
}
SystemMonitor::SystemMonitor()
: power_observer_list_(new ObserverListThreadSafe<PowerObserver>()),
devices_changed_observer_list_(
new ObserverListThreadSafe<DevicesChangedObserver>()),
battery_in_use_(false),
suspended_(false) {
DCHECK(!g_system_monitor);
g_system_monitor = this;
DCHECK(MessageLoop::current());
#if defined(ENABLE_BATTERY_MONITORING)
delayed_battery_check_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
&SystemMonitor::BatteryCheck);
#endif // defined(ENABLE_BATTERY_MONITORING)
#if defined(OS_MACOSX)
PlatformInit();
#endif
}
SystemMonitor::~SystemMonitor() {
#if defined(OS_MACOSX)
PlatformDestroy();
#endif
DCHECK_EQ(this, g_system_monitor);
g_system_monitor = NULL;
}
// static
SystemMonitor* SystemMonitor::Get() {
return g_system_monitor;
}
void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
// Suppress duplicate notifications. Some platforms may
// send multiple notifications of the same event.
switch (event_id) {
case POWER_STATE_EVENT:
{
bool on_battery = IsBatteryPower();
if (on_battery != battery_in_use_) {
battery_in_use_ = on_battery;
NotifyPowerStateChange();
}
}
break;
case RESUME_EVENT:
if (suspended_) {
suspended_ = false;
NotifyResume();
}
break;
case SUSPEND_EVENT:
if (!suspended_) {
suspended_ = true;
NotifySuspend();
}
break;
}
}
void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) {
NotifyDevicesChanged(device_type);
}
void SystemMonitor::ProcessRemovableStorageAttached(
const std::string& id,
const string16& name,
const FilePath::StringType& location) {
{
base::AutoLock lock(removable_storage_lock_);
if (ContainsKey(removable_storage_map_, id)) {
// This can happen if our unique id scheme fails. Ignore the incoming
// non-unique attachment.
return;
}
RemovableStorageInfo info(id, name, location);
removable_storage_map_.insert(std::make_pair(id, info));
}
NotifyRemovableStorageAttached(id, name, location);
}
void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) {
{
base::AutoLock lock(removable_storage_lock_);
RemovableStorageMap::iterator it = removable_storage_map_.find(id);
if (it == removable_storage_map_.end())
return;
removable_storage_map_.erase(it);
}
NotifyRemovableStorageDetached(id);
}
std::vector<SystemMonitor::RemovableStorageInfo>
SystemMonitor::GetAttachedRemovableStorage() const {
std::vector<RemovableStorageInfo> results;
base::AutoLock lock(removable_storage_lock_);
for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin();
it != removable_storage_map_.end();
++it) {
results.push_back(it->second);
}
return results;
}
void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
power_observer_list_->AddObserver(obs);
}
void SystemMonitor::RemovePowerObserver(PowerObserver* obs) {
power_observer_list_->RemoveObserver(obs);
}
void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) {
devices_changed_observer_list_->AddObserver(obs);
}
void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
devices_changed_observer_list_->RemoveObserver(obs);
}
void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
DVLOG(1) << "DevicesChanged with device type " << device_type;
devices_changed_observer_list_->Notify(
&DevicesChangedObserver::OnDevicesChanged, device_type);
}
void SystemMonitor::NotifyRemovableStorageAttached(
const std::string& id,
const string16& name,
const FilePath::StringType& location) {
DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name)
<< " and id " << id;
devices_changed_observer_list_->Notify(
&DevicesChangedObserver::OnRemovableStorageAttached, id, name, location);
}
void SystemMonitor::NotifyRemovableStorageDetached(const std::string& id) {
DVLOG(1) << "RemovableStorageDetached for id " << id;
devices_changed_observer_list_->Notify(
&DevicesChangedObserver::OnRemovableStorageDetached, id);
}
void SystemMonitor::NotifyPowerStateChange() {
DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off")
<< " battery";
power_observer_list_->Notify(&PowerObserver::OnPowerStateChange,
BatteryPower());
}
void SystemMonitor::NotifySuspend() {
DVLOG(1) << "Power Suspending";
power_observer_list_->Notify(&PowerObserver::OnSuspend);
}
void SystemMonitor::NotifyResume() {
DVLOG(1) << "Power Resuming";
power_observer_list_->Notify(&PowerObserver::OnResume);
}
void SystemMonitor::BatteryCheck() {
ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
}
} // namespace base
|