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