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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
// 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/offline_pages/recent_tab_suggestions_provider.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/ntp_snippets/pref_util.h"
#include "components/offline_pages/core/client_policy_controller.h"
#include "components/offline_pages/core/offline_page_item.h"
#include "components/offline_pages/core/offline_page_model_query.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 offline_pages::ClientId;
using offline_pages::OfflinePageItem;
using offline_pages::OfflinePageModelQuery;
using offline_pages::OfflinePageModelQueryBuilder;
namespace ntp_snippets {
namespace {
const int kDefaultMaxSuggestionsCount = 5;
const char* kMaxSuggestionsCountParamName = "recent_tabs_max_count";
int GetMaxSuggestionsCount() {
return variations::GetVariationParamByFeatureAsInt(
kRecentOfflineTabSuggestionsFeature, kMaxSuggestionsCountParamName,
kDefaultMaxSuggestionsCount);
}
struct OrderOfflinePagesByMostRecentlyCreatedFirst {
bool operator()(const OfflinePageItem* left,
const OfflinePageItem* right) const {
return left->creation_time > right->creation_time;
}
};
struct OrderOfflinePagesByUrlAndThenMostRecentlyCreatedFirst {
bool operator()(const OfflinePageItem* left,
const OfflinePageItem* right) const {
if (left->url != right->url) {
return left->url < right->url;
}
return left->creation_time > right->creation_time;
}
};
std::unique_ptr<OfflinePageModelQuery> BuildRecentTabsQuery(
offline_pages::OfflinePageModel* model) {
OfflinePageModelQueryBuilder builder;
builder.RequireShownAsRecentlyVisitedSite(
OfflinePageModelQuery::Requirement::INCLUDE_MATCHING);
return builder.Build(model->GetPolicyController());
}
bool IsRecentTab(offline_pages::ClientPolicyController* policy_controller,
const OfflinePageItem& offline_page) {
return policy_controller->IsShownAsRecentlyVisitedSite(
offline_page.client_id.name_space);
}
} // namespace
RecentTabSuggestionsProvider::RecentTabSuggestionsProvider(
ContentSuggestionsProvider::Observer* observer,
offline_pages::OfflinePageModel* offline_page_model,
PrefService* pref_service)
: ContentSuggestionsProvider(observer),
category_status_(CategoryStatus::AVAILABLE_LOADING),
provided_category_(
Category::FromKnownCategory(KnownCategories::RECENT_TABS)),
offline_page_model_(offline_page_model),
pref_service_(pref_service),
weak_ptr_factory_(this) {
observer->OnCategoryStatusChanged(this, provided_category_, category_status_);
offline_page_model_->AddObserver(this);
FetchRecentTabs();
}
RecentTabSuggestionsProvider::~RecentTabSuggestionsProvider() {
offline_page_model_->RemoveObserver(this);
}
CategoryStatus RecentTabSuggestionsProvider::GetCategoryStatus(
Category category) {
if (category == provided_category_) {
return category_status_;
}
NOTREACHED() << "Unknown category " << category.id();
return CategoryStatus::NOT_PROVIDED;
}
CategoryInfo RecentTabSuggestionsProvider::GetCategoryInfo(Category category) {
DCHECK_EQ(provided_category_, category);
return CategoryInfo(
l10n_util::GetStringUTF16(IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_HEADER),
ContentSuggestionsCardLayout::MINIMAL_CARD,
/*has_more_action=*/false,
/*has_reload_action=*/false,
/*has_view_all_action=*/false,
/*show_if_empty=*/false,
l10n_util::GetStringUTF16(IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_EMPTY));
}
void RecentTabSuggestionsProvider::DismissSuggestion(
const ContentSuggestion::ID& suggestion_id) {
DCHECK_EQ(provided_category_, suggestion_id.category());
std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs();
dismissed_ids.insert(suggestion_id.id_within_category());
StoreDismissedIDsToPrefs(dismissed_ids);
}
void RecentTabSuggestionsProvider::FetchSuggestionImage(
const ContentSuggestion::ID& suggestion_id,
const ImageFetchedCallback& callback) {
// TODO(vitaliii): Fetch proper thumbnail from OfflinePageModel once it's
// available there.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, gfx::Image()));
}
void RecentTabSuggestionsProvider::Fetch(
const Category& category,
const std::set<std::string>& known_suggestion_ids,
const FetchDoneCallback& callback) {
LOG(DFATAL) << "RecentTabSuggestionsProvider has no |Fetch| functionality!";
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(
callback,
Status(StatusCode::PERMANENT_ERROR,
"RecentTabSuggestionsProvider has no |Fetch| functionality!"),
base::Passed(std::vector<ContentSuggestion>())));
}
void RecentTabSuggestionsProvider::ClearHistory(
base::Time begin,
base::Time end,
const base::Callback<bool(const GURL& url)>& filter) {
ClearDismissedSuggestionsForDebugging(provided_category_);
FetchRecentTabs();
}
void RecentTabSuggestionsProvider::ClearCachedSuggestions(Category category) {
// Ignored.
}
void RecentTabSuggestionsProvider::GetDismissedSuggestionsForDebugging(
Category category,
const DismissedSuggestionsCallback& callback) {
DCHECK_EQ(provided_category_, category);
// Offline pages which are not related to recent tabs are also queried here,
// so that they can be returned if they happen to be dismissed (e.g. due to a
// bug).
OfflinePageModelQueryBuilder query_builder;
offline_page_model_->GetPagesMatchingQuery(
query_builder.Build(offline_page_model_->GetPolicyController()),
base::Bind(&RecentTabSuggestionsProvider::
GetPagesMatchingQueryCallbackForGetDismissedSuggestions,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void RecentTabSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
Category category) {
DCHECK_EQ(provided_category_, category);
StoreDismissedIDsToPrefs(std::set<std::string>());
FetchRecentTabs();
}
// static
void RecentTabSuggestionsProvider::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterListPref(prefs::kDismissedRecentOfflineTabSuggestions);
}
////////////////////////////////////////////////////////////////////////////////
// Private methods
void RecentTabSuggestionsProvider::
GetPagesMatchingQueryCallbackForGetDismissedSuggestions(
const DismissedSuggestionsCallback& callback,
const std::vector<OfflinePageItem>& offline_pages) const {
std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs();
std::vector<ContentSuggestion> suggestions;
for (const OfflinePageItem& item : offline_pages) {
if (!dismissed_ids.count(base::IntToString(item.offline_id))) {
continue;
}
suggestions.push_back(ConvertOfflinePage(item));
}
callback.Run(std::move(suggestions));
}
void RecentTabSuggestionsProvider::OfflinePageModelLoaded(
offline_pages::OfflinePageModel* model) {}
void RecentTabSuggestionsProvider::OfflinePageAdded(
offline_pages::OfflinePageModel* model,
const offline_pages::OfflinePageItem& added_page) {
DCHECK_EQ(offline_page_model_, model);
if (IsRecentTab(model->GetPolicyController(), added_page)) {
FetchRecentTabs();
}
}
void RecentTabSuggestionsProvider::
GetPagesMatchingQueryCallbackForFetchRecentTabs(
const std::vector<OfflinePageItem>& offline_pages) {
NotifyStatusChanged(CategoryStatus::AVAILABLE);
std::set<std::string> old_dismissed_ids = ReadDismissedIDsFromPrefs();
std::set<std::string> new_dismissed_ids;
std::vector<const OfflinePageItem*> recent_tab_items;
for (const OfflinePageItem& item : offline_pages) {
std::string offline_page_id = base::IntToString(item.offline_id);
if (old_dismissed_ids.count(offline_page_id)) {
new_dismissed_ids.insert(offline_page_id);
} else {
recent_tab_items.push_back(&item);
}
}
observer()->OnNewSuggestions(
this, provided_category_,
GetMostRecentlyCreatedWithoutDuplicates(std::move(recent_tab_items)));
if (new_dismissed_ids.size() != old_dismissed_ids.size()) {
StoreDismissedIDsToPrefs(new_dismissed_ids);
}
}
void RecentTabSuggestionsProvider::OfflinePageDeleted(
int64_t offline_id,
const ClientId& client_id) {
// Because we never switch to NOT_PROVIDED dynamically, there can be no open
// UI containing an invalidated suggestion unless the status is something
// other than NOT_PROVIDED, so only notify invalidation in that case.
if (category_status_ != CategoryStatus::NOT_PROVIDED) {
InvalidateSuggestion(offline_id);
}
}
void RecentTabSuggestionsProvider::FetchRecentTabs() {
offline_page_model_->GetPagesMatchingQuery(
BuildRecentTabsQuery(offline_page_model_),
base::Bind(&RecentTabSuggestionsProvider::
GetPagesMatchingQueryCallbackForFetchRecentTabs,
weak_ptr_factory_.GetWeakPtr()));
}
void RecentTabSuggestionsProvider::NotifyStatusChanged(
CategoryStatus new_status) {
DCHECK_NE(CategoryStatus::NOT_PROVIDED, category_status_);
if (category_status_ == new_status) {
return;
}
category_status_ = new_status;
observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
}
ContentSuggestion RecentTabSuggestionsProvider::ConvertOfflinePage(
const OfflinePageItem& offline_page) const {
// TODO(vitaliii): Make sure the URL is opened in the existing tab.
ContentSuggestion suggestion(provided_category_,
base::IntToString(offline_page.offline_id),
offline_page.url);
if (offline_page.title.empty()) {
// TODO(vitaliii): Remove this fallback once the OfflinePageModel provides
// titles for all (relevant) OfflinePageItems.
suggestion.set_title(base::UTF8ToUTF16(offline_page.url.spec()));
} else {
suggestion.set_title(offline_page.title);
}
suggestion.set_publish_date(offline_page.creation_time);
suggestion.set_publisher_name(base::UTF8ToUTF16(offline_page.url.host()));
auto extra = base::MakeUnique<RecentTabSuggestionExtra>();
extra->tab_id = offline_page.client_id.id;
extra->offline_page_id = offline_page.offline_id;
suggestion.set_recent_tab_suggestion_extra(std::move(extra));
return suggestion;
}
std::vector<ContentSuggestion>
RecentTabSuggestionsProvider::GetMostRecentlyCreatedWithoutDuplicates(
std::vector<const OfflinePageItem*> offline_page_items) const {
// |std::unique| only removes duplicates that immediately follow each other.
// Thus, first, we have to sort by URL and creation time and only then remove
// duplicates and sort the remaining items by creation time.
std::sort(offline_page_items.begin(), offline_page_items.end(),
OrderOfflinePagesByUrlAndThenMostRecentlyCreatedFirst());
std::vector<const OfflinePageItem*>::iterator new_end = std::unique(
offline_page_items.begin(), offline_page_items.end(),
[](const OfflinePageItem* left, const OfflinePageItem* right) {
return left->url == right->url;
});
offline_page_items.erase(new_end, offline_page_items.end());
std::sort(offline_page_items.begin(), offline_page_items.end(),
OrderOfflinePagesByMostRecentlyCreatedFirst());
std::vector<ContentSuggestion> suggestions;
for (const OfflinePageItem* offline_page_item : offline_page_items) {
suggestions.push_back(ConvertOfflinePage(*offline_page_item));
if (static_cast<int>(suggestions.size()) == GetMaxSuggestionsCount()) {
break;
}
}
return suggestions;
}
void RecentTabSuggestionsProvider::InvalidateSuggestion(int64_t offline_id) {
std::string offline_page_id = base::IntToString(offline_id);
observer()->OnSuggestionInvalidated(
this, ContentSuggestion::ID(provided_category_, offline_page_id));
std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs();
auto it = dismissed_ids.find(offline_page_id);
if (it != dismissed_ids.end()) {
dismissed_ids.erase(it);
StoreDismissedIDsToPrefs(dismissed_ids);
}
}
std::set<std::string> RecentTabSuggestionsProvider::ReadDismissedIDsFromPrefs()
const {
return prefs::ReadDismissedIDsFromPrefs(
*pref_service_, prefs::kDismissedRecentOfflineTabSuggestions);
}
void RecentTabSuggestionsProvider::StoreDismissedIDsToPrefs(
const std::set<std::string>& dismissed_ids) {
prefs::StoreDismissedIDsToPrefs(pref_service_,
prefs::kDismissedRecentOfflineTabSuggestions,
dismissed_ids);
}
} // namespace ntp_snippets
|