Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: components/rappor/rappor_service.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split Name/Parameters Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
24
25 // Seconds before the initial log is generated.
26 const int kInitialLogIntervalSeconds = 15;
27 // Interval between ongoing logs.
28 const int kLogIntervalSeconds = 30 * 60;
29
30 const char kMimeType[] = "application/vnd.chrome.rappor";
31
32 // Constants for the RAPPOR rollout field trial.
33 const char kRapporRolloutFieldTrialName[] = "RapporRollout";
34
35 // Constant for the finch parameter name for the server URL
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl";
37
38 GURL GetServerUrl() {
39 return GURL(chrome_variations::GetVariationParamValue(
40 kRapporRolloutFieldTrialName,
41 kRapporRolloutServerUrlParam));
42 }
43
44 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
45 { // ETLD_PLUS_ONE_RAPPOR_TYPE
46 16 /* Bloom filter size bytes */,
47 2 /* Bloom filter hash count */,
48 rappor::PROBABILITY_75 /* Fake data probability */,
49 rappor::PROBABILITY_50 /* Fake one probability */,
50 rappor::PROBABILITY_75 /* One coin probability */,
51 rappor::PROBABILITY_50 /* Zero coin probability */
52 },
53 };
54
55 } // namespace
56
57 RapporService::RapporService() : cohort_(-1) {}
58
59 RapporService::~RapporService() {}
60
61 void RapporService::Start(PrefService* pref_service,
62 net::URLRequestContextGetter* request_context) {
63 GURL server_url = GetServerUrl();
64 if (!server_url.is_valid())
65 return;
66 DCHECK(!uploader_);
67 LoadSecret(pref_service);
68 LoadCohort(pref_service);
69 uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
70 log_rotation_timer_.Start(
71 FROM_HERE,
72 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
73 this,
74 &RapporService::OnLogInterval);
75 }
76
77 void RapporService::OnLogInterval() {
78 DCHECK(uploader_);
79 RapporReports reports;
80 bool metrics_logged = LogMetrics(&reports);
Alexei Svitkine (slow) 2014/02/05 18:07:01 Nit: Can just inline this into the if and remove t
Steven Holte 2014/02/05 22:44:37 Done.
81 if (metrics_logged) {
82 std::string log_text;
83 bool success = reports.SerializeToString(&log_text);
84 DCHECK(success);
85 uploader_->QueueLog(log_text);
86 }
87 log_rotation_timer_.Start(FROM_HERE,
88 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
89 this,
90 &RapporService::OnLogInterval);
91 }
92
93 // static
94 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
95 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
96 registry->RegisterIntegerPref(prefs::kRapporCohort, -1);
97 }
98
99 void RapporService::LoadCohort(PrefService* pref_service) {
100 DCHECK_EQ(cohort_, -1);
101 cohort_ = pref_service->GetInteger(prefs::kRapporCohort);
102 if (cohort_ >= 0 && cohort_ < kNumCohorts)
103 return;
104
105 cohort_ = base::RandGenerator(kNumCohorts);
106 pref_service->SetInteger(prefs::kRapporCohort, cohort_);
107 }
108
109 void RapporService::LoadSecret(PrefService* pref_service) {
110 DCHECK(secret_.empty());
111 std::string secret_base64 =
112 pref_service->GetString(prefs::kRapporSecret);
113 if (!secret_base64.empty()) {
114 bool decoded = base::Base64Decode(secret_base64, &secret_);
115 if (decoded)
116 return;
117 // If the preference fails to decode, it must be corrupt, so continue as
118 // though it didn't exist yet and generate a new one.
119 }
120
121 secret_ = base::RandBytesAsString(128);
Alexei Svitkine (slow) 2014/02/05 18:07:01 Can you make this a constant at the top of the fil
Steven Holte 2014/02/05 22:44:37 Done.
122 base::Base64Encode(secret_, &secret_base64);
123 pref_service->SetString(prefs::kRapporSecret, secret_base64);
124 }
125
126 void RapporService::LogMetric(const RapporMetric& metric,
127 RapporReports::Report* report) {
128 ByteVector bytes = metric.GetReport(secret_);
Alexei Svitkine (slow) 2014/02/05 18:07:01 DCHECK(!secret_.empty()); before this
Steven Holte 2014/02/05 22:44:37 N/A
129 std::string byte_string(bytes.begin(), bytes.end());
Alexei Svitkine (slow) 2014/02/05 18:07:01 Nit: Can inline this into the call below. At whic
Steven Holte 2014/02/05 22:44:37 Done
130 report->set_bits(byte_string);
131 }
132
133 bool RapporService::LogMetrics(RapporReports* reports) {
134 base::AutoLock auto_lock(lock_);
135 if (metrics_map_.empty())
136 return false;
137
138 DCHECK_GE(cohort_, 0);
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 LogMetric(*metric, report);
148 }
149 STLDeleteContainerPairSecondPointers(
150 metrics_map_.begin(), metrics_map_.end());
151 metrics_map_.clear();
152 return true;
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 (cohort_ < 0)
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_GE(cohort_, 0);
Alexei Svitkine (slow) 2014/02/05 18:07:01 I would make a helper function IsInitialized() whi
Steven Holte 2014/02/05 22:44:37 Done.
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_GE(cohort_, 0);
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698