Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Unified Diff: components/ntp_snippets/downloads/download_suggestions_provider.h

Issue 2360263002: [NTPSnippets] Show all downloads on the NTP (3/3): Downloads provider. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/downloads/download_suggestions_provider.h
diff --git a/components/ntp_snippets/downloads/download_suggestions_provider.h b/components/ntp_snippets/downloads/download_suggestions_provider.h
new file mode 100644
index 0000000000000000000000000000000000000000..4292db7dee10e5a6349dc4bf84c7e47df8933f76
--- /dev/null
+++ b/components/ntp_snippets/downloads/download_suggestions_provider.h
@@ -0,0 +1,159 @@
+// 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 COMPONENTS_NTP_SNIPPETS_DOWNLOADS_DOWNLOAD_SUGGESTIONS_PROVIDER_H_
+#define COMPONENTS_NTP_SNIPPETS_DOWNLOADS_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 "base/time/time.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 ntp_snippets {
+
+// 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 other downloads (e.g. images,
+// music, books) are referred to as asset downloads.
+class DownloadSuggestionsProvider : public ContentSuggestionsProvider,
+ public OfflinePageProxy::Observer,
+ public AllDownloadItemNotifier::Observer {
+ public:
+ DownloadSuggestionsProvider(
+ ContentSuggestionsProvider::Observer* observer,
+ CategoryFactory* category_factory,
+ const scoped_refptr<OfflinePageProxy>& offline_page_proxy,
+ content::DownloadManager* download_manager,
+ PrefService* pref_service,
+ bool download_manager_ui_enabled);
+ ~DownloadSuggestionsProvider() override;
+
+ // ContentSuggestionsProvider implementation.
+ CategoryStatus GetCategoryStatus(Category category) override;
+ CategoryInfo GetCategoryInfo(Category category) override;
+ void DismissSuggestion(const std::string& suggestion_id) override;
+ void FetchSuggestionImage(const std::string& 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(Category category) override;
+ void GetDismissedSuggestionsForDebugging(
+ Category category,
+ const DismissedSuggestionsCallback& callback) override;
+ void ClearDismissedSuggestionsForDebugging(Category category) override;
+
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry);
+
+ private:
+ // 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(CategoryStatus new_status);
+
+ // Manually requests all offline pages and after asynchronously obtaining the
+ // result caches some most recent ones and updates the suggestions through
+ // |SubmitContentSuggestions|.
+ void FetchOfflinePagesDownloadsAndSubmitSuggestions();
+
+ // Manually syncronously retrieves all asset downloads and caches them.
+ void FetchAssetsDownloads();
+
+ // Manually downloads both offline page and asset downloads, caches them and
+ // updates the suggestions through |SubmitContentSuggestions|.
+ void FetchAllDownloadsAndSubmitSuggestions();
+
+ // Takes |kMaxSuggestionsCount| cached suggestions and notifies the service
+ // about them.
+ void SubmitContentSuggestions();
+
+ // Converts an OfflinePageItem to a ContentSuggestion for the
+ // |provided_category_|.
+ ContentSuggestion ConvertOfflinePage(
+ const offline_pages::OfflinePageItem& offline_page) const;
+
+ // Converts DownloadItem to a ContentSuggestion for the |provided_category_|.
+ ContentSuggestion ConvertDownloadItem(
+ const content::DownloadItem* download_item) const;
+
+ // 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);
+
+ CategoryStatus category_status_;
+ const Category provided_category_;
+ scoped_refptr<OfflinePageProxy> offline_page_proxy_;
+ AllDownloadItemNotifier download_manager_notifier_;
+
+ PrefService* pref_service_;
+
+ // Cached offline page downloads. Any of them could be shown. Order is
+ // undefined.
+ std::vector<offline_pages::OfflinePageItem> cached_offline_page_downloads_;
+ // Cached asset downloads. Any of them could be shown. Order is undefined.
+ std::vector<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.
+ bool download_manager_ui_enabled_;
+
+ base::WeakPtrFactory<DownloadSuggestionsProvider> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProvider);
+};
+
+} // namespace ntp_snippets
+
+#endif // COMPONENTS_NTP_SNIPPETS_RECENT_TABS_RECENT_TAB_SUGGESTIONS_PROVIDER_H_

Powered by Google App Engine
This is Rietveld 408576698