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/memory/scoped_ptr.h" |
11 #include "base/callback.h" | 13 #include "base/memory/weak_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 // Caller must guarantee that HistoryService outlives DownloadHistory. |
| 29 DownloadHistory( |
| 30 content::DownloadManager* manager, |
| 31 HistoryService* history); |
32 | 32 |
33 // Retrieves the next_id counter from the sql meta_table. | 33 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 | 34 |
38 // Retrieves DownloadCreateInfos saved in the history. | 35 // 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, | 36 void CheckVisitedReferrerBefore(int32 download_id, |
44 const GURL& referrer_url, | 37 const GURL& referrer_url, |
45 const VisitedBeforeDoneCallback& callback); | 38 const VisitedBeforeDoneCallback& callback); |
46 | 39 |
47 // Adds a new entry for a download to the history database. | 40 // content::DownloadManager::Observer |
48 void AddEntry(content::DownloadItem* download_item, | 41 virtual void OnDownloadCreated( |
49 const HistoryService::DownloadCreateCallback& callback); | 42 content::DownloadManager* manager, |
| 43 content::DownloadItem* item) OVERRIDE; |
| 44 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; |
50 | 45 |
51 // Updates the history entry for |download_item|. | 46 // content::DownloadItem::Observer |
52 void UpdateEntry(content::DownloadItem* download_item); | 47 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE; |
53 | 48 virtual void OnDownloadOpened(content::DownloadItem* item) OVERRIDE; |
54 // Updates the download path for |download_item| to |new_path|. | 49 virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE; |
55 void UpdateDownloadPath(content::DownloadItem* download_item, | 50 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 | 51 |
68 private: | 52 private: |
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 53 typedef std::set<int64> HandleSet; |
70 VisitedBeforeRequestsMap; | 54 typedef std::set<int32> IdSet; |
| 55 typedef std::set<content::DownloadItem*> ItemSet; |
71 | 56 |
72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 57 void MaybeAddToHistory(content::DownloadItem* item); |
73 bool found_visits, | 58 void ItemAdded(int32 id, int64 db_handle); |
74 int count, | 59 void RemoveDownloadsBatch(); |
75 base::Time first_visit); | 60 void OnGotVisitCountToHost( |
| 61 const VisitedBeforeDoneCallback& callback, |
| 62 bool found_visits, int count, base::Time first_visit); |
| 63 void QueryCallback( |
| 64 scoped_ptr<std::vector<DownloadPersistentStoreInfo> > infos); |
76 | 65 |
77 Profile* profile_; | 66 content::DownloadManager* manager_; |
| 67 ItemSet observing_items_; |
78 | 68 |
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 69 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 | 70 |
85 CancelableRequestConsumer history_consumer_; | 71 // |db_handle| of the item being created in response to QueryCallback(), |
| 72 // matched up with created items in OnDownloadCreated() so that the item is |
| 73 // not re-added to the database. For items not created by QueryCallback(), |
| 74 // this is DownloadDatabase::kUninitializedHandle. |
| 75 int64 loading_db_handle_; |
86 | 76 |
87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 77 // |db_handles| of items that are scheduled for removal from history, to |
88 VisitedBeforeRequestsMap visited_before_requests_; | 78 // facilitate batching removals together for database efficiency. |
| 79 HandleSet removing_; |
| 80 |
| 81 // |GetId()|s of items that were removed while they were being added, so that |
| 82 // they can be removed when their db_handles are received from the database. |
| 83 IdSet removed_while_adding_; |
| 84 |
| 85 // Count the number of items in the history for UMA. |
| 86 int64 history_size_; |
| 87 |
| 88 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_; |
89 | 89 |
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); | 90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); |
91 }; | 91 }; |
92 | 92 |
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ | 93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |
OLD | NEW |