Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/policy/device_status_collector.cc

Issue 8702009: Add device status reports to policy requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix one issue I missed. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/string_number_conversions.h"
11 #include "base/task.h"
12 #include "base/time.h"
13 #include "chrome/browser/idle.h"
14 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
15 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/prefs/scoped_user_pref_update.h"
17
18 using base::Time;
19 using base::TimeDelta;
20
21 namespace em = enterprise_management;
22
23 namespace {
24 // How many seconds of inactivity triggers the idle state.
25 const unsigned int kIdleStateThresholdSeconds = 300;
26
27 // The maximum number of time periods stored in the local state.
28 const unsigned int kMaxStoredActivePeriods = 500;
29
30 // Stores the baseline timestamp, to which the active periods are relative.
31 const char* const kPrefBaselineTime = "device_status.baseline_timestamp";
32
33 // Stores a list of timestamps representing device active periods.
34 const char* const kPrefDeviceActivePeriods = "device_status.active_periods";
35
36 bool GetTimestamp(const ListValue* list, int index, int64* out_value) {
37 std::string string_value;
38 if (list->GetString(index, &string_value))
39 return base::StringToInt64(string_value, out_value);
40 return false;
41 }
42
43 } // namespace
44
45 namespace policy {
46
47 DeviceStatusCollector::DeviceStatusCollector(PrefService* local_state)
48 : max_stored_active_periods_(kMaxStoredActivePeriods),
49 local_state_(local_state),
50 last_idle_check_(Time()),
51 last_idle_state_(IDLE_STATE_UNKNOWN) {
52 // Only start the timer if we have a message loop. The only time this should
53 // matter is in unit_tests.
54 if (MessageLoop::current()) {
Mattias Nissler (ping if slow) 2011/12/06 15:20:18 That's a poor way of detecting whether this is a u
Patrick Dubroy 2011/12/06 16:41:57 Done.
55 timer_.Start(FROM_HERE,
56 TimeDelta::FromSeconds(
57 DeviceStatusCollector::kPollIntervalSeconds),
58 this, &DeviceStatusCollector::CheckIdleState);
59 }
60 }
61
62 // static
63 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) {
64 local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT());
65 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue);
66 }
67
68 void DeviceStatusCollector::CheckIdleState() {
69 CalculateIdleState(kIdleStateThresholdSeconds,
70 base::Bind(&DeviceStatusCollector::IdleStateCallback,
71 base::Unretained(this)));
72 }
73
74 Time DeviceStatusCollector::GetCurrentTime() {
75 return Time::Now();
76 }
77
78 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) {
79 // Maintain the list of active periods in a local_state pref.
80 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
81 ListValue* active_periods = update.Get();
82
83 // Cap the number of active periods that we store.
84 if (active_periods->GetSize() >= 2 * max_stored_active_periods_)
85 return;
86
87 Time epoch = Time::UnixEpoch();
88 int64 start_timestamp = (start - epoch).InMilliseconds();
89 Value* end_value = new StringValue(
90 base::Int64ToString((end - epoch).InMilliseconds()));
91
92 int list_size = active_periods->GetSize();
93 DCHECK(list_size % 2 == 0);
94
95 // Check if this period can be combined with the previous one.
96 if (list_size > 0 && last_idle_state_ == IDLE_STATE_ACTIVE) {
97 int64 last_period_end;
98 if (GetTimestamp(active_periods, list_size - 1, &last_period_end) &&
99 last_period_end == start_timestamp) {
100 active_periods->Set(list_size - 1, end_value);
101 return;
102 }
103 }
104 // Add a new period to the list.
105 active_periods->Append(
106 new StringValue(base::Int64ToString(start_timestamp)));
107 active_periods->Append(end_value);
108 }
109
110 void DeviceStatusCollector::IdleStateCallback(IdleState state) {
111 Time now = GetCurrentTime();
112
113 if (state == IDLE_STATE_ACTIVE) {
114 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds;
115
116 // If it's been too long since the last report, assume that the system was
117 // in standby, and only count a single interval of activity.
118 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval))
119 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now);
120 else
121 AddActivePeriod(last_idle_check_, now);
122 }
123 last_idle_check_ = now;
124 last_idle_state_ = state;
125 }
126
127 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
128 const ListValue* active_periods =
129 local_state_->GetList(kPrefDeviceActivePeriods);
130 em::TimePeriod* time_period;
131
132 DCHECK(active_periods->GetSize() % 2 == 0);
133
134 int period_count = active_periods->GetSize() / 2;
135 for (int i = 0; i < period_count; i++) {
136 int64 start, end;
137
138 if (!GetTimestamp(active_periods, 2 * i, &start) ||
139 !GetTimestamp(active_periods, 2 * i + 1, &end) ||
140 end < start) {
141 // Something is amiss -- bail out.
142 NOTREACHED();
143 break;
144 }
145 time_period = request->add_active_time();
146 time_period->set_start_timestamp(start);
147 time_period->set_end_timestamp(end);
148 }
149 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
150 update.Get()->Clear();
151 }
152
153 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698