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 // Length of the rappor secret in bytes. |
| 29 const int kRapporSecretSize = 128; |
| 30 |
| 31 // Seconds before the initial log is generated. |
| 32 const int kInitialLogIntervalSeconds = 15; |
| 33 // Interval between ongoing logs. |
| 34 const int kLogIntervalSeconds = 30 * 60; |
| 35 |
| 36 const char kMimeType[] = "application/vnd.chrome.rappor"; |
| 37 |
| 38 // Constants for the RAPPOR rollout field trial. |
| 39 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; |
| 40 |
| 41 // Constant for the finch parameter name for the server URL |
| 42 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; |
| 43 |
| 44 GURL GetServerUrl() { |
| 45 return GURL(chrome_variations::GetVariationParamValue( |
| 46 kRapporRolloutFieldTrialName, |
| 47 kRapporRolloutServerUrlParam)); |
| 48 } |
| 49 |
| 50 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { |
| 51 { // ETLD_PLUS_ONE_RAPPOR_TYPE |
| 52 16 /* Bloom filter size bytes */, |
| 53 2 /* Bloom filter hash count */, |
| 54 rappor::PROBABILITY_75 /* Fake data probability */, |
| 55 rappor::PROBABILITY_50 /* Fake one probability */, |
| 56 rappor::PROBABILITY_75 /* One coin probability */, |
| 57 rappor::PROBABILITY_50 /* Zero coin probability */ |
| 58 }, |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
| 63 RapporService::RapporService() : cohort_(-1) {} |
| 64 |
| 65 RapporService::~RapporService() {} |
| 66 |
| 67 void RapporService::Start(PrefService* pref_service, |
| 68 net::URLRequestContextGetter* request_context) { |
| 69 GURL server_url = GetServerUrl(); |
| 70 if (!server_url.is_valid()) |
| 71 return; |
| 72 DCHECK(!uploader_); |
| 73 LoadSecret(pref_service); |
| 74 LoadCohort(pref_service); |
| 75 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
| 76 log_rotation_timer_.Start( |
| 77 FROM_HERE, |
| 78 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
| 79 this, |
| 80 &RapporService::OnLogInterval); |
| 81 } |
| 82 |
| 83 void RapporService::OnLogInterval() { |
| 84 DCHECK(uploader_); |
| 85 RapporReports reports; |
| 86 if (ExportMetrics(&reports)) { |
| 87 std::string log_text; |
| 88 bool success = reports.SerializeToString(&log_text); |
| 89 DCHECK(success); |
| 90 uploader_->QueueLog(log_text); |
| 91 } |
| 92 log_rotation_timer_.Start(FROM_HERE, |
| 93 base::TimeDelta::FromSeconds(kLogIntervalSeconds), |
| 94 this, |
| 95 &RapporService::OnLogInterval); |
| 96 } |
| 97 |
| 98 // static |
| 99 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 100 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); |
| 101 registry->RegisterIntegerPref(prefs::kRapporCohort, -1); |
| 102 } |
| 103 |
| 104 void RapporService::LoadCohort(PrefService* pref_service) { |
| 105 DCHECK_EQ(cohort_, -1); |
| 106 cohort_ = pref_service->GetInteger(prefs::kRapporCohort); |
| 107 if (cohort_ >= 0 && cohort_ < kNumCohorts) |
| 108 return; |
| 109 |
| 110 cohort_ = base::RandGenerator(kNumCohorts); |
| 111 pref_service->SetInteger(prefs::kRapporCohort, cohort_); |
| 112 } |
| 113 |
| 114 void RapporService::LoadSecret(PrefService* pref_service) { |
| 115 DCHECK(secret_.empty()); |
| 116 std::string secret_base64 = |
| 117 pref_service->GetString(prefs::kRapporSecret); |
| 118 if (!secret_base64.empty()) { |
| 119 bool decoded = base::Base64Decode(secret_base64, &secret_); |
| 120 if (decoded) |
| 121 return; |
| 122 // If the preference fails to decode, it must be corrupt, so continue as |
| 123 // though it didn't exist yet and generate a new one. |
| 124 } |
| 125 |
| 126 secret_ = base::RandBytesAsString(kRapporSecretSize); |
| 127 base::Base64Encode(secret_, &secret_base64); |
| 128 pref_service->SetString(prefs::kRapporSecret, secret_base64); |
| 129 } |
| 130 |
| 131 bool RapporService::ExportMetrics(RapporReports* reports) { |
| 132 base::AutoLock auto_lock(lock_); |
| 133 if (metrics_map_.empty()) |
| 134 return false; |
| 135 |
| 136 DCHECK_GE(cohort_, 0); |
| 137 reports->set_version(kRapporVersion); |
| 138 reports->set_cohort(cohort_); |
| 139 |
| 140 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin(); |
| 141 metrics_map_.end() != it; |
| 142 ++it) { |
| 143 const RapporMetric* metric = it->second; |
| 144 RapporReports::Report* report = reports->add_report(); |
| 145 report->set_name_hash(metrics::HashMetricName(it->first)); |
| 146 ByteVector bytes = metric->GetReport(secret_); |
| 147 report->set_bits(std::string(bytes.begin(), bytes.end())); |
| 148 } |
| 149 STLDeleteContainerPairSecondPointers( |
| 150 metrics_map_.begin(), metrics_map_.end()); |
| 151 metrics_map_.clear(); |
| 152 return true; |
| 153 } |
| 154 |
| 155 bool RapporService::IsInitialized() const { |
| 156 return cohort_ >= 0; |
| 157 } |
| 158 |
| 159 void RapporService::RecordSample(const std::string& metric_name, |
| 160 RapporType type, |
| 161 const std::string& sample) { |
| 162 // Ignore the sample if the service hasn't started yet. |
| 163 if (!IsInitialized()) |
| 164 return; |
| 165 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
| 166 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); |
| 167 } |
| 168 |
| 169 void RapporService::RecordSampleInternal(const std::string& metric_name, |
| 170 const RapporParameters& parameters, |
| 171 const std::string& sample) { |
| 172 DCHECK(IsInitialized()); |
| 173 base::AutoLock auto_lock(lock_); |
| 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 |