Chromium Code Reviews| Index: chrome/browser/policy/device_status_reporter.cc |
| diff --git a/chrome/browser/policy/device_status_reporter.cc b/chrome/browser/policy/device_status_reporter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f8cf8b28542de3738113b9143890f7d88ddb6f5a |
| --- /dev/null |
| +++ b/chrome/browser/policy/device_status_reporter.cc |
| @@ -0,0 +1,142 @@ |
| +// 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_reporter.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/message_loop.h" |
| +#include "base/task.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" |
| + |
| +namespace em = enterprise_management; |
| + |
| +namespace { |
| + // How many seconds of inactivity triggers the idle state. |
| + 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.
|
| + |
| + // How often, in seconds, to poll to see if the user is idle. |
| + const unsigned int kDefaultPollIntervalSeconds = 30; |
| + |
| + // 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 { |
| + |
| +DeviceStatusReporter::DeviceStatusReporter(PrefService* local_state) |
| + : poll_interval_seconds_(kDefaultPollIntervalSeconds), |
| + local_state_(local_state), |
| + last_idle_check_(base::Time::Now()), |
| + last_idle_state_(IDLE_STATE_UNKNOWN) { |
| + local_state_->RegisterInt64Pref(kPrefBaselineTime, |
| + base::Time::Now().ToTimeT()); |
| + local_state_->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); |
| + CheckIdleState(); |
| +} |
| + |
| +void DeviceStatusReporter::CheckIdleState() { |
| + CalculateIdleState(kIdleStateThresholdSeconds, |
| + base::Bind(&DeviceStatusReporter::IdleStateCallback, |
| + base::Unretained(this))); |
| +} |
| + |
| +int DeviceStatusReporter::ToInternalTime(const base::Time& time) { |
| + base::Time baseline = base::Time::FromTimeT( |
| + local_state_->GetInt64(kPrefBaselineTime)); |
| + return (int)(time - baseline).InSeconds(); |
| +} |
| + |
| +base::Time DeviceStatusReporter::FromInternalTime(int internal_time) { |
| + base::Time baseline = base::Time::FromTimeT( |
| + local_state_->GetInt64(kPrefBaselineTime)); |
| + return baseline + base::TimeDelta::FromSeconds(internal_time); |
| +} |
| + |
| +void DeviceStatusReporter::AddActivePeriod(base::Time start, base::Time end) { |
| + // Maintain the list of active periods in a local_state pref. |
| + ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| + ListValue* active_periods = update.Get(); |
| + |
| + int start_offset = ToInternalTime(start); |
| + int end_offset = ToInternalTime(end); |
| + |
| + 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)); |
|
Mattias Nissler (ping if slow)
2011/11/25 14:15:19
we should probably limit the maximum number of ent
|
| +} |
| + |
| +void DeviceStatusReporter::IdleStateCallback(IdleState state) { |
| + base::Time now = base::Time::Now(); |
| + |
| + if (state == IDLE_STATE_ACTIVE) { |
| + base::Time period_start = last_idle_check_; |
| + |
| + // 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 - period_start).InSeconds() >= (2 * poll_interval_seconds_)) { |
| + period_start = |
| + now - base::TimeDelta::FromSeconds(poll_interval_seconds_); |
| + } |
| + |
| + AddActivePeriod(period_start, now); |
| + } |
| + last_idle_check_ = now; |
| + last_idle_state_ = state; |
| + |
| + 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.
|
| + FROM_HERE, |
| + base::Bind(&DeviceStatusReporter::CheckIdleState, |
| + base::Unretained(this)), |
| + poll_interval_seconds_ * 1000); |
| +} |
| + |
| +void DeviceStatusReporter::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 -- 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.
|
| + NOTREACHED(); |
| + continue; |
| + } |
| + time_period = request->add_active_time(); |
| + time_period->set_start_timestamp(FromInternalTime(start).ToTimeT()); |
| + time_period->set_end_timestamp(FromInternalTime(end).ToTimeT()); |
| + } |
| + // TODO(dubroy): This should probably happen elsewhere, after we know that |
| + // the status was successfully sent. |
| + ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| + update.Get()->Clear(); |
| +} |
| + |
| +} // namespace policy |