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 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/guid.h" | 11 #include "base/guid.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "components/ntp_snippets/pref_names.h" | 17 #include "components/ntp_snippets/pref_names.h" |
| 18 #include "components/ntp_snippets/pref_util.h" |
17 #include "components/offline_pages/client_namespace_constants.h" | 19 #include "components/offline_pages/client_namespace_constants.h" |
18 #include "components/prefs/pref_registry_simple.h" | 20 #include "components/prefs/pref_registry_simple.h" |
19 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
20 #include "grit/components_strings.h" | 22 #include "grit/components_strings.h" |
21 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
23 | 25 |
24 using offline_pages::MultipleOfflinePageItemResult; | 26 using offline_pages::MultipleOfflinePageItemResult; |
25 using offline_pages::OfflinePageModel; | 27 using offline_pages::OfflinePageModel; |
26 using offline_pages::OfflinePageItem; | 28 using offline_pages::OfflinePageItem; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 NOTREACHED() << "Unknown category " << category.id(); | 110 NOTREACHED() << "Unknown category " << category.id(); |
109 return CategoryStatus::NOT_PROVIDED; | 111 return CategoryStatus::NOT_PROVIDED; |
110 } | 112 } |
111 | 113 |
112 CategoryInfo OfflinePageSuggestionsProvider::GetCategoryInfo( | 114 CategoryInfo OfflinePageSuggestionsProvider::GetCategoryInfo( |
113 Category category) { | 115 Category category) { |
114 if (category == recent_tabs_category_) { | 116 if (category == recent_tabs_category_) { |
115 return CategoryInfo(l10n_util::GetStringUTF16( | 117 return CategoryInfo(l10n_util::GetStringUTF16( |
116 IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_HEADER), | 118 IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_HEADER), |
117 ContentSuggestionsCardLayout::MINIMAL_CARD, | 119 ContentSuggestionsCardLayout::MINIMAL_CARD, |
118 /* has_more_button */ false, | 120 /*has_more_button=*/false, |
119 /* show_if_empty */ false); | 121 /*show_if_empty=*/false); |
120 } | 122 } |
121 if (category == downloads_category_) { | 123 if (category == downloads_category_) { |
122 return CategoryInfo( | 124 return CategoryInfo( |
123 l10n_util::GetStringUTF16(IDS_NTP_DOWNLOAD_SUGGESTIONS_SECTION_HEADER), | 125 l10n_util::GetStringUTF16(IDS_NTP_DOWNLOAD_SUGGESTIONS_SECTION_HEADER), |
124 ContentSuggestionsCardLayout::MINIMAL_CARD, | 126 ContentSuggestionsCardLayout::MINIMAL_CARD, |
125 /* has_more_button */ download_manager_ui_enabled_, | 127 /*has_more_button=*/download_manager_ui_enabled_, |
126 /* show_if_empty */ false); | 128 /*show_if_empty=*/false); |
127 } | 129 } |
128 NOTREACHED() << "Unknown category " << category.id(); | 130 NOTREACHED() << "Unknown category " << category.id(); |
129 return CategoryInfo(base::string16(), | 131 return CategoryInfo(base::string16(), |
130 ContentSuggestionsCardLayout::MINIMAL_CARD, | 132 ContentSuggestionsCardLayout::MINIMAL_CARD, |
131 /* has_more_button */ false, | 133 /*has_more_button=*/false, |
132 /* show_if_empty */ false); | 134 /*show_if_empty=*/false); |
133 } | 135 } |
134 | 136 |
135 void OfflinePageSuggestionsProvider::DismissSuggestion( | 137 void OfflinePageSuggestionsProvider::DismissSuggestion( |
136 const std::string& suggestion_id) { | 138 const std::string& suggestion_id) { |
137 Category category = GetCategoryFromUniqueID(suggestion_id); | 139 Category category = GetCategoryFromUniqueID(suggestion_id); |
138 std::string offline_page_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | 140 std::string offline_page_id = GetWithinCategoryIDFromUniqueID(suggestion_id); |
139 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(category); | 141 std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs(category); |
140 dismissed_ids.insert(offline_page_id); | 142 dismissed_ids.insert(offline_page_id); |
141 StoreDismissedIDsToPrefs(category, dismissed_ids); | 143 StoreDismissedIDsToPrefs(category, dismissed_ids); |
142 } | 144 } |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 if (category == recent_tabs_category_) | 365 if (category == recent_tabs_category_) |
364 return prefs::kDismissedRecentOfflineTabSuggestions; | 366 return prefs::kDismissedRecentOfflineTabSuggestions; |
365 if (category == downloads_category_) | 367 if (category == downloads_category_) |
366 return prefs::kDismissedDownloadSuggestions; | 368 return prefs::kDismissedDownloadSuggestions; |
367 NOTREACHED() << "Unknown category " << category.id(); | 369 NOTREACHED() << "Unknown category " << category.id(); |
368 return std::string(); | 370 return std::string(); |
369 } | 371 } |
370 | 372 |
371 std::set<std::string> OfflinePageSuggestionsProvider::ReadDismissedIDsFromPrefs( | 373 std::set<std::string> OfflinePageSuggestionsProvider::ReadDismissedIDsFromPrefs( |
372 Category category) const { | 374 Category category) const { |
373 std::set<std::string> dismissed_ids; | 375 return prefs::ReadDismissedIDsFromPrefs(*pref_service_, |
374 const base::ListValue* list = | 376 GetDismissedPref(category)); |
375 pref_service_->GetList(GetDismissedPref(category)); | |
376 for (const std::unique_ptr<base::Value>& value : *list) { | |
377 std::string dismissed_id; | |
378 bool success = value->GetAsString(&dismissed_id); | |
379 DCHECK(success) << "Failed to parse dismissed offline page ID from prefs"; | |
380 dismissed_ids.insert(dismissed_id); | |
381 } | |
382 return dismissed_ids; | |
383 } | 377 } |
384 | 378 |
385 void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs( | 379 void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs( |
386 Category category, | 380 Category category, |
387 const std::set<std::string>& dismissed_ids) { | 381 const std::set<std::string>& dismissed_ids) { |
388 base::ListValue list; | 382 prefs::StoreDismissedIDsToPrefs(pref_service_, GetDismissedPref(category), |
389 for (const std::string& dismissed_id : dismissed_ids) | 383 dismissed_ids); |
390 list.AppendString(dismissed_id); | |
391 pref_service_->Set(GetDismissedPref(category), list); | |
392 } | 384 } |
393 | 385 |
394 } // namespace ntp_snippets | 386 } // namespace ntp_snippets |
OLD | NEW |