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.
4 
5 #include "components/rappor/rappor_prefs.h"
6 
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/rand_util.h"
11 #include "components/metrics/daily_event.h"
12 #include "components/prefs/pref_registry_simple.h"
13 #include "components/prefs/pref_service.h"
14 #include "components/rappor/byte_vector_utils.h"
15 #include "components/rappor/public/rappor_parameters.h"
16 #include "components/rappor/rappor_pref_names.h"
17 
18 namespace rappor {
19 
20 namespace internal {
21 
22 const char kLoadCohortHistogramName[] = "Rappor.LoadCohortResult";
23 const char kLoadSecretHistogramName[] = "Rappor.LoadSecretResult";
24 
25 namespace {
26 
RecordLoadCohortResult(LoadResult reason)27 void RecordLoadCohortResult(LoadResult reason) {
28   UMA_HISTOGRAM_ENUMERATION(kLoadCohortHistogramName,
29                             reason,
30                             NUM_LOAD_RESULTS);
31 }
32 
RecordLoadSecretResult(LoadResult reason)33 void RecordLoadSecretResult(LoadResult reason) {
34   UMA_HISTOGRAM_ENUMERATION(kLoadSecretHistogramName,
35                             reason,
36                             NUM_LOAD_RESULTS);
37 }
38 
39 } // namespace
40 
RegisterPrefs(PrefRegistrySimple * registry)41 void RegisterPrefs(PrefRegistrySimple* registry) {
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);
46 }
47 
LoadCohort(PrefService * pref_service)48 int32_t LoadCohort(PrefService* pref_service) {
49   // Ignore and delete old cohort parameter.
50   pref_service->ClearPref(prefs::kRapporCohortDeprecated);
51 
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;
58   }
59 
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;
67 }
68 
LoadSecret(PrefService * pref_service)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;
79     }
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);
88   }
89 
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;
95 }
96 
97 }  // namespace internal
98 
99 }  // namespace rappor
100