Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |
| 6 #define CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | 6 #define CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "chrome/browser/download/download_history.h" | |
| 15 #include "components/ntp_snippets/callbacks.h" | 16 #include "components/ntp_snippets/callbacks.h" |
| 16 #include "components/ntp_snippets/category.h" | 17 #include "components/ntp_snippets/category.h" |
| 17 #include "components/ntp_snippets/category_status.h" | 18 #include "components/ntp_snippets/category_status.h" |
| 18 #include "components/ntp_snippets/content_suggestion.h" | 19 #include "components/ntp_snippets/content_suggestion.h" |
| 19 #include "components/ntp_snippets/content_suggestions_provider.h" | 20 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 20 #include "components/offline_pages/core/offline_page_model.h" | 21 #include "components/offline_pages/core/offline_page_model.h" |
| 21 #include "content/public/browser/download_manager.h" | 22 #include "content/public/browser/download_manager.h" |
| 22 | 23 |
| 23 class PrefRegistrySimple; | 24 class PrefRegistrySimple; |
| 24 class PrefService; | 25 class PrefService; |
| 25 | 26 |
| 26 namespace offline_pages { | 27 namespace offline_pages { |
| 27 struct OfflinePageItem; | 28 struct OfflinePageItem; |
| 28 } | 29 } |
| 29 | 30 |
| 30 // Provides download content suggestions from the offline pages model and the | 31 // Provides download content suggestions from the offline pages model and the |
| 31 // download manager (obtaining the data through DownloadManager and each | 32 // download manager (obtaining the data through DownloadManager and each |
| 32 // DownloadItem). Offline page related downloads are referred to as offline page | 33 // DownloadItem). Offline page related downloads are referred to as offline page |
| 33 // downloads, while the remaining downloads (e.g. images, music, books) are | 34 // downloads, while the remaining downloads (e.g. images, music, books) are |
| 34 // called asset downloads. In case either of the data sources is |nullptr|, it | 35 // called asset downloads. In case either of the data sources is |nullptr|, it |
| 35 // is ignored. | 36 // is ignored. |
| 36 class DownloadSuggestionsProvider | 37 class DownloadSuggestionsProvider |
| 37 : public ntp_snippets::ContentSuggestionsProvider, | 38 : public ntp_snippets::ContentSuggestionsProvider, |
| 38 public offline_pages::OfflinePageModel::Observer, | 39 public offline_pages::OfflinePageModel::Observer, |
| 39 public content::DownloadManager::Observer, | 40 public content::DownloadManager::Observer, |
| 40 public content::DownloadItem::Observer { | 41 public content::DownloadItem::Observer, |
| 42 public DownloadHistory::Observer { | |
|
tschumann
2017/01/12 11:06:09
wow, that's a massive list of interfaces to implem
vitaliii
2017/01/16 08:51:35
I have never seen this before in Chromium.
treib@
tschumann
2017/01/16 09:14:05
=) Well, not sure if you'd notice after the refact
Marc Treib
2017/01/16 10:33:05
There definitely are instances of what Tim suggest
vitaliii
2017/01/16 14:32:39
Acknowledged.
| |
| 41 public: | 43 public: |
| 42 DownloadSuggestionsProvider( | 44 DownloadSuggestionsProvider( |
| 43 ContentSuggestionsProvider::Observer* observer, | 45 ContentSuggestionsProvider::Observer* observer, |
| 44 offline_pages::OfflinePageModel* offline_page_model, | 46 offline_pages::OfflinePageModel* offline_page_model, |
| 45 content::DownloadManager* download_manager, | 47 content::DownloadManager* download_manager, |
| 48 DownloadHistory* download_history, | |
| 46 PrefService* pref_service, | 49 PrefService* pref_service, |
| 47 bool download_manager_ui_enabled); | 50 bool download_manager_ui_enabled); |
| 48 ~DownloadSuggestionsProvider() override; | 51 ~DownloadSuggestionsProvider() override; |
| 49 | 52 |
| 50 // ContentSuggestionsProvider implementation. | 53 // ContentSuggestionsProvider implementation. |
| 51 ntp_snippets::CategoryStatus GetCategoryStatus( | 54 ntp_snippets::CategoryStatus GetCategoryStatus( |
| 52 ntp_snippets::Category category) override; | 55 ntp_snippets::Category category) override; |
| 53 ntp_snippets::CategoryInfo GetCategoryInfo( | 56 ntp_snippets::CategoryInfo GetCategoryInfo( |
| 54 ntp_snippets::Category category) override; | 57 ntp_snippets::Category category) override; |
| 55 void DismissSuggestion( | 58 void DismissSuggestion( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 66 const base::Callback<bool(const GURL& url)>& filter) override; | 69 const base::Callback<bool(const GURL& url)>& filter) override; |
| 67 void ClearCachedSuggestions(ntp_snippets::Category category) override; | 70 void ClearCachedSuggestions(ntp_snippets::Category category) override; |
| 68 void GetDismissedSuggestionsForDebugging( | 71 void GetDismissedSuggestionsForDebugging( |
| 69 ntp_snippets::Category category, | 72 ntp_snippets::Category category, |
| 70 const ntp_snippets::DismissedSuggestionsCallback& callback) override; | 73 const ntp_snippets::DismissedSuggestionsCallback& callback) override; |
| 71 void ClearDismissedSuggestionsForDebugging( | 74 void ClearDismissedSuggestionsForDebugging( |
| 72 ntp_snippets::Category category) override; | 75 ntp_snippets::Category category) override; |
| 73 | 76 |
| 74 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 77 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 75 | 78 |
| 76 static int GetMaxDismissedCountForTesting(); | |
| 77 | |
| 78 private: | 79 private: |
| 79 friend class DownloadSuggestionsProviderTest; | 80 friend class DownloadSuggestionsProviderTest; |
| 80 | 81 |
| 81 void GetPagesMatchingQueryCallbackForGetDismissedSuggestions( | 82 void GetPagesMatchingQueryCallbackForGetDismissedSuggestions( |
| 82 const ntp_snippets::DismissedSuggestionsCallback& callback, | 83 const ntp_snippets::DismissedSuggestionsCallback& callback, |
| 83 const std::vector<offline_pages::OfflinePageItem>& offline_pages) const; | 84 const std::vector<offline_pages::OfflinePageItem>& offline_pages) const; |
| 84 | 85 |
| 85 // OfflinePageModel::Observer implementation. | 86 // OfflinePageModel::Observer implementation. |
| 86 void OfflinePageModelLoaded(offline_pages::OfflinePageModel* model) override; | 87 void OfflinePageModelLoaded(offline_pages::OfflinePageModel* model) override; |
| 87 void OfflinePageAdded( | 88 void OfflinePageAdded( |
| 88 offline_pages::OfflinePageModel* model, | 89 offline_pages::OfflinePageModel* model, |
| 89 const offline_pages::OfflinePageItem& added_page) override; | 90 const offline_pages::OfflinePageItem& added_page) override; |
| 90 void OfflinePageDeleted(int64_t offline_id, | 91 void OfflinePageDeleted(int64_t offline_id, |
| 91 const offline_pages::ClientId& client_id) override; | 92 const offline_pages::ClientId& client_id) override; |
| 92 | 93 |
| 93 // content::DownloadManager::Observer implementation. | 94 // content::DownloadManager::Observer implementation. |
| 94 void OnDownloadCreated(content::DownloadManager* manager, | 95 void OnDownloadCreated(content::DownloadManager* manager, |
| 95 content::DownloadItem* item) override; | 96 content::DownloadItem* item) override; |
| 96 void ManagerGoingDown(content::DownloadManager* manager) override; | 97 void ManagerGoingDown(content::DownloadManager* manager) override; |
| 97 | 98 |
| 98 // content::DownloadItem::Observer implementation. | 99 // content::DownloadItem::Observer implementation. |
| 99 void OnDownloadUpdated(content::DownloadItem* item) override; | 100 void OnDownloadUpdated(content::DownloadItem* item) override; |
| 100 void OnDownloadOpened(content::DownloadItem* item) override; | 101 void OnDownloadOpened(content::DownloadItem* item) override; |
| 101 void OnDownloadRemoved(content::DownloadItem* item) override; | 102 void OnDownloadRemoved(content::DownloadItem* item) override; |
| 102 void OnDownloadDestroyed(content::DownloadItem* item) override; | 103 void OnDownloadDestroyed(content::DownloadItem* item) override; |
| 103 | 104 |
| 105 // DownloadHistory::Observer implementation. | |
| 106 void OnHistoryQueryComplete() override; | |
| 107 void OnDownloadHistoryDestroyed() override; | |
| 108 | |
| 104 // Updates the |category_status_| of the |provided_category_| and notifies the | 109 // Updates the |category_status_| of the |provided_category_| and notifies the |
| 105 // |observer_|, if necessary. | 110 // |observer_|, if necessary. |
| 106 void NotifyStatusChanged(ntp_snippets::CategoryStatus new_status); | 111 void NotifyStatusChanged(ntp_snippets::CategoryStatus new_status); |
| 107 | 112 |
| 108 // Requests all offline pages and after asynchronously obtaining the result, | 113 // Requests all offline pages and after asynchronously obtaining the result, |
| 109 // prunes dismissed IDs and caches some most recent items. If |notify| is | 114 // prunes dismissed IDs and caches some most recent items. If |notify| is |
| 110 // true, notifies |ContentSuggestionsProvider::Observer| about them. | 115 // true, notifies |ContentSuggestionsProvider::Observer| about them. |
| 111 void AsynchronouslyFetchOfflinePagesDownloads(bool notify); | 116 void AsynchronouslyFetchOfflinePagesDownloads(bool notify); |
| 112 | 117 |
| 113 // Retrieves all asset downloads, prunes dismissed IDs and caches some most | 118 // Retrieves all asset downloads, prunes dismissed IDs and caches some most |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 bool notify, | 169 bool notify, |
| 165 const std::vector<offline_pages::OfflinePageItem>& | 170 const std::vector<offline_pages::OfflinePageItem>& |
| 166 all_download_offline_pages); | 171 all_download_offline_pages); |
| 167 | 172 |
| 168 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding | 173 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding |
| 169 // to the given |id_within_category| and clears it from the dismissed IDs | 174 // to the given |id_within_category| and clears it from the dismissed IDs |
| 170 // list, if necessary. | 175 // list, if necessary. |
| 171 void InvalidateSuggestion(const std::string& id_within_category); | 176 void InvalidateSuggestion(const std::string& id_within_category); |
| 172 | 177 |
| 173 // Reads dismissed IDs related to asset downloads from prefs. | 178 // Reads dismissed IDs related to asset downloads from prefs. |
| 174 std::vector<std::string> ReadAssetDismissedIDsFromPrefs() const; | 179 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const; |
| 175 | 180 |
| 176 // Writes |dismissed_ids| into prefs for asset downloads. | 181 // Writes |dismissed_ids| into prefs for asset downloads. |
| 177 void StoreAssetDismissedIDsToPrefs( | 182 void StoreAssetDismissedIDsToPrefs( |
| 178 const std::vector<std::string>& dismissed_ids); | 183 const std::set<std::string>& dismissed_ids); |
| 179 | 184 |
| 180 // Reads dismissed IDs related to offline page downloads from prefs. | 185 // Reads dismissed IDs related to offline page downloads from prefs. |
| 181 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; | 186 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; |
| 182 | 187 |
| 183 // Writes |dismissed_ids| into prefs for offline page downloads. | 188 // Writes |dismissed_ids| into prefs for offline page downloads. |
| 184 void StoreOfflinePageDismissedIDsToPrefs( | 189 void StoreOfflinePageDismissedIDsToPrefs( |
| 185 const std::set<std::string>& dismissed_ids); | 190 const std::set<std::string>& dismissed_ids); |
| 186 | 191 |
| 187 // Adds a suggestion ID to the dismissed list in prefs, if it is not there. | 192 // Reads from prefs dismissed IDs related to either offline page or asset |
| 188 // Works for both Offline Page and Asset downloads. | 193 // downloads (given by |for_offline_page_downloads|). |
| 189 void AddToDismissedStorageIfNeeded( | 194 std::set<std::string> ReadDismissedIDsFromPrefs( |
| 190 const ntp_snippets::ContentSuggestion::ID& suggestion_id); | 195 bool for_offline_page_downloads) const; |
| 191 | 196 |
| 192 // Removes a suggestion ID from the dismissed list in prefs, if it is there. | 197 // Writes |dismissed_ids| into prefs for either offline page or asset |
| 193 // Works for both Offline Page and Asset downloads. | 198 // downloads (given by |for_offline_page_downloads|). |
| 194 void RemoveFromDismissedStorageIfNeeded( | 199 void StoreDismissedIDsToPrefs(bool for_offline_page_downloads, |
| 195 const ntp_snippets::ContentSuggestion::ID& suggestion_id); | 200 const std::set<std::string>& dismissed_ids); |
| 196 | 201 |
| 197 void UnregisterDownloadItemObservers(); | 202 void UnregisterDownloadItemObservers(); |
| 198 | 203 |
| 199 ntp_snippets::CategoryStatus category_status_; | 204 ntp_snippets::CategoryStatus category_status_; |
| 200 const ntp_snippets::Category provided_category_; | 205 const ntp_snippets::Category provided_category_; |
| 201 offline_pages::OfflinePageModel* offline_page_model_; | 206 offline_pages::OfflinePageModel* offline_page_model_; |
| 202 content::DownloadManager* download_manager_; | 207 content::DownloadManager* download_manager_; |
| 208 DownloadHistory* download_history_; | |
| 203 | 209 |
| 204 PrefService* pref_service_; | 210 PrefService* pref_service_; |
| 205 | 211 |
| 206 // Cached offline page downloads. If there are not enough asset downloads, all | 212 // Cached offline page downloads. If there are not enough asset downloads, all |
| 207 // of these could be shown (they are the most recently visited, not dismissed | 213 // of these could be shown (they are the most recently visited, not dismissed |
| 208 // and not invalidated). Order is undefined. If the model has less than | 214 // and not invalidated). Order is undefined. If the model has less than |
| 209 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the | 215 // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the |
| 210 // criteria above are cached, otherwise only |kMaxSuggestionsCount|. | 216 // criteria above are cached, otherwise only |kMaxSuggestionsCount|. |
| 211 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; | 217 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; |
| 212 // Cached asset downloads. If there are not enough offline page downloads, all | 218 // Cached asset downloads. If there are not enough offline page downloads, all |
| 213 // of these could be shown (they are the most recently downloaded, not | 219 // of these could be shown (they are the most recently downloaded, not |
| 214 // dismissed and not invalidated). Order is undefined. If the model has less | 220 // dismissed and not invalidated). Order is undefined. If the model has less |
| 215 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy | 221 // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy |
| 216 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|. | 222 // the criteria above are cached, otherwise only |kMaxSuggestionsCount|. |
| 217 std::vector<const content::DownloadItem*> cached_asset_downloads_; | 223 std::vector<const content::DownloadItem*> cached_asset_downloads_; |
| 218 | 224 |
| 219 // Whether the Download Manager UI is enabled, in which case the More button | 225 // Whether the Download Manager UI is enabled, in which case the More button |
| 220 // for the Downloads section can redirect there. | 226 // for the Downloads section can redirect there. |
| 221 const bool download_manager_ui_enabled_; | 227 const bool download_manager_ui_enabled_; |
| 222 | 228 |
| 229 bool is_download_manager_loaded_; | |
| 230 | |
| 223 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; | 231 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; |
| 224 | 232 |
| 225 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); | 233 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); |
| 226 }; | 234 }; |
| 227 | 235 |
| 228 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ | 236 #endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |
| OLD | NEW |