OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/bookmarks/bookmark_suggestions_provider.h" | 5 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 169 |
170 void BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging( | 170 void BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging( |
171 Category category, | 171 Category category, |
172 const DismissedSuggestionsCallback& callback) { | 172 const DismissedSuggestionsCallback& callback) { |
173 DCHECK_EQ(category, provided_category_); | 173 DCHECK_EQ(category, provided_category_); |
174 std::vector<const BookmarkNode*> bookmarks = | 174 std::vector<const BookmarkNode*> bookmarks = |
175 GetDismissedBookmarksForDebugging(bookmark_model_); | 175 GetDismissedBookmarksForDebugging(bookmark_model_); |
176 | 176 |
177 std::vector<ContentSuggestion> suggestions; | 177 std::vector<ContentSuggestion> suggestions; |
178 for (const BookmarkNode* bookmark : bookmarks) { | 178 for (const BookmarkNode* bookmark : bookmarks) { |
179 ConvertBookmark(bookmark, &suggestions); | 179 ConvertBookmark(*bookmark, &suggestions); |
180 } | 180 } |
181 callback.Run(std::move(suggestions)); | 181 callback.Run(std::move(suggestions)); |
182 } | 182 } |
183 | 183 |
184 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | 184 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
185 Category category) { | 185 Category category) { |
186 DCHECK_EQ(category, provided_category_); | 186 DCHECK_EQ(category, provided_category_); |
187 if (!bookmark_model_->loaded()) { | 187 if (!bookmark_model_->loaded()) { |
188 return; | 188 return; |
189 } | 189 } |
190 MarkAllBookmarksUndismissed(bookmark_model_); | 190 MarkAllBookmarksUndismissed(bookmark_model_); |
191 } | 191 } |
192 | 192 |
193 void BookmarkSuggestionsProvider::BookmarkModelLoaded( | 193 void BookmarkSuggestionsProvider::BookmarkModelLoaded( |
194 bookmarks::BookmarkModel* model, | 194 bookmarks::BookmarkModel* model, |
195 bool ids_reassigned) { | 195 bool ids_reassigned) { |
196 DCHECK_EQ(bookmark_model_, model); | 196 DCHECK_EQ(bookmark_model_, model); |
197 if (fetch_requested_) { | 197 if (fetch_requested_) { |
198 fetch_requested_ = false; | 198 fetch_requested_ = false; |
199 FetchBookmarksInternal(); | 199 FetchBookmarksInternal(); |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | 203 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
204 BookmarkModel* model, | 204 BookmarkModel* model, |
205 const BookmarkNode* node) { | 205 const BookmarkNode* node) { |
206 // Store the last visit date of the node that is about to change. | 206 // Store the last visit date of the node that is about to change. |
207 if (!GetLastVisitDateForNTPBookmark(node, | 207 if (!GetLastVisitDateForNTPBookmark(*node, |
208 consider_bookmark_visits_from_desktop_, | 208 consider_bookmark_visits_from_desktop_, |
209 &node_to_change_last_visit_date_)) { | 209 &node_to_change_last_visit_date_)) { |
210 node_to_change_last_visit_date_ = base::Time::UnixEpoch(); | 210 node_to_change_last_visit_date_ = base::Time::UnixEpoch(); |
211 } | 211 } |
212 } | 212 } |
213 | 213 |
214 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | 214 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
215 BookmarkModel* model, | 215 BookmarkModel* model, |
216 const BookmarkNode* node) { | 216 const BookmarkNode* node) { |
217 base::Time time; | 217 base::Time time; |
218 if (!GetLastVisitDateForNTPBookmark( | 218 if (!GetLastVisitDateForNTPBookmark( |
219 node, consider_bookmark_visits_from_desktop_, &time)) { | 219 *node, consider_bookmark_visits_from_desktop_, &time)) { |
220 // Error in loading the last visit date after the change. This happens when | 220 // Error in loading the last visit date after the change. This happens when |
221 // the bookmark just got dismissed. We must not update the suggestion in | 221 // the bookmark just got dismissed. We must not update the suggestion in |
222 // such a case. | 222 // such a case. |
223 return; | 223 return; |
224 } | 224 } |
225 | 225 |
226 if (time == node_to_change_last_visit_date_ || | 226 if (time == node_to_change_last_visit_date_ || |
227 time < end_of_list_last_visit_date_) { | 227 time < end_of_list_last_visit_date_) { |
228 // The last visit date has not changed or the change is irrelevant. | 228 // The last visit date has not changed or the change is irrelevant. |
229 return; | 229 return; |
230 } | 230 } |
231 | 231 |
232 // Otherwise, we should update the suggestions. | 232 // Otherwise, we should update the suggestions. |
233 FetchBookmarks(); | 233 FetchBookmarks(); |
234 } | 234 } |
235 | 235 |
236 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( | 236 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( |
237 bookmarks::BookmarkModel* model, | 237 bookmarks::BookmarkModel* model, |
238 const bookmarks::BookmarkNode* parent, | 238 const bookmarks::BookmarkNode* parent, |
239 int old_index, | 239 int old_index, |
240 const bookmarks::BookmarkNode* node, | 240 const bookmarks::BookmarkNode* node, |
241 const std::set<GURL>& no_longer_bookmarked) { | 241 const std::set<GURL>& no_longer_bookmarked) { |
242 base::Time time; | 242 base::Time time; |
243 if (GetLastVisitDateForNTPBookmark( | 243 if (GetLastVisitDateForNTPBookmark( |
244 node, consider_bookmark_visits_from_desktop_, &time) && | 244 *node, consider_bookmark_visits_from_desktop_, &time) && |
245 time < end_of_list_last_visit_date_) { | 245 time < end_of_list_last_visit_date_) { |
246 // We know the node is too old to influence the list. | 246 // We know the node is too old to influence the list. |
247 return; | 247 return; |
248 } | 248 } |
249 | 249 |
250 // Some node from our list got deleted, we should update the suggestions. | 250 // Some node from our list got deleted, we should update the suggestions. |
251 FetchBookmarks(); | 251 FetchBookmarks(); |
252 } | 252 } |
253 | 253 |
254 void BookmarkSuggestionsProvider::BookmarkNodeAdded( | 254 void BookmarkSuggestionsProvider::BookmarkNodeAdded( |
255 bookmarks::BookmarkModel* model, | 255 bookmarks::BookmarkModel* model, |
256 const bookmarks::BookmarkNode* parent, | 256 const bookmarks::BookmarkNode* parent, |
257 int index) { | 257 int index) { |
258 base::Time time; | 258 base::Time time; |
259 if (!GetLastVisitDateForNTPBookmark(parent->GetChild(index), | 259 if (!GetLastVisitDateForNTPBookmark(*parent->GetChild(index), |
260 consider_bookmark_visits_from_desktop_, | 260 consider_bookmark_visits_from_desktop_, |
261 &time) || | 261 &time) || |
262 time < end_of_list_last_visit_date_) { | 262 time < end_of_list_last_visit_date_) { |
263 // The new node has no last visited info or is too old to get into the list. | 263 // The new node has no last visited info or is too old to get into the list. |
264 return; | 264 return; |
265 } | 265 } |
266 | 266 |
267 // Some relevant node got created (e.g. by sync), we should update the list. | 267 // Some relevant node got created (e.g. by sync), we should update the list. |
268 FetchBookmarks(); | 268 FetchBookmarks(); |
269 } | 269 } |
270 | 270 |
271 void BookmarkSuggestionsProvider::ConvertBookmark( | 271 void BookmarkSuggestionsProvider::ConvertBookmark( |
272 const BookmarkNode* bookmark, | 272 const BookmarkNode& bookmark, |
273 std::vector<ContentSuggestion>* suggestions) { | 273 std::vector<ContentSuggestion>* suggestions) { |
274 base::Time publish_date; | 274 base::Time publish_date; |
275 if (!GetLastVisitDateForNTPBookmark( | 275 if (!GetLastVisitDateForNTPBookmark( |
276 bookmark, consider_bookmark_visits_from_desktop_, &publish_date)) { | 276 bookmark, consider_bookmark_visits_from_desktop_, &publish_date)) { |
277 return; | 277 return; |
278 } | 278 } |
279 | 279 |
280 ContentSuggestion suggestion(provided_category_, bookmark->url().spec(), | 280 ContentSuggestion suggestion(provided_category_, bookmark.url().spec(), |
281 bookmark->url()); | 281 bookmark.url()); |
282 suggestion.set_title(bookmark->GetTitle()); | 282 suggestion.set_title(bookmark.GetTitle()); |
283 suggestion.set_snippet_text(base::string16()); | 283 suggestion.set_snippet_text(base::string16()); |
284 suggestion.set_publish_date(publish_date); | 284 suggestion.set_publish_date(publish_date); |
285 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | 285 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark.url().host())); |
286 | 286 |
287 suggestions->emplace_back(std::move(suggestion)); | 287 suggestions->emplace_back(std::move(suggestion)); |
288 } | 288 } |
289 | 289 |
290 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 290 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
291 DCHECK(bookmark_model_->loaded()); | 291 DCHECK(bookmark_model_->loaded()); |
292 | 292 |
293 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 293 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
294 | 294 |
295 base::Time threshold_time = GetThresholdTime(); | 295 base::Time threshold_time = GetThresholdTime(); |
296 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | 296 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( |
297 bookmark_model_, GetMaxCount(), threshold_time, | 297 bookmark_model_, GetMaxCount(), threshold_time, |
298 consider_bookmark_visits_from_desktop_); | 298 consider_bookmark_visits_from_desktop_); |
299 | 299 |
300 std::vector<ContentSuggestion> suggestions; | 300 std::vector<ContentSuggestion> suggestions; |
301 for (const BookmarkNode* bookmark : bookmarks) { | 301 for (const BookmarkNode* bookmark : bookmarks) { |
302 ConvertBookmark(bookmark, &suggestions); | 302 ConvertBookmark(*bookmark, &suggestions); |
303 } | 303 } |
304 | 304 |
305 if (suggestions.empty()) { | 305 if (suggestions.empty()) { |
306 end_of_list_last_visit_date_ = threshold_time; | 306 end_of_list_last_visit_date_ = threshold_time; |
307 } else { | 307 } else { |
308 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | 308 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
309 } | 309 } |
310 | 310 |
311 observer()->OnNewSuggestions(this, provided_category_, | 311 observer()->OnNewSuggestions(this, provided_category_, |
312 std::move(suggestions)); | 312 std::move(suggestions)); |
(...skipping 10 matching lines...) Expand all Loading... |
323 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 323 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
324 CategoryStatus new_status) { | 324 CategoryStatus new_status) { |
325 if (category_status_ == new_status) { | 325 if (category_status_ == new_status) { |
326 return; | 326 return; |
327 } | 327 } |
328 category_status_ = new_status; | 328 category_status_ = new_status; |
329 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 329 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
330 } | 330 } |
331 | 331 |
332 } // namespace ntp_snippets | 332 } // namespace ntp_snippets |
OLD | NEW |