summaryrefslogtreecommitdiffstats
path: root/components/rappor/rappor_prefs.cc
blob: 3606b327c0af8a2826199031bf3e656c4897a142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/rappor/rappor_prefs.h"

#include "base/base64.h"
#include "base/metrics/histogram_macros.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/rand_util.h"
#include "components/metrics/daily_event.h"
#include "components/rappor/byte_vector_utils.h"
#include "components/rappor/rappor_parameters.h"
#include "components/rappor/rappor_pref_names.h"

namespace rappor {

namespace internal {

const char kLoadCohortHistogramName[] = "Rappor.LoadCohortResult";
const char kLoadSecretHistogramName[] = "Rappor.LoadSecretResult";

namespace {

void RecordLoadCohortResult(LoadResult reason) {
  UMA_HISTOGRAM_ENUMERATION(kLoadCohortHistogramName,
                            reason,
                            NUM_LOAD_RESULTS);
}

void RecordLoadSecretResult(LoadResult reason) {
  UMA_HISTOGRAM_ENUMERATION(kLoadSecretHistogramName,
                            reason,
                            NUM_LOAD_RESULTS);
}

} // namespace

void RegisterPrefs(PrefRegistrySimple* registry) {
  registry->RegisterStringPref(prefs::kRapporSecret, std::string());
  registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
  registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
  metrics::DailyEvent::RegisterPref(registry, prefs::kRapporLastDailySample);
}

int32_t LoadCohort(PrefService* pref_service) {
  // Ignore and delete old cohort parameter.
  pref_service->ClearPref(prefs::kRapporCohortDeprecated);

  int32_t cohort = pref_service->GetInteger(prefs::kRapporCohortSeed);
  // If the user is already assigned to a valid cohort, we're done.
  if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts) {
    RecordLoadCohortResult(LOAD_SUCCESS);
    DVLOG(2) << "Rappor cohort loaded.";
    return cohort;
  }

  // This is the first time the client has started the service (or their
  // preferences were corrupted).  Randomly assign them to a cohort.
  RecordLoadCohortResult(cohort == -1 ? LOAD_EMPTY_VALUE : LOAD_CORRUPT_VALUE);
  cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
  DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
  pref_service->SetInteger(prefs::kRapporCohortSeed, cohort);
  return cohort;
}

std::string LoadSecret(PrefService* pref_service) {
  std::string secret;
  std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret);
  if (!secret_base64.empty()) {
    bool decoded = base::Base64Decode(secret_base64, &secret);
    if (decoded &&
        secret.size() == HmacByteVectorGenerator::kEntropyInputSize) {
      DVLOG(2) << "Rappor secret loaded.";
      RecordLoadSecretResult(LOAD_SUCCESS);
      return secret;
    }
    // If the preference fails to decode, or is the wrong size, it must be
    // corrupt, so continue as though it didn't exist yet and generate a new
    // one.
    DVLOG(2) << "Corrupt Rappor secret found.";
    RecordLoadSecretResult(LOAD_CORRUPT_VALUE);
  } else {
    DVLOG(2) << "No Rappor secret found.";
    RecordLoadSecretResult(LOAD_EMPTY_VALUE);
  }

  DVLOG(2) << "Generated a new Rappor secret.";
  secret = HmacByteVectorGenerator::GenerateEntropyInput();
  base::Base64Encode(secret, &secret_base64);
  pref_service->SetString(prefs::kRapporSecret, secret_base64);
  return secret;
}

}  // namespace internal

}  // namespace rappor