Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: components/ntp_snippets/reading_list/reading_list_suggestions_provider.cc

Issue 2770893003: Add logic for fetching the Reading List entries (Closed)
Patch Set: Add title logic Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <vector> 8 #include <vector>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
11 #include "components/ntp_snippets/category.h" 13 #include "components/ntp_snippets/category.h"
12 #include "components/reading_list/core/reading_list_entry.h" 14 #include "components/reading_list/core/reading_list_entry.h"
13 #include "components/reading_list/core/reading_list_model.h" 15 #include "components/reading_list/core/reading_list_model.h"
14 #include "components/strings/grit/components_strings.h" 16 #include "components/strings/grit/components_strings.h"
17 #include "components/url_formatter/url_formatter.h"
15 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
16 19
17 namespace ntp_snippets { 20 namespace ntp_snippets {
18 21
22 namespace {
23 // Max number of entries to return.
24 const int kMaxEntries = 3;
25
26 bool CompareEntries(const ReadingListEntry* lhs, const ReadingListEntry* rhs) {
27 return lhs->UpdateTime() > rhs->UpdateTime();
28 }
29 }
30
19 ReadingListSuggestionsProvider::ReadingListSuggestionsProvider( 31 ReadingListSuggestionsProvider::ReadingListSuggestionsProvider(
20 ContentSuggestionsProvider::Observer* observer, 32 ContentSuggestionsProvider::Observer* observer,
21 ReadingListModel* reading_list_model) 33 ReadingListModel* reading_list_model)
22 : ContentSuggestionsProvider(observer), 34 : ContentSuggestionsProvider(observer),
23 category_status_(CategoryStatus::AVAILABLE_LOADING), 35 category_status_(CategoryStatus::AVAILABLE_LOADING),
24 provided_category_( 36 provided_category_(
25 Category::FromKnownCategory(KnownCategories::READING_LIST)), 37 Category::FromKnownCategory(KnownCategories::READING_LIST)),
26 reading_list_model_(reading_list_model) { 38 reading_list_model_(reading_list_model),
39 scoped_observer_(this) {
27 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); 40 observer->OnCategoryStatusChanged(this, provided_category_, category_status_);
28 reading_list_model->AddObserver(this); 41
29 if (reading_list_model_->loaded()) { 42 // If the ReadingListModel is loaded, this will trigger a call to
30 FetchReadingListInternal(); 43 // ReadingListModelLoaded. Keep it as last instruction.
31 } 44 scoped_observer_.Add(reading_list_model_);
32 } 45 }
33 46
34 ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider() { 47 ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider(){};
35 reading_list_model_->RemoveObserver(this);
36 }
37 48
38 CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus( 49 CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus(
39 Category category) { 50 Category category) {
40 DCHECK_EQ(category, provided_category_); 51 DCHECK_EQ(category, provided_category_);
41 return category_status_; 52 return category_status_;
42 } 53 }
43 54
44 CategoryInfo ReadingListSuggestionsProvider::GetCategoryInfo( 55 CategoryInfo ReadingListSuggestionsProvider::GetCategoryInfo(
45 Category category) { 56 Category category) {
46 DCHECK_EQ(category, provided_category_); 57 DCHECK_EQ(category, provided_category_);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 Category category) { 111 Category category) {
101 // TODO(crbug.com/702241): Implement this method. 112 // TODO(crbug.com/702241): Implement this method.
102 } 113 }
103 114
104 void ReadingListSuggestionsProvider::ReadingListModelLoaded( 115 void ReadingListSuggestionsProvider::ReadingListModelLoaded(
105 const ReadingListModel* model) { 116 const ReadingListModel* model) {
106 DCHECK(model == reading_list_model_); 117 DCHECK(model == reading_list_model_);
107 FetchReadingListInternal(); 118 FetchReadingListInternal();
108 } 119 }
109 120
121 void ReadingListSuggestionsProvider::ReadingListModelBeingDeleted(
122 const ReadingListModel* model) {
123 DCHECK(model == reading_list_model_);
124 scoped_observer_.Remove(reading_list_model_);
125 reading_list_model_ = nullptr;
126 }
127
110 void ReadingListSuggestionsProvider::FetchReadingListInternal() { 128 void ReadingListSuggestionsProvider::FetchReadingListInternal() {
111 // TODO(crbug.com/702241): Implement this method. 129 if (!reading_list_model_)
130 return;
131
132 DCHECK(reading_list_model_->loaded());
133 std::vector<const ReadingListEntry*> entries;
134 for (const GURL& url : reading_list_model_->Keys()) {
135 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
136 if (!entry->IsRead()) {
137 entries.emplace_back(entry);
138 }
139 }
140
141 if (entries.size() > kMaxEntries) {
142 // Get the |kMaxEntries| most recent entries.
143 std::partial_sort(entries.begin(), entries.begin() + kMaxEntries,
144 entries.end(), CompareEntries);
145 entries.resize(kMaxEntries);
146 } else {
147 std::sort(entries.begin(), entries.end(), CompareEntries);
148 }
149
150 std::vector<ContentSuggestion> suggestions;
151 for (const ReadingListEntry* entry : entries) {
152 ContentSuggestion suggestion(provided_category_, entry->URL().spec(),
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 }
164
165 NotifyStatusChanged(CategoryStatus::AVAILABLE);
166 observer()->OnNewSuggestions(this, provided_category_,
167 std::move(suggestions));
168 }
169
170 void ReadingListSuggestionsProvider::NotifyStatusChanged(
171 CategoryStatus new_status) {
172 if (category_status_ == new_status) {
173 return;
174 }
175 category_status_ = new_status;
176 observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
112 } 177 }
113 178
114 } // namespace ntp_snippets 179 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698