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