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 COMPONENTS_NTP_SNIPPETS_DOWNLOADS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_DOWNLOADS_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 "base/time/time.h" |
| 16 #include "chrome/browser/download/all_download_item_notifier.h" |
| 17 #include "components/ntp_snippets/category.h" |
| 18 #include "components/ntp_snippets/category_factory.h" |
| 19 #include "components/ntp_snippets/category_status.h" |
| 20 #include "components/ntp_snippets/content_suggestion.h" |
| 21 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 22 #include "components/ntp_snippets/offline_pages/offline_page_proxy.h" |
| 23 |
| 24 class PrefRegistrySimple; |
| 25 class PrefService; |
| 26 |
| 27 namespace gfx { |
| 28 class Image; |
| 29 } |
| 30 |
| 31 namespace ntp_snippets { |
| 32 |
| 33 // Provides download content suggestions from the offline pages model (obtaining |
| 34 // the data through OfflinePageProxy) and the download manager (obtaining the |
| 35 // data through AllDownloadItemNotifier). Offline pages related downloads are |
| 36 // referred to as offline page downloads, while other downloads (e.g. images, |
| 37 // music, books) are referred to as asset downloads. |
| 38 class DownloadSuggestionsProvider : public ContentSuggestionsProvider, |
| 39 public OfflinePageProxy::Observer, |
| 40 public AllDownloadItemNotifier::Observer { |
| 41 public: |
| 42 DownloadSuggestionsProvider( |
| 43 ContentSuggestionsProvider::Observer* observer, |
| 44 CategoryFactory* category_factory, |
| 45 const scoped_refptr<OfflinePageProxy>& offline_page_proxy, |
| 46 content::DownloadManager* download_manager, |
| 47 PrefService* pref_service, |
| 48 bool download_manager_ui_enabled); |
| 49 ~DownloadSuggestionsProvider() override; |
| 50 |
| 51 // ContentSuggestionsProvider implementation. |
| 52 CategoryStatus GetCategoryStatus(Category category) override; |
| 53 CategoryInfo GetCategoryInfo(Category category) override; |
| 54 void DismissSuggestion(const std::string& suggestion_id) override; |
| 55 void FetchSuggestionImage(const std::string& suggestion_id, |
| 56 const ImageFetchedCallback& callback) override; |
| 57 void ClearHistory( |
| 58 base::Time begin, |
| 59 base::Time end, |
| 60 const base::Callback<bool(const GURL& url)>& filter) override; |
| 61 void ClearCachedSuggestions(Category category) override; |
| 62 void GetDismissedSuggestionsForDebugging( |
| 63 Category category, |
| 64 const DismissedSuggestionsCallback& callback) override; |
| 65 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 66 |
| 67 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 68 |
| 69 private: |
| 70 // OfflinePageProxy::Observer implementation. |
| 71 void OfflinePageModelChanged( |
| 72 const std::vector<offline_pages::OfflinePageItem>& offline_pages) |
| 73 override; |
| 74 void OfflinePageDeleted(int64_t offline_id, |
| 75 const offline_pages::ClientId& client_id) override; |
| 76 |
| 77 // AllDownloadItemNotifier::Observer implementation. |
| 78 void OnDownloadCreated(content::DownloadManager* manager, |
| 79 content::DownloadItem* item) override; |
| 80 void OnDownloadUpdated(content::DownloadManager* manager, |
| 81 content::DownloadItem* item) override; |
| 82 void OnDownloadOpened(content::DownloadManager* manager, |
| 83 content::DownloadItem* item) override; |
| 84 void OnDownloadRemoved(content::DownloadManager* manager, |
| 85 content::DownloadItem* item) override; |
| 86 |
| 87 // Updates the |category_status_| of the |provided_category_| and notifies the |
| 88 // |observer_|, if necessary. |
| 89 void NotifyStatusChanged(CategoryStatus new_status); |
| 90 |
| 91 // Manually requests all offline pages and after asynchronously obtaining the |
| 92 // result caches some most recent ones and updates the suggestions through |
| 93 // |SubmitContentSuggestions|. |
| 94 void FetchOfflinePagesDownloadsAndSubmitSuggestions(); |
| 95 |
| 96 // Manually syncronously retrieves all asset downloads and caches them. |
| 97 void FetchAssetsDownloads(); |
| 98 |
| 99 // Manually downloads both offline page and asset downloads, caches them and |
| 100 // updates the suggestions through |SubmitContentSuggestions|. |
| 101 void FetchAllDownloadsAndSubmitSuggestions(); |
| 102 |
| 103 // Takes |kMaxSuggestionsCount| cached suggestions and notifies the service |
| 104 // about them. |
| 105 void SubmitContentSuggestions(); |
| 106 |
| 107 // Converts an OfflinePageItem to a ContentSuggestion for the |
| 108 // |provided_category_|. |
| 109 ContentSuggestion ConvertOfflinePage( |
| 110 const offline_pages::OfflinePageItem& offline_page) const; |
| 111 |
| 112 // Converts DownloadItem to a ContentSuggestion for the |provided_category_|. |
| 113 ContentSuggestion ConvertDownloadItem( |
| 114 const content::DownloadItem* download_item) const; |
| 115 |
| 116 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding |
| 117 // to the given |id_in_category| and clears it from the dismissed IDs list, if |
| 118 // necessary. |
| 119 void InvalidateSuggestion(const std::string& id_in_category); |
| 120 |
| 121 // Reads dismissed IDs related to asset downloads from Prefs. |
| 122 std::set<std::string> ReadAssetDismissedIDsFromPrefs() const; |
| 123 |
| 124 // Writes |dismissed_ids| into Prefs for asset downloads. |
| 125 void StoreAssetDismissedIDsToPrefs( |
| 126 const std::set<std::string>& dismissed_ids); |
| 127 |
| 128 // Reads dismissed IDs related to offline page downloads from Prefs. |
| 129 std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; |
| 130 |
| 131 // Writes |dismissed_ids| into Prefs for offline page downloads. |
| 132 void StoreOfflinePageDismissedIDsToPrefs( |
| 133 const std::set<std::string>& dismissed_ids); |
| 134 |
| 135 CategoryStatus category_status_; |
| 136 const Category provided_category_; |
| 137 scoped_refptr<OfflinePageProxy> offline_page_proxy_; |
| 138 AllDownloadItemNotifier download_manager_notifier_; |
| 139 |
| 140 PrefService* pref_service_; |
| 141 |
| 142 // Cached offline page downloads. Any of them could be shown. Order is |
| 143 // undefined. |
| 144 std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; |
| 145 // Cached asset downloads. Any of them could be shown. Order is undefined. |
| 146 std::vector<content::DownloadItem*> cached_asset_downloads_; |
| 147 |
| 148 // Whether the Download Manager UI is enabled, in which case the More button |
| 149 // for the Downloads section can redirect there. |
| 150 bool download_manager_ui_enabled_; |
| 151 |
| 152 base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; |
| 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); |
| 155 }; |
| 156 |
| 157 } // namespace ntp_snippets |
| 158 |
| 159 #endif // COMPONENTS_NTP_SNIPPETS_RECENT_TABS_RECENT_TAB_SUGGESTIONS_PROVIDER_H
_ |
OLD | NEW |