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/bind.h" | |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "components/bookmarks/browser/bookmark_model.h" | 14 #include "components/bookmarks/browser/bookmark_model.h" |
14 #include "components/bookmarks/browser/bookmark_node.h" | 15 #include "components/bookmarks/browser/bookmark_node.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 using bookmarks::BookmarkModel; | 18 using bookmarks::BookmarkModel; |
18 using bookmarks::BookmarkNode; | 19 using bookmarks::BookmarkNode; |
19 | 20 |
20 namespace ntp_snippets { | 21 namespace ntp_snippets { |
(...skipping 13 matching lines...) Expand all Loading... | |
34 if (!base::StringToInt64(date_string, &date)) | 35 if (!base::StringToInt64(date_string, &date)) |
35 return base::Time::UnixEpoch(); | 36 return base::Time::UnixEpoch(); |
36 return base::Time::FromInternalValue(date); | 37 return base::Time::FromInternalValue(date); |
37 } | 38 } |
38 | 39 |
39 std::string FormatLastVisitDate(const base::Time& date) { | 40 std::string FormatLastVisitDate(const base::Time& date) { |
40 return base::Int64ToString(date.ToInternalValue()); | 41 return base::Int64ToString(date.ToInternalValue()); |
41 } | 42 } |
42 | 43 |
43 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, | 44 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, |
44 const BookmarkNode* b) { | 45 const BookmarkNode* b, |
45 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b); | 46 bool creation_date_fallback) { |
47 return GetLastVisitDateForBookmark(a, creation_date_fallback) > | |
48 GetLastVisitDateForBookmark(b, creation_date_fallback); | |
46 } | 49 } |
47 | 50 |
48 } // namespace | 51 } // namespace |
49 | 52 |
50 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, | 53 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, |
51 const GURL& url) { | 54 const GURL& url) { |
52 std::vector<const BookmarkNode*> bookmarks_for_url; | 55 std::vector<const BookmarkNode*> bookmarks_for_url; |
53 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | 56 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); |
54 if (bookmarks_for_url.empty()) | 57 if (bookmarks_for_url.empty()) |
55 return; | 58 return; |
56 | 59 |
57 // If there are bookmarks for |url|, set their last visit date to now. | 60 // If there are bookmarks for |url|, set their last visit date to now. |
58 std::string now = FormatLastVisitDate(base::Time::Now()); | 61 std::string now = FormatLastVisitDate(base::Time::Now()); |
59 for (const BookmarkNode* node : bookmarks_for_url) { | 62 for (const BookmarkNode* node : bookmarks_for_url) { |
60 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); | 63 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); |
61 // If the bookmark has been dismissed from NTP before, a new visit overrides | 64 // If the bookmark has been dismissed from NTP before, a new visit overrides |
62 // such a dismission. | 65 // such a dismission. |
63 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 66 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
64 } | 67 } |
65 } | 68 } |
66 | 69 |
67 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node, | 70 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node, |
68 bool creation_date_fallback) { | 71 bool creation_date_fallback) { |
69 if (!node) | 72 if (!node) |
70 return base::Time::UnixEpoch(); | 73 return base::Time::UnixEpoch(); |
71 | 74 |
72 std::string last_visit_date_string; | 75 std::string last_visit_date_string; |
73 if (!node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string) && | 76 if (!node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string) && |
74 creation_date_fallback) | 77 creation_date_fallback) { |
75 return node->date_added(); | 78 return node->date_added(); |
76 | 79 } |
77 return ParseLastVisitDate(last_visit_date_string); | 80 return ParseLastVisitDate(last_visit_date_string); |
78 } | 81 } |
79 | 82 |
80 base::Time GetLastVisitDateForBookmarkIfNotDismissed( | 83 base::Time GetLastVisitDateForBookmarkIfNotDismissed( |
81 const BookmarkNode* node, | 84 const BookmarkNode* node, |
82 bool creation_date_fallback) { | 85 bool creation_date_fallback) { |
83 if (IsDismissedFromNTPForBookmark(node)) | 86 if (IsDismissedFromNTPForBookmark(node)) |
84 return base::Time::UnixEpoch(); | 87 return base::Time::UnixEpoch(); |
85 | 88 |
86 return GetLastVisitDateForBookmark(node, creation_date_fallback); | 89 return GetLastVisitDateForBookmark(node, creation_date_fallback); |
(...skipping 28 matching lines...) Expand all Loading... | |
115 bookmark_model->GetNodesByURL(bookmark.url, &nodes); | 118 bookmark_model->GetNodesByURL(bookmark.url, &nodes); |
116 for (const BookmarkNode* node : nodes) | 119 for (const BookmarkNode* node : nodes) |
117 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 120 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
118 } | 121 } |
119 } | 122 } |
120 | 123 |
121 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | 124 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
122 BookmarkModel* bookmark_model, | 125 BookmarkModel* bookmark_model, |
123 int min_count, | 126 int min_count, |
124 int max_count, | 127 int max_count, |
125 const base::Time& min_visit_time) { | 128 const base::Time& min_visit_time, |
129 bool creation_date_fallback) { | |
126 // Get all the bookmark URLs. | 130 // Get all the bookmark URLs. |
127 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; | 131 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; |
128 bookmark_model->GetBookmarks(&bookmark_urls); | 132 bookmark_model->GetBookmarks(&bookmark_urls); |
129 | 133 |
130 std::vector<RecentBookmark> bookmarks; | 134 std::vector<RecentBookmark> bookmarks; |
131 int recently_visited_count = 0; | 135 int recently_visited_count = 0; |
132 // Find for each bookmark the most recently visited BookmarkNode and find out | 136 // Find for each bookmark the most recently visited BookmarkNode and find out |
133 // whether it is visited since |min_visit_time|. | 137 // whether it is visited since |min_visit_time|. |
134 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { | 138 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { |
135 // Get all bookmarks for the given URL. | 139 // Get all bookmarks for the given URL. |
136 std::vector<const BookmarkNode*> bookmarks_for_url; | 140 std::vector<const BookmarkNode*> bookmarks_for_url; |
137 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); | 141 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); |
142 DCHECK(!bookmarks_for_url.empty()); | |
138 | 143 |
139 // Find the most recent node (minimal w.r.t. | 144 // Find the most recent node (minimal w.r.t. |
140 // CompareBookmarksByLastVisitDate). | 145 // CompareBookmarksByLastVisitDate). |
141 std::vector<const BookmarkNode*>::iterator most_recent = | 146 std::vector<const BookmarkNode*>::iterator most_recent = std::min_element( |
142 std::min_element(bookmarks_for_url.begin(), bookmarks_for_url.end(), | 147 bookmarks_for_url.begin(), bookmarks_for_url.end(), |
143 &CompareBookmarksByLastVisitDate); | 148 [creation_date_fallback](const BookmarkNode* a, const BookmarkNode* b) { |
144 if (most_recent == bookmarks_for_url.end()) | 149 return CompareBookmarksByLastVisitDate(a, b, creation_date_fallback); |
145 continue; | 150 }); |
146 const BookmarkNode* node = *most_recent; | 151 const BookmarkNode* node = *most_recent; |
147 | 152 |
148 // Find out if it has been _visited_ recently enough. | 153 // Find out if it has been _visited_ recently enough. |
149 if (GetLastVisitDateForBookmarkIfNotDismissed( | 154 if (GetLastVisitDateForBookmarkIfNotDismissed( |
150 node, /*creation_date_fallback=*/false) > min_visit_time) { | 155 node, /*creation_date_fallback=*/false) > min_visit_time) { |
151 recently_visited_count++; | 156 recently_visited_count++; |
152 bookmarks.push_back({node, true}); | 157 bookmarks.push_back({node, true}); |
153 } else { | 158 } else { |
154 bookmarks.push_back({node, false}); | 159 bookmarks.push_back({node, false}); |
155 } | 160 } |
156 } | 161 } |
157 | 162 |
158 if (recently_visited_count < min_count) { | 163 if (recently_visited_count < min_count) { |
159 // Fill the list up to |min_count| but do not display more. | 164 // Fill the list up to |min_count| but do not display more. |
160 max_count = min_count; | 165 max_count = min_count; |
161 } else { | 166 } else { |
162 // Remove the bookmarks that are not recently visited; we do no need them. | 167 // Remove the bookmarks that are not recently visited; we do not need them. |
163 bookmarks.erase( | 168 bookmarks.erase( |
164 std::remove_if(bookmarks.begin(), bookmarks.end(), | 169 std::remove_if(bookmarks.begin(), bookmarks.end(), |
165 [](const RecentBookmark& bookmark) { | 170 [](const RecentBookmark& bookmark) { |
166 return !bookmark.visited_recently; | 171 return !bookmark.visited_recently; |
167 }), | 172 }), |
168 bookmarks.end()); | 173 bookmarks.end()); |
169 } | 174 } |
170 | 175 |
171 // Sort the remaining entries by date. | 176 // Sort the remaining entries by date. |
172 std::sort(bookmarks.begin(), bookmarks.end(), | 177 std::sort(bookmarks.begin(), bookmarks.end(), |
173 [](const RecentBookmark& a, const RecentBookmark& b) { | 178 [creation_date_fallback](const RecentBookmark& a, |
174 return CompareBookmarksByLastVisitDate(a.node, b.node); | 179 const RecentBookmark& b) { |
180 return CompareBookmarksByLastVisitDate(a.node, b.node, | |
181 creation_date_fallback); | |
175 }); | 182 }); |
176 | 183 |
177 // Insert the first |max_count| items from |bookmarks| into |result|. | 184 // Insert the first |max_count| items from |bookmarks| into |result|. |
178 std::vector<const BookmarkNode*> result; | 185 std::vector<const BookmarkNode*> result; |
179 for (const RecentBookmark& bookmark : bookmarks) { | 186 for (const RecentBookmark& bookmark : bookmarks) { |
180 result.push_back(bookmark.node); | 187 result.push_back(bookmark.node); |
181 if (result.size() >= static_cast<size_t>(max_count)) | 188 if (result.size() >= static_cast<size_t>(max_count)) |
182 break; | 189 break; |
183 } | 190 } |
184 return result; | 191 return result; |
185 } | 192 } |
186 | 193 |
187 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( | 194 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( |
188 BookmarkModel* bookmark_model) { | 195 BookmarkModel* bookmark_model) { |
189 // Get all the bookmark URLs. | 196 // Get all the bookmark URLs. |
190 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 197 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
191 bookmark_model->GetBookmarks(&bookmarks); | 198 bookmark_model->GetBookmarks(&bookmarks); |
192 | 199 |
193 // Remove the bookmark URLs which have at least one non-dismissed bookmark. | 200 // Remove the bookmark URLs which have at least one non-dismissed bookmark. |
194 bookmarks.erase( | 201 bookmarks.erase( |
195 std::remove_if( | 202 std::remove_if( |
196 bookmarks.begin(), bookmarks.end(), | 203 bookmarks.begin(), bookmarks.end(), |
197 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { | 204 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { |
198 std::vector<const BookmarkNode*> bookmarks_for_url; | 205 std::vector<const BookmarkNode*> bookmarks_for_url; |
199 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | 206 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
207 DCHECK(bookmarks_for_url.size()); | |
Bernhard Bauer
2016/08/19 17:37:32
Can you use .empty() here as well?
Philipp Keck
2016/08/19 17:43:21
Yes, obviously :-)
| |
200 | 208 |
201 for (const BookmarkNode* node : bookmarks_for_url) { | 209 for (const BookmarkNode* node : bookmarks_for_url) { |
202 if (!IsDismissedFromNTPForBookmark(node)) | 210 if (!IsDismissedFromNTPForBookmark(node)) |
203 return true; | 211 return true; |
204 } | 212 } |
205 return false; | 213 return false; |
206 }), | 214 }), |
207 bookmarks.end()); | 215 bookmarks.end()); |
208 | 216 |
209 // Insert into |result|. | 217 // Insert into |result|. |
210 std::vector<const BookmarkNode*> result; | 218 std::vector<const BookmarkNode*> result; |
211 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 219 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
212 result.push_back( | 220 result.push_back( |
213 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 221 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
214 } | 222 } |
215 return result; | 223 return result; |
216 } | 224 } |
217 | 225 |
218 } // namespace ntp_snippets | 226 } // namespace ntp_snippets |
OLD | NEW |