1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/rappor/rappor_service.h"
#include "base/base64.h"
#include "base/metrics/field_trial.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/rand_util.h"
#include "base/stl_util.h"
#include "base/time/time.h"
#include "components/metrics/metrics_hashes.h"
#include "components/rappor/log_uploader.h"
#include "components/rappor/proto/rappor_metric.pb.h"
#include "components/rappor/rappor_metric.h"
#include "components/rappor/rappor_pref_names.h"
#include "components/variations/variations_associated_data.h"
namespace rappor {
namespace {
// Seconds before the initial log is generated.
const int kInitialLogIntervalSeconds = 15;
// Interval between ongoing logs.
const int kLogIntervalSeconds = 30 * 60;
const char kMimeType[] = "application/vnd.chrome.rappor";
const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType";
// Constants for the RAPPOR rollout field trial.
const char kRapporRolloutFieldTrialName[] = "RapporRollout";
// Constant for the finch parameter name for the server URL
const char kRapporRolloutServerUrlParam[] = "ServerUrl";
// The rappor server's URL.
const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
GURL GetServerUrl() {
std::string server_url = variations::GetVariationParamValue(
kRapporRolloutFieldTrialName,
kRapporRolloutServerUrlParam);
if (!server_url.empty())
return GURL(server_url);
else
return GURL(kDefaultServerUrl);
}
const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
// ETLD_PLUS_ONE_RAPPOR_TYPE
{128 /* Num cohorts */,
16 /* Bloom filter size bytes */,
2 /* Bloom filter hash count */,
rappor::PROBABILITY_50 /* Fake data probability */,
rappor::PROBABILITY_50 /* Fake one probability */,
rappor::PROBABILITY_75 /* One coin probability */,
rappor::PROBABILITY_25 /* Zero coin probability */,
FINE_LEVEL /* Recording level */},
// COARSE_RAPPOR_TYPE
{128 /* Num cohorts */,
1 /* Bloom filter size bytes */,
2 /* Bloom filter hash count */,
rappor::PROBABILITY_50 /* Fake data probability */,
rappor::PROBABILITY_50 /* Fake one probability */,
rappor::PROBABILITY_75 /* One coin probability */,
rappor::PROBABILITY_25 /* Zero coin probability */,
COARSE_LEVEL /* Recording level */},
};
} // namespace
RapporService::RapporService(
PrefService* pref_service,
const base::Callback<bool(void)> is_incognito_callback)
: pref_service_(pref_service),
is_incognito_callback_(is_incognito_callback),
cohort_(-1),
daily_event_(pref_service,
prefs::kRapporLastDailySample,
kRapporDailyEventHistogram),
recording_level_(RECORDING_DISABLED) {
}
RapporService::~RapporService() {
STLDeleteValues(&metrics_map_);
}
void RapporService::AddDailyObserver(
scoped_ptr<metrics::DailyEvent::Observer> observer) {
daily_event_.AddObserver(observer.Pass());
}
void RapporService::Initialize(net::URLRequestContextGetter* request_context) {
DCHECK(!IsInitialized());
const GURL server_url = GetServerUrl();
if (!server_url.is_valid()) {
DVLOG(1) << server_url.spec() << " is invalid. "
<< "RapporService not started.";
return;
}
DVLOG(1) << "RapporService reporting to " << server_url.spec();
InitializeInternal(make_scoped_ptr(new LogUploader(server_url,
kMimeType,
request_context)),
LoadCohort(),
LoadSecret());
}
void RapporService::Update(RecordingLevel recording_level, bool may_upload) {
DCHECK(IsInitialized());
if (recording_level_ != recording_level) {
if (recording_level == RECORDING_DISABLED) {
DVLOG(1) << "Rappor service stopped due to RECORDING_DISABLED.";
recording_level_ = RECORDING_DISABLED;
CancelNextLogRotation();
} else if (recording_level_ == RECORDING_DISABLED) {
DVLOG(1) << "RapporService started at recording level: "
<< recording_level;
recording_level_ = recording_level;
ScheduleNextLogRotation(
base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds));
} else {
DVLOG(1) << "RapporService recording_level changed:" << recording_level;
recording_level_ = recording_level;
}
}
DVLOG(1) << "RapporService may_upload=" << may_upload;
if (may_upload) {
uploader_->Start();
} else {
uploader_->Stop();
}
}
void RapporService::InitializeInternal(
scoped_ptr<LogUploaderInterface> uploader,
int32_t cohort,
const std::string& secret) {
DCHECK(!IsInitialized());
DCHECK(secret_.empty());
uploader_.swap(uploader);
cohort_ = cohort;
secret_ = secret;
}
void RapporService::SetRecordingLevel(RecordingLevel recording_level) {
recording_level_ = recording_level;
}
void RapporService::CancelNextLogRotation() {
STLDeleteValues(&metrics_map_);
log_rotation_timer_.Stop();
}
void RapporService::ScheduleNextLogRotation(base::TimeDelta interval) {
log_rotation_timer_.Start(FROM_HERE,
interval,
this,
&RapporService::OnLogInterval);
}
void RapporService::OnLogInterval() {
DCHECK(uploader_);
DVLOG(2) << "RapporService::OnLogInterval";
daily_event_.CheckInterval();
RapporReports reports;
if (ExportMetrics(&reports)) {
std::string log_text;
bool success = reports.SerializeToString(&log_text);
DCHECK(success);
DVLOG(1) << "RapporService sending a report of "
<< reports.report_size() << " value(s).";
uploader_->QueueLog(log_text);
}
ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds));
}
// static
void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kRapporSecret, std::string());
registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
metrics::DailyEvent::RegisterPref(registry, prefs::kRapporLastDailySample);
}
int32_t RapporService::LoadCohort() {
// Ignore and delete old cohort parameter.
pref_service_->ClearPref(prefs::kRapporCohortDeprecated);
int32_t cohort = pref_service_->GetInteger(prefs::kRapporCohortSeed);
// If the user is already assigned to a valid cohort, we're done.
if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts)
return cohort;
// This is the first time the client has started the service (or their
// preferences were corrupted). Randomly assign them to a cohort.
cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort);
return cohort;
}
std::string RapporService::LoadSecret() {
std::string secret;
std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret);
if (!secret_base64.empty()) {
bool decoded = base::Base64Decode(secret_base64, &secret);
if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
return secret;
// If the preference fails to decode, or is the wrong size, it must be
// corrupt, so continue as though it didn't exist yet and generate a new
// one.
}
DVLOG(2) << "Generated a new Rappor secret.";
secret = HmacByteVectorGenerator::GenerateEntropyInput();
base::Base64Encode(secret, &secret_base64);
pref_service_->SetString(prefs::kRapporSecret, secret_base64);
return secret;
}
bool RapporService::ExportMetrics(RapporReports* reports) {
if (metrics_map_.empty()) {
DVLOG(2) << "metrics_map_ is empty.";
return false;
}
DCHECK_GE(cohort_, 0);
reports->set_cohort(cohort_);
for (std::map<std::string, RapporMetric*>::const_iterator it =
metrics_map_.begin();
it != metrics_map_.end();
++it) {
const RapporMetric* metric = it->second;
RapporReports::Report* report = reports->add_report();
report->set_name_hash(metrics::HashMetricName(it->first));
ByteVector bytes = metric->GetReport(secret_);
report->set_bits(std::string(bytes.begin(), bytes.end()));
}
STLDeleteValues(&metrics_map_);
return true;
}
bool RapporService::IsInitialized() const {
return cohort_ >= 0;
}
void RapporService::RecordSample(const std::string& metric_name,
RapporType type,
const std::string& sample) {
// Ignore the sample if the service hasn't started yet.
if (!IsInitialized())
return;
DCHECK_LT(type, NUM_RAPPOR_TYPES);
const RapporParameters& parameters = kRapporParametersForType[type];
DVLOG(2) << "Recording sample \"" << sample
<< "\" for metric \"" << metric_name
<< "\" of type: " << type;
RecordSampleInternal(metric_name, parameters, sample);
}
void RapporService::RecordSampleInternal(const std::string& metric_name,
const RapporParameters& parameters,
const std::string& sample) {
DCHECK(IsInitialized());
if (is_incognito_callback_.Run()) {
DVLOG(2) << "Metric not logged due to incognito mode.";
return;
}
// Skip this metric if it's reporting level is less than the enabled
// reporting level.
if (recording_level_ < parameters.recording_level) {
DVLOG(2) << "Metric not logged due to recording_level "
<< recording_level_ << " < " << parameters.recording_level;
return;
}
RapporMetric* metric = LookUpMetric(metric_name, parameters);
metric->AddSample(sample);
}
RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
const RapporParameters& parameters) {
DCHECK(IsInitialized());
std::map<std::string, RapporMetric*>::const_iterator it =
metrics_map_.find(metric_name);
if (it != metrics_map_.end()) {
RapporMetric* metric = it->second;
DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
return metric;
}
RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
metrics_map_[metric_name] = new_metric;
return new_metric;
}
} // namespace rappor
|