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/hyperbolic_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 HyperbolicDownloadItemNotifier::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 base::Callback<void(bool)> VisitedBeforeDoneCallback; | 28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; |
29 typedef std::set<int64> HandleSet; | |
29 | 30 |
30 explicit DownloadHistory(Profile* profile); | 31 // Since there isn't (and shouldn't be) a general public way to access |
Randy Smith (Not in Mondays)
2012/09/24 18:03:25
Why shouldn't there be? It feels very weird for a
benjhayden
2012/11/02 17:21:37
Ok, yeah, I'll expose DownloadHistory so that we d
Randy Smith (Not in Mondays)
2012/11/02 23:31:24
I think it's a good idea.
benjhayden
2012/11/06 20:01:14
Done.
| |
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 CreateDownload/UpdateDownload messages are sent to the DB |
34 // Should be much faster than Load so that we may delay downloads until after | 43 // thread. |
35 // this call with minimal performance penalty. | 44 virtual void OnDownloadStored(const DownloadPersistentStoreInfo& info) {} |
36 void GetNextId(const HistoryService::DownloadNextIdCallback& callback); | |
37 | 45 |
38 // Retrieves DownloadCreateInfos saved in the history. | 46 // Fires when RemoveDownloads messages are sent to the DB thread. |
39 void Load(const HistoryService::DownloadQueryCallback& callback); | 47 virtual void OnDownloadsRemoved(const HandleSet& db_handle) {} |
Randy Smith (Not in Mondays)
2012/09/24 18:03:25
The lack of an equivalent to OnDownloadDestroyed/M
benjhayden
2012/11/02 17:21:37
I'm not sure who you're referring to by 'users'.
A
Randy Smith (Not in Mondays)
2012/11/02 23:31:24
I'm confused about how you're saying relates to my
benjhayden
2012/11/06 20:01:14
Ah! Yes, thanks.
Done.
| |
48 }; | |
40 | 49 |
41 // Checks whether |referrer_url| has been visited before today. This takes | 50 // Neither |manager| nor |history| may be NULL. |
42 // ownership of |callback|. | 51 // Caller must guarantee that HistoryService outlives DownloadHistory. |
Randy Smith (Not in Mondays)
2012/09/24 18:03:25
Suggestion: Add a note explicitly stating that we'
benjhayden
2012/11/02 17:21:37
What do you mean by 'independent'?
ADIN::Os deal w
Randy Smith (Not in Mondays)
2012/11/02 23:31:24
Yep, that's how the implementation deals, but user
benjhayden
2012/11/06 20:01:14
Done.
| |
43 void CheckVisitedReferrerBefore(int32 download_id, | 52 DownloadHistory( |
44 const GURL& referrer_url, | 53 content::DownloadManager* manager, |
54 HistoryService* history); | |
55 | |
56 virtual ~DownloadHistory(); | |
57 | |
58 void AddObserver(Observer* observer); | |
59 void RemoveObserver(Observer* observer); | |
60 | |
61 // Checks whether |referrer_url| has been visited before today. | |
62 void CheckVisitedReferrerBefore(const GURL& referrer_url, | |
45 const VisitedBeforeDoneCallback& callback); | 63 const VisitedBeforeDoneCallback& callback); |
46 | 64 |
47 // Adds a new entry for a download to the history database. | 65 private: |
48 void AddEntry(content::DownloadItem* download_item, | 66 typedef std::set<int32> IdSet; |
49 const HistoryService::DownloadCreateCallback& callback); | 67 typedef std::set<content::DownloadItem*> ItemSet; |
50 | 68 |
51 // Updates the history entry for |download_item|. | 69 // HyperbolicDownloadItemNotifier::Observer |
52 void UpdateEntry(content::DownloadItem* download_item); | 70 virtual void OnDownloadCreated( |
71 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; | |
72 virtual void OnDownloadUpdated( | |
73 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; | |
74 virtual void OnDownloadOpened( | |
75 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; | |
76 virtual void OnDownloadRemoved( | |
77 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE; | |
53 | 78 |
54 // Updates the download path for |download_item| to |new_path|. | 79 void MaybeAddToHistory(content::DownloadItem* item); |
55 void UpdateDownloadPath(content::DownloadItem* download_item, | |
56 const FilePath& new_path); | |
57 | 80 |
58 // Removes |download_item| from the history database. | 81 void ItemAdded(int32 id, int64 db_handle); |
59 void RemoveEntry(content::DownloadItem* download_item); | |
60 | 82 |
61 // Removes download-related history entries in the given time range. | 83 void RemoveDownloadsBatch(); |
62 void RemoveEntriesBetween(const base::Time remove_begin, | |
63 const base::Time remove_end); | |
64 | 84 |
65 // Returns a new unique database handle which will not collide with real ones. | 85 void OnGotVisitCountToHost( |
66 int64 GetNextFakeDbHandle(); | 86 const VisitedBeforeDoneCallback& callback, |
87 HistoryService::Handle unused_handle, | |
88 bool found_visits, int count, base::Time first_visit); | |
67 | 89 |
68 private: | 90 void QueryCallback( |
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 91 std::vector<DownloadPersistentStoreInfo>* infos); |
70 VisitedBeforeRequestsMap; | |
71 | 92 |
72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 93 HyperbolicDownloadItemNotifier notifier_; |
73 bool found_visits, | |
74 int count, | |
75 base::Time first_visit); | |
76 | 94 |
77 Profile* profile_; | 95 HistoryService* history_; |
78 | 96 |
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 97 // |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. | 98 // 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 | 99 // not re-added to the database. For items not created by QueryCallback(), |
82 // fake handle value on every use. | 100 // this is DownloadDatabase::kUninitializedHandle. |
83 int64 next_fake_db_handle_; | 101 int64 loading_db_handle_; |
102 | |
103 // |db_handles| of items that are scheduled for removal from history, to | |
104 // facilitate batching removals together for database efficiency. | |
105 HandleSet removing_; | |
106 | |
107 // |GetId()|s of items that were removed while they were being added, so that | |
108 // they can be removed when their db_handles are received from the database. | |
109 IdSet removed_while_adding_; | |
110 | |
111 // Count the number of items in the history for UMA. | |
112 int64 history_size_; | |
113 | |
114 ObserverList<Observer> observers_; | |
84 | 115 |
85 CancelableRequestConsumer history_consumer_; | 116 CancelableRequestConsumer history_consumer_; |
86 | 117 |
87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 118 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_; |
88 VisitedBeforeRequestsMap visited_before_requests_; | |
89 | 119 |
90 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); | 120 DISALLOW_COPY_AND_ASSIGN(DownloadHistory); |
91 }; | 121 }; |
92 | 122 |
93 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ | 123 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_ |
OLD | NEW |