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