Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: chrome/browser/download/download_history.h

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r168573 Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ 5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
7 7
8 #include <map> 8 #include <set>
9 #include <vector>
9 10
10 #include "base/basictypes.h" 11 #include "base/basictypes.h"
11 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
12 #include "chrome/browser/common/cancelable_request.h" 15 #include "chrome/browser/common/cancelable_request.h"
16 #include "chrome/browser/download/all_download_item_notifier.h"
13 #include "chrome/browser/history/history.h" 17 #include "chrome/browser/history/history.h"
18 #include "content/public/browser/download_item.h"
19 #include "content/public/browser/download_manager.h"
14 20
15 class Profile; 21 namespace history {
22 struct DownloadRow;
23 } // namespace history
16 24
17 namespace base { 25 // Observes a single DownloadManager and all its DownloadItems, keeping the
18 class Time; 26 // DownloadDatabase up to date.
19 } 27 class DownloadHistory : public AllDownloadItemNotifier::Observer {
28 public:
29 typedef std::set<int32> IdSet;
20 30
21 namespace content { 31 // Caller must guarantee that HistoryService outlives HistoryAdapter.
22 class DownloadItem; 32 class HistoryAdapter {
23 } 33 public:
34 explicit HistoryAdapter(HistoryService* history);
35 virtual ~HistoryAdapter();
24 36
25 // Interacts with the HistoryService on behalf of the download subsystem. 37 virtual void QueryDownloads(
26 class DownloadHistory { 38 const HistoryService::DownloadQueryCallback& callback);
27 public:
28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback;
29 39
30 explicit DownloadHistory(Profile* profile); 40 virtual void CreateDownload(
31 ~DownloadHistory(); 41 const history::DownloadRow& info,
42 const HistoryService::DownloadCreateCallback& callback);
32 43
33 // Retrieves the next_id counter from the sql meta_table. 44 virtual void UpdateDownload(const history::DownloadRow& data);
34 // Should be much faster than Load so that we may delay downloads until after
35 // this call with minimal performance penalty.
36 void GetNextId(const HistoryService::DownloadNextIdCallback& callback);
37 45
38 // Retrieves DownloadCreateInfos saved in the history. 46 virtual void RemoveDownloads(const std::set<int64>& db_handles);
39 void Load(const HistoryService::DownloadQueryCallback& callback);
40 47
41 // Checks whether |referrer_url| has been visited before today. This takes 48 private:
42 // ownership of |callback|. 49 HistoryService* history_;
43 void CheckVisitedReferrerBefore(int32 download_id, 50 CancelableRequestConsumer consumer_;
44 const GURL& referrer_url, 51 DISALLOW_COPY_AND_ASSIGN(HistoryAdapter);
45 const VisitedBeforeDoneCallback& callback); 52 };
46 53
47 // Adds a new entry for a download to the history database. 54 class Observer {
48 void AddEntry(content::DownloadItem* download_item, 55 public:
49 const HistoryService::DownloadCreateCallback& callback); 56 Observer();
57 virtual ~Observer();
50 58
51 // Updates the history entry for |download_item|. 59 // Fires when a download is added to or updated in the database. When
52 void UpdateEntry(content::DownloadItem* download_item); 60 // downloads are first added, this fires after the callback from the
61 // database so that |info| includes the |db_handle|. When downloads are
62 // updated, this fires right after the message is sent to the database.
63 // |info| always includes the |db_handle|.
64 virtual void OnDownloadStored(content::DownloadItem* item,
65 const history::DownloadRow& info) {
66 }
53 67
54 // Updates the download path for |download_item| to |new_path|. 68 // Fires when RemoveDownloads messages are sent to the DB thread.
55 void UpdateDownloadPath(content::DownloadItem* download_item, 69 virtual void OnDownloadsRemoved(const IdSet& ids) {}
56 const FilePath& new_path);
57 70
58 // Removes |download_item| from the history database. 71 // Fires when the DownloadHistory is being destroyed so that implementors
59 void RemoveEntry(content::DownloadItem* download_item); 72 // can RemoveObserver() and nullify their DownloadHistory*s.
73 virtual void OnDownloadHistoryDestroyed() {}
74 };
60 75
61 // Removes download-related history entries in the given time range. 76 // Returns true if the item is persisted.
62 void RemoveEntriesBetween(const base::Time remove_begin, 77 static bool IsPersisted(content::DownloadItem* item);
63 const base::Time remove_end);
64 78
65 // Returns a new unique database handle which will not collide with real ones. 79 // Neither |manager| nor |history| may be NULL.
66 int64 GetNextFakeDbHandle(); 80 // DownloadService creates DownloadHistory some time after DownloadManager is
81 // created and destroys DownloadHistory as DownloadManager is shutting down.
82 DownloadHistory(
83 content::DownloadManager* manager,
84 scoped_ptr<HistoryAdapter> history);
85
86 virtual ~DownloadHistory();
87
88 void AddObserver(Observer* observer);
89 void RemoveObserver(Observer* observer);
67 90
68 private: 91 private:
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> 92 typedef std::set<int64> HandleSet;
70 VisitedBeforeRequestsMap; 93 typedef std::set<content::DownloadItem*> ItemSet;
71 94
72 void OnGotVisitCountToHost(HistoryService::Handle handle, 95 // Callback from |history_| containing all entries in the downloads database
73 bool found_visits, 96 // table.
74 int count, 97 void QueryCallback(
75 base::Time first_visit); 98 std::vector<history::DownloadRow>* infos);
76 99
77 Profile* profile_; 100 // May add |item| to |history_|.
101 void MaybeAddToHistory(content::DownloadItem* item);
78 102
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. 103 // Callback from |history_| when an item was successfully inserted into the
80 // This is useful for incognito mode or when the history database is offline. 104 // database.
81 // Downloads are expected to have unique handles, so we decrement the next 105 void ItemAdded(int32 id, int64 db_handle);
82 // fake handle value on every use.
83 int64 next_fake_db_handle_;
84 106
85 CancelableRequestConsumer history_consumer_; 107 // AllDownloadItemNotifier::Observer
108 virtual void OnDownloadCreated(
109 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
110 virtual void OnDownloadUpdated(
111 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
112 virtual void OnDownloadOpened(
113 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
114 virtual void OnDownloadRemoved(
115 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
86 116
87 // The outstanding requests made by CheckVisitedReferrerBefore(). 117 // Schedule a record to be removed from |history_| the next time
88 VisitedBeforeRequestsMap visited_before_requests_; 118 // RemoveDownloadsBatch() runs. Schedule RemoveDownloadsBatch() to be run soon
119 // if it isn't already scheduled.
120 void ScheduleRemoveDownload(int32 download_id, int64 db_handle);
121
122 // Removes all |removing_handles_| from |history_|.
123 void RemoveDownloadsBatch();
124
125
126 AllDownloadItemNotifier notifier_;
127
128 scoped_ptr<HistoryAdapter> history_;
129
130 // |db_handle| of the item being created in response to QueryCallback(),
131 // matched up with created items in OnDownloadCreated() so that the item is
132 // not re-added to the database. For items not created by QueryCallback(),
133 // this is DownloadDatabase::kUninitializedHandle.
134 int64 loading_db_handle_;
135
136 // |db_handles| and |ids| of items that are scheduled for removal from
137 // history, to facilitate batching removals together for database efficiency.
138 HandleSet removing_handles_;
139 IdSet removing_ids_;
140
141 // |GetId()|s of items that were removed while they were being added, so that
142 // they can be removed when their db_handles are received from the database.
143 IdSet removed_while_adding_;
144
145 // Count the number of items in the history for UMA.
146 int64 history_size_;
147
148 ObserverList<Observer> observers_;
149
150 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_;
89 151
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); 152 DISALLOW_COPY_AND_ASSIGN(DownloadHistory);
91 }; 153 };
92 154
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ 155 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698