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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
12 #include "components/ntp_snippets/category.h" | 13 #include "components/ntp_snippets/category.h" |
13 #include "components/reading_list/core/reading_list_entry.h" | 14 #include "components/reading_list/core/reading_list_entry.h" |
14 #include "components/reading_list/core/reading_list_model.h" | 15 #include "components/reading_list/core/reading_list_model.h" |
15 #include "components/strings/grit/components_strings.h" | 16 #include "components/strings/grit/components_strings.h" |
16 #include "components/url_formatter/url_formatter.h" | 17 #include "components/url_formatter/url_formatter.h" |
17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
18 | 19 |
19 namespace { | 20 namespace { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 entries.push_back(entry); | 140 entries.push_back(entry); |
140 } | 141 } |
141 if (entries.size() > kMaxEntries) { | 142 if (entries.size() > kMaxEntries) { |
142 entries.pop_back(); | 143 entries.pop_back(); |
143 } | 144 } |
144 } | 145 } |
145 } | 146 } |
146 | 147 |
147 std::vector<ContentSuggestion> suggestions; | 148 std::vector<ContentSuggestion> suggestions; |
148 for (const ReadingListEntry* entry : entries) { | 149 for (const ReadingListEntry* entry : entries) { |
149 ContentSuggestion suggestion(provided_category_, entry->URL().spec(), | 150 ConvertEntry(entry, &suggestions); |
150 entry->URL()); | |
151 | |
152 if (entry->Title().size() > 0) { | |
153 suggestion.set_title(base::UTF8ToUTF16(entry->Title())); | |
154 } else { | |
155 suggestion.set_title(url_formatter::FormatUrl(entry->URL())); | |
156 } | |
157 suggestion.set_snippet_text( | |
158 url_formatter::FormatUrl(entry->URL().GetOrigin())); | |
159 suggestions.emplace_back(std::move(suggestion)); | |
160 } | 151 } |
161 | 152 |
162 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 153 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
163 observer()->OnNewSuggestions(this, provided_category_, | 154 observer()->OnNewSuggestions(this, provided_category_, |
164 std::move(suggestions)); | 155 std::move(suggestions)); |
165 } | 156 } |
166 | 157 |
158 void ReadingListSuggestionsProvider::ConvertEntry( | |
159 const ReadingListEntry* entry, | |
160 std::vector<ContentSuggestion>* suggestions) { | |
Marc Treib
2017/03/28 13:38:20
I don't like out params, and IMO converting has li
gambard
2017/03/29 06:51:24
That was my first idea, but I followed the pattern
| |
161 ContentSuggestion suggestion(provided_category_, entry->URL().spec(), | |
162 entry->URL()); | |
163 if (entry->Title().size() > 0) { | |
164 suggestion.set_title(base::UTF8ToUTF16(entry->Title())); | |
165 } else { | |
166 suggestion.set_title(url_formatter::FormatUrl(entry->URL())); | |
167 } | |
168 suggestion.set_snippet_text( | |
169 url_formatter::FormatUrl(entry->URL().GetOrigin())); | |
170 | |
171 auto extra = base::MakeUnique<ReadingListSuggestionExtra>(); | |
172 if (entry->DistilledState() == ReadingListEntry::PROCESSED) { | |
Marc Treib
2017/03/28 13:38:20
switch?
Wrap this in a global helper method?
Olivier
2017/03/28 13:43:45
switch?
gambard
2017/03/29 06:51:24
Done.
gambard
2017/03/29 06:51:24
Done.
| |
173 extra->distilled_state = ReadingListSuggestionExtra:: | |
174 ReadingListSuggestionDistilledState::SUCCESS; | |
175 } else if (entry->DistilledState() == ReadingListEntry::DISTILLATION_ERROR) { | |
176 extra->distilled_state = ReadingListSuggestionExtra:: | |
177 ReadingListSuggestionDistilledState::FAILURE; | |
178 } else { | |
179 extra->distilled_state = ReadingListSuggestionExtra:: | |
180 ReadingListSuggestionDistilledState::PROCESSING; | |
181 } | |
182 extra->favicon_page_url = | |
183 entry->DistilledURL().is_valid() ? entry->DistilledURL() : entry->URL(); | |
184 suggestion.set_reading_list_suggestion_extra(std::move(extra)); | |
185 | |
186 suggestions->emplace_back(std::move(suggestion)); | |
187 } | |
188 | |
167 void ReadingListSuggestionsProvider::NotifyStatusChanged( | 189 void ReadingListSuggestionsProvider::NotifyStatusChanged( |
168 CategoryStatus new_status) { | 190 CategoryStatus new_status) { |
169 if (category_status_ == new_status) { | 191 if (category_status_ == new_status) { |
170 return; | 192 return; |
171 } | 193 } |
172 category_status_ = new_status; | 194 category_status_ = new_status; |
173 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 195 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
174 } | 196 } |
175 | 197 |
176 } // namespace ntp_snippets | 198 } // namespace ntp_snippets |
OLD | NEW |