#include "weblayer/browser/background_fetch/background_fetch_delegate_impl.h"
#include <utility>
#include "base/check_op.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "components/background_fetch/download_client.h"
#include "components/background_fetch/job_details.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "content/public/browser/background_fetch_description.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "weblayer/browser/background_download_service_factory.h"
#include "weblayer/browser/browser_context_impl.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/system_network_context_manager.h"
#include "weblayer/public/download_delegate.h"
namespace weblayer {
BackgroundFetchDelegateImpl::BackgroundFetchDelegateImpl(
content::BrowserContext* context)
: background_fetch::BackgroundFetchDelegateBase(context) {}
BackgroundFetchDelegateImpl::~BackgroundFetchDelegateImpl() = default;
void BackgroundFetchDelegateImpl::MarkJobComplete(const std::string& job_id) {
BackgroundFetchDelegateBase::MarkJobComplete(job_id);
#if BUILDFLAG(IS_ANDROID)
background_fetch::JobDetails* job_details =
GetJobDetails(job_id, true);
if (job_details && job_details->job_state ==
background_fetch::JobDetails::State::kJobComplete) {
static constexpr auto kDelay = base::Milliseconds(1500);
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&BackgroundFetchDelegateImpl::DoUpdateUi,
weak_ptr_factory_.GetWeakPtr(), job_id),
kDelay);
}
#endif
}
void BackgroundFetchDelegateImpl::UpdateUI(
const std::string& job_id,
const absl::optional<std::string>& title,
const absl::optional<SkBitmap>& icon) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(title || icon);
DCHECK(!icon || !icon->isNull());
background_fetch::JobDetails* job_details =
GetJobDetails(job_id, true);
if (!job_details)
return;
if (title)
job_details->fetch_description->title = *title;
if (icon)
job_details->fetch_description->icon = *icon;
DoUpdateUi(job_id);
if (auto client = GetClient(job_id))
client->OnUIUpdated(job_id);
}
download::BackgroundDownloadService*
BackgroundFetchDelegateImpl::GetDownloadService() {
return BackgroundDownloadServiceFactory::GetForBrowserContext(context());
}
void BackgroundFetchDelegateImpl::OnJobDetailsCreated(
const std::string& job_id) {
}
void BackgroundFetchDelegateImpl::DoShowUi(const std::string& job_id) {
background_fetch::JobDetails* job = GetJobDetails(job_id);
auto inserted = ui_item_map_.emplace(
std::piecewise_construct, std::forward_as_tuple(job_id),
std::forward_as_tuple(this, job_id, job));
DCHECK(inserted.second);
ProfileImpl::FromBrowserContext(context())
->download_delegate()
->DownloadStarted(&inserted.first->second);
}
void BackgroundFetchDelegateImpl::DoUpdateUi(const std::string& job_id) {
auto iter = ui_item_map_.find(job_id);
if (iter == ui_item_map_.end())
return;
BackgroundFetchDownload* download = &iter->second;
if (!download->HasBeenAddedToUi())
return;
ProfileImpl::FromBrowserContext(context())
->download_delegate()
->DownloadProgressChanged(download);
}
void BackgroundFetchDelegateImpl::DoCleanUpUi(const std::string& job_id) {
ui_item_map_.erase(job_id);
}
}