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

Unified 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: Addressed Mattias' comments and added unit test. Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/device_status_collector.cc
diff --git a/chrome/browser/policy/device_status_collector.cc b/chrome/browser/policy/device_status_collector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2426b52374516810d260acd59966c86d703cb917
--- /dev/null
+++ b/chrome/browser/policy/device_status_collector.cc
@@ -0,0 +1,176 @@
+// Copyright (c) 2011 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/message_loop.h"
+#include "base/task.h"
+#include "base/time.h"
+#include "chrome/browser/idle.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"
+
+using base::Time;
+using base::TimeDelta;
+
+namespace em = enterprise_management;
+
+namespace {
+// How many seconds of inactivity triggers the idle state.
+const unsigned int kIdleStateThresholdSeconds = 300;
+
+// The maximum number of time periods stored in the local state.
+const unsigned int kMaxStoredActivePeriods = 500;
+
+// Stores the baseline timestamp, to which the active periods are relative.
+const char* const kPrefBaselineTime = "device_status.baseline_timestamp";
+
+// Stores a list of timestamps representing device active periods.
+const char* const kPrefDeviceActivePeriods = "device_status.active_periods";
+}
+
+namespace policy {
+
+DeviceStatusCollector::DeviceStatusCollector(PrefService* local_state)
+ : local_state_(local_state),
+ last_idle_check_(Time()),
+ last_idle_state_(IDLE_STATE_UNKNOWN) {
+ // Only start the timer if we have a message loop. The only time this should
+ // matter is in unit_tests.
+ if (MessageLoop::current()) {
+ timer_.Start(FROM_HERE,
+ TimeDelta::FromSeconds(
+ DeviceStatusCollector::kPollIntervalSeconds),
+ this, &DeviceStatusCollector::CheckIdleState);
+ }
+}
+
+// static
+void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) {
+ local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT());
+ local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue);
+}
+
+unsigned int DeviceStatusCollector::max_stored_active_periods() const {
+ return kMaxStoredActivePeriods;
+}
+
+void DeviceStatusCollector::CheckIdleState() {
+ CalculateIdleState(kIdleStateThresholdSeconds,
+ base::Bind(&DeviceStatusCollector::IdleStateCallback,
+ base::Unretained(this)));
+}
+
+bool DeviceStatusCollector::ToInternalTime(const Time& time, int& out_val) {
+ Time baseline = Time::FromTimeT(
+ local_state_->GetInt64(kPrefBaselineTime));
+ int64 offset_millis = (time - baseline).InMilliseconds();
+ // Fail if the time cannot be represented by an integer.
+ // Assuming 32-bit integers, this would only happen if we haven't uploaded
+ // status in more than 49 days. This should never happen, and if it does,
+ // 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
+ if (offset_millis > static_cast<int64>(INT_MAX))
+ return false;
+ 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.
+ return true;
+}
+
+Time DeviceStatusCollector::FromInternalTime(int internal_time) {
+ Time baseline = Time::FromTimeT(
+ local_state_->GetInt64(kPrefBaselineTime));
+ return baseline + TimeDelta::FromMilliseconds(internal_time);
+}
+
+Time DeviceStatusCollector::GetCurrentTime() {
+ return Time::Now();
+}
+
+void DeviceStatusCollector::AddActivePeriod(Time start, Time end) {
+ // Maintain the list of active periods in a local_state pref.
+ ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
+ ListValue* active_periods = update.Get();
+
+ // Cap the number of active periods that we store.
+ if (active_periods->GetSize() >= 2 * max_stored_active_periods())
+ return;
+
+ int start_offset, end_offset;
+ if (!ToInternalTime(start, start_offset) ||
+ !ToInternalTime(end, end_offset)) {
+ NOTREACHED();
+ return;
+ }
+
+ int list_size = active_periods->GetSize();
+ DCHECK(list_size % 2 == 0);
+
+ // Check if this period can be combined with the previous one.
+ if (list_size > 0 && last_idle_state_ == IDLE_STATE_ACTIVE) {
+ int last_period_end;
+ if (active_periods->GetInteger(list_size - 1, &last_period_end) &&
+ last_period_end == start_offset) {
+ active_periods->Set(list_size - 1,
+ Value::CreateIntegerValue(end_offset));
+ return;
+ }
+ }
+ // Add a new period to the list.
+ active_periods->Append(Value::CreateIntegerValue(start_offset));
+ active_periods->Append(Value::CreateIntegerValue(end_offset));
+}
+
+void DeviceStatusCollector::IdleStateCallback(IdleState state) {
+ Time now = GetCurrentTime();
+
+ if (state == IDLE_STATE_ACTIVE) {
+ unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds;
+
+ // If it's been too long since the last report, assume that the system was
+ // in standby, and only count a single interval of activity.
+ if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval))
+ AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now);
+ else
+ AddActivePeriod(last_idle_check_, now);
+ }
+ last_idle_check_ = now;
+ last_idle_state_ = state;
+}
+
+void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
+ const ListValue* active_periods =
+ local_state_->GetList(kPrefDeviceActivePeriods);
+ em::TimePeriod* time_period;
+
+ DCHECK(active_periods->GetSize() % 2 == 0);
+
+ int period_count = active_periods->GetSize() / 2;
+ for (int i = 0; i < period_count; i++) {
+ int start, end;
+
+ if (!active_periods->GetInteger(2 * i, &start) ||
+ !active_periods->GetInteger(2 * i + 1, &end) ||
+ end < start) {
+ // Something is amiss -- bail out.
+ NOTREACHED();
+ break;
+ }
+ time_period = request->add_active_time();
+ time_period->set_start_timestamp(
+ (FromInternalTime(start) - Time::UnixEpoch()).InMilliseconds());
+ time_period->set_end_timestamp(
+ (FromInternalTime(end) - Time::UnixEpoch()).InMilliseconds());
+ }
+ ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
+ update.Get()->Clear();
+
+ // Reset the baseline time every time we report. If this isn't done
+ // 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
+ local_state_->SetInt64(kPrefBaselineTime,
+ GetCurrentTime().ToTimeT());
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698