Chromium Code Reviews| Index: components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
| diff --git a/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc b/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
| index 2fd01957986b9a85624f3b36041674331eae7b7d..1079bec80419101c37041cce512dbb578f4c8895 100644 |
| --- a/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
| +++ b/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
| @@ -22,6 +22,7 @@ namespace ntp_snippets { |
| namespace { |
| const char kBookmarkLastVisitDateKey[] = "last_visited"; |
| +const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; |
| base::Time ParseLastVisitDate(const std::string& date_string) { |
| int64_t date = 0; |
| @@ -70,15 +71,48 @@ base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { |
| return ParseLastVisitDate(last_visit_date_string); |
| } |
| +void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
| + std::vector<const BookmarkNode*> nodes; |
| + bookmark_model->GetNodesByURL(url, &nodes); |
| + for (const BookmarkNode* node : nodes) { |
|
Marc Treib
2016/08/08 09:10:46
nit: braces around single-line bodies or not?
Eith
Philipp Keck
2016/08/08 09:42:25
Done.
|
| + bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
| + } |
| +} |
| + |
| +bool GetDismissedFromNTPForBookmark(const BookmarkNode* node) { |
| + if (!node) |
| + return false; |
| + |
| + std::string dismissed_from_ntp; |
| + return node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp) && |
| + dismissed_from_ntp == "1"; |
|
Marc Treib
2016/08/08 09:10:46
If the meta info exists at all, it should never ha
Philipp Keck
2016/08/08 09:42:25
Done.
|
| +} |
| + |
| +void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { |
| + // Get all the bookmark URLs. |
| + std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| + bookmark_model->GetBookmarks(&bookmarks); |
| + |
| + // Remove dismissed flag from all bookmarks |
| + for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| + std::vector<const BookmarkNode*> nodes; |
| + bookmark_model->GetNodesByURL(bookmark.url, &nodes); |
| + for (const BookmarkNode* node : nodes) { |
| + bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
| + } |
| + } |
| +} |
| + |
| std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
| BookmarkModel* bookmark_model, |
| int max_count, |
| const base::Time& min_visit_time) { |
| - // Get all the bookmarks. |
| + // Get all the bookmark URLs. |
| std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| bookmark_model->GetBookmarks(&bookmarks); |
| - // Remove bookmarks that have not been visited after |min_visit_time|. |
| + // Remove the bookmark URLs whose bookmarks are all dismissed or whose most |
| + // recent visit is older than |min_visit_time|. |
| bookmarks.erase( |
| std::remove_if(bookmarks.begin(), bookmarks.end(), |
| [&bookmark_model, &min_visit_time]( |
| @@ -88,12 +122,16 @@ std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
| bookmark_model->GetNodesByURL(bookmark.url, |
| &bookmarks_for_url); |
| - // Return false if there is a more recent visit time. |
| + // Check if there is any recent and/or non-dismissed one. |
| + bool has_recent = false; |
| + bool has_non_dismissed = false; |
| for (const auto* node : bookmarks_for_url) { |
| if (GetLastVisitDateForBookmark(node) > min_visit_time) |
| - return false; |
| + has_recent = true; |
| + if (!GetDismissedFromNTPForBookmark(node)) |
| + has_non_dismissed = true; |
| } |
| - return true; |
| + return has_recent && has_non_dismissed; |
|
Marc Treib
2016/08/08 09:10:46
This is the wrong way around - you want to remove
Philipp Keck
2016/08/08 09:42:25
Right. I changed it around, trying to get rid of a
|
| }), |
| bookmarks.end()); |