blob: 9e110dda787569e23fe78806096ec42d495a9f8b [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"
avie4d7b6f2015-12-26 00:59:1814#include "base/macros.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);
[email protected]3d95e542012-11-20 00:52:0836 virtual ~HistoryAdapter();
[email protected]82f37b02010-07-29 22:04:5737
[email protected]3d95e542012-11-20 00:52:0838 virtual void QueryDownloads(
sdefresnec62efa32015-03-12 22:46:3139 const history::HistoryService::DownloadQueryCallback& callback);
[email protected]eda58402011-09-21 19:32:0240
[email protected]3d95e542012-11-20 00:52:0841 virtual void CreateDownload(
42 const history::DownloadRow& info,
sdefresnec62efa32015-03-12 22:46:3143 const history::HistoryService::DownloadCreateCallback& callback);
[email protected]82f37b02010-07-29 22:04:5744
qinmin64558cd2016-11-28 22:30:2945 virtual void UpdateDownload(const history::DownloadRow& data,
46 bool should_commit_immediately);
[email protected]88008002011-05-24 23:14:1547
avie4d7b6f2015-12-26 00:59:1848 virtual void RemoveDownloads(const std::set<uint32_t>& ids);
[email protected]82f37b02010-07-29 22:04:5749
[email protected]3d95e542012-11-20 00:52:0850 private:
sdefresnec62efa32015-03-12 22:46:3151 history::HistoryService* history_;
[email protected]3d95e542012-11-20 00:52:0852 DISALLOW_COPY_AND_ASSIGN(HistoryAdapter);
53 };
[email protected]82f37b02010-07-29 22:04:5754
[email protected]3d95e542012-11-20 00:52:0855 class Observer {
56 public:
57 Observer();
58 virtual ~Observer();
[email protected]82f37b02010-07-29 22:04:5759
[email protected]530047e2013-07-12 17:02:2560 // Fires when a download is added to or updated in the database, just after
61 // the task is posted to the history thread.
Min Qina9f487872018-02-09 20:43:2362 virtual void OnDownloadStored(download::DownloadItem* item,
[email protected]530047e2013-07-12 17:02:2563 const history::DownloadRow& info) {}
[email protected]82f37b02010-07-29 22:04:5764
[email protected]3d95e542012-11-20 00:52:0865 // Fires when RemoveDownloads messages are sent to the DB thread.
66 virtual void OnDownloadsRemoved(const IdSet& ids) {}
[email protected]82f37b02010-07-29 22:04:5767
asanka7fad54c2016-03-24 20:33:4068 // 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]3d95e542012-11-20 00:52:0874 // 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]9dd188f2014-05-15 18:35:2079 // 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 Qina9f487872018-02-09 20:43:2383 static bool IsPersisted(const download::DownloadItem* item);
[email protected]3d95e542012-11-20 00:52:0884
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.
dchengabbf44652016-04-07 22:23:3988 DownloadHistory(content::DownloadManager* manager,
89 std::unique_ptr<HistoryAdapter> history);
[email protected]3d95e542012-11-20 00:52:0890
dcheng0c8ba27e2014-10-21 12:00:5391 ~DownloadHistory() override;
[email protected]3d95e542012-11-20 00:52:0892
93 void AddObserver(Observer* observer);
94 void RemoveObserver(Observer* observer);
[email protected]82f37b02010-07-29 22:04:5795
[email protected]9dd188f2014-05-15 18:35:2096 // 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 Qina9f487872018-02-09 20:43:23102 bool WasRestoredFromHistory(const download::DownloadItem* item) const;
[email protected]9dd188f2014-05-15 18:35:20103
[email protected]82f37b02010-07-29 22:04:57104 private:
[email protected]3d95e542012-11-20 00:52:08105 // Callback from |history_| containing all entries in the downloads database
106 // table.
dchengabbf44652016-04-07 22:23:39107 void QueryCallback(std::unique_ptr<std::vector<history::DownloadRow>> infos);
[email protected]88008002011-05-24 23:14:15108
Min Qin5ca2c5e2018-06-01 19:39:24109 // Called to create all history downloads.
110 void LoadHistoryDownloads(
111 std::unique_ptr<std::vector<history::DownloadRow>> infos);
112
[email protected]3d95e542012-11-20 00:52:08113 // May add |item| to |history_|.
Min Qina9f487872018-02-09 20:43:23114 void MaybeAddToHistory(download::DownloadItem* item);
[email protected]82f37b02010-07-29 22:04:57115
[email protected]3d95e542012-11-20 00:52:08116 // Callback from |history_| when an item was successfully inserted into the
117 // database.
avie4d7b6f2015-12-26 00:59:18118 void ItemAdded(uint32_t id, bool success);
[email protected]82f37b02010-07-29 22:04:57119
[email protected]3d95e542012-11-20 00:52:08120 // AllDownloadItemNotifier::Observer
dcheng0c8ba27e2014-10-21 12:00:53121 void OnDownloadCreated(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23122 download::DownloadItem* item) override;
dcheng0c8ba27e2014-10-21 12:00:53123 void OnDownloadUpdated(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23124 download::DownloadItem* item) override;
dcheng0c8ba27e2014-10-21 12:00:53125 void OnDownloadOpened(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23126 download::DownloadItem* item) override;
dcheng0c8ba27e2014-10-21 12:00:53127 void OnDownloadRemoved(content::DownloadManager* manager,
Min Qina9f487872018-02-09 20:43:23128 download::DownloadItem* item) override;
[email protected]82f37b02010-07-29 22:04:57129
[email protected]3d95e542012-11-20 00:52:08130 // 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.
avie4d7b6f2015-12-26 00:59:18133 void ScheduleRemoveDownload(uint32_t download_id);
[email protected]3d95e542012-11-20 00:52:08134
[email protected]530047e2013-07-12 17:02:25135 // Removes all |removing_ids_| from |history_|.
[email protected]3d95e542012-11-20 00:52:08136 void RemoveDownloadsBatch();
137
Min Qin7289cfc2018-08-01 23:05:29138 // 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 Qin67332ad2018-07-25 18:31:05143
Xing Liu5c41b0a2017-07-14 19:16:46144 download::AllDownloadItemNotifier notifier_;
[email protected]3d95e542012-11-20 00:52:08145
dchengabbf44652016-04-07 22:23:39146 std::unique_ptr<HistoryAdapter> history_;
[email protected]3d95e542012-11-20 00:52:08147
[email protected]530047e2013-07-12 17:02:25148 // 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.
avie4d7b6f2015-12-26 00:59:18151 uint32_t loading_id_;
[email protected]3d95e542012-11-20 00:52:08152
[email protected]530047e2013-07-12 17:02:25153 // Identifiers of items that are scheduled for removal from history, to
154 // facilitate batching removals together for database efficiency.
[email protected]3d95e542012-11-20 00:52:08155 IdSet removing_ids_;
156
157 // |GetId()|s of items that were removed while they were being added, so that
[email protected]530047e2013-07-12 17:02:25158 // 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]3d95e542012-11-20 00:52:08161 IdSet removed_while_adding_;
162
163 // Count the number of items in the history for UMA.
avie4d7b6f2015-12-26 00:59:18164 int64_t history_size_;
[email protected]3d95e542012-11-20 00:52:08165
asanka7fad54c2016-03-24 20:33:40166 bool initial_history_query_complete_;
167
Trent Apteda250ec3ab2018-08-19 08:52:19168 base::ObserverList<Observer>::Unchecked observers_;
[email protected]3d95e542012-11-20 00:52:08169
170 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_;
[email protected]88008002011-05-24 23:14:15171
[email protected]82f37b02010-07-29 22:04:57172 DISALLOW_COPY_AND_ASSIGN(DownloadHistory);
173};
174
175#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_