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

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: . Created 8 years, 3 months 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 <map>
9 #include <set>
10 #include <vector>
9 11
10 #include "base/basictypes.h" 12 #include "base/memory/weak_ptr.h"
11 #include "base/callback.h" 13 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/cancelable_request.h"
13 #include "chrome/browser/history/history.h" 14 #include "chrome/browser/history/history.h"
15 #include "content/public/browser/download_item.h"
16 #include "content/public/browser/download_manager.h"
14 17
15 class Profile; 18 struct DownloadPersistentStoreInfo;
16 19
17 namespace base { 20 // Observes a single DownloadManager and all its DownloadItems, keeping the
18 class Time; 21 // DownloadDatabase up to date.
19 } 22 class DownloadHistory: public content::DownloadManager::Observer,
20 23 public content::DownloadItem::Observer {
21 namespace content {
22 class DownloadItem;
23 }
24
25 // Interacts with the HistoryService on behalf of the download subsystem.
26 class DownloadHistory {
27 public: 24 public:
28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; 25 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback;
29 26
30 explicit DownloadHistory(Profile* profile); 27 // Neither |manager| nor |history| may be NULL.
31 ~DownloadHistory(); 28 DownloadHistory(
29 content::DownloadManager* manager,
30 HistoryService* history);
Randy Smith (Not in Mondays) 2012/09/11 18:36:53 Comment that constructor must guarantee that Histo
benjhayden 2012/09/13 15:18:16 Done.
32 31
33 // Retrieves the next_id counter from the sql meta_table. 32 virtual ~DownloadHistory();
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 33
38 // Retrieves DownloadCreateInfos saved in the history. 34 // Checks whether |referrer_url| has been visited before today.
39 void Load(const HistoryService::DownloadQueryCallback& callback);
40
41 // Checks whether |referrer_url| has been visited before today. This takes
42 // ownership of |callback|.
43 void CheckVisitedReferrerBefore(int32 download_id, 35 void CheckVisitedReferrerBefore(int32 download_id,
44 const GURL& referrer_url, 36 const GURL& referrer_url,
45 const VisitedBeforeDoneCallback& callback); 37 const VisitedBeforeDoneCallback& callback);
46 38
47 // Adds a new entry for a download to the history database. 39 // content::DownloadManager::Observer
48 void AddEntry(content::DownloadItem* download_item, 40 virtual void OnDownloadCreated(
49 const HistoryService::DownloadCreateCallback& callback); 41 content::DownloadManager* manager,
42 content::DownloadItem* item) OVERRIDE;
43 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE;
50 44
51 // Updates the history entry for |download_item|. 45 // content::DownloadItem::Observer
52 void UpdateEntry(content::DownloadItem* download_item); 46 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE;
53 47 virtual void OnDownloadOpened(content::DownloadItem* item) OVERRIDE;
54 // Updates the download path for |download_item| to |new_path|. 48 virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE;
55 void UpdateDownloadPath(content::DownloadItem* download_item, 49 virtual void OnDownloadDestroyed(content::DownloadItem* item) OVERRIDE;
56 const FilePath& new_path);
57
58 // Removes |download_item| from the history database.
59 void RemoveEntry(content::DownloadItem* download_item);
60
61 // Removes download-related history entries in the given time range.
62 void RemoveEntriesBetween(const base::Time remove_begin,
63 const base::Time remove_end);
64
65 // Returns a new unique database handle which will not collide with real ones.
66 int64 GetNextFakeDbHandle();
67 50
68 private: 51 private:
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> 52 typedef std::set<int64> HandleSet;
70 VisitedBeforeRequestsMap; 53 typedef std::set<int32> IdSet;
54 typedef std::set<content::DownloadItem*> ItemSet;
71 55
72 void OnGotVisitCountToHost(HistoryService::Handle handle, 56 void MaybeAddToHistory(content::DownloadItem* item);
73 bool found_visits, 57 void ItemAdded(int32 id, int64 db_handle);
74 int count, 58 void RemoveDownloadsBatch();
75 base::Time first_visit); 59 void OnGotVisitCountToHost(
60 const VisitedBeforeDoneCallback& callback,
61 bool found_visits, int count, base::Time first_visit);
62 void QueryCallback(
63 scoped_ptr<std::vector<DownloadPersistentStoreInfo> > infos);
76 64
77 Profile* profile_; 65 content::DownloadManager* manager_;
66 ItemSet observing_items_;
78 67
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. 68 HistoryService* history_;
80 // This is useful for incognito mode or when the history database is offline.
81 // Downloads are expected to have unique handles, so we decrement the next
82 // fake handle value on every use.
83 int64 next_fake_db_handle_;
84 69
85 CancelableRequestConsumer history_consumer_; 70 // |db_handle| of the item being created in response to QueryCallback(),
71 // matched up with created items in OnDownloadCreated() so that the item is
72 // not re-added to the database. For items not created by QueryCallback(),
73 // this is DownloadDatabase::kUninitializedHandle.
74 int64 loading_db_handle_;
86 75
87 // The outstanding requests made by CheckVisitedReferrerBefore(). 76 // |db_handles| of items that are scheduled for removal from history, to
88 VisitedBeforeRequestsMap visited_before_requests_; 77 // facilitate batching removals together for database efficiency.
78 HandleSet removing_;
79
80 // |GetId()|s of items that were removed while they were being added, so that
81 // they can be removed when their db_handles are received from the database.
82 IdSet removed_while_adding_;
83
84 // Count the number of items in the history for UMA.
85 int64 history_size_;
86
87 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_;
89 88
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); 89 DISALLOW_COPY_AND_ASSIGN(DownloadHistory);
91 }; 90 };
92 91
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ 92 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698