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. | |
Marc Treib
2016/08/02 13:37:11
nit: bookmarks, plural
Philipp Keck
2016/08/02 14:06:51
Done.
| |
48 base::Time now = base::Time::Now(); | |
49 for (auto* node : bookmarks_for_url) { | |
50 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, | |
51 FormatLastVisitDate(now)); | |
Marc Treib
2016/08/02 13:37:11
nit: You could pull this out of the loop
Philipp Keck
2016/08/02 14:06:51
Done.
| |
52 } | |
53 } | |
54 | |
55 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { | |
56 if (!node) | |
57 return base::Time::UnixEpoch(); | |
58 | |
59 std::string last_visit_date_string; | |
60 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); | |
61 | |
62 if (last_visit_date_string.empty()) { | |
63 // Use creation date if no last visit info present. | |
64 return node->date_added(); | |
65 } | |
66 | |
67 return ParseLastVisitDate(last_visit_date_string); | |
68 } | |
69 | |
70 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | |
71 BookmarkModel* bookmark_model, | |
72 int max_count, | |
73 const base::Time& min_visit_time) { | |
74 // Get all the bookmarks. | |
75 std::vector<BookmarkModel::URLAndTitle> bookmarks; | |
76 bookmark_model->GetBookmarks(&bookmarks); | |
77 // Remove bookmarks that have not been visited after |min_time|. | |
Marc Treib
2016/08/02 13:37:11
nit: The empty line should be before this
Philipp Keck
2016/08/02 14:06:51
Done.
| |
78 | |
79 bookmarks.erase( | |
80 std::remove_if( | |
81 bookmarks.begin(), bookmarks.end(), | |
82 [&bookmark_model, | |
83 &min_visit_time](const BookmarkModel::URLAndTitle& bookmark) { | |
84 // Get all bookmarks for the given URL. | |
85 std::vector<const BookmarkNode*> bookmarks_for_url; | |
86 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | |
87 // Find the most recent visit time in the set. | |
88 base::Time max_visit_time = min_visit_time; | |
89 for (const auto* node : bookmarks_for_url) { | |
90 max_visit_time = | |
91 std::max(max_visit_time, GetLastVisitDateForBookmark(node)); | |
Marc Treib
2016/08/02 13:37:11
You could just do
if (GetLastVisitDateForBookmark(
Philipp Keck
2016/08/02 14:06:51
Done.
| |
92 } | |
93 return max_visit_time <= min_visit_time; | |
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) { | |
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 |