Index: components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
diff --git a/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc b/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e66cf1f04d5373a91334f519fdaa4ec4072ed69c |
--- /dev/null |
+++ b/components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
+ |
+#include <algorithm> |
+#include <string> |
+#include <utility> |
+ |
+#include "base/strings/string_number_conversions.h" |
+ |
+using bookmarks::BookmarkModel; |
+using bookmarks::BookmarkNode; |
+ |
+namespace { |
+ |
+const char kBookmarkLastVisitDateKey[] = "last_visited"; |
+ |
+base::Time ParseLastVisitDate(const std::string& date_string) { |
+ int64_t date = 0; |
+ if (!base::StringToInt64(date_string, &date)) |
+ return base::Time::UnixEpoch(); |
+ return base::Time::FromInternalValue(date); |
+} |
+ |
+std::string FormatLastVisitDate(const base::Time& date) { |
+ return base::Int64ToString(date.ToInternalValue()); |
+} |
+ |
+bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, |
+ const BookmarkNode* b) { |
Marc Treib
2016/07/29 14:36:43
misaligned
jkrcal
2016/07/29 15:34:07
Done.
|
+ return (ntp_snippets::GetLastVisitDateForBookmark(a) - |
+ ntp_snippets::GetLastVisitDateForBookmark(b)) |
Marc Treib
2016/07/29 14:36:42
optionally, you could move the anonymous namespace
jkrcal
2016/07/29 15:34:07
Done.
|
+ .InMilliseconds() > 0; |
+} |
+ |
+} // namespace |
+ |
+namespace ntp_snippets { |
+ |
+void UpdateBookmarkOnURLVisitedInMainFrame( |
+ BookmarkModel* bookmark_model, |
Marc Treib
2016/07/29 14:36:43
fits on the previous line?
jkrcal
2016/07/29 15:34:07
Done.
|
+ const GURL& url) { |
+ const BookmarkNode* node = |
+ bookmark_model->GetMostRecentlyAddedUserNodeForURL(url); |
+ if (!node) |
+ return; |
+ |
+ // If there exists a bookmark for |url|, set its last visit date to now. |
+ bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, |
+ FormatLastVisitDate(base::Time::Now())); |
+} |
+ |
+base::Time GetLastVisitDateForBookmark( |
+ const BookmarkNode* node) { |
Marc Treib
2016/07/29 14:36:42
fits on the previous line?
jkrcal
2016/07/29 15:34:07
Done.
|
+ if (!node) |
+ return base::Time::UnixEpoch(); |
+ |
+ std::string last_visit_date_string; |
+ node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); |
+ |
+ if (last_visit_date_string.empty()) { |
+ // Use creation date if no last visit info present. |
+ return node->date_added(); |
+ } |
+ |
+ return ParseLastVisitDate(last_visit_date_string); |
+} |
+ |
+std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
+ BookmarkModel* bookmark_model, |
+ int max_count, |
+ const base::Time& min_visit_time) { |
+ // Get all the bookmarks. |
+ std::vector<BookmarkModel::URLAndTitle> all_bookmarks; |
Marc Treib
2016/07/29 14:36:43
nit: I'd just call this bookmarks, since you filte
jkrcal
2016/07/29 15:34:07
Done.
|
+ bookmark_model->GetBookmarks(&all_bookmarks); |
+ // Remove bookmarks that have not been visited after |min_time|. |
+ all_bookmarks.erase( |
+ std::remove_if( |
+ all_bookmarks.begin(), all_bookmarks.end(), |
+ [&bookmark_model, |
+ &min_visit_time](const BookmarkModel::URLAndTitle& bookmark) { |
+ return GetLastVisitDateForBookmark( |
+ bookmark_model->GetMostRecentlyAddedUserNodeForURL( |
+ bookmark.url)) < min_visit_time; |
+ }), |
+ all_bookmarks.end()); |
+ // Sort the remaining bookmarks by date. |
+ std::sort(all_bookmarks.begin(), all_bookmarks.end(), |
+ [&bookmark_model](const BookmarkModel::URLAndTitle& a, |
+ const BookmarkModel::URLAndTitle& b) { |
+ return CompareBookmarksByLastVisitDate( |
+ bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), |
+ bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); |
+ }); |
+ |
+ std::vector<const BookmarkNode*> bookmarks; |
Marc Treib
2016/07/29 14:36:43
Ah, then you'll also have to rename this... result
jkrcal
2016/07/29 15:34:07
Done.
|
+ // Insert the first |max_count| into |bookmarks|. |
+ for (const BookmarkModel::URLAndTitle& bookmark : all_bookmarks) { |
+ bookmarks.push_back( |
+ bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
+ if (bookmarks.size() >= (unsigned) max_count) |
Marc Treib
2016/07/29 14:36:43
static_cast<size_t> ? It's ugly, but at least it's
jkrcal
2016/07/29 15:34:07
Done.
|
+ break; |
+ } |
+ return bookmarks; |
+} |
+ |
+} // namespace ntp_snippets |