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 } |
62 | 64 |
63 void RapporService::Start(PrefService* pref_service, | 65 void RapporService::Start(PrefService* pref_service, |
64 net::URLRequestContextGetter* request_context) { | 66 net::URLRequestContextGetter* request_context) { |
65 GURL server_url = GetServerUrl(); | 67 const GURL server_url = GetServerUrl(); |
66 if (!server_url.is_valid()) | 68 if (!server_url.is_valid()) |
67 return; | 69 return; |
68 DCHECK(!uploader_); | 70 DCHECK(!uploader_); |
69 LoadSecret(pref_service); | 71 LoadSecret(pref_service); |
70 LoadCohort(pref_service); | 72 LoadCohort(pref_service); |
71 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 73 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
72 log_rotation_timer_.Start( | 74 log_rotation_timer_.Start( |
73 FROM_HERE, | 75 FROM_HERE, |
74 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 76 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
75 this, | 77 this, |
(...skipping 15 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 their |
| 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 = pref_service->GetString(prefs::kRapporSecret); |
113 pref_service->GetString(prefs::kRapporSecret); | |
114 if (!secret_base64.empty()) { | 118 if (!secret_base64.empty()) { |
115 bool decoded = base::Base64Decode(secret_base64, &secret_); | 119 bool decoded = base::Base64Decode(secret_base64, &secret_); |
116 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | 120 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) |
117 return; | 121 return; |
118 // If the preference fails to decode, or is the wrong size, it must be | 122 // 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 | 123 // corrupt, so continue as though it didn't exist yet and generate a new |
120 // one. | 124 // one. |
121 } | 125 } |
122 | 126 |
123 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); | 127 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); |
124 base::Base64Encode(secret_, &secret_base64); | 128 base::Base64Encode(secret_, &secret_base64); |
125 pref_service->SetString(prefs::kRapporSecret, secret_base64); | 129 pref_service->SetString(prefs::kRapporSecret, secret_base64); |
126 } | 130 } |
127 | 131 |
128 bool RapporService::ExportMetrics(RapporReports* reports) { | 132 bool RapporService::ExportMetrics(RapporReports* reports) { |
129 if (metrics_map_.empty()) | 133 if (metrics_map_.empty()) |
130 return false; | 134 return false; |
131 | 135 |
132 DCHECK_GE(cohort_, 0); | 136 DCHECK_GE(cohort_, 0); |
133 reports->set_cohort(cohort_); | 137 reports->set_cohort(cohort_); |
134 | 138 |
135 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin(); | 139 for (std::map<std::string, RapporMetric*>::const_iterator it = |
136 metrics_map_.end() != it; | 140 metrics_map_.begin(); |
| 141 it != metrics_map_.end(); |
137 ++it) { | 142 ++it) { |
138 const RapporMetric* metric = it->second; | 143 const RapporMetric* metric = it->second; |
139 RapporReports::Report* report = reports->add_report(); | 144 RapporReports::Report* report = reports->add_report(); |
140 report->set_name_hash(metrics::HashMetricName(it->first)); | 145 report->set_name_hash(metrics::HashMetricName(it->first)); |
141 ByteVector bytes = metric->GetReport(secret_); | 146 ByteVector bytes = metric->GetReport(secret_); |
142 report->set_bits(std::string(bytes.begin(), bytes.end())); | 147 report->set_bits(std::string(bytes.begin(), bytes.end())); |
143 } | 148 } |
144 STLDeleteValues(&metrics_map_); | 149 STLDeleteValues(&metrics_map_); |
145 return true; | 150 return true; |
146 } | 151 } |
147 | 152 |
148 bool RapporService::IsInitialized() const { | 153 bool RapporService::IsInitialized() const { |
149 return cohort_ >= 0; | 154 return cohort_ >= 0; |
150 } | 155 } |
151 | 156 |
152 void RapporService::RecordSample(const std::string& metric_name, | 157 void RapporService::RecordSample(const std::string& metric_name, |
153 RapporType type, | 158 RapporType type, |
154 const std::string& sample) { | 159 const std::string& sample) { |
155 // Ignore the sample if the service hasn't started yet. | 160 // Ignore the sample if the service hasn't started yet. |
156 if (!IsInitialized()) | 161 if (!IsInitialized()) |
157 return; | 162 return; |
158 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 163 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
159 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | 164 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); |
160 } | 165 } |
161 | 166 |
162 void RapporService::RecordSampleInternal(const std::string& metric_name, | 167 void RapporService::RecordSampleInternal(const std::string& metric_name, |
163 const RapporParameters& parameters, | 168 const RapporParameters& parameters, |
164 const std::string& sample) { | 169 const std::string& sample) { |
165 DCHECK(IsInitialized()); | 170 DCHECK(IsInitialized()); |
166 | |
167 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 171 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
168 metric->AddSample(sample); | 172 metric->AddSample(sample); |
169 } | 173 } |
170 | 174 |
171 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 175 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
172 const RapporParameters& parameters) { | 176 const RapporParameters& parameters) { |
173 DCHECK(IsInitialized()); | 177 DCHECK(IsInitialized()); |
174 std::map<std::string, RapporMetric*>::iterator it = | 178 std::map<std::string, RapporMetric*>::const_iterator it = |
175 metrics_map_.find(metric_name); | 179 metrics_map_.find(metric_name); |
176 if (metrics_map_.end() != it) { | 180 if (it != metrics_map_.end()) { |
177 RapporMetric* metric = it->second; | 181 RapporMetric* metric = it->second; |
178 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 182 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
179 return metric; | 183 return metric; |
180 } | 184 } |
181 | 185 |
182 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 186 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
183 metrics_map_[metric_name] = new_metric; | 187 metrics_map_[metric_name] = new_metric; |
184 return new_metric; | 188 return new_metric; |
185 } | 189 } |
186 | 190 |
187 } // namespace rappor | 191 } // namespace rappor |
OLD | NEW |