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/offline_page_suggestions_provide
r.h" | 5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 /*show_if_empty=*/false); | 128 /*show_if_empty=*/false); |
129 } | 129 } |
130 NOTREACHED() << "Unknown category " << category.id(); | 130 NOTREACHED() << "Unknown category " << category.id(); |
131 return CategoryInfo(base::string16(), | 131 return CategoryInfo(base::string16(), |
132 ContentSuggestionsCardLayout::MINIMAL_CARD, | 132 ContentSuggestionsCardLayout::MINIMAL_CARD, |
133 /*has_more_button=*/false, | 133 /*has_more_button=*/false, |
134 /*show_if_empty=*/false); | 134 /*show_if_empty=*/false); |
135 } | 135 } |
136 | 136 |
137 void OfflinePageSuggestionsProvider::DismissSuggestion( | 137 void OfflinePageSuggestionsProvider::DismissSuggestion( |
138 const std::string& suggestion_id) { | 138 const ContentSuggestion::ID& suggestion_id) { |
139 Category category = GetCategoryFromUniqueID(suggestion_id); | 139 std::set<std::string> dismissed_ids = |
140 std::string offline_page_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | 140 ReadDismissedIDsFromPrefs(suggestion_id.category()); |
141 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(category); | 141 dismissed_ids.insert(suggestion_id.id_within_category()); |
142 dismissed_ids.insert(offline_page_id); | 142 StoreDismissedIDsToPrefs(suggestion_id.category(), dismissed_ids); |
143 StoreDismissedIDsToPrefs(category, dismissed_ids); | |
144 } | 143 } |
145 | 144 |
146 void OfflinePageSuggestionsProvider::FetchSuggestionImage( | 145 void OfflinePageSuggestionsProvider::FetchSuggestionImage( |
147 const std::string& suggestion_id, | 146 const ContentSuggestion::ID& suggestion_id, |
148 const ImageFetchedCallback& callback) { | 147 const ImageFetchedCallback& callback) { |
149 // TODO(pke): Fetch proper thumbnail from OfflinePageModel once it's available | 148 // TODO(pke): Fetch proper thumbnail from OfflinePageModel once it's available |
150 // there. | 149 // there. |
151 base::ThreadTaskRunnerHandle::Get()->PostTask( | 150 base::ThreadTaskRunnerHandle::Get()->PostTask( |
152 FROM_HERE, base::Bind(callback, gfx::Image())); | 151 FROM_HERE, base::Bind(callback, gfx::Image())); |
153 } | 152 } |
154 | 153 |
155 void OfflinePageSuggestionsProvider::ClearHistory( | 154 void OfflinePageSuggestionsProvider::ClearHistory( |
156 base::Time begin, | 155 base::Time begin, |
157 base::Time end, | 156 base::Time end, |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 NOTREACHED() << "Unknown category " << category.id(); | 306 NOTREACHED() << "Unknown category " << category.id(); |
308 } | 307 } |
309 } | 308 } |
310 | 309 |
311 ContentSuggestion OfflinePageSuggestionsProvider::ConvertOfflinePage( | 310 ContentSuggestion OfflinePageSuggestionsProvider::ConvertOfflinePage( |
312 Category category, | 311 Category category, |
313 const OfflinePageItem& offline_page) const { | 312 const OfflinePageItem& offline_page) const { |
314 // TODO(pke): Make sure the URL is actually opened as an offline URL. | 313 // TODO(pke): Make sure the URL is actually opened as an offline URL. |
315 // Currently, the browser opens the offline URL and then immediately | 314 // Currently, the browser opens the offline URL and then immediately |
316 // redirects to the online URL if the device is online. | 315 // redirects to the online URL if the device is online. |
317 ContentSuggestion suggestion( | 316 ContentSuggestion suggestion(category, |
318 MakeUniqueID(category, base::IntToString(offline_page.offline_id)), | 317 base::IntToString(offline_page.offline_id), |
319 offline_page.GetOfflineURL()); | 318 offline_page.GetOfflineURL()); |
320 | 319 |
321 if (offline_page.title.empty()) { | 320 if (offline_page.title.empty()) { |
322 // TODO(pke): Remove this fallback once the OfflinePageModel provides titles | 321 // TODO(pke): Remove this fallback once the OfflinePageModel provides titles |
323 // for all (relevant) OfflinePageItems. | 322 // for all (relevant) OfflinePageItems. |
324 suggestion.set_title(base::UTF8ToUTF16(offline_page.url.spec())); | 323 suggestion.set_title(base::UTF8ToUTF16(offline_page.url.spec())); |
325 } else { | 324 } else { |
326 suggestion.set_title(offline_page.title); | 325 suggestion.set_title(offline_page.title); |
327 } | 326 } |
328 suggestion.set_publish_date(offline_page.creation_time); | 327 suggestion.set_publish_date(offline_page.creation_time); |
329 suggestion.set_publisher_name(base::UTF8ToUTF16(offline_page.url.host())); | 328 suggestion.set_publisher_name(base::UTF8ToUTF16(offline_page.url.host())); |
(...skipping 11 matching lines...) Expand all Loading... |
341 suggestions.push_back(ConvertOfflinePage(category, *offline_page_item)); | 340 suggestions.push_back(ConvertOfflinePage(category, *offline_page_item)); |
342 if (suggestions.size() == kMaxSuggestionsCount) | 341 if (suggestions.size() == kMaxSuggestionsCount) |
343 break; | 342 break; |
344 } | 343 } |
345 return suggestions; | 344 return suggestions; |
346 } | 345 } |
347 | 346 |
348 void OfflinePageSuggestionsProvider::InvalidateSuggestion(Category category, | 347 void OfflinePageSuggestionsProvider::InvalidateSuggestion(Category category, |
349 int64_t offline_id) { | 348 int64_t offline_id) { |
350 std::string offline_page_id = base::IntToString(offline_id); | 349 std::string offline_page_id = base::IntToString(offline_id); |
351 observer()->OnSuggestionInvalidated(this, category, | 350 observer()->OnSuggestionInvalidated( |
352 MakeUniqueID(category, offline_page_id)); | 351 this, ContentSuggestion::ID(category, offline_page_id)); |
353 | 352 |
354 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(category); | 353 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(category); |
355 auto it = dismissed_ids.find(offline_page_id); | 354 auto it = dismissed_ids.find(offline_page_id); |
356 if (it != dismissed_ids.end()) { | 355 if (it != dismissed_ids.end()) { |
357 dismissed_ids.erase(it); | 356 dismissed_ids.erase(it); |
358 StoreDismissedIDsToPrefs(category, dismissed_ids); | 357 StoreDismissedIDsToPrefs(category, dismissed_ids); |
359 } | 358 } |
360 } | 359 } |
361 | 360 |
362 std::string OfflinePageSuggestionsProvider::GetDismissedPref( | 361 std::string OfflinePageSuggestionsProvider::GetDismissedPref( |
(...skipping 13 matching lines...) Expand all Loading... |
376 } | 375 } |
377 | 376 |
378 void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs( | 377 void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs( |
379 Category category, | 378 Category category, |
380 const std::set<std::string>& dismissed_ids) { | 379 const std::set<std::string>& dismissed_ids) { |
381 prefs::StoreDismissedIDsToPrefs(pref_service_, GetDismissedPref(category), | 380 prefs::StoreDismissedIDsToPrefs(pref_service_, GetDismissedPref(category), |
382 dismissed_ids); | 381 dismissed_ids); |
383 } | 382 } |
384 | 383 |
385 } // namespace ntp_snippets | 384 } // namespace ntp_snippets |
OLD | NEW |