| 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 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 return base::Time::UnixEpoch(); | 35 return base::Time::UnixEpoch(); |
| 36 return base::Time::FromInternalValue(date); | 36 return base::Time::FromInternalValue(date); |
| 37 } | 37 } |
| 38 | 38 |
| 39 std::string FormatLastVisitDate(const base::Time& date) { | 39 std::string FormatLastVisitDate(const base::Time& date) { |
| 40 return base::Int64ToString(date.ToInternalValue()); | 40 return base::Int64ToString(date.ToInternalValue()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, | 43 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, |
| 44 const BookmarkNode* b) { | 44 const BookmarkNode* b) { |
| 45 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b); | 45 return GetLastVisitDateForBookmark(a, /*creation_date_fallback=*/true) > |
| 46 GetLastVisitDateForBookmark(b, /*creation_date_fallback=*/true); |
| 46 } | 47 } |
| 47 | 48 |
| 48 } // namespace | 49 } // namespace |
| 49 | 50 |
| 50 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, | 51 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, |
| 51 const GURL& url) { | 52 const GURL& url) { |
| 52 std::vector<const BookmarkNode*> bookmarks_for_url; | 53 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 53 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | 54 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); |
| 54 if (bookmarks_for_url.empty()) | 55 if (bookmarks_for_url.empty()) |
| 55 return; | 56 return; |
| 56 | 57 |
| 57 // If there are bookmarks for |url|, set their last visit date to now. | 58 // If there are bookmarks for |url|, set their last visit date to now. |
| 58 std::string now = FormatLastVisitDate(base::Time::Now()); | 59 std::string now = FormatLastVisitDate(base::Time::Now()); |
| 59 for (const BookmarkNode* node : bookmarks_for_url) { | 60 for (const BookmarkNode* node : bookmarks_for_url) { |
| 60 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); | 61 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); |
| 61 // If the bookmark has been dismissed from NTP before, a new visit overrides | 62 // If the bookmark has been dismissed from NTP before, a new visit overrides |
| 62 // such a dismission. | 63 // such a dismission. |
| 63 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 64 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 | 67 |
| 67 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node, | 68 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node, |
| 68 bool creation_date_fallback) { | 69 bool creation_date_fallback) { |
| 69 if (!node) | 70 if (!node) |
| 70 return base::Time::UnixEpoch(); | 71 return base::Time::UnixEpoch(); |
| 71 | 72 |
| 72 std::string last_visit_date_string; | 73 std::string last_visit_date_string; |
| 73 if (!node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string) && | 74 if (!node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string) && |
| 74 creation_date_fallback) | 75 creation_date_fallback) { |
| 75 return node->date_added(); | 76 return node->date_added(); |
| 76 | 77 } |
| 77 return ParseLastVisitDate(last_visit_date_string); | 78 return ParseLastVisitDate(last_visit_date_string); |
| 78 } | 79 } |
| 79 | 80 |
| 80 base::Time GetLastVisitDateForBookmarkIfNotDismissed( | 81 base::Time GetLastVisitDateForBookmarkIfNotDismissed( |
| 81 const BookmarkNode* node, | 82 const BookmarkNode* node, |
| 82 bool creation_date_fallback) { | 83 bool creation_date_fallback) { |
| 83 if (IsDismissedFromNTPForBookmark(node)) | 84 if (IsDismissedFromNTPForBookmark(node)) |
| 84 return base::Time::UnixEpoch(); | 85 return base::Time::UnixEpoch(); |
| 85 | 86 |
| 86 return GetLastVisitDateForBookmark(node, creation_date_fallback); | 87 return GetLastVisitDateForBookmark(node, creation_date_fallback); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { | 135 for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) { |
| 135 // Get all bookmarks for the given URL. | 136 // Get all bookmarks for the given URL. |
| 136 std::vector<const BookmarkNode*> bookmarks_for_url; | 137 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 137 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); | 138 bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url); |
| 138 | 139 |
| 139 // Find the most recent node (minimal w.r.t. | 140 // Find the most recent node (minimal w.r.t. |
| 140 // CompareBookmarksByLastVisitDate). | 141 // CompareBookmarksByLastVisitDate). |
| 141 std::vector<const BookmarkNode*>::iterator most_recent = | 142 std::vector<const BookmarkNode*>::iterator most_recent = |
| 142 std::min_element(bookmarks_for_url.begin(), bookmarks_for_url.end(), | 143 std::min_element(bookmarks_for_url.begin(), bookmarks_for_url.end(), |
| 143 &CompareBookmarksByLastVisitDate); | 144 &CompareBookmarksByLastVisitDate); |
| 144 if (most_recent == bookmarks_for_url.end()) | |
| 145 continue; | |
| 146 const BookmarkNode* node = *most_recent; | 145 const BookmarkNode* node = *most_recent; |
| 147 | 146 |
| 148 // Find out if it has been _visited_ recently enough. | 147 // Find out if it has been _visited_ recently enough. |
| 149 if (GetLastVisitDateForBookmarkIfNotDismissed( | 148 if (GetLastVisitDateForBookmarkIfNotDismissed( |
| 150 node, /*creation_date_fallback=*/false) > min_visit_time) { | 149 node, /*creation_date_fallback=*/false) > min_visit_time) { |
| 151 recently_visited_count++; | 150 recently_visited_count++; |
| 152 bookmarks.push_back({node, true}); | 151 bookmarks.push_back({node, true}); |
| 153 } else { | 152 } else { |
| 154 bookmarks.push_back({node, false}); | 153 bookmarks.push_back({node, false}); |
| 155 } | 154 } |
| 156 } | 155 } |
| 157 | 156 |
| 158 if (recently_visited_count < min_count) { | 157 if (recently_visited_count < min_count) { |
| 159 // Fill the list up to |min_count| but do not display more. | 158 // Fill the list up to |min_count| but do not display more. |
| 160 max_count = min_count; | 159 max_count = min_count; |
| 161 } else { | 160 } else { |
| 162 // Remove the bookmarks that are not recently visited; we do no need them. | 161 // Remove the bookmarks that are not recently visited; we do not need them. |
| 163 bookmarks.erase( | 162 bookmarks.erase( |
| 164 std::remove_if(bookmarks.begin(), bookmarks.end(), | 163 std::remove_if(bookmarks.begin(), bookmarks.end(), |
| 165 [](const RecentBookmark& bookmark) { | 164 [](const RecentBookmark& bookmark) { |
| 166 return !bookmark.visited_recently; | 165 return !bookmark.visited_recently; |
| 167 }), | 166 }), |
| 168 bookmarks.end()); | 167 bookmarks.end()); |
| 169 } | 168 } |
| 170 | 169 |
| 171 // Sort the remaining entries by date. | 170 // Sort the remaining entries by date. |
| 172 std::sort(bookmarks.begin(), bookmarks.end(), | 171 std::sort(bookmarks.begin(), bookmarks.end(), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 // Insert into |result|. | 208 // Insert into |result|. |
| 210 std::vector<const BookmarkNode*> result; | 209 std::vector<const BookmarkNode*> result; |
| 211 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 210 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 212 result.push_back( | 211 result.push_back( |
| 213 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 212 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| 214 } | 213 } |
| 215 return result; | 214 return result; |
| 216 } | 215 } |
| 217 | 216 |
| 218 } // namespace ntp_snippets | 217 } // namespace ntp_snippets |
| OLD | NEW |