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

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: @r166680 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 struct DownloadPersistentStoreInfo;
16 22
17 namespace base { 23 // Observes a single DownloadManager and all its DownloadItems, keeping the
18 class Time; 24 // DownloadDatabase up to date.
19 } 25 class DownloadHistory : public AllDownloadItemNotifier::Observer {
26 public:
27 typedef std::set<int32> IdSet;
20 28
21 namespace content { 29 // Caller must guarantee that HistoryService outlives HistoryAdapter.
22 class DownloadItem; 30 class HistoryAdapter {
23 } 31 public:
32 explicit HistoryAdapter(HistoryService* history);
33 virtual ~HistoryAdapter();
24 34
25 // Interacts with the HistoryService on behalf of the download subsystem. 35 virtual void QueryDownloads(
26 class DownloadHistory { 36 const HistoryService::DownloadQueryCallback& callback);
27 public:
28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback;
29 37
30 explicit DownloadHistory(Profile* profile); 38 virtual void CreateDownload(
31 ~DownloadHistory(); 39 const DownloadPersistentStoreInfo& info,
40 const HistoryService::DownloadCreateCallback& callback);
32 41
33 // Retrieves the next_id counter from the sql meta_table. 42 virtual void UpdateDownload(const DownloadPersistentStoreInfo& 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 43
38 // Retrieves DownloadCreateInfos saved in the history. 44 virtual void RemoveDownloads(const std::set<int64>& db_handles);
39 void Load(const HistoryService::DownloadQueryCallback& callback);
40 45
41 // Checks whether |referrer_url| has been visited before today. This takes 46 private:
42 // ownership of |callback|. 47 HistoryService* history_;
43 void CheckVisitedReferrerBefore(int32 download_id, 48 CancelableRequestConsumer consumer_;
44 const GURL& referrer_url, 49 DISALLOW_COPY_AND_ASSIGN(HistoryAdapter);
45 const VisitedBeforeDoneCallback& callback); 50 };
46 51
47 // Adds a new entry for a download to the history database. 52 class Observer {
48 void AddEntry(content::DownloadItem* download_item, 53 public:
49 const HistoryService::DownloadCreateCallback& callback); 54 Observer();
55 virtual ~Observer();
50 56
51 // Updates the history entry for |download_item|. 57 // Fires when a download is added to or updated in the database. When
52 void UpdateEntry(content::DownloadItem* download_item); 58 // downloads are first added, this fires after the callback from the
59 // database so that |info| includes the |db_handle|. When downloads are
60 // updated, this fires right after the message is sent to the database.
61 // |info| always includes the |db_handle|.
62 virtual void OnDownloadStored(content::DownloadItem* item,
63 const DownloadPersistentStoreInfo& info) {
64 }
53 65
54 // Updates the download path for |download_item| to |new_path|. 66 // Fires when RemoveDownloads messages are sent to the DB thread.
55 void UpdateDownloadPath(content::DownloadItem* download_item, 67 virtual void OnDownloadsRemoved(const IdSet& ids) {}
56 const FilePath& new_path);
57 68
58 // Removes |download_item| from the history database. 69 // Fires when the DownloadHistory is being destroyed so that implementors
59 void RemoveEntry(content::DownloadItem* download_item); 70 // can RemoveObserver() and nullify their DownloadHistory*s.
71 virtual void OnDownloadHistoryDestroyed() {}
72 };
60 73
61 // Removes download-related history entries in the given time range. 74 // Returns true if the item is persisted.
62 void RemoveEntriesBetween(const base::Time remove_begin, 75 static bool IsPersisted(content::DownloadItem* item);
Randy Smith (Not in Mondays) 2012/11/09 21:36:39 I'll call out that I'm uncomfortable that this met
benjhayden 2012/11/12 18:44:16 There's also no way to mock DownloadHistory out of
63 const base::Time remove_end);
64 76
65 // Returns a new unique database handle which will not collide with real ones. 77 // Neither |manager| nor |history| may be NULL.
66 int64 GetNextFakeDbHandle(); 78 // DownloadService creates DownloadHistory some time after DownloadManager is
79 // created and destroys DownloadHistory as DownloadManager is shutting down.
80 DownloadHistory(
81 content::DownloadManager* manager,
82 scoped_ptr<HistoryAdapter> history);
83
84 virtual ~DownloadHistory();
85
86 void AddObserver(Observer* observer);
87 void RemoveObserver(Observer* observer);
67 88
68 private: 89 private:
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> 90 typedef std::set<int64> HandleSet;
70 VisitedBeforeRequestsMap; 91 typedef std::set<content::DownloadItem*> ItemSet;
71 92
72 void OnGotVisitCountToHost(HistoryService::Handle handle, 93 // Callback from |history_| containing all entries in the downloads database
73 bool found_visits, 94 // table.
74 int count, 95 void QueryCallback(
75 base::Time first_visit); 96 std::vector<DownloadPersistentStoreInfo>* infos);
76 97
77 Profile* profile_; 98 // May add |item| to |history_|.
99 void MaybeAddToHistory(content::DownloadItem* item);
78 100
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. 101 // 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. 102 // database.
81 // Downloads are expected to have unique handles, so we decrement the next 103 void ItemAdded(int32 id, int64 db_handle);
82 // fake handle value on every use.
83 int64 next_fake_db_handle_;
84 104
85 CancelableRequestConsumer history_consumer_; 105 // AllDownloadItemNotifier::Observer
106 virtual void OnDownloadCreated(
107 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
108 virtual void OnDownloadUpdated(
109 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
110 virtual void OnDownloadOpened(
111 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
112 virtual void OnDownloadRemoved(
113 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE;
86 114
87 // The outstanding requests made by CheckVisitedReferrerBefore(). 115 // Schedule a record to be removed from |history_| the next time
88 VisitedBeforeRequestsMap visited_before_requests_; 116 // RemoveDownloadsBatch() runs. Schedule RemoveDownloadsBatch() to be run soon
117 // if it isn't already scheduled.
118 void ScheduleRemoveDownload(int32 download_id, int64 db_handle);
119
120 // Removes all |removing_handles_| from |history_|.
121 void RemoveDownloadsBatch();
122
123
124 AllDownloadItemNotifier notifier_;
125
126 scoped_ptr<HistoryAdapter> history_;
127
128 // |db_handle| of the item being created in response to QueryCallback(),
129 // matched up with created items in OnDownloadCreated() so that the item is
130 // not re-added to the database. For items not created by QueryCallback(),
131 // this is DownloadDatabase::kUninitializedHandle.
132 int64 loading_db_handle_;
133
134 // |db_handles| and |ids| of items that are scheduled for removal from
135 // history, to facilitate batching removals together for database efficiency.
136 HandleSet removing_handles_;
137 IdSet removing_ids_;
138
139 // |GetId()|s of items that were removed while they were being added, so that
140 // they can be removed when their db_handles are received from the database.
141 IdSet removed_while_adding_;
142
143 // Count the number of items in the history for UMA.
144 int64 history_size_;
145
146 ObserverList<Observer> observers_;
147
148 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_;
89 149
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); 150 DISALLOW_COPY_AND_ASSIGN(DownloadHistory);
91 }; 151 };
92 152
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ 153 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698