| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/reading_list/reading_list_suggestions_provider
.h" | 5 #include "components/ntp_snippets/reading_list/reading_list_suggestions_provider
.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/ntp_snippets/category.h" | 14 #include "components/ntp_snippets/category.h" |
| 15 #include "components/ntp_snippets/reading_list/reading_list_distillation_state_u
til.h" |
| 14 #include "components/reading_list/core/reading_list_entry.h" | 16 #include "components/reading_list/core/reading_list_entry.h" |
| 15 #include "components/reading_list/core/reading_list_model.h" | 17 #include "components/reading_list/core/reading_list_model.h" |
| 16 #include "components/strings/grit/components_strings.h" | 18 #include "components/strings/grit/components_strings.h" |
| 17 #include "components/url_formatter/url_formatter.h" | 19 #include "components/url_formatter/url_formatter.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 19 | 21 |
| 20 namespace ntp_snippets { | 22 namespace ntp_snippets { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 // Max number of entries to return. | 25 // Max number of entries to return. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 // Get the |kMaxEntries| most recent entries. | 144 // Get the |kMaxEntries| most recent entries. |
| 143 std::partial_sort(entries.begin(), entries.begin() + kMaxEntries, | 145 std::partial_sort(entries.begin(), entries.begin() + kMaxEntries, |
| 144 entries.end(), CompareEntries); | 146 entries.end(), CompareEntries); |
| 145 entries.resize(kMaxEntries); | 147 entries.resize(kMaxEntries); |
| 146 } else { | 148 } else { |
| 147 std::sort(entries.begin(), entries.end(), CompareEntries); | 149 std::sort(entries.begin(), entries.end(), CompareEntries); |
| 148 } | 150 } |
| 149 | 151 |
| 150 std::vector<ContentSuggestion> suggestions; | 152 std::vector<ContentSuggestion> suggestions; |
| 151 for (const ReadingListEntry* entry : entries) { | 153 for (const ReadingListEntry* entry : entries) { |
| 152 ContentSuggestion suggestion(provided_category_, entry->URL().spec(), | 154 suggestions.emplace_back(ConvertEntry(entry)); |
| 153 entry->URL()); | |
| 154 | |
| 155 if (!entry->Title().empty()) { | |
| 156 suggestion.set_title(base::UTF8ToUTF16(entry->Title())); | |
| 157 } else { | |
| 158 suggestion.set_title(url_formatter::FormatUrl(entry->URL())); | |
| 159 } | |
| 160 suggestion.set_publisher_name( | |
| 161 url_formatter::FormatUrl(entry->URL().GetOrigin())); | |
| 162 suggestions.emplace_back(std::move(suggestion)); | |
| 163 } | 155 } |
| 164 | 156 |
| 165 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 157 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 166 observer()->OnNewSuggestions(this, provided_category_, | 158 observer()->OnNewSuggestions(this, provided_category_, |
| 167 std::move(suggestions)); | 159 std::move(suggestions)); |
| 168 } | 160 } |
| 169 | 161 |
| 162 ContentSuggestion ReadingListSuggestionsProvider::ConvertEntry( |
| 163 const ReadingListEntry* entry) { |
| 164 ContentSuggestion suggestion(provided_category_, entry->URL().spec(), |
| 165 entry->URL()); |
| 166 |
| 167 if (!entry->Title().empty()) { |
| 168 suggestion.set_title(base::UTF8ToUTF16(entry->Title())); |
| 169 } else { |
| 170 suggestion.set_title(url_formatter::FormatUrl(entry->URL())); |
| 171 } |
| 172 suggestion.set_publisher_name( |
| 173 url_formatter::FormatUrl(entry->URL().GetOrigin())); |
| 174 |
| 175 auto extra = base::MakeUnique<ReadingListSuggestionExtra>(); |
| 176 extra->distilled_state = |
| 177 SuggestionStateFromReadingListState(entry->DistilledState()); |
| 178 extra->favicon_page_url = |
| 179 entry->DistilledURL().is_valid() ? entry->DistilledURL() : entry->URL(); |
| 180 suggestion.set_reading_list_suggestion_extra(std::move(extra)); |
| 181 |
| 182 return suggestion; |
| 183 } |
| 184 |
| 170 void ReadingListSuggestionsProvider::NotifyStatusChanged( | 185 void ReadingListSuggestionsProvider::NotifyStatusChanged( |
| 171 CategoryStatus new_status) { | 186 CategoryStatus new_status) { |
| 172 if (category_status_ == new_status) { | 187 if (category_status_ == new_status) { |
| 173 return; | 188 return; |
| 174 } | 189 } |
| 175 category_status_ = new_status; | 190 category_status_ = new_status; |
| 176 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 191 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 177 } | 192 } |
| 178 | 193 |
| 179 } // namespace ntp_snippets | 194 } // namespace ntp_snippets |
| OLD | NEW |