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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 } | 68 } |
69 | 69 |
70 std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark( | 70 std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark( |
71 const std::vector<const BookmarkNode*>& bookmarks, | 71 const std::vector<const BookmarkNode*>& bookmarks, |
72 bool consider_visits_from_desktop) { | 72 bool consider_visits_from_desktop) { |
73 auto most_recent = bookmarks.end(); | 73 auto most_recent = bookmarks.end(); |
74 base::Time most_recent_last_visited = base::Time::UnixEpoch(); | 74 base::Time most_recent_last_visited = base::Time::UnixEpoch(); |
75 | 75 |
76 for (auto iter = bookmarks.begin(); iter != bookmarks.end(); ++iter) { | 76 for (auto iter = bookmarks.begin(); iter != bookmarks.end(); ++iter) { |
77 base::Time last_visited; | 77 base::Time last_visited; |
78 if (GetLastVisitDateForNTPBookmark(*iter, consider_visits_from_desktop, | 78 if (GetLastVisitDateForNTPBookmark(**iter, consider_visits_from_desktop, |
79 &last_visited) && | 79 &last_visited) && |
80 most_recent_last_visited <= last_visited) { | 80 most_recent_last_visited <= last_visited) { |
81 most_recent = iter; | 81 most_recent = iter; |
82 most_recent_last_visited = last_visited; | 82 most_recent_last_visited = last_visited; |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 return most_recent; | 86 return most_recent; |
87 } | 87 } |
88 | 88 |
(...skipping 20 matching lines...) Expand all Loading... |
109 bookmark_model->SetNodeMetaInfo( | 109 bookmark_model->SetNodeMetaInfo( |
110 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey | 110 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey |
111 : kBookmarkLastVisitDateOnDesktopKey, | 111 : kBookmarkLastVisitDateOnDesktopKey, |
112 now); | 112 now); |
113 // If the bookmark has been dismissed from NTP before, a new visit overrides | 113 // If the bookmark has been dismissed from NTP before, a new visit overrides |
114 // such a dismissal. | 114 // such a dismissal. |
115 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 115 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 bool GetLastVisitDateForNTPBookmark(const BookmarkNode* node, | 119 bool GetLastVisitDateForNTPBookmark(const BookmarkNode& node, |
120 bool consider_visits_from_desktop, | 120 bool consider_visits_from_desktop, |
121 base::Time* out) { | 121 base::Time* out) { |
122 if (!node || IsDismissedFromNTPForBookmark(node)) { | 122 if (IsDismissedFromNTPForBookmark(node)) { |
123 return false; | 123 return false; |
124 } | 124 } |
125 | 125 |
126 bool got_mobile_date = | 126 bool got_mobile_date = |
127 ExtractLastVisitDate(*node, kBookmarkLastVisitDateOnMobileKey, out); | 127 ExtractLastVisitDate(node, kBookmarkLastVisitDateOnMobileKey, out); |
128 | 128 |
129 if (consider_visits_from_desktop) { | 129 if (consider_visits_from_desktop) { |
130 // Consider the later visit from these two platform groups. | 130 // Consider the later visit from these two platform groups. |
131 base::Time last_visit_desktop; | 131 base::Time last_visit_desktop; |
132 if (ExtractLastVisitDate(*node, kBookmarkLastVisitDateOnDesktopKey, | 132 if (ExtractLastVisitDate(node, kBookmarkLastVisitDateOnDesktopKey, |
133 &last_visit_desktop)) { | 133 &last_visit_desktop)) { |
134 if (!got_mobile_date) { | 134 if (!got_mobile_date) { |
135 *out = last_visit_desktop; | 135 *out = last_visit_desktop; |
136 } else { | 136 } else { |
137 *out = std::max(*out, last_visit_desktop); | 137 *out = std::max(*out, last_visit_desktop); |
138 } | 138 } |
139 return true; | 139 return true; |
140 } | 140 } |
141 } | 141 } |
142 | 142 |
143 return got_mobile_date; | 143 return got_mobile_date; |
144 } | 144 } |
145 | 145 |
146 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { | 146 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
147 std::vector<const BookmarkNode*> nodes; | 147 std::vector<const BookmarkNode*> nodes; |
148 bookmark_model->GetNodesByURL(url, &nodes); | 148 bookmark_model->GetNodesByURL(url, &nodes); |
149 for (const BookmarkNode* node : nodes) { | 149 for (const BookmarkNode* node : nodes) { |
150 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); | 150 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { | 154 bool IsDismissedFromNTPForBookmark(const BookmarkNode& node) { |
155 if (!node) { | |
156 return false; | |
157 } | |
158 | |
159 std::string dismissed_from_ntp; | 155 std::string dismissed_from_ntp; |
160 bool result = | 156 bool result = |
161 node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); | 157 node.GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); |
162 DCHECK(!result || dismissed_from_ntp == "1"); | 158 DCHECK(!result || dismissed_from_ntp == "1"); |
163 return result; | 159 return result; |
164 } | 160 } |
165 | 161 |
166 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { | 162 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { |
167 // Get all the bookmark URLs. | 163 // Get all the bookmark URLs. |
168 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 164 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
169 bookmark_model->GetBookmarks(&bookmarks); | 165 bookmark_model->GetBookmarks(&bookmarks); |
170 | 166 |
171 // Remove dismissed flag from all bookmarks | 167 // Remove dismissed flag from all bookmarks |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // Find the most recently visited node for the given URL. | 200 // Find the most recently visited node for the given URL. |
205 auto most_recent = | 201 auto most_recent = |
206 FindMostRecentBookmark(bookmarks_for_url, consider_visits_from_desktop); | 202 FindMostRecentBookmark(bookmarks_for_url, consider_visits_from_desktop); |
207 if (most_recent == bookmarks_for_url.end()) { | 203 if (most_recent == bookmarks_for_url.end()) { |
208 continue; | 204 continue; |
209 } | 205 } |
210 | 206 |
211 // Extract the last visit of the node to use later for sorting. | 207 // Extract the last visit of the node to use later for sorting. |
212 base::Time last_visit_time; | 208 base::Time last_visit_time; |
213 if (!GetLastVisitDateForNTPBookmark( | 209 if (!GetLastVisitDateForNTPBookmark( |
214 *most_recent, consider_visits_from_desktop, &last_visit_time) || | 210 **most_recent, consider_visits_from_desktop, &last_visit_time) || |
215 last_visit_time <= min_visit_time) { | 211 last_visit_time <= min_visit_time) { |
216 continue; | 212 continue; |
217 } | 213 } |
218 | 214 |
219 bookmarks.push_back({*most_recent, last_visit_time}); | 215 bookmarks.push_back({*most_recent, last_visit_time}); |
220 } | 216 } |
221 | 217 |
222 // Sort the entries by date, getting the |max_count| most recent bookmarks | 218 // Sort the entries by date, getting the |max_count| most recent bookmarks |
223 // to the front. | 219 // to the front. |
224 size_t count_to_sort = | 220 size_t count_to_sort = |
(...skipping 24 matching lines...) Expand all Loading... |
249 // Remove the bookmark URLs which have at least one non-dismissed bookmark. | 245 // Remove the bookmark URLs which have at least one non-dismissed bookmark. |
250 bookmarks.erase( | 246 bookmarks.erase( |
251 std::remove_if( | 247 std::remove_if( |
252 bookmarks.begin(), bookmarks.end(), | 248 bookmarks.begin(), bookmarks.end(), |
253 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { | 249 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { |
254 std::vector<const BookmarkNode*> bookmarks_for_url; | 250 std::vector<const BookmarkNode*> bookmarks_for_url; |
255 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | 251 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
256 DCHECK(!bookmarks_for_url.empty()); | 252 DCHECK(!bookmarks_for_url.empty()); |
257 | 253 |
258 for (const BookmarkNode* node : bookmarks_for_url) { | 254 for (const BookmarkNode* node : bookmarks_for_url) { |
259 if (!IsDismissedFromNTPForBookmark(node)) { | 255 if (!IsDismissedFromNTPForBookmark(*node)) { |
260 return true; | 256 return true; |
261 } | 257 } |
262 } | 258 } |
263 return false; | 259 return false; |
264 }), | 260 }), |
265 bookmarks.end()); | 261 bookmarks.end()); |
266 | 262 |
267 // Insert into |result|. | 263 // Insert into |result|. |
268 std::vector<const BookmarkNode*> result; | 264 std::vector<const BookmarkNode*> result; |
269 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 265 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
(...skipping 16 matching lines...) Expand all Loading... |
286 for (const BookmarkNode* bookmark : bookmarks_for_url) { | 282 for (const BookmarkNode* bookmark : bookmarks_for_url) { |
287 bookmark_model->DeleteNodeMetaInfo(bookmark, | 283 bookmark_model->DeleteNodeMetaInfo(bookmark, |
288 kBookmarkLastVisitDateOnMobileKey); | 284 kBookmarkLastVisitDateOnMobileKey); |
289 bookmark_model->DeleteNodeMetaInfo(bookmark, | 285 bookmark_model->DeleteNodeMetaInfo(bookmark, |
290 kBookmarkLastVisitDateOnDesktopKey); | 286 kBookmarkLastVisitDateOnDesktopKey); |
291 } | 287 } |
292 } | 288 } |
293 } | 289 } |
294 | 290 |
295 } // namespace ntp_snippets | 291 } // namespace ntp_snippets |
OLD | NEW |