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

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: Address comments 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 <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
11 #include "components/ntp_snippets/category.h" 12 #include "components/ntp_snippets/category.h"
12 #include "components/reading_list/core/reading_list_entry.h" 13 #include "components/reading_list/core/reading_list_entry.h"
13 #include "components/reading_list/core/reading_list_model.h" 14 #include "components/reading_list/core/reading_list_model.h"
14 #include "components/strings/grit/components_strings.h" 15 #include "components/strings/grit/components_strings.h"
16 #include "components/url_formatter/url_formatter.h"
15 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
16 18
19 namespace {
Marc Treib 2017/03/28 13:25:15 nit: Move into the ntp_snippets namespace?
gambard 2017/03/28 15:10:06 Done.
20 const int kMaxEntries = 3;
21 }
22
17 namespace ntp_snippets { 23 namespace ntp_snippets {
18 24
19 ReadingListSuggestionsProvider::ReadingListSuggestionsProvider( 25 ReadingListSuggestionsProvider::ReadingListSuggestionsProvider(
20 ContentSuggestionsProvider::Observer* observer, 26 ContentSuggestionsProvider::Observer* observer,
21 ReadingListModel* reading_list_model) 27 ReadingListModel* reading_list_model)
22 : ContentSuggestionsProvider(observer), 28 : ContentSuggestionsProvider(observer),
23 category_status_(CategoryStatus::AVAILABLE_LOADING), 29 category_status_(CategoryStatus::AVAILABLE_LOADING),
24 provided_category_( 30 provided_category_(
25 Category::FromKnownCategory(KnownCategories::READING_LIST)), 31 Category::FromKnownCategory(KnownCategories::READING_LIST)),
26 reading_list_model_(reading_list_model) { 32 reading_list_model_(reading_list_model) {
27 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); 33 observer->OnCategoryStatusChanged(this, provided_category_, category_status_);
28 reading_list_model->AddObserver(this); 34 reading_list_model->AddObserver(this);
29 if (reading_list_model_->loaded()) {
Marc Treib 2017/03/28 13:25:15 Is this not needed anymore?
gambard 2017/03/28 15:10:06 No, if the model is loaded when an observer is add
30 FetchReadingListInternal();
31 }
32 } 35 }
33 36
34 ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider() { 37 ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider() {
35 reading_list_model_->RemoveObserver(this); 38 if (reading_list_model_)
Marc Treib 2017/03/28 13:25:15 nit: braces please optional: ScopedObserver might
gambard 2017/03/28 15:10:06 Thanks, I did not know about ScopedObserver!
39 reading_list_model_->RemoveObserver(this);
36 } 40 }
37 41
38 CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus( 42 CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus(
39 Category category) { 43 Category category) {
40 DCHECK_EQ(category, provided_category_); 44 DCHECK_EQ(category, provided_category_);
41 return category_status_; 45 return category_status_;
42 } 46 }
43 47
44 CategoryInfo ReadingListSuggestionsProvider::GetCategoryInfo( 48 CategoryInfo ReadingListSuggestionsProvider::GetCategoryInfo(
45 Category category) { 49 Category category) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 Category category) { 104 Category category) {
101 // TODO(crbug.com/702241): Implement this method. 105 // TODO(crbug.com/702241): Implement this method.
102 } 106 }
103 107
104 void ReadingListSuggestionsProvider::ReadingListModelLoaded( 108 void ReadingListSuggestionsProvider::ReadingListModelLoaded(
105 const ReadingListModel* model) { 109 const ReadingListModel* model) {
106 DCHECK(model == reading_list_model_); 110 DCHECK(model == reading_list_model_);
107 FetchReadingListInternal(); 111 FetchReadingListInternal();
108 } 112 }
109 113
114 void ReadingListSuggestionsProvider::ReadingListModelBeingDeleted(
115 const ReadingListModel* model) {
116 DCHECK(model == reading_list_model_);
117 reading_list_model_->RemoveObserver(this);
118 reading_list_model_ = nullptr;
119 }
120
110 void ReadingListSuggestionsProvider::FetchReadingListInternal() { 121 void ReadingListSuggestionsProvider::FetchReadingListInternal() {
111 // TODO(crbug.com/702241): Implement this method. 122 if (!reading_list_model_)
123 return;
124
125 DCHECK(reading_list_model_->loaded());
126 std::vector<const ReadingListEntry*> entries;
127 for (const auto& url : reading_list_model_->Keys()) {
Marc Treib 2017/03/28 13:25:15 s/auto/actual type/ ? IMO it's not clear what the
gambard 2017/03/28 15:10:06 Done.
128 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
129 if (!entry->IsRead()) {
Marc Treib 2017/03/28 13:25:16 optional: "if (entry->IsRead()) { continue; }" ?
gambard 2017/03/28 15:10:06 Done.
130 bool added = false;
131 for (auto it = entries.begin(); it != entries.end(); ++it) {
132 if ((*it)->UpdateTime() < entry->UpdateTime()) {
133 entries.insert(it, entry);
134 added = true;
135 break;
136 }
137 }
138 if (!added && entries.size() < kMaxEntries) {
139 entries.push_back(entry);
140 }
141 if (entries.size() > kMaxEntries) {
142 entries.pop_back();
143 }
Marc Treib 2017/03/28 13:25:16 Hm, IMO it's not really obvious what this is suppo
gambard 2017/03/28 15:10:07 Done.
144 }
145 }
146
147 std::vector<ContentSuggestion> suggestions;
148 for (const ReadingListEntry* entry : entries) {
149 ContentSuggestion suggestion(provided_category_, entry->URL().spec(),
150 entry->URL());
151
152 if (entry->Title().size() > 0) {
Marc Treib 2017/03/28 13:25:16 nit: !entry->Title().empty()
gambard 2017/03/28 15:10:07 Done.
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()));
Marc Treib 2017/03/28 13:25:16 If the URL is already used as the title, then this
gambard 2017/03/28 15:10:07 The question was to do this test in the provider o
Marc Treib 2017/03/28 15:31:29 How would you check it there? After formatting, I
gambard 2017/03/28 15:42:49 Sorry, I missread your comment. The URL is used in
Marc Treib 2017/03/28 15:51:45 So if the page doesn't have a title, you'd see: ww
gambard 2017/03/29 07:23:24 Yes, you would see both. I looks better in the UI
159 suggestions.emplace_back(std::move(suggestion));
160 }
161
162 NotifyStatusChanged(CategoryStatus::AVAILABLE);
163 observer()->OnNewSuggestions(this, provided_category_,
164 std::move(suggestions));
165 }
166
167 void ReadingListSuggestionsProvider::NotifyStatusChanged(
168 CategoryStatus new_status) {
169 if (category_status_ == new_status) {
170 return;
171 }
172 category_status_ = new_status;
173 observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
112 } 174 }
113 175
114 } // namespace ntp_snippets 176 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698