OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Change for readability | |
6 | |
5 #include "components/rappor/rappor_service.h" | 7 #include "components/rappor/rappor_service.h" |
6 | 8 |
7 #include "base/base64.h" | 9 #include "base/base64.h" |
8 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
9 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
11 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
12 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
13 #include "components/metrics/metrics_hashes.h" | 15 #include "components/metrics/metrics_hashes.h" |
14 #include "components/rappor/proto/rappor_metric.pb.h" | 16 #include "components/rappor/proto/rappor_metric.pb.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
35 // Constant for the finch parameter name for the server URL | 37 // Constant for the finch parameter name for the server URL |
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | 38 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; |
37 | 39 |
38 GURL GetServerUrl() { | 40 GURL GetServerUrl() { |
39 return GURL(chrome_variations::GetVariationParamValue( | 41 return GURL(chrome_variations::GetVariationParamValue( |
40 kRapporRolloutFieldTrialName, | 42 kRapporRolloutFieldTrialName, |
41 kRapporRolloutServerUrlParam)); | 43 kRapporRolloutServerUrlParam)); |
42 } | 44 } |
43 | 45 |
44 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { | 46 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { |
45 { // ETLD_PLUS_ONE_RAPPOR_TYPE | 47 {// ETLD_PLUS_ONE_RAPPOR_TYPE |
46 16 /* Bloom filter size bytes */, | 48 16 /* Bloom filter size bytes */, |
47 2 /* Bloom filter hash count */, | 49 2 /* Bloom filter hash count */, |
48 rappor::PROBABILITY_75 /* Fake data probability */, | 50 rappor::PROBABILITY_75 /* Fake data probability */, |
49 rappor::PROBABILITY_50 /* Fake one probability */, | 51 rappor::PROBABILITY_50 /* Fake one probability */, |
50 rappor::PROBABILITY_75 /* One coin probability */, | 52 rappor::PROBABILITY_75 /* One coin probability */, |
51 rappor::PROBABILITY_50 /* Zero coin probability */ | 53 rappor::PROBABILITY_50 /* Zero coin probability */ |
52 }, | 54 }, |
53 }; | 55 }; |
54 | 56 |
55 } // namespace | 57 } // namespace |
56 | 58 |
57 RapporService::RapporService() : cohort_(-1) {} | 59 RapporService::RapporService() : cohort_(-1) {} |
58 | 60 |
59 RapporService::~RapporService() { | 61 RapporService::~RapporService() { |
60 STLDeleteValues(&metrics_map_); | 62 STLDeleteValues(&metrics_map_); |
61 } | 63 } |
(...skipping 29 matching lines...) Expand all Loading... | |
91 &RapporService::OnLogInterval); | 93 &RapporService::OnLogInterval); |
92 } | 94 } |
93 | 95 |
94 // static | 96 // static |
95 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | 97 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { |
96 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | 98 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); |
97 registry->RegisterIntegerPref(prefs::kRapporCohort, -1); | 99 registry->RegisterIntegerPref(prefs::kRapporCohort, -1); |
98 } | 100 } |
99 | 101 |
100 void RapporService::LoadCohort(PrefService* pref_service) { | 102 void RapporService::LoadCohort(PrefService* pref_service) { |
101 DCHECK_EQ(cohort_, -1); | 103 DCHECK(!IsInitialized()); |
102 cohort_ = pref_service->GetInteger(prefs::kRapporCohort); | 104 cohort_ = pref_service->GetInteger(prefs::kRapporCohort); |
105 // If the user is already assigned to a valid cohort, we're done. | |
103 if (cohort_ >= 0 && cohort_ < kNumCohorts) | 106 if (cohort_ >= 0 && cohort_ < kNumCohorts) |
104 return; | 107 return; |
105 | 108 |
109 // This is the first time the client has started the service (or thier | |
ktl
2014/03/18 08:35:01
Spelling: their
Steven Holte
2014/03/19 23:46:55
Done.
| |
110 // preferences were corrupted). Randomly assign them to a cohort. | |
106 cohort_ = base::RandGenerator(kNumCohorts); | 111 cohort_ = base::RandGenerator(kNumCohorts); |
107 pref_service->SetInteger(prefs::kRapporCohort, cohort_); | 112 pref_service->SetInteger(prefs::kRapporCohort, cohort_); |
108 } | 113 } |
109 | 114 |
110 void RapporService::LoadSecret(PrefService* pref_service) { | 115 void RapporService::LoadSecret(PrefService* pref_service) { |
111 DCHECK(secret_.empty()); | 116 DCHECK(secret_.empty()); |
112 std::string secret_base64 = | 117 std::string secret_base64 = |
ktl
2014/03/18 08:35:01
This fits on one line.
Steven Holte
2014/03/19 23:46:55
Done.
| |
113 pref_service->GetString(prefs::kRapporSecret); | 118 pref_service->GetString(prefs::kRapporSecret); |
114 if (!secret_base64.empty()) { | 119 if (!secret_base64.empty()) { |
115 bool decoded = base::Base64Decode(secret_base64, &secret_); | 120 bool decoded = base::Base64Decode(secret_base64, &secret_); |
116 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | 121 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) |
117 return; | 122 return; |
118 // If the preference fails to decode, or is the wrong size, it must be | 123 // 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 | 124 // corrupt, so continue as though it didn't exist yet and generate a new |
120 // one. | 125 // one. |
121 } | 126 } |
122 | 127 |
123 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); | 128 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); |
124 base::Base64Encode(secret_, &secret_base64); | 129 base::Base64Encode(secret_, &secret_base64); |
125 pref_service->SetString(prefs::kRapporSecret, secret_base64); | 130 pref_service->SetString(prefs::kRapporSecret, secret_base64); |
126 } | 131 } |
127 | 132 |
128 bool RapporService::ExportMetrics(RapporReports* reports) { | 133 bool RapporService::ExportMetrics(RapporReports* reports) { |
129 if (metrics_map_.empty()) | 134 if (metrics_map_.empty()) |
130 return false; | 135 return false; |
131 | 136 |
132 DCHECK_GE(cohort_, 0); | 137 DCHECK_GE(cohort_, 0); |
133 reports->set_cohort(cohort_); | 138 reports->set_cohort(cohort_); |
134 | 139 |
135 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin(); | 140 for (std::map<std::string, RapporMetric*>::const_iterator it = |
141 metrics_map_.begin(); | |
ktl
2014/03/18 08:35:01
This would need one more space; the four space ind
Steven Holte
2014/03/19 23:46:55
Done.
| |
136 metrics_map_.end() != it; | 142 metrics_map_.end() != it; |
ktl
2014/03/18 08:35:01
I've not seen this style (putting the variable ite
Steven Holte
2014/03/19 23:46:55
base/metrics/... seems to use that style in a few
| |
137 ++it) { | 143 ++it) { |
138 const RapporMetric* metric = it->second; | 144 const RapporMetric* metric = it->second; |
139 RapporReports::Report* report = reports->add_report(); | 145 RapporReports::Report* report = reports->add_report(); |
140 report->set_name_hash(metrics::HashMetricName(it->first)); | 146 report->set_name_hash(metrics::HashMetricName(it->first)); |
141 ByteVector bytes = metric->GetReport(secret_); | 147 ByteVector bytes = metric->GetReport(secret_); |
142 report->set_bits(std::string(bytes.begin(), bytes.end())); | 148 report->set_bits(std::string(bytes.begin(), bytes.end())); |
143 } | 149 } |
144 STLDeleteValues(&metrics_map_); | 150 STLDeleteValues(&metrics_map_); |
145 return true; | 151 return true; |
146 } | 152 } |
147 | 153 |
148 bool RapporService::IsInitialized() const { | 154 bool RapporService::IsInitialized() const { |
149 return cohort_ >= 0; | 155 return cohort_ >= 0; |
150 } | 156 } |
151 | 157 |
152 void RapporService::RecordSample(const std::string& metric_name, | 158 void RapporService::RecordSample(const std::string& metric_name, |
153 RapporType type, | 159 RapporType type, |
154 const std::string& sample) { | 160 const std::string& sample) { |
155 // Ignore the sample if the service hasn't started yet. | 161 // Ignore the sample if the service hasn't started yet. |
156 if (!IsInitialized()) | 162 if (!IsInitialized()) |
157 return; | 163 return; |
158 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 164 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
159 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | 165 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); |
160 } | 166 } |
161 | 167 |
162 void RapporService::RecordSampleInternal(const std::string& metric_name, | 168 void RapporService::RecordSampleInternal(const std::string& metric_name, |
163 const RapporParameters& parameters, | 169 const RapporParameters& parameters, |
164 const std::string& sample) { | 170 const std::string& sample) { |
165 DCHECK(IsInitialized()); | 171 DCHECK(IsInitialized()); |
166 | |
167 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 172 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
168 metric->AddSample(sample); | 173 metric->AddSample(sample); |
169 } | 174 } |
170 | 175 |
171 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 176 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
172 const RapporParameters& parameters) { | 177 const RapporParameters& parameters) { |
173 DCHECK(IsInitialized()); | 178 DCHECK(IsInitialized()); |
174 std::map<std::string, RapporMetric*>::iterator it = | 179 std::map<std::string, RapporMetric*>::const_iterator it = |
175 metrics_map_.find(metric_name); | 180 metrics_map_.find(metric_name); |
176 if (metrics_map_.end() != it) { | 181 if (metrics_map_.end() != it) { |
177 RapporMetric* metric = it->second; | 182 RapporMetric* metric = it->second; |
178 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 183 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
179 return metric; | 184 return metric; |
180 } | 185 } |
181 | 186 |
182 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 187 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
183 metrics_map_[metric_name] = new_metric; | 188 metrics_map_[metric_name] = new_metric; |
184 return new_metric; | 189 return new_metric; |
185 } | 190 } |
186 | 191 |
187 } // namespace rappor | 192 } // namespace rappor |
OLD | NEW |