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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// 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_suggestions_provider.h"
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/location.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h"
#include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/variations/variations_associated_data.h"
#include "grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"
using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;
namespace ntp_snippets {
namespace {
const int kMaxBookmarks = 10;
const int kMaxBookmarkAgeInDays = 42;
const char* kMaxBookmarksParamName = "bookmarks_max_count";
const char* kMaxBookmarkAgeInDaysParamName = "bookmarks_max_age_in_days";
const char* kConsiderDesktopVisitsParamName =
"bookmarks_consider_desktop_visits";
// TODO(treib,jkrcal): Remove this after M57.
const char kDeprecatedBookmarksFirstM54StartPref[] =
"ntp_suggestions.bookmarks.first_M54_start";
// Any bookmark created or visited after this time will be considered recent.
// Note that bookmarks can be shown that do not meet this threshold.
base::Time GetThresholdTime() {
return base::Time::Now() -
base::TimeDelta::FromDays(variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kBookmarkSuggestionsFeature,
kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays));
}
// The maximum number of suggestions ever provided.
int GetMaxCount() {
return variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName,
kMaxBookmarks);
}
bool AreDesktopVisitsConsidered() {
return variations::GetVariationParamByFeatureAsBool(
ntp_snippets::kBookmarkSuggestionsFeature,
kConsiderDesktopVisitsParamName, false);
}
} // namespace
BookmarkSuggestionsProvider::BookmarkSuggestionsProvider(
ContentSuggestionsProvider::Observer* observer,
bookmarks::BookmarkModel* bookmark_model,
PrefService* pref_service)
: ContentSuggestionsProvider(observer),
category_status_(CategoryStatus::AVAILABLE_LOADING),
provided_category_(
Category::FromKnownCategory(KnownCategories::BOOKMARKS)),
bookmark_model_(bookmark_model),
fetch_requested_(false),
end_of_list_last_visit_date_(GetThresholdTime()),
consider_bookmark_visits_from_desktop_(AreDesktopVisitsConsidered()) {
observer->OnCategoryStatusChanged(this, provided_category_, category_status_);
pref_service->ClearPref(kDeprecatedBookmarksFirstM54StartPref);
bookmark_model_->AddObserver(this);
FetchBookmarks();
}
BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() {
bookmark_model_->RemoveObserver(this);
}
// static
void BookmarkSuggestionsProvider::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterInt64Pref(kDeprecatedBookmarksFirstM54StartPref, 0);
}
////////////////////////////////////////////////////////////////////////////////
// Private methods
CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus(
Category category) {
DCHECK_EQ(category, provided_category_);
return category_status_;
}
CategoryInfo BookmarkSuggestionsProvider::GetCategoryInfo(Category category) {
return CategoryInfo(
l10n_util::GetStringUTF16(IDS_NTP_BOOKMARK_SUGGESTIONS_SECTION_HEADER),
ContentSuggestionsCardLayout::MINIMAL_CARD,
/*has_more_action=*/false,
/*has_reload_action=*/false,
/*has_view_all_action=*/true,
/*show_if_empty=*/false,
l10n_util::GetStringUTF16(IDS_NTP_BOOKMARK_SUGGESTIONS_SECTION_EMPTY));
}
void BookmarkSuggestionsProvider::DismissSuggestion(
const ContentSuggestion::ID& suggestion_id) {
DCHECK(bookmark_model_->loaded());
GURL url(suggestion_id.id_within_category());
MarkBookmarksDismissed(bookmark_model_, url);
}
void BookmarkSuggestionsProvider::FetchSuggestionImage(
const ContentSuggestion::ID& suggestion_id,
const ImageFetchedCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, gfx::Image()));
}
void BookmarkSuggestionsProvider::Fetch(
const Category& category,
const std::set<std::string>& known_suggestion_ids,
const FetchDoneCallback& callback) {
LOG(DFATAL) << "BookmarkSuggestionsProvider has no |Fetch| functionality!";
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(
callback,
Status(StatusCode::PERMANENT_ERROR,
"BookmarkSuggestionsProvider has no |Fetch| functionality!"),
base::Passed(std::vector<ContentSuggestion>())));
}
void BookmarkSuggestionsProvider::ClearHistory(
base::Time begin,
base::Time end,
const base::Callback<bool(const GURL& url)>& filter) {
// To avoid race conditions with the history-removal of the last-visited
// timestamps we also trigger a deletion here. The problem is that we need to
// update the bookmarks data here and otherwise (depending on the order in
// which the code runs) could pick up to-be-deleted data again.
if (bookmark_model_->loaded()) {
RemoveLastVisitedDatesBetween(begin, end, filter, bookmark_model_);
}
ClearDismissedSuggestionsForDebugging(provided_category_);
FetchBookmarks();
}
void BookmarkSuggestionsProvider::ClearCachedSuggestions(Category category) {
DCHECK_EQ(category, provided_category_);
// Ignored.
}
void BookmarkSuggestionsProvider::GetDismissedSuggestionsForDebugging(
Category category,
const DismissedSuggestionsCallback& callback) {
DCHECK_EQ(category, provided_category_);
std::vector<const BookmarkNode*> bookmarks =
GetDismissedBookmarksForDebugging(bookmark_model_);
std::vector<ContentSuggestion> suggestions;
for (const BookmarkNode* bookmark : bookmarks) {
ConvertBookmark(*bookmark, &suggestions);
}
callback.Run(std::move(suggestions));
}
void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
Category category) {
DCHECK_EQ(category, provided_category_);
if (!bookmark_model_->loaded()) {
return;
}
MarkAllBookmarksUndismissed(bookmark_model_);
}
void BookmarkSuggestionsProvider::BookmarkModelLoaded(
bookmarks::BookmarkModel* model,
bool ids_reassigned) {
DCHECK_EQ(bookmark_model_, model);
if (fetch_requested_) {
fetch_requested_ = false;
FetchBookmarksInternal();
}
}
void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo(
BookmarkModel* model,
const BookmarkNode* node) {
// Store the last visit date of the node that is about to change.
if (!GetLastVisitDateForNTPBookmark(*node,
consider_bookmark_visits_from_desktop_,
&node_to_change_last_visit_date_)) {
node_to_change_last_visit_date_ = base::Time::UnixEpoch();
}
}
void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged(
BookmarkModel* model,
const BookmarkNode* node) {
base::Time time;
if (!GetLastVisitDateForNTPBookmark(
*node, consider_bookmark_visits_from_desktop_, &time)) {
// Error in loading the last visit date after the change. This happens when
// the bookmark just got dismissed. We must not update the suggestion in
// such a case.
return;
}
if (time == node_to_change_last_visit_date_ ||
time < end_of_list_last_visit_date_) {
// The last visit date has not changed or the change is irrelevant.
return;
}
// Otherwise, we should update the suggestions.
FetchBookmarks();
}
void BookmarkSuggestionsProvider::BookmarkNodeRemoved(
bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* parent,
int old_index,
const bookmarks::BookmarkNode* node,
const std::set<GURL>& no_longer_bookmarked) {
base::Time time;
if (GetLastVisitDateForNTPBookmark(
*node, consider_bookmark_visits_from_desktop_, &time) &&
time < end_of_list_last_visit_date_) {
// We know the node is too old to influence the list.
return;
}
// Some node from our list got deleted, we should update the suggestions.
FetchBookmarks();
}
void BookmarkSuggestionsProvider::BookmarkNodeAdded(
bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* parent,
int index) {
base::Time time;
if (!GetLastVisitDateForNTPBookmark(*parent->GetChild(index),
consider_bookmark_visits_from_desktop_,
&time) ||
time < end_of_list_last_visit_date_) {
// The new node has no last visited info or is too old to get into the list.
return;
}
// Some relevant node got created (e.g. by sync), we should update the list.
FetchBookmarks();
}
void BookmarkSuggestionsProvider::ConvertBookmark(
const BookmarkNode& bookmark,
std::vector<ContentSuggestion>* suggestions) {
base::Time publish_date;
if (!GetLastVisitDateForNTPBookmark(
bookmark, consider_bookmark_visits_from_desktop_, &publish_date)) {
return;
}
ContentSuggestion suggestion(provided_category_, bookmark.url().spec(),
bookmark.url());
suggestion.set_title(bookmark.GetTitle());
suggestion.set_snippet_text(base::string16());
suggestion.set_publish_date(publish_date);
suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark.url().host()));
suggestions->emplace_back(std::move(suggestion));
}
void BookmarkSuggestionsProvider::FetchBookmarksInternal() {
DCHECK(bookmark_model_->loaded());
NotifyStatusChanged(CategoryStatus::AVAILABLE);
base::Time threshold_time = GetThresholdTime();
std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks(
bookmark_model_, GetMaxCount(), threshold_time,
consider_bookmark_visits_from_desktop_);
std::vector<ContentSuggestion> suggestions;
for (const BookmarkNode* bookmark : bookmarks) {
ConvertBookmark(*bookmark, &suggestions);
}
if (suggestions.empty()) {
end_of_list_last_visit_date_ = threshold_time;
} else {
end_of_list_last_visit_date_ = suggestions.back().publish_date();
}
observer()->OnNewSuggestions(this, provided_category_,
std::move(suggestions));
}
void BookmarkSuggestionsProvider::FetchBookmarks() {
if (bookmark_model_->loaded()) {
FetchBookmarksInternal();
} else {
fetch_requested_ = true;
}
}
void BookmarkSuggestionsProvider::NotifyStatusChanged(
CategoryStatus new_status) {
if (category_status_ == new_status) {
return;
}
category_status_ = new_status;
observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
}
} // namespace ntp_snippets
|