Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc

Issue 2241463002: Update bookmark suggestions also after undismiss/delete. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Marc's comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_last_visit_utils.h" 5 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 std::string last_visit_date_string; 62 std::string last_visit_date_string;
63 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); 63 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string);
64 64
65 // Use creation date if no last visit info present. 65 // Use creation date if no last visit info present.
66 if (last_visit_date_string.empty()) 66 if (last_visit_date_string.empty())
67 return node->date_added(); 67 return node->date_added();
68 68
69 return ParseLastVisitDate(last_visit_date_string); 69 return ParseLastVisitDate(last_visit_date_string);
70 } 70 }
71 71
72 base::Time GetLastVisitDateForBookmarkIfNotDismissed(const BookmarkNode* node) {
73 if (IsDismissedFromNTPForBookmark(node))
74 return base::Time::UnixEpoch();
75
76 return GetLastVisitDateForBookmark(node);
77 }
78
72 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { 79 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) {
73 std::vector<const BookmarkNode*> nodes; 80 std::vector<const BookmarkNode*> nodes;
74 bookmark_model->GetNodesByURL(url, &nodes); 81 bookmark_model->GetNodesByURL(url, &nodes);
75 for (const BookmarkNode* node : nodes) 82 for (const BookmarkNode* node : nodes)
76 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); 83 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1");
77 } 84 }
78 85
79 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { 86 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) {
80 if (!node) 87 if (!node)
81 return false; 88 return false;
(...skipping 20 matching lines...) Expand all
102 } 109 }
103 110
104 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( 111 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
105 BookmarkModel* bookmark_model, 112 BookmarkModel* bookmark_model,
106 int max_count, 113 int max_count,
107 const base::Time& min_visit_time) { 114 const base::Time& min_visit_time) {
108 // Get all the bookmark URLs. 115 // Get all the bookmark URLs.
109 std::vector<BookmarkModel::URLAndTitle> bookmarks; 116 std::vector<BookmarkModel::URLAndTitle> bookmarks;
110 bookmark_model->GetBookmarks(&bookmarks); 117 bookmark_model->GetBookmarks(&bookmarks);
111 118
112 // Remove the bookmark URLs whose bookmarks are all dismissed or whose most 119 // Remove the URLs for that no bookmark has a recent visit (more recent than
113 // recent visit is older than |min_visit_time|. 120 // |min_visit_time|). Recent visits to dismissed bookmarks are not considered.
114 bookmarks.erase( 121 bookmarks.erase(
115 std::remove_if(bookmarks.begin(), bookmarks.end(), 122 std::remove_if(bookmarks.begin(), bookmarks.end(),
116 [&bookmark_model, &min_visit_time]( 123 [&bookmark_model, &min_visit_time](
117 const BookmarkModel::URLAndTitle& bookmark) { 124 const BookmarkModel::URLAndTitle& bookmark) {
118 // Get all bookmarks for the given URL. 125 // Get all bookmarks for the given URL.
119 std::vector<const BookmarkNode*> bookmarks_for_url; 126 std::vector<const BookmarkNode*> bookmarks_for_url;
120 bookmark_model->GetNodesByURL(bookmark.url, 127 bookmark_model->GetNodesByURL(bookmark.url,
121 &bookmarks_for_url); 128 &bookmarks_for_url);
122 129
123 // Check if there is a recently visited bookmark and not 130 // Keep if at least one (non-dismissed) bookmark has been
124 // all bookmarks are dismissed. 131 // recently visited.
125 bool has_recent_visit = false;
126 bool all_dismissed = true;
127 for (const BookmarkNode* node : bookmarks_for_url) { 132 for (const BookmarkNode* node : bookmarks_for_url) {
128 if (GetLastVisitDateForBookmark(node) > min_visit_time) 133 if (GetLastVisitDateForBookmarkIfNotDismissed(node) >
129 has_recent_visit = true; 134 min_visit_time)
130 if (!IsDismissedFromNTPForBookmark(node)) 135 return false;
131 all_dismissed = false;
132 } 136 }
133 return all_dismissed || !has_recent_visit; 137 // Otherwise erase this URL.
138 return true;
134 }), 139 }),
135 bookmarks.end()); 140 bookmarks.end());
136 141
137 // Sort the remaining bookmarks by date. 142 // Sort the remaining bookmarks by date.
138 std::sort(bookmarks.begin(), bookmarks.end(), 143 std::sort(bookmarks.begin(), bookmarks.end(),
139 [&bookmark_model](const BookmarkModel::URLAndTitle& a, 144 [&bookmark_model](const BookmarkModel::URLAndTitle& a,
140 const BookmarkModel::URLAndTitle& b) { 145 const BookmarkModel::URLAndTitle& b) {
141 return CompareBookmarksByLastVisitDate( 146 return CompareBookmarksByLastVisitDate(
142 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), 147 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url),
143 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); 148 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 // Insert into |result|. 184 // Insert into |result|.
180 std::vector<const BookmarkNode*> result; 185 std::vector<const BookmarkNode*> result;
181 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { 186 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
182 result.push_back( 187 result.push_back(
183 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); 188 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url));
184 } 189 }
185 return result; 190 return result;
186 } 191 }
187 192
188 } // namespace ntp_snippets 193 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698