Index: chrome/browser/ntp_snippets/download_suggestions_provider.h |
diff --git a/chrome/browser/ntp_snippets/download_suggestions_provider.h b/chrome/browser/ntp_snippets/download_suggestions_provider.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..43f10c9a4eff30738c12e02e9442abf349d6315f |
--- /dev/null |
+++ b/chrome/browser/ntp_snippets/download_suggestions_provider.h |
@@ -0,0 +1,220 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |
+#define CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |
+ |
+#include <set> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/callback_forward.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/memory/weak_ptr.h" |
+#include "chrome/browser/download/all_download_item_notifier.h" |
+#include "components/ntp_snippets/category.h" |
+#include "components/ntp_snippets/category_factory.h" |
+#include "components/ntp_snippets/category_status.h" |
+#include "components/ntp_snippets/content_suggestion.h" |
+#include "components/ntp_snippets/content_suggestions_provider.h" |
+#include "components/ntp_snippets/offline_pages/offline_page_proxy.h" |
+ |
+class PrefRegistrySimple; |
+class PrefService; |
+ |
+namespace gfx { |
+class Image; |
+} |
+ |
+namespace offline_pages { |
+struct OfflinePageItem; |
+} |
+ |
+// Provides download content suggestions from the offline pages model (obtaining |
+// the data through OfflinePageProxy) and the download manager (obtaining the |
+// data through AllDownloadItemNotifier). Offline pages related downloads are |
+// referred to as offline page downloads, while the remaining downloads (e.g. |
+// images, music, books) are called asset downloads. |
+class DownloadSuggestionsProvider |
+ : public ntp_snippets::ContentSuggestionsProvider, |
+ public ntp_snippets::OfflinePageProxy::Observer, |
+ public AllDownloadItemNotifier::Observer { |
+ public: |
+ DownloadSuggestionsProvider( |
+ ContentSuggestionsProvider::Observer* observer, |
+ ntp_snippets::CategoryFactory* category_factory, |
+ const scoped_refptr<ntp_snippets::OfflinePageProxy>& offline_page_proxy, |
+ content::DownloadManager* download_manager, |
+ PrefService* pref_service, |
+ bool download_manager_ui_enabled); |
+ ~DownloadSuggestionsProvider() override; |
+ |
+ // ContentSuggestionsProvider implementation. |
+ ntp_snippets::CategoryStatus GetCategoryStatus( |
+ ntp_snippets::Category category) override; |
+ ntp_snippets::CategoryInfo GetCategoryInfo( |
+ ntp_snippets::Category category) override; |
+ void DismissSuggestion( |
+ const ntp_snippets::ContentSuggestion::ID& suggestion_id) override; |
+ void FetchSuggestionImage( |
+ const ntp_snippets::ContentSuggestion::ID& suggestion_id, |
+ const ImageFetchedCallback& callback) override; |
+ void ClearHistory( |
+ base::Time begin, |
+ base::Time end, |
+ const base::Callback<bool(const GURL& url)>& filter) override; |
+ void ClearCachedSuggestions(ntp_snippets::Category category) override; |
+ void GetDismissedSuggestionsForDebugging( |
+ ntp_snippets::Category category, |
+ const DismissedSuggestionsCallback& callback) override; |
+ void ClearDismissedSuggestionsForDebugging( |
+ ntp_snippets::Category category) override; |
+ |
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
+ |
+ private: |
+ friend class DownloadSuggestionsProviderTest; |
+ |
+ void GetAllPagesCallbackForGetDismissedSuggestions( |
+ const DismissedSuggestionsCallback& callback, |
+ const std::vector<offline_pages::OfflinePageItem>& offline_pages) const; |
+ |
+ // OfflinePageProxy::Observer implementation. |
+ void OfflinePageModelChanged( |
+ const std::vector<offline_pages::OfflinePageItem>& offline_pages) |
+ override; |
+ void OfflinePageDeleted(int64_t offline_id, |
+ const offline_pages::ClientId& client_id) override; |
+ |
+ // AllDownloadItemNotifier::Observer implementation. |
+ void OnDownloadCreated(content::DownloadManager* manager, |
+ content::DownloadItem* item) override; |
+ void OnDownloadUpdated(content::DownloadManager* manager, |
+ content::DownloadItem* item) override; |
+ void OnDownloadOpened(content::DownloadManager* manager, |
+ content::DownloadItem* item) override; |
+ void OnDownloadRemoved(content::DownloadManager* manager, |
+ content::DownloadItem* item) override; |
+ |
+ // Updates the |category_status_| of the |provided_category_| and notifies the |
+ // |observer_|, if necessary. |
+ void NotifyStatusChanged(ntp_snippets::CategoryStatus new_status); |
+ |
+ // Requests all offline pages and after asynchronously obtaining the result, |
+ // prunes dismissed IDs and caches some most recent items, but does not notify |
+ // |ContentSuggestionsProvider::Observer| about them. |
+ void AsyncronouslyFetchOfflinePagesDownloads(); |
Marc Treib
2016/10/13 12:11:26
s/Asyncronously/Asynchronously/
(missing an "h")
vitaliii
2016/10/15 18:36:30
Done.
|
+ |
+ // Like |FetchOfflinePagesDownloads| above, but with |
+ // |SubmitContentSuggestions| afterwards. |
+ void AsyncronouslyFetchOfflinePagesDownloadsAndSubmitSuggestions(); |
+ |
+ // Retrieves all asset downloads, prunes dismissed IDs and caches some most |
+ // recent items, but does not notify |ContentSuggestionsProvider::Observer| |
+ // about them. |
+ 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
|
+ |
+ // Retrieves both offline page and asset downloads, updates the internal cache |
+ // and notifies |ContentSuggestionsProvider::Observer|. |
+ void AsyncronouslyFetchAllDownloadsAndSubmitSuggestions(); |
+ |
+ // Takes |kMaxSuggestionsCount| the most recent cached suggestions and |
+ // notifies |ContentSuggestionsProvider::Observer| about them. |
+ void SubmitContentSuggestions(); |
+ |
+ // Converts an OfflinePageItem to a ContentSuggestion for the |
+ // |provided_category_|. |
+ ntp_snippets::ContentSuggestion ConvertOfflinePage( |
+ const offline_pages::OfflinePageItem& offline_page) const; |
+ |
+ // Converts DownloadItem to a ContentSuggestion for the |provided_category_|. |
+ ntp_snippets::ContentSuggestion ConvertDownloadItem( |
+ const content::DownloadItem& download_item) const; |
+ |
+ // Adds |download_item| to internal asset download cache if all of the |
+ // following holds: |
+ // - the download is completed; |
+ // - its suggestion has not been dismissed; |
+ // - there are less than |kMaxSuggestionsCount| items cached or the oldest |
+ // cached item is older than the current item (the oldest item is removed |
+ // then); |
+ // - the item is not present in the cache yet. |
+ // Returns |true| if the item has been added. |
+ bool CacheAssetDownloadIfNeeded(const content::DownloadItem* item); |
+ |
+ // Removes item corresponding to |suggestion_id| either from offline pages or |
+ // asset download cache (depends on the |suggestion_id|). Returns |false| if |
+ // there is no corresponding item in the cache and so nothing was removed. |
+ bool RemoveSuggestionFromCacheIfPresent( |
+ const ntp_snippets::ContentSuggestion::ID& suggestion_id); |
+ |
+ // Removes item corresponding to |suggestion_id| from cache and fetches all |
+ // corresponding downloads to update the cache if there are any not in cache. |
+ void RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded( |
+ const ntp_snippets::ContentSuggestion::ID& suggestion_id); |
+ |
+ // Processes a list of all offline pages available at the moment pruning |
+ // dismissed IDs and updating internal cache. |
+ 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.
|
+ const std::vector<offline_pages::OfflinePageItem>& all_offline_pages); |
+ |
+ // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding |
+ // to the given |id_in_category| and clears it from the dismissed IDs list, if |
+ // necessary. |
+ void InvalidateSuggestion(const std::string& id_in_category); |
+ |
+ // Reads dismissed IDs related to asset downloads from prefs. |
+ std::set<std::string> ReadAssetDismissedIDsFromPrefs() const; |
+ |
+ // Writes |dismissed_ids| into prefs for asset downloads. |
+ void StoreAssetDismissedIDsToPrefs( |
+ const std::set<std::string>& dismissed_ids); |
+ |
+ // Reads dismissed IDs related to offline page downloads from prefs. |
+ std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const; |
+ |
+ // Writes |dismissed_ids| into prefs for offline page downloads. |
+ void StoreOfflinePageDismissedIDsToPrefs( |
+ const std::set<std::string>& dismissed_ids); |
+ |
+ // Reads from prefs dismissed IDs related to either offline page or asset |
+ // downloads (given by |for_offline_page_downloads|). |
+ std::set<std::string> ReadDismissedIDsFromPrefs( |
+ bool for_offline_page_downloads) const; |
+ |
+ // Writes |dismissed_ids| into prefs for either offline page or asset |
+ // downloads (given by |for_offline_page_downloads|). |
+ void StoreDismissedIDsToPrefs(bool for_offline_page_downloads, |
+ const std::set<std::string>& dismissed_ids); |
+ |
+ ntp_snippets::CategoryStatus category_status_; |
+ const ntp_snippets::Category provided_category_; |
+ scoped_refptr<ntp_snippets::OfflinePageProxy> offline_page_proxy_; |
+ AllDownloadItemNotifier download_manager_notifier_; |
+ |
+ PrefService* pref_service_; |
+ |
+ // Cached offline page downloads. If there are not enough asset downloads, all |
+ // of these could be shown (they are the most recently visited, not dismissed |
+ // and not invalidated). Order is undefined. If the model has less than |
+ // |kMaxSuggestionsCount| offline pages, then all of them which satisfy the |
+ // criteria above are cached, otherwise only |kMaxSuggestionsCount|. |
+ std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_; |
+ // Cached asset downloads. If there are not enough offline page downloads, all |
+ // of these could be shown (they are the most recently downloaded, not |
+ // dismissed and not invalidated). Order is undefined. If the model has less |
+ // than |kMaxSuggestionsCount| asset downloads, then all of them which satisfy |
+ // the criteria above are cached, otherwise only |kMaxSuggestionsCount|. |
+ std::vector<const content::DownloadItem*> cached_asset_downloads_; |
+ |
+ // Whether the Download Manager UI is enabled, in which case the More button |
+ // for the Downloads section can redirect there. |
+ const bool download_manager_ui_enabled_; |
+ |
+ base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider); |
+}; |
+ |
+#endif // CHROME_BROWSER_NTP_SNIPPETS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_ |