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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
Marc Treib
2017/03/10 10:38:58
#include "base/stl_util.h"
| |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
17 #include "components/bookmarks/browser/bookmark_node.h" | 17 #include "components/bookmarks/browser/bookmark_node.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 using bookmarks::BookmarkModel; | 20 using bookmarks::BookmarkModel; |
21 using bookmarks::BookmarkNode; | 21 using bookmarks::BookmarkNode; |
22 | 22 |
23 namespace ntp_snippets { | 23 namespace ntp_snippets { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 struct RecentBookmark { | 27 struct RecentBookmark { |
28 const bookmarks::BookmarkNode* node; | 28 const bookmarks::BookmarkNode* node; |
29 base::Time last_visited; | 29 base::Time last_visited; |
30 }; | 30 }; |
31 | 31 |
32 const char* kBookmarksURLBlacklist[] = {"chrome://newtab/", | 32 const char* kBookmarksURLBlacklist[] = { |
33 "chrome-native://newtab/", | 33 "chrome://newtab/", "chrome-native://newtab/", "chrome://bookmarks/"}; |
34 "chrome://bookmarks/"}; | |
35 | 34 |
36 const char kBookmarkLastVisitDateOnMobileKey[] = "last_visited"; | 35 const char kBookmarkLastVisitDateOnMobileKey[] = "last_visited"; |
37 const char kBookmarkLastVisitDateOnDesktopKey[] = "last_visited_desktop"; | 36 const char kBookmarkLastVisitDateOnDesktopKey[] = "last_visited_desktop"; |
38 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; | 37 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; |
39 | 38 |
40 std::string FormatLastVisitDate(const base::Time& date) { | 39 std::string FormatLastVisitDate(const base::Time& date) { |
41 return base::Int64ToString(date.ToInternalValue()); | 40 return base::Int64ToString(date.ToInternalValue()); |
42 } | 41 } |
43 | 42 |
44 bool ExtractLastVisitDate(const BookmarkNode& node, | 43 bool ExtractLastVisitDate(const BookmarkNode& node, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 // Skip URLs that are not bookmarked. | 98 // Skip URLs that are not bookmarked. |
100 std::vector<const BookmarkNode*> bookmarks_for_url; | 99 std::vector<const BookmarkNode*> bookmarks_for_url; |
101 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | 100 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); |
102 if (bookmarks_for_url.empty()) { | 101 if (bookmarks_for_url.empty()) { |
103 return; | 102 return; |
104 } | 103 } |
105 | 104 |
106 // If there are bookmarks for |url|, set their last visit date to now. | 105 // If there are bookmarks for |url|, set their last visit date to now. |
107 std::string now = FormatLastVisitDate(base::Time::Now()); | 106 std::string now = FormatLastVisitDate(base::Time::Now()); |
108 for (const BookmarkNode* node : bookmarks_for_url) { | 107 for (const BookmarkNode* node : bookmarks_for_url) { |
109 bookmark_model->SetNodeMetaInfo( | 108 bookmark_model->SetNodeMetaInfo(node, |
110 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey | 109 is_mobile_platform |
111 : kBookmarkLastVisitDateOnDesktopKey, | 110 ? kBookmarkLastVisitDateOnMobileKey |
112 now); | 111 : kBookmarkLastVisitDateOnDesktopKey, |
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 (IsDismissedFromNTPForBookmark(node)) { | 122 if (IsDismissedFromNTPForBookmark(node)) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 return result; | 236 return result; |
237 } | 237 } |
238 | 238 |
239 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( | 239 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( |
240 BookmarkModel* bookmark_model) { | 240 BookmarkModel* bookmark_model) { |
241 // Get all the bookmark URLs. | 241 // Get all the bookmark URLs. |
242 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 242 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
243 bookmark_model->GetBookmarks(&bookmarks); | 243 bookmark_model->GetBookmarks(&bookmarks); |
244 | 244 |
245 // 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. |
246 bookmarks.erase( | 246 base::EraseIf( |
247 std::remove_if( | 247 bookmarks, [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { |
248 bookmarks.begin(), bookmarks.end(), | 248 std::vector<const BookmarkNode*> bookmarks_for_url; |
249 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { | 249 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
250 std::vector<const BookmarkNode*> bookmarks_for_url; | 250 DCHECK(!bookmarks_for_url.empty()); |
251 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | |
252 DCHECK(!bookmarks_for_url.empty()); | |
253 | 251 |
254 for (const BookmarkNode* node : bookmarks_for_url) { | 252 for (const BookmarkNode* node : bookmarks_for_url) { |
255 if (!IsDismissedFromNTPForBookmark(*node)) { | 253 if (!IsDismissedFromNTPForBookmark(*node)) { |
256 return true; | 254 return true; |
257 } | 255 } |
258 } | 256 } |
259 return false; | 257 return false; |
260 }), | 258 }); |
261 bookmarks.end()); | |
262 | 259 |
263 // Insert into |result|. | 260 // Insert into |result|. |
264 std::vector<const BookmarkNode*> result; | 261 std::vector<const BookmarkNode*> result; |
265 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 262 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
266 result.push_back( | 263 result.push_back( |
267 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 264 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
268 } | 265 } |
269 return result; | 266 return result; |
270 } | 267 } |
271 | 268 |
(...skipping 27 matching lines...) Expand all Loading... | |
299 } | 296 } |
300 // Get all bookmarks for the given URL. | 297 // Get all bookmarks for the given URL. |
301 std::vector<const BookmarkNode*> bookmarks_for_url; | 298 std::vector<const BookmarkNode*> bookmarks_for_url; |
302 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); | 299 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); |
303 | 300 |
304 for (const BookmarkNode* bookmark : bookmarks_for_url) { | 301 for (const BookmarkNode* bookmark : bookmarks_for_url) { |
305 // The dismissal metadata is managed by the BookmarkSuggestionsProvider. | 302 // The dismissal metadata is managed by the BookmarkSuggestionsProvider. |
306 ClearLastVisitedMetadataIfBetween(bookmark_model, *bookmark, begin, end, | 303 ClearLastVisitedMetadataIfBetween(bookmark_model, *bookmark, begin, end, |
307 kBookmarkLastVisitDateOnMobileKey); | 304 kBookmarkLastVisitDateOnMobileKey); |
308 ClearLastVisitedMetadataIfBetween(bookmark_model, *bookmark, begin, end, | 305 ClearLastVisitedMetadataIfBetween(bookmark_model, *bookmark, begin, end, |
309 kBookmarkLastVisitDateOnDesktopKey); | 306 kBookmarkLastVisitDateOnDesktopKey); |
310 } | 307 } |
311 } | 308 } |
312 } | 309 } |
313 | 310 |
314 } // namespace ntp_snippets | 311 } // namespace ntp_snippets |
OLD | NEW |