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

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

Powered by Google App Engine
This is Rietveld 408576698