OLD | NEW |
---|---|
(Empty) | |
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 | |
13 namespace system_logs { | |
14 | |
15 namespace { | |
16 | |
17 // The ID of the product specific data as stored by the feedback server. | |
18 constexpr char kCrashReportIds[] = "crash_report_ids"; | |
19 | |
20 // The maximum number of crashes we retrieve from the crash list. | |
21 constexpr size_t kMaxCrashesCountToRetrieve = 10; | |
22 | |
23 // We are only interested in crashes that took place within the last hour. | |
24 constexpr base::TimeDelta kOneHourTimeDelta = base::TimeDelta::FromHours(1); | |
25 | |
26 } // namespace | |
27 | |
28 CrashIdsSource::CrashIdsSource() | |
29 : SystemLogsSource("CrashId"), | |
30 crash_upload_list_(CreateCrashUploadList(this)), | |
Rahul Chaturvedi
2016/09/08 19:58:24
What happens if the system logs fetcher is deleted
afakhry
2016/09/09 22:20:10
We can make it a weak_ptr, but is it really needed
Rahul Chaturvedi
2016/09/12 22:31:21
Ah - in that case just document that below where w
| |
31 pending_crash_list_loading_(false) {} | |
32 | |
33 CrashIdsSource::~CrashIdsSource() {} | |
34 | |
35 void CrashIdsSource::Fetch(const SysLogsSourceCallback& callback) { | |
36 // Unretained since we own these callbacks. | |
37 pending_requests_.emplace_back(base::Bind( | |
38 &CrashIdsSource::RespondWithCrashIds, base::Unretained(this), callback)); | |
39 | |
40 if (pending_crash_list_loading_) | |
41 return; | |
42 | |
43 pending_crash_list_loading_ = true; | |
44 crash_upload_list_->LoadUploadListAsynchronously(); | |
45 } | |
46 | |
47 void CrashIdsSource::OnUploadListAvailable() { | |
48 pending_crash_list_loading_ = false; | |
49 | |
50 // Only get the IDs of crashes that occurred within the last hour (if any). | |
51 std::vector<UploadList::UploadInfo> crashes; | |
52 crash_upload_list_->GetUploads(kMaxCrashesCountToRetrieve, &crashes); | |
53 const base::Time now = base::Time::Now(); | |
54 crash_ids_list_.clear(); | |
55 crash_ids_list_.reserve(kMaxCrashesCountToRetrieve); | |
56 | |
57 // The feedback server expects the crash IDs to be a comma-separated list. | |
58 for (const auto& crash_info : crashes) { | |
59 if (now - crash_info.capture_time < kOneHourTimeDelta) { | |
60 const std::string& crash_id = crash_info.upload_id; | |
61 crash_ids_list_.append(crash_ids_list_.empty() ? crash_id | |
62 : ", " + crash_id); | |
63 } | |
64 } | |
65 | |
66 for (const auto& request : pending_requests_) | |
67 request.Run(); | |
68 | |
69 pending_requests_.clear(); | |
70 } | |
71 | |
72 void CrashIdsSource::RespondWithCrashIds( | |
73 const SysLogsSourceCallback& callback) const { | |
74 std::unique_ptr<SystemLogsResponse> response(new SystemLogsResponse()); | |
75 (*response)[kCrashReportIds] = crash_ids_list_; | |
76 | |
77 // We must respond anyways. | |
78 callback.Run(response.get()); | |
79 } | |
80 | |
81 } // namespace system_logs | |
OLD | NEW |