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

Side by Side Diff: components/user_prefs/tracked/tracked_preference_helper.cc

Issue 2782803002: Move tracked prefs into services/preferences/tracked. (Closed)
Patch Set: rebase Created 3 years, 8 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 2014 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/user_prefs/tracked/tracked_preference_helper.h"
6
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "components/user_prefs/tracked/tracked_preference_histogram_names.h"
11
12 TrackedPreferenceHelper::TrackedPreferenceHelper(
13 const std::string& pref_path,
14 size_t reporting_id,
15 size_t reporting_ids_count,
16 PrefHashFilter::EnforcementLevel enforcement_level,
17 PrefHashFilter::ValueType value_type)
18 : pref_path_(pref_path),
19 reporting_id_(reporting_id),
20 reporting_ids_count_(reporting_ids_count),
21 enforce_(enforcement_level ==
22 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD),
23 personal_(value_type == PrefHashFilter::ValueType::PERSONAL) {}
24
25 TrackedPreferenceHelper::ResetAction TrackedPreferenceHelper::GetAction(
26 PrefHashStoreTransaction::ValueState value_state) const {
27 switch (value_state) {
28 case PrefHashStoreTransaction::UNCHANGED:
29 // Desired case, nothing to do.
30 return DONT_RESET;
31 case PrefHashStoreTransaction::CLEARED:
32 // Unfortunate case, but there is nothing we can do.
33 return DONT_RESET;
34 case PrefHashStoreTransaction::TRUSTED_NULL_VALUE: // Falls through.
35 case PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE:
36 // It is okay to seed the hash in this case.
37 return DONT_RESET;
38 case PrefHashStoreTransaction::SECURE_LEGACY:
39 // Accept secure legacy device ID based hashes.
40 return DONT_RESET;
41 case PrefHashStoreTransaction::UNSUPPORTED:
42 NOTREACHED()
43 << "GetAction should not be called with an UNSUPPORTED value state";
44 return DONT_RESET;
45 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE: // Falls through.
46 case PrefHashStoreTransaction::CHANGED:
47 return enforce_ ? DO_RESET : WANTED_RESET;
48 }
49 NOTREACHED() << "Unexpected PrefHashStoreTransaction::ValueState: "
50 << value_state;
51 return DONT_RESET;
52 }
53
54 bool TrackedPreferenceHelper::IsPersonal() const {
55 return personal_;
56 }
57
58 void TrackedPreferenceHelper::ReportValidationResult(
59 PrefHashStoreTransaction::ValueState value_state,
60 base::StringPiece validation_type_suffix) const {
61 const char* histogram_name = nullptr;
62 switch (value_state) {
63 case PrefHashStoreTransaction::UNCHANGED:
64 histogram_name = user_prefs::tracked::kTrackedPrefHistogramUnchanged;
65 break;
66 case PrefHashStoreTransaction::CLEARED:
67 histogram_name = user_prefs::tracked::kTrackedPrefHistogramCleared;
68 break;
69 case PrefHashStoreTransaction::SECURE_LEGACY:
70 histogram_name =
71 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId;
72 break;
73 case PrefHashStoreTransaction::CHANGED:
74 histogram_name = user_prefs::tracked::kTrackedPrefHistogramChanged;
75 break;
76 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE:
77 histogram_name = user_prefs::tracked::kTrackedPrefHistogramInitialized;
78 break;
79 case PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE:
80 histogram_name =
81 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized;
82 break;
83 case PrefHashStoreTransaction::TRUSTED_NULL_VALUE:
84 histogram_name =
85 user_prefs::tracked::kTrackedPrefHistogramNullInitialized;
86 break;
87 case PrefHashStoreTransaction::UNSUPPORTED:
88 NOTREACHED() << "ReportValidationResult should not be called with an "
89 "UNSUPPORTED value state";
90 return;
91 }
92 DCHECK(histogram_name);
93
94 std::string full_histogram_name(histogram_name);
95 if (!validation_type_suffix.empty()) {
96 full_histogram_name.push_back('.');
97 validation_type_suffix.AppendToString(&full_histogram_name);
98 }
99
100 // Using FactoryGet to allow dynamic histogram names. This is equivalent to
101 // UMA_HISTOGRAM_ENUMERATION(name, reporting_id_, reporting_ids_count_);
102 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
103 full_histogram_name, 1, reporting_ids_count_, reporting_ids_count_ + 1,
104 base::HistogramBase::kUmaTargetedHistogramFlag);
105 histogram->Add(reporting_id_);
106 }
107
108 void TrackedPreferenceHelper::ReportAction(ResetAction reset_action) const {
109 switch (reset_action) {
110 case DONT_RESET:
111 // No report for DONT_RESET.
112 break;
113 case WANTED_RESET:
114 UMA_HISTOGRAM_EXACT_LINEAR(
115 user_prefs::tracked::kTrackedPrefHistogramWantedReset, reporting_id_,
116 reporting_ids_count_);
117 break;
118 case DO_RESET:
119 UMA_HISTOGRAM_EXACT_LINEAR(
120 user_prefs::tracked::kTrackedPrefHistogramReset, reporting_id_,
121 reporting_ids_count_);
122 break;
123 }
124 }
125
126 void TrackedPreferenceHelper::ReportSplitPreferenceChangedCount(
127 size_t count) const {
128 // The histogram below is an expansion of the UMA_HISTOGRAM_COUNTS_100 macro
129 // adapted to allow for a dynamically suffixed histogram name.
130 // Note: The factory creates and owns the histogram.
131 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
132 user_prefs::tracked::kTrackedSplitPrefHistogramChanged + pref_path_, 1,
133 100, // Allow counts up to 100.
134 101, base::HistogramBase::kUmaTargetedHistogramFlag);
135 histogram->Add(count);
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698