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" | 13 #include "base/memory/scoped_ptr.h" |
12 #include "chrome/browser/cancelable_request.h" | 14 #include "chrome/browser/cancelable_request.h" |
13 #include "chrome/browser/history/history.h" | 15 #include "chrome/browser/history/history.h" |
16 #include "content/public/browser/download_item.h" | |
17 #include "content/public/browser/download_manager.h" | |
14 | 18 |
15 class Profile; | 19 struct DownloadPersistentStoreInfo; |
16 | 20 |
17 namespace base { | 21 // Wraps HistoryService for easy mocking. The base implementation contains no |
18 class Time; | 22 // mechanisms for life time management, it's just a shim. DownloadHistory owns |
19 } | 23 // its HSDA. DownloadServiceFactory ensures that the downloads system is shut |
24 // down before HistoryService is destroyed. | |
Randy Smith (Not in Mondays)
2012/08/27 18:25:11
Hmmm. I'd either like this entire system to not r
benjhayden
2012/09/10 19:02:56
It looks like you're talking about two different l
| |
25 class HistoryServiceDownloadAdapter { | |
Randy Smith (Not in Mondays)
2012/08/27 18:25:11
I'm not sure whether this makes sense or not--this
benjhayden
2012/09/10 19:02:56
Done.
| |
26 public: | |
27 explicit HistoryServiceDownloadAdapter(HistoryService* history_service); | |
28 virtual ~HistoryServiceDownloadAdapter(); | |
29 virtual void QueryDownloads(const HistoryService::DownloadQueryCallback&); | |
30 virtual void GetVisibleVisitCountToHost( | |
31 const GURL& referrer_url, | |
32 const HistoryService::GetVisibleVisitCountToHostSimpleCallback& callback); | |
33 virtual void CreateDownload( | |
34 const DownloadPersistentStoreInfo& info, | |
35 const HistoryService::DownloadCreateCallback& callback); | |
36 virtual void UpdateDownload(const DownloadPersistentStoreInfo&); | |
37 virtual void RemoveDownloads(const std::set<int64>& handles); | |
20 | 38 |
21 namespace content { | 39 private: |
22 class DownloadItem; | 40 HistoryService* history_service_; |
23 } | |
24 | 41 |
25 // Interacts with the HistoryService on behalf of the download subsystem. | 42 DISALLOW_COPY_AND_ASSIGN(HistoryServiceDownloadAdapter); |
26 class DownloadHistory { | 43 }; |
44 | |
45 // Observes a single DownloadManager and all its DownloadItems, keeping the | |
46 // DownloadDatabase up to date. | |
47 class DownloadHistory: public content::DownloadManager::Observer, | |
48 public content::DownloadItem::Observer { | |
27 public: | 49 public: |
28 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; | 50 typedef base::Callback<void(bool)> VisitedBeforeDoneCallback; |
29 | 51 |
30 explicit DownloadHistory(Profile* profile); | 52 // Neither |manager| nor |history| may be NULL. |
31 ~DownloadHistory(); | 53 DownloadHistory( |
54 content::DownloadManager* manager, | |
55 scoped_ptr<HistoryServiceDownloadAdapter> history); | |
32 | 56 |
33 // Retrieves the next_id counter from the sql meta_table. | 57 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 | 58 |
38 // Retrieves DownloadCreateInfos saved in the history. | 59 // 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, | 60 void CheckVisitedReferrerBefore(int32 download_id, |
44 const GURL& referrer_url, | 61 const GURL& referrer_url, |
45 const VisitedBeforeDoneCallback& callback); | 62 const VisitedBeforeDoneCallback& callback); |
46 | 63 |
47 // Adds a new entry for a download to the history database. | 64 // content::DownloadManager::Observer |
48 void AddEntry(content::DownloadItem* download_item, | 65 virtual void OnDownloadCreated( |
49 const HistoryService::DownloadCreateCallback& callback); | 66 content::DownloadManager* manager, |
67 content::DownloadItem* item) OVERRIDE; | |
68 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE; | |
50 | 69 |
51 // Updates the history entry for |download_item|. | 70 // content::DownloadItem::Observer |
52 void UpdateEntry(content::DownloadItem* download_item); | 71 virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE; |
53 | 72 virtual void OnDownloadOpened(content::DownloadItem* item) OVERRIDE; |
54 // Updates the download path for |download_item| to |new_path|. | 73 virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE; |
55 void UpdateDownloadPath(content::DownloadItem* download_item, | 74 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 | 75 |
68 private: | 76 private: |
69 typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback> | 77 typedef std::set<int64> HandleSet; |
70 VisitedBeforeRequestsMap; | 78 typedef std::set<int32> IdSet; |
79 typedef std::set<content::DownloadItem*> ItemSet; | |
71 | 80 |
72 void OnGotVisitCountToHost(HistoryService::Handle handle, | 81 void MaybeAddToHistory(content::DownloadItem* item); |
73 bool found_visits, | 82 void ItemAdded(int32 id, int64 db_handle); |
74 int count, | 83 void RemoveDownloadsBatch(); |
75 base::Time first_visit); | 84 void OnGotVisitCountToHost( |
85 const VisitedBeforeDoneCallback& callback, | |
86 bool found_visits, int count, base::Time first_visit); | |
87 void QueryCallback( | |
88 scoped_ptr<std::vector<DownloadPersistentStoreInfo> > infos); | |
76 | 89 |
77 Profile* profile_; | 90 content::DownloadManager* manager_; |
91 ItemSet observing_items_; | |
78 | 92 |
79 // In case we don't have a valid db_handle, we use |fake_db_handle_| instead. | 93 scoped_ptr<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 | 94 |
85 CancelableRequestConsumer history_consumer_; | 95 // Whether QueryCallback() is in process of creating DownloadItems from the |
96 // database. OnDownloadCreated is called before QueryCallback() has a chance | |
97 // to attach a db_handle to the item, so this field is used to disable adding | |
98 // new items to the database. | |
99 bool loading_; | |
86 | 100 |
87 // The outstanding requests made by CheckVisitedReferrerBefore(). | 101 // |db_handles| of items that are scheduled for removal from history, to |
88 VisitedBeforeRequestsMap visited_before_requests_; | 102 // facilitate batching removals together for database efficiency. |
103 HandleSet removing_; | |
104 | |
105 // |GetId()|s of items that were removed while they were being added, so that | |
106 // they can be removed when their db_handles are received from the database. | |
107 IdSet removed_while_adding_; | |
108 | |
109 // Count the number of items in the history for UMA. | |
110 int64 history_size_; | |
111 | |
112 base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_; | |
89 | 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 |