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 <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 Loading... | |
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) { | |
Marc Treib
2017/03/28 15:31:29
// Get the |kMaxEntries| most recent entries.
?
gambard
2017/03/28 15:40:10
Done.
| |
142 std::partial_sort(entries.begin(), entries.begin() + kMaxEntries, | |
143 entries.end(), CompareEntries); | |
144 entries.resize(kMaxEntries); | |
145 } else { | |
146 std::sort(entries.begin(), entries.end(), CompareEntries); | |
147 } | |
148 | |
149 std::vector<ContentSuggestion> suggestions; | |
150 for (const ReadingListEntry* entry : entries) { | |
151 ContentSuggestion suggestion(provided_category_, entry->URL().spec(), | |
152 entry->URL()); | |
153 | |
154 suggestion.set_title(base::UTF8ToUTF16(entry->Title())); | |
155 suggestion.set_snippet_text( | |
156 url_formatter::FormatUrl(entry->URL().GetOrigin())); | |
157 suggestions.emplace_back(std::move(suggestion)); | |
158 } | |
159 | |
160 NotifyStatusChanged(CategoryStatus::AVAILABLE); | |
161 observer()->OnNewSuggestions(this, provided_category_, | |
162 std::move(suggestions)); | |
163 } | |
164 | |
165 void ReadingListSuggestionsProvider::NotifyStatusChanged( | |
166 CategoryStatus new_status) { | |
167 if (category_status_ == new_status) { | |
168 return; | |
169 } | |
170 category_status_ = new_status; | |
171 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | |
112 } | 172 } |
113 | 173 |
114 } // namespace ntp_snippets | 174 } // namespace ntp_snippets |
OLD | NEW |