Chromium Code Reviews| Index: components/ntp_snippets/reading_list/reading_list_suggestions_provider.cc |
| diff --git a/components/ntp_snippets/reading_list/reading_list_suggestions_provider.cc b/components/ntp_snippets/reading_list/reading_list_suggestions_provider.cc |
| index f8eeb57b9ff11b7ecf4bb0fd670b587442daad84..e89abd33a8eeb38c994b578c4b11e756654ec6f8 100644 |
| --- a/components/ntp_snippets/reading_list/reading_list_suggestions_provider.cc |
| +++ b/components/ntp_snippets/reading_list/reading_list_suggestions_provider.cc |
| @@ -7,13 +7,19 @@ |
| #include <vector> |
| #include "base/bind.h" |
| +#include "base/strings/utf_string_conversions.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "components/ntp_snippets/category.h" |
| #include "components/reading_list/core/reading_list_entry.h" |
| #include "components/reading_list/core/reading_list_model.h" |
| #include "components/strings/grit/components_strings.h" |
| +#include "components/url_formatter/url_formatter.h" |
| #include "ui/base/l10n/l10n_util.h" |
| +namespace { |
| +const int kMaxEntries = 3; |
| +} |
| + |
| namespace ntp_snippets { |
| ReadingListSuggestionsProvider::ReadingListSuggestionsProvider( |
| @@ -26,9 +32,6 @@ ReadingListSuggestionsProvider::ReadingListSuggestionsProvider( |
| reading_list_model_(reading_list_model) { |
| observer->OnCategoryStatusChanged(this, provided_category_, category_status_); |
| reading_list_model->AddObserver(this); |
| - if (reading_list_model_->loaded()) { |
| - FetchReadingListInternal(); |
| - } |
| } |
| ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider() { |
| @@ -108,7 +111,50 @@ void ReadingListSuggestionsProvider::ReadingListModelLoaded( |
| } |
|
Olivier
2017/03/28 12:33:51
ReadingListModelBeingDeleted() {
reading_list_mo
gambard
2017/03/28 12:55:58
Done.
|
| void ReadingListSuggestionsProvider::FetchReadingListInternal() { |
| - // TODO(crbug.com/702241): Implement this method. |
| + std::vector<const ReadingListEntry*> entries; |
| + DCHECK(reading_list_model_->loaded()); |
| + for (const auto& url : reading_list_model_->Keys()) { |
| + const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); |
| + if (!entry->IsRead()) { |
| + bool added = false; |
| + for (auto it = entries.begin(); it != entries.end(); ++it) { |
| + if ((*it)->UpdateTime() < entry->UpdateTime()) { |
| + entries.insert(it, entry); |
| + added = true; |
| + break; |
| + } |
| + } |
| + if (!added && entries.size() < kMaxEntries) { |
| + entries.push_back(entry); |
| + } |
| + if (entries.size() > kMaxEntries) { |
| + entries.pop_back(); |
| + } |
| + } |
| + } |
| + |
| + std::vector<ContentSuggestion> suggestions; |
| + for (const ReadingListEntry* entry : entries) { |
| + ContentSuggestion suggestion(provided_category_, entry->URL().spec(), |
| + entry->URL()); |
| + suggestion.set_title(base::UTF8ToUTF16(entry->Title())); |
|
Olivier
2017/03/28 12:33:51
What if title is empty ? Should we adopt the same
gambard
2017/03/28 12:55:58
Done.
|
| + suggestion.set_snippet_text( |
| + url_formatter::FormatUrl(entry->URL().GetOrigin())); |
| + suggestions.emplace_back(std::move(suggestion)); |
| + } |
| + |
| + NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| + observer()->OnNewSuggestions(this, provided_category_, |
| + std::move(suggestions)); |
| +} |
| + |
| +void ReadingListSuggestionsProvider::NotifyStatusChanged( |
| + CategoryStatus new_status) { |
| + if (category_status_ == new_status) { |
| + return; |
| + } |
| + category_status_ = new_status; |
| + observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| } |
| } // namespace ntp_snippets |