OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Marc Treib
2016/07/29 09:54:57
.
jkrcal
2016/07/29 12:42:41
Done.
| |
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_date_helper.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/strings/string_number_conversions.h" | |
11 | |
12 namespace { | |
13 const char kBookmarkLastVisitDate[] = "last_visited"; | |
Marc Treib
2016/07/29 09:54:57
Add "Key" or something like that?
Marc Treib
2016/07/29 09:54:57
nit: empty line before
jkrcal
2016/07/29 12:42:41
Done (both).
| |
14 | |
15 base::Time ParseLastVisitDate(const std::string& date_string) { | |
16 int64_t date; | |
Marc Treib
2016/07/29 09:54:57
Initialize this? (I think the Win compiler will co
jkrcal
2016/07/29 12:42:41
Done.
| |
17 if (!base::StringToInt64(date_string, &date)) | |
18 return base::Time::UnixEpoch(); | |
Marc Treib
2016/07/29 09:54:57
just base::Time() ?
jkrcal
2016/07/29 12:42:41
Result of base::Time() is platform-dependent (can
Marc Treib
2016/07/29 14:36:42
The advantage would be that you could check with .
jkrcal
2016/07/29 15:34:06
I see. No, it does not matter here.
| |
19 return base::Time::FromInternalValue(date); | |
20 } | |
21 | |
22 std::string FormatLastVisitDate(const base::Time& date) { | |
23 return base::Int64ToString(date.ToInternalValue()); | |
24 } | |
25 | |
26 base::Time GetLastVisitDateImpl(const bookmarks::BookmarkNode* node) { | |
27 if (node == nullptr) | |
Marc Treib
2016/07/29 09:54:57
if (!node)
jkrcal
2016/07/29 12:42:41
Done.
| |
28 return base::Time::UnixEpoch(); | |
Marc Treib
2016/07/29 09:54:57
Here too, just base::Time() ?
jkrcal
2016/07/29 12:42:41
Same as above.
| |
29 | |
30 std::string last_visit_date_string; | |
31 node->GetMetaInfo(kBookmarkLastVisitDate, &last_visit_date_string); | |
32 | |
33 if (last_visit_date_string.empty()) { | |
34 // Use creation date if no last visit info present. | |
35 return node->date_added(); | |
36 } | |
37 | |
38 return ParseLastVisitDate(last_visit_date_string); | |
39 } | |
40 | |
41 int CompareBookmarksByLastVisitDate(const bookmarks::BookmarkNode* a, | |
42 const bookmarks::BookmarkNode* b) { | |
43 return (GetLastVisitDateImpl(a) - GetLastVisitDateImpl(b)).InMilliseconds() > | |
Marc Treib
2016/07/29 09:54:57
return GetLastVisitDateImpl(a) > GetLastVisitDateI
jkrcal
2016/07/29 12:42:41
error: cannot convert 'base::TimeDelta' to 'int' i
Marc Treib
2016/07/29 14:36:42
">", not "-".
jkrcal
2016/07/29 15:34:06
Done.
| |
44 0; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 namespace ntp_snippets { | |
50 | |
51 void BookmarkLastVisitDateHelper::OnURLVisitedInMainFrame( | |
52 BookmarkModel* bookmark_model, | |
53 const GURL& url) { | |
54 | |
Marc Treib
2016/07/29 09:54:57
nit: no empty line
jkrcal
2016/07/29 12:42:41
Done.
| |
55 const bookmarks::BookmarkNode* node = | |
56 bookmark_model->GetMostRecentlyAddedUserNodeForURL(url); | |
57 if (node == nullptr) | |
Marc Treib
2016/07/29 09:54:57
if (!node)
jkrcal
2016/07/29 12:42:41
Done.
| |
58 return; | |
59 | |
60 // If there exists a bookmark for |url|, set its last visit date to now. | |
61 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDate, | |
62 FormatLastVisitDate(base::Time::Now())); | |
63 } | |
64 | |
65 // static | |
66 base::Time BookmarkLastVisitDateHelper::GetLastVisitDate( | |
67 const bookmarks::BookmarkNode* node) { | |
68 return GetLastVisitDateImpl(node); | |
Marc Treib
2016/07/29 09:54:57
Just put the code here and get rid of the Impl?
jkrcal
2016/07/29 12:42:41
Done.
| |
69 } | |
70 | |
71 // static | |
72 void BookmarkLastVisitDateHelper::GetRecentlyVisitedBookmarks( | |
73 BookmarkModel* bookmark_model, | |
74 std::vector<const bookmarks::BookmarkNode*>* bookmarks, | |
75 int max_count, | |
76 const base::Time& min_time) { | |
77 // Get all the bookmarks. | |
78 std::vector<BookmarkModel::URLAndTitle> all_bookmarks; | |
79 bookmark_model->GetBookmarks(&all_bookmarks); | |
80 // Remove bookmarks that have not been visited after |min_time|. | |
81 all_bookmarks.erase( | |
82 std::remove_if( | |
83 all_bookmarks.begin(), all_bookmarks.end(), | |
84 [&](const BookmarkModel::URLAndTitle& bookmark) { | |
Marc Treib
2016/07/29 09:54:57
Don't use default captures; capture only what you
jkrcal
2016/07/29 12:42:41
Done.
| |
85 return GetLastVisitDateImpl( | |
86 bookmark_model->GetMostRecentlyAddedUserNodeForURL( | |
Marc Treib
2016/07/29 09:54:57
Can this return null here?
Also I'm not sure what
jkrcal
2016/07/29 12:42:41
It can return null but GetLastVisitDate() handles
Marc Treib
2016/07/29 14:36:42
My point is mostly: Could the "MostRecentlyAdded"
jkrcal
2016/07/29 15:34:06
Correct, thanks! I changed it to consider the most
| |
87 bookmark.url)) < min_time; | |
88 }), | |
89 all_bookmarks.end()); | |
90 // Sort the remaining bookmarks by date. | |
91 std::sort(all_bookmarks.begin(), all_bookmarks.end(), | |
92 [&](const BookmarkModel::URLAndTitle& a, | |
93 const BookmarkModel::URLAndTitle& b) { | |
94 return CompareBookmarksByLastVisitDate( | |
95 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), | |
96 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); | |
97 }); | |
98 // Insert the first |max_count| into |bookmarks|. | |
99 int count = 0; | |
100 for (const BookmarkModel::URLAndTitle& bookmark : all_bookmarks) { | |
101 bookmarks->push_back( | |
102 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | |
103 count++; | |
104 if (count > max_count) | |
Marc Treib
2016/07/29 09:54:57
Just check bookmarks.size() and get rid of count?
jkrcal
2016/07/29 12:42:41
Done.
| |
105 break; | |
106 } | |
107 } | |
108 | |
109 } // namespace ntp_snippets | |
OLD | NEW |