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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 | 202 |
203 void BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging( | 203 void BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging( |
204 Category category, | 204 Category category, |
205 const DismissedSuggestionsCallback& callback) { | 205 const DismissedSuggestionsCallback& callback) { |
206 DCHECK_EQ(category, provided_category_); | 206 DCHECK_EQ(category, provided_category_); |
207 std::vector<const BookmarkNode*> bookmarks = | 207 std::vector<const BookmarkNode*> bookmarks = |
208 GetDismissedBookmarksForDebugging(bookmark_model_); | 208 GetDismissedBookmarksForDebugging(bookmark_model_); |
209 | 209 |
210 std::vector<ContentSuggestion> suggestions; | 210 std::vector<ContentSuggestion> suggestions; |
211 for (const BookmarkNode* bookmark : bookmarks) { | 211 for (const BookmarkNode* bookmark : bookmarks) { |
212 ConvertBookmark(bookmark, &suggestions); | 212 ConvertBookmark(*bookmark, &suggestions); |
213 } | 213 } |
214 callback.Run(std::move(suggestions)); | 214 callback.Run(std::move(suggestions)); |
215 } | 215 } |
216 | 216 |
217 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | 217 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
218 Category category) { | 218 Category category) { |
219 DCHECK_EQ(category, provided_category_); | 219 DCHECK_EQ(category, provided_category_); |
220 if (!bookmark_model_->loaded()) { | 220 if (!bookmark_model_->loaded()) { |
221 return; | 221 return; |
222 } | 222 } |
223 MarkAllBookmarksUndismissed(bookmark_model_); | 223 MarkAllBookmarksUndismissed(bookmark_model_); |
224 } | 224 } |
225 | 225 |
226 void BookmarkSuggestionsProvider::BookmarkModelLoaded( | 226 void BookmarkSuggestionsProvider::BookmarkModelLoaded( |
227 bookmarks::BookmarkModel* model, | 227 bookmarks::BookmarkModel* model, |
228 bool ids_reassigned) { | 228 bool ids_reassigned) { |
229 DCHECK_EQ(bookmark_model_, model); | 229 DCHECK_EQ(bookmark_model_, model); |
230 if (fetch_requested_) { | 230 if (fetch_requested_) { |
231 fetch_requested_ = false; | 231 fetch_requested_ = false; |
232 FetchBookmarksInternal(); | 232 FetchBookmarksInternal(); |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | 236 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( |
237 BookmarkModel* model, | 237 BookmarkModel* model, |
238 const BookmarkNode* node) { | 238 const BookmarkNode* node) { |
239 // Store the last visit date of the node that is about to change. | 239 // Store the last visit date of the node that is about to change. |
240 if (!GetLastVisitDateForNTPBookmark(node, creation_date_fallback_, | 240 if (!GetLastVisitDateForNTPBookmark(*node, creation_date_fallback_, |
tschumann
2016/11/25 13:47:43
is |node| guaranteed to be non-null here and in Bo
jkrcal
2016/11/25 14:05:36
I am not sure. I see no info on this in BookmarkMo
sky
2016/11/28 16:25:09
They will never be null. I agree a const BookmarkN
| |
241 consider_bookmark_visits_from_desktop_, | 241 consider_bookmark_visits_from_desktop_, |
242 &node_to_change_last_visit_date_)) { | 242 &node_to_change_last_visit_date_)) { |
243 node_to_change_last_visit_date_ = base::Time::UnixEpoch(); | 243 node_to_change_last_visit_date_ = base::Time::UnixEpoch(); |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | 247 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( |
248 BookmarkModel* model, | 248 BookmarkModel* model, |
249 const BookmarkNode* node) { | 249 const BookmarkNode* node) { |
250 base::Time time; | 250 base::Time time; |
251 if (!GetLastVisitDateForNTPBookmark(node, creation_date_fallback_, | 251 if (!GetLastVisitDateForNTPBookmark(*node, creation_date_fallback_, |
252 consider_bookmark_visits_from_desktop_, | 252 consider_bookmark_visits_from_desktop_, |
253 &time)) { | 253 &time)) { |
254 // Error in loading the last visit date after the change. This happens when | 254 // Error in loading the last visit date after the change. This happens when |
255 // the bookmark just got dismissed. We must not update the suggestion in | 255 // the bookmark just got dismissed. We must not update the suggestion in |
256 // such a case. | 256 // such a case. |
257 return; | 257 return; |
258 } | 258 } |
259 | 259 |
260 if (time == node_to_change_last_visit_date_ || | 260 if (time == node_to_change_last_visit_date_ || |
261 time < end_of_list_last_visit_date_) { | 261 time < end_of_list_last_visit_date_) { |
262 // The last visit date has not changed or the change is irrelevant. | 262 // The last visit date has not changed or the change is irrelevant. |
263 return; | 263 return; |
264 } | 264 } |
265 | 265 |
266 // Otherwise, we should update the suggestions. | 266 // Otherwise, we should update the suggestions. |
267 FetchBookmarks(); | 267 FetchBookmarks(); |
268 } | 268 } |
269 | 269 |
270 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( | 270 void BookmarkSuggestionsProvider::BookmarkNodeRemoved( |
271 bookmarks::BookmarkModel* model, | 271 bookmarks::BookmarkModel* model, |
272 const bookmarks::BookmarkNode* parent, | 272 const bookmarks::BookmarkNode* parent, |
273 int old_index, | 273 int old_index, |
274 const bookmarks::BookmarkNode* node, | 274 const bookmarks::BookmarkNode* node, |
275 const std::set<GURL>& no_longer_bookmarked) { | 275 const std::set<GURL>& no_longer_bookmarked) { |
276 base::Time time; | 276 base::Time time; |
277 if (GetLastVisitDateForNTPBookmark(node, creation_date_fallback_, | 277 if (GetLastVisitDateForNTPBookmark(*node, creation_date_fallback_, |
278 consider_bookmark_visits_from_desktop_, | 278 consider_bookmark_visits_from_desktop_, |
279 &time) && | 279 &time) && |
280 time < end_of_list_last_visit_date_) { | 280 time < end_of_list_last_visit_date_) { |
281 // We know the node is too old to influence the list. | 281 // We know the node is too old to influence the list. |
282 return; | 282 return; |
283 } | 283 } |
284 | 284 |
285 // Some node from our list got deleted, we should update the suggestions. | 285 // Some node from our list got deleted, we should update the suggestions. |
286 FetchBookmarks(); | 286 FetchBookmarks(); |
287 } | 287 } |
288 | 288 |
289 void BookmarkSuggestionsProvider::BookmarkNodeAdded( | 289 void BookmarkSuggestionsProvider::BookmarkNodeAdded( |
290 bookmarks::BookmarkModel* model, | 290 bookmarks::BookmarkModel* model, |
291 const bookmarks::BookmarkNode* parent, | 291 const bookmarks::BookmarkNode* parent, |
292 int index) { | 292 int index) { |
293 base::Time time; | 293 base::Time time; |
294 if (!GetLastVisitDateForNTPBookmark( | 294 if (!GetLastVisitDateForNTPBookmark( |
295 parent->GetChild(index), creation_date_fallback_, | 295 *parent->GetChild(index), creation_date_fallback_, |
296 consider_bookmark_visits_from_desktop_, &time) || | 296 consider_bookmark_visits_from_desktop_, &time) || |
297 time < end_of_list_last_visit_date_) { | 297 time < end_of_list_last_visit_date_) { |
298 // The new node has no last visited info or is too old to get into the list. | 298 // The new node has no last visited info or is too old to get into the list. |
299 return; | 299 return; |
300 } | 300 } |
301 | 301 |
302 // Some relevant node got created (e.g. by sync), we should update the list. | 302 // Some relevant node got created (e.g. by sync), we should update the list. |
303 FetchBookmarks(); | 303 FetchBookmarks(); |
304 } | 304 } |
305 | 305 |
306 void BookmarkSuggestionsProvider::ConvertBookmark( | 306 void BookmarkSuggestionsProvider::ConvertBookmark( |
307 const BookmarkNode* bookmark, | 307 const BookmarkNode& bookmark, |
308 std::vector<ContentSuggestion>* suggestions) { | 308 std::vector<ContentSuggestion>* suggestions) { |
309 base::Time publish_date; | 309 base::Time publish_date; |
310 if (!GetLastVisitDateForNTPBookmark(bookmark, creation_date_fallback_, | 310 if (!GetLastVisitDateForNTPBookmark(bookmark, creation_date_fallback_, |
311 consider_bookmark_visits_from_desktop_, | 311 consider_bookmark_visits_from_desktop_, |
312 &publish_date)) { | 312 &publish_date)) { |
313 return; | 313 return; |
314 } | 314 } |
315 | 315 |
316 ContentSuggestion suggestion(provided_category_, bookmark->url().spec(), | 316 ContentSuggestion suggestion(provided_category_, bookmark.url().spec(), |
317 bookmark->url()); | 317 bookmark.url()); |
318 suggestion.set_title(bookmark->GetTitle()); | 318 suggestion.set_title(bookmark.GetTitle()); |
319 suggestion.set_snippet_text(base::string16()); | 319 suggestion.set_snippet_text(base::string16()); |
320 suggestion.set_publish_date(publish_date); | 320 suggestion.set_publish_date(publish_date); |
321 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | 321 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark.url().host())); |
322 | 322 |
323 suggestions->emplace_back(std::move(suggestion)); | 323 suggestions->emplace_back(std::move(suggestion)); |
324 } | 324 } |
325 | 325 |
326 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | 326 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { |
327 DCHECK(bookmark_model_->loaded()); | 327 DCHECK(bookmark_model_->loaded()); |
328 | 328 |
329 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 329 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
330 | 330 |
331 base::Time threshold_time = GetThresholdTime(); | 331 base::Time threshold_time = GetThresholdTime(); |
332 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | 332 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( |
333 bookmark_model_, GetMinCount(), GetMaxCount(), threshold_time, | 333 bookmark_model_, GetMinCount(), GetMaxCount(), threshold_time, |
334 creation_date_fallback_, consider_bookmark_visits_from_desktop_); | 334 creation_date_fallback_, consider_bookmark_visits_from_desktop_); |
335 | 335 |
336 std::vector<ContentSuggestion> suggestions; | 336 std::vector<ContentSuggestion> suggestions; |
337 for (const BookmarkNode* bookmark : bookmarks) { | 337 for (const BookmarkNode* bookmark : bookmarks) { |
338 ConvertBookmark(bookmark, &suggestions); | 338 ConvertBookmark(*bookmark, &suggestions); |
339 } | 339 } |
340 | 340 |
341 if (suggestions.empty()) { | 341 if (suggestions.empty()) { |
342 end_of_list_last_visit_date_ = threshold_time; | 342 end_of_list_last_visit_date_ = threshold_time; |
343 } else { | 343 } else { |
344 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | 344 end_of_list_last_visit_date_ = suggestions.back().publish_date(); |
345 } | 345 } |
346 | 346 |
347 observer()->OnNewSuggestions(this, provided_category_, | 347 observer()->OnNewSuggestions(this, provided_category_, |
348 std::move(suggestions)); | 348 std::move(suggestions)); |
(...skipping 10 matching lines...) Expand all Loading... | |
359 void BookmarkSuggestionsProvider::NotifyStatusChanged( | 359 void BookmarkSuggestionsProvider::NotifyStatusChanged( |
360 CategoryStatus new_status) { | 360 CategoryStatus new_status) { |
361 if (category_status_ == new_status) { | 361 if (category_status_ == new_status) { |
362 return; | 362 return; |
363 } | 363 } |
364 category_status_ = new_status; | 364 category_status_ = new_status; |
365 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | 365 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
366 } | 366 } |
367 | 367 |
368 } // namespace ntp_snippets | 368 } // namespace ntp_snippets |
OLD | NEW |