OLD | NEW |
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 class Observer { |
22 class DownloadItem; | 30 public: |
23 } | 31 Observer(); |
| 32 virtual ~Observer(); |
24 | 33 |
25 // Interacts with the HistoryService on behalf of the download subsystem. | 34 // Fires when a download is added to or updated in the database. When |
26 class DownloadHistory { | 35 // downloads are first added, this fires after the callback from the |
27 public: | 36 // database so that |info| includes the |db_handle|. When downloads are |
28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; | 37 // updated, this fires right after the message is sent to the database. |
| 38 // |info| always includes the |db_handle|. |
| 39 virtual void OnDownloadStored(content::DownloadItem* item, |
| 40 const DownloadPersistentStoreInfo& info) { |
| 41 } |
29 | 42 |
30 explicit DownloadHistory(Profile* profile); | 43 // Fires when RemoveDownloads messages are sent to the DB thread. |
31 ~DownloadHistory(); | 44 virtual void OnDownloadsRemoved(const IdSet& ids) {} |
32 | 45 |
33 // Retrieves the next_id counter from the sql meta_table. | 46 // Fires when the DownloadHistory is being destroyed so that implementors |
34 // Should be much faster than Load so that we may delay downloads until after | 47 // can RemoveObserver() and nullify their DownloadHistory*s. |
35 // this call with minimal performance penalty. | 48 virtual void OnDownloadHistoryDestroyed() {} |
36 void GetNextId(const HistoryService::DownloadNextIdCallback& callback); | 49 }; |
37 | 50 |
38 // Retrieves DownloadCreateInfos saved in the history. | 51 // Returns true if the item is persisted. |
39 void Load(const HistoryService::DownloadQueryCallback& callback); | 52 static bool IsPersisted(content::DownloadItem* item); |
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 // DownloadService creates DownloadHistory some time after DownloadManager is |
44 const GURL& referrer_url, | 57 // created and destroys DownloadHistory as DownloadManager is shutting down. |
45 const VisitedBeforeDoneCallback& callback); | 58 DownloadHistory( |
| 59 content::DownloadManager* manager, |
| 60 HistoryService* history); |
46 | 61 |
47 // Adds a new entry for a download to the history database. | 62 virtual ~DownloadHistory(); |
48 void AddEntry(content::DownloadItem* download_item, | |
49 const HistoryService::DownloadCreateCallback& callback); | |
50 | 63 |
51 // Updates the history entry for |download_item|. | 64 void AddObserver(Observer* observer); |
52 void UpdateEntry(content::DownloadItem* download_item); | 65 void RemoveObserver(Observer* observer); |
53 | |
54 // Updates the download path for |download_item| to |new_path|. | |
55 void UpdateDownloadPath(content::DownloadItem* download_item, | |
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 | 66 |
68 private: | 67 private: |
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 68 typedef std::set<int64> HandleSet; |
70 VisitedBeforeRequestsMap; | 69 typedef std::set<content::DownloadItem*> ItemSet; |
71 | 70 |
72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 71 // Callback from |history_| containing all entries in the downloads database |
73 bool found_visits, | 72 // table. |
74 int count, | 73 void QueryCallback( |
75 base::Time first_visit); | 74 std::vector<DownloadPersistentStoreInfo>* infos); |
76 | 75 |
77 Profile* profile_; | 76 // May add |item| to |history_|. |
| 77 void MaybeAddToHistory(content::DownloadItem* item); |
78 | 78 |
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 79 // 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. | 80 // database. |
81 // Downloads are expected to have unique handles, so we decrement the next | 81 void ItemAdded(int32 id, int64 db_handle); |
82 // fake handle value on every use. | 82 |
83 int64 next_fake_db_handle_; | 83 // AllDownloadItemNotifier::Observer |
| 84 virtual void OnDownloadCreated( |
| 85 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; |
| 86 virtual void OnDownloadUpdated( |
| 87 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; |
| 88 virtual void OnDownloadOpened( |
| 89 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; |
| 90 virtual void OnDownloadRemoved( |
| 91 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; |
| 92 |
| 93 // Schedule a record to be removed from |history_| the next time |
| 94 // RemoveDownloadsBatch() runs. Schedule RemoveDownloadsBatch() to be run soon |
| 95 // if it isn't already scheduled. |
| 96 void ScheduleRemoveDownload(int32 download_id, int64 db_handle); |
| 97 |
| 98 // Removes all |removing_handles_| from |history_|. |
| 99 void RemoveDownloadsBatch(); |
| 100 |
| 101 |
| 102 AllDownloadItemNotifier notifier_; |
| 103 |
| 104 HistoryService* history_; |
| 105 |
| 106 // |db_handle| of the item being created in response to QueryCallback(), |
| 107 // matched up with created items in OnDownloadCreated() so that the item is |
| 108 // not re-added to the database. For items not created by QueryCallback(), |
| 109 // this is DownloadDatabase::kUninitializedHandle. |
| 110 int64 loading_db_handle_; |
| 111 |
| 112 // |db_handles| and |ids| of items that are scheduled for removal from |
| 113 // history, to facilitate batching removals together for database efficiency. |
| 114 HandleSet removing_handles_; |
| 115 IdSet removing_ids_; |
| 116 |
| 117 // |GetId()|s of items that were removed while they were being added, so that |
| 118 // they can be removed when their db_handles are received from the database. |
| 119 IdSet removed_while_adding_; |
| 120 |
| 121 // Count the number of items in the history for UMA. |
| 122 int64 history_size_; |
| 123 |
| 124 ObserverList<Observer> observers_; |
84 | 125 |
85 CancelableRequestConsumer history_consumer_; | 126 CancelableRequestConsumer history_consumer_; |
86 | 127 |
87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 128 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_; |
88 VisitedBeforeRequestsMap visited_before_requests_; | |
89 | 129 |
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); | 130 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); |
91 }; | 131 }; |
92 | 132 |
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ | 133 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |
OLD | NEW |