blob: 962eedd89f002616fa302134c45994def4e6210c [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"
pke6dbb90af2016-07-08 14:00:4616#include "base/strings/string_number_conversions.h"
pke1da90602016-08-05 14:20:2717#include "base/threading/thread_task_runner_handle.h"
jkrcal27b02c12017-03-21 11:18:2618#include "base/time/default_clock.h"
dgn52914722016-10-18 10:28:4219#include "base/values.h"
jkrcal7b7e71f12017-04-06 13:12:4020#include "components/favicon/core/large_icon_service.h"
21#include "components/favicon_base/fallback_icon_style.h"
22#include "components/favicon_base/favicon_types.h"
dgnf6500c12017-05-09 17:05:3123#include "components/ntp_snippets/content_suggestions_metrics.h"
dgn52914722016-10-18 10:28:4224#include "components/ntp_snippets/pref_names.h"
jkrcal8083bc52017-04-24 16:34:0225#include "components/ntp_snippets/remote/remote_suggestions_provider.h"
dgn52914722016-10-18 10:28:4226#include "components/prefs/pref_registry_simple.h"
27#include "components/prefs/pref_service.h"
Ramin Halavati4bf78aa2017-06-01 04:53:4528#include "net/traffic_annotation/network_traffic_annotation.h"
pke6dbb90af2016-07-08 14:00:4629#include "ui/gfx/image/image.h"
30
31namespace ntp_snippets {
32
jkrcal08d79b52017-04-13 14:02:1133namespace {
34
35// Enumeration listing all possible outcomes for fetch attempts of favicons for
36// content suggestions. Used for UMA histograms, so do not change existing
37// values. Insert new values at the end, and update the histogram definition.
38// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets
39enum class FaviconFetchResult {
40 SUCCESS_CACHED = 0,
41 SUCCESS_FETCHED = 1,
42 FAILURE = 2,
43 COUNT = 3
44};
45
46void RecordFaviconFetchResult(FaviconFetchResult result) {
47 UMA_HISTOGRAM_ENUMERATION(
48 "NewTabPage.ContentSuggestions.ArticleFaviconFetchResult", result,
49 FaviconFetchResult::COUNT);
50}
51
52} // namespace
53
vitaliii45941152016-09-05 08:58:1354ContentSuggestionsService::ContentSuggestionsService(
55 State state,
dgnf5708892016-11-22 10:36:2256 SigninManagerBase* signin_manager,
jkrcale13510e2016-09-08 17:56:2057 history::HistoryService* history_service,
jkrcal7b7e71f12017-04-06 13:12:4058 favicon::LargeIconService* large_icon_service,
vitaliii7456f5a2016-12-19 11:13:2559 PrefService* pref_service,
jkrcalf9966462017-03-29 16:25:2160 std::unique_ptr<CategoryRanker> category_ranker,
61 std::unique_ptr<UserClassifier> user_classifier,
62 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler)
jkrcale13510e2016-09-08 17:56:2063 : state_(state),
dgnf5708892016-11-22 10:36:2264 signin_observer_(this),
jkrcale13510e2016-09-08 17:56:2065 history_service_observer_(this),
jkrcal093410c2016-12-21 16:13:5566 remote_suggestions_provider_(nullptr),
jkrcal7b7e71f12017-04-06 13:12:4067 large_icon_service_(large_icon_service),
dgn52914722016-10-18 10:28:4268 pref_service_(pref_service),
jkrcalf9966462017-03-29 16:25:2169 remote_suggestions_scheduler_(std::move(remote_suggestions_scheduler)),
70 user_classifier_(std::move(user_classifier)),
vitaliii7456f5a2016-12-19 11:13:2571 category_ranker_(std::move(category_ranker)) {
vitaliii45941152016-09-05 08:58:1372 // Can be null in tests.
dgnf5708892016-11-22 10:36:2273 if (signin_manager) {
74 signin_observer_.Add(signin_manager);
75 }
76
vitaliii4408d6c2016-11-21 14:16:2477 if (history_service) {
vitaliii45941152016-09-05 08:58:1378 history_service_observer_.Add(history_service);
vitaliii4408d6c2016-11-21 14:16:2479 }
dgn52914722016-10-18 10:28:4280
81 RestoreDismissedCategoriesFromPrefs();
vitaliii45941152016-09-05 08:58:1382}
pke6dbb90af2016-07-08 14:00:4683
treib62e819e2016-09-27 11:47:3484ContentSuggestionsService::~ContentSuggestionsService() = default;
pke6dbb90af2016-07-08 14:00:4685
86void ContentSuggestionsService::Shutdown() {
jkrcal093410c2016-12-21 16:13:5587 remote_suggestions_provider_ = nullptr;
88 remote_suggestions_scheduler_ = nullptr;
pke5728f082016-08-03 17:27:3589 suggestions_by_category_.clear();
90 providers_by_category_.clear();
91 categories_.clear();
92 providers_.clear();
pke6dbb90af2016-07-08 14:00:4693 state_ = State::DISABLED;
vitaliii4408d6c2016-11-21 14:16:2494 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:1395 observer.ContentSuggestionsServiceShutdown();
vitaliii4408d6c2016-11-21 14:16:2496 }
pke6dbb90af2016-07-08 14:00:4697}
98
dgn52914722016-10-18 10:28:4299// static
100void ContentSuggestionsService::RegisterProfilePrefs(
101 PrefRegistrySimple* registry) {
102 registry->RegisterListPref(prefs::kDismissedCategories);
103}
104
vitaliii8b5ab282016-12-20 11:06:22105std::vector<Category> ContentSuggestionsService::GetCategories() const {
106 std::vector<Category> sorted_categories = categories_;
107 std::sort(sorted_categories.begin(), sorted_categories.end(),
108 [this](const Category& left, const Category& right) {
109 return category_ranker_->Compare(left, right);
110 });
111 return sorted_categories;
112}
113
pke9c5095ac2016-08-01 13:53:12114CategoryStatus ContentSuggestionsService::GetCategoryStatus(
115 Category category) const {
pke6dbb90af2016-07-08 14:00:46116 if (state_ == State::DISABLED) {
pke9c5095ac2016-08-01 13:53:12117 return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED;
pke6dbb90af2016-07-08 14:00:46118 }
119
pke4d3a4d62016-08-02 09:06:21120 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24121 if (iterator == providers_by_category_.end()) {
pke9c5095ac2016-08-01 13:53:12122 return CategoryStatus::NOT_PROVIDED;
vitaliii4408d6c2016-11-21 14:16:24123 }
pke6dbb90af2016-07-08 14:00:46124
125 return iterator->second->GetCategoryStatus(category);
126}
127
pkebd2f650a2016-08-09 14:53:45128base::Optional<CategoryInfo> ContentSuggestionsService::GetCategoryInfo(
129 Category category) const {
130 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24131 if (iterator == providers_by_category_.end()) {
pkebd2f650a2016-08-09 14:53:45132 return base::Optional<CategoryInfo>();
vitaliii4408d6c2016-11-21 14:16:24133 }
pkebd2f650a2016-08-09 14:53:45134 return iterator->second->GetCategoryInfo(category);
135}
136
pke6dbb90af2016-07-08 14:00:46137const std::vector<ContentSuggestion>&
pke9c5095ac2016-08-01 13:53:12138ContentSuggestionsService::GetSuggestionsForCategory(Category category) const {
pke6dbb90af2016-07-08 14:00:46139 auto iterator = suggestions_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24140 if (iterator == suggestions_by_category_.end()) {
pke6dbb90af2016-07-08 14:00:46141 return no_suggestions_;
vitaliii4408d6c2016-11-21 14:16:24142 }
pke6dbb90af2016-07-08 14:00:46143 return iterator->second;
144}
145
146void ContentSuggestionsService::FetchSuggestionImage(
treib4bbc54922016-09-28 17:26:44147 const ContentSuggestion::ID& suggestion_id,
gaschler42544c12017-08-03 15:43:26148 ImageFetchedCallback callback) {
treib4bbc54922016-09-28 17:26:44149 if (!providers_by_category_.count(suggestion_id.category())) {
pke6dbb90af2016-07-08 14:00:46150 LOG(WARNING) << "Requested image for suggestion " << suggestion_id
treib4bbc54922016-09-28 17:26:44151 << " for unavailable category " << suggestion_id.category();
pke1da90602016-08-05 14:20:27152 base::ThreadTaskRunnerHandle::Get()->PostTask(
gaschler42544c12017-08-03 15:43:26153 FROM_HERE, base::BindOnce(std::move(callback), gfx::Image()));
pke6dbb90af2016-07-08 14:00:46154 return;
155 }
treib4bbc54922016-09-28 17:26:44156 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage(
gaschler42544c12017-08-03 15:43:26157 suggestion_id, std::move(callback));
pke6dbb90af2016-07-08 14:00:46158}
159
jkrcalcd011682017-04-13 05:41:16160// TODO(jkrcal): Split the favicon fetching into a separate class.
jkrcal10004602017-03-29 07:44:28161void ContentSuggestionsService::FetchSuggestionFavicon(
162 const ContentSuggestion::ID& suggestion_id,
163 int minimum_size_in_pixel,
164 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26165 ImageFetchedCallback callback) {
jkrcal8083bc52017-04-24 16:34:02166 const GURL& domain_with_favicon = GetFaviconDomain(suggestion_id);
167 if (!domain_with_favicon.is_valid() || !large_icon_service_) {
jkrcal7b7e71f12017-04-06 13:12:40168 base::ThreadTaskRunnerHandle::Get()->PostTask(
gaschler42544c12017-08-03 15:43:26169 FROM_HERE, base::BindOnce(std::move(callback), gfx::Image()));
jkrcal08d79b52017-04-13 14:02:11170 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40171 return;
172 }
173
jkrcal95acdc5a2017-05-19 15:34:37174 GetFaviconFromCache(domain_with_favicon, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26175 desired_size_in_pixel, std::move(callback),
jkrcal95acdc5a2017-05-19 15:34:37176 /*continue_to_google_server=*/true);
jkrcal7b7e71f12017-04-06 13:12:40177}
178
jkrcal8083bc52017-04-24 16:34:02179GURL ContentSuggestionsService::GetFaviconDomain(
180 const ContentSuggestion::ID& suggestion_id) {
181 const std::vector<ContentSuggestion>& suggestions =
182 suggestions_by_category_[suggestion_id.category()];
183 auto position =
184 std::find_if(suggestions.begin(), suggestions.end(),
185 [&suggestion_id](const ContentSuggestion& suggestion) {
186 return suggestion_id == suggestion.id();
187 });
188 if (position != suggestions.end()) {
189 return position->url_with_favicon();
190 }
191
192 // Look up the URL in the archive of |remote_suggestions_provider_|.
193 // TODO(jkrcal): Fix how Fetch more works or find other ways to remove this
194 // hack. crbug.com/714031
195 if (providers_by_category_[suggestion_id.category()] ==
196 remote_suggestions_provider_) {
197 return remote_suggestions_provider_->GetUrlWithFavicon(suggestion_id);
198 }
199 return GURL();
200}
201
jkrcal95acdc5a2017-05-19 15:34:37202void ContentSuggestionsService::GetFaviconFromCache(
203 const GURL& publisher_url,
204 int minimum_size_in_pixel,
205 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26206 ImageFetchedCallback callback,
jkrcal95acdc5a2017-05-19 15:34:37207 bool continue_to_google_server) {
208 // TODO(jkrcal): Create a general wrapper function in LargeIconService that
209 // does handle the get-from-cache-and-fallback-to-google-server functionality
210 // in one shot (for all clients that do not need to react in between).
Jan Krcalb0cbad92017-06-01 09:03:47211
212 // Use desired_size = 0 for getting the icon from the cache (so that the icon
213 // is not poorly rescaled by LargeIconService).
jkrcal95acdc5a2017-05-19 15:34:37214 large_icon_service_->GetLargeIconImageOrFallbackStyle(
Jan Krcalb0cbad92017-06-01 09:03:47215 publisher_url, minimum_size_in_pixel, /*desired_size_in_pixel=*/0,
jkrcal95acdc5a2017-05-19 15:34:37216 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
217 base::Unretained(this), publisher_url, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26218 desired_size_in_pixel, base::Passed(std::move(callback)),
219 continue_to_google_server),
jkrcal95acdc5a2017-05-19 15:34:37220 &favicons_task_tracker_);
221}
222
jkrcal7b7e71f12017-04-06 13:12:40223void ContentSuggestionsService::OnGetFaviconFromCacheFinished(
224 const GURL& publisher_url,
225 int minimum_size_in_pixel,
226 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26227 ImageFetchedCallback callback,
jkrcal7b7e71f12017-04-06 13:12:40228 bool continue_to_google_server,
229 const favicon_base::LargeIconImageResult& result) {
230 if (!result.image.IsEmpty()) {
gaschler42544c12017-08-03 15:43:26231 std::move(callback).Run(result.image);
jkrcal08d79b52017-04-13 14:02:11232 // The icon is from cache if we haven't gone to Google server yet. The icon
233 // is freshly fetched, otherwise.
234 RecordFaviconFetchResult(continue_to_google_server
235 ? FaviconFetchResult::SUCCESS_CACHED
236 : FaviconFetchResult::SUCCESS_FETCHED);
Jan Krcal5ade20c2017-07-11 13:03:00237 // Update the time when the icon was last requested - postpone thus the
238 // automatic eviction of the favicon from the favicon database.
239 large_icon_service_->TouchIconFromGoogleServer(result.icon_url);
jkrcal7b7e71f12017-04-06 13:12:40240 return;
241 }
242
243 if (!continue_to_google_server ||
244 (result.fallback_icon_style &&
245 !result.fallback_icon_style->is_default_background_color)) {
246 // We cannot download from the server if there is some small icon in the
jkrcal08d79b52017-04-13 14:02:11247 // cache (resulting in non-default background color) or if we already did
248 // so.
gaschler42544c12017-08-03 15:43:26249 std::move(callback).Run(gfx::Image());
jkrcal08d79b52017-04-13 14:02:11250 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40251 return;
252 }
253
254 // Try to fetch the favicon from a Google favicon server.
jkrcal5de6dff2017-05-24 09:59:47255 // TODO(jkrcal): Currently used only for Articles for you which have public
256 // URLs. Let the provider decide whether |publisher_url| may be private or
257 // not.
Ramin Halavati4bf78aa2017-06-01 04:53:45258 net::NetworkTrafficAnnotationTag traffic_annotation =
259 net::DefineNetworkTrafficAnnotation("content_suggestion_get_favicon", R"(
260 semantics {
261 sender: "Content Suggestion"
262 description:
263 "Sends a request to a Google server to retrieve the favicon bitmap "
264 "for an article suggestion on the new tab page (URLs are public "
265 "and provided by Google)."
266 trigger:
267 "A request can be sent if Chrome does not have a favicon for a "
268 "particular page."
269 data: "Page URL and desired icon size."
270 destination: GOOGLE_OWNED_SERVICE
271 }
272 policy {
Ramin Halavati3b979782017-07-21 11:40:26273 cookies_allowed: NO
Ramin Halavati4bf78aa2017-06-01 04:53:45274 setting: "This feature cannot be disabled by settings."
275 policy_exception_justification: "Not implemented."
276 })");
jkrcal7b7e71f12017-04-06 13:12:40277 large_icon_service_
278 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
jkrcalff2ef682017-05-09 07:22:05279 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
Ramin Halavati4bf78aa2017-06-01 04:53:45280 /*may_page_url_be_private=*/false, traffic_annotation,
jkrcal7b7e71f12017-04-06 13:12:40281 base::Bind(
282 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished,
283 base::Unretained(this), publisher_url, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26284 desired_size_in_pixel, base::Passed(std::move(callback))));
jkrcal7b7e71f12017-04-06 13:12:40285}
286
287void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished(
288 const GURL& publisher_url,
289 int minimum_size_in_pixel,
290 int desired_size_in_pixel,
gaschler42544c12017-08-03 15:43:26291 ImageFetchedCallback callback,
Jan Krcalf3e5733e2017-07-10 09:06:11292 favicon_base::GoogleFaviconServerRequestStatus status) {
293 if (status != favicon_base::GoogleFaviconServerRequestStatus::SUCCESS) {
gaschler42544c12017-08-03 15:43:26294 std::move(callback).Run(gfx::Image());
jkrcal08d79b52017-04-13 14:02:11295 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40296 return;
297 }
298
jkrcal95acdc5a2017-05-19 15:34:37299 GetFaviconFromCache(publisher_url, minimum_size_in_pixel,
gaschler42544c12017-08-03 15:43:26300 desired_size_in_pixel, std::move(callback),
jkrcal95acdc5a2017-05-19 15:34:37301 /*continue_to_google_server=*/false);
jkrcal10004602017-03-29 07:44:28302}
303
vitaliii685fdfaa2016-08-31 11:25:46304void ContentSuggestionsService::ClearHistory(
305 base::Time begin,
306 base::Time end,
307 const base::Callback<bool(const GURL& url)>& filter) {
308 for (const auto& provider : providers_) {
309 provider->ClearHistory(begin, end, filter);
310 }
vitaliii6343b2c2017-01-04 07:57:11311 category_ranker_->ClearHistory(begin, end);
tschumann5829c3412017-01-09 21:45:43312 // This potentially removed personalized data which we shouldn't display
313 // anymore.
314 for (Observer& observer : observers_) {
315 observer.OnFullRefreshRequired();
316 }
vitaliii685fdfaa2016-08-31 11:25:46317}
318
treib7d1d7a52016-08-24 14:04:55319void ContentSuggestionsService::ClearAllCachedSuggestions() {
pke6dbb90af2016-07-08 14:00:46320 suggestions_by_category_.clear();
pke151b5502016-08-09 12:15:13321 for (const auto& category_provider_pair : providers_by_category_) {
treib7d1d7a52016-08-24 14:04:55322 category_provider_pair.second->ClearCachedSuggestions(
pke151b5502016-08-09 12:15:13323 category_provider_pair.first);
vitaliii4408d6c2016-11-21 14:16:24324 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13325 observer.OnNewSuggestions(category_provider_pair.first);
vitaliii4408d6c2016-11-21 14:16:24326 }
pke6dbb90af2016-07-08 14:00:46327 }
pke6dbb90af2016-07-08 14:00:46328}
329
jkrcal928ed3f2016-09-23 18:47:06330void ContentSuggestionsService::ClearCachedSuggestions(Category category) {
pke151b5502016-08-09 12:15:13331 suggestions_by_category_[category].clear();
332 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24333 if (iterator != providers_by_category_.end()) {
treib7d1d7a52016-08-24 14:04:55334 iterator->second->ClearCachedSuggestions(category);
vitaliii4408d6c2016-11-21 14:16:24335 }
pke151b5502016-08-09 12:15:13336}
337
pkede0dd9f2016-08-23 09:18:11338void ContentSuggestionsService::GetDismissedSuggestionsForDebugging(
339 Category category,
gaschlerdf40dcc2017-08-04 14:06:37340 DismissedSuggestionsCallback callback) {
pke151b5502016-08-09 12:15:13341 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24342 if (iterator != providers_by_category_.end()) {
gaschlerdf40dcc2017-08-04 14:06:37343 iterator->second->GetDismissedSuggestionsForDebugging(category,
344 std::move(callback));
vitaliii4408d6c2016-11-21 14:16:24345 } else {
gaschlerdf40dcc2017-08-04 14:06:37346 std::move(callback).Run(std::vector<ContentSuggestion>());
vitaliii4408d6c2016-11-21 14:16:24347 }
pke151b5502016-08-09 12:15:13348}
349
350void ContentSuggestionsService::ClearDismissedSuggestionsForDebugging(
351 Category category) {
352 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24353 if (iterator != providers_by_category_.end()) {
pke151b5502016-08-09 12:15:13354 iterator->second->ClearDismissedSuggestionsForDebugging(category);
vitaliii4408d6c2016-11-21 14:16:24355 }
pke6dbb90af2016-07-08 14:00:46356}
357
pke2646c95b2016-07-25 12:18:44358void ContentSuggestionsService::DismissSuggestion(
treib4bbc54922016-09-28 17:26:44359 const ContentSuggestion::ID& suggestion_id) {
360 if (!providers_by_category_.count(suggestion_id.category())) {
pke2646c95b2016-07-25 12:18:44361 LOG(WARNING) << "Dismissed suggestion " << suggestion_id
treib4bbc54922016-09-28 17:26:44362 << " for unavailable category " << suggestion_id.category();
pke6dbb90af2016-07-08 14:00:46363 return;
364 }
dgnf6500c12017-05-09 17:05:31365
366 metrics::RecordContentSuggestionDismissed();
367
treib4bbc54922016-09-28 17:26:44368 providers_by_category_[suggestion_id.category()]->DismissSuggestion(
369 suggestion_id);
pke6dbb90af2016-07-08 14:00:46370
vitaliii2600e8e02016-12-09 17:23:36371 // Remove the suggestion locally if it is present. A suggestion may be missing
372 // localy e.g. if it was sent to UI through |Fetch| or it has been dismissed
373 // from a different NTP.
374 RemoveSuggestionByID(suggestion_id);
pke6dbb90af2016-07-08 14:00:46375}
376
dgn212feea3b2016-09-16 15:08:20377void ContentSuggestionsService::DismissCategory(Category category) {
378 auto providers_it = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24379 if (providers_it == providers_by_category_.end()) {
dgn212feea3b2016-09-16 15:08:20380 return;
vitaliii4408d6c2016-11-21 14:16:24381 }
dgn212feea3b2016-09-16 15:08:20382
dgnf6500c12017-05-09 17:05:31383 metrics::RecordCategoryDismissed();
384
dgn52914722016-10-18 10:28:42385 ContentSuggestionsProvider* provider = providers_it->second;
386 UnregisterCategory(category, provider);
387
388 dismissed_providers_by_category_[category] = provider;
389 StoreDismissedCategoriesToPrefs();
vitaliii3d9423e2017-01-03 17:08:13390
391 category_ranker_->OnCategoryDismissed(category);
dgn212feea3b2016-09-16 15:08:20392}
393
mvanouwerkerk52783d92016-10-12 11:03:40394void ContentSuggestionsService::RestoreDismissedCategories() {
395 // Make a copy as the original will be modified during iteration.
396 auto dismissed_providers_by_category_copy = dismissed_providers_by_category_;
397 for (const auto& category_provider_pair :
398 dismissed_providers_by_category_copy) {
dgn52914722016-10-18 10:28:42399 RestoreDismissedCategory(category_provider_pair.first);
mvanouwerkerk52783d92016-10-12 11:03:40400 }
dgn52914722016-10-18 10:28:42401 StoreDismissedCategoriesToPrefs();
mvanouwerkerk52783d92016-10-12 11:03:40402 DCHECK(dismissed_providers_by_category_.empty());
403}
404
pke6dbb90af2016-07-08 14:00:46405void ContentSuggestionsService::AddObserver(Observer* observer) {
406 observers_.AddObserver(observer);
407}
408
409void ContentSuggestionsService::RemoveObserver(Observer* observer) {
410 observers_.RemoveObserver(observer);
411}
412
413void ContentSuggestionsService::RegisterProvider(
pke5728f082016-08-03 17:27:35414 std::unique_ptr<ContentSuggestionsProvider> provider) {
415 DCHECK(state_ == State::ENABLED);
pke5728f082016-08-03 17:27:35416 providers_.push_back(std::move(provider));
pke6dbb90af2016-07-08 14:00:46417}
418
tschumann83578aa2016-11-03 13:18:32419void ContentSuggestionsService::Fetch(
420 const Category& category,
421 const std::set<std::string>& known_suggestion_ids,
gaschlerdf40dcc2017-08-04 14:06:37422 FetchDoneCallback callback) {
tschumann83578aa2016-11-03 13:18:32423 auto providers_it = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24424 if (providers_it == providers_by_category_.end()) {
tschumann83578aa2016-11-03 13:18:32425 return;
vitaliii4408d6c2016-11-21 14:16:24426 }
tschumann83578aa2016-11-03 13:18:32427
dgnf6500c12017-05-09 17:05:31428 metrics::RecordFetchAction();
429
gaschlerdf40dcc2017-08-04 14:06:37430 providers_it->second->Fetch(category, known_suggestion_ids,
431 std::move(callback));
tschumann83578aa2016-11-03 13:18:32432}
433
jkrcal093410c2016-12-21 16:13:55434void ContentSuggestionsService::ReloadSuggestions() {
435 for (const auto& provider : providers_) {
436 provider->ReloadSuggestions();
437 }
438}
439
dgn65d6cd82017-04-11 00:36:36440void ContentSuggestionsService::SetRemoteSuggestionsEnabled(bool enabled) {
Nicolas Dossou-gbete02481b22017-07-10 17:15:30441 // TODO(dgn): Rewire if we decide to implement a dedicated prefs page. If not
442 // remove by M62.
443 NOTREACHED();
dgnb8a8a5c2017-03-31 12:35:36444}
445
dgn65d6cd82017-04-11 00:36:36446bool ContentSuggestionsService::AreRemoteSuggestionsEnabled() const {
Nicolas Dossou-gbete18994652017-07-19 16:32:17447 return remote_suggestions_provider_ &&
448 !remote_suggestions_provider_->IsDisabled();
dgnb8a8a5c2017-03-31 12:35:36449}
450
dgn65d6cd82017-04-11 00:36:36451bool ContentSuggestionsService::AreRemoteSuggestionsManaged() const {
Nicolas Dossou-gbete02481b22017-07-10 17:15:30452 // TODO(dgn): Rewire if we decide to implement a dedicated prefs page. If not
453 // remove by M62.
454 NOTREACHED();
455 return false;
dgnb8a8a5c2017-03-31 12:35:36456}
457
dgn65d6cd82017-04-11 00:36:36458bool ContentSuggestionsService::AreRemoteSuggestionsManagedByCustodian() const {
Nicolas Dossou-gbete02481b22017-07-10 17:15:30459 // TODO(dgn): Rewire if we decide to implement a dedicated prefs page. If not
460 // remove by M62.
461 NOTREACHED();
462 return false;
dgnb8a8a5c2017-03-31 12:35:36463}
464
pke6dbb90af2016-07-08 14:00:46465////////////////////////////////////////////////////////////////////////////////
466// Private methods
467
468void ContentSuggestionsService::OnNewSuggestions(
pke4d3a4d62016-08-02 09:06:21469 ContentSuggestionsProvider* provider,
470 Category category,
treib62e819e2016-09-27 11:47:34471 std::vector<ContentSuggestion> suggestions) {
treib534523b2016-10-20 16:19:44472 // Providers shouldn't call this when they're in a non-available state.
473 DCHECK(
474 IsCategoryStatusInitOrAvailable(provider->GetCategoryStatus(category)));
475
dgn52914722016-10-18 10:28:42476 if (TryRegisterProviderForCategory(provider, category)) {
pke4d3a4d62016-08-02 09:06:21477 NotifyCategoryStatusChanged(category);
dgn52914722016-10-18 10:28:42478 } else if (IsCategoryDismissed(category)) {
479 // The category has been registered as a dismissed one. We need to
480 // check if the dismissal can be cleared now that we received new data.
vitaliii4408d6c2016-11-21 14:16:24481 if (suggestions.empty()) {
dgn52914722016-10-18 10:28:42482 return;
vitaliii4408d6c2016-11-21 14:16:24483 }
dgn52914722016-10-18 10:28:42484
485 RestoreDismissedCategory(category);
486 StoreDismissedCategoriesToPrefs();
487
488 NotifyCategoryStatusChanged(category);
489 }
pke3b2e3632016-08-12 12:52:53490
treib39fffa12016-10-14 14:59:10491 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
492 // A provider shouldn't send us suggestions while it's not available.
493 DCHECK(suggestions.empty());
pke3b2e3632016-08-12 12:52:53494 return;
treib39fffa12016-10-14 14:59:10495 }
pke6dbb90af2016-07-08 14:00:46496
treib62e819e2016-09-27 11:47:34497 suggestions_by_category_[category] = std::move(suggestions);
pke6dbb90af2016-07-08 14:00:46498
vitaliii4408d6c2016-11-21 14:16:24499 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13500 observer.OnNewSuggestions(category);
vitaliii4408d6c2016-11-21 14:16:24501 }
pke6dbb90af2016-07-08 14:00:46502}
503
504void ContentSuggestionsService::OnCategoryStatusChanged(
pke4d3a4d62016-08-02 09:06:21505 ContentSuggestionsProvider* provider,
506 Category category,
pke9c5095ac2016-08-01 13:53:12507 CategoryStatus new_status) {
pke4d3a4d62016-08-02 09:06:21508 if (new_status == CategoryStatus::NOT_PROVIDED) {
dgn52914722016-10-18 10:28:42509 UnregisterCategory(category, provider);
pke4d3a4d62016-08-02 09:06:21510 } else {
vitaliii4408d6c2016-11-21 14:16:24511 if (!IsCategoryStatusAvailable(new_status)) {
dgn52914722016-10-18 10:28:42512 suggestions_by_category_.erase(category);
vitaliii4408d6c2016-11-21 14:16:24513 }
dgn52914722016-10-18 10:28:42514 TryRegisterProviderForCategory(provider, category);
pke4d3a4d62016-08-02 09:06:21515 DCHECK_EQ(new_status, provider->GetCategoryStatus(category));
516 }
dgn52914722016-10-18 10:28:42517
vitaliii4408d6c2016-11-21 14:16:24518 if (!IsCategoryDismissed(category)) {
dgn52914722016-10-18 10:28:42519 NotifyCategoryStatusChanged(category);
vitaliii4408d6c2016-11-21 14:16:24520 }
pke6dbb90af2016-07-08 14:00:46521}
522
pke2a48f852016-08-18 13:33:52523void ContentSuggestionsService::OnSuggestionInvalidated(
524 ContentSuggestionsProvider* provider,
treib4bbc54922016-09-28 17:26:44525 const ContentSuggestion::ID& suggestion_id) {
526 RemoveSuggestionByID(suggestion_id);
vitaliii4408d6c2016-11-21 14:16:24527 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13528 observer.OnSuggestionInvalidated(suggestion_id);
vitaliii4408d6c2016-11-21 14:16:24529 }
pke2a48f852016-08-18 13:33:52530}
531
dgnf5708892016-11-22 10:36:22532// SigninManagerBase::Observer implementation
533void ContentSuggestionsService::GoogleSigninSucceeded(
534 const std::string& account_id,
Mihai Sardarescub4b697e2017-07-04 16:41:46535 const std::string& username) {
dgnf5708892016-11-22 10:36:22536 OnSignInStateChanged();
537}
538
539void ContentSuggestionsService::GoogleSignedOut(const std::string& account_id,
540 const std::string& username) {
541 OnSignInStateChanged();
542}
543
vitaliii45941152016-09-05 08:58:13544// history::HistoryServiceObserver implementation.
545void ContentSuggestionsService::OnURLsDeleted(
546 history::HistoryService* history_service,
547 bool all_history,
548 bool expired,
549 const history::URLRows& deleted_rows,
550 const std::set<GURL>& favicon_urls) {
551 // We don't care about expired entries.
vitaliii4408d6c2016-11-21 14:16:24552 if (expired) {
vitaliii45941152016-09-05 08:58:13553 return;
vitaliii4408d6c2016-11-21 14:16:24554 }
vitaliii45941152016-09-05 08:58:13555
vitaliii45941152016-09-05 08:58:13556 if (all_history) {
vitaliii45941152016-09-05 08:58:13557 base::Callback<bool(const GURL& url)> filter =
558 base::Bind([](const GURL& url) { return true; });
tschumann5829c3412017-01-09 21:45:43559 ClearHistory(base::Time(), base::Time::Max(), filter);
vitaliii45941152016-09-05 08:58:13560 } else {
tschumann5829c3412017-01-09 21:45:43561 // If a user deletes a single URL, we don't consider this a clear user
562 // intend to clear our data.
563 // TODO(tschumann): Single URL deletions should be handled on a case-by-case
564 // basis. However this depends on the provider's details and thus cannot be
565 // done here. Introduce a OnURLsDeleted() method on the providers to move
566 // this decision further down.
567 if (deleted_rows.size() < 2) {
vitaliii45941152016-09-05 08:58:13568 return;
vitaliii4408d6c2016-11-21 14:16:24569 }
vitaliii45941152016-09-05 08:58:13570 std::set<GURL> deleted_urls;
571 for (const history::URLRow& row : deleted_rows) {
vitaliii45941152016-09-05 08:58:13572 deleted_urls.insert(row.url());
573 }
sfierae8969bc2017-03-26 18:38:41574 base::Callback<bool(const GURL& url)> filter =
575 base::Bind([](const std::set<GURL>& set,
576 const GURL& url) { return set.count(url) != 0; },
577 deleted_urls);
tschumann5829c3412017-01-09 21:45:43578 // We usually don't have any time-related information (the URLRow objects
579 // usually don't provide a |last_visit()| timestamp. Hence we simply clear
580 // the whole history for the selected URLs.
581 ClearHistory(base::Time(), base::Time::Max(), filter);
vitaliii45941152016-09-05 08:58:13582 }
583}
584
585void ContentSuggestionsService::HistoryServiceBeingDeleted(
586 history::HistoryService* history_service) {
587 history_service_observer_.RemoveAll();
588}
589
dgn52914722016-10-18 10:28:42590bool ContentSuggestionsService::TryRegisterProviderForCategory(
pke4d3a4d62016-08-02 09:06:21591 ContentSuggestionsProvider* provider,
592 Category category) {
593 auto it = providers_by_category_.find(category);
594 if (it != providers_by_category_.end()) {
595 DCHECK_EQ(it->second, provider);
596 return false;
597 }
598
mvanouwerkerk52783d92016-10-12 11:03:40599 auto dismissed_it = dismissed_providers_by_category_.find(category);
600 if (dismissed_it != dismissed_providers_by_category_.end()) {
dgn52914722016-10-18 10:28:42601 // The initialisation of dismissed categories registers them with |nullptr|
602 // for providers, we need to check for that to see if the provider is
603 // already registered or not.
604 if (!dismissed_it->second) {
605 dismissed_it->second = provider;
606 } else {
607 DCHECK_EQ(dismissed_it->second, provider);
608 }
609 return false;
mvanouwerkerk52783d92016-10-12 11:03:40610 }
611
dgn52914722016-10-18 10:28:42612 RegisterCategory(category, provider);
613 return true;
614}
615
616void ContentSuggestionsService::RegisterCategory(
617 Category category,
618 ContentSuggestionsProvider* provider) {
619 DCHECK(!base::ContainsKey(providers_by_category_, category));
620 DCHECK(!IsCategoryDismissed(category));
621
pke4d3a4d62016-08-02 09:06:21622 providers_by_category_[category] = provider;
623 categories_.push_back(category);
pke4d3a4d62016-08-02 09:06:21624 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
625 suggestions_by_category_.insert(
626 std::make_pair(category, std::vector<ContentSuggestion>()));
627 }
dgn52914722016-10-18 10:28:42628}
629
630void ContentSuggestionsService::UnregisterCategory(
631 Category category,
632 ContentSuggestionsProvider* provider) {
633 auto providers_it = providers_by_category_.find(category);
634 if (providers_it == providers_by_category_.end()) {
635 DCHECK(IsCategoryDismissed(category));
636 return;
637 }
638
639 DCHECK_EQ(provider, providers_it->second);
640 providers_by_category_.erase(providers_it);
641 categories_.erase(
642 std::find(categories_.begin(), categories_.end(), category));
643 suggestions_by_category_.erase(category);
pke6dbb90af2016-07-08 14:00:46644}
645
pke2a48f852016-08-18 13:33:52646bool ContentSuggestionsService::RemoveSuggestionByID(
treib4bbc54922016-09-28 17:26:44647 const ContentSuggestion::ID& suggestion_id) {
pke2a48f852016-08-18 13:33:52648 std::vector<ContentSuggestion>* suggestions =
treib4bbc54922016-09-28 17:26:44649 &suggestions_by_category_[suggestion_id.category()];
pke2a48f852016-08-18 13:33:52650 auto position =
651 std::find_if(suggestions->begin(), suggestions->end(),
652 [&suggestion_id](const ContentSuggestion& suggestion) {
653 return suggestion_id == suggestion.id();
654 });
vitaliii4408d6c2016-11-21 14:16:24655 if (position == suggestions->end()) {
pke2a48f852016-08-18 13:33:52656 return false;
vitaliii4408d6c2016-11-21 14:16:24657 }
pke2a48f852016-08-18 13:33:52658 suggestions->erase(position);
treib063e6a62016-08-25 11:34:29659
pke2a48f852016-08-18 13:33:52660 return true;
661}
662
pke9c5095ac2016-08-01 13:53:12663void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) {
vitaliii4408d6c2016-11-21 14:16:24664 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13665 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category));
vitaliii4408d6c2016-11-21 14:16:24666 }
pke6dbb90af2016-07-08 14:00:46667}
668
dgnf5708892016-11-22 10:36:22669void ContentSuggestionsService::OnSignInStateChanged() {
670 // First notify the providers, so they can make the required changes.
671 for (const auto& provider : providers_) {
672 provider->OnSignInStateChanged();
673 }
674
675 // Finally notify the observers so they refresh only after the backend is
676 // ready.
677 for (Observer& observer : observers_) {
678 observer.OnFullRefreshRequired();
679 }
680}
681
dgn52914722016-10-18 10:28:42682bool ContentSuggestionsService::IsCategoryDismissed(Category category) const {
683 return base::ContainsKey(dismissed_providers_by_category_, category);
684}
685
686void ContentSuggestionsService::RestoreDismissedCategory(Category category) {
687 auto dismissed_it = dismissed_providers_by_category_.find(category);
688 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category));
689
690 // Keep the reference to the provider and remove it from the dismissed ones,
691 // because the category registration enforces that it's not dismissed.
692 ContentSuggestionsProvider* provider = dismissed_it->second;
693 dismissed_providers_by_category_.erase(dismissed_it);
694
vitaliii4408d6c2016-11-21 14:16:24695 if (provider) {
dgn52914722016-10-18 10:28:42696 RegisterCategory(category, provider);
vitaliii4408d6c2016-11-21 14:16:24697 }
dgn52914722016-10-18 10:28:42698}
699
700void ContentSuggestionsService::RestoreDismissedCategoriesFromPrefs() {
701 // This must only be called at startup.
702 DCHECK(dismissed_providers_by_category_.empty());
703 DCHECK(providers_by_category_.empty());
704
705 const base::ListValue* list =
706 pref_service_->GetList(prefs::kDismissedCategories);
jdoerriea5676c62017-04-11 18:09:14707 for (const base::Value& entry : *list) {
dgn52914722016-10-18 10:28:42708 int id = 0;
jdoerriea5676c62017-04-11 18:09:14709 if (!entry.GetAsInteger(&id)) {
710 DLOG(WARNING) << "Invalid category pref value: " << entry;
dgn52914722016-10-18 10:28:42711 continue;
712 }
713
714 // When the provider is registered, it will be stored in this map.
vitaliii7456f5a2016-12-19 11:13:25715 dismissed_providers_by_category_[Category::FromIDValue(id)] = nullptr;
dgn52914722016-10-18 10:28:42716 }
717}
718
719void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() {
720 base::ListValue list;
721 for (const auto& category_provider_pair : dismissed_providers_by_category_) {
722 list.AppendInteger(category_provider_pair.first.id());
723 }
724
725 pref_service_->Set(prefs::kDismissedCategories, list);
726}
727
pke6dbb90af2016-07-08 14:00:46728} // namespace ntp_snippets