1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/download/download_shelf.h"
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/offline_item_model.h"
#include "chrome/browser/download/offline_item_model_manager.h"
#include "chrome/browser/download/offline_item_model_manager_factory.h"
#include "chrome/browser/download/offline_item_utils.h"
#include "chrome/browser/offline_items_collection/offline_content_aggregator_factory.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/download/download_started_animation.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/download/public/common/download_item.h"
#include "components/offline_items_collection/core/offline_content_aggregator.h"
#include "components/offline_items_collection/core/offline_item.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/animation/animation.h"
DownloadShelf::DownloadShelf(Browser* browser, Profile* profile)
: browser_(browser), profile_(profile) {}
DownloadShelf::~DownloadShelf() = default;
void DownloadShelf::AddDownload(DownloadUIModel::DownloadUIModelPtr model) {
DCHECK(model);
if (model->ShouldRemoveFromShelfWhenComplete()) {
// If we are going to remove the download from the shelf upon completion,
// wait a few seconds to see if it completes quickly. If it's a small
// download, then the user won't have time to interact with it.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&DownloadShelf::ShowDownloadById,
weak_ptr_factory_.GetWeakPtr(), model->GetContentId()),
GetTransientDownloadShowDelay());
} else {
ShowDownload(std::move(model));
}
}
void DownloadShelf::Open() {
if (is_hidden_)
should_show_on_unhide_ = true;
else
DoOpen();
}
void DownloadShelf::Close() {
if (is_hidden_)
should_show_on_unhide_ = false;
else
DoClose();
}
void DownloadShelf::Hide() {
if (is_hidden_)
return;
is_hidden_ = true;
if (IsShowing()) {
should_show_on_unhide_ = true;
DoHide();
}
}
void DownloadShelf::Unhide() {
if (!is_hidden_)
return;
is_hidden_ = false;
if (should_show_on_unhide_) {
should_show_on_unhide_ = false;
DoUnhide();
}
}
base::TimeDelta DownloadShelf::GetTransientDownloadShowDelay() const {
return base::Seconds(2);
}
void DownloadShelf::ShowDownload(DownloadUIModel::DownloadUIModelPtr download) {
if (download->GetState() == download::DownloadItem::COMPLETE &&
download->ShouldRemoveFromShelfWhenComplete())
return;
if (!DownloadCoreServiceFactory::GetForBrowserContext(download->profile())
->IsDownloadUiEnabled())
return;
Unhide();
Open();
const bool should_show_download_started_animation =
download->ShouldShowDownloadStartedAnimation();
DoShowDownload(std::move(download));
// Show the download started animation if:
// - Download started animation is enabled for this download. It is disabled
// for "Save As" downloads and extension installs, for example.
// - Rich animations are enabled.
// - The browser has an active visible WebContents. (browser isn't minimized,
// or running under a test etc.)
if (!should_show_download_started_animation ||
!gfx::Animation::ShouldRenderRichAnimation() || !browser_)
return;
content::WebContents* const shelf_tab =
browser_->tab_strip_model()->GetActiveWebContents();
if (shelf_tab && platform_util::IsVisible(shelf_tab->GetNativeView()))
DownloadStartedAnimation::Show(shelf_tab);
}
void DownloadShelf::ShowDownloadById(
const offline_items_collection::ContentId& id) {
if (OfflineItemUtils::IsDownload(id)) {
auto* const manager = profile()->GetDownloadManager();
if (manager) {
auto* const download = manager->GetDownloadByGuid(id.id);
if (download)
ShowDownload(DownloadItemModel::Wrap(download));
}
} else {
auto* const aggregator =
OfflineContentAggregatorFactory::GetForKey(profile()->GetProfileKey());
if (aggregator) {
aggregator->GetItemById(
id, base::BindOnce(&DownloadShelf::OnGetDownloadDoneForOfflineItem,
weak_ptr_factory_.GetWeakPtr()));
}
}
}
void DownloadShelf::OnGetDownloadDoneForOfflineItem(
const std::optional<offline_items_collection::OfflineItem>& item) {
if (item.has_value()) {
auto* const manager =
OfflineItemModelManagerFactory::GetForBrowserContext(profile());
ShowDownload(OfflineItemModel::Wrap(manager, item.value()));
}
}
|