Chromium Code Reviews| 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_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 "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 | |
| 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 AllDownloadItemNotifier::Observer { | |
| 43 public: | |
| 44 DownloadSuggestionsProvider( | |
| 45 ContentSuggestionsProvider::Observer* observer, | |
| 46 ntp_snippets::CategoryFactory* category_factory, | |
| 47 const scoped_refptr<ntp_snippets::OfflinePageProxy>& offline_page_proxy, | |
| 48 content::DownloadManager* download_manager, | |
| 49 PrefService* pref_service, | |
| 50 bool download_manager_ui_enabled); | |
| 51 ~DownloadSuggestionsProvider() override; | |
| 52 | |
| 53 // ContentSuggestionsProvider implementation. | |
| 54 ntp_snippets::CategoryStatus GetCategoryStatus( | |
| 55 ntp_snippets::Category category) override; | |
| 56 ntp_snippets::CategoryInfo GetCategoryInfo( | |
| 57 ntp_snippets::Category category) override; | |
| 58 void DismissSuggestion( | |
| 59 const ntp_snippets::ContentSuggestion::ID& suggestion_id) override; | |
| 60 void FetchSuggestionImage( | |
| 61 const ntp_snippets::ContentSuggestion::ID& suggestion_id, | |
| 62 const ImageFetchedCallback& callback) override; | |
| 63 void ClearHistory( | |
| 64 base::Time begin, | |
| 65 base::Time end, | |
| 66 const base::Callback<bool(const GURL& url)>& filter) override; | |
| 67 void ClearCachedSuggestions(ntp_snippets::Category category) override; | |
| 68 void GetDismissedSuggestionsForDebugging( | |
| 69 ntp_snippets::Category category, | |
| 70 const DismissedSuggestionsCallback& callback) override; | |
| 71 void ClearDismissedSuggestionsForDebugging( | |
| 72 ntp_snippets::Category category) override; | |
| 73 | |
| 74 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
| 75 | |
| 76 private: | |
| 77 friend class DownloadSuggestionsProviderTest; | |
| 78 | |
| 79 void GetAllPagesCallbackForGetDismissedSuggestions( | |
| 80 const DismissedSuggestionsCallback& callback, | |
| 81 const std::vector<offline_pages::OfflinePageItem>& offline_pages) const; | |
| 82 | |
| 83 // OfflinePageProxy::Observer implementation. | |
| 84 void OfflinePageModelChanged( | |
| 85 const std::vector<offline_pages::OfflinePageItem>& offline_pages) | |
| 86 override; | |
| 87 void OfflinePageDeleted(int64_t offline_id, | |
| 88 const offline_pages::ClientId& client_id) override; | |
| 89 | |
| 90 // AllDownloadItemNotifier::Observer implementation. | |
| 91 void OnDownloadCreated(content::DownloadManager* manager, | |
| 92 content::DownloadItem* item) override; | |
| 93 void OnDownloadUpdated(content::DownloadManager* manager, | |
| 94 content::DownloadItem* item) override; | |
| 95 void OnDownloadOpened(content::DownloadManager* manager, | |
| 96 content::DownloadItem* item) override; | |
| 97 void OnDownloadRemoved(content::DownloadManager* manager, | |
| 98 content::DownloadItem* item) override; | |
| 99 | |
| 100 // Updates the |category_status_| of the |provided_category_| and notifies the | |
| 101 // |observer_|, if necessary. | |
| 102 void NotifyStatusChanged(ntp_snippets::CategoryStatus new_status); | |
| 103 | |
| 104 // Requests all offline pages and after asynchronously obtaining the result, | |
| 105 // prunes dismissed IDs and caches some most recent items, but does not notify | |
| 106 // |ContentSuggestionsProvider::Observer| about them. | |
| 107 void AsyncronouslyFetchOfflinePagesDownloads(); | |
|
Marc Treib
2016/10/13 12:11:26
s/Asyncronously/Asynchronously/
(missing an "h")
vitaliii
2016/10/15 18:36:30
Done.
| |
| 108 | |
| 109 // Like |FetchOfflinePagesDownloads| above, but with | |
| 110 // |SubmitContentSuggestions| afterwards. | |
| 111 void AsyncronouslyFetchOfflinePagesDownloadsAndSubmitSuggestions(); | |
| 112 | |
| 113 // Retrieves all asset downloads, prunes dismissed IDs and caches some most | |
| 114 // recent items, but does not notify |ContentSuggestionsProvider::Observer| | |
| 115 // about them. | |
| 116 void SyncronouslyFetchAssetsDownloads(); | |
|
Marc Treib
2016/10/13 12:11:26
nit: "Synchronously" isn't needed, that's the defa
vitaliii
2016/10/15 18:36:30
I would prefer Fetch...(), so that it is similar t
| |
| 117 | |
| 118 // Retrieves both offline page and asset downloads, updates the internal cache | |
| 119 // and notifies |ContentSuggestionsProvider::Observer|. | |
| 120 void AsyncronouslyFetchAllDownloadsAndSubmitSuggestions(); | |
| 121 | |
| 122 // Takes |kMaxSuggestionsCount| the most recent cached suggestions and | |
| 123 // notifies |ContentSuggestionsProvider::Observer| about them. | |
| 124 void SubmitContentSuggestions(); | |
| 125 | |
| 126 // Converts an OfflinePageItem to a ContentSuggestion for the | |
| 127 // |provided_category_|. | |
| 128 ntp_snippets::ContentSuggestion ConvertOfflinePage( | |
| 129 const offline_pages::OfflinePageItem& offline_page) const; | |
| 130 | |
| 131 // Converts DownloadItem to a ContentSuggestion for the |provided_category_|. | |
| 132 ntp_snippets::ContentSuggestion ConvertDownloadItem( | |
| 133 const content::DownloadItem& download_item) const; | |
| 134 | |
| 135 // Adds |download_item| to internal asset download cache if all of the | |
| 136 // following holds: | |
| 137 // - the download is completed; | |
| 138 // - its suggestion has not been dismissed; | |
| 139 // - there are less than |kMaxSuggestionsCount| items cached or the oldest | |
| 140 // cached item is older than the current item (the oldest item is removed | |
| 141 // then); | |
| 142 // - the item is not present in the cache yet. | |
| 143 // Returns |true| if the item has been added. | |
| 144 bool CacheAssetDownloadIfNeeded(const content::DownloadItem* item); | |
| 145 | |
| 146 // Removes item corresponding to |suggestion_id| either from offline pages or | |
| 147 // asset download cache (depends on the |suggestion_id|). Returns |false| if | |
| 148 // there is no corresponding item in the cache and so nothing was removed. | |
| 149 bool RemoveSuggestionFromCacheIfPresent( | |
| 150 const ntp_snippets::ContentSuggestion::ID& suggestion_id); | |
| 151 | |
| 152 // Removes item corresponding to |suggestion_id| from cache and fetches all | |
| 153 // corresponding downloads to update the cache if there are any not in cache. | |
| 154 void RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded( | |
| 155 const ntp_snippets::ContentSuggestion::ID& suggestion_id); | |
| 156 | |
| 157 // Processes a list of all offline pages available at the moment pruning | |
| 158 // dismissed IDs and updating internal cache. | |
| 159 void ProcessAllOfflinePages( | |
|
Marc Treib
2016/10/13 12:11:26
Hm. Maybe "UpdateOfflinePagesCache"? That's the ma
vitaliii
2016/10/15 18:36:30
Done.
| |
| 160 const std::vector<offline_pages::OfflinePageItem>& all_offline_pages); | |
| 161 | |
| 162 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding | |
| 163 // to the given |id_in_category| and clears it from the dismissed IDs list, if | |
| 164 // necessary. | |
| 165 void InvalidateSuggestion(const std::string& id_in_category); | |
| 166 | |
| 167 // Reads dismissed IDs related to asset downloads from prefs. | |
| 168 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const; | |
| 169 | |
| 170 // Writes |dismissed_ids| into prefs for asset downloads. | |
| 171 void StoreAssetDismissedIDsToPrefs( | |
| 172 const std::set<std::string>& dismissed_ids); | |
| 173 | |
| 174 // Reads dismissed IDs related to offline page downloads from prefs. | |
| 175 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; | |
| 176 | |
| 177 // Writes |dismissed_ids| into prefs for offline page downloads. | |
| 178 void StoreOfflinePageDismissedIDsToPrefs( | |
| 179 const std::set<std::string>& dismissed_ids); | |
| 180 | |
| 181 // Reads from prefs dismissed IDs related to either offline page or asset | |
| 182 // downloads (given by |for_offline_page_downloads|). | |
| 183 std::set<std::string> ReadDismissedIDsFromPrefs( | |
| 184 bool for_offline_page_downloads) const; | |
| 185 | |
| 186 // Writes |dismissed_ids| into prefs for either offline page or asset | |
| 187 // downloads (given by |for_offline_page_downloads|). | |
| 188 void StoreDismissedIDsToPrefs(bool for_offline_page_downloads, | |
| 189 const std::set<std::string>& dismissed_ids); | |
| 190 | |
| 191 ntp_snippets::CategoryStatus category_status_; | |
| 192 const ntp_snippets::Category provided_category_; | |
| 193 scoped_refptr<ntp_snippets::OfflinePageProxy> offline_page_proxy_; | |
| 194 AllDownloadItemNotifier download_manager_notifier_; | |
| 195 | |
| 196 PrefService* pref_service_; | |
| 197 | |
| 198 // Cached offline page downloads. If there are not enough asset downloads, all | |
| 199 // of these could be shown (they are the most recently visited, not dismissed | |
| 200 // and not invalidated). Order is undefined. If the model has less than | |
| 201 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the | |
| 202 // criteria above are cached, otherwise only |kMaxSuggestionsCount|. | |
| 203 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; | |
| 204 // Cached asset downloads. If there are not enough offline page downloads, all | |
| 205 // of these could be shown (they are the most recently downloaded, not | |
| 206 // dismissed and not invalidated). Order is undefined. If the model has less | |
| 207 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy | |
| 208 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|. | |
| 209 std::vector<const content::DownloadItem*> cached_asset_downloads_; | |
| 210 | |
| 211 // Whether the Download Manager UI is enabled, in which case the More button | |
| 212 // for the Downloads section can redirect there. | |
| 213 const bool download_manager_ui_enabled_; | |
| 214 | |
| 215 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; | |
| 216 | |
| 217 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); | |
| 218 }; | |
| 219 | |
| 220 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | |
| OLD | NEW |