|
| 1 | +// Copyright 2016 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 "chrome/browser/feedback/system_logs/log_sources/crash_ids_source.h" |
| 6 | + |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include "base/bind.h" |
| 10 | +#include "base/time/time.h" |
| 11 | +#include "chrome/browser/crash_upload_list/crash_upload_list.h" |
| 12 | +#include "components/feedback/feedback_report.h" |
| 13 | + |
| 14 | +namespace system_logs { |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +// The maximum number of crashes we retrieve from the crash list. |
| 19 | +constexpr size_t kMaxCrashesCountToRetrieve = 10; |
| 20 | + |
| 21 | +// The length of the crash ID string. |
| 22 | +constexpr size_t kCrashIdStringSize = 16; |
| 23 | + |
| 24 | +// We are only interested in crashes that took place within the last hour. |
| 25 | +constexpr base::TimeDelta kOneHourTimeDelta = base::TimeDelta::FromHours(1); |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
| 29 | +CrashIdsSource::CrashIdsSource() |
| 30 | + : SystemLogsSource("CrashId"), |
| 31 | + crash_upload_list_(CreateCrashUploadList(this)), |
| 32 | + pending_crash_list_loading_(false) {} |
| 33 | + |
| 34 | +CrashIdsSource::~CrashIdsSource() {} |
| 35 | + |
| 36 | +void CrashIdsSource::Fetch(const SysLogsSourceCallback& callback) { |
| 37 | + // Unretained since we own these callbacks. |
| 38 | + pending_requests_.emplace_back(base::Bind( |
| 39 | + &CrashIdsSource::RespondWithCrashIds, base::Unretained(this), callback)); |
| 40 | + |
| 41 | + if (pending_crash_list_loading_) |
| 42 | + return; |
| 43 | + |
| 44 | + pending_crash_list_loading_ = true; |
| 45 | + crash_upload_list_->LoadUploadListAsynchronously(); |
| 46 | +} |
| 47 | + |
| 48 | +void CrashIdsSource::OnUploadListAvailable() { |
| 49 | + pending_crash_list_loading_ = false; |
| 50 | + |
| 51 | + // Only get the IDs of crashes that occurred within the last hour (if any). |
| 52 | + std::vector<UploadList::UploadInfo> crashes; |
| 53 | + crash_upload_list_->GetUploads(kMaxCrashesCountToRetrieve, &crashes); |
| 54 | + const base::Time now = base::Time::Now(); |
| 55 | + crash_ids_list_.clear(); |
| 56 | + crash_ids_list_.reserve(kMaxCrashesCountToRetrieve * |
| 57 | + (kCrashIdStringSize + 2)); |
| 58 | + |
| 59 | + // The feedback server expects the crash IDs to be a comma-separated list. |
| 60 | + for (const auto& crash_info : crashes) { |
| 61 | + if (now - crash_info.capture_time < kOneHourTimeDelta) { |
| 62 | + const std::string& crash_id = crash_info.upload_id; |
| 63 | + crash_ids_list_.append(crash_ids_list_.empty() ? crash_id |
| 64 | + : ", " + crash_id); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + for (const auto& request : pending_requests_) |
| 69 | + request.Run(); |
| 70 | + |
| 71 | + pending_requests_.clear(); |
| 72 | +} |
| 73 | + |
| 74 | +void CrashIdsSource::RespondWithCrashIds( |
| 75 | + const SysLogsSourceCallback& callback) const { |
| 76 | + std::unique_ptr<SystemLogsResponse> response(new SystemLogsResponse()); |
| 77 | + (*response)[feedback::FeedbackReport::kCrashReportIdsKey] = crash_ids_list_; |
| 78 | + |
| 79 | + // We must respond anyways. |
| 80 | + callback.Run(response.get()); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace system_logs |
0 commit comments