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

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

Issue 8702009: Add device status reports to policy requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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_reporter.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 "chrome/browser/idle.h"
12 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/prefs/scoped_user_pref_update.h"
15
16 namespace em = enterprise_management;
17
18 namespace {
19 // How many seconds of inactivity triggers the idle state.
20 const unsigned int kIdleStateThresholdSeconds = 300;
Mattias Nissler (ping if slow) 2011/11/25 14:15:19 no indentation
Patrick Dubroy 2011/11/29 18:01:46 Done.
21
22 // How often, in seconds, to poll to see if the user is idle.
23 const unsigned int kDefaultPollIntervalSeconds = 30;
24
25 // Stores the baseline timestamp, to which the active periods are relative.
26 const char* const kPrefBaselineTime = "device_status.baseline_timestamp";
27
28 // Stores a list of timestamps representing device active periods.
29 const char* const kPrefDeviceActivePeriods = "device_status.active_periods";
30 }
31
32 namespace policy {
33
34 DeviceStatusReporter::DeviceStatusReporter(PrefService* local_state)
35 : poll_interval_seconds_(kDefaultPollIntervalSeconds),
36 local_state_(local_state),
37 last_idle_check_(base::Time::Now()),
38 last_idle_state_(IDLE_STATE_UNKNOWN) {
39 local_state_->RegisterInt64Pref(kPrefBaselineTime,
40 base::Time::Now().ToTimeT());
41 local_state_->RegisterListPref(kPrefDeviceActivePeriods, new ListValue);
42 CheckIdleState();
43 }
44
45 void DeviceStatusReporter::CheckIdleState() {
46 CalculateIdleState(kIdleStateThresholdSeconds,
47 base::Bind(&DeviceStatusReporter::IdleStateCallback,
48 base::Unretained(this)));
49 }
50
51 int DeviceStatusReporter::ToInternalTime(const base::Time& time) {
52 base::Time baseline = base::Time::FromTimeT(
53 local_state_->GetInt64(kPrefBaselineTime));
54 return (int)(time - baseline).InSeconds();
55 }
56
57 base::Time DeviceStatusReporter::FromInternalTime(int internal_time) {
58 base::Time baseline = base::Time::FromTimeT(
59 local_state_->GetInt64(kPrefBaselineTime));
60 return baseline + base::TimeDelta::FromSeconds(internal_time);
61 }
62
63 void DeviceStatusReporter::AddActivePeriod(base::Time start, base::Time end) {
64 // Maintain the list of active periods in a local_state pref.
65 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
66 ListValue* active_periods = update.Get();
67
68 int start_offset = ToInternalTime(start);
69 int end_offset = ToInternalTime(end);
70
71 int list_size = active_periods->GetSize();
72 DCHECK(list_size % 2 == 0);
73
74 // Check if this period can be combined with the previous one.
75 if (list_size > 0 && last_idle_state_ == IDLE_STATE_ACTIVE) {
76 int last_period_end;
77 if (active_periods->GetInteger(list_size - 1, &last_period_end) &&
78 last_period_end == start_offset) {
79 active_periods->Set(list_size - 1,
80 Value::CreateIntegerValue(end_offset));
81 return;
82 }
83 }
84 // Add a new period to the list.
85 active_periods->Append(Value::CreateIntegerValue(start_offset));
86 active_periods->Append(Value::CreateIntegerValue(end_offset));
Mattias Nissler (ping if slow) 2011/11/25 14:15:19 we should probably limit the maximum number of ent
87 }
88
89 void DeviceStatusReporter::IdleStateCallback(IdleState state) {
90 base::Time now = base::Time::Now();
91
92 if (state == IDLE_STATE_ACTIVE) {
93 base::Time period_start = last_idle_check_;
94
95 // If it's been too long since the last report, assume that the system was
96 // in standby, and only count a single interval of activity.
97 if ((now - period_start).InSeconds() >= (2 * poll_interval_seconds_)) {
98 period_start =
99 now - base::TimeDelta::FromSeconds(poll_interval_seconds_);
100 }
101
102 AddActivePeriod(period_start, now);
103 }
104 last_idle_check_ = now;
105 last_idle_state_ = state;
106
107 MessageLoop::current()->PostDelayedTask(
Mattias Nissler (ping if slow) 2011/11/25 14:15:19 You might want to use base/timer.h instead, which
Patrick Dubroy 2011/11/29 18:01:46 Done.
108 FROM_HERE,
109 base::Bind(&DeviceStatusReporter::CheckIdleState,
110 base::Unretained(this)),
111 poll_interval_seconds_ * 1000);
112 }
113
114 void DeviceStatusReporter::GetStatus(em::DeviceStatusReportRequest* request) {
115 const ListValue* active_periods =
116 local_state_->GetList(kPrefDeviceActivePeriods);
117 em::TimePeriod* time_period;
118
119 DCHECK(active_periods->GetSize() % 2 == 0);
120
121 int period_count = active_periods->GetSize() / 2;
122 for (int i = 0; i < period_count; i++) {
123 int start, end;
124
125 if (!active_periods->GetInteger(2 * i, &start) ||
126 !active_periods->GetInteger(2 * i + 1, &end) ||
127 end <= start) {
128 // Something is amiss -- move to the next pair.
Mattias Nissler (ping if slow) 2011/11/25 14:15:19 If this happens, we might as well clear the list t
Patrick Dubroy 2011/11/29 18:01:46 Done.
129 NOTREACHED();
130 continue;
131 }
132 time_period = request->add_active_time();
133 time_period->set_start_timestamp(FromInternalTime(start).ToTimeT());
134 time_period->set_end_timestamp(FromInternalTime(end).ToTimeT());
135 }
136 // TODO(dubroy): This should probably happen elsewhere, after we know that
137 // the status was successfully sent.
138 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
139 update.Get()->Clear();
140 }
141
142 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698