background_fetch_delegate_impl.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_delegate_impl.h"
  5. #include <utility>
  6. #include "base/check_op.h"
  7. #include "build/build_config.h"
  8. #include "components/background_fetch/download_client.h"
  9. #include "components/background_fetch/job_details.h"
  10. #include "components/content_settings/core/common/content_settings_types.h"
  11. #include "content/public/browser/background_fetch_description.h"
  12. #include "content/public/browser/browser_context.h"
  13. #include "content/public/browser/browser_thread.h"
  14. #include "content/public/browser/web_contents.h"
  15. #include "weblayer/browser/background_download_service_factory.h"
  16. #include "weblayer/browser/browser_context_impl.h"
  17. #include "weblayer/browser/profile_impl.h"
  18. #include "weblayer/browser/system_network_context_manager.h"
  19. #include "weblayer/public/download_delegate.h"
  20. namespace weblayer {
  21. BackgroundFetchDelegateImpl::BackgroundFetchDelegateImpl(
  22. content::BrowserContext* context)
  23. : background_fetch::BackgroundFetchDelegateBase(context) {}
  24. BackgroundFetchDelegateImpl::~BackgroundFetchDelegateImpl() = default;
  25. void BackgroundFetchDelegateImpl::MarkJobComplete(const std::string& job_id) {
  26. BackgroundFetchDelegateBase::MarkJobComplete(job_id);
  27. #if BUILDFLAG(IS_ANDROID)
  28. background_fetch::JobDetails* job_details =
  29. GetJobDetails(job_id, /*allow_null=*/true);
  30. if (job_details && job_details->job_state ==
  31. background_fetch::JobDetails::State::kJobComplete) {
  32. // The UI should have already been updated to the Completed state, however,
  33. // sometimes Android drops notification updates if there have been too many
  34. // requested in a short span of time, so make sure the completed state is
  35. // reflected in the UI after a brief delay. See
  36. // https://developer.android.com/training/notify-user/build-notification#Updating
  37. static constexpr auto kDelay = base::Milliseconds(1500);
  38. base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
  39. FROM_HERE,
  40. base::BindOnce(&BackgroundFetchDelegateImpl::DoUpdateUi,
  41. weak_ptr_factory_.GetWeakPtr(), job_id),
  42. kDelay);
  43. }
  44. #endif
  45. }
  46. void BackgroundFetchDelegateImpl::UpdateUI(
  47. const std::string& job_id,
  48. const absl::optional<std::string>& title,
  49. const absl::optional<SkBitmap>& icon) {
  50. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  51. DCHECK(title || icon); // One of the UI options must be updatable.
  52. DCHECK(!icon || !icon->isNull()); // The |icon|, if provided, is not null.
  53. background_fetch::JobDetails* job_details =
  54. GetJobDetails(job_id, /*allow_null=*/true);
  55. if (!job_details)
  56. return;
  57. if (title)
  58. job_details->fetch_description->title = *title;
  59. if (icon)
  60. job_details->fetch_description->icon = *icon;
  61. DoUpdateUi(job_id);
  62. if (auto client = GetClient(job_id))
  63. client->OnUIUpdated(job_id);
  64. }
  65. download::BackgroundDownloadService*
  66. BackgroundFetchDelegateImpl::GetDownloadService() {
  67. return BackgroundDownloadServiceFactory::GetForBrowserContext(context());
  68. }
  69. void BackgroundFetchDelegateImpl::OnJobDetailsCreated(
  70. const std::string& job_id) {
  71. // WebLayer doesn't create the UI until a download actually starts.
  72. }
  73. void BackgroundFetchDelegateImpl::DoShowUi(const std::string& job_id) {
  74. // Create the UI the first time a download starts. (There may be multiple
  75. // downloads for a single background fetch job.)
  76. background_fetch::JobDetails* job = GetJobDetails(job_id);
  77. auto inserted = ui_item_map_.emplace(
  78. std::piecewise_construct, std::forward_as_tuple(job_id),
  79. std::forward_as_tuple(this, job_id, job));
  80. DCHECK(inserted.second);
  81. ProfileImpl::FromBrowserContext(context())
  82. ->download_delegate()
  83. ->DownloadStarted(&inserted.first->second);
  84. }
  85. void BackgroundFetchDelegateImpl::DoUpdateUi(const std::string& job_id) {
  86. auto iter = ui_item_map_.find(job_id);
  87. if (iter == ui_item_map_.end())
  88. return;
  89. BackgroundFetchDownload* download = &iter->second;
  90. if (!download->HasBeenAddedToUi())
  91. return;
  92. ProfileImpl::FromBrowserContext(context())
  93. ->download_delegate()
  94. ->DownloadProgressChanged(download);
  95. }
  96. void BackgroundFetchDelegateImpl::DoCleanUpUi(const std::string& job_id) {
  97. ui_item_map_.erase(job_id);
  98. }
  99. } // namespace weblayer