| 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 COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_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/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/ntp_snippets/category.h" | 16 #include "components/ntp_snippets/category.h" |
| 17 #include "components/ntp_snippets/category_status.h" | 17 #include "components/ntp_snippets/category_status.h" |
| 18 #include "components/ntp_snippets/content_suggestion.h" | 18 #include "components/ntp_snippets/content_suggestion.h" |
| 19 #include "components/ntp_snippets/content_suggestions_provider.h" | 19 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 20 #include "components/offline_pages/core/downloads/download_ui_adapter.h" |
| 21 #include "components/offline_pages/core/downloads/download_ui_item.h" |
| 20 #include "components/offline_pages/core/offline_page_model.h" | 22 #include "components/offline_pages/core/offline_page_model.h" |
| 21 | 23 |
| 22 class PrefRegistrySimple; | 24 class PrefRegistrySimple; |
| 23 class PrefService; | 25 class PrefService; |
| 24 | 26 |
| 25 namespace ntp_snippets { | 27 namespace ntp_snippets { |
| 26 | 28 |
| 27 // Provides recent tabs content suggestions from the offline pages model. | 29 // Provides recent tabs content suggestions from the offline pages model. |
| 28 class RecentTabSuggestionsProvider | 30 class RecentTabSuggestionsProvider |
| 29 : public ContentSuggestionsProvider, | 31 : public ContentSuggestionsProvider, |
| 30 public offline_pages::OfflinePageModel::Observer { | 32 public offline_pages::DownloadUIAdapter::Observer { |
| 31 public: | 33 public: |
| 32 RecentTabSuggestionsProvider( | 34 RecentTabSuggestionsProvider(ContentSuggestionsProvider::Observer* observer, |
| 33 ContentSuggestionsProvider::Observer* observer, | 35 offline_pages::DownloadUIAdapter* ui_adapter, |
| 34 offline_pages::OfflinePageModel* offline_page_model, | 36 PrefService* pref_service); |
| 35 PrefService* pref_service); | |
| 36 ~RecentTabSuggestionsProvider() override; | 37 ~RecentTabSuggestionsProvider() override; |
| 37 | 38 |
| 38 // ContentSuggestionsProvider implementation. | 39 // ContentSuggestionsProvider implementation. |
| 39 CategoryStatus GetCategoryStatus(Category category) override; | 40 CategoryStatus GetCategoryStatus(Category category) override; |
| 40 CategoryInfo GetCategoryInfo(Category category) override; | 41 CategoryInfo GetCategoryInfo(Category category) override; |
| 41 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | 42 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
| 42 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | 43 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
| 43 const ImageFetchedCallback& callback) override; | 44 const ImageFetchedCallback& callback) override; |
| 44 void Fetch(const Category& category, | 45 void Fetch(const Category& category, |
| 45 const std::set<std::string>& known_suggestion_ids, | 46 const std::set<std::string>& known_suggestion_ids, |
| 46 const FetchDoneCallback& callback) override; | 47 const FetchDoneCallback& callback) override; |
| 47 void ClearHistory( | 48 void ClearHistory( |
| 48 base::Time begin, | 49 base::Time begin, |
| 49 base::Time end, | 50 base::Time end, |
| 50 const base::Callback<bool(const GURL& url)>& filter) override; | 51 const base::Callback<bool(const GURL& url)>& filter) override; |
| 51 void ClearCachedSuggestions(Category category) override; | 52 void ClearCachedSuggestions(Category category) override; |
| 52 void GetDismissedSuggestionsForDebugging( | 53 void GetDismissedSuggestionsForDebugging( |
| 53 Category category, | 54 Category category, |
| 54 const DismissedSuggestionsCallback& callback) override; | 55 const DismissedSuggestionsCallback& callback) override; |
| 55 void ClearDismissedSuggestionsForDebugging(Category category) override; | 56 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 56 | 57 |
| 57 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 58 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 58 | 59 |
| 59 private: | 60 private: |
| 60 friend class RecentTabSuggestionsProviderTest; | 61 friend class RecentTabSuggestionsProviderTestNoLoad; |
| 61 | 62 |
| 62 void GetPagesMatchingQueryCallbackForGetDismissedSuggestions( | 63 // DownloadUIAdapter::Observer implementation. |
| 63 const DismissedSuggestionsCallback& callback, | 64 void ItemsLoaded() override; |
| 64 const std::vector<offline_pages::OfflinePageItem>& offline_pages) const; | 65 void ItemAdded(const offline_pages::DownloadUIItem& ui_item) override; |
| 65 | 66 void ItemUpdated(const offline_pages::DownloadUIItem& ui_item) override; |
| 66 // OfflinePageModel::Observer implementation. | 67 void ItemDeleted(const std::string& ui_item_guid) override; |
| 67 void OfflinePageModelLoaded(offline_pages::OfflinePageModel* model) override; | |
| 68 void OfflinePageAdded( | |
| 69 offline_pages::OfflinePageModel* model, | |
| 70 const offline_pages::OfflinePageItem& added_page) override; | |
| 71 void OfflinePageDeleted(int64_t offline_id, | |
| 72 const offline_pages::ClientId& client_id) override; | |
| 73 | |
| 74 void GetPagesMatchingQueryCallbackForFetchRecentTabs( | |
| 75 const std::vector<offline_pages::OfflinePageItem>& offline_pages); | |
| 76 | 68 |
| 77 // Updates the |category_status_| of the |provided_category_| and notifies the | 69 // Updates the |category_status_| of the |provided_category_| and notifies the |
| 78 // |observer_|, if necessary. | 70 // |observer_|, if necessary. |
| 79 void NotifyStatusChanged(CategoryStatus new_status); | 71 void NotifyStatusChanged(CategoryStatus new_status); |
| 80 | 72 |
| 81 // Manually requests all offline pages and updates the suggestions. | 73 // Manually requests all Recent Tabs UI items and updates the suggestions. |
| 82 void FetchRecentTabs(); | 74 void FetchRecentTabs(); |
| 83 | 75 |
| 84 // Converts an OfflinePageItem to a ContentSuggestion for the | 76 // Converts an DownloadUIItem to a ContentSuggestion for the |
| 85 // |provided_category_|. | 77 // |provided_category_|. |
| 86 ContentSuggestion ConvertOfflinePage( | 78 ContentSuggestion ConvertUIItem( |
| 87 const offline_pages::OfflinePageItem& offline_page) const; | 79 const offline_pages::DownloadUIItem& ui_item) const; |
| 88 | 80 |
| 89 // Removes duplicates for the same URL leaving only the most recently created | 81 // Removes duplicates for the same URL leaving only the most recently created |
| 90 // items, returns at most |GetMaxSuggestionsCount()| ContentSuggestions | 82 // items, returns at most |GetMaxSuggestionsCount()| ContentSuggestions |
| 91 // corresponding to the remaining items, sorted by creation time (newer | 83 // corresponding to the remaining items, sorted by creation time (newer |
| 92 // first). | 84 // first). |
| 93 std::vector<ContentSuggestion> GetMostRecentlyCreatedWithoutDuplicates( | 85 std::vector<ContentSuggestion> GetMostRecentlyCreatedWithoutDuplicates( |
| 94 std::vector<const offline_pages::OfflinePageItem*> offline_page_items) | 86 std::vector<const offline_pages::DownloadUIItem*> ui_items) const; |
| 95 const; | |
| 96 | 87 |
| 97 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding | 88 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding |
| 98 // to the given |offline_id| and deletes it from the dismissed IDs list, if | 89 // to the given |offline_id| and deletes it from the dismissed IDs list, if |
| 99 // necessary. | 90 // necessary. |
| 100 void InvalidateSuggestion(int64_t offline_id); | 91 void InvalidateSuggestion(const std::string& ui_item_guid); |
| 101 | 92 |
| 102 // Reads dismissed IDs from Prefs. | 93 // Reads dismissed IDs from Prefs. |
| 103 std::set<std::string> ReadDismissedIDsFromPrefs() const; | 94 std::set<std::string> ReadDismissedIDsFromPrefs() const; |
| 104 | 95 |
| 105 // Writes |dismissed_ids| into Prefs. | 96 // Writes |dismissed_ids| into Prefs. |
| 106 void StoreDismissedIDsToPrefs(const std::set<std::string>& dismissed_ids); | 97 void StoreDismissedIDsToPrefs(const std::set<std::string>& dismissed_ids); |
| 107 | 98 |
| 108 CategoryStatus category_status_; | 99 CategoryStatus category_status_; |
| 109 const Category provided_category_; | 100 const Category provided_category_; |
| 110 offline_pages::OfflinePageModel* offline_page_model_; | 101 offline_pages::DownloadUIAdapter* recent_tabs_ui_adapter_; |
| 111 | 102 |
| 112 PrefService* pref_service_; | 103 PrefService* pref_service_; |
| 113 | 104 |
| 114 base::WeakPtrFactory<RecentTabSuggestionsProvider> weak_ptr_factory_; | 105 base::WeakPtrFactory<RecentTabSuggestionsProvider> weak_ptr_factory_; |
| 115 | 106 |
| 116 DISALLOW_COPY_AND_ASSIGN(RecentTabSuggestionsProvider); | 107 DISALLOW_COPY_AND_ASSIGN(RecentTabSuggestionsProvider); |
| 117 }; | 108 }; |
| 118 | 109 |
| 119 } // namespace ntp_snippets | 110 } // namespace ntp_snippets |
| 120 | 111 |
| 121 #endif // COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER
_H_ | 112 #endif // COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER
_H_ |
| OLD | NEW |