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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
// 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 "chrome/browser/policy/device_status_collector.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/chromeos/cros_settings.h"
#include "chrome/browser/chromeos/cros_settings_names.h"
#include "chrome/browser/chromeos/system/statistics_provider.h"
#include "chrome/browser/policy/proto/device_management_backend.pb.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
using base::Time;
using base::TimeDelta;
using chromeos::VersionLoader;
namespace em = enterprise_management;
namespace {
// How many seconds of inactivity triggers the idle state.
const unsigned int kIdleStateThresholdSeconds = 300;
// How many days in the past to store active periods for.
const unsigned int kMaxStoredPastActivityDays = 30;
// How many days in the future to store active periods for.
const unsigned int kMaxStoredFutureActivityDays = 2;
const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000;
// Record device activity for the specified day into the given dictionary.
void AddDeviceActivity(DictionaryValue* activity_times,
Time day_midnight,
TimeDelta activity) {
DCHECK(activity.InMilliseconds() < kMillisecondsPerDay);
int64 day_start_timestamp =
(day_midnight - Time::UnixEpoch()).InMilliseconds();
std::string day_key = base::Int64ToString(day_start_timestamp);
int previous_activity = 0;
activity_times->GetInteger(day_key, &previous_activity);
activity_times->SetInteger(day_key,
previous_activity + activity.InMilliseconds());
}
} // namespace
namespace policy {
DeviceStatusCollector::DeviceStatusCollector(
PrefService* local_state,
chromeos::system::StatisticsProvider* provider)
: max_stored_past_activity_days_(kMaxStoredPastActivityDays),
max_stored_future_activity_days_(kMaxStoredFutureActivityDays),
local_state_(local_state),
last_idle_check_(Time()),
statistics_provider_(provider),
report_version_info_(false),
report_activity_times_(false),
report_boot_mode_(false) {
timer_.Start(FROM_HERE,
TimeDelta::FromSeconds(kPollIntervalSeconds),
this, &DeviceStatusCollector::CheckIdleState);
cros_settings_ = chromeos::CrosSettings::Get();
// Watch for changes to the individual policies that control what the status
// reports contain.
cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this);
cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes,
this);
cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this);
// Fetch the current values of the policies.
UpdateReportingSettings();
// Get the the OS and firmware version info.
version_loader_.GetVersion(&consumer_,
base::Bind(&DeviceStatusCollector::OnOSVersion,
base::Unretained(this)),
VersionLoader::VERSION_FULL);
version_loader_.GetFirmware(&consumer_,
base::Bind(&DeviceStatusCollector::OnOSFirmware,
base::Unretained(this)));
}
DeviceStatusCollector::~DeviceStatusCollector() {
cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo,
this);
cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes,
this);
cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this);
}
// static
void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) {
local_state->RegisterDictionaryPref(prefs::kDeviceActivityTimes,
new DictionaryValue);
}
void DeviceStatusCollector::CheckIdleState() {
CalculateIdleState(kIdleStateThresholdSeconds,
base::Bind(&DeviceStatusCollector::IdleStateCallback,
base::Unretained(this)));
}
void DeviceStatusCollector::UpdateReportingSettings() {
// Attempt to fetch the current value of the reporting settings.
// If trusted values are not available, register this function to be called
// back when they are available.
bool is_trusted = cros_settings_->GetTrusted(
chromeos::kReportDeviceVersionInfo,
base::Bind(&DeviceStatusCollector::UpdateReportingSettings,
base::Unretained(this)));
if (is_trusted) {
cros_settings_->GetBoolean(
chromeos::kReportDeviceVersionInfo, &report_version_info_);
cros_settings_->GetBoolean(
chromeos::kReportDeviceActivityTimes, &report_activity_times_);
cros_settings_->GetBoolean(
chromeos::kReportDeviceBootMode, &report_boot_mode_);
}
}
Time DeviceStatusCollector::GetCurrentTime() {
return Time::Now();
}
// Remove all out-of-range activity times from the local store.
void DeviceStatusCollector::PruneStoredActivityPeriods(Time base_time) {
const DictionaryValue* activity_times =
local_state_->GetDictionary(prefs::kDeviceActivityTimes);
if (activity_times->size() <=
max_stored_past_activity_days_ + max_stored_future_activity_days_)
return;
Time min_time =
base_time - TimeDelta::FromDays(max_stored_past_activity_days_);
Time max_time =
base_time + TimeDelta::FromDays(max_stored_future_activity_days_);
const Time epoch = Time::UnixEpoch();
scoped_ptr<DictionaryValue> copy(activity_times->DeepCopy());
for (DictionaryValue::key_iterator it = activity_times->begin_keys();
it != activity_times->end_keys(); ++it) {
int64 timestamp;
if (base::StringToInt64(*it, ×tamp)) {
// Remove data that is too old, or too far in the future.
Time day_midnight = epoch + TimeDelta::FromMilliseconds(timestamp);
if (day_midnight > min_time && day_midnight < max_time)
continue;
}
// The entry is out of range or couldn't be parsed. Remove it.
copy->Remove(*it, NULL);
}
local_state_->Set(prefs::kDeviceActivityTimes, *copy);
}
void DeviceStatusCollector::AddActivePeriod(Time start, Time end) {
DCHECK(start < end);
// Maintain the list of active periods in a local_state pref.
DictionaryPrefUpdate update(local_state_, prefs::kDeviceActivityTimes);
DictionaryValue* activity_times = update.Get();
Time midnight = end.LocalMidnight();
// Figure out UTC midnight on the same day as the local day.
Time::Exploded exploded;
midnight.LocalExplode(&exploded);
Time utc_midnight = Time::FromUTCExploded(exploded);
// Record the device activity for today.
TimeDelta activity_today = end - MAX(midnight, start);
AddDeviceActivity(activity_times, utc_midnight, activity_today);
// If this interval spans two days, record activity for yesterday too.
if (start < midnight) {
AddDeviceActivity(activity_times,
utc_midnight - TimeDelta::FromDays(1),
midnight - start);
}
}
void DeviceStatusCollector::IdleStateCallback(IdleState state) {
// Do nothing if device activity reporting is disabled.
if (!report_activity_times_)
return;
Time now = GetCurrentTime();
if (state == IDLE_STATE_ACTIVE) {
// If it's been too long since the last report, or if the activity is
// negative (which can happen when the clock changes), assume a single
// interval of activity.
int active_seconds = (now - last_idle_check_).InSeconds();
if (active_seconds < 0 ||
active_seconds >= static_cast<int>((2 * kPollIntervalSeconds)))
AddActivePeriod(now - TimeDelta::FromSeconds(kPollIntervalSeconds), now);
else
AddActivePeriod(last_idle_check_, now);
PruneStoredActivityPeriods(now);
}
last_idle_check_ = now;
}
void DeviceStatusCollector::GetActivityTimes(
em::DeviceStatusReportRequest* request) {
DictionaryPrefUpdate update(local_state_, prefs::kDeviceActivityTimes);
DictionaryValue* activity_times = update.Get();
for (DictionaryValue::key_iterator it = activity_times->begin_keys();
it != activity_times->end_keys(); ++it) {
int64 start_timestamp;
int activity_milliseconds;
if (base::StringToInt64(*it, &start_timestamp) &&
activity_times->GetInteger(*it, &activity_milliseconds)) {
// This is correct even when there are leap seconds, because when a leap
// second occurs, two consecutive seconds have the same timestamp.
int64 end_timestamp = start_timestamp + kMillisecondsPerDay;
em::ActiveTimePeriod* active_period = request->add_active_period();
em::TimePeriod* period = active_period->mutable_time_period();
period->set_start_timestamp(start_timestamp);
period->set_end_timestamp(end_timestamp);
active_period->set_active_duration(activity_milliseconds);
} else {
NOTREACHED();
}
}
activity_times->Clear();
}
void DeviceStatusCollector::GetVersionInfo(
em::DeviceStatusReportRequest* request) {
chrome::VersionInfo version_info;
request->set_browser_version(version_info.Version());
request->set_os_version(os_version_);
request->set_firmware_version(firmware_version_);
}
void DeviceStatusCollector::GetBootMode(
em::DeviceStatusReportRequest* request) {
std::string dev_switch_mode;
if (statistics_provider_->GetMachineStatistic(
"devsw_boot", &dev_switch_mode)) {
if (dev_switch_mode == "1")
request->set_boot_mode("Dev");
else if (dev_switch_mode == "0")
request->set_boot_mode("Verified");
}
}
void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
if (report_activity_times_)
GetActivityTimes(request);
if (report_version_info_)
GetVersionInfo(request);
if (report_boot_mode_)
GetBootMode(request);
}
void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle,
std::string version) {
os_version_ = version;
}
void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle,
std::string version) {
firmware_version_ = version;
}
void DeviceStatusCollector::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED)
UpdateReportingSettings();
else
NOTREACHED();
}
} // namespace policy
|