[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 2 | // 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] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 7 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <vector> |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 12 | |
[email protected] | a309407 | 2011-10-10 17:14:05 | [diff] [blame] | 13 | #include "base/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "base/observer_list.h" |
Xing Liu | 5c41b0a | 2017-07-14 19:16:46 | [diff] [blame] | 17 | #include "components/download/content/public/all_download_item_notifier.h" |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 18 | #include "components/download/public/common/download_item.h" |
sdefresne | 506dd52 | 2015-03-12 18:49:12 | [diff] [blame] | 19 | #include "components/history/core/browser/history_service.h" |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 20 | #include "content/public/browser/download_manager.h" |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 21 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 22 | namespace history { |
| 23 | struct DownloadRow; |
| 24 | } // namespace history |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 25 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 26 | // Observes a single DownloadManager and all its DownloadItems, keeping the |
| 27 | // DownloadDatabase up to date. |
Xing Liu | 5c41b0a | 2017-07-14 19:16:46 | [diff] [blame] | 28 | class DownloadHistory : public download::AllDownloadItemNotifier::Observer { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 29 | public: |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 30 | typedef std::set<uint32_t> IdSet; |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 31 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 32 | // Caller must guarantee that HistoryService outlives HistoryAdapter. |
| 33 | class HistoryAdapter { |
| 34 | public: |
sdefresne | c62efa3 | 2015-03-12 22:46:31 | [diff] [blame] | 35 | explicit HistoryAdapter(history::HistoryService* history); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 36 | |
| 37 | HistoryAdapter(const HistoryAdapter&) = delete; |
| 38 | HistoryAdapter& operator=(const HistoryAdapter&) = delete; |
| 39 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 40 | virtual ~HistoryAdapter(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 41 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 42 | virtual void QueryDownloads( |
Sylvain Defresne | b6bab87 | 2019-06-19 08:09:48 | [diff] [blame] | 43 | history::HistoryService::DownloadQueryCallback callback); |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 44 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 45 | virtual void CreateDownload( |
| 46 | const history::DownloadRow& info, |
Sylvain Defresne | 7063929 | 2019-06-11 13:22:56 | [diff] [blame] | 47 | history::HistoryService::DownloadCreateCallback callback); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 48 | |
qinmin | 64558cd | 2016-11-28 22:30:29 | [diff] [blame] | 49 | virtual void UpdateDownload(const history::DownloadRow& data, |
| 50 | bool should_commit_immediately); |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 51 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 52 | virtual void RemoveDownloads(const std::set<uint32_t>& ids); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 53 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 54 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 55 | raw_ptr<history::HistoryService> history_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 56 | }; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 57 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 58 | class Observer { |
| 59 | public: |
| 60 | Observer(); |
| 61 | virtual ~Observer(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 62 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 63 | // Fires when a download is added to or updated in the database, just after |
| 64 | // the task is posted to the history thread. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 65 | virtual void OnDownloadStored(download::DownloadItem* item, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 66 | const history::DownloadRow& info) {} |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 67 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 68 | // Fires when RemoveDownloads messages are sent to the DB thread. |
| 69 | virtual void OnDownloadsRemoved(const IdSet& ids) {} |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 70 | |
asanka | 7fad54c | 2016-03-24 20:33:40 | [diff] [blame] | 71 | // 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] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 77 | // 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] | 9dd188f | 2014-05-15 18:35:20 | [diff] [blame] | 82 | // 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 Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 86 | static bool IsPersisted(const download::DownloadItem* item); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 87 | |
| 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 Sahu | 4af06b8d | 2019-03-27 22:17:02 | [diff] [blame] | 91 | DownloadHistory(content::DownloadManager* manager, |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 92 | std::unique_ptr<HistoryAdapter> history); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 93 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 94 | DownloadHistory(const DownloadHistory&) = delete; |
| 95 | DownloadHistory& operator=(const DownloadHistory&) = delete; |
| 96 | |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 97 | ~DownloadHistory() override; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 98 | |
| 99 | void AddObserver(Observer* observer); |
| 100 | void RemoveObserver(Observer* observer); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 101 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 102 | private: |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 103 | // Callback from |history_| containing all entries in the downloads database |
| 104 | // table. |
Sylvain Defresne | b6bab87 | 2019-06-19 08:09:48 | [diff] [blame] | 105 | void QueryCallback(std::vector<history::DownloadRow> rows); |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 106 | |
Min Qin | 5ca2c5e | 2018-06-01 19:39:24 | [diff] [blame] | 107 | // Called to create all history downloads. |
Min Qin | 6e1bd03c | 2020-11-24 00:26:32 | [diff] [blame] | 108 | void LoadHistoryDownloads(const std::vector<history::DownloadRow>& rows); |
Min Qin | 5ca2c5e | 2018-06-01 19:39:24 | [diff] [blame] | 109 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 110 | // May add |item| to |history_|. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 111 | void MaybeAddToHistory(download::DownloadItem* item); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 112 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 113 | // Callback from |history_| when an item was successfully inserted into the |
| 114 | // database. |
Min Qin | 67c62b61 | 2019-03-25 19:46:06 | [diff] [blame] | 115 | void ItemAdded(uint32_t id, const history::DownloadRow& info, bool success); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 116 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 117 | // AllDownloadItemNotifier::Observer |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 118 | void OnDownloadCreated(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 119 | download::DownloadItem* item) override; |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 120 | void OnDownloadUpdated(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 121 | download::DownloadItem* item) override; |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 122 | void OnDownloadOpened(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 123 | download::DownloadItem* item) override; |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 124 | void OnDownloadRemoved(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 125 | download::DownloadItem* item) override; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 126 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 127 | // 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. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 130 | void ScheduleRemoveDownload(uint32_t download_id); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 131 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 132 | // Removes all |removing_ids_| from |history_|. |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 133 | void RemoveDownloadsBatch(); |
| 134 | |
Min Qin | 7289cfc | 2018-08-01 23:05:29 | [diff] [blame] | 135 | // 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 Qin | 67332ad | 2018-07-25 18:31:05 | [diff] [blame] | 140 | |
Shakti Sahu | 4af06b8d | 2019-03-27 22:17:02 | [diff] [blame] | 141 | download::AllDownloadItemNotifier notifier_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 142 | |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 143 | std::unique_ptr<HistoryAdapter> history_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 144 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 145 | // 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. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 148 | uint32_t loading_id_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 149 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 150 | // Identifiers of items that are scheduled for removal from history, to |
| 151 | // facilitate batching removals together for database efficiency. |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 152 | IdSet removing_ids_; |
| 153 | |
| 154 | // |GetId()|s of items that were removed while they were being added, so that |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 155 | // 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] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 158 | IdSet removed_while_adding_; |
| 159 | |
asanka | 7fad54c | 2016-03-24 20:33:40 | [diff] [blame] | 160 | bool initial_history_query_complete_; |
| 161 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 162 | base::ObserverList<Observer>::Unchecked observers_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 163 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 164 | base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_{this}; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |