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

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

Powered by Google App Engine
This is Rietveld 408576698