#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
#include <stdint.h>
#include <set>
#include <vector>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "components/download/content/public/all_download_item_notifier.h"
#include "components/download/public/common/download_item.h"
#include "components/history/core/browser/history_service.h"
#include "content/public/browser/download_manager.h"
namespace history {
struct DownloadRow;
}
class DownloadHistory : public download::AllDownloadItemNotifier::Observer {
public:
typedef std::set<uint32_t> IdSet;
class HistoryAdapter {
public:
explicit HistoryAdapter(history::HistoryService* history);
HistoryAdapter(const HistoryAdapter&) = delete;
HistoryAdapter& operator=(const HistoryAdapter&) = delete;
virtual ~HistoryAdapter();
virtual void QueryDownloads(
history::HistoryService::DownloadQueryCallback callback);
virtual void CreateDownload(
const history::DownloadRow& info,
history::HistoryService::DownloadCreateCallback callback);
virtual void UpdateDownload(const history::DownloadRow& data,
bool should_commit_immediately);
virtual void RemoveDownloads(const std::set<uint32_t>& ids);
private:
raw_ptr<history::HistoryService> history_;
};
class Observer {
public:
Observer();
virtual ~Observer();
virtual void OnDownloadStored(download::DownloadItem* item,
const history::DownloadRow& info) {}
virtual void OnDownloadsRemoved(const IdSet& ids) {}
virtual void OnHistoryQueryComplete() {}
virtual void OnDownloadHistoryDestroyed() {}
};
static bool IsPersisted(const download::DownloadItem* item);
DownloadHistory(content::DownloadManager* manager,
std::unique_ptr<HistoryAdapter> history);
DownloadHistory(const DownloadHistory&) = delete;
DownloadHistory& operator=(const DownloadHistory&) = delete;
~DownloadHistory() override;
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
private:
void QueryCallback(std::vector<history::DownloadRow> rows);
void LoadHistoryDownloads(const std::vector<history::DownloadRow>& rows);
void MaybeAddToHistory(download::DownloadItem* item);
void ItemAdded(uint32_t id, const history::DownloadRow& info, bool success);
void OnDownloadCreated(content::DownloadManager* manager,
download::DownloadItem* item) override;
void OnDownloadUpdated(content::DownloadManager* manager,
download::DownloadItem* item) override;
void OnDownloadOpened(content::DownloadManager* manager,
download::DownloadItem* item) override;
void OnDownloadRemoved(content::DownloadManager* manager,
download::DownloadItem* item) override;
void ScheduleRemoveDownload(uint32_t download_id);
void RemoveDownloadsBatch();
void OnDownloadRestoredFromHistory(download::DownloadItem* item);
bool NeedToUpdateDownloadHistory(download::DownloadItem* item);
download::AllDownloadItemNotifier notifier_;
std::unique_ptr<HistoryAdapter> history_;
uint32_t loading_id_;
IdSet removing_ids_;
IdSet removed_while_adding_;
bool initial_history_query_complete_;
base::ObserverList<Observer>::Unchecked observers_;
base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_{this};
};
#endif