| OLD | NEW |
| (Empty) | |
| 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 "chrome/browser/policy/policy_statistics_collector.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/location.h" |
| 12 #include "base/metrics/histogram.h" |
| 13 #include "base/task_runner.h" |
| 14 #include "chrome/browser/policy/policy_service.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "policy/policy_constants.h" |
| 18 |
| 19 namespace policy { |
| 20 |
| 21 const int PolicyStatisticsCollector::kStatisticsUpdateRate = |
| 22 24 * 60 * 60 * 1000; // 24 hours. |
| 23 |
| 24 PolicyStatisticsCollector::PolicyStatisticsCollector( |
| 25 PolicyService* policy_service, |
| 26 PrefService* prefs, |
| 27 const scoped_refptr<base::TaskRunner>& task_runner) |
| 28 : max_policy_id_(-1), |
| 29 policy_service_(policy_service), |
| 30 prefs_(prefs), |
| 31 task_runner_(task_runner) { |
| 32 } |
| 33 |
| 34 PolicyStatisticsCollector::~PolicyStatisticsCollector() { |
| 35 } |
| 36 |
| 37 void PolicyStatisticsCollector::Initialize() { |
| 38 using base::Time; |
| 39 using base::TimeDelta; |
| 40 |
| 41 TimeDelta update_rate = TimeDelta::FromMilliseconds(kStatisticsUpdateRate); |
| 42 Time last_update = Time::FromInternalValue( |
| 43 prefs_->GetInt64(prefs::kLastPolicyStatisticsUpdate)); |
| 44 TimeDelta delay = std::max(Time::Now() - last_update, TimeDelta::FromDays(0)); |
| 45 if (delay >= update_rate) |
| 46 CollectStatistics(); |
| 47 else |
| 48 ScheduleUpdate(update_rate - delay); |
| 49 } |
| 50 |
| 51 // static |
| 52 void PolicyStatisticsCollector::RegisterPrefs(PrefService* prefs) { |
| 53 prefs->RegisterInt64Pref(prefs::kLastPolicyStatisticsUpdate, 0, |
| 54 PrefService::UNSYNCABLE_PREF); |
| 55 } |
| 56 |
| 57 void PolicyStatisticsCollector::RecordPolicyUse(int id) { |
| 58 if (max_policy_id_ == -1) { |
| 59 const policy::PolicyDefinitionList* policy_list = |
| 60 policy::GetChromePolicyDefinitionList(); |
| 61 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; |
| 62 policy != policy_list->end; ++policy) { |
| 63 if (policy->id > max_policy_id_) |
| 64 max_policy_id_ = policy->id; |
| 65 } |
| 66 } |
| 67 |
| 68 // Set the boundary to be max policy id + 1. Note that this may decrease in |
| 69 // future builds if the policy with maximum id is removed. This does not |
| 70 // pose a problem. |
| 71 DCHECK_LE(id, max_policy_id_); |
| 72 UMA_HISTOGRAM_ENUMERATION("Enterprise.Policies", id, max_policy_id_ + 1); |
| 73 } |
| 74 |
| 75 void PolicyStatisticsCollector::CollectStatistics() { |
| 76 const policy::PolicyDefinitionList* policy_list = |
| 77 policy::GetChromePolicyDefinitionList(); |
| 78 const PolicyMap& policies = policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, |
| 79 std::string()); |
| 80 |
| 81 // Collect statistics. |
| 82 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; |
| 83 policy != policy_list->end; ++policy) { |
| 84 if (policies.Get(policy->name)) |
| 85 RecordPolicyUse(policy->id); |
| 86 } |
| 87 |
| 88 // Take care of next update. |
| 89 prefs_->SetInt64(prefs::kLastPolicyStatisticsUpdate, |
| 90 base::Time::Now().ToInternalValue()); |
| 91 ScheduleUpdate(base::TimeDelta::FromMilliseconds(kStatisticsUpdateRate)); |
| 92 } |
| 93 |
| 94 void PolicyStatisticsCollector::ScheduleUpdate(base::TimeDelta delay) { |
| 95 update_callback_.Reset(base::Bind( |
| 96 &PolicyStatisticsCollector::CollectStatistics, |
| 97 base::Unretained(this))); |
| 98 task_runner_->PostDelayedTask(FROM_HERE, update_callback_.callback(), delay); |
| 99 } |
| 100 |
| 101 } // namespace policy |
| OLD | NEW |