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 <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "components/bookmarks/browser/bookmark_model.h" | 13 #include "components/bookmarks/browser/bookmark_model.h" |
14 #include "components/bookmarks/browser/bookmark_node.h" | 14 #include "components/bookmarks/browser/bookmark_node.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 using bookmarks::BookmarkModel; | 17 using bookmarks::BookmarkModel; |
18 using bookmarks::BookmarkNode; | 18 using bookmarks::BookmarkNode; |
19 | 19 |
20 namespace ntp_snippets { | 20 namespace ntp_snippets { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 struct RecentBookmark { | |
25 const bookmarks::BookmarkNode* node; | |
26 bool visited_recently; | |
27 }; | |
28 | |
24 const char kBookmarkLastVisitDateKey[] = "last_visited"; | 29 const char kBookmarkLastVisitDateKey[] = "last_visited"; |
25 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; | 30 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; |
26 | 31 |
27 base::Time ParseLastVisitDate(const std::string& date_string) { | 32 base::Time ParseLastVisitDate(const std::string& date_string) { |
28 int64_t date = 0; | 33 int64_t date = 0; |
29 if (!base::StringToInt64(date_string, &date)) | 34 if (!base::StringToInt64(date_string, &date)) |
30 return base::Time::UnixEpoch(); | 35 return base::Time::UnixEpoch(); |
31 return base::Time::FromInternalValue(date); | 36 return base::Time::FromInternalValue(date); |
32 } | 37 } |
33 | 38 |
(...skipping 18 matching lines...) Expand all Loading... | |
52 // If there are bookmarks for |url|, set their last visit date to now. | 57 // If there are bookmarks for |url|, set their last visit date to now. |
53 std::string now = FormatLastVisitDate(base::Time::Now()); | 58 std::string now = FormatLastVisitDate(base::Time::Now()); |
54 for (const BookmarkNode* node : bookmarks_for_url) { | 59 for (const BookmarkNode* node : bookmarks_for_url) { |
55 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); | 60 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); |
56 // If the bookmark has been dismissed from NTP before, a new visit overrides | 61 // If the bookmark has been dismissed from NTP before, a new visit overrides |
57 // such a dismission. | 62 // such a dismission. |
58 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 63 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
59 } | 64 } |
60 } | 65 } |
61 | 66 |
62 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { | 67 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node, |
68 bool creation_date_fallback) { | |
63 if (!node) | 69 if (!node) |
64 return base::Time::UnixEpoch(); | 70 return base::Time::UnixEpoch(); |
65 | 71 |
66 std::string last_visit_date_string; | 72 std::string last_visit_date_string; |
67 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); | 73 if (!node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string) && |
74 creation_date_fallback) | |
75 return node->date_added(); | |
Marc Treib
2016/08/18 09:31:44
nit: Should have braces if the condition doesn't f
Philipp Keck
2016/08/19 11:20:54
Done.
| |
76 | |
68 return ParseLastVisitDate(last_visit_date_string); | 77 return ParseLastVisitDate(last_visit_date_string); |
69 } | 78 } |
70 | 79 |
71 base::Time GetLastVisitDateForBookmarkIfNotDismissed(const BookmarkNode* node) { | 80 base::Time GetLastVisitDateForBookmarkIfNotDismissed( |
81 const BookmarkNode* node, | |
82 bool creation_date_fallback) { | |
72 if (IsDismissedFromNTPForBookmark(node)) | 83 if (IsDismissedFromNTPForBookmark(node)) |
73 return base::Time::UnixEpoch(); | 84 return base::Time::UnixEpoch(); |
74 | 85 |
75 return GetLastVisitDateForBookmark(node); | 86 return GetLastVisitDateForBookmark(node, creation_date_fallback); |
76 } | 87 } |
77 | 88 |
78 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { | 89 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
79 std::vector<const BookmarkNode*> nodes; | 90 std::vector<const BookmarkNode*> nodes; |
80 bookmark_model->GetNodesByURL(url, &nodes); | 91 bookmark_model->GetNodesByURL(url, &nodes); |
81 for (const BookmarkNode* node : nodes) | 92 for (const BookmarkNode* node : nodes) |
82 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); | 93 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
83 } | 94 } |
84 | 95 |
85 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { | 96 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { |
(...skipping 16 matching lines...) Expand all Loading... | |
102 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 113 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
103 std::vector<const BookmarkNode*> nodes; | 114 std::vector<const BookmarkNode*> nodes; |
104 bookmark_model->GetNodesByURL(bookmark.url, &nodes); | 115 bookmark_model->GetNodesByURL(bookmark.url, &nodes); |
105 for (const BookmarkNode* node : nodes) | 116 for (const BookmarkNode* node : nodes) |
106 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 117 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
107 } | 118 } |
108 } | 119 } |
109 | 120 |
110 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | 121 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
111 BookmarkModel* bookmark_model, | 122 BookmarkModel* bookmark_model, |
123 int min_count, | |
112 int max_count, | 124 int max_count, |
113 const base::Time& min_visit_time) { | 125 const base::Time& min_visit_time) { |
114 // Get all the bookmark URLs. | 126 // Get all the bookmark URLs. |
115 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 127 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; |
116 bookmark_model->GetBookmarks(&bookmarks); | 128 bookmark_model->GetBookmarks(&bookmark_urls); |
117 | 129 |
118 // Remove the URLs for that no bookmark has a recent visit (more recent than | 130 std::vector<RecentBookmark> bookmarks; |
119 // |min_visit_time|). Recent visits to dismissed bookmarks are not considered. | 131 int recently_visited_count = 0; |
120 bookmarks.erase( | 132 // Find for each bookmark the most recently visited BookmarkNode and find out |
121 std::remove_if(bookmarks.begin(), bookmarks.end(), | 133 // whether it is visited since |min_visit_time|. |
122 [&bookmark_model, &min_visit_time]( | 134 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { |
123 const BookmarkModel::URLAndTitle& bookmark) { | 135 // Get all bookmarks for the given URL. |
124 // Get all bookmarks for the given URL. | 136 std::vector<const BookmarkNode*> bookmarks_for_url; |
125 std::vector<const BookmarkNode*> bookmarks_for_url; | 137 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); |
126 bookmark_model->GetNodesByURL(bookmark.url, | |
127 &bookmarks_for_url); | |
128 | 138 |
129 // Keep if at least one (non-dismissed) bookmark has been | 139 // Find the most recent node (minimal w.r.t. |
130 // recently visited. | 140 // CompareBookmarksByLastVisitDate). |
131 for (const BookmarkNode* node : bookmarks_for_url) { | 141 std::vector<const BookmarkNode*>::iterator most_recent = |
132 if (GetLastVisitDateForBookmarkIfNotDismissed(node) > | 142 std::min_element(bookmarks_for_url.begin(), bookmarks_for_url.end(), |
133 min_visit_time) | 143 &CompareBookmarksByLastVisitDate); |
134 return false; | 144 if (most_recent == bookmarks_for_url.end()) |
Marc Treib
2016/08/18 09:31:44
Can this ever happen? I think it can't, so I'd jus
Philipp Keck
2016/08/19 11:20:54
Right, done. We could think about adding DCHECK(bo
Marc Treib
2016/08/19 11:25:41
Well, a DCHECK is never stricly necessary, it just
Philipp Keck
2016/08/19 12:27:45
Done. https://codereview.chromium.org/2256183004/d
| |
135 } | 145 continue; |
136 // Otherwise erase this URL. | 146 const BookmarkNode* node = *most_recent; |
137 return true; | |
138 }), | |
139 bookmarks.end()); | |
140 | 147 |
141 // Sort the remaining bookmarks by date. | 148 // Find out if it has been _visited_ recently enough. |
149 if (GetLastVisitDateForBookmarkIfNotDismissed( | |
150 node, /*creation_date_fallback=*/false) > min_visit_time) { | |
151 recently_visited_count++; | |
152 bookmarks.push_back({node, true}); | |
153 } else { | |
154 bookmarks.push_back({node, false}); | |
155 } | |
156 } | |
157 | |
158 if (recently_visited_count < min_count) { | |
159 // Fill the list up to |min_count| but do not display more. | |
160 max_count = min_count; | |
161 } else { | |
162 // Remove the bookmarks that are not recently visited; we do no need them. | |
Marc Treib
2016/08/18 09:31:44
nit: we do noT need them
Philipp Keck
2016/08/19 11:20:54
Done.
| |
163 bookmarks.erase( | |
164 std::remove_if(bookmarks.begin(), bookmarks.end(), | |
165 [](const RecentBookmark& bookmark) { | |
166 return !bookmark.visited_recently; | |
167 }), | |
168 bookmarks.end()); | |
169 } | |
170 | |
171 // Sort the remaining entries by date. | |
142 std::sort(bookmarks.begin(), bookmarks.end(), | 172 std::sort(bookmarks.begin(), bookmarks.end(), |
143 [&bookmark_model](const BookmarkModel::URLAndTitle& a, | 173 [](const RecentBookmark& a, const RecentBookmark& b) { |
144 const BookmarkModel::URLAndTitle& b) { | 174 return CompareBookmarksByLastVisitDate(a.node, b.node); |
145 return CompareBookmarksByLastVisitDate( | |
146 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), | |
147 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); | |
148 }); | 175 }); |
149 | 176 |
150 // Insert the first |max_count| items from |bookmarks| into |result|. | 177 // Insert the first |max_count| items from |bookmarks| into |result|. |
151 std::vector<const BookmarkNode*> result; | 178 std::vector<const BookmarkNode*> result; |
152 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 179 for (const RecentBookmark& bookmark : bookmarks) { |
153 result.push_back( | 180 result.push_back(bookmark.node); |
154 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | |
155 if (result.size() >= static_cast<size_t>(max_count)) | 181 if (result.size() >= static_cast<size_t>(max_count)) |
156 break; | 182 break; |
157 } | 183 } |
158 return result; | 184 return result; |
159 } | 185 } |
160 | 186 |
161 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( | 187 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( |
162 BookmarkModel* bookmark_model) { | 188 BookmarkModel* bookmark_model) { |
163 // Get all the bookmark URLs. | 189 // Get all the bookmark URLs. |
164 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 190 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
(...skipping 18 matching lines...) Expand all Loading... | |
183 // Insert into |result|. | 209 // Insert into |result|. |
184 std::vector<const BookmarkNode*> result; | 210 std::vector<const BookmarkNode*> result; |
185 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 211 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
186 result.push_back( | 212 result.push_back( |
187 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 213 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
188 } | 214 } |
189 return result; | 215 return result; |
190 } | 216 } |
191 | 217 |
192 } // namespace ntp_snippets | 218 } // namespace ntp_snippets |
OLD | NEW |