Chromium Code Reviews| 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/bind.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/bookmarks/browser/bookmark_model.h" | 14 #include "components/bookmarks/browser/bookmark_model.h" |
| 15 #include "components/bookmarks/browser/bookmark_node.h" | 15 #include "components/bookmarks/browser/bookmark_node.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 using bookmarks::BookmarkModel; | 18 using bookmarks::BookmarkModel; |
| 19 using bookmarks::BookmarkNode; | 19 using bookmarks::BookmarkNode; |
| 20 | 20 |
| 21 namespace ntp_snippets { | 21 namespace ntp_snippets { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 struct RecentBookmark { | |
| 26 const bookmarks::BookmarkNode* node; | |
| 27 bool visited_recently; | |
| 28 }; | |
| 29 | |
| 30 const char kBookmarkLastVisitDateKey[] = "last_visited"; | 25 const char kBookmarkLastVisitDateKey[] = "last_visited"; |
| 31 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; | 26 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; |
| 32 | 27 |
| 33 base::Time ParseLastVisitDate(const std::string& date_string) { | 28 base::Time ParseLastVisitDate(const std::string& date_string) { |
| 34 int64_t date = 0; | 29 int64_t date = 0; |
| 35 if (!base::StringToInt64(date_string, &date)) | 30 if (!base::StringToInt64(date_string, &date)) |
| 36 return base::Time::UnixEpoch(); | 31 return base::Time::UnixEpoch(); |
| 37 return base::Time::FromInternalValue(date); | 32 return base::Time::FromInternalValue(date); |
| 38 } | 33 } |
| 39 | 34 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | 119 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
| 125 BookmarkModel* bookmark_model, | 120 BookmarkModel* bookmark_model, |
| 126 int min_count, | 121 int min_count, |
| 127 int max_count, | 122 int max_count, |
| 128 const base::Time& min_visit_time, | 123 const base::Time& min_visit_time, |
| 129 bool creation_date_fallback) { | 124 bool creation_date_fallback) { |
| 130 // Get all the bookmark URLs. | 125 // Get all the bookmark URLs. |
| 131 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; | 126 std::vector<BookmarkModel::URLAndTitle> bookmark_urls; |
| 132 bookmark_model->GetBookmarks(&bookmark_urls); | 127 bookmark_model->GetBookmarks(&bookmark_urls); |
| 133 | 128 |
| 134 std::vector<RecentBookmark> bookmarks; | 129 std::vector<const BookmarkNode*> recent_not_dismissed_bookmarks; |
| 135 int recently_visited_count = 0; | |
| 136 // Find for each bookmark the most recently visited BookmarkNode and find out | 130 // Find for each bookmark the most recently visited BookmarkNode and find out |
| 137 // whether it is visited since |min_visit_time|. | 131 // whether it is visited since |min_visit_time|. |
| 138 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { | 132 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { |
| 139 // Get all bookmarks for the given URL. | 133 // Get all bookmarks for the given URL. |
| 140 std::vector<const BookmarkNode*> bookmarks_for_url; | 134 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 141 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); | 135 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); |
| 142 DCHECK(!bookmarks_for_url.empty()); | 136 DCHECK(!bookmarks_for_url.empty()); |
| 143 | 137 |
| 144 // Find the most recent node (minimal w.r.t. | 138 // Find the most recent node (minimal w.r.t. |
| 145 // CompareBookmarksByLastVisitDate). | 139 // CompareBookmarksByLastVisitDate). |
| 146 const BookmarkNode* most_recent = *std::min_element( | 140 const BookmarkNode* most_recent = *std::min_element( |
| 147 bookmarks_for_url.begin(), bookmarks_for_url.end(), | 141 bookmarks_for_url.begin(), bookmarks_for_url.end(), |
| 148 [creation_date_fallback](const BookmarkNode* a, const BookmarkNode* b) { | 142 [creation_date_fallback](const BookmarkNode* a, const BookmarkNode* b) { |
| 149 return CompareBookmarksByLastVisitDate(a, b, creation_date_fallback); | 143 return CompareBookmarksByLastVisitDate(a, b, creation_date_fallback); |
| 150 }); | 144 }); |
| 151 | 145 |
| 152 // Find out if it has been _visited_ recently enough. | 146 if (min_visit_time < GetLastVisitDateForBookmark( |
| 153 bool visited_recently = | 147 most_recent, /* creation_date_fallback */ false) && |
| 154 min_visit_time < GetLastVisitDateForBookmark( | 148 !IsDismissedFromNTPForBookmark(most_recent)) { |
| 155 most_recent, /*creation_date_fallback=*/false); | 149 recent_not_dismissed_bookmarks.push_back(most_recent); |
| 156 if (visited_recently) | 150 } |
| 157 recently_visited_count++; | |
| 158 if (!IsDismissedFromNTPForBookmark(most_recent)) | |
| 159 bookmarks.push_back({most_recent, visited_recently}); | |
|
Marc Treib
2016/09/01 09:46:01
We want to keep this, so that the creation_date_fa
vitaliii
2016/09/01 10:15:41
Done.
| |
| 160 } | 151 } |
| 161 | 152 |
| 162 if (recently_visited_count < min_count) { | 153 if (recent_not_dismissed_bookmarks.size() < static_cast<size_t>(min_count)) { |
| 163 // There aren't enough recently-visited bookmarks. Fill the list up to | 154 // There aren't enough recently-visited bookmarks, do not show any. |
|
Marc Treib
2016/09/01 09:46:01
This is not the behavior we want (sorry, I must ha
vitaliii
2016/09/01 10:15:41
Done.
| |
| 164 // |min_count| with older bookmarks (in particular those with only a | 155 return std::vector<const BookmarkNode*>(); |
| 165 // creation date, if creation_date_fallback is true). | |
| 166 max_count = min_count; | |
| 167 } else { | 156 } else { |
| 168 // Remove the bookmarks that are not recently visited; we do not need them. | 157 // Sort the bookmarks we can show by date. |
| 169 // (We might end up with fewer than |min_count| bookmarks if all the recent | 158 std::sort(recent_not_dismissed_bookmarks.begin(), |
| 170 // ones are dismissed.) | 159 recent_not_dismissed_bookmarks.end(), |
| 171 bookmarks.erase( | 160 [creation_date_fallback](const BookmarkNode* first, |
| 172 std::remove_if(bookmarks.begin(), bookmarks.end(), | 161 const BookmarkNode* second) { |
| 173 [](const RecentBookmark& bookmark) { | 162 return CompareBookmarksByLastVisitDate(first, second, |
| 174 return !bookmark.visited_recently; | 163 creation_date_fallback); |
| 175 }), | 164 }); |
| 176 bookmarks.end()); | 165 |
| 166 // Return the first |max_count| items from |recent_not_dismissed_bookmarks|. | |
| 167 if (recent_not_dismissed_bookmarks.size() <= | |
| 168 static_cast<size_t>(max_count)) { | |
| 169 return recent_not_dismissed_bookmarks; | |
| 170 } else { | |
| 171 return std::vector<const BookmarkNode*>( | |
| 172 recent_not_dismissed_bookmarks.begin(), | |
| 173 recent_not_dismissed_bookmarks.begin() + max_count); | |
| 174 } | |
| 177 } | 175 } |
| 178 | |
| 179 // Sort the remaining entries by date. | |
| 180 std::sort(bookmarks.begin(), bookmarks.end(), | |
| 181 [creation_date_fallback](const RecentBookmark& a, | |
| 182 const RecentBookmark& b) { | |
| 183 return CompareBookmarksByLastVisitDate(a.node, b.node, | |
| 184 creation_date_fallback); | |
| 185 }); | |
| 186 | |
| 187 // Insert the first |max_count| items from |bookmarks| into |result|. | |
|
Marc Treib
2016/09/01 09:46:01
I think the problem is here: We're missing a check
vitaliii
2016/09/01 10:15:41
Done.
| |
| 188 std::vector<const BookmarkNode*> result; | |
| 189 for (const RecentBookmark& bookmark : bookmarks) { | |
| 190 result.push_back(bookmark.node); | |
| 191 if (result.size() >= static_cast<size_t>(max_count)) | |
| 192 break; | |
| 193 } | |
| 194 return result; | |
| 195 } | 176 } |
| 196 | 177 |
| 197 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( | 178 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( |
| 198 BookmarkModel* bookmark_model) { | 179 BookmarkModel* bookmark_model) { |
| 199 // Get all the bookmark URLs. | 180 // Get all the bookmark URLs. |
| 200 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 181 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 201 bookmark_model->GetBookmarks(&bookmarks); | 182 bookmark_model->GetBookmarks(&bookmarks); |
| 202 | 183 |
| 203 // Remove the bookmark URLs which have at least one non-dismissed bookmark. | 184 // Remove the bookmark URLs which have at least one non-dismissed bookmark. |
| 204 bookmarks.erase( | 185 bookmarks.erase( |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 220 // Insert into |result|. | 201 // Insert into |result|. |
| 221 std::vector<const BookmarkNode*> result; | 202 std::vector<const BookmarkNode*> result; |
| 222 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 203 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 223 result.push_back( | 204 result.push_back( |
| 224 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 205 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| 225 } | 206 } |
| 226 return result; | 207 return result; |
| 227 } | 208 } |
| 228 | 209 |
| 229 } // namespace ntp_snippets | 210 } // namespace ntp_snippets |
| OLD | NEW |