Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc

Issue 2184263005: Add a tab helper to record the last visit date for each bookmark. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments #1 Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
17
18 const char kBookmarkLastVisitDateKey[] = "last_visited";
19
20 base::Time ParseLastVisitDate(const std::string& date_string) {
21 int64_t date = 0;
22 if (!base::StringToInt64(date_string, &date))
23 return base::Time::UnixEpoch();
24 return base::Time::FromInternalValue(date);
25 }
26
27 std::string FormatLastVisitDate(const base::Time& date) {
28 return base::Int64ToString(date.ToInternalValue());
29 }
30
31 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a,
32 const BookmarkNode* b) {
Marc Treib 2016/07/29 14:36:43 misaligned
jkrcal 2016/07/29 15:34:07 Done.
33 return (ntp_snippets::GetLastVisitDateForBookmark(a) -
34 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.
35 .InMilliseconds() > 0;
36 }
37
38 } // namespace
39
40 namespace ntp_snippets {
41
42 void UpdateBookmarkOnURLVisitedInMainFrame(
43 BookmarkModel* bookmark_model,
Marc Treib 2016/07/29 14:36:43 fits on the previous line?
jkrcal 2016/07/29 15:34:07 Done.
44 const GURL& url) {
45 const BookmarkNode* node =
46 bookmark_model->GetMostRecentlyAddedUserNodeForURL(url);
47 if (!node)
48 return;
49
50 // If there exists a bookmark for |url|, set its last visit date to now.
51 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey,
52 FormatLastVisitDate(base::Time::Now()));
53 }
54
55 base::Time GetLastVisitDateForBookmark(
56 const BookmarkNode* node) {
Marc Treib 2016/07/29 14:36:42 fits on the previous line?
jkrcal 2016/07/29 15:34:07 Done.
57 if (!node)
58 return base::Time::UnixEpoch();
59
60 std::string last_visit_date_string;
61 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string);
62
63 if (last_visit_date_string.empty()) {
64 // Use creation date if no last visit info present.
65 return node->date_added();
66 }
67
68 return ParseLastVisitDate(last_visit_date_string);
69 }
70
71 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
72 BookmarkModel* bookmark_model,
73 int max_count,
74 const base::Time& min_visit_time) {
75 // Get all the bookmarks.
76 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.
77 bookmark_model->GetBookmarks(&all_bookmarks);
78 // Remove bookmarks that have not been visited after |min_time|.
79 all_bookmarks.erase(
80 std::remove_if(
81 all_bookmarks.begin(), all_bookmarks.end(),
82 [&bookmark_model,
83 &min_visit_time](const BookmarkModel::URLAndTitle& bookmark) {
84 return GetLastVisitDateForBookmark(
85 bookmark_model->GetMostRecentlyAddedUserNodeForURL(
86 bookmark.url)) < min_visit_time;
87 }),
88 all_bookmarks.end());
89 // Sort the remaining bookmarks by date.
90 std::sort(all_bookmarks.begin(), all_bookmarks.end(),
91 [&bookmark_model](const BookmarkModel::URLAndTitle& a,
92 const BookmarkModel::URLAndTitle& b) {
93 return CompareBookmarksByLastVisitDate(
94 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url),
95 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url));
96 });
97
98 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.
99 // Insert the first |max_count| into |bookmarks|.
100 for (const BookmarkModel::URLAndTitle& bookmark : all_bookmarks) {
101 bookmarks.push_back(
102 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url));
103 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.
104 break;
105 }
106 return bookmarks;
107 }
108
109 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698