Remove migrateNetworkPredictionPreferences().
[chromium-blink-merge.git] / components / rappor / rappor_prefs.cc
blob3606b327c0af8a2826199031bf3e656c4897a142
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_macros.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->RegisterStringPref(prefs::kRapporSecret, std::string());
42 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
43 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
44 metrics::DailyEvent::RegisterPref(registry, prefs::kRapporLastDailySample);
47 int32_t LoadCohort(PrefService* pref_service) {
48 // Ignore and delete old cohort parameter.
49 pref_service->ClearPref(prefs::kRapporCohortDeprecated);
51 int32_t cohort = pref_service->GetInteger(prefs::kRapporCohortSeed);
52 // If the user is already assigned to a valid cohort, we're done.
53 if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts) {
54 RecordLoadCohortResult(LOAD_SUCCESS);
55 DVLOG(2) << "Rappor cohort loaded.";
56 return cohort;
59 // This is the first time the client has started the service (or their
60 // preferences were corrupted). Randomly assign them to a cohort.
61 RecordLoadCohortResult(cohort == -1 ? LOAD_EMPTY_VALUE : LOAD_CORRUPT_VALUE);
62 cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
63 DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
64 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort);
65 return cohort;
68 std::string LoadSecret(PrefService* pref_service) {
69 std::string secret;
70 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret);
71 if (!secret_base64.empty()) {
72 bool decoded = base::Base64Decode(secret_base64, &secret);
73 if (decoded &&
74 secret.size() == HmacByteVectorGenerator::kEntropyInputSize) {
75 DVLOG(2) << "Rappor secret loaded.";
76 RecordLoadSecretResult(LOAD_SUCCESS);
77 return secret;
79 // If the preference fails to decode, or is the wrong size, it must be
80 // corrupt, so continue as though it didn't exist yet and generate a new
81 // one.
82 DVLOG(2) << "Corrupt Rappor secret found.";
83 RecordLoadSecretResult(LOAD_CORRUPT_VALUE);
84 } else {
85 DVLOG(2) << "No Rappor secret found.";
86 RecordLoadSecretResult(LOAD_EMPTY_VALUE);
89 DVLOG(2) << "Generated a new Rappor secret.";
90 secret = HmacByteVectorGenerator::GenerateEntropyInput();
91 base::Base64Encode(secret, &secret_base64);
92 pref_service->SetString(prefs::kRapporSecret, secret_base64);
93 return secret;
96 } // namespace internal
98 } // namespace rappor