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

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

Powered by Google App Engine
This is Rietveld 408576698