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 struct DownloadPersistentStoreInfo; |
| 16 | 19 |
| 17 namespace base { | 20 // Wrap HistoryService for easy mocking and to hide CancelableRequestConsumer. |
| 18 class Time; | 21 // Theoretically, CancelableRequestConsumer guarantees that callbacks will not |
| 19 } | 22 // be called after this object is destroyed. As with all guarantees, you should |
| 23 // go dig for the code before relying on it. | |
|
Randy Smith (Not in Mondays)
2012/08/20 18:57:04
I understand why you feel this way, but I think th
benjhayden
2012/08/24 15:32:15
Done.
| |
| 24 class HistoryServiceDownloadAdapter { | |
| 25 public: | |
| 26 explicit HistoryServiceDownloadAdapter(HistoryService* history_service); | |
| 27 virtual ~HistoryServiceDownloadAdapter(); | |
| 28 virtual void QueryDownloads(const HistoryService::DownloadQueryCallback&); | |
| 29 virtual HistoryService::Handle GetVisibleVisitCountToHost( | |
| 30 const GURL& referrer_url, | |
| 31 const HistoryService::GetVisibleVisitCountToHostCallback& callback); | |
| 32 virtual void CreateDownload( | |
| 33 int32 id, | |
| 34 const DownloadPersistentStoreInfo& info, | |
| 35 const HistoryService::DownloadCreateCallback& callback); | |
| 36 virtual void UpdateDownload(const DownloadPersistentStoreInfo&); | |
| 37 virtual void RemoveDownloads(const std::set<int64>& handles); | |
| 38 virtual void OnDownloadHistoryDestroyed(); | |
| 20 | 39 |
| 21 namespace content { | 40 private: |
| 22 class DownloadItem; | 41 CancelableRequestConsumer history_consumer_; |
| 23 } | 42 HistoryService* history_service_; |
| 24 | 43 |
| 25 // Interacts with the HistoryService on behalf of the download subsystem. | 44 DISALLOW_COPY_AND_ASSIGN(HistoryServiceDownloadAdapter); |
| 26 class DownloadHistory { | 45 }; |
| 46 | |
| 47 // Observes a single DownloadManager and all its DownloadItems, keeping the | |
| 48 // DownloadDatabase up to date. | |
| 49 class DownloadHistory: public content::DownloadManager::Observer, | |
| 50 public content::DownloadItem::Observer { | |
| 27 public: | 51 public: |
| 28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; | 52 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; |
| 29 | 53 |
| 30 explicit DownloadHistory(Profile* profile); | 54 // Disable persisting this DownloadItem. Static so you don't need to know the |
| 31 ~DownloadHistory(); | 55 // item's profile or manager or expose this from anywhere. |
|
Randy Smith (Not in Mondays)
2012/08/20 18:57:04
What is this used for?
(If the IsTemporary() re
benjhayden
2012/08/24 15:32:15
Done.
| |
| 56 static void Disable(content::DownloadItem* item); | |
| 32 | 57 |
| 33 // Retrieves the next_id counter from the sql meta_table. | 58 // Neither |manager| nor |history| may be NULL. |
| 34 // Should be much faster than Load so that we may delay downloads until after | 59 DownloadHistory( |
| 35 // this call with minimal performance penalty. | 60 content::DownloadManager* manager, |
| 36 void GetNextId(const HistoryService::DownloadNextIdCallback& callback); | 61 HistoryServiceDownloadAdapter* history); |
| 37 | 62 |
| 38 // Retrieves DownloadCreateInfos saved in the history. | 63 virtual ~DownloadHistory(); |
| 39 void Load(const HistoryService::DownloadQueryCallback& callback); | |
| 40 | 64 |
| 41 // Checks whether |referrer_url| has been visited before today. This takes | 65 // Checks whether |referrer_url| has been visited before today. |
| 42 // ownership of |callback|. | |
| 43 void CheckVisitedReferrerBefore(int32 download_id, | 66 void CheckVisitedReferrerBefore(int32 download_id, |
| 44 const GURL& referrer_url, | 67 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 // content::DownloadManager::Observer |
| 48 void AddEntry(content::DownloadItem* download_item, | 71 virtual void OnDownloadCreated( |
| 49 const HistoryService::DownloadCreateCallback& callback); | 72 content::DownloadManager* manager, |
| 73 content::DownloadItem* item) OVERRIDE; | |
| 74 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | |
| 50 | 75 |
| 51 // Updates the history entry for |download_item|. | 76 // content::DownloadItem::Observer |
| 52 void UpdateEntry(content::DownloadItem* download_item); | 77 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE; |
| 53 | 78 virtual void OnDownloadOpened(content::DownloadItem* item) OVERRIDE; |
| 54 // Updates the download path for |download_item| to |new_path|. | 79 virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE; |
| 55 void UpdateDownloadPath(content::DownloadItem* download_item, | 80 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 | 81 |
| 68 private: | 82 private: |
| 83 typedef std::set<int64> HandleSet; | |
| 84 typedef std::set<int32> IdSet; | |
| 85 typedef std::vector<DownloadPersistentStoreInfo> InfoVector; | |
| 86 typedef std::set<content::DownloadItem*> ItemSet; | |
| 69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 87 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> |
| 70 VisitedBeforeRequestsMap; | 88 VisitedBeforeRequestsMap; |
| 71 | 89 |
| 90 void MaybeAddToHistory(content::DownloadItem* item); | |
| 91 void ItemAdded(int32 id, int64 db_handle); | |
| 92 void RemoveDownloadsBatch(); | |
| 72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 93 void OnGotVisitCountToHost(HistoryService::Handle handle, |
| 73 bool found_visits, | 94 bool found_visits, |
| 74 int count, | 95 int count, |
| 75 base::Time first_visit); | 96 base::Time first_visit); |
| 97 void QueryCallback(InfoVector* infos); | |
| 76 | 98 |
| 77 Profile* profile_; | 99 content::DownloadManager* manager_; |
| 100 ItemSet observing_items_; | |
| 78 | 101 |
| 79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 102 HistoryServiceDownloadAdapter* 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 | 103 |
| 87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 104 // The outstanding requests made by CheckVisitedReferrerBefore(). |
| 88 VisitedBeforeRequestsMap visited_before_requests_; | 105 VisitedBeforeRequestsMap visited_before_requests_; |
| 89 | 106 |
| 107 // Whether we are in process of creating DownloadItems from the database. | |
| 108 // OnDownloadCreated is called before we have a chance to attach a | |
| 109 // DownloadHistoryData to the item, so this field is used to disable adding | |
| 110 // new items to the database. | |
| 111 bool loading_; | |
| 112 | |
| 113 // |db_handles| of items that are scheduled for removal from history, to | |
| 114 // facilitate batching removals together for database efficiency. | |
| 115 HandleSet removing_; | |
| 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 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_; | |
| 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 |