1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h"
#include <algorithm>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "url/gurl.h"
using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;
namespace ntp_snippets {
namespace {
struct RecentBookmark {
const bookmarks::BookmarkNode* node;
base::Time last_visited;
};
const char* kBookmarksURLBlacklist[] = {"chrome://newtab/",
"chrome-native://newtab/",
"chrome://bookmarks/"};
const char kBookmarkLastVisitDateOnMobileKey[] = "last_visited";
const char kBookmarkLastVisitDateOnDesktopKey[] = "last_visited_desktop";
const char kBookmarkDismissedFromNTP[] = "dismissed_from_ntp";
std::string FormatLastVisitDate(const base::Time& date) {
return base::Int64ToString(date.ToInternalValue());
}
bool ExtractLastVisitDate(const BookmarkNode& node,
const std::string& meta_info_key,
base::Time* out) {
std::string last_visit_date_string;
if (!node.GetMetaInfo(meta_info_key, &last_visit_date_string)) {
return false;
}
int64_t date = 0;
if (!base::StringToInt64(last_visit_date_string, &date) || date < 0) {
return false;
}
*out = base::Time::FromInternalValue(date);
return true;
}
bool IsBlacklisted(const GURL& url) {
for (const char* blacklisted : kBookmarksURLBlacklist) {
if (url.spec() == blacklisted) {
return true;
}
}
return false;
}
std::vector<const BookmarkNode*>::const_iterator FindMostRecentBookmark(
const std::vector<const BookmarkNode*>& bookmarks,
bool consider_visits_from_desktop) {
auto most_recent = bookmarks.end();
base::Time most_recent_last_visited = base::Time::UnixEpoch();
for (auto iter = bookmarks.begin(); iter != bookmarks.end(); ++iter) {
base::Time last_visited;
if (GetLastVisitDateForNTPBookmark(**iter, consider_visits_from_desktop,
&last_visited) &&
most_recent_last_visited <= last_visited) {
most_recent = iter;
most_recent_last_visited = last_visited;
}
}
return most_recent;
}
} // namespace
void UpdateBookmarkOnURLVisitedInMainFrame(BookmarkModel* bookmark_model,
const GURL& url,
bool is_mobile_platform) {
// Skip URLs that are blacklisted.
if (IsBlacklisted(url)) {
return;
}
// Skip URLs that are not bookmarked.
std::vector<const BookmarkNode*> bookmarks_for_url;
bookmark_model->GetNodesByURL(url, &bookmarks_for_url);
if (bookmarks_for_url.empty()) {
return;
}
// If there are bookmarks for |url|, set their last visit date to now.
std::string now = FormatLastVisitDate(base::Time::Now());
for (const BookmarkNode* node : bookmarks_for_url) {
bookmark_model->SetNodeMetaInfo(
node, is_mobile_platform ? kBookmarkLastVisitDateOnMobileKey
: kBookmarkLastVisitDateOnDesktopKey,
now);
// If the bookmark has been dismissed from NTP before, a new visit overrides
// such a dismissal.
bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP);
}
}
bool GetLastVisitDateForNTPBookmark(const BookmarkNode& node,
bool consider_visits_from_desktop,
base::Time* out) {
if (IsDismissedFromNTPForBookmark(node)) {
return false;
}
bool got_mobile_date =
ExtractLastVisitDate(node, kBookmarkLastVisitDateOnMobileKey, out);
if (consider_visits_from_desktop) {
// Consider the later visit from these two platform groups.
base::Time last_visit_desktop;
if (ExtractLastVisitDate(node, kBookmarkLastVisitDateOnDesktopKey,
&last_visit_desktop)) {
if (!got_mobile_date) {
*out = last_visit_desktop;
} else {
*out = std::max(*out, last_visit_desktop);
}
return true;
}
}
return got_mobile_date;
}
void MarkBookmarksDismissed(BookmarkModel* bookmark_model, const GURL& url) {
std::vector<const BookmarkNode*> nodes;
bookmark_model->GetNodesByURL(url, &nodes);
for (const BookmarkNode* node : nodes) {
bookmark_model->SetNodeMetaInfo(node, kBookmarkDismissedFromNTP, "1");
}
}
bool IsDismissedFromNTPForBookmark(const BookmarkNode& node) {
std::string dismissed_from_ntp;
bool result =
node.GetMetaInfo(kBookmarkDismissedFromNTP, &dismissed_from_ntp);
DCHECK(!result || dismissed_from_ntp == "1");
return result;
}
void MarkAllBookmarksUndismissed(BookmarkModel* bookmark_model) {
// Get all the bookmark URLs.
std::vector<BookmarkModel::URLAndTitle> bookmarks;
bookmark_model->GetBookmarks(&bookmarks);
// Remove dismissed flag from all bookmarks
for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
std::vector<const BookmarkNode*> nodes;
bookmark_model->GetNodesByURL(bookmark.url, &nodes);
for (const BookmarkNode* node : nodes) {
bookmark_model->DeleteNodeMetaInfo(node, kBookmarkDismissedFromNTP);
}
}
}
std::vector<const BookmarkNode*> GetRecentlyVisitedBookmarks(
BookmarkModel* bookmark_model,
int max_count,
const base::Time& min_visit_time,
bool consider_visits_from_desktop) {
// Get all the bookmark URLs.
std::vector<BookmarkModel::URLAndTitle> bookmark_urls;
bookmark_model->GetBookmarks(&bookmark_urls);
std::vector<RecentBookmark> bookmarks;
// Find for each bookmark the most recently visited BookmarkNode and find out
// whether it is visited since |min_visit_time|.
for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) {
// Skip URLs that are blacklisted.
if (IsBlacklisted(url_and_title.url)) {
continue;
}
// Get all bookmarks for the given URL.
std::vector<const BookmarkNode*> bookmarks_for_url;
bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url);
DCHECK(!bookmarks_for_url.empty());
// Find the most recently visited node for the given URL.
auto most_recent =
FindMostRecentBookmark(bookmarks_for_url, consider_visits_from_desktop);
if (most_recent == bookmarks_for_url.end()) {
continue;
}
// Extract the last visit of the node to use later for sorting.
base::Time last_visit_time;
if (!GetLastVisitDateForNTPBookmark(
**most_recent, consider_visits_from_desktop, &last_visit_time) ||
last_visit_time <= min_visit_time) {
continue;
}
bookmarks.push_back({*most_recent, last_visit_time});
}
// Sort the entries by date, getting the |max_count| most recent bookmarks
// to the front.
size_t count_to_sort =
std::min(bookmarks.size(), static_cast<size_t>(max_count));
std::partial_sort(bookmarks.begin(), bookmarks.begin() + count_to_sort,
bookmarks.end(),
[](const RecentBookmark& a, const RecentBookmark& b) {
return a.last_visited > b.last_visited;
});
// Insert the first |max_count| items from |bookmarks| into |result|.
std::vector<const BookmarkNode*> result;
for (const RecentBookmark& bookmark : bookmarks) {
result.push_back(bookmark.node);
if (result.size() >= static_cast<size_t>(max_count)) {
break;
}
}
return result;
}
std::vector<const BookmarkNode*> GetDismissedBookmarksForDebugging(
BookmarkModel* bookmark_model) {
// Get all the bookmark URLs.
std::vector<BookmarkModel::URLAndTitle> bookmarks;
bookmark_model->GetBookmarks(&bookmarks);
// Remove the bookmark URLs which have at least one non-dismissed bookmark.
bookmarks.erase(
std::remove_if(
bookmarks.begin(), bookmarks.end(),
[&bookmark_model](const BookmarkModel::URLAndTitle& bookmark) {
std::vector<const BookmarkNode*> bookmarks_for_url;
bookmark_model->GetNodesByURL(bookmark.url, &bookmarks_for_url);
DCHECK(!bookmarks_for_url.empty());
for (const BookmarkNode* node : bookmarks_for_url) {
if (!IsDismissedFromNTPForBookmark(*node)) {
return true;
}
}
return false;
}),
bookmarks.end());
// Insert into |result|.
std::vector<const BookmarkNode*> result;
for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) {
result.push_back(
bookmark_model->GetMostRecentlyAddedUserNodeForURL(bookmark.url));
}
return result;
}
namespace {
void ClearLastVisitedMetadataIfBetween(bookmarks::BookmarkModel* model,
const BookmarkNode& node,
const base::Time& begin,
const base::Time& end,
const std::string& meta_key) {
base::Time last_visit_time;
if (ExtractLastVisitDate(node, meta_key, &last_visit_time) &&
begin <= last_visit_time && last_visit_time <= end) {
model->DeleteNodeMetaInfo(&node, meta_key);
}
}
} // namespace
void RemoveLastVisitedDatesBetween(const base::Time& begin,
const base::Time& end,
base::Callback<bool(const GURL& url)> filter,
bookmarks::BookmarkModel* bookmark_model) {
// Get all the bookmark URLs.
std::vector<BookmarkModel::URLAndTitle> bookmark_urls;
bookmark_model->GetBookmarks(&bookmark_urls);
for (const BookmarkModel::URLAndTitle& url_and_title : bookmark_urls) {
if (!filter.Run(url_and_title.url)) {
continue;
}
// Get all bookmarks for the given URL.
std::vector<const BookmarkNode*> bookmarks_for_url;
bookmark_model->GetNodesByURL(url_and_title.url, &bookmarks_for_url);
for (const BookmarkNode* bookmark : bookmarks_for_url) {
// The dismissal metadata is managed by the BookmarkSuggestionsProvider.
ClearLastVisitedMetadataIfBetween(bookmark_model, *bookmark, begin, end,
kBookmarkLastVisitDateOnMobileKey);
ClearLastVisitedMetadataIfBetween(bookmark_model, *bookmark, begin, end,
kBookmarkLastVisitDateOnDesktopKey);
}
}
}
} // namespace ntp_snippets
|