1// Copyright (c) 2012 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 "components/policy/core/common/policy_statistics_collector.h"
6
7#include <algorithm>
8#include <string>
9
10#include "base/bind.h"
11#include "base/callback.h"
12#include "base/location.h"
13#include "base/logging.h"
14#include "base/metrics/sparse_histogram.h"
15#include "base/prefs/pref_registry_simple.h"
16#include "base/prefs/pref_service.h"
17#include "base/task_runner.h"
18#include "components/policy/core/common/policy_pref_names.h"
19#include "components/policy/core/common/policy_service.h"
20
21namespace policy {
22
23const int PolicyStatisticsCollector::kStatisticsUpdateRate =
24    24 * 60 * 60 * 1000;  // 24 hours.
25
26PolicyStatisticsCollector::PolicyStatisticsCollector(
27    const GetChromePolicyDetailsCallback& get_details,
28    const Schema& chrome_schema,
29    PolicyService* policy_service,
30    PrefService* prefs,
31    const scoped_refptr<base::TaskRunner>& task_runner)
32    : get_details_(get_details),
33      chrome_schema_(chrome_schema),
34      policy_service_(policy_service),
35      prefs_(prefs),
36      task_runner_(task_runner) {
37}
38
39PolicyStatisticsCollector::~PolicyStatisticsCollector() {
40}
41
42void PolicyStatisticsCollector::Initialize() {
43  using base::Time;
44  using base::TimeDelta;
45
46  TimeDelta update_rate = TimeDelta::FromMilliseconds(kStatisticsUpdateRate);
47  Time last_update = Time::FromInternalValue(
48      prefs_->GetInt64(policy_prefs::kLastPolicyStatisticsUpdate));
49  TimeDelta delay = std::max(Time::Now() - last_update, TimeDelta::FromDays(0));
50  if (delay >= update_rate)
51    CollectStatistics();
52  else
53    ScheduleUpdate(update_rate - delay);
54}
55
56// static
57void PolicyStatisticsCollector::RegisterPrefs(PrefRegistrySimple* registry) {
58  registry->RegisterInt64Pref(policy_prefs::kLastPolicyStatisticsUpdate, 0);
59}
60
61void PolicyStatisticsCollector::RecordPolicyUse(int id) {
62  UMA_HISTOGRAM_SPARSE_SLOWLY("Enterprise.Policies", id);
63}
64
65void PolicyStatisticsCollector::CollectStatistics() {
66  const PolicyMap& policies = policy_service_->GetPolicies(
67      PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
68
69  // Collect statistics.
70  for (Schema::Iterator it(chrome_schema_.GetPropertiesIterator());
71       !it.IsAtEnd(); it.Advance()) {
72    if (policies.Get(it.key())) {
73      const PolicyDetails* details = get_details_.Run(it.key());
74      if (details)
75        RecordPolicyUse(details->id);
76      else
77        NOTREACHED();
78    }
79  }
80
81  // Take care of next update.
82  prefs_->SetInt64(policy_prefs::kLastPolicyStatisticsUpdate,
83                   base::Time::Now().ToInternalValue());
84  ScheduleUpdate(base::TimeDelta::FromMilliseconds(kStatisticsUpdateRate));
85}
86
87void PolicyStatisticsCollector::ScheduleUpdate(base::TimeDelta delay) {
88  update_callback_.Reset(base::Bind(
89      &PolicyStatisticsCollector::CollectStatistics,
90      base::Unretained(this)));
91  task_runner_->PostDelayedTask(FROM_HERE, update_callback_.callback(), delay);
92}
93
94}  // namespace policy
95