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

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: Rebase 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 {
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()) {
30 FetchReadingListInternal();
31 }
32 } 35 }
33 36
34 ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider() { 37 ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider() {
35 reading_list_model_->RemoveObserver(this); 38 reading_list_model_->RemoveObserver(this);
36 } 39 }
37 40
38 CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus( 41 CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus(
39 Category category) { 42 Category category) {
40 DCHECK_EQ(category, provided_category_); 43 DCHECK_EQ(category, provided_category_);
41 return category_status_; 44 return category_status_;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 void ReadingListSuggestionsProvider::ClearDismissedSuggestionsForDebugging( 102 void ReadingListSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
100 Category category) { 103 Category category) {
101 // TODO(crbug.com/702241): Implement this method. 104 // TODO(crbug.com/702241): Implement this method.
102 } 105 }
103 106
104 void ReadingListSuggestionsProvider::ReadingListModelLoaded( 107 void ReadingListSuggestionsProvider::ReadingListModelLoaded(
105 const ReadingListModel* model) { 108 const ReadingListModel* model) {
106 DCHECK(model == reading_list_model_); 109 DCHECK(model == reading_list_model_);
107 FetchReadingListInternal(); 110 FetchReadingListInternal();
108 } 111 }
109 112
Olivier 2017/03/28 12:33:51 ReadingListModelBeingDeleted() { reading_list_mo
gambard 2017/03/28 12:55:58 Done.
110 void ReadingListSuggestionsProvider::FetchReadingListInternal() { 113 void ReadingListSuggestionsProvider::FetchReadingListInternal() {
111 // TODO(crbug.com/702241): Implement this method. 114 std::vector<const ReadingListEntry*> entries;
115 DCHECK(reading_list_model_->loaded());
116 for (const auto& url : reading_list_model_->Keys()) {
117 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
118 if (!entry->IsRead()) {
119 bool added = false;
120 for (auto it = entries.begin(); it != entries.end(); ++it) {
121 if ((*it)->UpdateTime() < entry->UpdateTime()) {
122 entries.insert(it, entry);
123 added = true;
124 break;
125 }
126 }
127 if (!added && entries.size() < kMaxEntries) {
128 entries.push_back(entry);
129 }
130 if (entries.size() > kMaxEntries) {
131 entries.pop_back();
132 }
133 }
134 }
135
136 std::vector<ContentSuggestion> suggestions;
137 for (const ReadingListEntry* entry : entries) {
138 ContentSuggestion suggestion(provided_category_, entry->URL().spec(),
139 entry->URL());
140 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.
141 suggestion.set_snippet_text(
142 url_formatter::FormatUrl(entry->URL().GetOrigin()));
143 suggestions.emplace_back(std::move(suggestion));
144 }
145
146 NotifyStatusChanged(CategoryStatus::AVAILABLE);
147 observer()->OnNewSuggestions(this, provided_category_,
148 std::move(suggestions));
149 }
150
151 void ReadingListSuggestionsProvider::NotifyStatusChanged(
152 CategoryStatus new_status) {
153 if (category_status_ == new_status) {
154 return;
155 }
156 category_status_ = new_status;
157 observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
112 } 158 }
113 159
114 } // namespace ntp_snippets 160 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698