OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/device_status_collector.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/task.h" | |
11 #include "base/time.h" | |
12 #include "chrome/browser/idle.h" | |
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
14 #include "chrome/browser/prefs/pref_service.h" | |
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
16 | |
17 using base::Time; | |
18 using base::TimeDelta; | |
19 | |
20 namespace em = enterprise_management; | |
21 | |
22 namespace { | |
23 // How many seconds of inactivity triggers the idle state. | |
24 const unsigned int kIdleStateThresholdSeconds = 300; | |
25 | |
26 // The maximum number of time periods stored in the local state. | |
27 const unsigned int kMaxStoredActivePeriods = 500; | |
28 | |
29 // Stores the baseline timestamp, to which the active periods are relative. | |
30 const char* const kPrefBaselineTime = "device_status.baseline_timestamp"; | |
31 | |
32 // Stores a list of timestamps representing device active periods. | |
33 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; | |
34 } | |
35 | |
36 namespace policy { | |
37 | |
38 DeviceStatusCollector::DeviceStatusCollector(PrefService* local_state) | |
39 : local_state_(local_state), | |
40 last_idle_check_(Time()), | |
41 last_idle_state_(IDLE_STATE_UNKNOWN) { | |
42 // Only start the timer if we have a message loop. The only time this should | |
43 // matter is in unit_tests. | |
44 if (MessageLoop::current()) { | |
45 timer_.Start(FROM_HERE, | |
46 TimeDelta::FromSeconds( | |
47 DeviceStatusCollector::kPollIntervalSeconds), | |
48 this, &DeviceStatusCollector::CheckIdleState); | |
49 } | |
50 } | |
51 | |
52 // static | |
53 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { | |
54 local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT()); | |
55 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); | |
56 } | |
57 | |
58 unsigned int DeviceStatusCollector::max_stored_active_periods() const { | |
59 return kMaxStoredActivePeriods; | |
60 } | |
61 | |
62 void DeviceStatusCollector::CheckIdleState() { | |
63 CalculateIdleState(kIdleStateThresholdSeconds, | |
64 base::Bind(&DeviceStatusCollector::IdleStateCallback, | |
65 base::Unretained(this))); | |
66 } | |
67 | |
68 bool DeviceStatusCollector::ToInternalTime(const Time& time, int& out_val) { | |
69 Time baseline = Time::FromTimeT( | |
70 local_state_->GetInt64(kPrefBaselineTime)); | |
71 int64 offset_millis = (time - baseline).InMilliseconds(); | |
72 // Fail if the time cannot be represented by an integer. | |
73 // Assuming 32-bit integers, this would only happen if we haven't uploaded | |
74 // status in more than 49 days. This should never happen, and if it does, | |
75 // it's reasonsable to stop recording the status. | |
Mattias Nissler (ping if slow)
2011/11/30 12:44:29
What if somebody is on vacation for 2 months and d
Patrick Dubroy
2011/12/06 14:30:36
This doesn't matter anymore -- the method has been
| |
76 if (offset_millis > static_cast<int64>(INT_MAX)) | |
77 return false; | |
78 out_val = (int)offset_millis; | |
Mattias Nissler (ping if slow)
2011/11/30 12:44:29
space after )
Patrick Dubroy
2011/12/06 14:30:36
Done.
| |
79 return true; | |
80 } | |
81 | |
82 Time DeviceStatusCollector::FromInternalTime(int internal_time) { | |
83 Time baseline = Time::FromTimeT( | |
84 local_state_->GetInt64(kPrefBaselineTime)); | |
85 return baseline + TimeDelta::FromMilliseconds(internal_time); | |
86 } | |
87 | |
88 Time DeviceStatusCollector::GetCurrentTime() { | |
89 return Time::Now(); | |
90 } | |
91 | |
92 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { | |
93 // Maintain the list of active periods in a local_state pref. | |
94 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | |
95 ListValue* active_periods = update.Get(); | |
96 | |
97 // Cap the number of active periods that we store. | |
98 if (active_periods->GetSize() >= 2 * max_stored_active_periods()) | |
99 return; | |
100 | |
101 int start_offset, end_offset; | |
102 if (!ToInternalTime(start, start_offset) || | |
103 !ToInternalTime(end, end_offset)) { | |
104 NOTREACHED(); | |
105 return; | |
106 } | |
107 | |
108 int list_size = active_periods->GetSize(); | |
109 DCHECK(list_size % 2 == 0); | |
110 | |
111 // Check if this period can be combined with the previous one. | |
112 if (list_size > 0 && last_idle_state_ == IDLE_STATE_ACTIVE) { | |
113 int last_period_end; | |
114 if (active_periods->GetInteger(list_size - 1, &last_period_end) && | |
115 last_period_end == start_offset) { | |
116 active_periods->Set(list_size - 1, | |
117 Value::CreateIntegerValue(end_offset)); | |
118 return; | |
119 } | |
120 } | |
121 // Add a new period to the list. | |
122 active_periods->Append(Value::CreateIntegerValue(start_offset)); | |
123 active_periods->Append(Value::CreateIntegerValue(end_offset)); | |
124 } | |
125 | |
126 void DeviceStatusCollector::IdleStateCallback(IdleState state) { | |
127 Time now = GetCurrentTime(); | |
128 | |
129 if (state == IDLE_STATE_ACTIVE) { | |
130 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; | |
131 | |
132 // If it's been too long since the last report, assume that the system was | |
133 // in standby, and only count a single interval of activity. | |
134 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) | |
135 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); | |
136 else | |
137 AddActivePeriod(last_idle_check_, now); | |
138 } | |
139 last_idle_check_ = now; | |
140 last_idle_state_ = state; | |
141 } | |
142 | |
143 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | |
144 const ListValue* active_periods = | |
145 local_state_->GetList(kPrefDeviceActivePeriods); | |
146 em::TimePeriod* time_period; | |
147 | |
148 DCHECK(active_periods->GetSize() % 2 == 0); | |
149 | |
150 int period_count = active_periods->GetSize() / 2; | |
151 for (int i = 0; i < period_count; i++) { | |
152 int start, end; | |
153 | |
154 if (!active_periods->GetInteger(2 * i, &start) || | |
155 !active_periods->GetInteger(2 * i + 1, &end) || | |
156 end < start) { | |
157 // Something is amiss -- bail out. | |
158 NOTREACHED(); | |
159 break; | |
160 } | |
161 time_period = request->add_active_time(); | |
162 time_period->set_start_timestamp( | |
163 (FromInternalTime(start) - Time::UnixEpoch()).InMilliseconds()); | |
164 time_period->set_end_timestamp( | |
165 (FromInternalTime(end) - Time::UnixEpoch()).InMilliseconds()); | |
166 } | |
167 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | |
168 update.Get()->Clear(); | |
169 | |
170 // Reset the baseline time every time we report. If this isn't done | |
171 // periodically, the times will overflow. | |
Mattias Nissler (ping if slow)
2011/11/30 12:44:29
It seems likes maintaining the baseline time makes
Patrick Dubroy
2011/12/06 14:30:36
You're right. It's simpler to store as strings. Do
| |
172 local_state_->SetInt64(kPrefBaselineTime, | |
173 GetCurrentTime().ToTimeT()); | |
174 } | |
175 | |
176 } // namespace policy | |
OLD | NEW |