blob: 616d749bbedd1fc4d2048e6baed95878e390f630 [file] [log] [blame]
[email protected]3d833de2012-05-30 23:32:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]82f37b02010-07-29 22:04:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
6#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
[email protected]82f37b02010-07-29 22:04:577
avie4d7b6f2015-12-26 00:59:188#include <stdint.h>
9
[email protected]3d95e542012-11-20 00:52:0810#include <set>
11#include <vector>
[email protected]88008002011-05-24 23:14:1512
[email protected]a3094072011-10-10 17:14:0513#include "base/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
[email protected]3d95e542012-11-20 00:52:0815#include "base/memory/weak_ptr.h"
16#include "base/observer_list.h"
Xing Liu5c41b0a2017-07-14 19:16:4617#include "components/download/content/public/all_download_item_notifier.h"
Min Qina9f487872018-02-09 20:43:2318#include "components/download/public/common/download_item.h"
sdefresne506dd522015-03-12 18:49:1219#include "components/history/core/browser/history_service.h"
[email protected]3d95e542012-11-20 00:52:0820#include "content/public/browser/download_manager.h"
[email protected]82f37b02010-07-29 22:04:5721
[email protected]3d95e542012-11-20 00:52:0822namespace history {
23struct DownloadRow;
24} // namespace history
[email protected]82f37b02010-07-29 22:04:5725
[email protected]3d95e542012-11-20 00:52:0826// Observes a single DownloadManager and all its DownloadItems, keeping the
27// DownloadDatabase up to date.
Xing Liu5c41b0a2017-07-14 19:16:4628class DownloadHistory : public download::AllDownloadItemNotifier::Observer {
[email protected]82f37b02010-07-29 22:04:5729 public:
avie4d7b6f2015-12-26 00:59:1830 typedef std::set<uint32_t> IdSet;
[email protected]88008002011-05-24 23:14:1531
[email protected]3d95e542012-11-20 00:52:0832 // Caller must guarantee that HistoryService outlives HistoryAdapter.
33 class HistoryAdapter {
34 public:
sdefresnec62efa32015-03-12 22:46:3135 explicit HistoryAdapter(history::HistoryService* history);
Peter Boström53c6c5952021-09-17 09:41:2636
37 HistoryAdapter(const HistoryAdapter&) = delete;
38 HistoryAdapter& operator=(const HistoryAdapter&) = delete;
39
[email protected]3d95e542012-11-20 00:52:0840 virtual ~HistoryAdapter();
[email protected]82f37b02010-07-29 22:04:5741
[email protected]3d95e542012-11-20 00:52:0842 virtual void QueryDownloads(
Sylvain Defresneb6bab872019-06-19 08:09:4843 history::HistoryService::DownloadQueryCallback callback);
[email protected]eda58402011-09-21 19:32:0244
[email protected]3d95e542012-11-20 00:52:0845 virtual void CreateDownload(
46 const history::DownloadRow& info,
Sylvain Defresne70639292019-06-11 13:22:5647 history::HistoryService::DownloadCreateCallback callback);
[email protected]82f37b02010-07-29 22:04:5748
qinmin64558cd2016-11-28 22:30:2949 virtual void UpdateDownload(const history::DownloadRow& data,
50 bool should_commit_immediately);
[email protected]88008002011-05-24 23:14:1551
avie4d7b6f2015-12-26 00:59:1852 virtual void RemoveDownloads(const std::set<uint32_t>& ids);
[email protected]82f37b02010-07-29 22:04:5753
[email protected]3d95e542012-11-20 00:52:0854 private:
Keishi Hattori0e45c022021-11-27 09:25:5255 raw_ptr<history::HistoryService> history_;
[email protected]3d95e542012-11-20 00:52:0856 };
[email protected]82f37b02010-07-29 22:04:5757
[email protected]3d95e542012-11-20 00:52:0858 class Observer {
59 public:
60 Observer();
61 virtual ~Observer();
[email protected]82f37b02010-07-29 22:04:5762
[email protected]530047e2013-07-12 17:02:2563 // Fires when a download is added to or updated in the database, just after
64 // the task is posted to the history thread.
Min Qina9f487872018-02-09 20:43:2365 virtual void OnDownloadStored(download::DownloadItem* item,
[email protected]530047e2013-07-12 17:02:2566 const history::DownloadRow& info) {}
[email protected]82f37b02010-07-29 22:04:5767
[email protected]3d95e542012-11-20 00:52:0868 // Fires when RemoveDownloads messages are sent to the DB thread.
69 virtual void OnDownloadsRemoved(const IdSet& ids) {}
[email protected]82f37b02010-07-29 22:04:5770
asanka7fad54c2016-03-24 20:33:4071 // Fires when the DownloadHistory completes the initial history query.
72 // Unlike the other observer methods, this one is invoked if the initial
73 // history query has already completed by the time the caller calls
74 // AddObserver().
75 virtual void OnHistoryQueryComplete() {}
76
[email protected]3d95e542012-11-20 00:52:0877 // Fires when the DownloadHistory is being destroyed so that implementors
78 // can RemoveObserver() and nullify their DownloadHistory*s.
79 virtual void OnDownloadHistoryDestroyed() {}
80 };
81
[email protected]9dd188f2014-05-15 18:35:2082 // Returns true if the download is persisted. Not reliable when called from
83 // within a DownloadManager::Observer::OnDownloadCreated handler since the
84 // persisted state may not yet have been updated for a download that was
85 // restored from history.
Min Qina9f487872018-02-09 20:43:2386 static bool IsPersisted(const download::DownloadItem* item);
[email protected]3d95e542012-11-20 00:52:0887
88 // Neither |manager| nor |history| may be NULL.
89 // DownloadService creates DownloadHistory some time after DownloadManager is
90 // created and destroys DownloadHistory as DownloadManager is shutting down.
Shakti Sahu4af06b8d2019-03-27 22:17:0291 DownloadHistory(content::DownloadManager* manager,
dchengabbf44652016-04-07 22:23:3992 std::unique_ptr<HistoryAdapter> history);
[email protected]3d95e542012-11-20 00:52:0893
Peter Boström53c6c5952021-09-17 09:41:2694 DownloadHistory(const DownloadHistory&) = delete;
95 DownloadHistory& operator=(const DownloadHistory&) = delete;
96
dcheng0c8ba27e2014-10-21 12:00:5397 ~DownloadHistory() override;
[email protected]3d95e542012-11-20 00:52:0898
99 void AddObserver(Observer* observer);
100 void RemoveObserver(Observer* observer);
[email protected]82f37b02010-07-29 22:04:57101
[email protected]82f37b02010-07-29 22:04:57102 private:
[email protected]3d95e542012-11-20 00:52:08103 // Callback from |history_| containing all entries in the downloads database
104 // table.
Sylvain Defresneb6bab872019-06-19 08:09:48105 void QueryCallback(std::vector<history::DownloadRow> rows);
[email protected]88008002011-05-24 23:14:15106
Min Qin5ca2c5e2018-06-01 19:39:24107 // Called to create all history downloads.
Min Qin6e1bd03c2020-11-24 00:26:32108 void LoadHistoryDownloads(const std::vector<history::DownloadRow>& rows);
Min Qin5ca2c5e2018-06-01 19:39:24109
[email protected]3d95e542012-11-20 00:52:08110 // May add |item| to |history_|.
Min Qina9f487872018-02-09 20:43:23111 void MaybeAddToHistory(download::DownloadItem* item);
[email protected]82f37b02010-07-29 22:04:57112
[email protected]3d95e542012-11-20 00:52:08113 // Callback from |history_| when an item was successfully inserted into the
114 // database.
Min Qin67c62b612019-03-25 19:46:06115 void ItemAdded(uint32_t id, const history::DownloadRow& info, bool success);
[email protected]82f37b02010-07-29 22:04:57116
[email protected]3d95e542012-11-20 00:52:08117 // AllDownloadItemNotifier::Observer
dcheng0c8ba27e2014-10-21 12:00:53118 void OnDownloadCreated(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23119 download::DownloadItem* item) override;
dcheng0c8ba27e2014-10-21 12:00:53120 void OnDownloadUpdated(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23121 download::DownloadItem* item) override;
dcheng0c8ba27e2014-10-21 12:00:53122 void OnDownloadOpened(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23123 download::DownloadItem* item) override;
dcheng0c8ba27e2014-10-21 12:00:53124 void OnDownloadRemoved(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23125 download::DownloadItem* item) override;
[email protected]82f37b02010-07-29 22:04:57126
[email protected]3d95e542012-11-20 00:52:08127 // Schedule a record to be removed from |history_| the next time
128 // RemoveDownloadsBatch() runs. Schedule RemoveDownloadsBatch() to be run soon
129 // if it isn't already scheduled.
avie4d7b6f2015-12-26 00:59:18130 void ScheduleRemoveDownload(uint32_t download_id);
[email protected]3d95e542012-11-20 00:52:08131
[email protected]530047e2013-07-12 17:02:25132 // Removes all |removing_ids_| from |history_|.
[email protected]3d95e542012-11-20 00:52:08133 void RemoveDownloadsBatch();
134
Min Qin7289cfc2018-08-01 23:05:29135 // Called when a download was restored from history.
136 void OnDownloadRestoredFromHistory(download::DownloadItem* item);
137
138 // Check whether an download item needs be updated or added to history DB.
139 bool NeedToUpdateDownloadHistory(download::DownloadItem* item);
Min Qin67332ad2018-07-25 18:31:05140
Shakti Sahu4af06b8d2019-03-27 22:17:02141 download::AllDownloadItemNotifier notifier_;
[email protected]3d95e542012-11-20 00:52:08142
dchengabbf44652016-04-07 22:23:39143 std::unique_ptr<HistoryAdapter> history_;
[email protected]3d95e542012-11-20 00:52:08144
[email protected]530047e2013-07-12 17:02:25145 // Identifier of the item being created in QueryCallback(), matched up with
146 // created items in OnDownloadCreated() so that the item is not re-added to
147 // the database.
avie4d7b6f2015-12-26 00:59:18148 uint32_t loading_id_;
[email protected]3d95e542012-11-20 00:52:08149
[email protected]530047e2013-07-12 17:02:25150 // Identifiers of items that are scheduled for removal from history, to
151 // facilitate batching removals together for database efficiency.
[email protected]3d95e542012-11-20 00:52:08152 IdSet removing_ids_;
153
154 // |GetId()|s of items that were removed while they were being added, so that
[email protected]530047e2013-07-12 17:02:25155 // they can be removed when the database finishes adding them.
156 // TODO(benjhayden) Can this be removed now that it doesn't need to wait for
157 // the db_handle, and can rely on PostTask sequentiality?
[email protected]3d95e542012-11-20 00:52:08158 IdSet removed_while_adding_;
159
asanka7fad54c2016-03-24 20:33:40160 bool initial_history_query_complete_;
161
Trent Apteda250ec3ab2018-08-19 08:52:19162 base::ObserverList<Observer>::Unchecked observers_;
[email protected]3d95e542012-11-20 00:52:08163
Jeremy Roman495db682019-07-12 16:03:24164 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_{this};
[email protected]82f37b02010-07-29 22:04:57165};
166
167#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_