Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: chrome/browser/ntp_snippets/download_suggestions_provider.h

Issue 2360263002: [NTPSnippets] Show all downloads on the NTP (3/3): Downloads provider. (Closed)
Patch Set: rebase + Marc's comments. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_DOWNLOAD_SUGGESTIONS_PROVIDER_H_
6 #define CHROME_BROWSER_NTP_SNIPPETS_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 "components/ntp_snippets/category.h"
16 #include "components/ntp_snippets/category_factory.h"
17 #include "components/ntp_snippets/category_status.h"
18 #include "components/ntp_snippets/content_suggestion.h"
19 #include "components/ntp_snippets/content_suggestions_provider.h"
20 #include "components/ntp_snippets/offline_pages/offline_page_proxy.h"
21 #include "content/public/browser/download_manager.h"
22
23 class PrefRegistrySimple;
24 class PrefService;
25
26 namespace gfx {
27 class Image;
28 }
29
30 namespace offline_pages {
31 struct OfflinePageItem;
32 }
33
34 // Provides download content suggestions from the offline pages model (obtaining
35 // the data through OfflinePageProxy) and the download manager (obtaining the
36 // data through AllDownloadItemNotifier). Offline pages related downloads are
37 // referred to as offline page downloads, while the remaining downloads (e.g.
38 // images, music, books) are called asset downloads.
39 class DownloadSuggestionsProvider
40 : public ntp_snippets::ContentSuggestionsProvider,
41 public ntp_snippets::OfflinePageProxy::Observer,
42 public content::DownloadManager::Observer,
43 public content::DownloadItem::Observer {
44 public:
45 DownloadSuggestionsProvider(
46 ContentSuggestionsProvider::Observer* observer,
47 ntp_snippets::CategoryFactory* category_factory,
48 const scoped_refptr<ntp_snippets::OfflinePageProxy>& offline_page_proxy,
49 content::DownloadManager* download_manager,
50 PrefService* pref_service,
51 bool download_manager_ui_enabled);
52 ~DownloadSuggestionsProvider() override;
53
54 // ContentSuggestionsProvider implementation.
55 ntp_snippets::CategoryStatus GetCategoryStatus(
56 ntp_snippets::Category category) override;
57 ntp_snippets::CategoryInfo GetCategoryInfo(
58 ntp_snippets::Category category) override;
59 void DismissSuggestion(
60 const ntp_snippets::ContentSuggestion::ID& suggestion_id) override;
61 void FetchSuggestionImage(
62 const ntp_snippets::ContentSuggestion::ID& suggestion_id,
63 const ImageFetchedCallback& callback) override;
64 void ClearHistory(
65 base::Time begin,
66 base::Time end,
67 const base::Callback<bool(const GURL& url)>& filter) override;
68 void ClearCachedSuggestions(ntp_snippets::Category category) override;
69 void GetDismissedSuggestionsForDebugging(
70 ntp_snippets::Category category,
71 const DismissedSuggestionsCallback& callback) override;
72 void ClearDismissedSuggestionsForDebugging(
73 ntp_snippets::Category category) override;
74
75 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
76
77 private:
78 friend class DownloadSuggestionsProviderTest;
79
80 void GetAllPagesCallbackForGetDismissedSuggestions(
81 const DismissedSuggestionsCallback& callback,
82 const std::vector<offline_pages::OfflinePageItem>& offline_pages) const;
83
84 // OfflinePageProxy::Observer implementation.
85 void OfflinePageModelChanged(
86 const std::vector<offline_pages::OfflinePageItem>& offline_pages)
87 override;
88 void OfflinePageDeleted(int64_t offline_id,
89 const offline_pages::ClientId& client_id) override;
90
91 // content::DownloadManager::Observer implementation.
92 void OnDownloadCreated(content::DownloadManager* manager,
93 content::DownloadItem* item) override;
94 void ManagerGoingDown(content::DownloadManager* manager) override;
95
96 // content::DownloadItem::Observer implementation.
97 void OnDownloadUpdated(content::DownloadItem* item) override;
98 void OnDownloadOpened(content::DownloadItem* item) override;
99 void OnDownloadRemoved(content::DownloadItem* item) override;
100 void OnDownloadDestroyed(content::DownloadItem* item) override;
101
102 // Updates the |category_status_| of the |provided_category_| and notifies the
103 // |observer_|, if necessary.
104 void NotifyStatusChanged(ntp_snippets::CategoryStatus new_status);
105
106 // Requests all offline pages and after asynchronously obtaining the result,
107 // prunes dismissed IDs and caches some most recent items. If |notify| is
108 // true, notifies |ContentSuggestionsProvider::Observer| about them.
109 void AsynchronouslyFetchOfflinePagesDownloads(bool notify);
110
111 // Retrieves all asset downloads, prunes dismissed IDs and caches some most
112 // recent items, but does not notify |ContentSuggestionsProvider::Observer|
113 // about them.
114 void FetchAssetsDownloads();
115
116 // Retrieves both offline page and asset downloads, updates the internal cache
117 // and notifies |ContentSuggestionsProvider::Observer|.
118 void AsynchronouslyFetchAllDownloadsAndSubmitSuggestions();
119
120 // Takes |kMaxSuggestionsCount| the most recent cached suggestions and
121 // notifies |ContentSuggestionsProvider::Observer| about them.
122 void SubmitContentSuggestions();
123
124 // Converts an OfflinePageItem to a ContentSuggestion for the
125 // |provided_category_|.
126 ntp_snippets::ContentSuggestion ConvertOfflinePage(
127 const offline_pages::OfflinePageItem& offline_page) const;
128
129 // Converts DownloadItem to a ContentSuggestion for the |provided_category_|.
130 ntp_snippets::ContentSuggestion ConvertDownloadItem(
131 const content::DownloadItem& download_item) const;
132
133 // Adds |download_item| to internal asset download cache if all of the
134 // following holds:
135 // - the download is completed;
136 // - its suggestion has not been dismissed;
137 // - there are less than |kMaxSuggestionsCount| items cached or the oldest
138 // cached item is older than the current item (the oldest item is removed
139 // then);
140 // - the item is not present in the cache yet.
141 // Returns |true| if the item has been added.
142 bool CacheAssetDownloadIfNeeded(const content::DownloadItem* item);
143
144 // Removes item corresponding to |suggestion_id| either from offline pages or
145 // asset download cache (depends on the |suggestion_id|). Returns |false| if
146 // there is no corresponding item in the cache and so nothing was removed.
147 bool RemoveSuggestionFromCacheIfPresent(
148 const ntp_snippets::ContentSuggestion::ID& suggestion_id);
149
150 // Removes item corresponding to |suggestion_id| from cache and fetches all
151 // corresponding downloads to update the cache if there are any not in cache.
152 void RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(
153 const ntp_snippets::ContentSuggestion::ID& suggestion_id);
154
155 // Processes a list of offline pages (assuming that all available at the
156 // moment offline pages are in the list), prunes dismissed IDs and updates
Marc Treib 2016/10/26 08:54:09 grammar nit: "all offline pages available at the m
vitaliii 2016/10/27 15:49:20 Done.
157 // internal cache. If |notify| is true, notifies
158 // |ContentSuggestionsProvider::Observer|.
159 void UpdateOfflinePagesCache(
160 bool notify,
161 const std::vector<offline_pages::OfflinePageItem>& all_offline_pages);
162
163 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding
164 // to the given |id_within_category| and clears it from the dismissed IDs
165 // list, if necessary.
166 void InvalidateSuggestion(const std::string& id_within_category);
167
168 // Reads dismissed IDs related to asset downloads from prefs.
169 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const;
170
171 // Writes |dismissed_ids| into prefs for asset downloads.
172 void StoreAssetDismissedIDsToPrefs(
173 const std::set<std::string>& dismissed_ids);
174
175 // Reads dismissed IDs related to offline page downloads from prefs.
176 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const;
177
178 // Writes |dismissed_ids| into prefs for offline page downloads.
179 void StoreOfflinePageDismissedIDsToPrefs(
180 const std::set<std::string>& dismissed_ids);
181
182 // Reads from prefs dismissed IDs related to either offline page or asset
183 // downloads (given by |for_offline_page_downloads|).
184 std::set<std::string> ReadDismissedIDsFromPrefs(
185 bool for_offline_page_downloads) const;
186
187 // Writes |dismissed_ids| into prefs for either offline page or asset
188 // downloads (given by |for_offline_page_downloads|).
189 void StoreDismissedIDsToPrefs(bool for_offline_page_downloads,
190 const std::set<std::string>& dismissed_ids);
191
192 void UnregisterDownloadItemObservers();
193
194 ntp_snippets::CategoryStatus category_status_;
195 const ntp_snippets::Category provided_category_;
196 scoped_refptr<ntp_snippets::OfflinePageProxy> offline_page_proxy_;
197 content::DownloadManager* download_manager_;
198
199 PrefService* pref_service_;
200
201 // Cached offline page downloads. If there are not enough asset downloads, all
202 // of these could be shown (they are the most recently visited, not dismissed
203 // and not invalidated). Order is undefined. If the model has less than
204 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the
205 // criteria above are cached, otherwise only |kMaxSuggestionsCount|.
206 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_;
207 // Cached asset downloads. If there are not enough offline page downloads, all
208 // of these could be shown (they are the most recently downloaded, not
209 // dismissed and not invalidated). Order is undefined. If the model has less
210 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy
211 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|.
212 std::vector<const content::DownloadItem*> cached_asset_downloads_;
213
214 // Whether the Download Manager UI is enabled, in which case the More button
215 // for the Downloads section can redirect there.
216 const bool download_manager_ui_enabled_;
217
218 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_;
219
220 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider);
221 };
222
223 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698