cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / policy / core / common / policy_statistics_collector.cc
blob3e6a5880a03f125c93a07779a721503f02dd4504
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.
5 #include "components/policy/core/common/policy_statistics_collector.h"
7 #include <algorithm>
8 #include <string>
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"
21 namespace policy {
23 const int PolicyStatisticsCollector::kStatisticsUpdateRate =
24 24 * 60 * 60 * 1000; // 24 hours.
26 PolicyStatisticsCollector::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) {
39 PolicyStatisticsCollector::~PolicyStatisticsCollector() {
42 void PolicyStatisticsCollector::Initialize() {
43 using base::Time;
44 using base::TimeDelta;
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);
56 // static
57 void PolicyStatisticsCollector::RegisterPrefs(PrefRegistrySimple* registry) {
58 registry->RegisterInt64Pref(policy_prefs::kLastPolicyStatisticsUpdate, 0);
61 void PolicyStatisticsCollector::RecordPolicyUse(int id) {
62 UMA_HISTOGRAM_SPARSE_SLOWLY("Enterprise.Policies", id);
65 void PolicyStatisticsCollector::CollectStatistics() {
66 const PolicyMap& policies = policy_service_->GetPolicies(
67 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
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();
81 // Take care of next update.
82 prefs_->SetInt64(policy_prefs::kLastPolicyStatisticsUpdate,
83 base::Time::Now().ToInternalValue());
84 ScheduleUpdate(base::TimeDelta::FromMilliseconds(kStatisticsUpdateRate));
87 void 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);
94 } // namespace policy