Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / prefs / tracked / tracked_preference_helper.cc
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 "chrome/browser/prefs/tracked/tracked_preference_helper.h"
6
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9
10 TrackedPreferenceHelper::TrackedPreferenceHelper(
11     const std::string& pref_path,
12     size_t reporting_id,
13     size_t reporting_ids_count,
14     PrefHashFilter::EnforcementLevel enforcement_level)
15     : pref_path_(pref_path),
16       reporting_id_(reporting_id), reporting_ids_count_(reporting_ids_count),
17       enforce_(enforcement_level == PrefHashFilter::ENFORCE_ON_LOAD) {
18 }
19
20 TrackedPreferenceHelper::ResetAction TrackedPreferenceHelper::GetAction(
21     PrefHashStoreTransaction::ValueState value_state) const {
22   switch (value_state) {
23     case PrefHashStoreTransaction::UNCHANGED:
24       // Desired case, nothing to do.
25       return DONT_RESET;
26     case PrefHashStoreTransaction::CLEARED:
27       // Unfortunate case, but there is nothing we can do.
28       return DONT_RESET;
29     case PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE:
30       // It is okay to seed the hash in this case.
31       return DONT_RESET;
32     case PrefHashStoreTransaction::MIGRATED:  // Falls through.
33     case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE:  // Falls through.
34     case PrefHashStoreTransaction::CHANGED:
35       return enforce_ ? DO_RESET : WANTED_RESET;
36   }
37   NOTREACHED() << "Unexpected PrefHashStoreTransaction::ValueState: "
38                << value_state;
39   return DONT_RESET;
40 }
41
42 void TrackedPreferenceHelper::ReportValidationResult(
43     PrefHashStoreTransaction::ValueState value_state) const {
44   switch (value_state) {
45     case PrefHashStoreTransaction::UNCHANGED:
46       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceUnchanged",
47                                 reporting_id_, reporting_ids_count_);
48       return;
49     case PrefHashStoreTransaction::CLEARED:
50       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceCleared",
51                                 reporting_id_, reporting_ids_count_);
52       return;
53     case PrefHashStoreTransaction::MIGRATED:
54       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceMigrated",
55                                 reporting_id_, reporting_ids_count_);
56       return;
57     case PrefHashStoreTransaction::CHANGED:
58       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceChanged",
59                                 reporting_id_, reporting_ids_count_);
60       return;
61     case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE:
62       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized",
63                                 reporting_id_, reporting_ids_count_);
64       return;
65     case PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE:
66       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceTrustedInitialized",
67                                 reporting_id_, reporting_ids_count_);
68       return;
69   }
70   NOTREACHED() << "Unexpected PrefHashStoreTransaction::ValueState: "
71                << value_state;
72 }
73
74 void TrackedPreferenceHelper::ReportAction(ResetAction reset_action) const {
75   switch (reset_action) {
76     case DONT_RESET:
77       // No report for DONT_RESET.
78       break;
79     case WANTED_RESET:
80       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceWantedReset",
81                                 reporting_id_, reporting_ids_count_);
82       break;
83     case DO_RESET:
84       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceReset",
85                                 reporting_id_, reporting_ids_count_);
86       break;
87   }
88 }
89
90 void TrackedPreferenceHelper::ReportSplitPreferenceChangedCount(
91     size_t count) const {
92   // The histogram below is an expansion of the UMA_HISTOGRAM_COUNTS_100 macro
93   // adapted to allow for a dynamically suffixed histogram name.
94   // Note: The factory creates and owns the histogram.
95   base::HistogramBase* histogram =
96       base::LinearHistogram::FactoryGet(
97           "Settings.TrackedSplitPreferenceChanged." + pref_path_,
98           1,
99           100,  // Allow counts up to 100.
100           101,
101           base::HistogramBase::kUmaTargetedHistogramFlag);
102   histogram->Add(count);
103 }