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

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

Issue 2214683002: [New CL] Add bookmark provider for content suggestions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bookmarks
Patch Set: Insert a blank line 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/category_factory.h"
14 #include "components/ntp_snippets/content_suggestion.h"
15
16 using bookmarks::BookmarkModel;
17 using bookmarks::BookmarkNode;
18
19 namespace {
20
21 const int kMaxBookmarks = 10;
22 const int kMaxBookmarkAgeInDays = 42;
23
24 base::Time GetThresholdTime() {
25 return base::Time::Now() - base::TimeDelta::FromDays(kMaxBookmarkAgeInDays);
26 }
27
28 } // namespace
29
30 namespace ntp_snippets {
31
32 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider(
33 ContentSuggestionsProvider::Observer* observer,
34 CategoryFactory* category_factory,
35 bookmarks::BookmarkModel* bookmark_model)
36 : ContentSuggestionsProvider(observer, category_factory),
37 category_status_(CategoryStatus::AVAILABLE_LOADING),
38 provided_category_(
39 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)),
40 bookmark_model_(bookmark_model),
41 fetch_requested_(false),
42 end_of_list_last_visit_date_(GetThresholdTime()) {
43 bookmark_model_->AddObserver(this);
44 FetchBookmarks();
45 }
46
47 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() {
48 bookmark_model_->RemoveObserver(this);
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52 // Private methods
53
54 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() {
55 return std::vector<Category>({provided_category_});
56 }
57
58 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus(
59 Category category) {
60 return category_status_;
61 }
62
63 void BookmarkSuggestionsProvider::DismissSuggestion(
64 const std::string& suggestion_id) {
65 // TODO(jkrcal): Implement blacklisting bookmarks until they are next visited.
66 // Then also implement ClearDismissedSuggestionsForDebugging.
67 }
68
69 void BookmarkSuggestionsProvider::FetchSuggestionImage(
70 const std::string& suggestion_id,
71 const ImageFetchedCallback& callback) {
72 callback.Run(suggestion_id, gfx::Image());
73 }
74
75 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging() {
76 // Ignored.
77 }
78
79 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging() {
80 // TODO(jkrcal): Implement when discarded suggestions are supported.
81 }
82
83 void BookmarkSuggestionsProvider::BookmarkModelLoaded(
84 bookmarks::BookmarkModel* model,
85 bool ids_reassigned) {
86 DCHECK_EQ(bookmark_model_, model);
87 if (fetch_requested_) {
88 fetch_requested_ = false;
89 FetchBookmarksInternal();
90 }
91 }
92
93 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo(
94 BookmarkModel* model,
95 const BookmarkNode* node) {
96 // Store the last visit date of the node that is about to change.
97 node_to_change_last_visit_date_ = GetLastVisitDateForBookmark(node);
98 }
99
100 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged(
101 BookmarkModel* model,
102 const BookmarkNode* node) {
103 base::Time time = GetLastVisitDateForBookmark(node);
104 if (time == node_to_change_last_visit_date_ ||
105 time < end_of_list_last_visit_date_)
106 return;
107
108 // Last visit date of a node has changed (and is relevant for the list), we
109 // should update the suggestions.
110 FetchBookmarks();
111 }
112
113 void BookmarkSuggestionsProvider::FetchBookmarksInternal() {
114 DCHECK(bookmark_model_->loaded());
115
116 NotifyStatusChanged(CategoryStatus::AVAILABLE);
117
118 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks(
119 bookmark_model_, kMaxBookmarks, GetThresholdTime());
120
121 std::vector<ContentSuggestion> suggestions;
122 for (const BookmarkNode* bookmark : bookmarks) {
123 ContentSuggestion suggestion(
124 MakeUniqueID(provided_category_, bookmark->url().spec()),
125 bookmark->url());
126
127 suggestion.set_title(bookmark->GetTitle());
128 suggestion.set_snippet_text(base::string16());
129 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark));
130 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host()));
131 suggestions.emplace_back(std::move(suggestion));
132 }
133
134 if (suggestions.empty())
135 end_of_list_last_visit_date_ = GetThresholdTime();
136 else
137 end_of_list_last_visit_date_ = suggestions.back().publish_date();
138
139 observer()->OnNewSuggestions(this, provided_category_,
140 std::move(suggestions));
141 }
142
143 void BookmarkSuggestionsProvider::FetchBookmarks() {
144 if (bookmark_model_->loaded())
145 FetchBookmarksInternal();
146 else
147 fetch_requested_ = true;
148 }
149
150 void BookmarkSuggestionsProvider::NotifyStatusChanged(
151 CategoryStatus new_status) {
152 if (category_status_ == new_status)
153 return;
154 category_status_ = new_status;
155 observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
156 }
157
158 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h ('k') | components/ntp_snippets/category.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698