blob: be037b124262f9d5caba5783c065e18746c1a97a [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
[email protected]3d95e542012-11-20 00:52:088#include <set>
9#include <vector>
[email protected]88008002011-05-24 23:14:1510
[email protected]82f37b02010-07-29 22:04:5711#include "base/basictypes.h"
[email protected]a3094072011-10-10 17:14:0512#include "base/callback.h"
[email protected]3d95e542012-11-20 00:52:0813#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
[email protected]3d95e542012-11-20 00:52:0815#include "chrome/browser/download/all_download_item_notifier.h"
[email protected]6a2c09f2013-01-25 04:50:0716#include "chrome/browser/history/history_service.h"
[email protected]3d95e542012-11-20 00:52:0817#include "content/public/browser/download_item.h"
18#include "content/public/browser/download_manager.h"
[email protected]82f37b02010-07-29 22:04:5719
[email protected]3d95e542012-11-20 00:52:0820namespace history {
21struct DownloadRow;
22} // namespace history
[email protected]82f37b02010-07-29 22:04:5723
[email protected]3d95e542012-11-20 00:52:0824// Observes a single DownloadManager and all its DownloadItems, keeping the
25// DownloadDatabase up to date.
26class DownloadHistory : public AllDownloadItemNotifier::Observer {
[email protected]82f37b02010-07-29 22:04:5727 public:
[email protected]530047e2013-07-12 17:02:2528 typedef std::set<uint32> IdSet;
[email protected]88008002011-05-24 23:14:1529
[email protected]3d95e542012-11-20 00:52:0830 // Caller must guarantee that HistoryService outlives HistoryAdapter.
31 class HistoryAdapter {
32 public:
33 explicit HistoryAdapter(HistoryService* history);
34 virtual ~HistoryAdapter();
[email protected]82f37b02010-07-29 22:04:5735
[email protected]3d95e542012-11-20 00:52:0836 virtual void QueryDownloads(
37 const HistoryService::DownloadQueryCallback& callback);
[email protected]eda58402011-09-21 19:32:0238
[email protected]3d95e542012-11-20 00:52:0839 virtual void CreateDownload(
40 const history::DownloadRow& info,
41 const HistoryService::DownloadCreateCallback& callback);
[email protected]82f37b02010-07-29 22:04:5742
[email protected]3d95e542012-11-20 00:52:0843 virtual void UpdateDownload(const history::DownloadRow& data);
[email protected]88008002011-05-24 23:14:1544
[email protected]530047e2013-07-12 17:02:2545 virtual void RemoveDownloads(const std::set<uint32>& ids);
[email protected]82f37b02010-07-29 22:04:5746
[email protected]3d95e542012-11-20 00:52:0847 private:
48 HistoryService* history_;
[email protected]3d95e542012-11-20 00:52:0849 DISALLOW_COPY_AND_ASSIGN(HistoryAdapter);
50 };
[email protected]82f37b02010-07-29 22:04:5751
[email protected]3d95e542012-11-20 00:52:0852 class Observer {
53 public:
54 Observer();
55 virtual ~Observer();
[email protected]82f37b02010-07-29 22:04:5756
[email protected]530047e2013-07-12 17:02:2557 // Fires when a download is added to or updated in the database, just after
58 // the task is posted to the history thread.
[email protected]3d95e542012-11-20 00:52:0859 virtual void OnDownloadStored(content::DownloadItem* item,
[email protected]530047e2013-07-12 17:02:2560 const history::DownloadRow& info) {}
[email protected]82f37b02010-07-29 22:04:5761
[email protected]3d95e542012-11-20 00:52:0862 // Fires when RemoveDownloads messages are sent to the DB thread.
63 virtual void OnDownloadsRemoved(const IdSet& ids) {}
[email protected]82f37b02010-07-29 22:04:5764
[email protected]3d95e542012-11-20 00:52:0865 // Fires when the DownloadHistory is being destroyed so that implementors
66 // can RemoveObserver() and nullify their DownloadHistory*s.
67 virtual void OnDownloadHistoryDestroyed() {}
68 };
69
[email protected]9dd188f2014-05-15 18:35:2070 // Returns true if the download is persisted. Not reliable when called from
71 // within a DownloadManager::Observer::OnDownloadCreated handler since the
72 // persisted state may not yet have been updated for a download that was
73 // restored from history.
74 static bool IsPersisted(const content::DownloadItem* item);
[email protected]3d95e542012-11-20 00:52:0875
76 // Neither |manager| nor |history| may be NULL.
77 // DownloadService creates DownloadHistory some time after DownloadManager is
78 // created and destroys DownloadHistory as DownloadManager is shutting down.
79 DownloadHistory(
80 content::DownloadManager* manager,
81 scoped_ptr<HistoryAdapter> history);
82
dcheng0c8ba27e2014-10-21 12:00:5383 ~DownloadHistory() override;
[email protected]3d95e542012-11-20 00:52:0884
85 void AddObserver(Observer* observer);
86 void RemoveObserver(Observer* observer);
[email protected]82f37b02010-07-29 22:04:5787
[email protected]9dd188f2014-05-15 18:35:2088 // Returns true if the download was restored from history. Safe to call from
89 // within a DownloadManager::Observer::OnDownloadCreated handler and can be
90 // used to distinguish between downloads that were created due to new requests
91 // vs. downloads that were created due to being restored from history. Note
92 // that the return value is only reliable for downloads that were restored by
93 // this specific DownloadHistory instance.
94 bool WasRestoredFromHistory(const content::DownloadItem* item) const;
95
[email protected]82f37b02010-07-29 22:04:5796 private:
[email protected]3d95e542012-11-20 00:52:0897 typedef std::set<content::DownloadItem*> ItemSet;
[email protected]88008002011-05-24 23:14:1598
[email protected]3d95e542012-11-20 00:52:0899 // Callback from |history_| containing all entries in the downloads database
100 // table.
101 void QueryCallback(
[email protected]77987312012-12-07 22:56:03102 scoped_ptr<std::vector<history::DownloadRow> > infos);
[email protected]88008002011-05-24 23:14:15103
[email protected]3d95e542012-11-20 00:52:08104 // May add |item| to |history_|.
105 void MaybeAddToHistory(content::DownloadItem* item);
[email protected]82f37b02010-07-29 22:04:57106
[email protected]3d95e542012-11-20 00:52:08107 // Callback from |history_| when an item was successfully inserted into the
108 // database.
[email protected]530047e2013-07-12 17:02:25109 void ItemAdded(uint32 id, bool success);
[email protected]82f37b02010-07-29 22:04:57110
[email protected]3d95e542012-11-20 00:52:08111 // AllDownloadItemNotifier::Observer
dcheng0c8ba27e2014-10-21 12:00:53112 void OnDownloadCreated(content::DownloadManager* manager,
113 content::DownloadItem* item) override;
114 void OnDownloadUpdated(content::DownloadManager* manager,
115 content::DownloadItem* item) override;
116 void OnDownloadOpened(content::DownloadManager* manager,
117 content::DownloadItem* item) override;
118 void OnDownloadRemoved(content::DownloadManager* manager,
119 content::DownloadItem* item) override;
[email protected]82f37b02010-07-29 22:04:57120
[email protected]3d95e542012-11-20 00:52:08121 // Schedule a record to be removed from |history_| the next time
122 // RemoveDownloadsBatch() runs. Schedule RemoveDownloadsBatch() to be run soon
123 // if it isn't already scheduled.
[email protected]530047e2013-07-12 17:02:25124 void ScheduleRemoveDownload(uint32 download_id);
[email protected]3d95e542012-11-20 00:52:08125
[email protected]530047e2013-07-12 17:02:25126 // Removes all |removing_ids_| from |history_|.
[email protected]3d95e542012-11-20 00:52:08127 void RemoveDownloadsBatch();
128
[email protected]3d95e542012-11-20 00:52:08129 AllDownloadItemNotifier notifier_;
130
131 scoped_ptr<HistoryAdapter> history_;
132
[email protected]530047e2013-07-12 17:02:25133 // Identifier of the item being created in QueryCallback(), matched up with
134 // created items in OnDownloadCreated() so that the item is not re-added to
135 // the database.
136 uint32 loading_id_;
[email protected]3d95e542012-11-20 00:52:08137
[email protected]530047e2013-07-12 17:02:25138 // Identifiers of items that are scheduled for removal from history, to
139 // facilitate batching removals together for database efficiency.
[email protected]3d95e542012-11-20 00:52:08140 IdSet removing_ids_;
141
142 // |GetId()|s of items that were removed while they were being added, so that
[email protected]530047e2013-07-12 17:02:25143 // they can be removed when the database finishes adding them.
144 // TODO(benjhayden) Can this be removed now that it doesn't need to wait for
145 // the db_handle, and can rely on PostTask sequentiality?
[email protected]3d95e542012-11-20 00:52:08146 IdSet removed_while_adding_;
147
148 // Count the number of items in the history for UMA.
149 int64 history_size_;
150
151 ObserverList<Observer> observers_;
152
153 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_;
[email protected]88008002011-05-24 23:14:15154
[email protected]82f37b02010-07-29 22:04:57155 DISALLOW_COPY_AND_ASSIGN(DownloadHistory);
156};
157
158#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_