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 #include "components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.
h" | 5 #include "components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.
h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 offline_page_model_->AddObserver(this); | 67 offline_page_model_->AddObserver(this); |
68 FetchRecentTabs(); | 68 FetchRecentTabs(); |
69 } | 69 } |
70 | 70 |
71 RecentTabSuggestionsProvider::~RecentTabSuggestionsProvider() { | 71 RecentTabSuggestionsProvider::~RecentTabSuggestionsProvider() { |
72 offline_page_model_->RemoveObserver(this); | 72 offline_page_model_->RemoveObserver(this); |
73 } | 73 } |
74 | 74 |
75 CategoryStatus RecentTabSuggestionsProvider::GetCategoryStatus( | 75 CategoryStatus RecentTabSuggestionsProvider::GetCategoryStatus( |
76 Category category) { | 76 Category category) { |
77 if (category == provided_category_) | 77 if (category == provided_category_) { |
78 return category_status_; | 78 return category_status_; |
| 79 } |
79 NOTREACHED() << "Unknown category " << category.id(); | 80 NOTREACHED() << "Unknown category " << category.id(); |
80 return CategoryStatus::NOT_PROVIDED; | 81 return CategoryStatus::NOT_PROVIDED; |
81 } | 82 } |
82 | 83 |
83 CategoryInfo RecentTabSuggestionsProvider::GetCategoryInfo(Category category) { | 84 CategoryInfo RecentTabSuggestionsProvider::GetCategoryInfo(Category category) { |
84 DCHECK_EQ(provided_category_, category); | 85 DCHECK_EQ(provided_category_, category); |
85 return CategoryInfo( | 86 return CategoryInfo( |
86 l10n_util::GetStringUTF16(IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_HEADER), | 87 l10n_util::GetStringUTF16(IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_HEADER), |
87 ContentSuggestionsCardLayout::MINIMAL_CARD, | 88 ContentSuggestionsCardLayout::MINIMAL_CARD, |
88 /*has_more_action=*/false, | 89 /*has_more_action=*/false, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 //////////////////////////////////////////////////////////////////////////////// | 167 //////////////////////////////////////////////////////////////////////////////// |
167 // Private methods | 168 // Private methods |
168 | 169 |
169 void RecentTabSuggestionsProvider:: | 170 void RecentTabSuggestionsProvider:: |
170 GetPagesMatchingQueryCallbackForGetDismissedSuggestions( | 171 GetPagesMatchingQueryCallbackForGetDismissedSuggestions( |
171 const DismissedSuggestionsCallback& callback, | 172 const DismissedSuggestionsCallback& callback, |
172 const std::vector<OfflinePageItem>& offline_pages) const { | 173 const std::vector<OfflinePageItem>& offline_pages) const { |
173 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(); | 174 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(); |
174 std::vector<ContentSuggestion> suggestions; | 175 std::vector<ContentSuggestion> suggestions; |
175 for (const OfflinePageItem& item : offline_pages) { | 176 for (const OfflinePageItem& item : offline_pages) { |
176 if (!dismissed_ids.count(base::IntToString(item.offline_id))) | 177 if (!dismissed_ids.count(base::IntToString(item.offline_id))) { |
177 continue; | 178 continue; |
| 179 } |
178 | 180 |
179 suggestions.push_back(ConvertOfflinePage(item)); | 181 suggestions.push_back(ConvertOfflinePage(item)); |
180 } | 182 } |
181 callback.Run(std::move(suggestions)); | 183 callback.Run(std::move(suggestions)); |
182 } | 184 } |
183 | 185 |
184 void RecentTabSuggestionsProvider::OfflinePageModelLoaded( | 186 void RecentTabSuggestionsProvider::OfflinePageModelLoaded( |
185 offline_pages::OfflinePageModel* model) {} | 187 offline_pages::OfflinePageModel* model) {} |
186 | 188 |
187 void RecentTabSuggestionsProvider::OfflinePageModelChanged( | 189 void RecentTabSuggestionsProvider::OfflinePageModelChanged( |
188 offline_pages::OfflinePageModel* model) { | 190 offline_pages::OfflinePageModel* model) { |
189 DCHECK_EQ(offline_page_model_, model); | 191 DCHECK_EQ(offline_page_model_, model); |
190 FetchRecentTabs(); | 192 FetchRecentTabs(); |
191 } | 193 } |
192 | 194 |
193 void RecentTabSuggestionsProvider:: | 195 void RecentTabSuggestionsProvider:: |
194 GetPagesMatchingQueryCallbackForFetchRecentTabs( | 196 GetPagesMatchingQueryCallbackForFetchRecentTabs( |
195 const std::vector<OfflinePageItem>& offline_pages) { | 197 const std::vector<OfflinePageItem>& offline_pages) { |
196 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 198 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
197 std::set<std::string> old_dismissed_ids = ReadDismissedIDsFromPrefs(); | 199 std::set<std::string> old_dismissed_ids = ReadDismissedIDsFromPrefs(); |
198 std::set<std::string> new_dismissed_ids; | 200 std::set<std::string> new_dismissed_ids; |
199 std::vector<const OfflinePageItem*> recent_tab_items; | 201 std::vector<const OfflinePageItem*> recent_tab_items; |
200 for (const OfflinePageItem& item : offline_pages) { | 202 for (const OfflinePageItem& item : offline_pages) { |
201 std::string offline_page_id = base::IntToString(item.offline_id); | 203 std::string offline_page_id = base::IntToString(item.offline_id); |
202 if (old_dismissed_ids.count(offline_page_id)) | 204 if (old_dismissed_ids.count(offline_page_id)) { |
203 new_dismissed_ids.insert(offline_page_id); | 205 new_dismissed_ids.insert(offline_page_id); |
204 else | 206 } else { |
205 recent_tab_items.push_back(&item); | 207 recent_tab_items.push_back(&item); |
| 208 } |
206 } | 209 } |
207 | 210 |
208 observer()->OnNewSuggestions( | 211 observer()->OnNewSuggestions( |
209 this, provided_category_, | 212 this, provided_category_, |
210 GetMostRecentlyVisited(std::move(recent_tab_items))); | 213 GetMostRecentlyVisited(std::move(recent_tab_items))); |
211 if (new_dismissed_ids.size() != old_dismissed_ids.size()) | 214 if (new_dismissed_ids.size() != old_dismissed_ids.size()) { |
212 StoreDismissedIDsToPrefs(new_dismissed_ids); | 215 StoreDismissedIDsToPrefs(new_dismissed_ids); |
| 216 } |
213 } | 217 } |
214 | 218 |
215 void RecentTabSuggestionsProvider::OfflinePageDeleted( | 219 void RecentTabSuggestionsProvider::OfflinePageDeleted( |
216 int64_t offline_id, | 220 int64_t offline_id, |
217 const ClientId& client_id) { | 221 const ClientId& client_id) { |
218 // Because we never switch to NOT_PROVIDED dynamically, there can be no open | 222 // Because we never switch to NOT_PROVIDED dynamically, there can be no open |
219 // UI containing an invalidated suggestion unless the status is something | 223 // UI containing an invalidated suggestion unless the status is something |
220 // other than NOT_PROVIDED, so only notify invalidation in that case. | 224 // other than NOT_PROVIDED, so only notify invalidation in that case. |
221 if (category_status_ != CategoryStatus::NOT_PROVIDED) | 225 if (category_status_ != CategoryStatus::NOT_PROVIDED) { |
222 InvalidateSuggestion(offline_id); | 226 InvalidateSuggestion(offline_id); |
| 227 } |
223 } | 228 } |
224 | 229 |
225 void RecentTabSuggestionsProvider::FetchRecentTabs() { | 230 void RecentTabSuggestionsProvider::FetchRecentTabs() { |
226 offline_page_model_->GetPagesMatchingQuery( | 231 offline_page_model_->GetPagesMatchingQuery( |
227 BuildRecentTabsQuery(offline_page_model_), | 232 BuildRecentTabsQuery(offline_page_model_), |
228 base::Bind(&RecentTabSuggestionsProvider:: | 233 base::Bind(&RecentTabSuggestionsProvider:: |
229 GetPagesMatchingQueryCallbackForFetchRecentTabs, | 234 GetPagesMatchingQueryCallbackForFetchRecentTabs, |
230 weak_ptr_factory_.GetWeakPtr())); | 235 weak_ptr_factory_.GetWeakPtr())); |
231 } | 236 } |
232 | 237 |
233 void RecentTabSuggestionsProvider::NotifyStatusChanged( | 238 void RecentTabSuggestionsProvider::NotifyStatusChanged( |
234 CategoryStatus new_status) { | 239 CategoryStatus new_status) { |
235 DCHECK_NE(CategoryStatus::NOT_PROVIDED, category_status_); | 240 DCHECK_NE(CategoryStatus::NOT_PROVIDED, category_status_); |
236 if (category_status_ == new_status) | 241 if (category_status_ == new_status) { |
237 return; | 242 return; |
| 243 } |
238 category_status_ = new_status; | 244 category_status_ = new_status; |
239 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 245 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
240 } | 246 } |
241 | 247 |
242 ContentSuggestion RecentTabSuggestionsProvider::ConvertOfflinePage( | 248 ContentSuggestion RecentTabSuggestionsProvider::ConvertOfflinePage( |
243 const OfflinePageItem& offline_page) const { | 249 const OfflinePageItem& offline_page) const { |
244 // TODO(vitaliii): Make sure the URL is opened in the existing tab. | 250 // TODO(vitaliii): Make sure the URL is opened in the existing tab. |
245 ContentSuggestion suggestion(provided_category_, | 251 ContentSuggestion suggestion(provided_category_, |
246 base::IntToString(offline_page.offline_id), | 252 base::IntToString(offline_page.offline_id), |
247 offline_page.url); | 253 offline_page.url); |
(...skipping 15 matching lines...) Expand all Loading... |
263 } | 269 } |
264 | 270 |
265 std::vector<ContentSuggestion> | 271 std::vector<ContentSuggestion> |
266 RecentTabSuggestionsProvider::GetMostRecentlyVisited( | 272 RecentTabSuggestionsProvider::GetMostRecentlyVisited( |
267 std::vector<const OfflinePageItem*> offline_page_items) const { | 273 std::vector<const OfflinePageItem*> offline_page_items) const { |
268 std::sort(offline_page_items.begin(), offline_page_items.end(), | 274 std::sort(offline_page_items.begin(), offline_page_items.end(), |
269 OrderOfflinePagesByMostRecentlyVisitedFirst()); | 275 OrderOfflinePagesByMostRecentlyVisitedFirst()); |
270 std::vector<ContentSuggestion> suggestions; | 276 std::vector<ContentSuggestion> suggestions; |
271 for (const OfflinePageItem* offline_page_item : offline_page_items) { | 277 for (const OfflinePageItem* offline_page_item : offline_page_items) { |
272 suggestions.push_back(ConvertOfflinePage(*offline_page_item)); | 278 suggestions.push_back(ConvertOfflinePage(*offline_page_item)); |
273 if (suggestions.size() == kMaxSuggestionsCount) | 279 if (suggestions.size() == kMaxSuggestionsCount) { |
274 break; | 280 break; |
| 281 } |
275 } | 282 } |
276 return suggestions; | 283 return suggestions; |
277 } | 284 } |
278 | 285 |
279 void RecentTabSuggestionsProvider::InvalidateSuggestion(int64_t offline_id) { | 286 void RecentTabSuggestionsProvider::InvalidateSuggestion(int64_t offline_id) { |
280 std::string offline_page_id = base::IntToString(offline_id); | 287 std::string offline_page_id = base::IntToString(offline_id); |
281 observer()->OnSuggestionInvalidated( | 288 observer()->OnSuggestionInvalidated( |
282 this, ContentSuggestion::ID(provided_category_, offline_page_id)); | 289 this, ContentSuggestion::ID(provided_category_, offline_page_id)); |
283 | 290 |
284 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(); | 291 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(); |
(...skipping 11 matching lines...) Expand all Loading... |
296 } | 303 } |
297 | 304 |
298 void RecentTabSuggestionsProvider::StoreDismissedIDsToPrefs( | 305 void RecentTabSuggestionsProvider::StoreDismissedIDsToPrefs( |
299 const std::set<std::string>& dismissed_ids) { | 306 const std::set<std::string>& dismissed_ids) { |
300 prefs::StoreDismissedIDsToPrefs(pref_service_, | 307 prefs::StoreDismissedIDsToPrefs(pref_service_, |
301 prefs::kDismissedRecentOfflineTabSuggestions, | 308 prefs::kDismissedRecentOfflineTabSuggestions, |
302 dismissed_ids); | 309 dismissed_ids); |
303 } | 310 } |
304 | 311 |
305 } // namespace ntp_snippets | 312 } // namespace ntp_snippets |
OLD | NEW |