Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: chrome/browser/policy/policy_statistics_collector.cc

Issue 10916235: Record policy usage statistics every 24 hours. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed comments, moved PolicyStatisticsCollector to BrowserPolicyConnector, fixed policy sorting Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
Joao da Silva 2012/09/18 11:49:34 nit (again): used? :-)
qfel 2012/09/18 13:50:06 Oh, was sure removed it already O_O
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 namespace {
22 // Policy usage statistics update rate, in milliseconds.
23 const int kStatisticsUpdateRate = 24 * 60 * 60 * 1000; // 24 hours.
24 }
25
26 PolicyStatisticsCollector::PolicyStatisticsCollector(
27 PolicyService* policy_service,
28 PrefService* prefs,
29 const scoped_refptr<base::TaskRunner>& task_runner)
30 : policy_service_(policy_service),
31 prefs_(prefs),
32 task_runner_(task_runner) {
33 using base::Time;
34 using base::TimeDelta;
35
36 TimeDelta update_rate = TimeDelta::FromMilliseconds(kStatisticsUpdateRate);
37 Time last_update = Time::FromInternalValue(
38 prefs_->GetInt64(prefs::kLastPolicyStatisticsUpdate));
39 TimeDelta delay = std::max(Time::Now() - last_update, TimeDelta::FromDays(0));
40 if (delay >= update_rate)
41 CollectStatistics();
42 else
43 ScheduleUpdate(update_rate - delay);
44 }
45
46 PolicyStatisticsCollector::~PolicyStatisticsCollector() {
47 }
48
49 // static
50 void PolicyStatisticsCollector::RegisterPrefs(PrefService* prefs) {
51 prefs->RegisterInt64Pref(prefs::kLastPolicyStatisticsUpdate, 0,
52 PrefService::UNSYNCABLE_PREF);
53 }
54
55 void PolicyStatisticsCollector::CollectStatistics() {
56 const PolicyMap& policies = policy_service_->GetPolicies(POLICY_DOMAIN_CHROME,
57 std::string());
Joao da Silva 2012/09/18 11:49:34 #include <string>
qfel 2012/09/18 13:50:06 Done.
58 const policy::PolicyDefinitionList* policy_list =
59 policy::GetChromePolicyDefinitionList();
60
61 // Set the boundary to be max policy id + 1. Note that this may decrease in
62 // future builds if the policy with maximum id is removed. This does not
63 // pose a problem.
64 int boundary = 0;
65 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin;
66 policy != policy_list->end; ++policy) {
67 if (policy->id > boundary)
68 boundary = policy->id;
69 }
70 boundary++;
71
72 // Collect statistics.
73 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin;
74 policy != policy_list->end; ++policy) {
75 if (policies.Get(policy->name))
76 UMA_HISTOGRAM_ENUMERATION("Enterprise.Policies", policy->id, boundary);
77 }
78
79 // Take care of next update.
80 prefs_->SetInt64(prefs::kLastPolicyStatisticsUpdate,
81 base::Time::Now().ToInternalValue());
82 ScheduleUpdate(base::TimeDelta::FromMilliseconds(kStatisticsUpdateRate));
83 }
84
85 void PolicyStatisticsCollector::ScheduleUpdate(base::TimeDelta delay) {
86 update_callback_.Reset(base::Bind(
87 &PolicyStatisticsCollector::CollectStatistics,
88 base::Unretained(this)));
89 task_runner_->PostDelayedTask(FROM_HERE, update_callback_.callback(), delay);
90 }
91
92 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698