| 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..77f659d17d4ca7628d42ed3eb3d931dbd504dc1a 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 (const BookmarkNode* 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");
|
| + 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,17 @@ std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
|
| bookmark_model->GetNodesByURL(bookmark.url,
|
| &bookmarks_for_url);
|
|
|
| - // Return false if there is a more recent visit time.
|
| - for (const auto* node : bookmarks_for_url) {
|
| + // Check if there is a recently visited bookmark and not
|
| + // all bookmarks are dismissed.
|
| + bool has_recent_visit = false;
|
| + bool all_dismissed = true;
|
| + for (const BookmarkNode* node : bookmarks_for_url) {
|
| if (GetLastVisitDateForBookmark(node) > min_visit_time)
|
| - return false;
|
| + has_recent_visit = true;
|
| + if (!IsDismissedFromNTPForBookmark(node))
|
| + all_dismissed = false;
|
| }
|
| - return true;
|
| + return all_dismissed || !has_recent_visit;
|
| }),
|
| bookmarks.end());
|
|
|
| @@ -117,4 +154,35 @@ std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
|
| return result;
|
| }
|
|
|
| +std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging(
|
| + BookmarkModel* bookmark_model) {
|
| + // Get all the bookmark URLs.
|
| + std::vector<BookmarkModel::URLAndTitle> bookmarks;
|
| + bookmark_model->GetBookmarks(&bookmarks);
|
| +
|
| + // Remove the bookmark URLs which have at least one non-dismissed bookmark.
|
| + bookmarks.erase(
|
| + std::remove_if(
|
| + bookmarks.begin(), bookmarks.end(),
|
| + [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) {
|
| + std::vector<const BookmarkNode*> bookmarks_for_url;
|
| + bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url);
|
| +
|
| + for (const BookmarkNode* node : bookmarks_for_url) {
|
| + if (!IsDismissedFromNTPForBookmark(node))
|
| + return true;
|
| + }
|
| + return false;
|
| + }),
|
| + bookmarks.end());
|
| +
|
| + // Insert into |result|.
|
| + std::vector<const BookmarkNode*> result;
|
| + for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
|
| + result.push_back(
|
| + bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url));
|
| + }
|
| + return result;
|
| +}
|
| +
|
| } // namespace ntp_snippets
|
|
|