Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 | |
| 13 using bookmarks::BookmarkModel; | |
| 14 using bookmarks::BookmarkNode; | |
| 15 | |
| 16 namespace ntp_snippets { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kBookmarkLastVisitDateKey[] = "last_visited"; | |
| 21 | |
| 22 base::Time ParseLastVisitDate(const std::string& date_string) { | |
| 23 int64_t date = 0; | |
| 24 if (!base::StringToInt64(date_string, &date)) | |
| 25 return base::Time::UnixEpoch(); | |
| 26 return base::Time::FromInternalValue(date); | |
| 27 } | |
| 28 | |
| 29 std::string FormatLastVisitDate(const base::Time& date) { | |
| 30 return base::Int64ToString(date.ToInternalValue()); | |
| 31 } | |
| 32 | |
| 33 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, | |
| 34 const BookmarkNode* b) { | |
| 35 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b); | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, | |
| 41 const GURL& url) { | |
| 42 std::vector<const BookmarkNode*> bookmarks_for_url; | |
| 43 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | |
| 44 if (bookmarks_for_url.empty()) | |
| 45 return; | |
| 46 | |
| 47 // If there are bookmark for |url|, set their last visit date to now. | |
| 48 for (auto* node : bookmarks_for_url) { | |
| 49 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, | |
| 50 FormatLastVisitDate(base::Time::Now())); | |
|
Marc Treib
2016/07/30 14:38:09
nit: You could pull the base::Time::Now() out of t
Philipp Keck
2016/08/02 13:05:33
Done.
| |
| 51 } | |
| 52 } | |
| 53 | |
| 54 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { | |
| 55 if (!node) | |
| 56 return base::Time::UnixEpoch(); | |
| 57 | |
| 58 std::string last_visit_date_string; | |
| 59 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); | |
| 60 | |
| 61 if (last_visit_date_string.empty()) { | |
| 62 // Use creation date if no last visit info present. | |
| 63 return node->date_added(); | |
| 64 } | |
| 65 | |
| 66 return ParseLastVisitDate(last_visit_date_string); | |
| 67 } | |
| 68 | |
| 69 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | |
| 70 BookmarkModel* bookmark_model, | |
| 71 int max_count, | |
| 72 const base::Time& min_visit_time) { | |
| 73 // Get all the bookmarks. | |
| 74 std::vector<BookmarkModel::URLAndTitle> bookmarks; | |
| 75 bookmark_model->GetBookmarks(&bookmarks); | |
| 76 // Remove bookmarks that have not been visited after |min_time|. | |
| 77 std::vector<const BookmarkNode*> bookmarks_for_url; | |
|
Marc Treib
2016/07/30 14:38:09
Can you declare this variable inside the lambda?
Philipp Keck
2016/08/02 13:05:33
Done.
| |
| 78 bookmarks.erase( | |
| 79 std::remove_if( | |
| 80 bookmarks.begin(), bookmarks.end(), | |
| 81 [&bookmark_model, &min_visit_time, | |
| 82 &bookmarks_for_url](const BookmarkModel::URLAndTitle& bookmark) { | |
|
Marc Treib
2016/07/30 14:38:09
Is this line break correct? Looks super weird
Philipp Keck
2016/08/02 13:05:33
Looks fine to me, though now it looks different be
| |
| 83 // Get all bookmarks for the given URL. | |
| 84 bookmarks_for_url.clear(); | |
| 85 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | |
| 86 // Find the most recent visit time in the set. | |
| 87 base::Time max_visit_time = min_visit_time; | |
| 88 for (auto* node : bookmarks_for_url) { | |
|
Marc Treib
2016/07/30 14:38:09
const auto* ?
Philipp Keck
2016/08/02 13:05:33
Done.
| |
| 89 base::Time node_visit_time = GetLastVisitDateForBookmark(node); | |
| 90 if (node_visit_time > max_visit_time) | |
| 91 max_visit_time = node_visit_time; | |
|
Marc Treib
2016/07/30 14:38:09
optional nit:
max_visit_time = std::max(max_visit_
Philipp Keck
2016/08/02 13:05:33
Done.
| |
| 92 } | |
| 93 return (max_visit_time <= min_visit_time); | |
|
Marc Treib
2016/07/30 14:38:09
nit: no parens required
Philipp Keck
2016/08/02 13:05:33
Done.
| |
| 94 }), | |
| 95 bookmarks.end()); | |
| 96 // Sort the remaining bookmarks by date. | |
| 97 std::sort(bookmarks.begin(), bookmarks.end(), | |
| 98 [&bookmark_model](const BookmarkModel::URLAndTitle& a, | |
| 99 const BookmarkModel::URLAndTitle& b) { | |
|
Marc Treib
2016/07/30 14:38:09
Is this the correct indentation? Looks super weird
Philipp Keck
2016/08/02 13:05:33
Done.
| |
| 100 return CompareBookmarksByLastVisitDate( | |
| 101 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), | |
| 102 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); | |
| 103 }); | |
| 104 | |
| 105 std::vector<const BookmarkNode*> result; | |
| 106 // Insert the first |max_count| items from |bookmarks| into |result|. | |
| 107 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | |
| 108 result.push_back( | |
| 109 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | |
| 110 if (result.size() >= static_cast<size_t>(max_count)) | |
| 111 break; | |
| 112 } | |
| 113 return result; | |
| 114 } | |
| 115 | |
| 116 } // namespace ntp_snippets | |
| OLD | NEW |