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_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 <numeric> | 8 #include <numeric> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 | 70 |
71 std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark( | 71 std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark( |
72 const std::vector<const BookmarkNode*>& bookmarks, | 72 const std::vector<const BookmarkNode*>& bookmarks, |
73 bool creation_date_fallback, | 73 bool creation_date_fallback, |
74 bool consider_visits_from_desktop) { | 74 bool consider_visits_from_desktop) { |
75 auto most_recent = bookmarks.end(); | 75 auto most_recent = bookmarks.end(); |
76 base::Time most_recent_last_visited = base::Time::UnixEpoch(); | 76 base::Time most_recent_last_visited = base::Time::UnixEpoch(); |
77 | 77 |
78 for (auto iter = bookmarks.begin(); iter != bookmarks.end(); ++iter) { | 78 for (auto iter = bookmarks.begin(); iter != bookmarks.end(); ++iter) { |
79 base::Time last_visited; | 79 base::Time last_visited; |
80 if (GetLastVisitDateForNTPBookmark(*iter, creation_date_fallback, | 80 if (GetLastVisitDateForNTPBookmark(**iter, creation_date_fallback, |
81 consider_visits_from_desktop, | 81 consider_visits_from_desktop, |
82 &last_visited) && | 82 &last_visited) && |
83 most_recent_last_visited <= last_visited) { | 83 most_recent_last_visited <= last_visited) { |
84 most_recent = iter; | 84 most_recent = iter; |
85 most_recent_last_visited = last_visited; | 85 most_recent_last_visited = last_visited; |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 return most_recent; | 89 return most_recent; |
90 } | 90 } |
(...skipping 21 matching lines...) Expand all Loading... | |
112 bookmark_model->SetNodeMetaInfo( | 112 bookmark_model->SetNodeMetaInfo( |
113 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey | 113 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey |
114 : kBookmarkLastVisitDateOnDesktopKey, | 114 : kBookmarkLastVisitDateOnDesktopKey, |
115 now); | 115 now); |
116 // If the bookmark has been dismissed from NTP before, a new visit overrides | 116 // If the bookmark has been dismissed from NTP before, a new visit overrides |
117 // such a dismissal. | 117 // such a dismissal. |
118 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 118 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
122 bool GetLastVisitDateForNTPBookmark(const BookmarkNode* node, | 122 bool GetLastVisitDateForNTPBookmark(const BookmarkNode& node, |
123 bool creation_date_fallback, | 123 bool creation_date_fallback, |
124 bool consider_visits_from_desktop, | 124 bool consider_visits_from_desktop, |
125 base::Time* out) { | 125 base::Time* out) { |
126 if (!node || IsDismissedFromNTPForBookmark(node)) { | 126 if (IsDismissedFromNTPForBookmark(node)) { |
127 return false; | 127 return false; |
128 } | 128 } |
129 | 129 |
130 bool got_mobile_date = | 130 bool got_mobile_date = |
131 ExtractLastVisitDate(*node, kBookmarkLastVisitDateOnMobileKey, out); | 131 ExtractLastVisitDate(node, kBookmarkLastVisitDateOnMobileKey, out); |
132 | 132 |
133 if (consider_visits_from_desktop) { | 133 if (consider_visits_from_desktop) { |
134 // Consider the later visit from these two platform groups. | 134 // Consider the later visit from these two platform groups. |
135 base::Time last_visit_desktop; | 135 base::Time last_visit_desktop; |
136 if (ExtractLastVisitDate(*node, kBookmarkLastVisitDateOnDesktopKey, | 136 if (ExtractLastVisitDate(node, kBookmarkLastVisitDateOnDesktopKey, |
137 &last_visit_desktop)) { | 137 &last_visit_desktop)) { |
138 if (!got_mobile_date) { | 138 if (!got_mobile_date) { |
139 *out = last_visit_desktop; | 139 *out = last_visit_desktop; |
140 } else { | 140 } else { |
141 *out = std::max(*out, last_visit_desktop); | 141 *out = std::max(*out, last_visit_desktop); |
142 } | 142 } |
143 return true; | 143 return true; |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 if (!got_mobile_date && creation_date_fallback) { | 147 if (!got_mobile_date && creation_date_fallback) { |
148 *out = node->date_added(); | 148 *out = node.date_added(); |
149 return true; | 149 return true; |
150 } | 150 } |
151 | 151 |
152 return got_mobile_date; | 152 return got_mobile_date; |
153 } | 153 } |
154 | 154 |
155 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { | 155 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
156 std::vector<const BookmarkNode*> nodes; | 156 std::vector<const BookmarkNode*> nodes; |
157 bookmark_model->GetNodesByURL(url, &nodes); | 157 bookmark_model->GetNodesByURL(url, &nodes); |
158 for (const BookmarkNode* node : nodes) { | 158 for (const BookmarkNode* node : nodes) { |
159 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); | 159 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { | 163 bool IsDismissedFromNTPForBookmark(const BookmarkNode& node) { |
164 if (!node) { | |
165 return false; | |
166 } | |
167 | |
168 std::string dismissed_from_ntp; | 164 std::string dismissed_from_ntp; |
169 bool result = | 165 bool result = |
170 node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); | 166 node.GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); |
171 DCHECK(!result || dismissed_from_ntp == "1"); | 167 DCHECK(!result || dismissed_from_ntp == "1"); |
172 return result; | 168 return result; |
173 } | 169 } |
174 | 170 |
175 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { | 171 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { |
176 // Get all the bookmark URLs. | 172 // Get all the bookmark URLs. |
177 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 173 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
178 bookmark_model->GetBookmarks(&bookmarks); | 174 bookmark_model->GetBookmarks(&bookmarks); |
179 | 175 |
180 // Remove dismissed flag from all bookmarks | 176 // Remove dismissed flag from all bookmarks |
(...skipping 10 matching lines...) Expand all Loading... | |
191 BookmarkModel* bookmark_model, | 187 BookmarkModel* bookmark_model, |
192 int min_count, | 188 int min_count, |
193 int max_count, | 189 int max_count, |
194 const base::Time& min_visit_time, | 190 const base::Time& min_visit_time, |
195 bool creation_date_fallback, | 191 bool creation_date_fallback, |
196 bool consider_visits_from_desktop) { | 192 bool consider_visits_from_desktop) { |
197 // Get all the bookmark URLs. | 193 // Get all the bookmark URLs. |
198 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; | 194 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; |
199 bookmark_model->GetBookmarks(&bookmark_urls); | 195 bookmark_model->GetBookmarks(&bookmark_urls); |
200 | 196 |
201 std::vector<RecentBookmark> bookmarks; | 197 std::vector<RecentBookmark> bookmarks; |
tschumann
2016/11/25 13:47:43
nit: unrelated to your change, but consider moving
jkrcal
2016/11/25 14:05:36
Both these variables should outlive the for-cycle
tschumann
2016/11/25 15:38:14
ahh... i see... the nesting tricked me in the diff
Marc Treib
2016/11/25 16:23:47
Semi-related: I just sent out a CL which significa
| |
202 int recently_visited_count = 0; | 198 int recently_visited_count = 0; |
203 // Find for each bookmark the most recently visited BookmarkNode and find out | 199 // Find for each bookmark the most recently visited BookmarkNode and find out |
204 // whether it is visited since |min_visit_time|. | 200 // whether it is visited since |min_visit_time|. |
205 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { | 201 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { |
206 // Skip URLs that are blacklisted. | 202 // Skip URLs that are blacklisted. |
207 if (IsBlacklisted(url_and_title.url)) { | 203 if (IsBlacklisted(url_and_title.url)) { |
208 continue; | 204 continue; |
209 } | 205 } |
210 | 206 |
211 // Get all bookmarks for the given URL. | 207 // Get all bookmarks for the given URL. |
212 std::vector<const BookmarkNode*> bookmarks_for_url; | 208 std::vector<const BookmarkNode*> bookmarks_for_url; |
213 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); | 209 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); |
214 DCHECK(!bookmarks_for_url.empty()); | 210 DCHECK(!bookmarks_for_url.empty()); |
215 | 211 |
216 // Find the most recently visited node for the given URL. | 212 // Find the most recently visited node for the given URL. |
217 auto most_recent = | 213 auto most_recent = |
218 FindMostRecentBookmark(bookmarks_for_url, creation_date_fallback, | 214 FindMostRecentBookmark(bookmarks_for_url, creation_date_fallback, |
219 consider_visits_from_desktop); | 215 consider_visits_from_desktop); |
220 if (most_recent == bookmarks_for_url.end()) { | 216 if (most_recent == bookmarks_for_url.end()) { |
221 continue; | 217 continue; |
222 } | 218 } |
223 | 219 |
224 // Extract the last visit of the node to use later for sorting. | 220 // Extract the last visit of the node to use later for sorting. |
225 base::Time last_visit; | 221 base::Time last_visit; |
226 if (!GetLastVisitDateForNTPBookmark(*most_recent, creation_date_fallback, | 222 if (!GetLastVisitDateForNTPBookmark(**most_recent, creation_date_fallback, |
227 consider_visits_from_desktop, | 223 consider_visits_from_desktop, |
228 &last_visit)) { | 224 &last_visit)) { |
229 continue; | 225 continue; |
230 } | 226 } |
231 | 227 |
232 // Has it been _visited_ recently enough (not considering creation date)? | 228 // Has it been _visited_ recently enough (not considering creation date)? |
233 base::Time last_real_visit; | 229 base::Time last_real_visit; |
234 if (GetLastVisitDateForNTPBookmark( | 230 if (GetLastVisitDateForNTPBookmark( |
235 *most_recent, /*creation_date_fallback=*/false, | 231 **most_recent, /*creation_date_fallback=*/false, |
236 consider_visits_from_desktop, &last_real_visit) && | 232 consider_visits_from_desktop, &last_real_visit) && |
237 min_visit_time < last_real_visit) { | 233 min_visit_time < last_real_visit) { |
238 recently_visited_count++; | 234 recently_visited_count++; |
239 bookmarks.push_back( | 235 bookmarks.push_back( |
240 {*most_recent, last_visit, /*visited_recently=*/true}); | 236 {*most_recent, last_visit, /*visited_recently=*/true}); |
241 } else { | 237 } else { |
242 bookmarks.push_back( | 238 bookmarks.push_back( |
243 {*most_recent, last_visit, /*visited_recently=*/false}); | 239 {*most_recent, last_visit, /*visited_recently=*/false}); |
244 } | 240 } |
245 } | 241 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 // Remove the bookmark URLs which have at least one non-dismissed bookmark. | 291 // Remove the bookmark URLs which have at least one non-dismissed bookmark. |
296 bookmarks.erase( | 292 bookmarks.erase( |
297 std::remove_if( | 293 std::remove_if( |
298 bookmarks.begin(), bookmarks.end(), | 294 bookmarks.begin(), bookmarks.end(), |
299 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { | 295 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { |
300 std::vector<const BookmarkNode*> bookmarks_for_url; | 296 std::vector<const BookmarkNode*> bookmarks_for_url; |
301 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | 297 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
302 DCHECK(!bookmarks_for_url.empty()); | 298 DCHECK(!bookmarks_for_url.empty()); |
303 | 299 |
304 for (const BookmarkNode* node : bookmarks_for_url) { | 300 for (const BookmarkNode* node : bookmarks_for_url) { |
305 if (!IsDismissedFromNTPForBookmark(node)) { | 301 if (!IsDismissedFromNTPForBookmark(*node)) { |
306 return true; | 302 return true; |
307 } | 303 } |
308 } | 304 } |
309 return false; | 305 return false; |
310 }), | 306 }), |
311 bookmarks.end()); | 307 bookmarks.end()); |
312 | 308 |
313 // Insert into |result|. | 309 // Insert into |result|. |
314 std::vector<const BookmarkNode*> result; | 310 std::vector<const BookmarkNode*> result; |
315 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 311 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
(...skipping 16 matching lines...) Expand all Loading... | |
332 for (const BookmarkNode* bookmark : bookmarks_for_url) { | 328 for (const BookmarkNode* bookmark : bookmarks_for_url) { |
333 bookmark_model->DeleteNodeMetaInfo(bookmark, | 329 bookmark_model->DeleteNodeMetaInfo(bookmark, |
334 kBookmarkLastVisitDateOnMobileKey); | 330 kBookmarkLastVisitDateOnMobileKey); |
335 bookmark_model->DeleteNodeMetaInfo(bookmark, | 331 bookmark_model->DeleteNodeMetaInfo(bookmark, |
336 kBookmarkLastVisitDateOnDesktopKey); | 332 kBookmarkLastVisitDateOnDesktopKey); |
337 } | 333 } |
338 } | 334 } |
339 } | 335 } |
340 | 336 |
341 } // namespace ntp_snippets | 337 } // namespace ntp_snippets |
OLD | NEW |