Commit f505f39f authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Simplify DownloadShelf "show download" code flow.

By inlining GetDownload() into ShowDownloadById() and making
OnGetDownloadDoneForOfflineItem() a member, the code becomes clearer and
more direct (mostly by converting the bound ShowDownload() callback into
direct calls).

Bug: none
Change-Id: I92e424ee658e00aff5fbb243d4b6b574fc6285f3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2296001
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#787928}
parent 4d87f5b2
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h"
#include "base/location.h" #include "base/location.h"
#include "base/optional.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/download/download_core_service.h" #include "chrome/browser/download/download_core_service.h"
...@@ -38,50 +38,6 @@ namespace { ...@@ -38,50 +38,6 @@ namespace {
// Delay before we show a transient download. // Delay before we show a transient download.
const int64_t kDownloadShowDelayInSeconds = 2; const int64_t kDownloadShowDelayInSeconds = 2;
void OnGetDownloadDoneForOfflineItem(
Profile* profile,
base::OnceCallback<void(DownloadUIModel::DownloadUIModelPtr)> callback,
const base::Optional<offline_items_collection::OfflineItem>& offline_item) {
if (!offline_item.has_value())
return;
OfflineItemModelManager* manager =
OfflineItemModelManagerFactory::GetForBrowserContext(profile);
DownloadUIModel::DownloadUIModelPtr model =
OfflineItemModel::Wrap(manager, offline_item.value());
std::move(callback).Run(std::move(model));
}
void GetDownload(
Profile* profile,
const offline_items_collection::ContentId& id,
base::OnceCallback<void(DownloadUIModel::DownloadUIModelPtr)> callback) {
if (OfflineItemUtils::IsDownload(id)) {
content::DownloadManager* download_manager =
content::BrowserContext::GetDownloadManager(profile);
if (!download_manager)
return;
download::DownloadItem* download =
download_manager->GetDownloadByGuid(id.id);
if (!download)
return;
DownloadUIModel::DownloadUIModelPtr model =
DownloadItemModel::Wrap(download);
std::move(callback).Run(std::move(model));
} else {
offline_items_collection::OfflineContentAggregator* aggregator =
OfflineContentAggregatorFactory::GetForKey(profile->GetProfileKey());
if (!aggregator)
return;
aggregator->GetItemById(id, base::BindOnce(&OnGetDownloadDoneForOfflineItem,
profile, std::move(callback)));
}
}
} // namespace } // namespace
DownloadShelf::DownloadShelf(Browser* browser, Profile* profile) DownloadShelf::DownloadShelf(Browser* browser, Profile* profile)
...@@ -184,7 +140,36 @@ void DownloadShelf::ShowDownload(DownloadUIModel::DownloadUIModelPtr download) { ...@@ -184,7 +140,36 @@ void DownloadShelf::ShowDownload(DownloadUIModel::DownloadUIModelPtr download) {
void DownloadShelf::ShowDownloadById( void DownloadShelf::ShowDownloadById(
const offline_items_collection::ContentId& id) { const offline_items_collection::ContentId& id) {
GetDownload(profile_, id, if (OfflineItemUtils::IsDownload(id)) {
base::BindOnce(&DownloadShelf::ShowDownload, content::DownloadManager* download_manager =
weak_ptr_factory_.GetWeakPtr())); content::BrowserContext::GetDownloadManager(profile_);
if (!download_manager)
return;
download::DownloadItem* download =
download_manager->GetDownloadByGuid(id.id);
if (!download)
return;
ShowDownload(DownloadItemModel::Wrap(download));
} else {
offline_items_collection::OfflineContentAggregator* aggregator =
OfflineContentAggregatorFactory::GetForKey(profile_->GetProfileKey());
if (!aggregator)
return;
aggregator->GetItemById(
id, base::BindOnce(&DownloadShelf::OnGetDownloadDoneForOfflineItem,
weak_ptr_factory_.GetWeakPtr()));
}
}
void DownloadShelf::OnGetDownloadDoneForOfflineItem(
const base::Optional<offline_items_collection::OfflineItem>& item) {
if (!item.has_value())
return;
OfflineItemModelManager* manager =
OfflineItemModelManagerFactory::GetForBrowserContext(profile_);
ShowDownload(OfflineItemModel::Wrap(manager, item.value()));
} }
...@@ -14,8 +14,14 @@ ...@@ -14,8 +14,14 @@
class Browser; class Browser;
namespace base {
template <typename T>
class Optional;
} // namespace base
namespace offline_items_collection { namespace offline_items_collection {
struct ContentId; struct ContentId;
struct OfflineItem;
} // namespace offline_items_collection } // namespace offline_items_collection
// This is an abstract base class for platform specific download shelf // This is an abstract base class for platform specific download shelf
...@@ -82,6 +88,11 @@ class DownloadShelf { ...@@ -82,6 +88,11 @@ class DownloadShelf {
// Similar to ShowDownload() but refers to the download using an ID. // Similar to ShowDownload() but refers to the download using an ID.
void ShowDownloadById(const offline_items_collection::ContentId& id); void ShowDownloadById(const offline_items_collection::ContentId& id);
// Callback used by ShowDownloadById() to trigger ShowDownload() once |item|
// has been fetched.
void OnGetDownloadDoneForOfflineItem(
const base::Optional<offline_items_collection::OfflineItem>& item);
Browser* const browser_; Browser* const browser_;
Profile* const profile_; Profile* const profile_;
bool should_show_on_unhide_; bool should_show_on_unhide_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment