chromium/chrome/browser/download/bubble/download_bubble_update_service.cc

// Copyright 2023 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/bubble/download_bubble_update_service.h"

#include <iterator>
#include <optional>
#include <tuple>

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/content_index/content_index_provider_impl.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/bubble/download_bubble_update_service_factory.h"
#include "chrome/browser/download/bubble/download_bubble_utils.h"
#include "chrome/browser/download/bubble/download_display_controller.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_crx_util.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_item_web_app_data.h"
#include "chrome/browser/download/download_ui_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/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "components/download/content/public/all_download_item_notifier.h"
#include "components/download/public/common/download_item.h"
#include "components/offline_items_collection/core/offline_content_provider.h"
#include "components/offline_items_collection/core/offline_item.h"
#include "content/public/browser/download_manager.h"

namespace {

ContentId;
OfflineContentProvider;
OfflineItem;
OfflineItemState;
DownloadState;
DownloadUIModelPtr;
ItemSortKey;
IterMap;
ProgressInfo;
SortedItems;

// Show up to 30 items in total by default.
constexpr size_t kDefaultMaxNumItemsToShow =;
// Cache a few more items of each type than we will return from
// GetAllModelsToDisplay. This gives us some wiggle room and makes it more
// likely that we'll return enough items before backfilling.
constexpr size_t kDefaultExtraItemsToCache =;
// Amount of time to show an item in the bubble. Items older than this duration
// ago will be pruned.
constexpr base::TimeDelta kShowItemInBubbleDuration =;
// Don't send the "download started" notification for an extension or theme
// (crx) download until 2 seconds after it has begun. If it is a small download
// that finishes in under 2 seconds, the download UI does not show at all. If it
// is a large download that takes longer than 2 seconds, show the UI so that the
// user knows Chrome is working on it.
constexpr base::TimeDelta kCrxShowNewItemDelay =;
// Limit the size of the |delayed_crx_guids_| set so it doesn't grow
// unboundedly. It is unlikely that the user would have 20 active crx downloads
// simultaneously.
constexpr int kMaxDelayedCrxGuids =;

template <typename Item>
ItemSortKey::State GetState(const Item& item) {}

template <typename Item>
ItemSortKey GetSortKey(const Item& item) {}

// Helper to get an iterator to the last element in the cache. The cache
// must not be empty.
template <typename Item>
SortedItems<Item>::const_iterator GetLastIter(const SortedItems<Item>& cache) {}

// Returns the earliest creation time for which we will show items in the
// bubble.
base::Time GetCutoffTime() {}

// Updates the count of received vs total bytes. Returns whether progress is
// certain.
bool AddItemProgress(int64_t item_received_bytes,
                     int64_t item_total_bytes,
                     int& in_progress_items,
                     int64_t& received_bytes,
                     int64_t& total_bytes) {}

bool ShouldIncludeModel(const DownloadUIModel* model, base::Time cutoff_time) {}

// Returns whether |model| was eligible to be added to |models|, regardless of
// whether it was actually added.
bool MaybeAddModel(DownloadUIModelPtr model,
                   base::Time cutoff_time,
                   size_t max_num_models,
                   std::vector<DownloadUIModelPtr>& models) {}

// For GetAllModelsToDisplay()'s iteration over the merged caches, don't stop
// until all models have been processed.
bool NeverStop() {}

// `update_is_for_model` is whether the current call to this function was
// triggered on behalf of `model`.
void UpdateInfoForModel(const DownloadUIModel& model,
                        bool update_is_for_model,
                        base::Time cutoff_time,
                        DownloadBubbleDisplayInfo& info) {}

bool BrowserMatchesWebAppData(const Browser* browser,
                              const DownloadItemWebAppData* data) {}

}  // namespace

bool DownloadBubbleUpdateService::ItemSortKey::operator<(
    const DownloadBubbleUpdateService::ItemSortKey& other) const {}

bool DownloadBubbleUpdateService::ItemSortKey::operator==(
    const DownloadBubbleUpdateService::ItemSortKey& other) const {}

bool DownloadBubbleUpdateService::ItemSortKey::operator!=(
    const DownloadBubbleUpdateService::ItemSortKey& other) const {}

bool DownloadBubbleUpdateService::ItemSortKey::operator>(
    const DownloadBubbleUpdateService::ItemSortKey& other) const {}

// static
DownloadBubbleUpdateService::ItemSortKey
DownloadBubbleUpdateService::ItemSortKey::Min() {}

DownloadBubbleUpdateService::CacheManager::CacheManager(
    DownloadBubbleUpdateService* update_service)
    :{}

DownloadBubbleUpdateService::CacheManager::~CacheManager() = default;

DownloadBubbleUpdateService::DownloadBubbleUpdateService(Profile* profile)
    :{}

DownloadBubbleUpdateService::~DownloadBubbleUpdateService() = default;

void DownloadBubbleUpdateService::Shutdown() {}

bool DownloadBubbleUpdateService::IsShutDown() const {}

bool DownloadBubbleUpdateService::IsProfileOtr() const {}

size_t DownloadBubbleUpdateService::GetMaxNumItemsToShow() const {}

size_t DownloadBubbleUpdateService::GetNumItemsToCache() const {}

size_t DownloadBubbleUpdateService::CacheManager::GetMaxNumItemsToShow() const {}

size_t DownloadBubbleUpdateService::CacheManager::GetNumItemsToCache() const {}

bool DownloadBubbleUpdateService::CacheManager::IsDownloadItemCacheAtMax()
    const {}

bool DownloadBubbleUpdateService::CacheManager::IsOfflineItemCacheAtMax()
    const {}

DownloadBubbleUpdateService::CacheManager&
DownloadBubbleUpdateService::GetCacheForWebApp(const webapps::AppId& app_id) {}

const DownloadBubbleUpdateService::CacheManager*
DownloadBubbleUpdateService::GetExistingCacheForWebApp(
    const webapps::AppId& app_id) const {}

DownloadBubbleUpdateService::CacheManager*
DownloadBubbleUpdateService::GetExistingCacheForWebApp(
    const webapps::AppId& app_id) {}

DownloadBubbleUpdateService::CacheManager&
DownloadBubbleUpdateService::GetCacheForItem(download::DownloadItem* item) {}

std::vector<DownloadBubbleUpdateService::CacheManager*>
DownloadBubbleUpdateService::GetAllCacheManagers() {}

void DownloadBubbleUpdateService::ObserveDownloadHistory() {}

void DownloadBubbleUpdateService::Initialize(
    content::DownloadManager* manager) {}

void DownloadBubbleUpdateService::InitializeOriginalNotifier(
    content::DownloadManager* manager) {}

content::DownloadManager* DownloadBubbleUpdateService::GetDownloadManager() {}

bool DownloadBubbleUpdateService::IsInitialized() const {}

bool DownloadBubbleUpdateService::CacheManager::GetAllModelsToDisplay(
    std::vector<DownloadUIModelPtr>& models,
    bool force_backfill_download_items) {}

void DownloadBubbleUpdateService::CacheManager::
    GetDownloadItemModelToDisplayOrPrune(
        base::Time cutoff_time,
        std::vector<DownloadUIModel::DownloadUIModelPtr>& models,
        bool& download_item_pruned,
        SortedDownloadItems::iterator& download_item_it) {}

void DownloadBubbleUpdateService::CacheManager::
    GetOfflineItemModelToDisplayOrPrune(
        base::Time cutoff_time,
        std::vector<DownloadUIModel::DownloadUIModelPtr>& models,
        bool& offline_item_pruned,
        SortedOfflineItems::iterator& offline_item_it) {}

bool DownloadBubbleUpdateService::GetAllModelsToDisplay(
    std::vector<DownloadUIModelPtr>& models,
    const webapps::AppId* web_app_id,
    bool force_backfill_download_items) {}

const DownloadBubbleDisplayInfo&
DownloadBubbleUpdateService::CacheManager::GetDisplayInfo() const {}

const DownloadBubbleDisplayInfo& DownloadBubbleUpdateService::GetDisplayInfo(
    const webapps::AppId* web_app_id) {}

void DownloadBubbleUpdateService::CacheManager::UpdateDisplayInfo(
    const std::string& updating_for_item) {}

void DownloadBubbleUpdateService::CacheManager::UpdateDisplayInfo(
    const ContentId& updating_for_item) {}

void DownloadBubbleUpdateService::CacheManager::
    UpdateDisplayInfoForDownloadItem(
        base::optional_ref<const std::string> updating_for_item,
        base::Time cutoff_time,
        DownloadBubbleDisplayInfo& info,
        SortedDownloadItems::iterator& download_item_it) {}

void DownloadBubbleUpdateService::CacheManager::UpdateDisplayInfoForOfflineItem(
    base::optional_ref<const ContentId> updating_for_item,
    base::Time cutoff_time,
    DownloadBubbleDisplayInfo& info,
    SortedOfflineItems::iterator& offline_item_it) {}

bool DownloadBubbleUpdateService::CacheManager::ShouldStopUpdatingDisplayInfo(
    const DownloadBubbleDisplayInfo& info) {}

void DownloadBubbleUpdateService::CacheManager::IterateOverMergedCaches(
    base::RepeatingCallback<void(SortedDownloadItems::iterator&)>
        download_item_action,
    base::RepeatingCallback<void(SortedOfflineItems::iterator&)>
        offline_item_action,
    base::RepeatingCallback<bool()> should_stop) {}

ProgressInfo DownloadBubbleUpdateService::CacheManager::GetProgressInfo()
    const {}

ProgressInfo DownloadBubbleUpdateService::GetProgressInfo(
    const webapps::AppId* web_app_id) const {}

std::vector<std::u16string> DownloadBubbleUpdateService::CacheManager::
    TakeAccessibleAlertsForAnnouncement() {}

std::vector<std::u16string>
DownloadBubbleUpdateService::TakeAccessibleAlertsForAnnouncement(
    const webapps::AppId* web_app_id) {}

void DownloadBubbleUpdateService::OnDownloadCreated(
    content::DownloadManager* manager,
    download::DownloadItem* item) {}

void DownloadBubbleUpdateService::OnDelayedCrxDownloadCreated(
    const std::string& guid) {}

void DownloadBubbleUpdateService::NotifyWindowsOfDownloadItemAdded(
    download::DownloadItem* item) {}

void DownloadBubbleUpdateService::OnDownloadUpdated(
    content::DownloadManager* manager,
    download::DownloadItem* item) {}

void DownloadBubbleUpdateService::CacheManager::OnDownloadItemUpdated(
    download::DownloadItem* item) {}

void DownloadBubbleUpdateService::OnDownloadRemoved(
    content::DownloadManager* manager,
    download::DownloadItem* item) {}

void DownloadBubbleUpdateService::CacheManager::OnDownloadItemRemoved(
    download::DownloadItem* item) {}

void DownloadBubbleUpdateService::OnManagerGoingDown(
    content::DownloadManager* manager) {}

void DownloadBubbleUpdateService::OnItemsAdded(
    const OfflineContentProvider::OfflineItemList& items) {}

void DownloadBubbleUpdateService::OnItemRemoved(const ContentId& id) {}

void DownloadBubbleUpdateService::CacheManager::OnOfflineItemRemoved(
    const ContentId& id) {}

void DownloadBubbleUpdateService::OnItemUpdated(
    const OfflineItem& item,
    const std::optional<offline_items_collection::UpdateDelta>& update_delta) {}

void DownloadBubbleUpdateService::CacheManager::OnOfflineItemUpdated(
    const OfflineItem& item) {}

void DownloadBubbleUpdateService::OnContentProviderGoingDown() {}

void DownloadBubbleUpdateService::OnHistoryQueryComplete() {}

