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

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: the freshest rebase. 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 DownloadManager and each DownloadItem). Offline page related
37 // downloads are referred to as offline page downloads, while the remaining
38 // downloads (e.g. 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 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 |item| to the internal asset download cache if all of the following
134 // 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 has been
147 // removed.
148 bool RemoveSuggestionFromCacheIfPresent(
149 const ntp_snippets::ContentSuggestion::ID& suggestion_id);
150
151 // Removes item corresponding to |suggestion_id| from cache and fetches all
152 // corresponding downloads to update the cache if there may be any not in
153 // cache.
154 void RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(
155 const ntp_snippets::ContentSuggestion::ID& suggestion_id);
156
157 // Processes a list of offline pages (assuming that these are all the offline
158 // pages that currently exist), prunes dismissed IDs and updates internal
159 // cache. If |notify| is true, notifies
160 // |ContentSuggestionsProvider::Observer|.
161 void UpdateOfflinePagesCache(
162 bool notify,
163 const std::vector<offline_pages::OfflinePageItem>& all_offline_pages);
164
165 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding
166 // to the given |id_within_category| and clears it from the dismissed IDs
167 // list, if necessary.
168 void InvalidateSuggestion(const std::string& id_within_category);
169
170 // Reads dismissed IDs related to asset downloads from prefs.
171 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const;
172
173 // Writes |dismissed_ids| into prefs for asset downloads.
174 void StoreAssetDismissedIDsToPrefs(
175 const std::set<std::string>& dismissed_ids);
176
177 // Reads dismissed IDs related to offline page downloads from prefs.
178 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const;
179
180 // Writes |dismissed_ids| into prefs for offline page downloads.
181 void StoreOfflinePageDismissedIDsToPrefs(
182 const std::set<std::string>& dismissed_ids);
183
184 // Reads from prefs dismissed IDs related to either offline page or asset
185 // downloads (given by |for_offline_page_downloads|).
186 std::set<std::string> ReadDismissedIDsFromPrefs(
187 bool for_offline_page_downloads) const;
188
189 // Writes |dismissed_ids| into prefs for either offline page or asset
190 // downloads (given by |for_offline_page_downloads|).
191 void StoreDismissedIDsToPrefs(bool for_offline_page_downloads,
192 const std::set<std::string>& dismissed_ids);
193
194 void UnregisterDownloadItemObservers();
195
196 ntp_snippets::CategoryStatus category_status_;
197 const ntp_snippets::Category provided_category_;
198 scoped_refptr<ntp_snippets::OfflinePageProxy> offline_page_proxy_;
199 content::DownloadManager* download_manager_;
200
201 PrefService* pref_service_;
202
203 // Cached offline page downloads. If there are not enough asset downloads, all
204 // of these could be shown (they are the most recently visited, not dismissed
205 // and not invalidated). Order is undefined. If the model has less than
206 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the
207 // criteria above are cached, otherwise only |kMaxSuggestionsCount|.
208 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_;
209 // Cached asset downloads. If there are not enough offline page downloads, all
210 // of these could be shown (they are the most recently downloaded, not
211 // dismissed and not invalidated). Order is undefined. If the model has less
212 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy
213 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|.
214 std::vector<const content::DownloadItem*> cached_asset_downloads_;
215
216 // Whether the Download Manager UI is enabled, in which case the More button
217 // for the Downloads section can redirect there.
218 const bool download_manager_ui_enabled_;
219
220 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_;
221
222 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider);
223 };
224
225 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698