summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/device_status_collector.cc
blob: 3d82084c4e62ab1de5651146cb0b643bf5396426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2012 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/string_number_conversions.h"
#include "chrome/browser/chromeos/system/statistics_provider.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"
#include "chrome/common/chrome_version_info.h"

using base::Time;
using base::TimeDelta;
using chromeos::VersionLoader;

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";

bool GetTimestamp(const ListValue* list, int index, int64* out_value) {
  std::string string_value;
  if (list->GetString(index, &string_value))
    return base::StringToInt64(string_value, out_value);
  return false;
}

}  // namespace

namespace policy {

DeviceStatusCollector::DeviceStatusCollector(
    PrefService* local_state,
    chromeos::system::StatisticsProvider* provider)
    : max_stored_active_periods_(kMaxStoredActivePeriods),
      local_state_(local_state),
      last_idle_check_(Time()),
      last_idle_state_(IDLE_STATE_UNKNOWN),
      statistics_provider_(provider) {
  timer_.Start(FROM_HERE,
               TimeDelta::FromSeconds(
                   DeviceStatusCollector::kPollIntervalSeconds),
               this, &DeviceStatusCollector::CheckIdleState);

  // Get the the OS and firmware version info.
  version_loader_.GetVersion(&consumer_,
                             base::Bind(&DeviceStatusCollector::OnOSVersion,
                                        base::Unretained(this)),
                             VersionLoader::VERSION_FULL);
  version_loader_.GetFirmware(&consumer_,
                              base::Bind(&DeviceStatusCollector::OnOSFirmware,
                                         base::Unretained(this)));
}

DeviceStatusCollector::~DeviceStatusCollector() {
}

// static
void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) {
  local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT());
  local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue);
}

void DeviceStatusCollector::CheckIdleState() {
  CalculateIdleState(kIdleStateThresholdSeconds,
      base::Bind(&DeviceStatusCollector::IdleStateCallback,
                 base::Unretained(this)));
}

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;

  Time epoch = Time::UnixEpoch();
  int64 start_timestamp = (start - epoch).InMilliseconds();
  Value* end_value = new StringValue(
      base::Int64ToString((end - epoch).InMilliseconds()));

  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) {
    int64 last_period_end;
    if (GetTimestamp(active_periods, list_size - 1, &last_period_end) &&
        last_period_end == start_timestamp) {
      active_periods->Set(list_size - 1, end_value);
      return;
    }
  }
  // Add a new period to the list.
  active_periods->Append(
      new StringValue(base::Int64ToString(start_timestamp)));
  active_periods->Append(end_value);
}

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) {
  // Report device active periods.
  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++) {
    int64 start, end;

    if (!GetTimestamp(active_periods, 2 * i, &start) ||
        !GetTimestamp(active_periods, 2 * i + 1, &end) ||
        end < start) {
      // Something is amiss -- bail out.
      NOTREACHED();
      break;
    }
    time_period = request->add_active_time();
    time_period->set_start_timestamp(start);
    time_period->set_end_timestamp(end);
  }
  ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
  update.Get()->Clear();

  chrome::VersionInfo version_info;
  request->set_browser_version(version_info.Version());
  request->set_os_version(os_version_);
  request->set_firmware_version(firmware_version_);

  // Report the state of the dev switch at boot.
  std::string dev_switch_mode;
  if (statistics_provider_->GetMachineStatistic(
      "devsw_boot", &dev_switch_mode)) {
    if (dev_switch_mode == "1")
      request->set_boot_mode("Dev");
    else if (dev_switch_mode == "0")
      request->set_boot_mode("Verified");
  }
}

void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle,
                                        std::string version) {
  os_version_ = version;
}

void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle,
                                         std::string version) {
  firmware_version_ = version;
}

}  // namespace policy