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

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

Issue 2200113002: [New CL] 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: Remove unnecessary explicit 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
« no previous file with comments | « components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/time/time.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "components/bookmarks/browser/bookmark_node.h"
15 #include "url/gurl.h"
16
17 using bookmarks::BookmarkModel;
18 using bookmarks::BookmarkNode;
19
20 namespace ntp_snippets {
21
22 namespace {
23
24 const char kBookmarkLastVisitDateKey[] = "last_visited";
25
26 base::Time ParseLastVisitDate(const std::string& date_string) {
27 int64_t date = 0;
28 if (!base::StringToInt64(date_string, &date))
29 return base::Time::UnixEpoch();
30 return base::Time::FromInternalValue(date);
31 }
32
33 std::string FormatLastVisitDate(const base::Time& date) {
34 return base::Int64ToString(date.ToInternalValue());
35 }
36
37 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a,
38 const BookmarkNode* b) {
39 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b);
40 }
41
42 } // namespace
43
44 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model,
45 const GURL& url) {
46 std::vector<const BookmarkNode*> bookmarks_for_url;
47 bookmark_model->GetNodesByURL(url, &bookmarks_for_url);
48 if (bookmarks_for_url.empty())
49 return;
50
51 // If there are bookmarks for |url|, set their last visit date to now.
52 std::string now = FormatLastVisitDate(base::Time::Now());
53 for (auto* node : bookmarks_for_url) {
54 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now);
55 }
56 }
57
58 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) {
59 if (!node)
60 return base::Time::UnixEpoch();
61
62 std::string last_visit_date_string;
63 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string);
64
65 if (last_visit_date_string.empty()) {
66 // Use creation date if no last visit info present.
67 return node->date_added();
68 }
69
70 return ParseLastVisitDate(last_visit_date_string);
71 }
72
73 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
74 BookmarkModel* bookmark_model,
75 int max_count,
76 const base::Time& min_visit_time) {
77 // Get all the bookmarks.
78 std::vector<BookmarkModel::URLAndTitle> bookmarks;
79 bookmark_model->GetBookmarks(&bookmarks);
80
81 // Remove bookmarks that have not been visited after |min_visit_time|.
82 bookmarks.erase(
83 std::remove_if(bookmarks.begin(), bookmarks.end(),
84 [&bookmark_model, &min_visit_time](
85 const BookmarkModel::URLAndTitle& bookmark) {
86 // Get all bookmarks for the given URL.
87 std::vector<const BookmarkNode*> bookmarks_for_url;
88 bookmark_model->GetNodesByURL(bookmark.url,
89 &bookmarks_for_url);
90
91 // Return false if there is a more recent visit time.
92 for (const auto* node : bookmarks_for_url) {
93 if (GetLastVisitDateForBookmark(node) > min_visit_time)
94 return false;
95 }
96 return true;
97 }),
98 bookmarks.end());
99
100 // Sort the remaining bookmarks by date.
101 std::sort(bookmarks.begin(), bookmarks.end(),
102 [&bookmark_model](const BookmarkModel::URLAndTitle& a,
103 const BookmarkModel::URLAndTitle& b) {
104 return CompareBookmarksByLastVisitDate(
105 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url),
106 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url));
107 });
108
109 // Insert the first |max_count| items from |bookmarks| into |result|.
110 std::vector<const BookmarkNode*> result;
111 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
112 result.push_back(
113 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url));
114 if (result.size() >= static_cast<size_t>(max_count))
115 break;
116 }
117 return result;
118 }
119
120 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698