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

Side by Side Diff: components/ntp_snippets/bookmarks/bookmark_suggestions_provider.cc

Issue 2190583002: Add bookmark provider for content suggestions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments #1 Created 4 years, 4 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h"
6
7 #include <utility>
8 #include <vector>
9
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/bookmarks/browser/bookmark_model.h"
12 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h"
13 #include "components/ntp_snippets/content_suggestion.h"
14
15 using bookmarks::BookmarkModel;
16 using bookmarks::BookmarkNode;
17
18 namespace {
19 const int kMaxBookmarks = 10;
20 const int kMaxBookmarkAgeInDays = 42;
21
22 base::Time GetThresholdTime() {
23 return base::Time::Now() - base::TimeDelta::FromDays(kMaxBookmarkAgeInDays);
24 }
25
26 } // namespace
27
28 namespace ntp_snippets {
29
30 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider(
31 bookmarks::BookmarkModel* bookmark_model)
32 : ContentSuggestionsProvider({ContentSuggestionsCategory::BOOKMARKS}),
33 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING),
34 observer_(nullptr),
35 bookmark_model_(bookmark_model),
36 fetch_requested_(false),
37 end_of_list_last_visit_date_(GetThresholdTime()) {
38 bookmark_model_->AddObserver(this);
39 }
40
41 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() {}
42
43 // Inherited from KeyedService.
44 void BookmarkSuggestionsProvider::Shutdown() {
45 bookmark_model_->RemoveObserver(this);
46 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED;
47 }
48
49 ////////////////////////////////////////////////////////////////////////////////
50 // Private methods
51
52 void BookmarkSuggestionsProvider::SetObserver(
53 ContentSuggestionsProvider::Observer* observer) {
54 observer_ = observer;
55 if (observer)
56 FetchBookmarks();
57 }
58
59 ContentSuggestionsCategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus(
60 ContentSuggestionsCategory category) {
61 return category_status_;
62 }
63
64 void BookmarkSuggestionsProvider::DismissSuggestion(
65 const std::string& suggestion_id) {
66 // TODO(jkrcal): Implement blacklisting bookmarks until they are next visited.
67 // Then also implement ClearDismissedSuggestionsForDebugging.
68 }
69
70 void BookmarkSuggestionsProvider::FetchSuggestionImage(
71 const std::string& suggestion_id,
72 const ImageFetchedCallback& callback) {
73 gfx::Image image;
Bernhard Bauer 2016/07/29 14:32:42 Just inline this.
jkrcal 2016/07/29 16:09:56 Done.
74 callback.Run(suggestion_id, image);
75 }
76
77 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging() {
78 // Ignored.
79 }
80
81 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging() {
82 // TODO(jkrcal): Implement when discarded suggestions are supported.
83 }
84
85 void BookmarkSuggestionsProvider::BookmarkModelLoaded(
86 bookmarks::BookmarkModel* model,
87 bool ids_reassigned) {
88 DCHECK_EQ(bookmark_model_, model);
89 if (fetch_requested_) {
90 fetch_requested_ = false;
91 FetchBookmarksInternal();
92 }
93 }
94
95 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo(
96 BookmarkModel* model,
97 const BookmarkNode* node) {
98 // Store the last visit date of the node that is about to change.
99 node_to_change_last_visit_date_ = GetLastVisitDateForBookmark(node);
100 }
101
102 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged(
103 BookmarkModel* model,
104 const BookmarkNode* node) {
105 base::Time time = GetLastVisitDateForBookmark(node);
106 if (time == node_to_change_last_visit_date_ ||
107 time < end_of_list_last_visit_date_)
108 return;
109
110 // Last visit date of a node has changed (and is relevant for the list), we
111 // should update the suggestions.
112 FetchBookmarks();
113 }
114
115 void BookmarkSuggestionsProvider::FetchBookmarksInternal() {
116 DCHECK(bookmark_model_->loaded());
117
118 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE);
119 if (!observer_)
120 return;
121
122 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks(
123 bookmark_model_, kMaxBookmarks, GetThresholdTime());
124
125 std::vector<ContentSuggestion> suggestions;
126 for (const BookmarkNode* bookmark : bookmarks) {
127 ContentSuggestion suggestion(
128 MakeUniqueID(ContentSuggestionsCategory::BOOKMARKS,
129 bookmark->url().spec()),
130 bookmark->url());
131
132 // TODO(jkrcal): keep it in UTF16 when ContentSuggestion title adapts to it.
133 suggestion.set_title(base::UTF16ToUTF8(bookmark->GetTitle()));
134 suggestion.set_snippet_text(std::string());
135 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark));
136 suggestion.set_publisher_name(bookmark->url().host());
137 suggestions.emplace_back(std::move(suggestion));
138 }
139
140 if (suggestions.empty())
141 end_of_list_last_visit_date_ = GetThresholdTime();
142 else
143 end_of_list_last_visit_date_ = suggestions.back().publish_date();
144
145 observer_->OnNewSuggestions(ContentSuggestionsCategory::BOOKMARKS,
146 std::move(suggestions));
147 }
148
149 void BookmarkSuggestionsProvider::FetchBookmarks() {
150 if (bookmark_model_->loaded())
151 FetchBookmarksInternal();
152 else
153 fetch_requested_ = true;
154 }
155
156 void BookmarkSuggestionsProvider::NotifyStatusChanged(
157 ContentSuggestionsCategoryStatus new_status) {
158 if (category_status_ == new_status)
159 return;
160 category_status_ = new_status;
161
162 if (!observer_)
163 return;
164 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::BOOKMARKS,
165 new_status);
166 }
167
168 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698