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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// 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/sessions/foreign_sessions_suggestions_provider.h"
#include <algorithm>
#include <map>
#include <tuple>
#include <utility>
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/category_info.h"
#include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/ntp_snippets/pref_util.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/sessions/core/session_types.h"
#include "components/sync_sessions/synced_session.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"
#include "url/gurl.h"
using base::Time;
using base::TimeDelta;
using sessions::SerializedNavigationEntry;
using sessions::SessionTab;
using sessions::SessionWindow;
using sync_sessions::SyncedSession;
using DismissedFilter = base::Callback<bool(const std::string& id)>;
namespace ntp_snippets {
namespace {
const int kMaxForeignTabsTotal = 10;
const int kMaxForeignTabsPerDevice = 3;
const int kMaxForeignTabAgeInMinutes = 180;
const char* kMaxForeignTabsTotalParamName = "max_foreign_tabs_total";
const char* kMaxForeignTabsPerDeviceParamName = "max_foreign_tabs_per_device";
const char* kMaxForeignTabAgeInMinutesParamName =
"max_foreign_tabs_age_in_minutes";
int GetMaxForeignTabsTotal() {
return variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kForeignSessionsSuggestionsFeature,
kMaxForeignTabsTotalParamName, kMaxForeignTabsTotal);
}
int GetMaxForeignTabsPerDevice() {
return variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kForeignSessionsSuggestionsFeature,
kMaxForeignTabsPerDeviceParamName, kMaxForeignTabsPerDevice);
}
TimeDelta GetMaxForeignTabAge() {
return TimeDelta::FromMinutes(variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kForeignSessionsSuggestionsFeature,
kMaxForeignTabAgeInMinutesParamName, kMaxForeignTabAgeInMinutes));
}
// This filter does two things. Most importantly it lets through only ids that
// have not been dismissed. The other responsibility this class has is it tracks
// all of the ids that fly past it, and it will save the intersection of
// initially dismissed ids, and seen ids. This will aggressively prune any
// dismissal that is not currently blocking a recent tab.
class PrefsPruningDismissedItemFilter {
public:
explicit PrefsPruningDismissedItemFilter(PrefService* pref_service)
: pref_service_(pref_service),
initial_dismissed_ids_(prefs::ReadDismissedIDsFromPrefs(
*pref_service_,
prefs::kDismissedForeignSessionsSuggestions)) {}
~PrefsPruningDismissedItemFilter() {
prefs::StoreDismissedIDsToPrefs(pref_service_,
prefs::kDismissedForeignSessionsSuggestions,
active_dismissed_ids_);
}
// Returns a Callback that can be easily used to filter out ids. Should not be
// stored anywhere, the filter should always outlive the returned callback.
DismissedFilter ToCallback() {
return base::Bind(&PrefsPruningDismissedItemFilter::ShouldInclude,
base::Unretained(this));
}
private:
bool ShouldInclude(const std::string& id) {
if (initial_dismissed_ids_.find(id) == initial_dismissed_ids_.end()) {
return true;
}
active_dismissed_ids_.insert(id);
return false;
}
PrefService* pref_service_;
// Ids that we know should be filterd out.
std::set<std::string> initial_dismissed_ids_;
// Ids that we have seen and were filtered out. This will be what is saved to
// preferences upon our destructor.
std::set<std::string> active_dismissed_ids_;
DISALLOW_COPY_AND_ASSIGN(PrefsPruningDismissedItemFilter);
};
// This filter only lets through ids that should normally be filtered out. As
// such, this filter should only be used when purposely trying to view dismissed
// content.
class InverseDismissedItemFilter {
public:
explicit InverseDismissedItemFilter(PrefService* pref_service)
: dismissed_ids_(prefs::ReadDismissedIDsFromPrefs(
*pref_service,
prefs::kDismissedForeignSessionsSuggestions)) {}
// Returns a Callback that can be easily used to filter out ids. Should not be
// stored anywhere, the filter should always outlive the returned callback.
DismissedFilter ToCallback() {
return base::Bind(&InverseDismissedItemFilter::ShouldInclude,
base::Unretained(this));
}
private:
bool ShouldInclude(const std::string& id) {
return dismissed_ids_.find(id) != dismissed_ids_.end();
}
std::set<std::string> dismissed_ids_;
DISALLOW_COPY_AND_ASSIGN(InverseDismissedItemFilter);
};
} // namespace
// Collection of pointers to various sessions objects that contain a superset of
// the information needed to create a single suggestion.
struct ForeignSessionsSuggestionsProvider::SessionData {
const sync_sessions::SyncedSession* session;
const sessions::SessionTab* tab;
const sessions::SerializedNavigationEntry* navigation;
bool operator<(const SessionData& other) const {
// Note that SerializedNavigationEntry::timestamp() is never set to a
// value, so always use SessionTab::timestamp() instead.
// TODO(skym): It might be better if we sorted by recency of session, and
// only then by recency of the tab. Right now this causes a single
// device's tabs to be interleaved with another devices' tabs.
return tab->timestamp > other.tab->timestamp;
}
};
ForeignSessionsSuggestionsProvider::ForeignSessionsSuggestionsProvider(
ContentSuggestionsProvider::Observer* observer,
std::unique_ptr<ForeignSessionsProvider> foreign_sessions_provider,
PrefService* pref_service)
: ContentSuggestionsProvider(observer),
category_status_(CategoryStatus::INITIALIZING),
provided_category_(
Category::FromKnownCategory(KnownCategories::FOREIGN_TABS)),
foreign_sessions_provider_(std::move(foreign_sessions_provider)),
pref_service_(pref_service) {
foreign_sessions_provider_->SubscribeForForeignTabChange(
base::Bind(&ForeignSessionsSuggestionsProvider::OnForeignTabChange,
base::Unretained(this)));
// If sync is already initialzed, try suggesting now, though this is unlikely.
OnForeignTabChange();
}
ForeignSessionsSuggestionsProvider::~ForeignSessionsSuggestionsProvider() =
default;
// static
void ForeignSessionsSuggestionsProvider::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterListPref(prefs::kDismissedForeignSessionsSuggestions);
}
CategoryStatus ForeignSessionsSuggestionsProvider::GetCategoryStatus(
Category category) {
DCHECK_EQ(category, provided_category_);
return category_status_;
}
CategoryInfo ForeignSessionsSuggestionsProvider::GetCategoryInfo(
Category category) {
DCHECK_EQ(category, provided_category_);
return CategoryInfo(l10n_util::GetStringUTF16(
IDS_NTP_FOREIGN_SESSIONS_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_FOREIGN_SESSIONS_SUGGESTIONS_SECTION_EMPTY));
}
void ForeignSessionsSuggestionsProvider::DismissSuggestion(
const ContentSuggestion::ID& suggestion_id) {
// Assume this suggestion is still valid, and blindly add it to dismissals.
// Pruning will happen the next time we are asked to suggest.
std::set<std::string> dismissed_ids = prefs::ReadDismissedIDsFromPrefs(
*pref_service_, prefs::kDismissedForeignSessionsSuggestions);
dismissed_ids.insert(suggestion_id.id_within_category());
prefs::StoreDismissedIDsToPrefs(pref_service_,
prefs::kDismissedForeignSessionsSuggestions,
dismissed_ids);
}
void ForeignSessionsSuggestionsProvider::FetchSuggestionImage(
const ContentSuggestion::ID& suggestion_id,
const ImageFetchedCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, gfx::Image()));
}
void ForeignSessionsSuggestionsProvider::Fetch(
const Category& category,
const std::set<std::string>& known_suggestion_ids,
const FetchDoneCallback& callback) {
LOG(DFATAL)
<< "ForeignSessionsSuggestionsProvider has no |Fetch| functionality!";
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(callback, Status(StatusCode::PERMANENT_ERROR,
"ForeignSessionsSuggestionsProvider "
"has no |Fetch| functionality!"),
base::Passed(std::vector<ContentSuggestion>())));
}
void ForeignSessionsSuggestionsProvider::ClearHistory(
Time begin,
Time end,
const base::Callback<bool(const GURL& url)>& filter) {
std::set<std::string> dismissed_ids = prefs::ReadDismissedIDsFromPrefs(
*pref_service_, prefs::kDismissedForeignSessionsSuggestions);
for (auto iter = dismissed_ids.begin(); iter != dismissed_ids.end();) {
if (filter.Run(GURL(*iter))) {
iter = dismissed_ids.erase(iter);
} else {
++iter;
}
}
prefs::StoreDismissedIDsToPrefs(pref_service_,
prefs::kDismissedForeignSessionsSuggestions,
dismissed_ids);
}
void ForeignSessionsSuggestionsProvider::ClearCachedSuggestions(
Category category) {
DCHECK_EQ(category, provided_category_);
// Ignored.
}
void ForeignSessionsSuggestionsProvider::GetDismissedSuggestionsForDebugging(
Category category,
const DismissedSuggestionsCallback& callback) {
DCHECK_EQ(category, provided_category_);
InverseDismissedItemFilter filter(pref_service_);
// Use GetSuggestionCandidates instead of BuildSuggestions(), to avoid the
// size and duplicate filtering. We want to return a complete list of
// everything that could potentially be blocked by the not dismissed filter.
std::vector<ContentSuggestion> suggestions;
for (auto data : GetSuggestionCandidates(filter.ToCallback())) {
suggestions.push_back(BuildSuggestion(data));
}
callback.Run(std::move(suggestions));
}
void ForeignSessionsSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
Category category) {
DCHECK_EQ(category, provided_category_);
pref_service_->ClearPref(prefs::kDismissedForeignSessionsSuggestions);
}
void ForeignSessionsSuggestionsProvider::OnForeignTabChange() {
if (!foreign_sessions_provider_->HasSessionsData()) {
if (category_status_ == CategoryStatus::AVAILABLE) {
// This is to handle the case where the user disabled sync [sessions] or
// logs out after we've already provided actual suggestions.
category_status_ = CategoryStatus::NOT_PROVIDED;
observer()->OnCategoryStatusChanged(this, provided_category_,
category_status_);
}
return;
}
if (category_status_ != CategoryStatus::AVAILABLE) {
// The further below logic will overwrite any error state. This is
// currently okay because no where in the current implementation does the
// status get set to an error state. Should this change, reconsider the
// overwriting logic.
DCHECK(category_status_ == CategoryStatus::INITIALIZING ||
category_status_ == CategoryStatus::NOT_PROVIDED);
// It is difficult to tell if sync simply has not initialized yet or there
// will never be data because the user is signed out or has disabled the
// sessions data type. Because this provider is hidden when there are no
// results, always just update to AVAILABLE once we might have results.
category_status_ = CategoryStatus::AVAILABLE;
observer()->OnCategoryStatusChanged(this, provided_category_,
category_status_);
}
// observer()->OnNewSuggestions must be called even when we have no
// suggestions to remove previous suggestions that are now filtered out.
observer()->OnNewSuggestions(this, provided_category_, BuildSuggestions());
}
std::vector<ContentSuggestion>
ForeignSessionsSuggestionsProvider::BuildSuggestions() {
const int max_foreign_tabs_total = GetMaxForeignTabsTotal();
const int max_foreign_tabs_per_device = GetMaxForeignTabsPerDevice();
PrefsPruningDismissedItemFilter filter(pref_service_);
std::vector<SessionData> suggestion_candidates =
GetSuggestionCandidates(filter.ToCallback());
// This sorts by recency so that we keep the most recent entries and they
// appear as suggestions in reverse chronological order.
std::sort(suggestion_candidates.begin(), suggestion_candidates.end());
std::vector<ContentSuggestion> suggestions;
std::set<std::string> included_urls;
std::map<std::string, int> suggestions_per_session;
for (const SessionData& candidate : suggestion_candidates) {
const std::string& session_tag = candidate.session->session_tag;
auto duplicates_iter =
included_urls.find(candidate.navigation->virtual_url().spec());
auto count_iter = suggestions_per_session.find(session_tag);
int count =
count_iter == suggestions_per_session.end() ? 0 : count_iter->second;
// Pick up to max (total and per device) tabs, and ensure no duplicates
// are selected. This filtering must be done in a second pass because
// this can cause newer tabs occluding less recent tabs, requiring more
// than |max_foreign_tabs_per_device| to be considered per device.
if (static_cast<int>(suggestions.size()) >= max_foreign_tabs_total ||
duplicates_iter != included_urls.end() ||
count >= max_foreign_tabs_per_device) {
continue;
}
included_urls.insert(candidate.navigation->virtual_url().spec());
suggestions_per_session[session_tag] = count + 1;
suggestions.push_back(BuildSuggestion(candidate));
}
return suggestions;
}
std::vector<ForeignSessionsSuggestionsProvider::SessionData>
ForeignSessionsSuggestionsProvider::GetSuggestionCandidates(
const DismissedFilter& suggestions_filter) {
const std::vector<const SyncedSession*>& foreign_sessions =
foreign_sessions_provider_->GetAllForeignSessions();
const TimeDelta max_foreign_tab_age = GetMaxForeignTabAge();
std::vector<SessionData> suggestion_candidates;
for (const SyncedSession* session : foreign_sessions) {
for (const std::pair<const SessionID::id_type,
std::unique_ptr<sessions::SessionWindow>>& key_value :
session->windows) {
for (const std::unique_ptr<SessionTab>& tab : key_value.second->tabs) {
if (tab->navigations.empty()) {
continue;
}
const SerializedNavigationEntry& navigation = tab->navigations.back();
const std::string id = navigation.virtual_url().spec();
// TODO(skym): Filter out internal pages. Tabs that contain only
// non-syncable content should never reach the local client. However,
// sync will let tabs through whose current navigation entry is
// internal, as long as a back or forward navigation entry is valid. We
// however, are only currently exposing the current entry, and so we
// should ideally exclude these.
TimeDelta tab_age = Time::Now() - tab->timestamp;
if (tab_age < max_foreign_tab_age && suggestions_filter.Run(id)) {
suggestion_candidates.push_back(
SessionData{session, tab.get(), &navigation});
}
}
}
}
return suggestion_candidates;
}
ContentSuggestion ForeignSessionsSuggestionsProvider::BuildSuggestion(
const SessionData& data) {
ContentSuggestion suggestion(provided_category_,
data.navigation->virtual_url().spec(),
data.navigation->virtual_url());
suggestion.set_title(data.navigation->title());
suggestion.set_publish_date(data.tab->timestamp);
suggestion.set_publisher_name(
base::UTF8ToUTF16(data.navigation->virtual_url().host()));
return suggestion;
}
} // namespace ntp_snippets
|