void DownloadBubbleUpdateService::OnDownloadHistoryDestroyed() {}

bool DownloadBubbleUpdateService::CacheManager::MaybeAddDownloadItemToCache(
    download::DownloadItem* item,
    bool is_new,
    bool maybe_add_alert) {}

bool DownloadBubbleUpdateService::CacheManager::MaybeAddOfflineItemToCache(
    const OfflineItem& item,
    bool is_new,
    bool maybe_add_alert) {}

template <typename Id, typename Item>
bool DownloadBubbleUpdateService::CacheManager::AddItemToCacheImpl(
    Item item,
    SortedItems<Item>& cache,
    IterMap<Id, Item>& iter_map) {}

bool DownloadBubbleUpdateService::CacheManager::RemoveDownloadItemFromCache(
    download::DownloadItem* item) {}

bool DownloadBubbleUpdateService::CacheManager::RemoveOfflineItemFromCache(
    const ContentId& id) {}

template <typename Id, typename Item>
bool DownloadBubbleUpdateService::CacheManager::RemoveItemFromCacheImpl(
    const Id& id,
    SortedItems<Item>& cache,
    IterMap<Id, Item>& iter_map) {}

template <typename Id, typename Item>
SortedItems<Item>::iterator
DownloadBubbleUpdateService::CacheManager::RemoveItemFromCacheByIter(
    SortedItems<Item>::iterator iter,
    SortedItems<Item>& cache,
    IterMap<Id, Item>& iter_map) {}

void DownloadBubbleUpdateService::StartBackfillDownloadItems(
    const ItemSortKey& last_key) {}

void DownloadBubbleUpdateService::BackfillDownloadItems(
    const ItemSortKey& last_key) {}

void DownloadBubbleUpdateService::StartBackfillOfflineItems(
    const ItemSortKey& last_key) {}

void DownloadBubbleUpdateService::BackfillOfflineItems(
    const ItemSortKey& last_key,
    const std::vector<OfflineItem>& all_items) {}

void DownloadBubbleUpdateService::CacheManager::DropAllDownloadItems() {}

void DownloadBubbleUpdateService::InitializeDownloadItemsCache() {}

void DownloadBubbleUpdateService::CacheManager::DropAllOfflineItems() {}

void DownloadBubbleUpdateService::StartInitializeOfflineItemsCache() {}

void DownloadBubbleUpdateService::InitializeOfflineItemsCache(
    const std::vector<OfflineItem>& all_items) {}

std::vector<raw_ptr<download::DownloadItem, VectorExperimental>>
DownloadBubbleUpdateService::GetAllDownloadItems() {}

OfflineItemModelManager* DownloadBubbleUpdateService::GetOfflineManager()
    const {}

bool DownloadBubbleUpdateService::CacheManager::MaybeAddDownloadItemModel(
    download::DownloadItem* item,
    base::Time cutoff_time,
    std::vector<DownloadUIModelPtr>& models) {}

bool DownloadBubbleUpdateService::CacheManager::MaybeAddOfflineItemModel(
    const offline_items_collection::OfflineItem& item,
    base::Time cutoff_time,
    std::vector<DownloadUIModelPtr>& models) {}

void DownloadBubbleUpdateService::CacheManager::AppendBackfilledDownloadItems(
    const ItemSortKey& last_key,
    base::Time cutoff_time,
    std::vector<DownloadUIModelPtr>& models) {}

bool DownloadBubbleUpdateService::IsMainCache(
    const DownloadBubbleUpdateService::CacheManager& cache) const {}

void DownloadBubbleUpdateService::OnEphemeralWarningExpired(
    const std::string& guid) {}

#if DCHECK_IS_ON()
void DownloadBubbleUpdateService::CacheManager::ConsistencyCheckCaches() const {}

template <typename Id, typename Item>
void DownloadBubbleUpdateService::CacheManager::ConsistencyCheckImpl(
    const SortedItems<Item>& cache,
    const IterMap<Id, Item>& iter_map) const {}
#endif  // DCHECK_IS_ON()