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

Side by Side Diff: components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.h

Issue 2684973014: Only show Last N Pages in the UI when the corresponding tab is visible. (Closed)
Patch Set: Add unit tests Created 3 years, 10 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 unified diff | Download patch
OLDNEW
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(
33 ContentSuggestionsProvider::Observer* observer, 35 ContentSuggestionsProvider::Observer* observer,
34 offline_pages::OfflinePageModel* offline_page_model, 36 offline_pages::OfflinePageModel* offline_page_model,
37 offline_pages::RequestCoordinator* request_coordinator,
35 PrefService* pref_service); 38 PrefService* pref_service);
36 ~RecentTabSuggestionsProvider() override; 39 ~RecentTabSuggestionsProvider() override;
37 40
38 // ContentSuggestionsProvider implementation. 41 // ContentSuggestionsProvider implementation.
39 CategoryStatus GetCategoryStatus(Category category) override; 42 CategoryStatus GetCategoryStatus(Category category) override;
40 CategoryInfo GetCategoryInfo(Category category) override; 43 CategoryInfo GetCategoryInfo(Category category) override;
41 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; 44 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override;
42 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, 45 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id,
43 const ImageFetchedCallback& callback) override; 46 const ImageFetchedCallback& callback) override;
44 void Fetch(const Category& category, 47 void Fetch(const Category& category,
45 const std::set<std::string>& known_suggestion_ids, 48 const std::set<std::string>& known_suggestion_ids,
46 const FetchDoneCallback& callback) override; 49 const FetchDoneCallback& callback) override;
47 void ClearHistory( 50 void ClearHistory(
48 base::Time begin, 51 base::Time begin,
49 base::Time end, 52 base::Time end,
50 const base::Callback<bool(const GURL& url)>& filter) override; 53 const base::Callback<bool(const GURL& url)>& filter) override;
51 void ClearCachedSuggestions(Category category) override; 54 void ClearCachedSuggestions(Category category) override;
52 void GetDismissedSuggestionsForDebugging( 55 void GetDismissedSuggestionsForDebugging(
53 Category category, 56 Category category,
54 const DismissedSuggestionsCallback& callback) override; 57 const DismissedSuggestionsCallback& callback) override;
55 void ClearDismissedSuggestionsForDebugging(Category category) override; 58 void ClearDismissedSuggestionsForDebugging(Category category) override;
56 59
57 static void RegisterProfilePrefs(PrefRegistrySimple* registry); 60 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
58 61
59 private: 62 private:
60 friend class RecentTabSuggestionsProviderTest; 63 friend class RecentTabSuggestionsProviderTest;
61 64
62 void GetPagesMatchingQueryCallbackForGetDismissedSuggestions( 65 // DownloadUIAdapter::Observer implementation.
63 const DismissedSuggestionsCallback& callback, 66 void ItemsLoaded() override;
64 const std::vector<offline_pages::OfflinePageItem>& offline_pages) const; 67 void ItemAdded(const offline_pages::DownloadUIItem& ui_item) override;
65 68 void ItemUpdated(const offline_pages::DownloadUIItem& ui_item) override;
66 // OfflinePageModel::Observer implementation. 69 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 70
74 void GetPagesMatchingQueryCallbackForFetchRecentTabs( 71 void GetPagesMatchingQueryCallbackForFetchRecentTabs(
75 const std::vector<offline_pages::OfflinePageItem>& offline_pages); 72 const std::vector<offline_pages::OfflinePageItem>& offline_pages);
76 73
77 // Updates the |category_status_| of the |provided_category_| and notifies the 74 // Updates the |category_status_| of the |provided_category_| and notifies the
78 // |observer_|, if necessary. 75 // |observer_|, if necessary.
79 void NotifyStatusChanged(CategoryStatus new_status); 76 void NotifyStatusChanged(CategoryStatus new_status);
80 77
81 // Manually requests all offline pages and updates the suggestions. 78 // Manually requests all offline pages and updates the suggestions.
vitaliii 2017/02/15 10:12:15 s/offline pages/ui utems
dewittj 2017/02/15 19:49:45 Done.
82 void FetchRecentTabs(); 79 void FetchRecentTabs();
83 80
84 // Converts an OfflinePageItem to a ContentSuggestion for the 81 // Converts an OfflinePageItem to a ContentSuggestion for the
vitaliii 2017/02/15 10:12:15 s/OfflinePageItem/DownloadUIItem
dewittj 2017/02/15 19:49:45 Done.
85 // |provided_category_|. 82 // |provided_category_|.
86 ContentSuggestion ConvertOfflinePage( 83 ContentSuggestion ConvertUIItem(
87 const offline_pages::OfflinePageItem& offline_page) const; 84 const offline_pages::DownloadUIItem& ui_item) const;
88 85
89 // Removes duplicates for the same URL leaving only the most recently created 86 // Removes duplicates for the same URL leaving only the most recently created
90 // items, returns at most |GetMaxSuggestionsCount()| ContentSuggestions 87 // items, returns at most |GetMaxSuggestionsCount()| ContentSuggestions
91 // corresponding to the remaining items, sorted by creation time (newer 88 // corresponding to the remaining items, sorted by creation time (newer
92 // first). 89 // first).
93 std::vector<ContentSuggestion> GetMostRecentlyCreatedWithoutDuplicates( 90 std::vector<ContentSuggestion> GetMostRecentlyCreatedWithoutDuplicates(
94 std::vector<const offline_pages::OfflinePageItem*> offline_page_items) 91 std::vector<const offline_pages::DownloadUIItem*> ui_items) const;
95 const;
96 92
97 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding 93 // Fires the |OnSuggestionInvalidated| event for the suggestion corresponding
98 // to the given |offline_id| and deletes it from the dismissed IDs list, if 94 // to the given |offline_id| and deletes it from the dismissed IDs list, if
99 // necessary. 95 // necessary.
100 void InvalidateSuggestion(int64_t offline_id); 96 void InvalidateSuggestion(const std::string& ui_item_guid);
101 97
102 // Reads dismissed IDs from Prefs. 98 // Reads dismissed IDs from Prefs.
103 std::set<std::string> ReadDismissedIDsFromPrefs() const; 99 std::set<std::string> ReadDismissedIDsFromPrefs() const;
104 100
105 // Writes |dismissed_ids| into Prefs. 101 // Writes |dismissed_ids| into Prefs.
106 void StoreDismissedIDsToPrefs(const std::set<std::string>& dismissed_ids); 102 void StoreDismissedIDsToPrefs(const std::set<std::string>& dismissed_ids);
107 103
108 CategoryStatus category_status_; 104 CategoryStatus category_status_;
109 const Category provided_category_; 105 const Category provided_category_;
110 offline_pages::OfflinePageModel* offline_page_model_; 106 offline_pages::DownloadUIAdapter* recent_tabs_ui_adapter_;
111 107
112 PrefService* pref_service_; 108 PrefService* pref_service_;
113 109
114 base::WeakPtrFactory<RecentTabSuggestionsProvider> weak_ptr_factory_; 110 base::WeakPtrFactory<RecentTabSuggestionsProvider> weak_ptr_factory_;
115 111
116 DISALLOW_COPY_AND_ASSIGN(RecentTabSuggestionsProvider); 112 DISALLOW_COPY_AND_ASSIGN(RecentTabSuggestionsProvider);
117 }; 113 };
118 114
119 } // namespace ntp_snippets 115 } // namespace ntp_snippets
120 116
121 #endif // COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER _H_ 117 #endif // COMPONENTS_NTP_SNIPPETS_OFFLINE_PAGES_RECENT_TAB_SUGGESTIONS_PROVIDER _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698