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. |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
Document the lifetime guarantees that this class p
benjhayden
2012/08/17 18:20:45
Done.
| |
18 class Time; | 21 class HistoryServiceDownloadAdapter { |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
Why in .h file rather than a forward decl?
benjhayden
2012/08/17 18:20:45
The test needs to be able to override the methods.
| |
19 } | 22 public: |
23 explicit HistoryServiceDownloadAdapter(HistoryService* history_service); | |
24 virtual ~HistoryServiceDownloadAdapter(); | |
25 virtual void QueryDownloads(const HistoryService::DownloadQueryCallback&); | |
26 virtual HistoryService::Handle GetVisibleVisitCountToHost( | |
27 const GURL& referrer_url, | |
28 const HistoryService::GetVisibleVisitCountToHostCallback& callback); | |
29 virtual void CreateDownload( | |
30 int32 id, | |
31 const DownloadPersistentStoreInfo& info, | |
32 const HistoryService::DownloadCreateCallback& callback); | |
33 virtual void UpdateDownload(const DownloadPersistentStoreInfo&); | |
34 virtual void RemoveDownloads(const std::set<int64>& handles); | |
35 virtual void OnDownloadHistoryDestroyed(); | |
20 | 36 |
21 namespace content { | 37 private: |
22 class DownloadItem; | 38 CancelableRequestConsumer history_consumer_; |
23 } | 39 HistoryService* history_service_; |
24 | 40 |
25 // Interacts with the HistoryService on behalf of the download subsystem. | 41 DISALLOW_COPY_AND_ASSIGN(HistoryServiceDownloadAdapter); |
26 class DownloadHistory { | 42 }; |
43 | |
44 // Observes a single DownloadManager and all its DownloadItems, keeping the | |
45 // DownloadDatabase up to date. | |
46 class DownloadHistory: public content::DownloadManager::Observer, | |
47 public content::DownloadItem::Observer, | |
48 public base::SupportsWeakPtr<DownloadHistory> { | |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
We've talked about my concerns about SupportsWeakP
benjhayden
2012/08/17 18:20:45
Done.
| |
27 public: | 49 public: |
28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; | 50 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; |
51 typedef std::vector<DownloadPersistentStoreInfo> InfoVector; | |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
InfoVector isn't used in the public portion of the
benjhayden
2012/08/17 18:20:45
Done.
| |
52 typedef std::set<int64> HandleSet; | |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
Ditto HandleSet.
benjhayden
2012/08/17 18:20:45
Done.
| |
29 | 53 |
30 explicit DownloadHistory(Profile* profile); | 54 DownloadHistory( |
31 ~DownloadHistory(); | 55 content::DownloadManager* manager, |
56 HistoryServiceDownloadAdapter* history); | |
32 | 57 |
33 // Retrieves the next_id counter from the sql meta_table. | 58 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 | |
38 // Retrieves DownloadCreateInfos saved in the history. | |
39 void Load(const HistoryService::DownloadQueryCallback& callback); | |
40 | 59 |
41 // Checks whether |referrer_url| has been visited before today. This takes | 60 // Checks whether |referrer_url| has been visited before today. This takes |
42 // ownership of |callback|. | 61 // ownership of |callback|. |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
Not this CL's problem, but: "Takes ownership of |c
benjhayden
2012/08/17 18:20:45
I think it meant that it copied the callback to an
Randy Smith (Not in Mondays)
2012/08/20 18:57:04
Thanks.
| |
43 void CheckVisitedReferrerBefore(int32 download_id, | 62 void CheckVisitedReferrerBefore(int32 download_id, |
44 const GURL& referrer_url, | 63 const GURL& referrer_url, |
45 const VisitedBeforeDoneCallback& callback); | 64 const VisitedBeforeDoneCallback& callback); |
46 | 65 |
47 // Adds a new entry for a download to the history database. | 66 // content::DownloadManager::Observer |
48 void AddEntry(content::DownloadItem* download_item, | 67 virtual void OnDownloadCreated( |
49 const HistoryService::DownloadCreateCallback& callback); | 68 content::DownloadManager* manager, |
69 content::DownloadItem* item) OVERRIDE; | |
70 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | |
50 | 71 |
51 // Updates the history entry for |download_item|. | 72 // content::DownloadItem::Observer |
52 void UpdateEntry(content::DownloadItem* download_item); | 73 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE; |
53 | 74 virtual void OnDownloadOpened(content::DownloadItem* item) OVERRIDE; |
54 // Updates the download path for |download_item| to |new_path|. | 75 virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE; |
55 void UpdateDownloadPath(content::DownloadItem* download_item, | 76 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 | 77 |
68 private: | 78 private: |
79 typedef std::set<int32> IdSet; | |
80 typedef std::map<int32, DownloadPersistentStoreInfo> InfoMap; | |
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 81 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> |
70 VisitedBeforeRequestsMap; | 82 VisitedBeforeRequestsMap; |
71 | 83 |
84 void MaybeAddToHistory(content::DownloadItem* item); | |
85 void ItemAdded(int32 id, int64 db_handle); | |
86 void RemoveDownloadsBatch(); | |
72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 87 void OnGotVisitCountToHost(HistoryService::Handle handle, |
73 bool found_visits, | 88 bool found_visits, |
74 int count, | 89 int count, |
75 base::Time first_visit); | 90 base::Time first_visit); |
91 void QueryCallback(InfoVector* infos); | |
76 | 92 |
77 Profile* profile_; | 93 content::DownloadManager* manager_; |
78 | 94 |
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 95 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 | 96 |
87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 97 // The outstanding requests made by CheckVisitedReferrerBefore(). |
88 VisitedBeforeRequestsMap visited_before_requests_; | 98 VisitedBeforeRequestsMap visited_before_requests_; |
89 | 99 |
100 // Whether we are in process of creating DownloadItems from the database. | |
101 // OnDownloadCreated is called before we have a chance to attach a | |
102 // DownloadHistoryData to the item, so this field is used to disable adding | |
103 // new items to the database. | |
104 bool loading_; | |
105 | |
106 // |db_handles| of items that are scheduled for removal from history. | |
107 // Cannot be merged into DownloadHistoryData. | |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
Document that the motivation for this is batching
benjhayden
2012/08/17 18:20:45
Done.
| |
108 HandleSet removing_; | |
109 | |
110 // |GetId()|s of items that were removed while they were being added. | |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
I wince because these comments aren't really under
benjhayden
2012/08/17 18:20:45
Done.
| |
111 std::set<int32> removed_while_adding_; | |
Randy Smith (Not in Mondays)
2012/08/15 20:47:44
Document that the motivation is that we can't remo
benjhayden
2012/08/17 18:20:45
Done.
| |
112 | |
113 // Count the number of items in the history for UMA. | |
114 int64 history_size_; | |
115 | |
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); | 116 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); |
91 }; | 117 }; |
92 | 118 |
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ | 119 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |
OLD | NEW |