Chromium Code Reviews| 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/weak_ptr.h" |
| 11 #include "base/callback.h" | |
| 12 #include "chrome/browser/cancelable_request.h" | 13 #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 namespace content { |
| 16 | 19 struct DownloadPersistenStoreInfo; |
| 17 namespace base { | |
| 18 class Time; | |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace content { | 22 // Wrap HistoryService for easy mocking and to hide CancelableRequestConsumer. |
| 22 class DownloadItem; | 23 class HistoryServiceDownloadInterface { |
| 23 } | 24 public: |
| 25 virtual void QueryDownloads(const HistoryService::DownloadQueryCallback&) = 0; | |
| 26 virtual HistoryService::Handle GetVisibleVisitCountToHost( | |
| 27 const GURL& referrer_url, | |
| 28 const HistoryService::GetVisibleVisitCountToHostCallback& callback) = 0; | |
| 29 virtual void CreateDownload( | |
| 30 int32 id, | |
| 31 const content::DownloadPersistentStoreInfo& info, | |
| 32 const HistoryService::DownloadCreateCallback& callback) = 0; | |
| 33 virtual void UpdateDownload(const content::DownloadPersistentStoreInfo&) = 0; | |
| 34 virtual void RemoveDownloads(const std::set<int64>& handles) = 0; | |
| 35 virtual void OnDownloadHistoryDestroyed() = 0; | |
| 36 }; | |
| 24 | 37 |
| 25 // Interacts with the HistoryService on behalf of the download subsystem. | 38 // Observes a single DownloadManager and all its DownloadItems, keeping the |
| 26 class DownloadHistory { | 39 // DownloadDatabase up to date. |
| 40 class DownloadHistory: public content::DownloadManager::Observer, | |
| 41 public content::DownloadItem::Observer, | |
| 42 public base::SupportsWeakPtr<DownloadHistory> { | |
| 27 public: | 43 public: |
| 28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; | 44 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; |
| 45 typedef std::vector<content::DownloadPersistentStoreInfo> InfoVector; | |
| 46 typedef std::set<int64> HandleSet; | |
| 29 | 47 |
| 30 explicit DownloadHistory(Profile* profile); | 48 DownloadHistory( |
| 31 ~DownloadHistory(); | 49 content::DownloadManager* manager, |
| 50 HistoryServiceDownloadInterface* history); | |
| 32 | 51 |
| 33 // Retrieves the next_id counter from the sql meta_table. | 52 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 | 53 |
| 38 // Retrieves DownloadCreateInfos saved in the history. | 54 // Retrieves DownloadCreateInfos saved in the history. |
| 39 void Load(const HistoryService::DownloadQueryCallback& callback); | 55 void Load(const HistoryService::DownloadQueryCallback& callback); |
| 40 | 56 |
| 41 // Checks whether |referrer_url| has been visited before today. This takes | 57 // Checks whether |referrer_url| has been visited before today. This takes |
| 42 // ownership of |callback|. | 58 // ownership of |callback|. |
| 43 void CheckVisitedReferrerBefore(int32 download_id, | 59 void CheckVisitedReferrerBefore(int32 download_id, |
| 44 const GURL& referrer_url, | 60 const GURL& referrer_url, |
| 45 const VisitedBeforeDoneCallback& callback); | 61 const VisitedBeforeDoneCallback& callback); |
| 46 | 62 |
| 47 // Adds a new entry for a download to the history database. | 63 // content::DownloadManager::Observer |
| 48 void AddEntry(content::DownloadItem* download_item, | 64 virtual void OnDownloadCreated( |
| 49 const HistoryService::DownloadCreateCallback& callback); | 65 content::DownloadManager* manager, |
| 66 content::DownloadItem* item) OVERRIDE; | |
| 67 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | |
| 50 | 68 |
| 51 // Updates the history entry for |download_item|. | 69 // content::DownloadItem::Observer |
| 52 void UpdateEntry(content::DownloadItem* download_item); | 70 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE; |
| 53 | 71 virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE; |
| 54 // Updates the download path for |download_item| to |new_path|. | 72 virtual void OnDownloadDestroyed(content::DownloadItem* item) OVERRIDE; |
| 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 | 73 |
| 68 private: | 74 private: |
| 75 typedef std::set<int32> IdSet; | |
| 76 typedef std::map<int32, content::DownloadPersistentStoreInfo> InfoMap; | |
|
Randy Smith (Not in Mondays)
2012/08/02 22:47:56
Maybe put in a UMA to track how often we get an On
Randy Smith (Not in Mondays)
2012/08/02 22:47:56
Evict persistent infos from the map on completion
benjhayden
2012/08/14 21:31:47
Done.
benjhayden
2012/08/14 21:31:47
Done.
| |
| 69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 77 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> |
| 70 VisitedBeforeRequestsMap; | 78 VisitedBeforeRequestsMap; |
| 71 | 79 |
| 80 void MaybeAddToHistory(content::DownloadItem* item); | |
| 81 void ItemAdded(int32 id, int64 db_handle); | |
| 82 void RemoveDownloadsBatch(); | |
| 72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 83 void OnGotVisitCountToHost(HistoryService::Handle handle, |
| 73 bool found_visits, | 84 bool found_visits, |
| 74 int count, | 85 int count, |
| 75 base::Time first_visit); | 86 base::Time first_visit); |
| 87 void LoadCallback(const HistoryService::DownloadQueryCallback& callback, | |
| 88 InfoVector* infos); | |
| 76 | 89 |
| 77 Profile* profile_; | 90 content::DownloadManager* manager_; |
| 78 | 91 |
| 79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 92 HistoryServiceDownloadInterface* 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 | |
| 85 CancelableRequestConsumer history_consumer_; | |
| 86 | 93 |
| 87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 94 // The outstanding requests made by CheckVisitedReferrerBefore(). |
| 88 VisitedBeforeRequestsMap visited_before_requests_; | 95 VisitedBeforeRequestsMap visited_before_requests_; |
| 89 | 96 |
| 97 // Ids of the downloads that are currently being added to the database. | |
| 98 IdSet adding_; | |
| 99 | |
| 100 // |db_handles| of items that are scheduled for removal from history. | |
| 101 HandleSet removing_; | |
| 102 | |
| 103 // This allows OnDownloadUpdated() to see what changed in a DownloadItem if | |
| 104 // anything, in order to prevent writing to the database unnecessarily. | |
| 105 // All downloads for |manager_| are in |infos_| and are observed by |this|. | |
| 106 InfoMap infos_; | |
| 107 | |
| 108 // Items loaded from the history, to be matched up with OnDownloadCreated. | |
| 109 InfoVector loaded_infos_; | |
| 110 | |
| 111 // Count the number of items in the history for UMA. | |
| 112 int64 history_size_; | |
| 113 | |
| 90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); | 114 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); |
| 91 }; | 115 }; |
| 92 | 116 |
| 93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ | 117 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |
| OLD | NEW |