Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: components/rappor/rappor_service.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/rappor/rappor_service.h ('k') | components/rappor/rappor_service_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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_service.h"
6
7 #include "base/base64.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/rand_util.h"
12 #include "base/stl_util.h"
13 #include "components/metrics/metrics_hashes.h"
14 #include "components/rappor/proto/rappor_metric.pb.h"
15 #include "components/rappor/rappor_pref_names.h"
16 #include "components/variations/variations_associated_data.h"
17
18 namespace rappor {
19
20 namespace {
21
22 // The number of cohorts we divide clients into.
23 const int kNumCohorts = 32;
24
25 // Seconds before the initial log is generated.
26 const int kInitialLogIntervalSeconds = 15;
27 // Interval between ongoing logs.
28 const int kLogIntervalSeconds = 30 * 60;
29
30 const char kMimeType[] = "application/vnd.chrome.rappor";
31
32 // Constants for the RAPPOR rollout field trial.
33 const char kRapporRolloutFieldTrialName[] = "RapporRollout";
34
35 // Constant for the finch parameter name for the server URL
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl";
37
38 GURL GetServerUrl() {
39 return GURL(chrome_variations::GetVariationParamValue(
40 kRapporRolloutFieldTrialName,
41 kRapporRolloutServerUrlParam));
42 }
43
44 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
45 { // ETLD_PLUS_ONE_RAPPOR_TYPE
46 16 /* Bloom filter size bytes */,
47 2 /* Bloom filter hash count */,
48 rappor::PROBABILITY_75 /* Fake data probability */,
49 rappor::PROBABILITY_50 /* Fake one probability */,
50 rappor::PROBABILITY_75 /* One coin probability */,
51 rappor::PROBABILITY_50 /* Zero coin probability */
52 },
53 };
54
55 } // namespace
56
57 RapporService::RapporService() : cohort_(-1) {}
58
59 RapporService::~RapporService() {
60 STLDeleteValues(&metrics_map_);
61 }
62
63 void RapporService::Start(PrefService* pref_service,
64 net::URLRequestContextGetter* request_context) {
65 GURL server_url = GetServerUrl();
66 if (!server_url.is_valid())
67 return;
68 DCHECK(!uploader_);
69 LoadSecret(pref_service);
70 LoadCohort(pref_service);
71 uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
72 log_rotation_timer_.Start(
73 FROM_HERE,
74 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
75 this,
76 &RapporService::OnLogInterval);
77 }
78
79 void RapporService::OnLogInterval() {
80 DCHECK(uploader_);
81 RapporReports reports;
82 if (ExportMetrics(&reports)) {
83 std::string log_text;
84 bool success = reports.SerializeToString(&log_text);
85 DCHECK(success);
86 uploader_->QueueLog(log_text);
87 }
88 log_rotation_timer_.Start(FROM_HERE,
89 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
90 this,
91 &RapporService::OnLogInterval);
92 }
93
94 // static
95 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
96 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
97 registry->RegisterIntegerPref(prefs::kRapporCohort, -1);
98 }
99
100 void RapporService::LoadCohort(PrefService* pref_service) {
101 DCHECK_EQ(cohort_, -1);
102 cohort_ = pref_service->GetInteger(prefs::kRapporCohort);
103 if (cohort_ >= 0 && cohort_ < kNumCohorts)
104 return;
105
106 cohort_ = base::RandGenerator(kNumCohorts);
107 pref_service->SetInteger(prefs::kRapporCohort, cohort_);
108 }
109
110 void RapporService::LoadSecret(PrefService* pref_service) {
111 DCHECK(secret_.empty());
112 std::string secret_base64 =
113 pref_service->GetString(prefs::kRapporSecret);
114 if (!secret_base64.empty()) {
115 bool decoded = base::Base64Decode(secret_base64, &secret_);
116 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
117 return;
118 // If the preference fails to decode, or is the wrong size, it must be
119 // corrupt, so continue as though it didn't exist yet and generate a new
120 // one.
121 }
122
123 secret_ = HmacByteVectorGenerator::GenerateEntropyInput();
124 base::Base64Encode(secret_, &secret_base64);
125 pref_service->SetString(prefs::kRapporSecret, secret_base64);
126 }
127
128 bool RapporService::ExportMetrics(RapporReports* reports) {
129 if (metrics_map_.empty())
130 return false;
131
132 DCHECK_GE(cohort_, 0);
133 reports->set_cohort(cohort_);
134
135 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin();
136 metrics_map_.end() != it;
137 ++it) {
138 const RapporMetric* metric = it->second;
139 RapporReports::Report* report = reports->add_report();
140 report->set_name_hash(metrics::HashMetricName(it->first));
141 ByteVector bytes = metric->GetReport(secret_);
142 report->set_bits(std::string(bytes.begin(), bytes.end()));
143 }
144 STLDeleteValues(&metrics_map_);
145 return true;
146 }
147
148 bool RapporService::IsInitialized() const {
149 return cohort_ >= 0;
150 }
151
152 void RapporService::RecordSample(const std::string& metric_name,
153 RapporType type,
154 const std::string& sample) {
155 // Ignore the sample if the service hasn't started yet.
156 if (!IsInitialized())
157 return;
158 DCHECK_LT(type, NUM_RAPPOR_TYPES);
159 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample);
160 }
161
162 void RapporService::RecordSampleInternal(const std::string& metric_name,
163 const RapporParameters& parameters,
164 const std::string& sample) {
165 DCHECK(IsInitialized());
166
167 RapporMetric* metric = LookUpMetric(metric_name, parameters);
168 metric->AddSample(sample);
169 }
170
171 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
172 const RapporParameters& parameters) {
173 DCHECK(IsInitialized());
174 std::map<std::string, RapporMetric*>::iterator it =
175 metrics_map_.find(metric_name);
176 if (metrics_map_.end() != it) {
177 RapporMetric* metric = it->second;
178 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
179 return metric;
180 }
181
182 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
183 metrics_map_[metric_name] = new_metric;
184 return new_metric;
185 }
186
187 } // namespace rappor
OLDNEW
« no previous file with comments | « components/rappor/rappor_service.h ('k') | components/rappor/rappor_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698