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

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

Issue 2510803003: Pass RapporService to content/browser/ (Closed)
Patch Set: Fix more compile errors in JNI files Created 4 years 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 <utility>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/metrics_hashes.h"
12 #include "base/time/time.h"
13 #include "components/rappor/log_uploader.h"
14 #include "components/rappor/proto/rappor_metric.pb.h"
15 #include "components/rappor/rappor_metric.h"
16 #include "components/rappor/rappor_pref_names.h"
17 #include "components/rappor/rappor_prefs.h"
18 #include "components/variations/variations_associated_data.h"
19
20 namespace rappor {
21
22 namespace {
23
24 // Seconds before the initial log is generated.
25 const int kInitialLogIntervalSeconds = 15;
26 // Interval between ongoing logs.
27 const int kLogIntervalSeconds = 30 * 60;
28
29 const char kMimeType[] = "application/vnd.chrome.rappor";
30
31 const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType";
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 // The rappor server's URL.
40 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
41
42 GURL GetServerUrl() {
43 std::string server_url = variations::GetVariationParamValue(
44 kRapporRolloutFieldTrialName,
45 kRapporRolloutServerUrlParam);
46 if (!server_url.empty())
47 return GURL(server_url);
48 else
49 return GURL(kDefaultServerUrl);
50 }
51
52 } // namespace
53
54 RapporService::RapporService(
55 PrefService* pref_service,
56 const base::Callback<bool(void)> is_incognito_callback)
57 : pref_service_(pref_service),
58 is_incognito_callback_(is_incognito_callback),
59 cohort_(-1),
60 daily_event_(pref_service,
61 prefs::kRapporLastDailySample,
62 kRapporDailyEventHistogram),
63 recording_groups_(0) {
64 }
65
66 RapporService::~RapporService() {
67 }
68
69 void RapporService::AddDailyObserver(
70 std::unique_ptr<metrics::DailyEvent::Observer> observer) {
71 daily_event_.AddObserver(std::move(observer));
72 }
73
74 void RapporService::Initialize(net::URLRequestContextGetter* request_context) {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 DCHECK(!IsInitialized());
77 const GURL server_url = GetServerUrl();
78 if (!server_url.is_valid()) {
79 DVLOG(1) << server_url.spec() << " is invalid. "
80 << "RapporService not started.";
81 return;
82 }
83 DVLOG(1) << "RapporService reporting to " << server_url.spec();
84 InitializeInternal(
85 base::MakeUnique<LogUploader>(server_url, kMimeType, request_context),
86 internal::LoadCohort(pref_service_), internal::LoadSecret(pref_service_));
87 }
88
89 void RapporService::Update(int recording_groups, bool may_upload) {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 DCHECK(IsInitialized());
92 if (recording_groups_ != recording_groups) {
93 if (recording_groups == 0) {
94 DVLOG(1) << "Rappor service stopped because all groups were disabled.";
95 recording_groups_ = 0;
96 CancelNextLogRotation();
97 } else if (recording_groups_ == 0) {
98 DVLOG(1) << "RapporService started for groups: "
99 << recording_groups;
100 recording_groups_ = recording_groups;
101 ScheduleNextLogRotation(
102 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds));
103 } else {
104 DVLOG(1) << "RapporService recording_groups changed:" << recording_groups;
105 recording_groups_ = recording_groups;
106 }
107 }
108
109 DVLOG(1) << "RapporService recording_groups=" << recording_groups_
110 << " may_upload=" << may_upload;
111 if (may_upload) {
112 uploader_->Start();
113 } else {
114 uploader_->Stop();
115 }
116 }
117
118 // static
119 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
120 internal::RegisterPrefs(registry);
121 }
122
123 void RapporService::InitializeInternal(
124 std::unique_ptr<LogUploaderInterface> uploader,
125 int32_t cohort,
126 const std::string& secret) {
127 DCHECK(thread_checker_.CalledOnValidThread());
128 DCHECK(!IsInitialized());
129 DCHECK(secret_.empty());
130 uploader_.swap(uploader);
131 cohort_ = cohort;
132 secret_ = secret;
133 }
134
135 void RapporService::CancelNextLogRotation() {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 metrics_map_.clear();
138 log_rotation_timer_.Stop();
139 }
140
141 void RapporService::ScheduleNextLogRotation(base::TimeDelta interval) {
142 log_rotation_timer_.Start(FROM_HERE,
143 interval,
144 this,
145 &RapporService::OnLogInterval);
146 }
147
148 void RapporService::OnLogInterval() {
149 DCHECK(thread_checker_.CalledOnValidThread());
150 DCHECK(uploader_);
151 DVLOG(2) << "RapporService::OnLogInterval";
152 daily_event_.CheckInterval();
153 RapporReports reports;
154 if (ExportMetrics(&reports)) {
155 std::string log_text;
156 bool success = reports.SerializeToString(&log_text);
157 DCHECK(success);
158 DVLOG(1) << "RapporService sending a report of "
159 << reports.report_size() << " value(s).";
160 uploader_->QueueLog(log_text);
161 }
162 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds));
163 }
164
165 bool RapporService::ExportMetrics(RapporReports* reports) {
166 DCHECK(thread_checker_.CalledOnValidThread());
167 DCHECK_GE(cohort_, 0);
168 reports->set_cohort(cohort_);
169
170 for (const auto& kv : metrics_map_) {
171 const RapporMetric* metric = kv.second.get();
172 RapporReports::Report* report = reports->add_report();
173 report->set_name_hash(base::HashMetricName(kv.first));
174 ByteVector bytes = metric->GetReport(secret_);
175 report->set_bits(std::string(bytes.begin(), bytes.end()));
176 DVLOG(2) << "Exporting metric " << kv.first;
177 }
178 metrics_map_.clear();
179
180 sampler_.ExportMetrics(secret_, reports);
181
182 DVLOG(2) << "Generated a report with " << reports->report_size()
183 << " metrics.";
184 return reports->report_size() > 0;
185 }
186
187 bool RapporService::IsInitialized() const {
188 return cohort_ >= 0;
189 }
190
191 bool RapporService::RecordingAllowed(const RapporParameters& parameters) {
192 // Skip recording in incognito mode.
193 if (is_incognito_callback_.Run()) {
194 DVLOG(2) << "Metric not logged due to incognito mode.";
195 return false;
196 }
197 // Skip this metric if its recording_group is not enabled.
198 if (!(recording_groups_ & parameters.recording_group)) {
199 DVLOG(2) << "Metric not logged due to recording_group "
200 << recording_groups_ << " < " << parameters.recording_group;
201 return false;
202 }
203 return true;
204 }
205
206 void RapporService::RecordSample(const std::string& metric_name,
207 RapporType type,
208 const std::string& sample) {
209 DCHECK(thread_checker_.CalledOnValidThread());
210 // Ignore the sample if the service hasn't started yet.
211 if (!IsInitialized())
212 return;
213 DCHECK_LT(type, NUM_RAPPOR_TYPES);
214 const RapporParameters& parameters = internal::kRapporParametersForType[type];
215 DVLOG(2) << "Recording sample \"" << sample
216 << "\" for metric \"" << metric_name
217 << "\" of type: " << type;
218 RecordSampleInternal(metric_name, parameters, sample);
219 }
220
221 void RapporService::RecordSampleInternal(const std::string& metric_name,
222 const RapporParameters& parameters,
223 const std::string& sample) {
224 DCHECK(thread_checker_.CalledOnValidThread());
225 DCHECK(IsInitialized());
226 if (!RecordingAllowed(parameters))
227 return;
228 RapporMetric* metric = LookUpMetric(metric_name, parameters);
229 metric->AddSample(sample);
230 }
231
232 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
233 const RapporParameters& parameters) {
234 DCHECK(thread_checker_.CalledOnValidThread());
235 DCHECK(IsInitialized());
236 auto it = metrics_map_.find(metric_name);
237 if (it != metrics_map_.end()) {
238 RapporMetric* metric = it->second.get();
239 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
240 return metric;
241 }
242
243 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
244 metrics_map_[metric_name] = base::WrapUnique(new_metric);
245 return new_metric;
246 }
247
248 std::unique_ptr<Sample> RapporService::CreateSample(RapporType type) {
249 DCHECK(thread_checker_.CalledOnValidThread());
250 DCHECK(IsInitialized());
251 return base::WrapUnique(
252 new Sample(cohort_, internal::kRapporParametersForType[type]));
253 }
254
255 void RapporService::RecordSampleObj(const std::string& metric_name,
256 std::unique_ptr<Sample> sample) {
257 DCHECK(thread_checker_.CalledOnValidThread());
258 if (!RecordingAllowed(sample->parameters()))
259 return;
260 DVLOG(1) << "Recording sample of metric \"" << metric_name << "\"";
261 sampler_.AddSample(metric_name, std::move(sample));
262 }
263
264 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698