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"
21 const char kLoadCohortHistogramName
[] = "Rappor.LoadCohortResult";
22 const char kLoadSecretHistogramName
[] = "Rappor.LoadSecretResult";
26 void RecordLoadCohortResult(LoadResult reason
) {
27 UMA_HISTOGRAM_ENUMERATION(kLoadCohortHistogramName
,
32 void RecordLoadSecretResult(LoadResult reason
) {
33 UMA_HISTOGRAM_ENUMERATION(kLoadSecretHistogramName
,
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.";
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
);
68 std::string
LoadSecret(PrefService
* pref_service
) {
70 std::string secret_base64
= pref_service
->GetString(prefs::kRapporSecret
);
71 if (!secret_base64
.empty()) {
72 bool decoded
= base::Base64Decode(secret_base64
, &secret
);
74 secret
.size() == HmacByteVectorGenerator::kEntropyInputSize
) {
75 DVLOG(2) << "Rappor secret loaded.";
76 RecordLoadSecretResult(LOAD_SUCCESS
);
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
82 DVLOG(2) << "Corrupt Rappor secret found.";
83 RecordLoadSecretResult(LOAD_CORRUPT_VALUE
);
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
);
96 } // namespace internal