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..cb2da40ad4295c81553698c29fcf3965ac8d129c 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; |
| @@ -50,9 +51,8 @@ void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, |
| // If there are bookmarks for |url|, set their last visit date to now. |
| std::string now = FormatLastVisitDate(base::Time::Now()); |
| - for (auto* node : bookmarks_for_url) { |
| + for (auto* node : bookmarks_for_url) |
| bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); |
| - } |
| } |
| base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { |
| @@ -62,23 +62,55 @@ base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { |
| std::string last_visit_date_string; |
| node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); |
| - if (last_visit_date_string.empty()) { |
| - // Use creation date if no last visit info present. |
| + // Use creation date if no last visit info present. |
| + if (last_visit_date_string.empty()) |
| return node->date_added(); |
| - } |
| 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) |
| + bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
| +} |
| + |
| +bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { |
| + if (!node) |
| + return false; |
| + |
| + std::string dismissed_from_ntp; |
| + bool result = |
| + node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); |
| + DCHECK(!result || dismissed_from_ntp == "1"); |
|
Marc Treib
2016/08/08 10:04:50
nit: DCHECK_IF(result, dismissed_from_ntp == "1");
Philipp Keck
2016/08/08 10:15:20
Done.
|
| + return result; |
| +} |
| + |
| +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 +120,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. |
|
Marc Treib
2016/08/08 10:04:50
Just "and", not "and/or" (?)
Philipp Keck
2016/08/08 10:15:21
Changed the comment into two parts, bc it doesn't
|
| + bool none_recently_visited = true; |
|
Marc Treib
2016/08/08 10:04:50
There's still a negation here. Make this "has_rece
Philipp Keck
2016/08/08 10:15:20
Also good. Done.
|
| + bool all_dismissed = true; |
| for (const auto* node : bookmarks_for_url) { |
| if (GetLastVisitDateForBookmark(node) > min_visit_time) |
| - return false; |
| + none_recently_visited = false; |
| + if (!IsDismissedFromNTPForBookmark(node)) |
| + all_dismissed = false; |
| } |
| - return true; |
| + return all_dismissed || none_recently_visited; |
| }), |
| bookmarks.end()); |