Chromium Code Reviews| Index: chrome/browser/policy/policy_statistics_collector.cc |
| diff --git a/chrome/browser/policy/policy_statistics_collector.cc b/chrome/browser/policy/policy_statistics_collector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4231aab45272ab0192751040e33df1c348ff9e0f |
| --- /dev/null |
| +++ b/chrome/browser/policy/policy_statistics_collector.cc |
| @@ -0,0 +1,86 @@ |
| +// 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/policy_statistics_collector.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
|
Joao da Silva
2012/09/12 13:29:38
Used?
qfel
2012/09/18 11:30:56
Oh, that DCHECK is no longer there.
Done.
|
| +#include "base/metrics/histogram.h" |
| +#include "base/task_runner.h" |
| +#include "chrome/browser/policy/policy_service.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "policy/policy_constants.h" |
| + |
| +namespace policy { |
| + |
| +namespace { |
| +// The time delta specifying how often policy usage statistics are collected. |
| +const base::TimeDelta kStatisticsUpdateRate = base::TimeDelta::FromDays(1); |
|
Joao da Silva
2012/09/12 13:29:38
This requires a static initializer, which are avoi
qfel
2012/09/18 11:30:56
Done.
|
| +} |
| + |
| +PolicyStatisticsCollector::PolicyStatisticsCollector( |
| + PolicyService* policy_service, |
| + PrefService* prefs, |
| + const scoped_refptr<base::TaskRunner>& task_runner) |
| + : policy_service_(policy_service), |
| + prefs_(prefs), |
| + task_runner_(task_runner) { |
| + base::Time last_update = base::Time::FromInternalValue( |
| + prefs_->GetInt64(prefs::kLastPolicyStatisticsUpdate)); |
| + base::TimeDelta delay = base::Time::Now() - last_update; |
|
Joao da Silva
2012/09/12 13:29:38
|last_update| should be clamped to at most Now().
qfel
2012/09/18 11:30:56
Done.
|
| + if (delay >= kStatisticsUpdateRate) |
| + CollectStatistics(); |
| + else |
| + ScheduleUpdate(kStatisticsUpdateRate - delay); |
| +} |
| + |
| +PolicyStatisticsCollector::~PolicyStatisticsCollector() { |
| +} |
| + |
| +// static |
| +void PolicyStatisticsCollector::RegisterPrefs(PrefService* prefs) { |
| + prefs->RegisterInt64Pref(prefs::kLastPolicyStatisticsUpdate, 0, |
| + PrefService::UNSYNCABLE_PREF); |
| +} |
| + |
| +void PolicyStatisticsCollector::CollectStatistics() { |
| + const PolicyMap& policies = policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, |
| + ""); |
|
Mattias Nissler (ping if slow)
2012/09/12 13:06:27
use std::string() instead of ""
qfel
2012/09/18 11:30:56
Done.
|
| + const policy::PolicyDefinitionList* policy_list = |
| + policy::GetChromePolicyDefinitionList(); |
| + |
| + // Set the boundary to be max policy id + 1. Note that this may decrease in |
| + // future builds if the policy with maximum id is removed. This does not |
| + // pose a problem. |
| + int boundary = 0; |
| + for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; |
| + policy != policy_list->end; ++policy) { |
| + if (policy->id > boundary) |
|
Mattias Nissler (ping if slow)
2012/09/12 13:06:27
boundary = std::max(policy->id, boundary)?
qfel
2012/09/18 11:30:56
Why is it better?
|
| + boundary = policy->id; |
| + } |
| + boundary++; |
| + |
| + // Collect statistics. |
| + for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; |
| + policy != policy_list->end; ++policy) { |
| + if (policies.Get(policy->name)) |
| + UMA_HISTOGRAM_ENUMERATION("Enterprise.Policies", policy->id, boundary); |
| + } |
| + |
| + // Take care of next update. |
| + prefs_->SetInt64(prefs::kLastPolicyStatisticsUpdate, |
| + base::Time::Now().ToInternalValue()); |
| + ScheduleUpdate(kStatisticsUpdateRate); |
| +} |
| + |
| +void PolicyStatisticsCollector::ScheduleUpdate(base::TimeDelta delay) { |
| + update_callback_.Reset(base::Bind( |
| + &PolicyStatisticsCollector::CollectStatistics, |
| + base::Unretained(this))); |
| + task_runner_->PostDelayedTask(FROM_HERE, update_callback_.callback(), delay); |
| +} |
| + |
| +} // namespace policy |