| 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/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "components/bookmarks/browser/bookmark_model.h" | 13 #include "components/bookmarks/browser/bookmark_model.h" |
| 14 #include "components/bookmarks/browser/bookmark_node.h" | 14 #include "components/bookmarks/browser/bookmark_node.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 | 16 |
| 17 using bookmarks::BookmarkModel; | 17 using bookmarks::BookmarkModel; |
| 18 using bookmarks::BookmarkNode; | 18 using bookmarks::BookmarkNode; |
| 19 | 19 |
| 20 namespace ntp_snippets { | 20 namespace ntp_snippets { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kBookmarkLastVisitDateKey[] = "last_visited"; | 24 const char kBookmarkLastVisitDateKey[] = "last_visited"; |
| 25 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; |
| 25 | 26 |
| 26 base::Time ParseLastVisitDate(const std::string& date_string) { | 27 base::Time ParseLastVisitDate(const std::string& date_string) { |
| 27 int64_t date = 0; | 28 int64_t date = 0; |
| 28 if (!base::StringToInt64(date_string, &date)) | 29 if (!base::StringToInt64(date_string, &date)) |
| 29 return base::Time::UnixEpoch(); | 30 return base::Time::UnixEpoch(); |
| 30 return base::Time::FromInternalValue(date); | 31 return base::Time::FromInternalValue(date); |
| 31 } | 32 } |
| 32 | 33 |
| 33 std::string FormatLastVisitDate(const base::Time& date) { | 34 std::string FormatLastVisitDate(const base::Time& date) { |
| 34 return base::Int64ToString(date.ToInternalValue()); | 35 return base::Int64ToString(date.ToInternalValue()); |
| 35 } | 36 } |
| 36 | 37 |
| 37 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, | 38 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, |
| 38 const BookmarkNode* b) { | 39 const BookmarkNode* b) { |
| 39 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b); | 40 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b); |
| 40 } | 41 } |
| 41 | 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, | 45 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, |
| 45 const GURL& url) { | 46 const GURL& url) { |
| 46 std::vector<const BookmarkNode*> bookmarks_for_url; | 47 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 47 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | 48 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); |
| 48 if (bookmarks_for_url.empty()) | 49 if (bookmarks_for_url.empty()) |
| 49 return; | 50 return; |
| 50 | 51 |
| 51 // If there are bookmarks for |url|, set their last visit date to now. | 52 // If there are bookmarks for |url|, set their last visit date to now. |
| 52 std::string now = FormatLastVisitDate(base::Time::Now()); | 53 std::string now = FormatLastVisitDate(base::Time::Now()); |
| 53 for (auto* node : bookmarks_for_url) { | 54 for (const BookmarkNode* node : bookmarks_for_url) |
| 54 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); | 55 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); |
| 55 } | |
| 56 } | 56 } |
| 57 | 57 |
| 58 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { | 58 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { |
| 59 if (!node) | 59 if (!node) |
| 60 return base::Time::UnixEpoch(); | 60 return base::Time::UnixEpoch(); |
| 61 | 61 |
| 62 std::string last_visit_date_string; | 62 std::string last_visit_date_string; |
| 63 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); | 63 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); |
| 64 | 64 |
| 65 if (last_visit_date_string.empty()) { | 65 // Use creation date if no last visit info present. |
| 66 // Use creation date if no last visit info present. | 66 if (last_visit_date_string.empty()) |
| 67 return node->date_added(); | 67 return node->date_added(); |
| 68 } | |
| 69 | 68 |
| 70 return ParseLastVisitDate(last_visit_date_string); | 69 return ParseLastVisitDate(last_visit_date_string); |
| 71 } | 70 } |
| 72 | 71 |
| 72 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
| 73 std::vector<const BookmarkNode*> nodes; |
| 74 bookmark_model->GetNodesByURL(url, &nodes); |
| 75 for (const BookmarkNode* node : nodes) |
| 76 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
| 77 } |
| 78 |
| 79 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { |
| 80 if (!node) |
| 81 return false; |
| 82 |
| 83 std::string dismissed_from_ntp; |
| 84 bool result = |
| 85 node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); |
| 86 DCHECK(!result || dismissed_from_ntp == "1"); |
| 87 return result; |
| 88 } |
| 89 |
| 90 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { |
| 91 // Get all the bookmark URLs. |
| 92 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 93 bookmark_model->GetBookmarks(&bookmarks); |
| 94 |
| 95 // Remove dismissed flag from all bookmarks |
| 96 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 97 std::vector<const BookmarkNode*> nodes; |
| 98 bookmark_model->GetNodesByURL(bookmark.url, &nodes); |
| 99 for (const BookmarkNode* node : nodes) |
| 100 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
| 101 } |
| 102 } |
| 103 |
| 73 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | 104 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
| 74 BookmarkModel* bookmark_model, | 105 BookmarkModel* bookmark_model, |
| 75 int max_count, | 106 int max_count, |
| 76 const base::Time& min_visit_time) { | 107 const base::Time& min_visit_time) { |
| 77 // Get all the bookmarks. | 108 // Get all the bookmark URLs. |
| 78 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 109 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 79 bookmark_model->GetBookmarks(&bookmarks); | 110 bookmark_model->GetBookmarks(&bookmarks); |
| 80 | 111 |
| 81 // Remove bookmarks that have not been visited after |min_visit_time|. | 112 // Remove the bookmark URLs whose bookmarks are all dismissed or whose most |
| 113 // recent visit is older than |min_visit_time|. |
| 82 bookmarks.erase( | 114 bookmarks.erase( |
| 83 std::remove_if(bookmarks.begin(), bookmarks.end(), | 115 std::remove_if(bookmarks.begin(), bookmarks.end(), |
| 84 [&bookmark_model, &min_visit_time]( | 116 [&bookmark_model, &min_visit_time]( |
| 85 const BookmarkModel::URLAndTitle& bookmark) { | 117 const BookmarkModel::URLAndTitle& bookmark) { |
| 86 // Get all bookmarks for the given URL. | 118 // Get all bookmarks for the given URL. |
| 87 std::vector<const BookmarkNode*> bookmarks_for_url; | 119 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 88 bookmark_model->GetNodesByURL(bookmark.url, | 120 bookmark_model->GetNodesByURL(bookmark.url, |
| 89 &bookmarks_for_url); | 121 &bookmarks_for_url); |
| 90 | 122 |
| 91 // Return false if there is a more recent visit time. | 123 // Check if there is a recently visited bookmark and not |
| 92 for (const auto* node : bookmarks_for_url) { | 124 // all bookmarks are dismissed. |
| 125 bool has_recent_visit = false; |
| 126 bool all_dismissed = true; |
| 127 for (const BookmarkNode* node : bookmarks_for_url) { |
| 93 if (GetLastVisitDateForBookmark(node) > min_visit_time) | 128 if (GetLastVisitDateForBookmark(node) > min_visit_time) |
| 94 return false; | 129 has_recent_visit = true; |
| 130 if (!IsDismissedFromNTPForBookmark(node)) |
| 131 all_dismissed = false; |
| 95 } | 132 } |
| 96 return true; | 133 return all_dismissed || !has_recent_visit; |
| 97 }), | 134 }), |
| 98 bookmarks.end()); | 135 bookmarks.end()); |
| 99 | 136 |
| 100 // Sort the remaining bookmarks by date. | 137 // Sort the remaining bookmarks by date. |
| 101 std::sort(bookmarks.begin(), bookmarks.end(), | 138 std::sort(bookmarks.begin(), bookmarks.end(), |
| 102 [&bookmark_model](const BookmarkModel::URLAndTitle& a, | 139 [&bookmark_model](const BookmarkModel::URLAndTitle& a, |
| 103 const BookmarkModel::URLAndTitle& b) { | 140 const BookmarkModel::URLAndTitle& b) { |
| 104 return CompareBookmarksByLastVisitDate( | 141 return CompareBookmarksByLastVisitDate( |
| 105 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), | 142 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), |
| 106 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); | 143 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); |
| 107 }); | 144 }); |
| 108 | 145 |
| 109 // Insert the first |max_count| items from |bookmarks| into |result|. | 146 // Insert the first |max_count| items from |bookmarks| into |result|. |
| 110 std::vector<const BookmarkNode*> result; | 147 std::vector<const BookmarkNode*> result; |
| 111 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 148 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 112 result.push_back( | 149 result.push_back( |
| 113 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 150 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| 114 if (result.size() >= static_cast<size_t>(max_count)) | 151 if (result.size() >= static_cast<size_t>(max_count)) |
| 115 break; | 152 break; |
| 116 } | 153 } |
| 117 return result; | 154 return result; |
| 118 } | 155 } |
| 119 | 156 |
| 157 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( |
| 158 BookmarkModel* bookmark_model) { |
| 159 // Get all the bookmark URLs. |
| 160 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 161 bookmark_model->GetBookmarks(&bookmarks); |
| 162 |
| 163 // Remove the bookmark URLs which have at least one non-dismissed bookmark. |
| 164 bookmarks.erase( |
| 165 std::remove_if( |
| 166 bookmarks.begin(), bookmarks.end(), |
| 167 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { |
| 168 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 169 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
| 170 |
| 171 for (const BookmarkNode* node : bookmarks_for_url) { |
| 172 if (!IsDismissedFromNTPForBookmark(node)) |
| 173 return true; |
| 174 } |
| 175 return false; |
| 176 }), |
| 177 bookmarks.end()); |
| 178 |
| 179 // Insert into |result|. |
| 180 std::vector<const BookmarkNode*> result; |
| 181 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 182 result.push_back( |
| 183 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| 184 } |
| 185 return result; |
| 186 } |
| 187 |
| 120 } // namespace ntp_snippets | 188 } // namespace ntp_snippets |
| OLD | NEW |