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 "chrome/browser/policy/device_status_collector.h" | 5 #include "chrome/browser/policy/device_status_collector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "chrome/browser/chromeos/cros_settings.h" | 10 #include "chrome/browser/chromeos/cros_settings.h" |
11 #include "chrome/browser/chromeos/cros_settings_names.h" | 11 #include "chrome/browser/chromeos/cros_settings_names.h" |
12 #include "chrome/browser/chromeos/system/statistics_provider.h" | 12 #include "chrome/browser/chromeos/system/statistics_provider.h" |
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_version_info.h" | 17 #include "chrome/common/chrome_version_info.h" |
18 | 18 |
19 using base::Time; | 19 using base::Time; |
20 using base::TimeDelta; | 20 using base::TimeDelta; |
21 using chromeos::VersionLoader; | 21 using chromeos::VersionLoader; |
22 | 22 |
23 namespace em = enterprise_management; | 23 namespace em = enterprise_management; |
24 | 24 |
25 namespace { | 25 namespace { |
26 // How many seconds of inactivity triggers the idle state. | 26 // How many seconds of inactivity triggers the idle state. |
27 const unsigned int kIdleStateThresholdSeconds = 300; | 27 const unsigned int kIdleStateThresholdSeconds = 300; |
28 | 28 |
29 // The maximum number of time periods stored in the local state. | 29 // How many days in the past to store active periods for. |
30 const unsigned int kMaxStoredActivePeriods = 500; | 30 const unsigned int kMaxStoredPastActivityDays = 30; |
31 | 31 |
32 // Stores a list of timestamps representing device active periods. | 32 // How many days in the future to store active periods for. |
33 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; | 33 const unsigned int kMaxStoredFutureActivityDays = 2; |
34 | 34 |
35 bool GetTimestamp(const ListValue* list, int index, int64* out_value) { | 35 const char* const kPrefDeviceActivityTimes = "device_status.activity_times"; |
36 std::string string_value; | 36 |
37 if (list->GetString(index, &string_value)) | 37 // Record device activity for the specified day into the given dictionary. |
38 return base::StringToInt64(string_value, out_value); | 38 void AddDeviceActivity( |
39 return false; | 39 DictionaryValue* activity_times, Time day_midnight, TimeDelta activity) { |
Mattias Nissler (ping if slow)
2012/02/15 14:56:21
If you break the declaration, you need to put all
Patrick Dubroy
2012/02/15 15:37:25
Done.
| |
40 DCHECK(activity < TimeDelta::FromDays(1)); | |
41 int64 day_start_timestamp = | |
42 (day_midnight - Time::UnixEpoch()).InMilliseconds(); | |
Mattias Nissler (ping if slow)
2012/02/15 14:56:21
indentation
Patrick Dubroy
2012/02/15 15:37:25
Done.
| |
43 std::string day_key = base::Int64ToString(day_start_timestamp); | |
44 int previous_activity = 0; | |
45 activity_times->GetInteger(day_key, &previous_activity); | |
46 activity_times->SetInteger(day_key, | |
47 previous_activity + activity.InMilliseconds()); | |
40 } | 48 } |
41 | 49 |
Mattias Nissler (ping if slow)
2012/02/15 14:56:21
remove extra blank line
Patrick Dubroy
2012/02/15 15:37:25
Done.
| |
50 | |
42 } // namespace | 51 } // namespace |
43 | 52 |
44 namespace policy { | 53 namespace policy { |
45 | 54 |
46 DeviceStatusCollector::DeviceStatusCollector( | 55 DeviceStatusCollector::DeviceStatusCollector( |
47 PrefService* local_state, | 56 PrefService* local_state, |
48 chromeos::system::StatisticsProvider* provider) | 57 chromeos::system::StatisticsProvider* provider) |
49 : max_stored_active_periods_(kMaxStoredActivePeriods), | 58 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), |
59 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), | |
50 local_state_(local_state), | 60 local_state_(local_state), |
51 last_idle_check_(Time()), | 61 last_idle_check_(Time()), |
52 last_idle_state_(IDLE_STATE_UNKNOWN), | |
53 statistics_provider_(provider), | 62 statistics_provider_(provider), |
54 report_version_info_(false), | 63 report_version_info_(false), |
55 report_activity_times_(false), | 64 report_activity_times_(false), |
56 report_boot_mode_(false) { | 65 report_boot_mode_(false) { |
57 timer_.Start(FROM_HERE, | 66 timer_.Start(FROM_HERE, |
58 TimeDelta::FromSeconds( | 67 TimeDelta::FromSeconds(kPollIntervalSeconds), |
59 DeviceStatusCollector::kPollIntervalSeconds), | |
60 this, &DeviceStatusCollector::CheckIdleState); | 68 this, &DeviceStatusCollector::CheckIdleState); |
61 | 69 |
62 cros_settings_ = chromeos::CrosSettings::Get(); | 70 cros_settings_ = chromeos::CrosSettings::Get(); |
63 | 71 |
64 // Watch for changes to the individual policies that control what the status | 72 // Watch for changes to the individual policies that control what the status |
65 // reports contain. | 73 // reports contain. |
66 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); | 74 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); |
67 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, | 75 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, |
68 this); | 76 this); |
69 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); | 77 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); |
(...skipping 14 matching lines...) Expand all Loading... | |
84 DeviceStatusCollector::~DeviceStatusCollector() { | 92 DeviceStatusCollector::~DeviceStatusCollector() { |
85 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, | 93 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, |
86 this); | 94 this); |
87 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, | 95 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, |
88 this); | 96 this); |
89 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); | 97 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); |
90 } | 98 } |
91 | 99 |
92 // static | 100 // static |
93 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { | 101 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { |
94 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); | 102 local_state->RegisterDictionaryPref( |
103 kPrefDeviceActivityTimes, new DictionaryValue); | |
95 } | 104 } |
96 | 105 |
97 void DeviceStatusCollector::CheckIdleState() { | 106 void DeviceStatusCollector::CheckIdleState() { |
98 CalculateIdleState(kIdleStateThresholdSeconds, | 107 CalculateIdleState(kIdleStateThresholdSeconds, |
99 base::Bind(&DeviceStatusCollector::IdleStateCallback, | 108 base::Bind(&DeviceStatusCollector::IdleStateCallback, |
100 base::Unretained(this))); | 109 base::Unretained(this))); |
101 } | 110 } |
102 | 111 |
103 void DeviceStatusCollector::UpdateReportingSettings() { | 112 void DeviceStatusCollector::UpdateReportingSettings() { |
104 // Attempt to fetch the current value of the reporting settings. | 113 // Attempt to fetch the current value of the reporting settings. |
(...skipping 10 matching lines...) Expand all Loading... | |
115 chromeos::kReportDeviceActivityTimes, &report_activity_times_); | 124 chromeos::kReportDeviceActivityTimes, &report_activity_times_); |
116 cros_settings_->GetBoolean( | 125 cros_settings_->GetBoolean( |
117 chromeos::kReportDeviceBootMode, &report_boot_mode_); | 126 chromeos::kReportDeviceBootMode, &report_boot_mode_); |
118 } | 127 } |
119 } | 128 } |
120 | 129 |
121 Time DeviceStatusCollector::GetCurrentTime() { | 130 Time DeviceStatusCollector::GetCurrentTime() { |
122 return Time::Now(); | 131 return Time::Now(); |
123 } | 132 } |
124 | 133 |
125 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { | 134 // Remove all out-of-range activity times from the local store. |
126 // Maintain the list of active periods in a local_state pref. | 135 void DeviceStatusCollector::PruneStoredActivityPeriods(Time base_time) { |
127 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | 136 const DictionaryValue* activity_times = |
128 ListValue* active_periods = update.Get(); | 137 local_state_->GetDictionary(kPrefDeviceActivityTimes); |
129 | 138 if (activity_times->size() <= |
130 // Cap the number of active periods that we store. | 139 max_stored_past_activity_days_ + max_stored_future_activity_days_) |
131 if (active_periods->GetSize() >= 2 * max_stored_active_periods_) | |
132 return; | 140 return; |
133 | 141 |
142 Time min_time = | |
143 base_time - TimeDelta::FromDays(max_stored_past_activity_days_); | |
144 Time max_time = | |
145 base_time + TimeDelta::FromDays(max_stored_future_activity_days_); | |
134 Time epoch = Time::UnixEpoch(); | 146 Time epoch = Time::UnixEpoch(); |
Mattias Nissler (ping if slow)
2012/02/15 14:56:21
declare const.
Patrick Dubroy
2012/02/15 15:37:25
Done.
| |
135 int64 start_timestamp = (start - epoch).InMilliseconds(); | |
136 Value* end_value = new StringValue( | |
137 base::Int64ToString((end - epoch).InMilliseconds())); | |
138 | 147 |
139 int list_size = active_periods->GetSize(); | 148 DictionaryValue* copy = activity_times->DeepCopy(); |
140 DCHECK(list_size % 2 == 0); | 149 for (DictionaryValue::key_iterator it = activity_times->begin_keys(); |
150 it != activity_times->end_keys(); ++it) { | |
151 int64 timestamp; | |
141 | 152 |
142 // Check if this period can be combined with the previous one. | 153 if (base::StringToInt64(*it, ×tamp)) { |
Mattias Nissler (ping if slow)
2012/02/15 14:56:21
We should probably also remove it if we cannot par
Patrick Dubroy
2012/02/15 15:37:25
Done.
| |
143 if (list_size > 0 && last_idle_state_ == IDLE_STATE_ACTIVE) { | 154 // Remove data that is too old, or too far in the future. |
144 int64 last_period_end; | 155 Time day_midnight = epoch + TimeDelta::FromMilliseconds(timestamp); |
145 if (GetTimestamp(active_periods, list_size - 1, &last_period_end) && | 156 if (day_midnight < min_time || day_midnight > max_time) |
146 last_period_end == start_timestamp) { | 157 copy->Remove(*it, NULL); |
147 active_periods->Set(list_size - 1, end_value); | |
148 return; | |
149 } | 158 } |
150 } | 159 } |
151 // Add a new period to the list. | 160 local_state_->Set(kPrefDeviceActivityTimes, *copy); |
152 active_periods->Append( | 161 } |
153 new StringValue(base::Int64ToString(start_timestamp))); | 162 |
154 active_periods->Append(end_value); | 163 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { |
164 DCHECK(start < end); | |
165 | |
166 // Maintain the list of active periods in a local_state pref. | |
167 DictionaryPrefUpdate update(local_state_, kPrefDeviceActivityTimes); | |
168 DictionaryValue* activity_times = update.Get(); | |
169 | |
170 Time midnight = end.LocalMidnight(); | |
171 | |
172 // Figure out UTC midnight on the same day as the local day. | |
173 Time::Exploded exploded; | |
174 midnight.LocalExplode(&exploded); | |
175 Time utc_midnight = Time::FromUTCExploded(exploded); | |
176 | |
177 // Record the device activity for today. | |
178 TimeDelta activity_today = end - MAX(midnight, start); | |
179 AddDeviceActivity(activity_times, utc_midnight, activity_today); | |
180 | |
181 // If this interval spans two days, record activity for yesterday too. | |
182 if (start < midnight) { | |
183 AddDeviceActivity(activity_times, | |
184 utc_midnight - TimeDelta::FromDays(1), | |
185 midnight - start); | |
186 } | |
155 } | 187 } |
156 | 188 |
157 void DeviceStatusCollector::IdleStateCallback(IdleState state) { | 189 void DeviceStatusCollector::IdleStateCallback(IdleState state) { |
158 // Do nothing if device activity reporting is disabled. | 190 // Do nothing if device activity reporting is disabled. |
159 if (!report_activity_times_) | 191 if (!report_activity_times_) |
160 return; | 192 return; |
161 | 193 |
162 Time now = GetCurrentTime(); | 194 Time now = GetCurrentTime(); |
163 | 195 |
164 if (state == IDLE_STATE_ACTIVE) { | 196 if (state == IDLE_STATE_ACTIVE) { |
165 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; | 197 // If it's been too long since the last report, or if the activity is |
166 | 198 // negative (which can happen when the clock changes), assume a single |
167 // If it's been too long since the last report, assume that the system was | 199 // interval of activity. |
168 // in standby, and only count a single interval of activity. | 200 unsigned int active_seconds = (now - last_idle_check_).InSeconds(); |
169 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) | 201 if (active_seconds < 0 || active_seconds >= (2 * kPollIntervalSeconds)) |
170 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); | 202 AddActivePeriod(now - TimeDelta::FromSeconds(kPollIntervalSeconds), now); |
171 else | 203 else |
172 AddActivePeriod(last_idle_check_, now); | 204 AddActivePeriod(last_idle_check_, now); |
205 | |
206 PruneStoredActivityPeriods(now); | |
173 } | 207 } |
174 last_idle_check_ = now; | 208 last_idle_check_ = now; |
175 last_idle_state_ = state; | |
176 } | 209 } |
177 | 210 |
178 void DeviceStatusCollector::GetActivityTimes( | 211 void DeviceStatusCollector::GetActivityTimes( |
179 em::DeviceStatusReportRequest* request) { | 212 em::DeviceStatusReportRequest* request) { |
180 const ListValue* active_periods = | 213 DictionaryPrefUpdate update(local_state_, kPrefDeviceActivityTimes); |
181 local_state_->GetList(kPrefDeviceActivePeriods); | 214 DictionaryValue* activity_times = update.Get(); |
182 em::TimePeriod* time_period; | 215 int64 milliseconds_per_day = TimeDelta::FromDays(1).InMilliseconds(); |
Mattias Nissler (ping if slow)
2012/02/15 14:56:21
declare const
Patrick Dubroy
2012/02/15 15:37:25
Done.
| |
183 | 216 |
184 DCHECK(active_periods->GetSize() % 2 == 0); | 217 for (DictionaryValue::key_iterator it = activity_times->begin_keys(); |
185 | 218 it != activity_times->end_keys(); ++it) { |
186 int period_count = active_periods->GetSize() / 2; | 219 int64 start_timestamp; |
187 for (int i = 0; i < period_count; i++) { | 220 int activity_milliseconds; |
188 int64 start, end; | 221 if (base::StringToInt64(*it, &start_timestamp) && |
189 | 222 activity_times->GetInteger(*it, &activity_milliseconds)) { |
190 if (!GetTimestamp(active_periods, 2 * i, &start) || | 223 em::ActiveTimePeriod* active_period = request->add_active_period(); |
191 !GetTimestamp(active_periods, 2 * i + 1, &end) || | 224 em::TimePeriod* period = active_period->mutable_time_period(); |
192 end < start) { | 225 period->set_start_timestamp(start_timestamp); |
193 // Something is amiss -- bail out. | 226 period->set_end_timestamp(start_timestamp + milliseconds_per_day); |
227 active_period->set_active_duration(activity_milliseconds); | |
228 } else { | |
194 NOTREACHED(); | 229 NOTREACHED(); |
195 break; | |
196 } | 230 } |
197 time_period = request->add_active_time(); | |
198 time_period->set_start_timestamp(start); | |
199 time_period->set_end_timestamp(end); | |
200 } | 231 } |
201 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | 232 activity_times->Clear(); |
202 update.Get()->Clear(); | |
203 } | 233 } |
204 | 234 |
205 void DeviceStatusCollector::GetVersionInfo( | 235 void DeviceStatusCollector::GetVersionInfo( |
206 em::DeviceStatusReportRequest* request) { | 236 em::DeviceStatusReportRequest* request) { |
207 chrome::VersionInfo version_info; | 237 chrome::VersionInfo version_info; |
208 request->set_browser_version(version_info.Version()); | 238 request->set_browser_version(version_info.Version()); |
209 request->set_os_version(os_version_); | 239 request->set_os_version(os_version_); |
210 request->set_firmware_version(firmware_version_); | 240 request->set_firmware_version(firmware_version_); |
211 } | 241 } |
212 | 242 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 int type, | 277 int type, |
248 const content::NotificationSource& source, | 278 const content::NotificationSource& source, |
249 const content::NotificationDetails& details) { | 279 const content::NotificationDetails& details) { |
250 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) | 280 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED) |
251 UpdateReportingSettings(); | 281 UpdateReportingSettings(); |
252 else | 282 else |
253 NOTREACHED(); | 283 NOTREACHED(); |
254 } | 284 } |
255 | 285 |
256 } // namespace policy | 286 } // namespace policy |
OLD | NEW |