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 "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 these are all available at | |
| 156 // the moment), prunes dismissed IDs and updates internal cache. | |
|
Marc Treib
2016/10/17 10:18:41
nit: I don't understand the part in parens - what
vitaliii
2016/10/26 00:07:55
Done.
| |
| 157 void UpdateOfflinePagesCache( | |
| 158 const std::vector<offline_pages::OfflinePageItem>& all_offline_pages); | |
| 159 | |
| 160 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding | |
| 161 // to the given |id_within_category| and clears it from the dismissed IDs | |
| 162 // list, if necessary. | |
| 163 void InvalidateSuggestion(const std::string& id_within_category); | |
| 164 | |
| 165 // Reads dismissed IDs related to asset downloads from prefs. | |
| 166 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const; | |
| 167 | |
| 168 // Writes |dismissed_ids| into prefs for asset downloads. | |
| 169 void StoreAssetDismissedIDsToPrefs( | |
| 170 const std::set<std::string>& dismissed_ids); | |
| 171 | |
| 172 // Reads dismissed IDs related to offline page downloads from prefs. | |
| 173 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; | |
| 174 | |
| 175 // Writes |dismissed_ids| into prefs for offline page downloads. | |
| 176 void StoreOfflinePageDismissedIDsToPrefs( | |
| 177 const std::set<std::string>& dismissed_ids); | |
| 178 | |
| 179 // Reads from prefs dismissed IDs related to either offline page or asset | |
| 180 // downloads (given by |for_offline_page_downloads|). | |
| 181 std::set<std::string> ReadDismissedIDsFromPrefs( | |
| 182 bool for_offline_page_downloads) const; | |
| 183 | |
| 184 // Writes |dismissed_ids| into prefs for either offline page or asset | |
| 185 // downloads (given by |for_offline_page_downloads|). | |
| 186 void StoreDismissedIDsToPrefs(bool for_offline_page_downloads, | |
| 187 const std::set<std::string>& dismissed_ids); | |
| 188 | |
| 189 ntp_snippets::CategoryStatus category_status_; | |
| 190 const ntp_snippets::Category provided_category_; | |
| 191 scoped_refptr<ntp_snippets::OfflinePageProxy> offline_page_proxy_; | |
| 192 content::DownloadManager* download_manager_; | |
| 193 | |
| 194 PrefService* pref_service_; | |
| 195 | |
| 196 // Cached offline page downloads. If there are not enough asset downloads, all | |
| 197 // of these could be shown (they are the most recently visited, not dismissed | |
| 198 // and not invalidated). Order is undefined. If the model has less than | |
| 199 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the | |
| 200 // criteria above are cached, otherwise only |kMaxSuggestionsCount|. | |
| 201 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; | |
| 202 // Cached asset downloads. If there are not enough offline page downloads, all | |
| 203 // of these could be shown (they are the most recently downloaded, not | |
| 204 // dismissed and not invalidated). Order is undefined. If the model has less | |
| 205 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy | |
| 206 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|. | |
| 207 std::vector<const content::DownloadItem*> cached_asset_downloads_; | |
| 208 | |
| 209 // Whether the Download Manager UI is enabled, in which case the More button | |
| 210 // for the Downloads section can redirect there. | |
| 211 const bool download_manager_ui_enabled_; | |
| 212 | |
| 213 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; | |
| 214 | |
| 215 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); | |
| 216 }; | |
| 217 | |
| 218 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | |
| OLD | NEW |