Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(802)

Side by Side Diff: components/ntp_snippets/bookmarks/bookmark_last_visit_utils.cc

Issue 2222933002: Make bookmark suggestions dismissable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make bookmark suggestions dismissable Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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());
(...skipping 28 matching lines...) Expand all
63 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string); 64 node->GetMetaInfo(kBookmarkLastVisitDateKey, &last_visit_date_string);
64 65
65 if (last_visit_date_string.empty()) { 66 if (last_visit_date_string.empty()) {
66 // Use creation date if no last visit info present. 67 // Use creation date if no last visit info present.
67 return node->date_added(); 68 return node->date_added();
68 } 69 }
69 70
70 return ParseLastVisitDate(last_visit_date_string); 71 return ParseLastVisitDate(last_visit_date_string);
71 } 72 }
72 73
74 void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) {
75 std::vector<const BookmarkNode*> nodes;
76 bookmark_model->GetNodesByURL(url, &nodes);
77 for (const BookmarkNode* node : nodes) {
Marc Treib 2016/08/08 09:10:46 nit: braces around single-line bodies or not? Eith
Philipp Keck 2016/08/08 09:42:25 Done.
78 bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1");
79 }
80 }
81
82 bool GetDismissedFromNTPForBookmark(const BookmarkNode* node) {
83 if (!node)
84 return false;
85
86 std::string dismissed_from_ntp;
87 return node->GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp) &&
88 dismissed_from_ntp == "1";
Marc Treib 2016/08/08 09:10:46 If the meta info exists at all, it should never ha
Philipp Keck 2016/08/08 09:42:25 Done.
89 }
90
91 void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) {
92 // Get all the bookmark URLs.
93 std::vector<BookmarkModel::URLAndTitle> bookmarks;
94 bookmark_model->GetBookmarks(&bookmarks);
95
96 // Remove dismissed flag from all bookmarks
97 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
98 std::vector<const BookmarkNode*> nodes;
99 bookmark_model->GetNodesByURL(bookmark.url, &nodes);
100 for (const BookmarkNode* node : nodes) {
101 bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP);
102 }
103 }
104 }
105
73 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks( 106 std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
74 BookmarkModel* bookmark_model, 107 BookmarkModel* bookmark_model,
75 int max_count, 108 int max_count,
76 const base::Time& min_visit_time) { 109 const base::Time& min_visit_time) {
77 // Get all the bookmarks. 110 // Get all the bookmark URLs.
78 std::vector<BookmarkModel::URLAndTitle> bookmarks; 111 std::vector<BookmarkModel::URLAndTitle> bookmarks;
79 bookmark_model->GetBookmarks(&bookmarks); 112 bookmark_model->GetBookmarks(&bookmarks);
80 113
81 // Remove bookmarks that have not been visited after |min_visit_time|. 114 // Remove the bookmark URLs whose bookmarks are all dismissed or whose most
115 // recent visit is older than |min_visit_time|.
82 bookmarks.erase( 116 bookmarks.erase(
83 std::remove_if(bookmarks.begin(), bookmarks.end(), 117 std::remove_if(bookmarks.begin(), bookmarks.end(),
84 [&bookmark_model, &min_visit_time]( 118 [&bookmark_model, &min_visit_time](
85 const BookmarkModel::URLAndTitle& bookmark) { 119 const BookmarkModel::URLAndTitle& bookmark) {
86 // Get all bookmarks for the given URL. 120 // Get all bookmarks for the given URL.
87 std::vector<const BookmarkNode*> bookmarks_for_url; 121 std::vector<const BookmarkNode*> bookmarks_for_url;
88 bookmark_model->GetNodesByURL(bookmark.url, 122 bookmark_model->GetNodesByURL(bookmark.url,
89 &bookmarks_for_url); 123 &bookmarks_for_url);
90 124
91 // Return false if there is a more recent visit time. 125 // Check if there is any recent and/or non-dismissed one.
126 bool has_recent = false;
127 bool has_non_dismissed = false;
92 for (const auto* node : bookmarks_for_url) { 128 for (const auto* node : bookmarks_for_url) {
93 if (GetLastVisitDateForBookmark(node) > min_visit_time) 129 if (GetLastVisitDateForBookmark(node) > min_visit_time)
94 return false; 130 has_recent = true;
131 if (!GetDismissedFromNTPForBookmark(node))
132 has_non_dismissed = true;
95 } 133 }
96 return true; 134 return has_recent && has_non_dismissed;
Marc Treib 2016/08/08 09:10:46 This is the wrong way around - you want to remove
Philipp Keck 2016/08/08 09:42:25 Right. I changed it around, trying to get rid of a
97 }), 135 }),
98 bookmarks.end()); 136 bookmarks.end());
99 137
100 // Sort the remaining bookmarks by date. 138 // Sort the remaining bookmarks by date.
101 std::sort(bookmarks.begin(), bookmarks.end(), 139 std::sort(bookmarks.begin(), bookmarks.end(),
102 [&bookmark_model](const BookmarkModel::URLAndTitle& a, 140 [&bookmark_model](const BookmarkModel::URLAndTitle& a,
103 const BookmarkModel::URLAndTitle& b) { 141 const BookmarkModel::URLAndTitle& b) {
104 return CompareBookmarksByLastVisitDate( 142 return CompareBookmarksByLastVisitDate(
105 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url), 143 bookmark_model->GetMostRecentlyAddedUserNodeForURL(a.url),
106 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url)); 144 bookmark_model->GetMostRecentlyAddedUserNodeForURL(b.url));
107 }); 145 });
108 146
109 // Insert the first |max_count| items from |bookmarks| into |result|. 147 // Insert the first |max_count| items from |bookmarks| into |result|.
110 std::vector<const BookmarkNode*> result; 148 std::vector<const BookmarkNode*> result;
111 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { 149 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
112 result.push_back( 150 result.push_back(
113 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url)); 151 bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url));
114 if (result.size() >= static_cast<size_t>(max_count)) 152 if (result.size() >= static_cast<size_t>(max_count))
115 break; 153 break;
116 } 154 }
117 return result; 155 return result;
118 } 156 }
119 157
120 } // namespace ntp_snippets 158 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698