background_fetch_download.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "weblayer/browser/background_fetch/background_fetch_download.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "components/background_fetch/job_details.h"
  7. #include "content/public/browser/background_fetch_description.h"
  8. #include "url/origin.h"
  9. #include "weblayer/browser/background_fetch/background_fetch_delegate_impl.h"
  10. using background_fetch::JobDetails;
  11. namespace weblayer {
  12. BackgroundFetchDownload::BackgroundFetchDownload(
  13. BackgroundFetchDelegateImpl* controller,
  14. const std::string& job_id,
  15. const JobDetails* job)
  16. : controller_(controller), job_id_(job_id), job_(job) {
  17. // The only other kind of DownloadImpl uses the DownloadItem's ID as its
  18. // notification ID, and that's constrained to positive integers.
  19. static int id = 0;
  20. if (id > 0)
  21. id = 0;
  22. notification_id_ = --id;
  23. }
  24. BackgroundFetchDownload::~BackgroundFetchDownload() = default;
  25. DownloadState BackgroundFetchDownload::GetState() {
  26. switch (job_->job_state) {
  27. case JobDetails::State::kPendingWillStartDownloading:
  28. case JobDetails::State::kStartedAndDownloading:
  29. return DownloadState::kInProgress;
  30. case JobDetails::State::kPendingWillStartPaused:
  31. case JobDetails::State::kStartedButPaused:
  32. return DownloadState::kPaused;
  33. case JobDetails::State::kCancelled:
  34. return DownloadState::kCancelled;
  35. case JobDetails::State::kDownloadsComplete:
  36. case JobDetails::State::kJobComplete:
  37. return DownloadState::kComplete;
  38. }
  39. }
  40. int64_t BackgroundFetchDownload::GetTotalBytes() {
  41. return job_->GetTotalBytes();
  42. }
  43. int64_t BackgroundFetchDownload::GetReceivedBytes() {
  44. return job_->GetProcessedBytes();
  45. }
  46. void BackgroundFetchDownload::Pause() {
  47. controller_->PauseDownload(job_id_);
  48. }
  49. void BackgroundFetchDownload::Resume() {
  50. controller_->ResumeDownload(job_id_);
  51. }
  52. void BackgroundFetchDownload::Cancel() {
  53. controller_->CancelDownload(job_id_);
  54. }
  55. base::FilePath BackgroundFetchDownload::GetLocation() {
  56. NOTREACHED();
  57. return {};
  58. }
  59. std::u16string BackgroundFetchDownload::GetFileNameToReportToUser() {
  60. return base::UTF8ToUTF16(job_->fetch_description->title);
  61. }
  62. std::string BackgroundFetchDownload::GetMimeType() {
  63. NOTREACHED();
  64. return {};
  65. }
  66. DownloadError BackgroundFetchDownload::GetError() {
  67. NOTREACHED();
  68. return DownloadError::kNoError;
  69. }
  70. int BackgroundFetchDownload::GetNotificationId() {
  71. return notification_id_;
  72. }
  73. bool BackgroundFetchDownload::IsTransient() {
  74. return true;
  75. }
  76. GURL BackgroundFetchDownload::GetSourceUrl() {
  77. return job_->fetch_description->origin.GetURL();
  78. }
  79. const SkBitmap* BackgroundFetchDownload::GetLargeIcon() {
  80. return &job_->fetch_description->icon;
  81. }
  82. void BackgroundFetchDownload::OnFinished(bool activated) {
  83. if (activated)
  84. controller_->OnUiActivated(job_id_);
  85. controller_->OnUiFinished(job_id_);
  86. // |this| is deleted.
  87. }
  88. } // namespace weblayer