OLD | NEW |
(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 timer_.Start(FROM_HERE, |
| 53 TimeDelta::FromSeconds( |
| 54 DeviceStatusCollector::kPollIntervalSeconds), |
| 55 this, &DeviceStatusCollector::CheckIdleState); |
| 56 } |
| 57 |
| 58 DeviceStatusCollector::~DeviceStatusCollector() { |
| 59 } |
| 60 |
| 61 // static |
| 62 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { |
| 63 local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT()); |
| 64 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); |
| 65 } |
| 66 |
| 67 void DeviceStatusCollector::CheckIdleState() { |
| 68 CalculateIdleState(kIdleStateThresholdSeconds, |
| 69 base::Bind(&DeviceStatusCollector::IdleStateCallback, |
| 70 base::Unretained(this))); |
| 71 } |
| 72 |
| 73 Time DeviceStatusCollector::GetCurrentTime() { |
| 74 return Time::Now(); |
| 75 } |
| 76 |
| 77 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { |
| 78 // Maintain the list of active periods in a local_state pref. |
| 79 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| 80 ListValue* active_periods = update.Get(); |
| 81 |
| 82 // Cap the number of active periods that we store. |
| 83 if (active_periods->GetSize() >= 2 * max_stored_active_periods_) |
| 84 return; |
| 85 |
| 86 Time epoch = Time::UnixEpoch(); |
| 87 int64 start_timestamp = (start - epoch).InMilliseconds(); |
| 88 Value* end_value = new StringValue( |
| 89 base::Int64ToString((end - epoch).InMilliseconds())); |
| 90 |
| 91 int list_size = active_periods->GetSize(); |
| 92 DCHECK(list_size % 2 == 0); |
| 93 |
| 94 // Check if this period can be combined with the previous one. |
| 95 if (list_size > 0 && last_idle_state_ == IDLE_STATE_ACTIVE) { |
| 96 int64 last_period_end; |
| 97 if (GetTimestamp(active_periods, list_size - 1, &last_period_end) && |
| 98 last_period_end == start_timestamp) { |
| 99 active_periods->Set(list_size - 1, end_value); |
| 100 return; |
| 101 } |
| 102 } |
| 103 // Add a new period to the list. |
| 104 active_periods->Append( |
| 105 new StringValue(base::Int64ToString(start_timestamp))); |
| 106 active_periods->Append(end_value); |
| 107 } |
| 108 |
| 109 void DeviceStatusCollector::IdleStateCallback(IdleState state) { |
| 110 Time now = GetCurrentTime(); |
| 111 |
| 112 if (state == IDLE_STATE_ACTIVE) { |
| 113 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; |
| 114 |
| 115 // If it's been too long since the last report, assume that the system was |
| 116 // in standby, and only count a single interval of activity. |
| 117 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) |
| 118 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); |
| 119 else |
| 120 AddActivePeriod(last_idle_check_, now); |
| 121 } |
| 122 last_idle_check_ = now; |
| 123 last_idle_state_ = state; |
| 124 } |
| 125 |
| 126 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { |
| 127 const ListValue* active_periods = |
| 128 local_state_->GetList(kPrefDeviceActivePeriods); |
| 129 em::TimePeriod* time_period; |
| 130 |
| 131 DCHECK(active_periods->GetSize() % 2 == 0); |
| 132 |
| 133 int period_count = active_periods->GetSize() / 2; |
| 134 for (int i = 0; i < period_count; i++) { |
| 135 int64 start, end; |
| 136 |
| 137 if (!GetTimestamp(active_periods, 2 * i, &start) || |
| 138 !GetTimestamp(active_periods, 2 * i + 1, &end) || |
| 139 end < start) { |
| 140 // Something is amiss -- bail out. |
| 141 NOTREACHED(); |
| 142 break; |
| 143 } |
| 144 time_period = request->add_active_time(); |
| 145 time_period->set_start_timestamp(start); |
| 146 time_period->set_end_timestamp(end); |
| 147 } |
| 148 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| 149 update.Get()->Clear(); |
| 150 } |
| 151 |
| 152 } // namespace policy |
OLD | NEW |