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 // The number of cohorts we divide clients into. | |
| 23 const int kNumCohorts = 8; | |
|
ulfar
2014/02/07 20:19:02
We'll may want a slightly higher number here, for
Steven Holte
2014/02/07 21:08:16
Changed to 32.
| |
| 24 | |
| 25 // Length of the rappor secret in bytes. | |
| 26 const int kRapporSecretSize = 128; | |
| 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 | |
| 64 void RapporService::Start(PrefService* pref_service, | |
| 65 net::URLRequestContextGetter* request_context) { | |
| 66 GURL server_url = GetServerUrl(); | |
| 67 if (!server_url.is_valid()) | |
| 68 return; | |
| 69 DCHECK(!uploader_); | |
| 70 LoadSecret(pref_service); | |
| 71 LoadCohort(pref_service); | |
| 72 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | |
| 73 log_rotation_timer_.Start( | |
| 74 FROM_HERE, | |
| 75 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | |
| 76 this, | |
| 77 &RapporService::OnLogInterval); | |
| 78 } | |
| 79 | |
| 80 void RapporService::OnLogInterval() { | |
| 81 DCHECK(uploader_); | |
| 82 RapporReports reports; | |
| 83 if (ExportMetrics(&reports)) { | |
| 84 std::string log_text; | |
| 85 bool success = reports.SerializeToString(&log_text); | |
| 86 DCHECK(success); | |
| 87 uploader_->QueueLog(log_text); | |
| 88 } | |
| 89 log_rotation_timer_.Start(FROM_HERE, | |
| 90 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | |
| 91 this, | |
| 92 &RapporService::OnLogInterval); | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 97 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | |
| 98 registry->RegisterIntegerPref(prefs::kRapporCohort, -1); | |
| 99 } | |
| 100 | |
| 101 void RapporService::LoadCohort(PrefService* pref_service) { | |
| 102 DCHECK_EQ(cohort_, -1); | |
| 103 cohort_ = pref_service->GetInteger(prefs::kRapporCohort); | |
| 104 if (cohort_ >= 0 && cohort_ < kNumCohorts) | |
| 105 return; | |
| 106 | |
| 107 cohort_ = base::RandGenerator(kNumCohorts); | |
| 108 pref_service->SetInteger(prefs::kRapporCohort, cohort_); | |
| 109 } | |
| 110 | |
| 111 void RapporService::LoadSecret(PrefService* pref_service) { | |
| 112 DCHECK(secret_.empty()); | |
| 113 std::string secret_base64 = | |
| 114 pref_service->GetString(prefs::kRapporSecret); | |
| 115 if (!secret_base64.empty()) { | |
| 116 bool decoded = base::Base64Decode(secret_base64, &secret_); | |
| 117 if (decoded) | |
| 118 return; | |
| 119 // If the preference fails to decode, it must be corrupt, so continue as | |
| 120 // though it didn't exist yet and generate a new one. | |
| 121 } | |
| 122 | |
| 123 secret_ = base::RandBytesAsString(kRapporSecretSize); | |
| 124 base::Base64Encode(secret_, &secret_base64); | |
| 125 pref_service->SetString(prefs::kRapporSecret, secret_base64); | |
| 126 } | |
| 127 | |
| 128 bool RapporService::ExportMetrics(RapporReports* reports) { | |
| 129 base::AutoLock auto_lock(lock_); | |
| 130 if (metrics_map_.empty()) | |
| 131 return false; | |
| 132 | |
| 133 DCHECK_GE(cohort_, 0); | |
| 134 reports->set_cohort(cohort_); | |
| 135 | |
| 136 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin(); | |
| 137 metrics_map_.end() != it; | |
| 138 ++it) { | |
| 139 const RapporMetric* metric = it->second; | |
| 140 RapporReports::Report* report = reports->add_report(); | |
| 141 report->set_name_hash(metrics::HashMetricName(it->first)); | |
| 142 ByteVector bytes = metric->GetReport(secret_); | |
| 143 report->set_bits(std::string(bytes.begin(), bytes.end())); | |
| 144 } | |
| 145 STLDeleteContainerPairSecondPointers( | |
| 146 metrics_map_.begin(), metrics_map_.end()); | |
| 147 metrics_map_.clear(); | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 bool RapporService::IsInitialized() const { | |
| 152 return cohort_ >= 0; | |
| 153 } | |
| 154 | |
| 155 void RapporService::RecordSample(const std::string& metric_name, | |
| 156 RapporType type, | |
| 157 const std::string& sample) { | |
| 158 // Ignore the sample if the service hasn't started yet. | |
| 159 if (!IsInitialized()) | |
| 160 return; | |
| 161 DCHECK_LT(type, NUM_RAPPOR_TYPES); | |
| 162 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | |
| 163 } | |
| 164 | |
| 165 void RapporService::RecordSampleInternal(const std::string& metric_name, | |
| 166 const RapporParameters& parameters, | |
| 167 const std::string& sample) { | |
| 168 DCHECK(IsInitialized()); | |
| 169 base::AutoLock auto_lock(lock_); | |
| 170 | |
| 171 RapporMetric* metric = LookupMetric(metric_name, parameters); | |
| 172 metric->AddSample(sample); | |
| 173 } | |
| 174 | |
| 175 RapporMetric* RapporService::LookupMetric(const std::string& metric_name, | |
| 176 const RapporParameters& parameters) { | |
| 177 DCHECK(IsInitialized()); | |
| 178 std::map<std::string, RapporMetric*>::iterator it = | |
| 179 metrics_map_.find(metric_name); | |
| 180 if (metrics_map_.end() != it) { | |
| 181 RapporMetric* metric = it->second; | |
| 182 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | |
| 183 return metric; | |
| 184 } | |
| 185 | |
| 186 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | |
| 187 metrics_map_[metric_name] = new_metric; | |
| 188 return new_metric; | |
| 189 } | |
| 190 | |
| 191 } // namespace rappor | |
| OLD | NEW |