[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" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 14 | #include "base/macros.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); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 36 | virtual ~HistoryAdapter(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 37 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 38 | virtual void QueryDownloads( |
sdefresne | c62efa3 | 2015-03-12 22:46:31 | [diff] [blame] | 39 | const history::HistoryService::DownloadQueryCallback& callback); |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 40 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 41 | virtual void CreateDownload( |
| 42 | const history::DownloadRow& info, |
sdefresne | c62efa3 | 2015-03-12 22:46:31 | [diff] [blame] | 43 | const history::HistoryService::DownloadCreateCallback& callback); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 44 | |
qinmin | 64558cd | 2016-11-28 22:30:29 | [diff] [blame] | 45 | virtual void UpdateDownload(const history::DownloadRow& data, |
| 46 | bool should_commit_immediately); |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 47 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 48 | virtual void RemoveDownloads(const std::set<uint32_t>& ids); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 49 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 50 | private: |
sdefresne | c62efa3 | 2015-03-12 22:46:31 | [diff] [blame] | 51 | history::HistoryService* history_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(HistoryAdapter); |
| 53 | }; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 54 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 55 | class Observer { |
| 56 | public: |
| 57 | Observer(); |
| 58 | virtual ~Observer(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 59 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 60 | // Fires when a download is added to or updated in the database, just after |
| 61 | // the task is posted to the history thread. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 62 | virtual void OnDownloadStored(download::DownloadItem* item, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 63 | const history::DownloadRow& info) {} |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 64 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 65 | // Fires when RemoveDownloads messages are sent to the DB thread. |
| 66 | virtual void OnDownloadsRemoved(const IdSet& ids) {} |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 67 | |
asanka | 7fad54c | 2016-03-24 20:33:40 | [diff] [blame] | 68 | // Fires when the DownloadHistory completes the initial history query. |
| 69 | // Unlike the other observer methods, this one is invoked if the initial |
| 70 | // history query has already completed by the time the caller calls |
| 71 | // AddObserver(). |
| 72 | virtual void OnHistoryQueryComplete() {} |
| 73 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 74 | // Fires when the DownloadHistory is being destroyed so that implementors |
| 75 | // can RemoveObserver() and nullify their DownloadHistory*s. |
| 76 | virtual void OnDownloadHistoryDestroyed() {} |
| 77 | }; |
| 78 | |
[email protected] | 9dd188f | 2014-05-15 18:35:20 | [diff] [blame] | 79 | // Returns true if the download is persisted. Not reliable when called from |
| 80 | // within a DownloadManager::Observer::OnDownloadCreated handler since the |
| 81 | // persisted state may not yet have been updated for a download that was |
| 82 | // restored from history. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 83 | static bool IsPersisted(const download::DownloadItem* item); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 84 | |
| 85 | // Neither |manager| nor |history| may be NULL. |
| 86 | // DownloadService creates DownloadHistory some time after DownloadManager is |
| 87 | // created and destroys DownloadHistory as DownloadManager is shutting down. |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 88 | DownloadHistory(content::DownloadManager* manager, |
| 89 | std::unique_ptr<HistoryAdapter> history); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 90 | |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 91 | ~DownloadHistory() override; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 92 | |
| 93 | void AddObserver(Observer* observer); |
| 94 | void RemoveObserver(Observer* observer); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 95 | |
[email protected] | 9dd188f | 2014-05-15 18:35:20 | [diff] [blame] | 96 | // Returns true if the download was restored from history. Safe to call from |
| 97 | // within a DownloadManager::Observer::OnDownloadCreated handler and can be |
| 98 | // used to distinguish between downloads that were created due to new requests |
| 99 | // vs. downloads that were created due to being restored from history. Note |
| 100 | // that the return value is only reliable for downloads that were restored by |
| 101 | // this specific DownloadHistory instance. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 102 | bool WasRestoredFromHistory(const download::DownloadItem* item) const; |
[email protected] | 9dd188f | 2014-05-15 18:35:20 | [diff] [blame] | 103 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 104 | private: |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 105 | // Callback from |history_| containing all entries in the downloads database |
| 106 | // table. |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 107 | void QueryCallback(std::unique_ptr<std::vector<history::DownloadRow>> infos); |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 108 | |
Min Qin | 5ca2c5e | 2018-06-01 19:39:24 | [diff] [blame] | 109 | // Called to create all history downloads. |
| 110 | void LoadHistoryDownloads( |
| 111 | std::unique_ptr<std::vector<history::DownloadRow>> infos); |
| 112 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 113 | // May add |item| to |history_|. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 114 | void MaybeAddToHistory(download::DownloadItem* item); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 115 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 116 | // Callback from |history_| when an item was successfully inserted into the |
| 117 | // database. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 118 | void ItemAdded(uint32_t id, bool success); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 119 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 120 | // AllDownloadItemNotifier::Observer |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 121 | void OnDownloadCreated(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 122 | download::DownloadItem* item) override; |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 123 | void OnDownloadUpdated(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 124 | download::DownloadItem* item) override; |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 125 | void OnDownloadOpened(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 126 | download::DownloadItem* item) override; |
dcheng | 0c8ba27e | 2014-10-21 12:00:53 | [diff] [blame] | 127 | void OnDownloadRemoved(content::DownloadManager* manager, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 128 | download::DownloadItem* item) override; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 129 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 130 | // Schedule a record to be removed from |history_| the next time |
| 131 | // RemoveDownloadsBatch() runs. Schedule RemoveDownloadsBatch() to be run soon |
| 132 | // if it isn't already scheduled. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 133 | void ScheduleRemoveDownload(uint32_t download_id); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 134 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 135 | // Removes all |removing_ids_| from |history_|. |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 136 | void RemoveDownloadsBatch(); |
| 137 | |
Min Qin | 7289cfc | 2018-08-01 23:05:29 | [diff] [blame] | 138 | // Called when a download was restored from history. |
| 139 | void OnDownloadRestoredFromHistory(download::DownloadItem* item); |
| 140 | |
| 141 | // Check whether an download item needs be updated or added to history DB. |
| 142 | bool NeedToUpdateDownloadHistory(download::DownloadItem* item); |
Min Qin | 67332ad | 2018-07-25 18:31:05 | [diff] [blame] | 143 | |
Xing Liu | 5c41b0a | 2017-07-14 19:16:46 | [diff] [blame] | 144 | download::AllDownloadItemNotifier notifier_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 145 | |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 146 | std::unique_ptr<HistoryAdapter> history_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 147 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 148 | // Identifier of the item being created in QueryCallback(), matched up with |
| 149 | // created items in OnDownloadCreated() so that the item is not re-added to |
| 150 | // the database. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 151 | uint32_t loading_id_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 152 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 153 | // Identifiers of items that are scheduled for removal from history, to |
| 154 | // facilitate batching removals together for database efficiency. |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 155 | IdSet removing_ids_; |
| 156 | |
| 157 | // |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] | 158 | // they can be removed when the database finishes adding them. |
| 159 | // TODO(benjhayden) Can this be removed now that it doesn't need to wait for |
| 160 | // the db_handle, and can rely on PostTask sequentiality? |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 161 | IdSet removed_while_adding_; |
| 162 | |
| 163 | // Count the number of items in the history for UMA. |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 164 | int64_t history_size_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 165 | |
asanka | 7fad54c | 2016-03-24 20:33:40 | [diff] [blame] | 166 | bool initial_history_query_complete_; |
| 167 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame^] | 168 | base::ObserverList<Observer>::Unchecked observers_; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 169 | |
| 170 | base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_; |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 171 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 172 | DISALLOW_COPY_AND_ASSIGN(DownloadHistory); |
| 173 | }; |
| 174 | |
| 175 | #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |