| 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 18 matching lines...) Expand all Loading... |
| 29 if (!base::StringToInt64(date_string, &date)) | 29 if (!base::StringToInt64(date_string, &date)) |
| 30 return base::Time::UnixEpoch(); | 30 return base::Time::UnixEpoch(); |
| 31 return base::Time::FromInternalValue(date); | 31 return base::Time::FromInternalValue(date); |
| 32 } | 32 } |
| 33 | 33 |
| 34 std::string FormatLastVisitDate(const base::Time& date) { | 34 std::string FormatLastVisitDate(const base::Time& date) { |
| 35 return base::Int64ToString(date.ToInternalValue()); | 35 return base::Int64ToString(date.ToInternalValue()); |
| 36 } | 36 } |
| 37 | 37 |
| 38 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, | 38 bool CompareBookmarksByLastVisitDate(const BookmarkNode* a, |
| 39 const BookmarkNode* b) { | 39 const BookmarkNode* b, |
| 40 return GetLastVisitDateForBookmark(a) > GetLastVisitDateForBookmark(b); | 40 bool use_creation_date) { |
| 41 return GetLastVisitDateForBookmark(a, use_creation_date) > |
| 42 GetLastVisitDateForBookmark(b, use_creation_date); |
| 41 } | 43 } |
| 42 | 44 |
| 43 } // namespace | 45 } // namespace |
| 44 | 46 |
| 45 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, | 47 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, |
| 46 const GURL& url) { | 48 const GURL& url) { |
| 47 std::vector<const BookmarkNode*> bookmarks_for_url; | 49 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 48 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | 50 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); |
| 49 if (bookmarks_for_url.empty()) | 51 if (bookmarks_for_url.empty()) |
| 50 return; | 52 return; |
| 51 | 53 |
| 52 // If there are bookmarks for |url|, set their last visit date to now. | 54 // If there are bookmarks for |url|, set their last visit date to now. |
| 53 std::string now = FormatLastVisitDate(base::Time::Now()); | 55 std::string now = FormatLastVisitDate(base::Time::Now()); |
| 54 for (const BookmarkNode* node : bookmarks_for_url) { | 56 for (const BookmarkNode* node : bookmarks_for_url) { |
| 55 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); | 57 bookmark_model->SetNodeMetaInfo(node, kBookmarkLastVisitDateKey, now); |
| 56 // If the bookmark has been dismissed from NTP before, a new visit overrides | 58 // If the bookmark has been dismissed from NTP before, a new visit overrides |
| 57 // such a dismission. | 59 // such a dismission. |
| 58 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 60 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
| 59 } | 61 } |
| 60 } | 62 } |
| 61 | 63 |
| 62 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node) { | 64 base::Time GetLastVisitDateForBookmark(const BookmarkNode* node, |
| 65 bool use_creation_date) { |
| 63 if (!node) | 66 if (!node) |
| 64 return base::Time::UnixEpoch(); | 67 return base::Time::UnixEpoch(); |
| 65 | 68 |
| 69 if (use_creation_date) |
| 70 return node->date_added(); |
| 71 |
| 66 std::string last_visit_date_string; | 72 std::string last_visit_date_string; |
| 67 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); | 73 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); |
| 68 return ParseLastVisitDate(last_visit_date_string); | 74 return ParseLastVisitDate(last_visit_date_string); |
| 69 } | 75 } |
| 70 | 76 |
| 71 base::Time GetLastVisitDateForBookmarkIfNotDismissed(const BookmarkNode* node) { | 77 base::Time GetLastVisitDateForBookmarkIfNotDismissed(const BookmarkNode* node, |
| 78 bool use_creation_date) { |
| 72 if (IsDismissedFromNTPForBookmark(node)) | 79 if (IsDismissedFromNTPForBookmark(node)) |
| 73 return base::Time::UnixEpoch(); | 80 return base::Time::UnixEpoch(); |
| 74 | 81 |
| 75 return GetLastVisitDateForBookmark(node); | 82 return GetLastVisitDateForBookmark(node, use_creation_date); |
| 76 } | 83 } |
| 77 | 84 |
| 78 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { | 85 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
| 79 std::vector<const BookmarkNode*> nodes; | 86 std::vector<const BookmarkNode*> nodes; |
| 80 bookmark_model->GetNodesByURL(url, &nodes); | 87 bookmark_model->GetNodesByURL(url, &nodes); |
| 81 for (const BookmarkNode* node : nodes) | 88 for (const BookmarkNode* node : nodes) |
| 82 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); | 89 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
| 83 } | 90 } |
| 84 | 91 |
| 85 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { | 92 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 100 | 107 |
| 101 // Remove dismissed flag from all bookmarks | 108 // Remove dismissed flag from all bookmarks |
| 102 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 109 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 103 std::vector<const BookmarkNode*> nodes; | 110 std::vector<const BookmarkNode*> nodes; |
| 104 bookmark_model->GetNodesByURL(bookmark.url, &nodes); | 111 bookmark_model->GetNodesByURL(bookmark.url, &nodes); |
| 105 for (const BookmarkNode* node : nodes) | 112 for (const BookmarkNode* node : nodes) |
| 106 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 113 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
| 107 } | 114 } |
| 108 } | 115 } |
| 109 | 116 |
| 117 bool IsLastVisitDataAvailable(BookmarkModel* bookmark_model) { |
| 118 // Get all the bookmark URLs. |
| 119 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 120 bookmark_model->GetBookmarks(&bookmarks); |
| 121 |
| 122 std::string unused; |
| 123 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 124 // Get all bookmarks for the given URL. |
| 125 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 126 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
| 127 for (const BookmarkNode* node : bookmarks_for_url) { |
| 128 if (node->GetMetaInfo(kBookmarkLastVisitDateKey, &unused)) |
| 129 return true; |
| 130 } |
| 131 } |
| 132 |
| 133 return false; |
| 134 } |
| 135 |
| 110 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | 136 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
| 111 BookmarkModel* bookmark_model, | 137 BookmarkModel* bookmark_model, |
| 138 bool use_creation_date, |
| 112 int max_count, | 139 int max_count, |
| 113 const base::Time& min_visit_time) { | 140 const base::Time& min_visit_time) { |
| 114 // Get all the bookmark URLs. | 141 // Get all the bookmark URLs. |
| 115 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 142 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 116 bookmark_model->GetBookmarks(&bookmarks); | 143 bookmark_model->GetBookmarks(&bookmarks); |
| 117 | 144 |
| 118 // Remove the URLs for that no bookmark has a recent visit (more recent than | 145 // Remove the URLs for that no bookmark has a recent visit (more recent than |
| 119 // |min_visit_time|). Recent visits to dismissed bookmarks are not considered. | 146 // |min_visit_time|). Recent visits to dismissed bookmarks are not considered. |
| 120 bookmarks.erase( | 147 bookmarks.erase( |
| 121 std::remove_if(bookmarks.begin(), bookmarks.end(), | 148 std::remove_if(bookmarks.begin(), bookmarks.end(), |
| 122 [&bookmark_model, &min_visit_time]( | 149 [&bookmark_model, &min_visit_time, use_creation_date]( |
| 123 const BookmarkModel::URLAndTitle& bookmark) { | 150 const BookmarkModel::URLAndTitle& bookmark) { |
| 124 // Get all bookmarks for the given URL. | 151 // Get all bookmarks for the given URL. |
| 125 std::vector<const BookmarkNode*> bookmarks_for_url; | 152 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 126 bookmark_model->GetNodesByURL(bookmark.url, | 153 bookmark_model->GetNodesByURL(bookmark.url, |
| 127 &bookmarks_for_url); | 154 &bookmarks_for_url); |
| 128 | 155 |
| 129 // Keep if at least one (non-dismissed) bookmark has been | 156 // Keep if at least one (non-dismissed) bookmark has been |
| 130 // recently visited. | 157 // recently visited. |
| 131 for (const BookmarkNode* node : bookmarks_for_url) { | 158 for (const BookmarkNode* node : bookmarks_for_url) { |
| 132 if (GetLastVisitDateForBookmarkIfNotDismissed(node) > | 159 if (GetLastVisitDateForBookmarkIfNotDismissed( |
| 133 min_visit_time) | 160 node, use_creation_date) > min_visit_time) |
| 134 return false; | 161 return false; |
| 135 } | 162 } |
| 136 // Otherwise erase this URL. | 163 // Otherwise erase this URL. |
| 137 return true; | 164 return true; |
| 138 }), | 165 }), |
| 139 bookmarks.end()); | 166 bookmarks.end()); |
| 140 | 167 |
| 141 // Sort the remaining bookmarks by date. | 168 // Sort the remaining bookmarks by date. |
| 142 std::sort(bookmarks.begin(), bookmarks.end(), | 169 std::sort(bookmarks.begin(), bookmarks.end(), |
| 143 [&bookmark_model](const BookmarkModel::URLAndTitle& a, | 170 [&bookmark_model, use_creation_date]( |
| 144 const BookmarkModel::URLAndTitle& b) { | 171 const BookmarkModel::URLAndTitle& a, |
| 172 const BookmarkModel::URLAndTitle& b) { |
| 145 return CompareBookmarksByLastVisitDate( | 173 return CompareBookmarksByLastVisitDate( |
| 146 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), | 174 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), |
| 147 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); | 175 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url), |
| 176 use_creation_date); |
| 148 }); | 177 }); |
| 149 | 178 |
| 150 // Insert the first |max_count| items from |bookmarks| into |result|. | 179 // Insert the first |max_count| items from |bookmarks| into |result|. |
| 151 std::vector<const BookmarkNode*> result; | 180 std::vector<const BookmarkNode*> result; |
| 152 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 181 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 153 result.push_back( | 182 result.push_back( |
| 154 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 183 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| 155 if (result.size() >= static_cast<size_t>(max_count)) | 184 if (result.size() >= static_cast<size_t>(max_count)) |
| 156 break; | 185 break; |
| 157 } | 186 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 183 // Insert into |result|. | 212 // Insert into |result|. |
| 184 std::vector<const BookmarkNode*> result; | 213 std::vector<const BookmarkNode*> result; |
| 185 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 214 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 186 result.push_back( | 215 result.push_back( |
| 187 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 216 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| 188 } | 217 } |
| 189 return result; | 218 return result; |
| 190 } | 219 } |
| 191 | 220 |
| 192 } // namespace ntp_snippets | 221 } // namespace ntp_snippets |
| OLD | NEW |