| 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 <numeric> | 8 #include <numeric> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; | 39 const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp"; |
| 40 | 40 |
| 41 std::string FormatLastVisitDate(const base::Time& date) { | 41 std::string FormatLastVisitDate(const base::Time& date) { |
| 42 return base::Int64ToString(date.ToInternalValue()); | 42 return base::Int64ToString(date.ToInternalValue()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool ExtractLastVisitDate(const BookmarkNode& node, | 45 bool ExtractLastVisitDate(const BookmarkNode& node, |
| 46 const std::string& meta_info_key, | 46 const std::string& meta_info_key, |
| 47 base::Time* out) { | 47 base::Time* out) { |
| 48 std::string last_visit_date_string; | 48 std::string last_visit_date_string; |
| 49 if (!node.GetMetaInfo(meta_info_key, &last_visit_date_string)) | 49 if (!node.GetMetaInfo(meta_info_key, &last_visit_date_string)) { |
| 50 return false; | 50 return false; |
| 51 } |
| 51 | 52 |
| 52 int64_t date = 0; | 53 int64_t date = 0; |
| 53 if (!base::StringToInt64(last_visit_date_string, &date) || date < 0) | 54 if (!base::StringToInt64(last_visit_date_string, &date) || date < 0) { |
| 54 return false; | 55 return false; |
| 56 } |
| 55 | 57 |
| 56 *out = base::Time::FromInternalValue(date); | 58 *out = base::Time::FromInternalValue(date); |
| 57 return true; | 59 return true; |
| 58 } | 60 } |
| 59 | 61 |
| 60 bool IsBlacklisted(const GURL& url) { | 62 bool IsBlacklisted(const GURL& url) { |
| 61 for (const char* blacklisted : kBookmarksURLBlacklist) { | 63 for (const char* blacklisted : kBookmarksURLBlacklist) { |
| 62 if (url.spec() == blacklisted) | 64 if (url.spec() == blacklisted) { |
| 63 return true; | 65 return true; |
| 66 } |
| 64 } | 67 } |
| 65 return false; | 68 return false; |
| 66 } | 69 } |
| 67 | 70 |
| 68 std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark( | 71 std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark( |
| 69 const std::vector<const BookmarkNode*>& bookmarks, | 72 const std::vector<const BookmarkNode*>& bookmarks, |
| 70 bool creation_date_fallback, | 73 bool creation_date_fallback, |
| 71 bool consider_visits_from_desktop) { | 74 bool consider_visits_from_desktop) { |
| 72 auto most_recent = bookmarks.end(); | 75 auto most_recent = bookmarks.end(); |
| 73 base::Time most_recent_last_visited = base::Time::UnixEpoch(); | 76 base::Time most_recent_last_visited = base::Time::UnixEpoch(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 85 | 88 |
| 86 return most_recent; | 89 return most_recent; |
| 87 } | 90 } |
| 88 | 91 |
| 89 } // namespace | 92 } // namespace |
| 90 | 93 |
| 91 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, | 94 void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model, |
| 92 const GURL& url, | 95 const GURL& url, |
| 93 bool is_mobile_platform) { | 96 bool is_mobile_platform) { |
| 94 // Skip URLs that are blacklisted. | 97 // Skip URLs that are blacklisted. |
| 95 if (IsBlacklisted(url)) | 98 if (IsBlacklisted(url)) { |
| 96 return; | 99 return; |
| 100 } |
| 97 | 101 |
| 98 // Skip URLs that are not bookmarked. | 102 // Skip URLs that are not bookmarked. |
| 99 std::vector<const BookmarkNode*> bookmarks_for_url; | 103 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 100 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); | 104 bookmark_model->GetNodesByURL(url, &bookmarks_for_url); |
| 101 if (bookmarks_for_url.empty()) | 105 if (bookmarks_for_url.empty()) { |
| 102 return; | 106 return; |
| 107 } |
| 103 | 108 |
| 104 // If there are bookmarks for |url|, set their last visit date to now. | 109 // If there are bookmarks for |url|, set their last visit date to now. |
| 105 std::string now = FormatLastVisitDate(base::Time::Now()); | 110 std::string now = FormatLastVisitDate(base::Time::Now()); |
| 106 for (const BookmarkNode* node : bookmarks_for_url) { | 111 for (const BookmarkNode* node : bookmarks_for_url) { |
| 107 bookmark_model->SetNodeMetaInfo( | 112 bookmark_model->SetNodeMetaInfo( |
| 108 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey | 113 node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey |
| 109 : kBookmarkLastVisitDateOnDesktopKey, | 114 : kBookmarkLastVisitDateOnDesktopKey, |
| 110 now); | 115 now); |
| 111 // If the bookmark has been dismissed from NTP before, a new visit overrides | 116 // If the bookmark has been dismissed from NTP before, a new visit overrides |
| 112 // such a dismissal. | 117 // such a dismissal. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 143 *out = node->date_added(); | 148 *out = node->date_added(); |
| 144 return true; | 149 return true; |
| 145 } | 150 } |
| 146 | 151 |
| 147 return got_mobile_date; | 152 return got_mobile_date; |
| 148 } | 153 } |
| 149 | 154 |
| 150 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { | 155 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) { |
| 151 std::vector<const BookmarkNode*> nodes; | 156 std::vector<const BookmarkNode*> nodes; |
| 152 bookmark_model->GetNodesByURL(url, &nodes); | 157 bookmark_model->GetNodesByURL(url, &nodes); |
| 153 for (const BookmarkNode* node : nodes) | 158 for (const BookmarkNode* node : nodes) { |
| 154 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); | 159 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1"); |
| 160 } |
| 155 } | 161 } |
| 156 | 162 |
| 157 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { | 163 bool IsDismissedFromNTPForBookmark(const BookmarkNode* node) { |
| 158 if (!node) | 164 if (!node) { |
| 159 return false; | 165 return false; |
| 166 } |
| 160 | 167 |
| 161 std::string dismissed_from_ntp; | 168 std::string dismissed_from_ntp; |
| 162 bool result = | 169 bool result = |
| 163 node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); | 170 node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp); |
| 164 DCHECK(!result || dismissed_from_ntp == "1"); | 171 DCHECK(!result || dismissed_from_ntp == "1"); |
| 165 return result; | 172 return result; |
| 166 } | 173 } |
| 167 | 174 |
| 168 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { | 175 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) { |
| 169 // Get all the bookmark URLs. | 176 // Get all the bookmark URLs. |
| 170 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 177 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 171 bookmark_model->GetBookmarks(&bookmarks); | 178 bookmark_model->GetBookmarks(&bookmarks); |
| 172 | 179 |
| 173 // Remove dismissed flag from all bookmarks | 180 // Remove dismissed flag from all bookmarks |
| 174 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 181 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 175 std::vector<const BookmarkNode*> nodes; | 182 std::vector<const BookmarkNode*> nodes; |
| 176 bookmark_model->GetNodesByURL(bookmark.url, &nodes); | 183 bookmark_model->GetNodesByURL(bookmark.url, &nodes); |
| 177 for (const BookmarkNode* node : nodes) | 184 for (const BookmarkNode* node : nodes) { |
| 178 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); | 185 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP); |
| 186 } |
| 179 } | 187 } |
| 180 } | 188 } |
| 181 | 189 |
| 182 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( | 190 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( |
| 183 BookmarkModel* bookmark_model, | 191 BookmarkModel* bookmark_model, |
| 184 int min_count, | 192 int min_count, |
| 185 int max_count, | 193 int max_count, |
| 186 const base::Time& min_visit_time, | 194 const base::Time& min_visit_time, |
| 187 bool creation_date_fallback, | 195 bool creation_date_fallback, |
| 188 bool consider_visits_from_desktop) { | 196 bool consider_visits_from_desktop) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 std::vector<const BookmarkNode*> result; | 272 std::vector<const BookmarkNode*> result; |
| 265 for (const RecentBookmark& bookmark : bookmarks) { | 273 for (const RecentBookmark& bookmark : bookmarks) { |
| 266 // TODO(jkrcal): The following break rule is in conflict with the comment | 274 // TODO(jkrcal): The following break rule is in conflict with the comment |
| 267 // and code above that we give at least |min_count| bookmarks (irrespective | 275 // and code above that we give at least |min_count| bookmarks (irrespective |
| 268 // of creation_date_fallback). Discuss with treib@ and clear up. | 276 // of creation_date_fallback). Discuss with treib@ and clear up. |
| 269 if (!creation_date_fallback && bookmark.last_visited < min_visit_time) { | 277 if (!creation_date_fallback && bookmark.last_visited < min_visit_time) { |
| 270 break; | 278 break; |
| 271 } | 279 } |
| 272 | 280 |
| 273 result.push_back(bookmark.node); | 281 result.push_back(bookmark.node); |
| 274 if (result.size() >= static_cast<size_t>(max_count)) | 282 if (result.size() >= static_cast<size_t>(max_count)) { |
| 275 break; | 283 break; |
| 284 } |
| 276 } | 285 } |
| 277 return result; | 286 return result; |
| 278 } | 287 } |
| 279 | 288 |
| 280 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( | 289 std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging( |
| 281 BookmarkModel* bookmark_model) { | 290 BookmarkModel* bookmark_model) { |
| 282 // Get all the bookmark URLs. | 291 // Get all the bookmark URLs. |
| 283 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 292 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
| 284 bookmark_model->GetBookmarks(&bookmarks); | 293 bookmark_model->GetBookmarks(&bookmarks); |
| 285 | 294 |
| 286 // Remove the bookmark URLs which have at least one non-dismissed bookmark. | 295 // Remove the bookmark URLs which have at least one non-dismissed bookmark. |
| 287 bookmarks.erase( | 296 bookmarks.erase( |
| 288 std::remove_if( | 297 std::remove_if( |
| 289 bookmarks.begin(), bookmarks.end(), | 298 bookmarks.begin(), bookmarks.end(), |
| 290 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { | 299 [&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) { |
| 291 std::vector<const BookmarkNode*> bookmarks_for_url; | 300 std::vector<const BookmarkNode*> bookmarks_for_url; |
| 292 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); | 301 bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url); |
| 293 DCHECK(!bookmarks_for_url.empty()); | 302 DCHECK(!bookmarks_for_url.empty()); |
| 294 | 303 |
| 295 for (const BookmarkNode* node : bookmarks_for_url) { | 304 for (const BookmarkNode* node : bookmarks_for_url) { |
| 296 if (!IsDismissedFromNTPForBookmark(node)) | 305 if (!IsDismissedFromNTPForBookmark(node)) { |
| 297 return true; | 306 return true; |
| 307 } |
| 298 } | 308 } |
| 299 return false; | 309 return false; |
| 300 }), | 310 }), |
| 301 bookmarks.end()); | 311 bookmarks.end()); |
| 302 | 312 |
| 303 // Insert into |result|. | 313 // Insert into |result|. |
| 304 std::vector<const BookmarkNode*> result; | 314 std::vector<const BookmarkNode*> result; |
| 305 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 315 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { |
| 306 result.push_back( | 316 result.push_back( |
| 307 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); | 317 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 322 for (const BookmarkNode* bookmark : bookmarks_for_url) { | 332 for (const BookmarkNode* bookmark : bookmarks_for_url) { |
| 323 bookmark_model->DeleteNodeMetaInfo(bookmark, | 333 bookmark_model->DeleteNodeMetaInfo(bookmark, |
| 324 kBookmarkLastVisitDateOnMobileKey); | 334 kBookmarkLastVisitDateOnMobileKey); |
| 325 bookmark_model->DeleteNodeMetaInfo(bookmark, | 335 bookmark_model->DeleteNodeMetaInfo(bookmark, |
| 326 kBookmarkLastVisitDateOnDesktopKey); | 336 kBookmarkLastVisitDateOnDesktopKey); |
| 327 } | 337 } |
| 328 } | 338 } |
| 329 } | 339 } |
| 330 | 340 |
| 331 } // namespace ntp_snippets | 341 } // namespace ntp_snippets |
| OLD | NEW |