2016-12-12 11:05:29 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/common/cef_crash_report_upload_thread.h"
|
|
|
|
|
2020-07-08 19:23:29 +02:00
|
|
|
#include "base/notreached.h"
|
2018-02-15 01:12:09 +01:00
|
|
|
#include "libcef/common/cef_crash_report_utils.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "third_party/crashpad/crashpad/client/settings.h"
|
|
|
|
|
|
|
|
using namespace crashpad;
|
|
|
|
|
|
|
|
CefCrashReportUploadThread::CefCrashReportUploadThread(
|
|
|
|
CrashReportDatabase* database,
|
|
|
|
const std::string& url,
|
2017-12-07 22:44:24 +01:00
|
|
|
const Options& options,
|
2022-09-26 21:30:45 +02:00
|
|
|
ProcessPendingReportsObservationCallback callback,
|
2016-12-12 11:05:29 +01:00
|
|
|
int max_uploads)
|
2022-09-26 21:30:45 +02:00
|
|
|
: CrashReportUploadThread(database, url, options, std::move(callback)),
|
2017-05-17 11:29:28 +02:00
|
|
|
max_uploads_(max_uploads) {}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefCrashReportUploadThread::~CefCrashReportUploadThread() = default;
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
void CefCrashReportUploadThread::ProcessPendingReports() {
|
|
|
|
if (BackoffPending()) {
|
|
|
|
// Try again later.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MaxUploadsEnabled()) {
|
|
|
|
// Retrieve all completed reports.
|
|
|
|
std::vector<CrashReportDatabase::Report> reports;
|
|
|
|
if (database_->GetCompletedReports(&reports) !=
|
2017-05-17 11:29:28 +02:00
|
|
|
CrashReportDatabase::kNoError) {
|
2016-12-12 11:05:29 +01:00
|
|
|
// The database is sick. It might be prudent to stop trying to poke it
|
|
|
|
// from this thread by abandoning the thread altogether. On the other
|
|
|
|
// hand, if the problem is transient, it might be possible to talk to it
|
|
|
|
// again on the next pass. For now, take the latter approach.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const time_t now = time(nullptr);
|
|
|
|
const int kSeconds = 60 * 60 * 24; // 24 hours
|
|
|
|
|
|
|
|
// Count how many reports have completed in the last 24 hours.
|
|
|
|
recent_upload_ct_ = 0;
|
|
|
|
for (const CrashReportDatabase::Report& report : reports) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (report.last_upload_attempt_time > now - kSeconds) {
|
2016-12-12 11:05:29 +01:00
|
|
|
recent_upload_ct_++;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue with processing pending reports.
|
|
|
|
CrashReportUploadThread::ProcessPendingReports();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCrashReportUploadThread::ProcessPendingReport(
|
|
|
|
const CrashReportDatabase::Report& report) {
|
|
|
|
// Always allow upload if it's been explicitly requested by the user.
|
|
|
|
if (!report.upload_explicitly_requested) {
|
|
|
|
if (!UploadsEnabled()) {
|
2023-01-02 23:59:03 +01:00
|
|
|
// Don't attempt an upload if there's no URL or if uploads have been
|
|
|
|
// disabled in the database's settings.
|
2016-12-12 11:05:29 +01:00
|
|
|
database_->SkipReportUpload(
|
|
|
|
report.uuid, Metrics::CrashSkippedReason::kUploadsDisabled);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MaxUploadsExceeded()) {
|
|
|
|
// Don't send uploads if the rate limit has been exceeded.
|
|
|
|
database_->SkipReportUpload(
|
|
|
|
report.uuid, Metrics::CrashSkippedReason::kUploadThrottled);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BackoffPending()) {
|
|
|
|
// Try again later.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
std::unique_ptr<const CrashReportDatabase::UploadReport> upload_report;
|
2016-12-12 11:05:29 +01:00
|
|
|
CrashReportDatabase::OperationStatus status =
|
|
|
|
database_->GetReportForUploading(report.uuid, &upload_report);
|
|
|
|
switch (status) {
|
|
|
|
case CrashReportDatabase::kNoError:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CrashReportDatabase::kBusyError:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CrashReportDatabase::kReportNotFound:
|
|
|
|
case CrashReportDatabase::kFileSystemError:
|
|
|
|
case CrashReportDatabase::kDatabaseError:
|
2023-01-02 23:59:03 +01:00
|
|
|
// In these cases, SkipReportUpload() might not work either, but it's best
|
2016-12-12 11:05:29 +01:00
|
|
|
// to at least try to get the report out of the way.
|
|
|
|
database_->SkipReportUpload(report.uuid,
|
|
|
|
Metrics::CrashSkippedReason::kDatabaseError);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CrashReportDatabase::kCannotRequestUpload:
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2016-12-12 11:05:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string response_body;
|
2018-04-19 17:44:42 +02:00
|
|
|
UploadResult upload_result =
|
|
|
|
UploadReport(upload_report.get(), &response_body);
|
2016-12-12 11:05:29 +01:00
|
|
|
switch (upload_result) {
|
|
|
|
case UploadResult::kSuccess:
|
|
|
|
// The upload completed successfully.
|
2018-04-19 17:44:42 +02:00
|
|
|
database_->RecordUploadComplete(std::move(upload_report), response_body);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (MaxUploadsEnabled()) {
|
2016-12-12 11:05:29 +01:00
|
|
|
recent_upload_ct_++;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
ResetBackoff();
|
|
|
|
break;
|
|
|
|
case UploadResult::kPermanentFailure:
|
|
|
|
// The upload should never be retried.
|
|
|
|
database_->SkipReportUpload(report.uuid,
|
|
|
|
Metrics::CrashSkippedReason::kUploadFailed);
|
|
|
|
break;
|
|
|
|
case UploadResult::kRetry:
|
|
|
|
// The upload will be retried after a reasonable backoff delay. Since we
|
|
|
|
// didn't successfully upload it we won't count it against the rate limit.
|
|
|
|
IncreaseBackoff();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 01:12:09 +01:00
|
|
|
CrashReportUploadThread::ParameterMap
|
|
|
|
CefCrashReportUploadThread::FilterParameters(const ParameterMap& parameters) {
|
|
|
|
return crash_report_utils::FilterParameters(parameters);
|
|
|
|
}
|
|
|
|
|
2016-12-12 11:05:29 +01:00
|
|
|
bool CefCrashReportUploadThread::UploadsEnabled() const {
|
|
|
|
Settings* const settings = database_->GetSettings();
|
|
|
|
bool uploads_enabled;
|
2017-05-17 11:29:28 +02:00
|
|
|
return !url_.empty() && settings->GetUploadsEnabled(&uploads_enabled) &&
|
|
|
|
uploads_enabled;
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCrashReportUploadThread::MaxUploadsEnabled() const {
|
2017-12-07 22:44:24 +01:00
|
|
|
return options_.rate_limit && max_uploads_ > 0;
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCrashReportUploadThread::MaxUploadsExceeded() const {
|
|
|
|
return MaxUploadsEnabled() && recent_upload_ct_ >= max_uploads_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCrashReportUploadThread::BackoffPending() const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!options_.rate_limit) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
Settings* const settings = database_->GetSettings();
|
|
|
|
|
|
|
|
time_t next_upload_time;
|
|
|
|
if (settings->GetNextUploadAttemptTime(&next_upload_time) &&
|
|
|
|
next_upload_time > 0) {
|
|
|
|
const time_t now = time(nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (now < next_upload_time) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCrashReportUploadThread::IncreaseBackoff() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!options_.rate_limit) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
const int kHour = 60 * 60; // 1 hour
|
|
|
|
const int kBackoffSchedule[] = {
|
2017-05-17 11:29:28 +02:00
|
|
|
kHour / 4, // 15 minutes
|
|
|
|
kHour, // 1 hour
|
|
|
|
kHour * 2, // 2 hours
|
|
|
|
kHour * 4, // 4 hours
|
|
|
|
kHour * 8, // 8 hours
|
|
|
|
kHour * 24, // 24 hours
|
2016-12-12 11:05:29 +01:00
|
|
|
};
|
|
|
|
const int kBackoffScheduleSize =
|
|
|
|
sizeof(kBackoffSchedule) / sizeof(kBackoffSchedule[0]);
|
|
|
|
|
|
|
|
Settings* settings = database_->GetSettings();
|
|
|
|
|
|
|
|
int backoff_step = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (settings->GetBackoffStep(&backoff_step) && backoff_step < 0) {
|
2016-12-12 11:05:29 +01:00
|
|
|
backoff_step = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (++backoff_step > kBackoffScheduleSize) {
|
2016-12-12 11:05:29 +01:00
|
|
|
backoff_step = kBackoffScheduleSize;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
time_t next_upload_time = time(nullptr); // now
|
|
|
|
next_upload_time += kBackoffSchedule[backoff_step - 1];
|
|
|
|
|
|
|
|
settings->SetBackoffStep(backoff_step);
|
|
|
|
settings->SetNextUploadAttemptTime(next_upload_time);
|
|
|
|
|
|
|
|
if (max_uploads_ > 1) {
|
|
|
|
// If the server is having trouble then we don't want to send many crash
|
|
|
|
// reports after the backoff expires. Reduce max uploads to 1 per 24 hours
|
|
|
|
// until the client is restarted.
|
|
|
|
max_uploads_ = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCrashReportUploadThread::ResetBackoff() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!options_.rate_limit) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
Settings* settings = database_->GetSettings();
|
|
|
|
settings->SetBackoffStep(0);
|
|
|
|
settings->SetNextUploadAttemptTime(0);
|
|
|
|
}
|