Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/rappor/proto/rappor_metric.pb.h" | |
| 14 #include "components/rappor/rappor_pref_names.h" | |
| 15 #include "components/variations/metrics_util.h" | |
| 16 #include "components/variations/variations_associated_data.h" | |
| 17 | |
| 18 namespace rappor { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Version which is incremented when changes are made in the reporting scheme. | |
| 23 const int kRapporVersion = 1; | |
| 24 | |
| 25 // The number of cohorts we divide clients into. | |
| 26 const int kNumCohorts = 32; | |
| 27 | |
| 28 // Seconds before the initial log is generated. | |
| 29 const int kInitialLogIntervalSeconds = 15; | |
| 30 // Interval between ongoing logs. | |
| 31 const int kLogIntervalSeconds = 30 * 60; | |
| 32 | |
| 33 const char kMimeType[] = "application/vnd.chrome.rappor"; | |
| 34 | |
| 35 // Constants for the RAPPOR rollout field trial. | |
| 36 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; | |
| 37 | |
| 38 // Constant for the finch parameter name for the server URL | |
| 39 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | |
| 40 | |
| 41 GURL GetServerUrl() { | |
| 42 return GURL(chrome_variations::GetVariationParamValue( | |
| 43 kRapporRolloutFieldTrialName, | |
| 44 kRapporRolloutServerUrlParam)); | |
| 45 } | |
| 46 | |
| 47 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { | |
| 48 { // ETLD_PLUS_ONE_RAPPOR_TYPE | |
| 49 16 /* Bloom filter size bytes */, | |
| 50 2 /* Bloom filter hash count */, | |
| 51 rappor::PROBABILITY_75 /* Fake data probability */, | |
| 52 rappor::PROBABILITY_50 /* Fake one probability */, | |
| 53 rappor::PROBABILITY_75 /* One coin probability */, | |
| 54 rappor::PROBABILITY_50 /* Zero coin probability */ | |
| 55 }, | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 RapporService::RapporService() : cohort_(-1) {} | |
| 61 | |
| 62 RapporService::~RapporService() { | |
| 63 STLDeleteContainerPairSecondPointers( | |
| 64 metrics_map_.begin(), metrics_map_.end()); | |
| 65 metrics_map_.clear(); | |
|
Ilya Sherman
2014/02/13 23:23:08
nit: I think STLDeleteValues does both of these.
Steven Holte
2014/02/14 02:53:29
So it does. Fixed.
| |
| 66 } | |
| 67 | |
| 68 void RapporService::Start(PrefService* pref_service, | |
| 69 net::URLRequestContextGetter* request_context) { | |
| 70 GURL server_url = GetServerUrl(); | |
| 71 if (!server_url.is_valid()) | |
| 72 return; | |
| 73 DCHECK(!uploader_); | |
| 74 LoadSecret(pref_service); | |
| 75 LoadCohort(pref_service); | |
| 76 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | |
| 77 log_rotation_timer_.Start( | |
| 78 FROM_HERE, | |
| 79 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | |
| 80 this, | |
| 81 &RapporService::OnLogInterval); | |
| 82 } | |
| 83 | |
| 84 void RapporService::OnLogInterval() { | |
| 85 DCHECK(uploader_); | |
| 86 RapporReports reports; | |
| 87 if (ExportMetrics(&reports)) { | |
| 88 std::string log_text; | |
| 89 bool success = reports.SerializeToString(&log_text); | |
| 90 DCHECK(success); | |
| 91 uploader_->QueueLog(log_text); | |
| 92 } | |
| 93 log_rotation_timer_.Start(FROM_HERE, | |
| 94 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | |
| 95 this, | |
| 96 &RapporService::OnLogInterval); | |
| 97 } | |
| 98 | |
| 99 // static | |
| 100 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 101 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | |
| 102 registry->RegisterIntegerPref(prefs::kRapporCohort, -1); | |
| 103 } | |
| 104 | |
| 105 void RapporService::LoadCohort(PrefService* pref_service) { | |
| 106 DCHECK_EQ(cohort_, -1); | |
| 107 cohort_ = pref_service->GetInteger(prefs::kRapporCohort); | |
| 108 if (cohort_ >= 0 && cohort_ < kNumCohorts) | |
| 109 return; | |
| 110 | |
| 111 cohort_ = base::RandGenerator(kNumCohorts); | |
| 112 pref_service->SetInteger(prefs::kRapporCohort, cohort_); | |
| 113 } | |
| 114 | |
| 115 void RapporService::LoadSecret(PrefService* pref_service) { | |
| 116 DCHECK(secret_.empty()); | |
| 117 std::string secret_base64 = | |
| 118 pref_service->GetString(prefs::kRapporSecret); | |
| 119 if (!secret_base64.empty()) { | |
| 120 bool decoded = base::Base64Decode(secret_base64, &secret_); | |
| 121 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | |
| 122 return; | |
| 123 // If the preference fails to decode, or is the wrong size, it must be | |
| 124 // corrupt, so continue as though it didn't exist yet and generate a new | |
| 125 // one. | |
| 126 } | |
| 127 | |
| 128 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); | |
| 129 base::Base64Encode(secret_, &secret_base64); | |
| 130 pref_service->SetString(prefs::kRapporSecret, secret_base64); | |
| 131 } | |
| 132 | |
| 133 bool RapporService::ExportMetrics(RapporReports* reports) { | |
| 134 if (metrics_map_.empty()) | |
| 135 return false; | |
| 136 | |
| 137 DCHECK_GE(cohort_, 0); | |
| 138 reports->set_version(kRapporVersion); | |
| 139 reports->set_cohort(cohort_); | |
| 140 | |
| 141 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin(); | |
| 142 metrics_map_.end() != it; | |
| 143 ++it) { | |
| 144 const RapporMetric* metric = it->second; | |
| 145 RapporReports::Report* report = reports->add_report(); | |
| 146 report->set_name_hash(metrics::HashMetricName(it->first)); | |
| 147 ByteVector bytes = metric->GetReport(secret_); | |
| 148 report->set_bits(std::string(bytes.begin(), bytes.end())); | |
| 149 } | |
| 150 STLDeleteContainerPairSecondPointers( | |
| 151 metrics_map_.begin(), metrics_map_.end()); | |
| 152 metrics_map_.clear(); | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 bool RapporService::IsInitialized() const { | |
| 157 return cohort_ >= 0; | |
| 158 } | |
| 159 | |
| 160 void RapporService::RecordSample(const std::string& metric_name, | |
| 161 RapporType type, | |
| 162 const std::string& sample) { | |
| 163 // Ignore the sample if the service hasn't started yet. | |
| 164 if (!IsInitialized()) | |
| 165 return; | |
| 166 DCHECK_LT(type, NUM_RAPPOR_TYPES); | |
| 167 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | |
| 168 } | |
| 169 | |
| 170 void RapporService::RecordSampleInternal(const std::string& metric_name, | |
| 171 const RapporParameters& parameters, | |
| 172 const std::string& sample) { | |
| 173 DCHECK(IsInitialized()); | |
| 174 | |
| 175 RapporMetric* metric = LookUpMetric(metric_name, parameters); | |
| 176 metric->AddSample(sample); | |
| 177 } | |
| 178 | |
| 179 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | |
| 180 const RapporParameters& parameters) { | |
| 181 DCHECK(IsInitialized()); | |
| 182 std::map<std::string, RapporMetric*>::iterator it = | |
| 183 metrics_map_.find(metric_name); | |
| 184 if (metrics_map_.end() != it) { | |
| 185 RapporMetric* metric = it->second; | |
| 186 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | |
| 187 return metric; | |
| 188 } | |
| 189 | |
| 190 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | |
| 191 metrics_map_[metric_name] = new_metric; | |
| 192 return new_metric; | |
| 193 } | |
| 194 | |
| 195 } // namespace rappor | |
| OLD | NEW |