ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / rappor / rappor_prefs.cc
blob93c00b62cda2527373e9c4b0b5f9c1bcfc0b562f
1 // Copyright 2015 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.
5 #include "components/rappor/rappor_prefs.h"
7 #include "base/base64.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/rand_util.h"
12 #include "components/metrics/daily_event.h"
13 #include "components/rappor/byte_vector_utils.h"
14 #include "components/rappor/rappor_parameters.h"
15 #include "components/rappor/rappor_pref_names.h"
17 namespace rappor {
19 namespace internal {
21 const char kLoadCohortHistogramName[] = "Rappor.LoadCohortResult";
22 const char kLoadSecretHistogramName[] = "Rappor.LoadSecretResult";
24 namespace {
26 void RecordLoadCohortResult(LoadResult reason) {
27 UMA_HISTOGRAM_ENUMERATION(kLoadCohortHistogramName,
28 reason,
29 NUM_LOAD_RESULTS);
32 void RecordLoadSecretResult(LoadResult reason) {
33 UMA_HISTOGRAM_ENUMERATION(kLoadSecretHistogramName,
34 reason,
35 NUM_LOAD_RESULTS);
38 } // namespace
40 void RegisterPrefs(PrefRegistrySimple* registry) {
41 registry->RegisterBooleanPref(prefs::kRapporEnabled, true);
42 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
43 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
44 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
45 metrics::DailyEvent::RegisterPref(registry, prefs::kRapporLastDailySample);
48 int32_t LoadCohort(PrefService* pref_service) {
49 // Ignore and delete old cohort parameter.
50 pref_service->ClearPref(prefs::kRapporCohortDeprecated);
52 int32_t cohort = pref_service->GetInteger(prefs::kRapporCohortSeed);
53 // If the user is already assigned to a valid cohort, we're done.
54 if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts) {
55 RecordLoadCohortResult(LOAD_SUCCESS);
56 DVLOG(2) << "Rappor cohort loaded.";
57 return cohort;
60 // This is the first time the client has started the service (or their
61 // preferences were corrupted). Randomly assign them to a cohort.
62 RecordLoadCohortResult(cohort == -1 ? LOAD_EMPTY_VALUE : LOAD_CORRUPT_VALUE);
63 cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
64 DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
65 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort);
66 return cohort;
69 std::string LoadSecret(PrefService* pref_service) {
70 std::string secret;
71 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret);
72 if (!secret_base64.empty()) {
73 bool decoded = base::Base64Decode(secret_base64, &secret);
74 if (decoded &&
75 secret.size() == HmacByteVectorGenerator::kEntropyInputSize) {
76 DVLOG(2) << "Rappor secret loaded.";
77 RecordLoadSecretResult(LOAD_SUCCESS);
78 return secret;
80 // If the preference fails to decode, or is the wrong size, it must be
81 // corrupt, so continue as though it didn't exist yet and generate a new
82 // one.
83 DVLOG(2) << "Corrupt Rappor secret found.";
84 RecordLoadSecretResult(LOAD_CORRUPT_VALUE);
85 } else {
86 DVLOG(2) << "No Rappor secret found.";
87 RecordLoadSecretResult(LOAD_EMPTY_VALUE);
90 DVLOG(2) << "Generated a new Rappor secret.";
91 secret = HmacByteVectorGenerator::GenerateEntropyInput();
92 base::Base64Encode(secret, &secret_base64);
93 pref_service->SetString(prefs::kRapporSecret, secret_base64);
94 return secret;
97 } // namespace internal
99 } // namespace rappor