blob: 4f9850d17d10e2cee1efb84979bc8716cfc96a92 [file] [log] [blame]
pke6dbb90af2016-07-08 14:00:461// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/ntp_snippets/content_suggestions_service.h"
6
7#include <algorithm>
8#include <iterator>
vitaliii45941152016-09-05 08:58:139#include <set>
jkrcale13510e2016-09-08 17:56:2010#include <utility>
pke6dbb90af2016-07-08 14:00:4611
12#include "base/bind.h"
pke1da90602016-08-05 14:20:2713#include "base/location.h"
jkrcal27b02c12017-03-21 11:18:2614#include "base/memory/ptr_util.h"
jkrcal08d79b52017-04-13 14:02:1115#include "base/metrics/histogram_macros.h"
vitaliii69689f312017-10-25 17:36:0616#include "base/stl_util.h"
pke6dbb90af2016-07-08 14:00:4617#include "base/strings/string_number_conversions.h"
vitaliii69689f312017-10-25 17:36:0618#include "base/strings/stringprintf.h"
pke1da90602016-08-05 14:20:2719#include "base/threading/thread_task_runner_handle.h"
jkrcal27b02c12017-03-21 11:18:2620#include "base/time/default_clock.h"
dgn52914722016-10-18 10:28:4221#include "base/values.h"
jkrcal7b7e71f12017-04-06 13:12:4022#include "components/favicon/core/large_icon_service.h"
23#include "components/favicon_base/fallback_icon_style.h"
24#include "components/favicon_base/favicon_types.h"
dgnf6500c12017-05-09 17:05:3125#include "components/ntp_snippets/content_suggestions_metrics.h"
dgn52914722016-10-18 10:28:4226#include "components/ntp_snippets/pref_names.h"
jkrcal8083bc52017-04-24 16:34:0227#include "components/ntp_snippets/remote/remote_suggestions_provider.h"
dgn52914722016-10-18 10:28:4228#include "components/prefs/pref_registry_simple.h"
29#include "components/prefs/pref_service.h"
Ramin Halavati4bf78aa2017-06-01 04:53:4530#include "net/traffic_annotation/network_traffic_annotation.h"
pke6dbb90af2016-07-08 14:00:4631#include "ui/gfx/image/image.h"
32
33namespace ntp_snippets {
34
jkrcal08d79b52017-04-13 14:02:1135namespace {
36
37// Enumeration listing all possible outcomes for fetch attempts of favicons for
38// content suggestions. Used for UMA histograms, so do not change existing
39// values. Insert new values at the end, and update the histogram definition.
40// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets
41enum class FaviconFetchResult {
42 SUCCESS_CACHED = 0,
43 SUCCESS_FETCHED = 1,
44 FAILURE = 2,
45 COUNT = 3
46};
47
48void RecordFaviconFetchResult(FaviconFetchResult result) {
49 UMA_HISTOGRAM_ENUMERATION(
50 "NewTabPage.ContentSuggestions.ArticleFaviconFetchResult", result,
51 FaviconFetchResult::COUNT);
52}
53
54} // namespace
55
vitaliii45941152016-09-05 08:58:1356ContentSuggestionsService::ContentSuggestionsService(
57 State state,
Colin Blundell076714ff2018-02-12 17:26:1458 identity::IdentityManager* identity_manager,
jkrcale13510e2016-09-08 17:56:2059 history::HistoryService* history_service,
jkrcal7b7e71f12017-04-06 13:12:4060 favicon::LargeIconService* large_icon_service,
vitaliii7456f5a2016-12-19 11:13:2561 PrefService* pref_service,
jkrcalf9966462017-03-29 16:25:2162 std::unique_ptr<CategoryRanker> category_ranker,
63 std::unique_ptr<UserClassifier> user_classifier,
Vitalii Iarkof52ff802017-09-14 12:34:2464 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler,
65 std::unique_ptr<Logger> debug_logger)
jkrcale13510e2016-09-08 17:56:2066 : state_(state),
Colin Blundell076714ff2018-02-12 17:26:1467 identity_manager_observer_(this),
jkrcale13510e2016-09-08 17:56:2068 history_service_observer_(this),
jkrcal093410c2016-12-21 16:13:5569 remote_suggestions_provider_(nullptr),
jkrcal7b7e71f12017-04-06 13:12:4070 large_icon_service_(large_icon_service),
dgn52914722016-10-18 10:28:4271 pref_service_(pref_service),
jkrcalf9966462017-03-29 16:25:2172 remote_suggestions_scheduler_(std::move(remote_suggestions_scheduler)),
73 user_classifier_(std::move(user_classifier)),
Vitalii Iarkof52ff802017-09-14 12:34:2474 category_ranker_(std::move(category_ranker)),
75 debug_logger_(std::move(debug_logger)) {
vitaliii45941152016-09-05 08:58:1376 // Can be null in tests.
Colin Blundell076714ff2018-02-12 17:26:1477 if (identity_manager) {
78 identity_manager_observer_.Add(identity_manager);
dgnf5708892016-11-22 10:36:2279 }
80
vitaliii4408d6c2016-11-21 14:16:2481 if (history_service) {
vitaliii45941152016-09-05 08:58:1382 history_service_observer_.Add(history_service);
vitaliii4408d6c2016-11-21 14:16:2483 }
dgn52914722016-10-18 10:28:4284
Vitalii Iarkof52ff802017-09-14 12:34:2485 debug_logger_->Log(FROM_HERE, /*message=*/std::string());
86
dgn52914722016-10-18 10:28:4287 RestoreDismissedCategoriesFromPrefs();
vitaliii45941152016-09-05 08:58:1388}
pke6dbb90af2016-07-08 14:00:4689
treib62e819e2016-09-27 11:47:3490ContentSuggestionsService::~ContentSuggestionsService() = default;
pke6dbb90af2016-07-08 14:00:4691
92void ContentSuggestionsService::Shutdown() {
jkrcal093410c2016-12-21 16:13:5593 remote_suggestions_provider_ = nullptr;
94 remote_suggestions_scheduler_ = nullptr;
pke5728f082016-08-03 17:27:3595 suggestions_by_category_.clear();
96 providers_by_category_.clear();
97 categories_.clear();
98 providers_.clear();
pke6dbb90af2016-07-08 14:00:4699 state_ = State::DISABLED;
vitaliii4408d6c2016-11-21 14:16:24100 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13101 observer.ContentSuggestionsServiceShutdown();
vitaliii4408d6c2016-11-21 14:16:24102 }
pke6dbb90af2016-07-08 14:00:46103}
104
dgn52914722016-10-18 10:28:42105// static
106void ContentSuggestionsService::RegisterProfilePrefs(
107 PrefRegistrySimple* registry) {
108 registry->RegisterListPref(prefs::kDismissedCategories);
109}
110
vitaliii8b5ab282016-12-20 11:06:22111std::vector<Category> ContentSuggestionsService::GetCategories() const {
112 std::vector<Category> sorted_categories = categories_;
113 std::sort(sorted_categories.begin(), sorted_categories.end(),
114 [this](const Category& left, const Category& right) {
115 return category_ranker_->Compare(left, right);
116 });
117 return sorted_categories;
118}
119
pke9c5095ac2016-08-01 13:53:12120CategoryStatus ContentSuggestionsService::GetCategoryStatus(
121 Category category) const {
pke6dbb90af2016-07-08 14:00:46122 if (state_ == State::DISABLED) {
pke9c5095ac2016-08-01 13:53:12123 return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED;
pke6dbb90af2016-07-08 14:00:46124 }
125
pke4d3a4d62016-08-02 09:06:21126 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24127 if (iterator == providers_by_category_.end()) {
pke9c5095ac2016-08-01 13:53:12128 return CategoryStatus::NOT_PROVIDED;
vitaliii4408d6c2016-11-21 14:16:24129 }
pke6dbb90af2016-07-08 14:00:46130
131 return iterator->second->GetCategoryStatus(category);
132}
133
pkebd2f650a2016-08-09 14:53:45134base::Optional<CategoryInfo> ContentSuggestionsService::GetCategoryInfo(
135 Category category) const {
136 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24137 if (iterator == providers_by_category_.end()) {
pkebd2f650a2016-08-09 14:53:45138 return base::Optional<CategoryInfo>();
vitaliii4408d6c2016-11-21 14:16:24139 }
pkebd2f650a2016-08-09 14:53:45140 return iterator->second->GetCategoryInfo(category);
141}
142
pke6dbb90af2016-07-08 14:00:46143const std::vector<ContentSuggestion>&
pke9c5095ac2016-08-01 13:53:12144ContentSuggestionsService::GetSuggestionsForCategory(Category category) const {
pke6dbb90af2016-07-08 14:00:46145 auto iterator = suggestions_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24146 if (iterator == suggestions_by_category_.end()) {
pke6dbb90af2016-07-08 14:00:46147 return no_suggestions_;
vitaliii4408d6c2016-11-21 14:16:24148 }
pke6dbb90af2016-07-08 14:00:46149 return iterator->second;
150}
151
152void ContentSuggestionsService::FetchSuggestionImage(
treib4bbc54922016-09-28 17:26:44153 const ContentSuggestion::ID& suggestion_id,
gaschler42544c12017-08-03 15:43:26154 ImageFetchedCallback callback) {
treib4bbc54922016-09-28 17:26:44155 if (!providers_by_category_.count(suggestion_id.category())) {
pke6dbb90af2016-07-08 14:00:46156 LOG(WARNING) << "Requested image for suggestion " << suggestion_id
treib4bbc54922016-09-28 17:26:44157 << " for unavailable category " << suggestion_id.category();
pke1da90602016-08-05 14:20:27158 base::ThreadTaskRunnerHandle::Get()->PostTask(
gaschler42544c12017-08-03 15:43:26159 FROM_HERE, base::BindOnce(std::move(callback), gfx::Image()));
pke6dbb90af2016-07-08 14:00:46160 return;
161 }
treib4bbc54922016-09-28 17:26:44162 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage(
gaschler42544c12017-08-03 15:43:26163 suggestion_id, std::move(callback));
pke6dbb90af2016-07-08 14:00:46164}
165
Dan Harrington6eb74dc82018-03-20 17:39:18166void ContentSuggestionsService::FetchSuggestionImageData(
167 const ContentSuggestion::ID& suggestion_id,
168 ImageDataFetchedCallback callback) {
169 if (!providers_by_category_.count(suggestion_id.category())) {
170 LOG(WARNING) << "Requested image for suggestion " << suggestion_id
171 << " for unavailable category " << suggestion_id.category();
172 base::ThreadTaskRunnerHandle::Get()->PostTask(
173 FROM_HERE, base::BindOnce(std::move(callback), std::string()));
174 return;
175 }
176 providers_by_category_[suggestion_id.category()]->FetchSuggestionImageData(
177 suggestion_id, std::move(callback));
178}
179
jkrcalcd011682017-04-13 05:41:16180// TODO(jkrcal): Split the favicon fetching into a separate class.
jkrcal10004602017-03-29 07:44:28181void ContentSuggestionsService::FetchSuggestionFavicon(
182 const ContentSuggestion::ID& suggestion_id,
183 int minimum_size_in_pixel,
184 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26185 ImageFetchedCallback callback) {
jkrcal8083bc52017-04-24 16:34:02186 const GURL& domain_with_favicon = GetFaviconDomain(suggestion_id);
187 if (!domain_with_favicon.is_valid() || !large_icon_service_) {
jkrcal7b7e71f12017-04-06 13:12:40188 base::ThreadTaskRunnerHandle::Get()->PostTask(
gaschler42544c12017-08-03 15:43:26189 FROM_HERE, base::BindOnce(std::move(callback), gfx::Image()));
jkrcal08d79b52017-04-13 14:02:11190 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40191 return;
192 }
193
jkrcal95acdc5a2017-05-19 15:34:37194 GetFaviconFromCache(domain_with_favicon, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26195 desired_size_in_pixel, std::move(callback),
jkrcal95acdc5a2017-05-19 15:34:37196 /*continue_to_google_server=*/true);
jkrcal7b7e71f12017-04-06 13:12:40197}
198
jkrcal8083bc52017-04-24 16:34:02199GURL ContentSuggestionsService::GetFaviconDomain(
200 const ContentSuggestion::ID& suggestion_id) {
201 const std::vector<ContentSuggestion>& suggestions =
202 suggestions_by_category_[suggestion_id.category()];
203 auto position =
204 std::find_if(suggestions.begin(), suggestions.end(),
205 [&suggestion_id](const ContentSuggestion& suggestion) {
206 return suggestion_id == suggestion.id();
207 });
208 if (position != suggestions.end()) {
209 return position->url_with_favicon();
210 }
211
212 // Look up the URL in the archive of |remote_suggestions_provider_|.
213 // TODO(jkrcal): Fix how Fetch more works or find other ways to remove this
214 // hack. crbug.com/714031
215 if (providers_by_category_[suggestion_id.category()] ==
216 remote_suggestions_provider_) {
217 return remote_suggestions_provider_->GetUrlWithFavicon(suggestion_id);
218 }
219 return GURL();
220}
221
jkrcal95acdc5a2017-05-19 15:34:37222void ContentSuggestionsService::GetFaviconFromCache(
223 const GURL& publisher_url,
224 int minimum_size_in_pixel,
225 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26226 ImageFetchedCallback callback,
jkrcal95acdc5a2017-05-19 15:34:37227 bool continue_to_google_server) {
228 // TODO(jkrcal): Create a general wrapper function in LargeIconService that
229 // does handle the get-from-cache-and-fallback-to-google-server functionality
230 // in one shot (for all clients that do not need to react in between).
Jan Krcalb0cbad92017-06-01 09:03:47231
232 // Use desired_size = 0 for getting the icon from the cache (so that the icon
233 // is not poorly rescaled by LargeIconService).
jkrcal95acdc5a2017-05-19 15:34:37234 large_icon_service_->GetLargeIconImageOrFallbackStyle(
Jan Krcalb0cbad92017-06-01 09:03:47235 publisher_url, minimum_size_in_pixel, /*desired_size_in_pixel=*/0,
jkrcal95acdc5a2017-05-19 15:34:37236 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
237 base::Unretained(this), publisher_url, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26238 desired_size_in_pixel, base::Passed(std::move(callback)),
239 continue_to_google_server),
jkrcal95acdc5a2017-05-19 15:34:37240 &favicons_task_tracker_);
241}
242
jkrcal7b7e71f12017-04-06 13:12:40243void ContentSuggestionsService::OnGetFaviconFromCacheFinished(
244 const GURL& publisher_url,
245 int minimum_size_in_pixel,
246 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26247 ImageFetchedCallback callback,
jkrcal7b7e71f12017-04-06 13:12:40248 bool continue_to_google_server,
249 const favicon_base::LargeIconImageResult& result) {
250 if (!result.image.IsEmpty()) {
gaschler42544c12017-08-03 15:43:26251 std::move(callback).Run(result.image);
jkrcal08d79b52017-04-13 14:02:11252 // The icon is from cache if we haven't gone to Google server yet. The icon
253 // is freshly fetched, otherwise.
254 RecordFaviconFetchResult(continue_to_google_server
255 ? FaviconFetchResult::SUCCESS_CACHED
256 : FaviconFetchResult::SUCCESS_FETCHED);
Jan Krcal5ade20c2017-07-11 13:03:00257 // Update the time when the icon was last requested - postpone thus the
258 // automatic eviction of the favicon from the favicon database.
259 large_icon_service_->TouchIconFromGoogleServer(result.icon_url);
jkrcal7b7e71f12017-04-06 13:12:40260 return;
261 }
262
263 if (!continue_to_google_server ||
264 (result.fallback_icon_style &&
265 !result.fallback_icon_style->is_default_background_color)) {
266 // We cannot download from the server if there is some small icon in the
jkrcal08d79b52017-04-13 14:02:11267 // cache (resulting in non-default background color) or if we already did
268 // so.
gaschler42544c12017-08-03 15:43:26269 std::move(callback).Run(gfx::Image());
jkrcal08d79b52017-04-13 14:02:11270 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40271 return;
272 }
273
274 // Try to fetch the favicon from a Google favicon server.
jkrcal5de6dff2017-05-24 09:59:47275 // TODO(jkrcal): Currently used only for Articles for you which have public
276 // URLs. Let the provider decide whether |publisher_url| may be private or
277 // not.
Ramin Halavati4bf78aa2017-06-01 04:53:45278 net::NetworkTrafficAnnotationTag traffic_annotation =
279 net::DefineNetworkTrafficAnnotation("content_suggestion_get_favicon", R"(
280 semantics {
281 sender: "Content Suggestion"
282 description:
283 "Sends a request to a Google server to retrieve the favicon bitmap "
284 "for an article suggestion on the new tab page (URLs are public "
285 "and provided by Google)."
286 trigger:
287 "A request can be sent if Chrome does not have a favicon for a "
288 "particular page."
289 data: "Page URL and desired icon size."
290 destination: GOOGLE_OWNED_SERVICE
291 }
292 policy {
Ramin Halavati3b979782017-07-21 11:40:26293 cookies_allowed: NO
Ramin Halavati4bf78aa2017-06-01 04:53:45294 setting: "This feature cannot be disabled by settings."
295 policy_exception_justification: "Not implemented."
296 })");
jkrcal7b7e71f12017-04-06 13:12:40297 large_icon_service_
298 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
jkrcalff2ef682017-05-09 07:22:05299 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
Ramin Halavati4bf78aa2017-06-01 04:53:45300 /*may_page_url_be_private=*/false, traffic_annotation,
jkrcal7b7e71f12017-04-06 13:12:40301 base::Bind(
302 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished,
303 base::Unretained(this), publisher_url, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26304 desired_size_in_pixel, base::Passed(std::move(callback))));
jkrcal7b7e71f12017-04-06 13:12:40305}
306
307void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished(
308 const GURL& publisher_url,
309 int minimum_size_in_pixel,
310 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26311 ImageFetchedCallback callback,
Jan Krcalf3e5733e2017-07-10 09:06:11312 favicon_base::GoogleFaviconServerRequestStatus status) {
313 if (status != favicon_base::GoogleFaviconServerRequestStatus::SUCCESS) {
gaschler42544c12017-08-03 15:43:26314 std::move(callback).Run(gfx::Image());
jkrcal08d79b52017-04-13 14:02:11315 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40316 return;
317 }
318
jkrcal95acdc5a2017-05-19 15:34:37319 GetFaviconFromCache(publisher_url, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26320 desired_size_in_pixel, std::move(callback),
jkrcal95acdc5a2017-05-19 15:34:37321 /*continue_to_google_server=*/false);
jkrcal10004602017-03-29 07:44:28322}
323
vitaliii685fdfaa2016-08-31 11:25:46324void ContentSuggestionsService::ClearHistory(
325 base::Time begin,
326 base::Time end,
327 const base::Callback<bool(const GURL& url)>& filter) {
328 for (const auto& provider : providers_) {
329 provider->ClearHistory(begin, end, filter);
330 }
vitaliii6343b2c2017-01-04 07:57:11331 category_ranker_->ClearHistory(begin, end);
tschumann5829c3412017-01-09 21:45:43332 // This potentially removed personalized data which we shouldn't display
333 // anymore.
334 for (Observer& observer : observers_) {
335 observer.OnFullRefreshRequired();
336 }
vitaliii685fdfaa2016-08-31 11:25:46337}
338
treib7d1d7a52016-08-24 14:04:55339void ContentSuggestionsService::ClearAllCachedSuggestions() {
pke6dbb90af2016-07-08 14:00:46340 suggestions_by_category_.clear();
Jan Krcal33ed7512017-11-29 12:45:09341 for (const auto& provider : providers_) {
342 provider->ClearCachedSuggestions();
pke6dbb90af2016-07-08 14:00:46343 }
Jan Krcal33ed7512017-11-29 12:45:09344 for (Observer& observer : observers_) {
345 observer.OnFullRefreshRequired();
vitaliii4408d6c2016-11-21 14:16:24346 }
pke151b5502016-08-09 12:15:13347}
348
pkede0dd9f2016-08-23 09:18:11349void ContentSuggestionsService::GetDismissedSuggestionsForDebugging(
350 Category category,
gaschlerdf40dcc2017-08-04 14:06:37351 DismissedSuggestionsCallback callback) {
pke151b5502016-08-09 12:15:13352 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24353 if (iterator != providers_by_category_.end()) {
gaschlerdf40dcc2017-08-04 14:06:37354 iterator->second->GetDismissedSuggestionsForDebugging(category,
355 std::move(callback));
vitaliii4408d6c2016-11-21 14:16:24356 } else {
gaschlerdf40dcc2017-08-04 14:06:37357 std::move(callback).Run(std::vector<ContentSuggestion>());
vitaliii4408d6c2016-11-21 14:16:24358 }
pke151b5502016-08-09 12:15:13359}
360
361void ContentSuggestionsService::ClearDismissedSuggestionsForDebugging(
362 Category category) {
363 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24364 if (iterator != providers_by_category_.end()) {
pke151b5502016-08-09 12:15:13365 iterator->second->ClearDismissedSuggestionsForDebugging(category);
vitaliii4408d6c2016-11-21 14:16:24366 }
pke6dbb90af2016-07-08 14:00:46367}
368
pke2646c95b2016-07-25 12:18:44369void ContentSuggestionsService::DismissSuggestion(
treib4bbc54922016-09-28 17:26:44370 const ContentSuggestion::ID& suggestion_id) {
371 if (!providers_by_category_.count(suggestion_id.category())) {
pke2646c95b2016-07-25 12:18:44372 LOG(WARNING) << "Dismissed suggestion " << suggestion_id
treib4bbc54922016-09-28 17:26:44373 << " for unavailable category " << suggestion_id.category();
pke6dbb90af2016-07-08 14:00:46374 return;
375 }
dgnf6500c12017-05-09 17:05:31376
377 metrics::RecordContentSuggestionDismissed();
378
treib4bbc54922016-09-28 17:26:44379 providers_by_category_[suggestion_id.category()]->DismissSuggestion(
380 suggestion_id);
pke6dbb90af2016-07-08 14:00:46381
vitaliii2600e8e02016-12-09 17:23:36382 // Remove the suggestion locally if it is present. A suggestion may be missing
383 // localy e.g. if it was sent to UI through |Fetch| or it has been dismissed
384 // from a different NTP.
385 RemoveSuggestionByID(suggestion_id);
pke6dbb90af2016-07-08 14:00:46386}
387
dgn212feea3b2016-09-16 15:08:20388void ContentSuggestionsService::DismissCategory(Category category) {
389 auto providers_it = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24390 if (providers_it == providers_by_category_.end()) {
dgn212feea3b2016-09-16 15:08:20391 return;
vitaliii4408d6c2016-11-21 14:16:24392 }
dgn212feea3b2016-09-16 15:08:20393
dgnf6500c12017-05-09 17:05:31394 metrics::RecordCategoryDismissed();
395
dgn52914722016-10-18 10:28:42396 ContentSuggestionsProvider* provider = providers_it->second;
397 UnregisterCategory(category, provider);
398
399 dismissed_providers_by_category_[category] = provider;
400 StoreDismissedCategoriesToPrefs();
vitaliii3d9423e2017-01-03 17:08:13401
402 category_ranker_->OnCategoryDismissed(category);
dgn212feea3b2016-09-16 15:08:20403}
404
mvanouwerkerk52783d92016-10-12 11:03:40405void ContentSuggestionsService::RestoreDismissedCategories() {
406 // Make a copy as the original will be modified during iteration.
407 auto dismissed_providers_by_category_copy = dismissed_providers_by_category_;
408 for (const auto& category_provider_pair :
409 dismissed_providers_by_category_copy) {
dgn52914722016-10-18 10:28:42410 RestoreDismissedCategory(category_provider_pair.first);
mvanouwerkerk52783d92016-10-12 11:03:40411 }
dgn52914722016-10-18 10:28:42412 StoreDismissedCategoriesToPrefs();
mvanouwerkerk52783d92016-10-12 11:03:40413 DCHECK(dismissed_providers_by_category_.empty());
414}
415
pke6dbb90af2016-07-08 14:00:46416void ContentSuggestionsService::AddObserver(Observer* observer) {
417 observers_.AddObserver(observer);
418}
419
420void ContentSuggestionsService::RemoveObserver(Observer* observer) {
421 observers_.RemoveObserver(observer);
422}
423
424void ContentSuggestionsService::RegisterProvider(
pke5728f082016-08-03 17:27:35425 std::unique_ptr<ContentSuggestionsProvider> provider) {
426 DCHECK(state_ == State::ENABLED);
pke5728f082016-08-03 17:27:35427 providers_.push_back(std::move(provider));
pke6dbb90af2016-07-08 14:00:46428}
429
tschumann83578aa2016-11-03 13:18:32430void ContentSuggestionsService::Fetch(
431 const Category& category,
432 const std::set<std::string>& known_suggestion_ids,
gaschlerdf40dcc2017-08-04 14:06:37433 FetchDoneCallback callback) {
tschumann83578aa2016-11-03 13:18:32434 auto providers_it = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24435 if (providers_it == providers_by_category_.end()) {
tschumann83578aa2016-11-03 13:18:32436 return;
vitaliii4408d6c2016-11-21 14:16:24437 }
tschumann83578aa2016-11-03 13:18:32438
dgnf6500c12017-05-09 17:05:31439 metrics::RecordFetchAction();
440
gaschlerdf40dcc2017-08-04 14:06:37441 providers_it->second->Fetch(category, known_suggestion_ids,
442 std::move(callback));
tschumann83578aa2016-11-03 13:18:32443}
444
jkrcal093410c2016-12-21 16:13:55445void ContentSuggestionsService::ReloadSuggestions() {
446 for (const auto& provider : providers_) {
447 provider->ReloadSuggestions();
448 }
449}
450
dgn65d6cd82017-04-11 00:36:36451bool ContentSuggestionsService::AreRemoteSuggestionsEnabled() const {
Nicolas Dossou-gbete18994652017-07-19 16:32:17452 return remote_suggestions_provider_ &&
453 !remote_suggestions_provider_->IsDisabled();
dgnb8a8a5c2017-03-31 12:35:36454}
455
pke6dbb90af2016-07-08 14:00:46456////////////////////////////////////////////////////////////////////////////////
457// Private methods
458
459void ContentSuggestionsService::OnNewSuggestions(
pke4d3a4d62016-08-02 09:06:21460 ContentSuggestionsProvider* provider,
461 Category category,
treib62e819e2016-09-27 11:47:34462 std::vector<ContentSuggestion> suggestions) {
treib534523b2016-10-20 16:19:44463 // Providers shouldn't call this when they're in a non-available state.
464 DCHECK(
465 IsCategoryStatusInitOrAvailable(provider->GetCategoryStatus(category)));
466
dgn52914722016-10-18 10:28:42467 if (TryRegisterProviderForCategory(provider, category)) {
pke4d3a4d62016-08-02 09:06:21468 NotifyCategoryStatusChanged(category);
dgn52914722016-10-18 10:28:42469 } else if (IsCategoryDismissed(category)) {
470 // The category has been registered as a dismissed one. We need to
471 // check if the dismissal can be cleared now that we received new data.
vitaliii4408d6c2016-11-21 14:16:24472 if (suggestions.empty()) {
dgn52914722016-10-18 10:28:42473 return;
vitaliii4408d6c2016-11-21 14:16:24474 }
dgn52914722016-10-18 10:28:42475
476 RestoreDismissedCategory(category);
477 StoreDismissedCategoriesToPrefs();
478
479 NotifyCategoryStatusChanged(category);
480 }
pke3b2e3632016-08-12 12:52:53481
treib39fffa12016-10-14 14:59:10482 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
483 // A provider shouldn't send us suggestions while it's not available.
484 DCHECK(suggestions.empty());
pke3b2e3632016-08-12 12:52:53485 return;
treib39fffa12016-10-14 14:59:10486 }
pke6dbb90af2016-07-08 14:00:46487
treib62e819e2016-09-27 11:47:34488 suggestions_by_category_[category] = std::move(suggestions);
pke6dbb90af2016-07-08 14:00:46489
vitaliii4408d6c2016-11-21 14:16:24490 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13491 observer.OnNewSuggestions(category);
vitaliii4408d6c2016-11-21 14:16:24492 }
pke6dbb90af2016-07-08 14:00:46493}
494
495void ContentSuggestionsService::OnCategoryStatusChanged(
pke4d3a4d62016-08-02 09:06:21496 ContentSuggestionsProvider* provider,
497 Category category,
pke9c5095ac2016-08-01 13:53:12498 CategoryStatus new_status) {
pke4d3a4d62016-08-02 09:06:21499 if (new_status == CategoryStatus::NOT_PROVIDED) {
dgn52914722016-10-18 10:28:42500 UnregisterCategory(category, provider);
pke4d3a4d62016-08-02 09:06:21501 } else {
vitaliii4408d6c2016-11-21 14:16:24502 if (!IsCategoryStatusAvailable(new_status)) {
dgn52914722016-10-18 10:28:42503 suggestions_by_category_.erase(category);
vitaliii4408d6c2016-11-21 14:16:24504 }
dgn52914722016-10-18 10:28:42505 TryRegisterProviderForCategory(provider, category);
pke4d3a4d62016-08-02 09:06:21506 DCHECK_EQ(new_status, provider->GetCategoryStatus(category));
507 }
dgn52914722016-10-18 10:28:42508
vitaliii4408d6c2016-11-21 14:16:24509 if (!IsCategoryDismissed(category)) {
dgn52914722016-10-18 10:28:42510 NotifyCategoryStatusChanged(category);
vitaliii4408d6c2016-11-21 14:16:24511 }
pke6dbb90af2016-07-08 14:00:46512}
513
pke2a48f852016-08-18 13:33:52514void ContentSuggestionsService::OnSuggestionInvalidated(
515 ContentSuggestionsProvider* provider,
treib4bbc54922016-09-28 17:26:44516 const ContentSuggestion::ID& suggestion_id) {
517 RemoveSuggestionByID(suggestion_id);
vitaliii4408d6c2016-11-21 14:16:24518 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13519 observer.OnSuggestionInvalidated(suggestion_id);
vitaliii4408d6c2016-11-21 14:16:24520 }
pke2a48f852016-08-18 13:33:52521}
Colin Blundell076714ff2018-02-12 17:26:14522// identity::IdentityManager::Observer implementation
523void ContentSuggestionsService::OnPrimaryAccountSet(
524 const AccountInfo& account_info) {
Colin Blundellce266a692018-01-16 11:07:16525 OnSignInStateChanged(/*has_signed_in=*/true);
dgnf5708892016-11-22 10:36:22526}
527
Colin Blundell076714ff2018-02-12 17:26:14528void ContentSuggestionsService::OnPrimaryAccountCleared(
529 const AccountInfo& account_info) {
Colin Blundellce266a692018-01-16 11:07:16530 OnSignInStateChanged(/*has_signed_in=*/false);
dgnf5708892016-11-22 10:36:22531}
532
vitaliii45941152016-09-05 08:58:13533// history::HistoryServiceObserver implementation.
534void ContentSuggestionsService::OnURLsDeleted(
535 history::HistoryService* history_service,
536 bool all_history,
537 bool expired,
538 const history::URLRows& deleted_rows,
539 const std::set<GURL>& favicon_urls) {
540 // We don't care about expired entries.
vitaliii4408d6c2016-11-21 14:16:24541 if (expired) {
vitaliii45941152016-09-05 08:58:13542 return;
vitaliii4408d6c2016-11-21 14:16:24543 }
vitaliii45941152016-09-05 08:58:13544
vitaliii45941152016-09-05 08:58:13545 if (all_history) {
vitaliii45941152016-09-05 08:58:13546 base::Callback<bool(const GURL& url)> filter =
547 base::Bind([](const GURL& url) { return true; });
tschumann5829c3412017-01-09 21:45:43548 ClearHistory(base::Time(), base::Time::Max(), filter);
vitaliii45941152016-09-05 08:58:13549 } else {
tschumann5829c3412017-01-09 21:45:43550 // If a user deletes a single URL, we don't consider this a clear user
551 // intend to clear our data.
552 // TODO(tschumann): Single URL deletions should be handled on a case-by-case
553 // basis. However this depends on the provider's details and thus cannot be
554 // done here. Introduce a OnURLsDeleted() method on the providers to move
555 // this decision further down.
556 if (deleted_rows.size() < 2) {
vitaliii45941152016-09-05 08:58:13557 return;
vitaliii4408d6c2016-11-21 14:16:24558 }
vitaliii45941152016-09-05 08:58:13559 std::set<GURL> deleted_urls;
560 for (const history::URLRow& row : deleted_rows) {
vitaliii45941152016-09-05 08:58:13561 deleted_urls.insert(row.url());
562 }
sfierae8969bc2017-03-26 18:38:41563 base::Callback<bool(const GURL& url)> filter =
564 base::Bind([](const std::set<GURL>& set,
565 const GURL& url) { return set.count(url) != 0; },
566 deleted_urls);
tschumann5829c3412017-01-09 21:45:43567 // We usually don't have any time-related information (the URLRow objects
568 // usually don't provide a |last_visit()| timestamp. Hence we simply clear
569 // the whole history for the selected URLs.
570 ClearHistory(base::Time(), base::Time::Max(), filter);
vitaliii45941152016-09-05 08:58:13571 }
572}
573
574void ContentSuggestionsService::HistoryServiceBeingDeleted(
575 history::HistoryService* history_service) {
576 history_service_observer_.RemoveAll();
577}
578
dgn52914722016-10-18 10:28:42579bool ContentSuggestionsService::TryRegisterProviderForCategory(
pke4d3a4d62016-08-02 09:06:21580 ContentSuggestionsProvider* provider,
581 Category category) {
582 auto it = providers_by_category_.find(category);
583 if (it != providers_by_category_.end()) {
584 DCHECK_EQ(it->second, provider);
585 return false;
586 }
587
mvanouwerkerk52783d92016-10-12 11:03:40588 auto dismissed_it = dismissed_providers_by_category_.find(category);
589 if (dismissed_it != dismissed_providers_by_category_.end()) {
dgn52914722016-10-18 10:28:42590 // The initialisation of dismissed categories registers them with |nullptr|
591 // for providers, we need to check for that to see if the provider is
592 // already registered or not.
593 if (!dismissed_it->second) {
594 dismissed_it->second = provider;
595 } else {
596 DCHECK_EQ(dismissed_it->second, provider);
597 }
598 return false;
mvanouwerkerk52783d92016-10-12 11:03:40599 }
600
dgn52914722016-10-18 10:28:42601 RegisterCategory(category, provider);
602 return true;
603}
604
605void ContentSuggestionsService::RegisterCategory(
606 Category category,
607 ContentSuggestionsProvider* provider) {
608 DCHECK(!base::ContainsKey(providers_by_category_, category));
609 DCHECK(!IsCategoryDismissed(category));
610
pke4d3a4d62016-08-02 09:06:21611 providers_by_category_[category] = provider;
612 categories_.push_back(category);
pke4d3a4d62016-08-02 09:06:21613 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
614 suggestions_by_category_.insert(
615 std::make_pair(category, std::vector<ContentSuggestion>()));
616 }
dgn52914722016-10-18 10:28:42617}
618
619void ContentSuggestionsService::UnregisterCategory(
620 Category category,
621 ContentSuggestionsProvider* provider) {
622 auto providers_it = providers_by_category_.find(category);
623 if (providers_it == providers_by_category_.end()) {
624 DCHECK(IsCategoryDismissed(category));
625 return;
626 }
627
628 DCHECK_EQ(provider, providers_it->second);
629 providers_by_category_.erase(providers_it);
630 categories_.erase(
631 std::find(categories_.begin(), categories_.end(), category));
632 suggestions_by_category_.erase(category);
pke6dbb90af2016-07-08 14:00:46633}
634
pke2a48f852016-08-18 13:33:52635bool ContentSuggestionsService::RemoveSuggestionByID(
treib4bbc54922016-09-28 17:26:44636 const ContentSuggestion::ID& suggestion_id) {
pke2a48f852016-08-18 13:33:52637 std::vector<ContentSuggestion>* suggestions =
treib4bbc54922016-09-28 17:26:44638 &suggestions_by_category_[suggestion_id.category()];
pke2a48f852016-08-18 13:33:52639 auto position =
640 std::find_if(suggestions->begin(), suggestions->end(),
641 [&suggestion_id](const ContentSuggestion& suggestion) {
642 return suggestion_id == suggestion.id();
643 });
vitaliii4408d6c2016-11-21 14:16:24644 if (position == suggestions->end()) {
pke2a48f852016-08-18 13:33:52645 return false;
vitaliii4408d6c2016-11-21 14:16:24646 }
pke2a48f852016-08-18 13:33:52647 suggestions->erase(position);
treib063e6a62016-08-25 11:34:29648
pke2a48f852016-08-18 13:33:52649 return true;
650}
651
pke9c5095ac2016-08-01 13:53:12652void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) {
vitaliii4408d6c2016-11-21 14:16:24653 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13654 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category));
vitaliii4408d6c2016-11-21 14:16:24655 }
pke6dbb90af2016-07-08 14:00:46656}
657
Colin Blundellce266a692018-01-16 11:07:16658void ContentSuggestionsService::OnSignInStateChanged(bool has_signed_in) {
dgnf5708892016-11-22 10:36:22659 // First notify the providers, so they can make the required changes.
660 for (const auto& provider : providers_) {
Colin Blundellce266a692018-01-16 11:07:16661 provider->OnSignInStateChanged(has_signed_in);
dgnf5708892016-11-22 10:36:22662 }
663
664 // Finally notify the observers so they refresh only after the backend is
665 // ready.
666 for (Observer& observer : observers_) {
667 observer.OnFullRefreshRequired();
668 }
669}
670
dgn52914722016-10-18 10:28:42671bool ContentSuggestionsService::IsCategoryDismissed(Category category) const {
672 return base::ContainsKey(dismissed_providers_by_category_, category);
673}
674
675void ContentSuggestionsService::RestoreDismissedCategory(Category category) {
676 auto dismissed_it = dismissed_providers_by_category_.find(category);
677 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category));
678
679 // Keep the reference to the provider and remove it from the dismissed ones,
680 // because the category registration enforces that it's not dismissed.
681 ContentSuggestionsProvider* provider = dismissed_it->second;
682 dismissed_providers_by_category_.erase(dismissed_it);
683
vitaliii4408d6c2016-11-21 14:16:24684 if (provider) {
dgn52914722016-10-18 10:28:42685 RegisterCategory(category, provider);
vitaliii4408d6c2016-11-21 14:16:24686 }
dgn52914722016-10-18 10:28:42687}
688
689void ContentSuggestionsService::RestoreDismissedCategoriesFromPrefs() {
690 // This must only be called at startup.
691 DCHECK(dismissed_providers_by_category_.empty());
692 DCHECK(providers_by_category_.empty());
693
694 const base::ListValue* list =
695 pref_service_->GetList(prefs::kDismissedCategories);
jdoerriea5676c62017-04-11 18:09:14696 for (const base::Value& entry : *list) {
dgn52914722016-10-18 10:28:42697 int id = 0;
jdoerriea5676c62017-04-11 18:09:14698 if (!entry.GetAsInteger(&id)) {
699 DLOG(WARNING) << "Invalid category pref value: " << entry;
dgn52914722016-10-18 10:28:42700 continue;
701 }
702
703 // When the provider is registered, it will be stored in this map.
vitaliii7456f5a2016-12-19 11:13:25704 dismissed_providers_by_category_[Category::FromIDValue(id)] = nullptr;
dgn52914722016-10-18 10:28:42705 }
706}
707
708void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() {
709 base::ListValue list;
710 for (const auto& category_provider_pair : dismissed_providers_by_category_) {
711 list.AppendInteger(category_provider_pair.first.id());
712 }
713
714 pref_service_->Set(prefs::kDismissedCategories, list);
715}
716
vitaliii69689f312017-10-25 17:36:06717void ContentSuggestionsService::DestroyCategoryAndItsProvider(
718 Category category) {
719 // Destroying articles category is more complex and not implemented.
720 DCHECK_NE(category, Category::FromKnownCategory(KnownCategories::ARTICLES));
721
722 if (providers_by_category_.count(category) != 1) {
723 return;
724 }
725
726 { // Destroy the provider and delete its mentions.
727 ContentSuggestionsProvider* raw_provider = providers_by_category_[category];
728 base::EraseIf(
729 providers_,
730 [&raw_provider](
731 const std::unique_ptr<ContentSuggestionsProvider>& provider) {
732 return provider.get() == raw_provider;
733 });
734 providers_by_category_.erase(category);
735
736 if (dismissed_providers_by_category_.count(category) == 1) {
737 dismissed_providers_by_category_[category] = nullptr;
738 }
739 }
740
741 suggestions_by_category_.erase(category);
742
743 auto it = std::find(categories_.begin(), categories_.end(), category);
744 categories_.erase(it);
745
746 // Notify observers that the category is gone.
747 NotifyCategoryStatusChanged(category);
748}
749
pke6dbb90af2016-07-08 14:00:46750} // namespace ntp_snippets