combining_upload_list.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2021 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. #include "components/upload_list/combining_upload_list.h"
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <utility>
  8. #include "base/time/time.h"
  9. CombiningUploadList::CombiningUploadList(
  10. std::vector<scoped_refptr<UploadList>> sublists)
  11. : sublists_(std::move(sublists)) {
  12. DCHECK(!sublists_.empty());
  13. }
  14. CombiningUploadList::~CombiningUploadList() = default;
  15. std::vector<UploadList::UploadInfo> CombiningUploadList::LoadUploadList() {
  16. std::vector<UploadInfo> uploads;
  17. for (const auto& sublist : sublists_) {
  18. std::vector<UploadInfo> sublist_uploads = sublist->LoadUploadList();
  19. uploads.reserve(uploads.size() + sublist_uploads.size());
  20. std::move(sublist_uploads.begin(), sublist_uploads.end(),
  21. std::back_inserter(uploads));
  22. }
  23. // UploadList expects the list to be sorted, newest first. We sort by
  24. // capture_time if we have it because that's the most stable (won't change
  25. // if a crash is uploaded), but we'll use upload_time if that's all we have.
  26. std::sort(uploads.begin(), uploads.end(),
  27. [](const UploadInfo& a, const UploadInfo& b) {
  28. base::Time time_a =
  29. a.capture_time.is_null() ? a.upload_time : a.capture_time;
  30. base::Time time_b =
  31. b.capture_time.is_null() ? b.upload_time : b.capture_time;
  32. return time_a > time_b;
  33. });
  34. return uploads;
  35. }
  36. void CombiningUploadList::ClearUploadList(const base::Time& begin,
  37. const base::Time& end) {
  38. for (const scoped_refptr<UploadList>& sublist : sublists_) {
  39. sublist->ClearUploadList(begin, end);
  40. }
  41. }
  42. void CombiningUploadList::RequestSingleUpload(const std::string& local_id) {
  43. for (const scoped_refptr<UploadList>& sublist : sublists_) {
  44. sublist->RequestSingleUpload(local_id);
  45. }
  46. }