OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_NTP_SNIPPETS_DOWNLOADS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | |
Marc Treib
2016/10/11 12:07:42
I don't think this needs a "downloads" subfolder.
vitaliii
2016/10/13 08:43:07
As it was discussed, this provider is not by desig
| |
6 #define CHROME_BROWSER_NTP_SNIPPETS_DOWNLOADS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | |
7 | |
8 #include <set> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback_forward.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "chrome/browser/download/all_download_item_notifier.h" | |
16 #include "components/ntp_snippets/category.h" | |
17 #include "components/ntp_snippets/category_factory.h" | |
18 #include "components/ntp_snippets/category_status.h" | |
19 #include "components/ntp_snippets/content_suggestion.h" | |
20 #include "components/ntp_snippets/content_suggestions_provider.h" | |
21 #include "components/ntp_snippets/offline_pages/offline_page_proxy.h" | |
22 #include "components/offline_pages/offline_page_item.h" | |
Marc Treib
2016/10/11 12:07:42
I think a forward declaration is enough here
vitaliii
2016/10/13 08:43:07
Done.
| |
23 | |
24 class PrefRegistrySimple; | |
25 class PrefService; | |
26 | |
27 namespace gfx { | |
28 class Image; | |
29 } | |
30 | |
31 namespace ntp_snippets { | |
Marc Treib
2016/10/11 12:07:42
No namespace; that's used only for the component.
vitaliii
2016/10/13 08:43:07
Done.
| |
32 | |
33 // Provides download content suggestions from the offline pages model (obtaining | |
34 // the data through OfflinePageProxy) and the download manager (obtaining the | |
35 // data through AllDownloadItemNotifier). Offline pages related downloads are | |
36 // referred to as offline page downloads, while the remaining downloads (e.g. | |
37 // images, music, books) are called asset downloads. | |
38 class DownloadSuggestionsProvider : public ContentSuggestionsProvider, | |
39 public OfflinePageProxy::Observer, | |
40 public AllDownloadItemNotifier::Observer { | |
41 public: | |
42 DownloadSuggestionsProvider( | |
43 ContentSuggestionsProvider::Observer* observer, | |
44 CategoryFactory* category_factory, | |
45 const scoped_refptr<OfflinePageProxy>& offline_page_proxy, | |
46 content::DownloadManager* download_manager, | |
47 PrefService* pref_service, | |
48 bool download_manager_ui_enabled); | |
49 ~DownloadSuggestionsProvider() override; | |
50 | |
51 // ContentSuggestionsProvider implementation. | |
52 CategoryStatus GetCategoryStatus(Category category) override; | |
53 CategoryInfo GetCategoryInfo(Category category) override; | |
54 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | |
55 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | |
56 const ImageFetchedCallback& callback) override; | |
57 void ClearHistory( | |
58 base::Time begin, | |
59 base::Time end, | |
60 const base::Callback<bool(const GURL& url)>& filter) override; | |
61 void ClearCachedSuggestions(Category category) override; | |
62 void GetDismissedSuggestionsForDebugging( | |
63 Category category, | |
64 const DismissedSuggestionsCallback& callback) override; | |
65 void ClearDismissedSuggestionsForDebugging(Category category) override; | |
66 | |
67 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
68 | |
69 private: | |
70 // OfflinePageProxy::Observer implementation. | |
71 void OfflinePageModelChanged( | |
72 const std::vector<offline_pages::OfflinePageItem>& offline_pages) | |
73 override; | |
74 void OfflinePageDeleted(int64_t offline_id, | |
75 const offline_pages::ClientId& client_id) override; | |
76 | |
77 // AllDownloadItemNotifier::Observer implementation. | |
78 void OnDownloadCreated(content::DownloadManager* manager, | |
79 content::DownloadItem* item) override; | |
80 void OnDownloadUpdated(content::DownloadManager* manager, | |
81 content::DownloadItem* item) override; | |
82 void OnDownloadOpened(content::DownloadManager* manager, | |
83 content::DownloadItem* item) override; | |
84 void OnDownloadRemoved(content::DownloadManager* manager, | |
85 content::DownloadItem* item) override; | |
86 | |
87 // Updates the |category_status_| of the |provided_category_| and notifies the | |
88 // |observer_|, if necessary. | |
89 void NotifyStatusChanged(CategoryStatus new_status); | |
90 | |
91 // Requests all offline pages and after asynchronously obtaining the result, | |
92 // prunes dismissed IDs and caches some most recent items, but does not notify | |
93 // the service. | |
Marc Treib
2016/10/11 12:07:41
"the service" being ContentSuggestionsService? Tec
vitaliii
2016/10/13 08:43:07
Done.
| |
94 void FetchOfflinePagesDownloads(); | |
95 | |
96 // Like |FetchOfflinePagesDownloads| above, but with | |
97 // |SubmitContentSuggestions| afterwards. | |
98 void FetchOfflinePagesDownloadsAndSubmitSuggestions(); | |
99 | |
100 // Retrieves all asset downloads, prunes dismissed IDs and caches some most | |
101 // recent items, but does not notify the service. | |
102 void FetchAssetsDownloads(); | |
103 | |
104 // Retrieves both offline page and asset downloads, updates the internal cache | |
105 // and notifies the service. | |
106 void FetchAllDownloadsAndSubmitSuggestions(); | |
107 | |
108 // Takes |kMaxSuggestionsCount| the most recent cached suggestions and | |
109 // notifies the service about them. | |
110 void SubmitContentSuggestions(); | |
111 | |
112 // Converts an OfflinePageItem to a ContentSuggestion for the | |
113 // |provided_category_|. | |
114 ContentSuggestion ConvertOfflinePage( | |
115 const offline_pages::OfflinePageItem& offline_page) const; | |
116 | |
117 // Converts DownloadItem to a ContentSuggestion for the |provided_category_|. | |
118 ContentSuggestion ConvertDownloadItem( | |
119 const content::DownloadItem* download_item) const; | |
120 | |
121 // Adds |download_item| to internal asset download cache if all of the | |
122 // following holds: | |
123 // - the download is completed; | |
124 // - its suggestion has not been dismissed; | |
125 // - there are less than |kMaxSuggestionsCount| items cached or the oldest | |
126 // cached item is older than the current item (the oldest item is removed | |
127 // then); | |
128 // - the item is not present in the cache yet. | |
129 // Returns |true| if the item has been added. | |
130 bool CacheAssetDownloadIfNeeded(const content::DownloadItem* item); | |
131 | |
132 // Removes item corresponding to |suggestion_id| either from offline pages or | |
133 // asset download cache (depends on the suggestion). Returns |false| if there | |
134 // are no corresponding item is not cached. | |
Marc Treib
2016/10/11 12:07:42
"if there are no corresponding item is not cached"
vitaliii
2016/10/13 08:43:07
Done.
| |
135 bool RemoveSuggestionFromCacheIfPresent( | |
136 const ContentSuggestion::ID& suggestion_id); | |
137 | |
138 // Removes item corresponding to |suggestion_id| from cache and fetches all | |
139 // corresponding downloads to update the cache if there are any not in cache. | |
vitaliii
2016/10/13 08:43:07
Can you parse this one?
| |
140 void RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded( | |
141 const ContentSuggestion::ID& suggestion_id); | |
142 | |
143 // Processes a list of all offline pages available at the moment pruning | |
144 // dismissed IDs and updating internal cache. | |
145 void ProcessAllOfflinePages( | |
146 const std::vector<offline_pages::OfflinePageItem>& all_offline_pages); | |
147 | |
148 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding | |
149 // to the given |id_in_category| and clears it from the dismissed IDs list, if | |
150 // necessary. | |
151 void InvalidateSuggestion(const std::string& id_in_category); | |
152 | |
153 // Reads dismissed IDs related to asset downloads from Prefs. | |
Marc Treib
2016/10/11 12:07:41
nit: prefs, not capitalized. Also below.
vitaliii
2016/10/13 08:43:07
Done.
| |
154 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const; | |
155 | |
156 // Writes |dismissed_ids| into Prefs for asset downloads. | |
157 void StoreAssetDismissedIDsToPrefs( | |
158 const std::set<std::string>& dismissed_ids); | |
159 | |
160 // Reads dismissed IDs related to offline page downloads from Prefs. | |
161 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; | |
162 | |
163 // Writes |dismissed_ids| into Prefs for offline page downloads. | |
164 void StoreOfflinePageDismissedIDsToPrefs( | |
165 const std::set<std::string>& dismissed_ids); | |
166 | |
167 // Reads from Prefs dismissed IDs related to either offline page or asset | |
168 // downloads (depending on |suggestion_id|). | |
169 std::set<std::string> ReadDismissedIDsFromPrefs( | |
170 const ContentSuggestion::ID& suggestion_id) const; | |
171 | |
172 // Writes |dismissed_ids| into Prefs for either offline page or asset | |
173 // downloads (depending on |suggestion_id|). | |
174 void StoreDismissedIDsToPrefs(const ContentSuggestion::ID& suggestion_id, | |
175 const std::set<std::string>& dismissed_ids); | |
176 | |
177 CategoryStatus category_status_; | |
178 const Category provided_category_; | |
179 scoped_refptr<OfflinePageProxy> offline_page_proxy_; | |
180 AllDownloadItemNotifier download_manager_notifier_; | |
181 | |
182 PrefService* pref_service_; | |
183 | |
184 // Cached offline page downloads. If there are not enough asset downloads, all | |
185 // of these could be shown (they are the most recently visited, not dismissed | |
186 // and not invalidated). Order is undefined. If the model has less than | |
187 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the | |
188 // criteria above are cached, otherwise only |kMaxSuggestionsCount|. | |
Marc Treib
2016/10/11 12:07:42
nit: For the last sentence, I'd just say something
vitaliii
2016/10/13 08:43:07
But the suggested sentence does not say that there
Marc Treib
2016/10/13 12:11:25
...eh, fair enough.
vitaliii
2016/10/15 18:36:30
Acknowledged.
| |
189 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; | |
190 // Cached asset downloads. If there are not enough offline page downloads, all | |
191 // of these could be shown (they are the most recently downloaded, not | |
192 // dismissed and not invalidated). Order is undefined. If the model has less | |
193 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy | |
194 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|. | |
195 std::vector<const content::DownloadItem*> cached_asset_downloads_; | |
196 | |
197 // Whether the Download Manager UI is enabled, in which case the More button | |
198 // for the Downloads section can redirect there. | |
199 bool download_manager_ui_enabled_; | |
Marc Treib
2016/10/11 12:07:41
nit: const?
vitaliii
2016/10/13 08:43:07
Done.
| |
200 | |
201 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; | |
202 | |
203 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); | |
204 }; | |
205 | |
206 } // namespace ntp_snippets | |
207 | |
208 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOADS_DOWNLOAD_SUGGESTIONS_PROVIDER_H _ | |
OLD | NEW |