background_fetch_delegate_base.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef COMPONENTS_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_BASE_H_
  5. #define COMPONENTS_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_BASE_H_
  6. #include <cstdint>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "components/download/public/background_service/download_params.h"
  14. #include "content/public/browser/background_fetch_delegate.h"
  15. #include "ui/gfx/image/image.h"
  16. #include "url/origin.h"
  17. namespace content {
  18. class BrowserContext;
  19. }
  20. namespace download {
  21. class BackgroundDownloadService;
  22. } // namespace download
  23. namespace background_fetch {
  24. struct JobDetails;
  25. // Implementation of BackgroundFetchDelegate using the
  26. // BackgroundDownloadService. This base class is shared by multiple embedders,
  27. // with specializations providing their own UI.
  28. class BackgroundFetchDelegateBase : public content::BackgroundFetchDelegate {
  29. public:
  30. explicit BackgroundFetchDelegateBase(content::BrowserContext* context);
  31. BackgroundFetchDelegateBase(const BackgroundFetchDelegateBase&) = delete;
  32. BackgroundFetchDelegateBase& operator=(const BackgroundFetchDelegateBase&) =
  33. delete;
  34. ~BackgroundFetchDelegateBase() override;
  35. // BackgroundFetchDelegate implementation:
  36. void GetIconDisplaySize(GetIconDisplaySizeCallback callback) override;
  37. void CreateDownloadJob(base::WeakPtr<Client> client,
  38. std::unique_ptr<content::BackgroundFetchDescription>
  39. fetch_description) override;
  40. void DownloadUrl(const std::string& job_id,
  41. const std::string& guid,
  42. const std::string& method,
  43. const GURL& url,
  44. ::network::mojom::CredentialsMode credentials_mode,
  45. const net::NetworkTrafficAnnotationTag& traffic_annotation,
  46. const net::HttpRequestHeaders& headers,
  47. bool has_request_body) override;
  48. void Abort(const std::string& job_id) override;
  49. void MarkJobComplete(const std::string& job_id) override;
  50. // Abort all ongoing downloads and fail the fetch. Currently only used when
  51. // the bytes downloaded exceed the total download size, if specified.
  52. void FailFetch(const std::string& job_id, const std::string& download_guid);
  53. void OnDownloadStarted(
  54. const std::string& guid,
  55. std::unique_ptr<content::BackgroundFetchResponse> response);
  56. void OnDownloadUpdated(const std::string& guid,
  57. uint64_t bytes_uploaded,
  58. uint64_t bytes_downloaded);
  59. void OnDownloadFailed(const std::string& guid,
  60. std::unique_ptr<content::BackgroundFetchResult> result);
  61. void OnDownloadSucceeded(
  62. const std::string& guid,
  63. std::unique_ptr<content::BackgroundFetchResult> result);
  64. // Whether the provided GUID is resuming from the perspective of Background
  65. // Fetch.
  66. bool IsGuidOutstanding(const std::string& guid) const;
  67. // Notifies the OfflineContentAggregator of an interrupted download that is
  68. // in a paused state.
  69. void RestartPausedDownload(const std::string& download_guid);
  70. // Returns the set of download GUIDs that have started but did not finish
  71. // according to Background Fetch. Clears out all references to outstanding
  72. // GUIDs.
  73. std::set<std::string> TakeOutstandingGuids();
  74. // Gets the upload data, if any, associated with the `download_guid`.
  75. void GetUploadData(const std::string& download_guid,
  76. download::GetUploadDataCallback callback);
  77. base::WeakPtr<BackgroundFetchDelegateBase> GetWeakPtr() {
  78. return weak_ptr_factory_.GetWeakPtr();
  79. }
  80. // Called in response to UI interactions.
  81. void PauseDownload(const std::string& job_id);
  82. void ResumeDownload(const std::string& job_id);
  83. // |job_id| is passed as a copy since the Abort workflow may invalidate it.
  84. void CancelDownload(std::string job_id);
  85. // Called when the UI has finished showing. If `activated` is true, it was
  86. // tapped, otherwise it was dismissed.
  87. void OnUiFinished(const std::string& job_id);
  88. // Called when the UI has been tapped.
  89. void OnUiActivated(const std::string& job);
  90. protected:
  91. // Return the download service for `context_`.
  92. virtual download::BackgroundDownloadService* GetDownloadService() = 0;
  93. // Called when a new JobDetails object has been created and inserted in
  94. // |job_details_map_|.
  95. virtual void OnJobDetailsCreated(const std::string& job_id) = 0;
  96. // Called when the UI should first be shown for a Background Fetch job.
  97. virtual void DoShowUi(const std::string& job_id) = 0;
  98. // Called when a change to the given job warrants updating the UI.
  99. virtual void DoUpdateUi(const std::string& job_id) = 0;
  100. // Called to delete the UI object for a job after the UI is no longer needed.
  101. virtual void DoCleanUpUi(const std::string& job_id) = 0;
  102. // Looks up the JobDetails object by `job_id`. `allow_null` defines whether
  103. // it's OK/expected to not find the job.
  104. JobDetails* GetJobDetails(const std::string& job_id, bool allow_null = false);
  105. // Returns the client for a given `job_id`.
  106. base::WeakPtr<Client> GetClient(const std::string& job_id);
  107. content::BrowserContext* context() { return context_; }
  108. private:
  109. // Starts a download according to `params` belonging to `job_id`.
  110. void StartDownload(const std::string& job_id,
  111. download::DownloadParams params,
  112. bool has_request_body);
  113. void OnDownloadReceived(const std::string& guid,
  114. download::DownloadParams::StartResult result);
  115. void DidGetUploadData(const std::string& job_id,
  116. const std::string& download_guid,
  117. download::GetUploadDataCallback callback,
  118. blink::mojom::SerializedBlobPtr blob);
  119. raw_ptr<content::BrowserContext> context_;
  120. // Map from individual download GUIDs to job unique ids.
  121. std::map<std::string, std::string> download_job_id_map_;
  122. // Map from job unique ids to the details of the job.
  123. std::map<std::string, JobDetails> job_details_map_;
  124. base::WeakPtrFactory<BackgroundFetchDelegateBase> weak_ptr_factory_{this};
  125. };
  126. } // namespace background_fetch
  127. #endif // COMPONENTS_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_BASE_H_