OLD | NEW |
---|---|
(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_date_helper.h" | |
13 #include "components/ntp_snippets/content_suggestion.h" | |
14 | |
15 namespace { | |
16 const int kMaxBookmarks = 10; | |
17 const int kMaxBookmarkAgeInDays = 42; | |
18 | |
19 base::Time GetThresholdTime() { | |
20 return base::Time::Now() - base::TimeDelta::FromDays(kMaxBookmarkAgeInDays); | |
21 } | |
22 | |
23 } // namespace | |
24 | |
25 namespace ntp_snippets { | |
26 | |
27 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | |
28 bookmarks::BookmarkModel* bookmark_model) | |
29 : ContentSuggestionsProvider({ContentSuggestionsCategory::BOOKMARKS}), | |
30 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), | |
31 observer_(nullptr), | |
32 bookmark_model_(bookmark_model), | |
33 fetch_requested_(false), | |
34 end_of_list_last_visit_date_(GetThresholdTime()) { | |
35 bookmark_model_->AddObserver(this); | |
36 } | |
37 | |
38 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() {} | |
39 | |
40 // Inherited from KeyedService. | |
41 void BookmarkSuggestionsProvider::Shutdown() { | |
42 bookmark_model_->RemoveObserver(this); | |
43 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED; | |
44 } | |
45 | |
46 //////////////////////////////////////////////////////////////////////////////// | |
47 // Private methods | |
48 | |
49 void BookmarkSuggestionsProvider::SetObserver( | |
50 ContentSuggestionsProvider::Observer* observer) { | |
51 observer_ = observer; | |
52 if (observer) | |
53 FetchBookmarks(); | |
54 } | |
55 | |
56 ContentSuggestionsCategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | |
57 ContentSuggestionsCategory category) { | |
58 return category_status_; | |
59 } | |
60 | |
61 void BookmarkSuggestionsProvider::DismissSuggestion( | |
62 const std::string& suggestion_id) { | |
63 // TODO(jkrcal): Implement blacklisting bookmarks until they are next visited. | |
64 // Then also implement ClearDismissedSuggestionsForDebugging. | |
65 } | |
66 | |
67 void BookmarkSuggestionsProvider::FetchSuggestionImage( | |
68 const std::string& suggestion_id, | |
69 const ImageFetchedCallback& callback) { | |
70 // TODO(jkrcal): Make sure no image is displayed in the UI. | |
Marc Treib
2016/07/29 10:08:34
Might be nice to call the callback with a "null" i
jkrcal
2016/07/29 13:57:17
Done.
| |
71 } | |
72 | |
73 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging() { | |
74 // Ignored. | |
75 } | |
76 | |
77 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging() { | |
78 // TODO(jkrcal): Implement when discarded suggestions are supported. | |
79 } | |
80 | |
81 void BookmarkSuggestionsProvider::BookmarkModelLoaded( | |
82 bookmarks::BookmarkModel* model, | |
83 bool ids_reassigned) { | |
84 DCHECK_EQ(bookmark_model_, model); | |
85 if (fetch_requested_) { | |
86 fetch_requested_ = false; | |
87 FetchBookmarksWhenLoaded(); | |
88 } | |
89 } | |
90 | |
91 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | |
92 BookmarkModel* model, | |
93 const BookmarkNode* node) { | |
94 // Store the last visit date of the node that is about to change. | |
95 node_to_change_last_visit_date_ = | |
96 BookmarkLastVisitDateHelper::GetLastVisitDate(node); | |
97 } | |
98 | |
99 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | |
100 BookmarkModel* model, | |
101 const BookmarkNode* node) { | |
102 base::Time time = BookmarkLastVisitDateHelper::GetLastVisitDate(node); | |
103 if (time == node_to_change_last_visit_date_ || | |
104 time < end_of_list_last_visit_date_) | |
105 return; | |
106 | |
107 // Last visit date of a node has changed (and is relevant for the list), we | |
108 // should update the suggestions. | |
109 FetchBookmarks(); | |
110 } | |
111 | |
112 void BookmarkSuggestionsProvider::FetchBookmarksWhenLoaded() { | |
113 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); | |
114 if (!observer_) | |
115 return; | |
116 | |
117 std::vector<const BookmarkNode*> bookmarks; | |
118 BookmarkLastVisitDateHelper::GetRecentlyVisitedBookmarks( | |
119 bookmark_model_, &bookmarks, kMaxBookmarks, | |
120 GetThresholdTime()); | |
121 | |
122 std::vector<ContentSuggestion> suggestions; | |
123 for (const BookmarkNode* bookmark : bookmarks) { | |
124 ContentSuggestion suggestion( | |
125 MakeUniqueID(ContentSuggestionsCategory::BOOKMARKS, | |
126 bookmark->url().spec()), | |
127 bookmark->url()); | |
128 | |
129 // TODO(jkrcal): keep it in UTF16 when ContentSuggestion title adapts to it. | |
130 suggestion.set_title(base::UTF16ToUTF8(bookmark->GetTitle())); | |
131 suggestion.set_snippet_text(std::string()); | |
132 suggestion.set_publish_date( | |
133 BookmarkLastVisitDateHelper::GetLastVisitDate(bookmark)); | |
134 suggestion.set_publisher_name(bookmark->url().host()); | |
135 suggestions.emplace_back(std::move(suggestion)); | |
136 } | |
137 | |
138 if (suggestions.empty()) | |
139 end_of_list_last_visit_date_ = GetThresholdTime(); | |
140 else | |
141 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | |
142 | |
143 observer_->OnNewSuggestions(ContentSuggestionsCategory::BOOKMARKS, | |
144 std::move(suggestions)); | |
145 } | |
146 | |
147 void BookmarkSuggestionsProvider::FetchBookmarks() { | |
148 if (bookmark_model_->loaded()) | |
149 FetchBookmarksWhenLoaded(); | |
150 else | |
151 fetch_requested_ = true; | |
152 } | |
153 | |
154 void BookmarkSuggestionsProvider::NotifyStatusChanged( | |
155 ContentSuggestionsCategoryStatus new_status) { | |
156 if (category_status_ == new_status) | |
157 return; | |
158 category_status_ = new_status; | |
159 | |
160 if (!observer_) | |
161 return; | |
162 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::BOOKMARKS, | |
163 new_status); | |
164 } | |
165 | |
166 } // namespace ntp_snippets | |
OLD | NEW |