1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // ReportingService handles uploading serialized logs to a server.
6
7 #include "components/metrics/reporting_service.h"
8
9 #include <cstdio>
10 #include <memory>
11
12 #include "base/base64.h"
13 #include "base/command_line.h"
14 #include "base/functional/bind.h"
15 #include "base/functional/callback.h"
16 #include "base/logging.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "components/metrics/data_use_tracker.h"
20 #include "components/metrics/log_store.h"
21 #include "components/metrics/metrics_features.h"
22 #include "components/metrics/metrics_log_uploader.h"
23 #include "components/metrics/metrics_service_client.h"
24 #include "components/metrics/metrics_upload_scheduler.h"
25
26 namespace metrics {
27
28 // static
RegisterPrefs(PrefRegistrySimple * registry)29 void ReportingService::RegisterPrefs(PrefRegistrySimple* registry) {
30 DataUseTracker::RegisterPrefs(registry);
31 }
32
ReportingService(MetricsServiceClient * client,PrefService * local_state,size_t max_retransmit_size,MetricsLogsEventManager * logs_event_manager)33 ReportingService::ReportingService(MetricsServiceClient* client,
34 PrefService* local_state,
35 size_t max_retransmit_size,
36 MetricsLogsEventManager* logs_event_manager)
37 : client_(client),
38 local_state_(local_state),
39 max_retransmit_size_(max_retransmit_size),
40 logs_event_manager_(logs_event_manager),
41 reporting_active_(false),
42 log_upload_in_progress_(false),
43 data_use_tracker_(DataUseTracker::Create(local_state)) {
44 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
45 DCHECK(client_);
46 DCHECK(local_state);
47 }
48
~ReportingService()49 ReportingService::~ReportingService() {
50 DisableReporting();
51 }
52
Initialize()53 void ReportingService::Initialize() {
54 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
55 DCHECK(!upload_scheduler_);
56 log_store()->LoadPersistedUnsentLogs();
57 base::RepeatingClosure send_next_log_callback = base::BindRepeating(
58 &ReportingService::SendNextLog, self_ptr_factory_.GetWeakPtr());
59 bool fast_startup_for_testing = client_->ShouldStartUpFastForTesting();
60 upload_scheduler_ = std::make_unique<MetricsUploadScheduler>(
61 send_next_log_callback, fast_startup_for_testing);
62 }
63
Start()64 void ReportingService::Start() {
65 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
66 if (reporting_active_)
67 upload_scheduler_->Start();
68 }
69
Stop()70 void ReportingService::Stop() {
71 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
72 if (upload_scheduler_)
73 upload_scheduler_->Stop();
74 }
75
EnableReporting()76 void ReportingService::EnableReporting() {
77 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
78 if (reporting_active_)
79 return;
80 reporting_active_ = true;
81 Start();
82 }
83
DisableReporting()84 void ReportingService::DisableReporting() {
85 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
86 reporting_active_ = false;
87 Stop();
88 }
89
reporting_active() const90 bool ReportingService::reporting_active() const {
91 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
92 return reporting_active_;
93 }
94
95 //------------------------------------------------------------------------------
96 // private methods
97 //------------------------------------------------------------------------------
98
SendNextLog()99 void ReportingService::SendNextLog() {
100 DVLOG(1) << "SendNextLog";
101 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
102
103 const base::TimeTicks now = base::TimeTicks::Now();
104 LogActualUploadInterval(last_upload_finish_time_.is_null()
105 ? base::TimeDelta()
106 : now - last_upload_finish_time_);
107 last_upload_finish_time_ = now;
108
109 if (!reporting_active()) {
110 upload_scheduler_->StopAndUploadCancelled();
111 return;
112 }
113 if (!log_store()->has_unsent_logs()) {
114 // Should only get here if serializing the log failed somehow.
115 upload_scheduler_->Stop();
116 // Reset backoff interval
117 upload_scheduler_->UploadFinished(true);
118 return;
119 }
120 if (!log_store()->has_staged_log()) {
121 reporting_info_.set_attempt_count(0);
122 log_store()->StageNextLog();
123 }
124
125 // Check whether the log should be uploaded based on user id. If it should not
126 // be sent, then discard the log from the store and notify the scheduler.
127 auto staged_user_id = log_store()->staged_log_user_id();
128 if (staged_user_id.has_value() &&
129 !client_->ShouldUploadMetricsForUserId(staged_user_id.value())) {
130 // Remove the log and update list to disk.
131 log_store()->DiscardStagedLog();
132 log_store()->TrimAndPersistUnsentLogs(/*overwrite_in_memory_store=*/true);
133
134 // Notify the scheduler that the next log should be uploaded. If there are
135 // no more logs, then stop the scheduler.
136 if (!log_store()->has_unsent_logs()) {
137 DVLOG(1) << "Stopping upload_scheduler_.";
138 upload_scheduler_->Stop();
139 }
140 upload_scheduler_->UploadFinished(true);
141 return;
142 }
143
144 // Proceed to stage the log for upload if log size satisfies cellular log
145 // upload constrains.
146 bool upload_canceled = false;
147 bool is_cellular_logic = client_->IsOnCellularConnection();
148 if (is_cellular_logic && data_use_tracker_ &&
149 !data_use_tracker_->ShouldUploadLogOnCellular(
150 log_store()->staged_log().size())) {
151 upload_scheduler_->UploadOverDataUsageCap();
152 upload_canceled = true;
153 } else {
154 SendStagedLog();
155 }
156 if (is_cellular_logic) {
157 LogCellularConstraint(upload_canceled);
158 }
159 }
160
SendStagedLog()161 void ReportingService::SendStagedLog() {
162 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
163 DCHECK(log_store()->has_staged_log());
164 if (!log_store()->has_staged_log())
165 return;
166
167 DCHECK(!log_upload_in_progress_);
168 log_upload_in_progress_ = true;
169
170 if (!log_uploader_) {
171 log_uploader_ = client_->CreateUploader(
172 GetUploadUrl(), GetInsecureUploadUrl(), upload_mime_type(),
173 service_type(),
174 base::BindRepeating(&ReportingService::OnLogUploadComplete,
175 self_ptr_factory_.GetWeakPtr()));
176 }
177
178 reporting_info_.set_attempt_count(reporting_info_.attempt_count() + 1);
179
180 const std::string hash =
181 base::HexEncode(log_store()->staged_log_hash().data(),
182 log_store()->staged_log_hash().size());
183 std::string signature;
184 base::Base64Encode(log_store()->staged_log_signature(), &signature);
185
186 if (logs_event_manager_) {
187 logs_event_manager_->NotifyLogEvent(
188 MetricsLogsEventManager::LogEvent::kLogUploading,
189 log_store()->staged_log_hash());
190 }
191 log_uploader_->UploadLog(log_store()->staged_log(),
192 log_store()->staged_log_metadata(), hash, signature,
193 reporting_info_);
194 }
195
OnLogUploadComplete(int response_code,int error_code,bool was_https,bool force_discard,base::StringPiece force_discard_reason)196 void ReportingService::OnLogUploadComplete(
197 int response_code,
198 int error_code,
199 bool was_https,
200 bool force_discard,
201 base::StringPiece force_discard_reason) {
202 DVLOG(1) << "OnLogUploadComplete:" << response_code;
203 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
204 DCHECK(log_upload_in_progress_);
205 log_upload_in_progress_ = false;
206
207 reporting_info_.set_last_response_code(response_code);
208 reporting_info_.set_last_error_code(error_code);
209 reporting_info_.set_last_attempt_was_https(was_https);
210
211 // Log a histogram to track response success vs. failure rates.
212 LogResponseOrErrorCode(response_code, error_code, was_https);
213
214 bool upload_succeeded = response_code == 200;
215
216 // Staged log could have been removed already (such as by Purge() in some
217 // implementations), otherwise we may remove it here.
218 if (log_store()->has_staged_log()) {
219 // Provide boolean for error recovery (allow us to ignore response_code).
220 bool discard_log = false;
221 base::StringPiece discard_reason;
222
223 const std::string& staged_log = log_store()->staged_log();
224 const size_t log_size = staged_log.length();
225 if (upload_succeeded) {
226 LogSuccessLogSize(log_size);
227 LogSuccessMetadata(staged_log);
228 discard_log = true;
229 discard_reason = "Log upload successful.";
230 } else if (force_discard) {
231 discard_log = true;
232 discard_reason = force_discard_reason;
233 } else if (log_size > max_retransmit_size_) {
234 LogLargeRejection(log_size);
235 discard_log = true;
236 discard_reason =
237 "Failed to upload, and log is too large. Will not attempt to "
238 "retransmit.";
239 } else if (response_code == 400) {
240 // Bad syntax. Retransmission won't work.
241 discard_log = true;
242 discard_reason =
243 "Failed to upload because log has bad syntax. Will not attempt to "
244 "retransmit.";
245 }
246
247 if (!discard_log && logs_event_manager_) {
248 // The log is not discarded, meaning that it has failed to upload. We will
249 // try to retransmit it.
250 logs_event_manager_->NotifyLogEvent(
251 MetricsLogsEventManager::LogEvent::kLogStaged,
252 log_store()->staged_log_hash(),
253 base::StringPrintf("Failed to upload (status code: %d, net error "
254 "code: %d). Staged again for retransmission.",
255 response_code, error_code));
256 }
257
258 if (discard_log) {
259 if (upload_succeeded)
260 log_store()->MarkStagedLogAsSent();
261
262 log_store()->DiscardStagedLog(discard_reason);
263 // Store the updated list to disk now that the removed log is uploaded.
264 log_store()->TrimAndPersistUnsentLogs(/*overwrite_in_memory_store=*/true);
265
266 bool flush_local_state =
267 base::FeatureList::IsEnabled(features::kReportingServiceAlwaysFlush);
268 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
269 // If Chrome is in the background, flush the discarded and trimmed logs
270 // from |local_state_| immediately because the process may be killed at
271 // any time from now without persisting the changes. Otherwise, we may end
272 // up re-uploading the same log in a future session. We do not do this if
273 // Chrome is in the foreground because of the assumption that
274 // |local_state_| will be flushed when convenient, and we do not want to
275 // do more work than necessary on the main thread while Chrome is visible.
276 flush_local_state = flush_local_state || !is_in_foreground_;
277 #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
278 if (flush_local_state) {
279 local_state_->CommitPendingWrite();
280 }
281 }
282 }
283
284 // Error 400 indicates a problem with the log, not with the server, so
285 // don't consider that a sign that the server is in trouble. Similarly, if
286 // |force_discard| is true, do not delay the sending of other logs. For
287 // example, if |force_discard| is true because there are no metrics server
288 // URLs included in this build, do not indicate that the "non-existent server"
289 // is in trouble, which would delay the sending of other logs and causing the
290 // accumulation of logs on disk.
291 bool server_is_healthy =
292 upload_succeeded || response_code == 400 || force_discard;
293
294 if (!log_store()->has_unsent_logs()) {
295 DVLOG(1) << "Stopping upload_scheduler_.";
296 upload_scheduler_->Stop();
297 }
298 upload_scheduler_->UploadFinished(server_is_healthy);
299 }
300
301 } // namespace metrics
302