blob: 4fa090c38e74fafb0f2dfc574cde83abf12bbee5 [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,
pke6dbb90af2016-07-08 14:00:46148 const 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(
pkef29505d2016-08-26 14:46:34153 FROM_HERE, base::Bind(callback, gfx::Image()));
pke6dbb90af2016-07-08 14:00:46154 return;
155 }
treib4bbc54922016-09-28 17:26:44156 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage(
157 suggestion_id, 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,
165 const 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(
169 FROM_HERE, base::Bind(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,
175 desired_size_in_pixel, callback,
176 /*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,
206 const ImageFetchedCallback& callback,
207 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,
Jan Krcalb0cbad92017-06-01 09:03:47218 desired_size_in_pixel, callback, continue_to_google_server),
jkrcal95acdc5a2017-05-19 15:34:37219 &favicons_task_tracker_);
220}
221
jkrcal7b7e71f12017-04-06 13:12:40222void ContentSuggestionsService::OnGetFaviconFromCacheFinished(
223 const GURL& publisher_url,
224 int minimum_size_in_pixel,
225 int desired_size_in_pixel,
226 const ImageFetchedCallback& callback,
227 bool continue_to_google_server,
228 const favicon_base::LargeIconImageResult& result) {
229 if (!result.image.IsEmpty()) {
230 callback.Run(result.image);
jkrcal08d79b52017-04-13 14:02:11231 // The icon is from cache if we haven't gone to Google server yet. The icon
232 // is freshly fetched, otherwise.
233 RecordFaviconFetchResult(continue_to_google_server
234 ? FaviconFetchResult::SUCCESS_CACHED
235 : FaviconFetchResult::SUCCESS_FETCHED);
Jan Krcal5ade20c2017-07-11 13:03:00236 // Update the time when the icon was last requested - postpone thus the
237 // automatic eviction of the favicon from the favicon database.
238 large_icon_service_->TouchIconFromGoogleServer(result.icon_url);
jkrcal7b7e71f12017-04-06 13:12:40239 return;
240 }
241
242 if (!continue_to_google_server ||
243 (result.fallback_icon_style &&
244 !result.fallback_icon_style->is_default_background_color)) {
245 // We cannot download from the server if there is some small icon in the
jkrcal08d79b52017-04-13 14:02:11246 // cache (resulting in non-default background color) or if we already did
247 // so.
jkrcal7b7e71f12017-04-06 13:12:40248 callback.Run(gfx::Image());
jkrcal08d79b52017-04-13 14:02:11249 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40250 return;
251 }
252
253 // Try to fetch the favicon from a Google favicon server.
jkrcal5de6dff2017-05-24 09:59:47254 // TODO(jkrcal): Currently used only for Articles for you which have public
255 // URLs. Let the provider decide whether |publisher_url| may be private or
256 // not.
Ramin Halavati4bf78aa2017-06-01 04:53:45257 net::NetworkTrafficAnnotationTag traffic_annotation =
258 net::DefineNetworkTrafficAnnotation("content_suggestion_get_favicon", R"(
259 semantics {
260 sender: "Content Suggestion"
261 description:
262 "Sends a request to a Google server to retrieve the favicon bitmap "
263 "for an article suggestion on the new tab page (URLs are public "
264 "and provided by Google)."
265 trigger:
266 "A request can be sent if Chrome does not have a favicon for a "
267 "particular page."
268 data: "Page URL and desired icon size."
269 destination: GOOGLE_OWNED_SERVICE
270 }
271 policy {
Ramin Halavati3b979782017-07-21 11:40:26272 cookies_allowed: NO
Ramin Halavati4bf78aa2017-06-01 04:53:45273 setting: "This feature cannot be disabled by settings."
274 policy_exception_justification: "Not implemented."
275 })");
jkrcal7b7e71f12017-04-06 13:12:40276 large_icon_service_
277 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
jkrcalff2ef682017-05-09 07:22:05278 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
Ramin Halavati4bf78aa2017-06-01 04:53:45279 /*may_page_url_be_private=*/false, traffic_annotation,
jkrcal7b7e71f12017-04-06 13:12:40280 base::Bind(
281 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished,
282 base::Unretained(this), publisher_url, minimum_size_in_pixel,
283 desired_size_in_pixel, callback));
284}
285
286void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished(
287 const GURL& publisher_url,
288 int minimum_size_in_pixel,
289 int desired_size_in_pixel,
290 const ImageFetchedCallback& callback,
Jan Krcalf3e5733e2017-07-10 09:06:11291 favicon_base::GoogleFaviconServerRequestStatus status) {
292 if (status != favicon_base::GoogleFaviconServerRequestStatus::SUCCESS) {
jkrcal7b7e71f12017-04-06 13:12:40293 callback.Run(gfx::Image());
jkrcal08d79b52017-04-13 14:02:11294 RecordFaviconFetchResult(FaviconFetchResult::FAILURE);
jkrcal7b7e71f12017-04-06 13:12:40295 return;
296 }
297
jkrcal95acdc5a2017-05-19 15:34:37298 GetFaviconFromCache(publisher_url, minimum_size_in_pixel,
299 desired_size_in_pixel, callback,
300 /*continue_to_google_server=*/false);
jkrcal10004602017-03-29 07:44:28301}
302
vitaliii685fdfaa2016-08-31 11:25:46303void ContentSuggestionsService::ClearHistory(
304 base::Time begin,
305 base::Time end,
306 const base::Callback<bool(const GURL& url)>& filter) {
307 for (const auto& provider : providers_) {
308 provider->ClearHistory(begin, end, filter);
309 }
vitaliii6343b2c2017-01-04 07:57:11310 category_ranker_->ClearHistory(begin, end);
tschumann5829c3412017-01-09 21:45:43311 // This potentially removed personalized data which we shouldn't display
312 // anymore.
313 for (Observer& observer : observers_) {
314 observer.OnFullRefreshRequired();
315 }
vitaliii685fdfaa2016-08-31 11:25:46316}
317
treib7d1d7a52016-08-24 14:04:55318void ContentSuggestionsService::ClearAllCachedSuggestions() {
pke6dbb90af2016-07-08 14:00:46319 suggestions_by_category_.clear();
pke151b5502016-08-09 12:15:13320 for (const auto& category_provider_pair : providers_by_category_) {
treib7d1d7a52016-08-24 14:04:55321 category_provider_pair.second->ClearCachedSuggestions(
pke151b5502016-08-09 12:15:13322 category_provider_pair.first);
vitaliii4408d6c2016-11-21 14:16:24323 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13324 observer.OnNewSuggestions(category_provider_pair.first);
vitaliii4408d6c2016-11-21 14:16:24325 }
pke6dbb90af2016-07-08 14:00:46326 }
pke6dbb90af2016-07-08 14:00:46327}
328
jkrcal928ed3f2016-09-23 18:47:06329void ContentSuggestionsService::ClearCachedSuggestions(Category category) {
pke151b5502016-08-09 12:15:13330 suggestions_by_category_[category].clear();
331 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24332 if (iterator != providers_by_category_.end()) {
treib7d1d7a52016-08-24 14:04:55333 iterator->second->ClearCachedSuggestions(category);
vitaliii4408d6c2016-11-21 14:16:24334 }
pke151b5502016-08-09 12:15:13335}
336
pkede0dd9f2016-08-23 09:18:11337void ContentSuggestionsService::GetDismissedSuggestionsForDebugging(
338 Category category,
339 const DismissedSuggestionsCallback& callback) {
pke151b5502016-08-09 12:15:13340 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24341 if (iterator != providers_by_category_.end()) {
pkede0dd9f2016-08-23 09:18:11342 iterator->second->GetDismissedSuggestionsForDebugging(category, callback);
vitaliii4408d6c2016-11-21 14:16:24343 } else {
pkede0dd9f2016-08-23 09:18:11344 callback.Run(std::vector<ContentSuggestion>());
vitaliii4408d6c2016-11-21 14:16:24345 }
pke151b5502016-08-09 12:15:13346}
347
348void ContentSuggestionsService::ClearDismissedSuggestionsForDebugging(
349 Category category) {
350 auto iterator = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24351 if (iterator != providers_by_category_.end()) {
pke151b5502016-08-09 12:15:13352 iterator->second->ClearDismissedSuggestionsForDebugging(category);
vitaliii4408d6c2016-11-21 14:16:24353 }
pke6dbb90af2016-07-08 14:00:46354}
355
pke2646c95b2016-07-25 12:18:44356void ContentSuggestionsService::DismissSuggestion(
treib4bbc54922016-09-28 17:26:44357 const ContentSuggestion::ID& suggestion_id) {
358 if (!providers_by_category_.count(suggestion_id.category())) {
pke2646c95b2016-07-25 12:18:44359 LOG(WARNING) << "Dismissed suggestion " << suggestion_id
treib4bbc54922016-09-28 17:26:44360 << " for unavailable category " << suggestion_id.category();
pke6dbb90af2016-07-08 14:00:46361 return;
362 }
dgnf6500c12017-05-09 17:05:31363
364 metrics::RecordContentSuggestionDismissed();
365
treib4bbc54922016-09-28 17:26:44366 providers_by_category_[suggestion_id.category()]->DismissSuggestion(
367 suggestion_id);
pke6dbb90af2016-07-08 14:00:46368
vitaliii2600e8e02016-12-09 17:23:36369 // Remove the suggestion locally if it is present. A suggestion may be missing
370 // localy e.g. if it was sent to UI through |Fetch| or it has been dismissed
371 // from a different NTP.
372 RemoveSuggestionByID(suggestion_id);
pke6dbb90af2016-07-08 14:00:46373}
374
dgn212feea3b2016-09-16 15:08:20375void ContentSuggestionsService::DismissCategory(Category category) {
376 auto providers_it = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24377 if (providers_it == providers_by_category_.end()) {
dgn212feea3b2016-09-16 15:08:20378 return;
vitaliii4408d6c2016-11-21 14:16:24379 }
dgn212feea3b2016-09-16 15:08:20380
dgnf6500c12017-05-09 17:05:31381 metrics::RecordCategoryDismissed();
382
dgn52914722016-10-18 10:28:42383 ContentSuggestionsProvider* provider = providers_it->second;
384 UnregisterCategory(category, provider);
385
386 dismissed_providers_by_category_[category] = provider;
387 StoreDismissedCategoriesToPrefs();
vitaliii3d9423e2017-01-03 17:08:13388
389 category_ranker_->OnCategoryDismissed(category);
dgn212feea3b2016-09-16 15:08:20390}
391
mvanouwerkerk52783d92016-10-12 11:03:40392void ContentSuggestionsService::RestoreDismissedCategories() {
393 // Make a copy as the original will be modified during iteration.
394 auto dismissed_providers_by_category_copy = dismissed_providers_by_category_;
395 for (const auto& category_provider_pair :
396 dismissed_providers_by_category_copy) {
dgn52914722016-10-18 10:28:42397 RestoreDismissedCategory(category_provider_pair.first);
mvanouwerkerk52783d92016-10-12 11:03:40398 }
dgn52914722016-10-18 10:28:42399 StoreDismissedCategoriesToPrefs();
mvanouwerkerk52783d92016-10-12 11:03:40400 DCHECK(dismissed_providers_by_category_.empty());
401}
402
pke6dbb90af2016-07-08 14:00:46403void ContentSuggestionsService::AddObserver(Observer* observer) {
404 observers_.AddObserver(observer);
405}
406
407void ContentSuggestionsService::RemoveObserver(Observer* observer) {
408 observers_.RemoveObserver(observer);
409}
410
411void ContentSuggestionsService::RegisterProvider(
pke5728f082016-08-03 17:27:35412 std::unique_ptr<ContentSuggestionsProvider> provider) {
413 DCHECK(state_ == State::ENABLED);
pke5728f082016-08-03 17:27:35414 providers_.push_back(std::move(provider));
pke6dbb90af2016-07-08 14:00:46415}
416
tschumann83578aa2016-11-03 13:18:32417void ContentSuggestionsService::Fetch(
418 const Category& category,
419 const std::set<std::string>& known_suggestion_ids,
fhorschigd24046c42016-11-09 11:26:25420 const FetchDoneCallback& callback) {
tschumann83578aa2016-11-03 13:18:32421 auto providers_it = providers_by_category_.find(category);
vitaliii4408d6c2016-11-21 14:16:24422 if (providers_it == providers_by_category_.end()) {
tschumann83578aa2016-11-03 13:18:32423 return;
vitaliii4408d6c2016-11-21 14:16:24424 }
tschumann83578aa2016-11-03 13:18:32425
dgnf6500c12017-05-09 17:05:31426 metrics::RecordFetchAction();
427
tschumann83578aa2016-11-03 13:18:32428 providers_it->second->Fetch(category, known_suggestion_ids, callback);
429}
430
jkrcal093410c2016-12-21 16:13:55431void ContentSuggestionsService::ReloadSuggestions() {
432 for (const auto& provider : providers_) {
433 provider->ReloadSuggestions();
434 }
435}
436
dgn65d6cd82017-04-11 00:36:36437void ContentSuggestionsService::SetRemoteSuggestionsEnabled(bool enabled) {
Nicolas Dossou-gbete02481b22017-07-10 17:15:30438 // TODO(dgn): Rewire if we decide to implement a dedicated prefs page. If not
439 // remove by M62.
440 NOTREACHED();
dgnb8a8a5c2017-03-31 12:35:36441}
442
dgn65d6cd82017-04-11 00:36:36443bool ContentSuggestionsService::AreRemoteSuggestionsEnabled() const {
Nicolas Dossou-gbete18994652017-07-19 16:32:17444 return remote_suggestions_provider_ &&
445 !remote_suggestions_provider_->IsDisabled();
dgnb8a8a5c2017-03-31 12:35:36446}
447
dgn65d6cd82017-04-11 00:36:36448bool ContentSuggestionsService::AreRemoteSuggestionsManaged() const {
Nicolas Dossou-gbete02481b22017-07-10 17:15:30449 // TODO(dgn): Rewire if we decide to implement a dedicated prefs page. If not
450 // remove by M62.
451 NOTREACHED();
452 return false;
dgnb8a8a5c2017-03-31 12:35:36453}
454
dgn65d6cd82017-04-11 00:36:36455bool ContentSuggestionsService::AreRemoteSuggestionsManagedByCustodian() const {
Nicolas Dossou-gbete02481b22017-07-10 17:15:30456 // TODO(dgn): Rewire if we decide to implement a dedicated prefs page. If not
457 // remove by M62.
458 NOTREACHED();
459 return false;
dgnb8a8a5c2017-03-31 12:35:36460}
461
pke6dbb90af2016-07-08 14:00:46462////////////////////////////////////////////////////////////////////////////////
463// Private methods
464
465void ContentSuggestionsService::OnNewSuggestions(
pke4d3a4d62016-08-02 09:06:21466 ContentSuggestionsProvider* provider,
467 Category category,
treib62e819e2016-09-27 11:47:34468 std::vector<ContentSuggestion> suggestions) {
treib534523b2016-10-20 16:19:44469 // Providers shouldn't call this when they're in a non-available state.
470 DCHECK(
471 IsCategoryStatusInitOrAvailable(provider->GetCategoryStatus(category)));
472
dgn52914722016-10-18 10:28:42473 if (TryRegisterProviderForCategory(provider, category)) {
pke4d3a4d62016-08-02 09:06:21474 NotifyCategoryStatusChanged(category);
dgn52914722016-10-18 10:28:42475 } else if (IsCategoryDismissed(category)) {
476 // The category has been registered as a dismissed one. We need to
477 // check if the dismissal can be cleared now that we received new data.
vitaliii4408d6c2016-11-21 14:16:24478 if (suggestions.empty()) {
dgn52914722016-10-18 10:28:42479 return;
vitaliii4408d6c2016-11-21 14:16:24480 }
dgn52914722016-10-18 10:28:42481
482 RestoreDismissedCategory(category);
483 StoreDismissedCategoriesToPrefs();
484
485 NotifyCategoryStatusChanged(category);
486 }
pke3b2e3632016-08-12 12:52:53487
treib39fffa12016-10-14 14:59:10488 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
489 // A provider shouldn't send us suggestions while it's not available.
490 DCHECK(suggestions.empty());
pke3b2e3632016-08-12 12:52:53491 return;
treib39fffa12016-10-14 14:59:10492 }
pke6dbb90af2016-07-08 14:00:46493
treib62e819e2016-09-27 11:47:34494 suggestions_by_category_[category] = std::move(suggestions);
pke6dbb90af2016-07-08 14:00:46495
vitaliii4408d6c2016-11-21 14:16:24496 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13497 observer.OnNewSuggestions(category);
vitaliii4408d6c2016-11-21 14:16:24498 }
pke6dbb90af2016-07-08 14:00:46499}
500
501void ContentSuggestionsService::OnCategoryStatusChanged(
pke4d3a4d62016-08-02 09:06:21502 ContentSuggestionsProvider* provider,
503 Category category,
pke9c5095ac2016-08-01 13:53:12504 CategoryStatus new_status) {
pke4d3a4d62016-08-02 09:06:21505 if (new_status == CategoryStatus::NOT_PROVIDED) {
dgn52914722016-10-18 10:28:42506 UnregisterCategory(category, provider);
pke4d3a4d62016-08-02 09:06:21507 } else {
vitaliii4408d6c2016-11-21 14:16:24508 if (!IsCategoryStatusAvailable(new_status)) {
dgn52914722016-10-18 10:28:42509 suggestions_by_category_.erase(category);
vitaliii4408d6c2016-11-21 14:16:24510 }
dgn52914722016-10-18 10:28:42511 TryRegisterProviderForCategory(provider, category);
pke4d3a4d62016-08-02 09:06:21512 DCHECK_EQ(new_status, provider->GetCategoryStatus(category));
513 }
dgn52914722016-10-18 10:28:42514
vitaliii4408d6c2016-11-21 14:16:24515 if (!IsCategoryDismissed(category)) {
dgn52914722016-10-18 10:28:42516 NotifyCategoryStatusChanged(category);
vitaliii4408d6c2016-11-21 14:16:24517 }
pke6dbb90af2016-07-08 14:00:46518}
519
pke2a48f852016-08-18 13:33:52520void ContentSuggestionsService::OnSuggestionInvalidated(
521 ContentSuggestionsProvider* provider,
treib4bbc54922016-09-28 17:26:44522 const ContentSuggestion::ID& suggestion_id) {
523 RemoveSuggestionByID(suggestion_id);
vitaliii4408d6c2016-11-21 14:16:24524 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13525 observer.OnSuggestionInvalidated(suggestion_id);
vitaliii4408d6c2016-11-21 14:16:24526 }
pke2a48f852016-08-18 13:33:52527}
528
dgnf5708892016-11-22 10:36:22529// SigninManagerBase::Observer implementation
530void ContentSuggestionsService::GoogleSigninSucceeded(
531 const std::string& account_id,
Mihai Sardarescub4b697e2017-07-04 16:41:46532 const std::string& username) {
dgnf5708892016-11-22 10:36:22533 OnSignInStateChanged();
534}
535
536void ContentSuggestionsService::GoogleSignedOut(const std::string& account_id,
537 const std::string& username) {
538 OnSignInStateChanged();
539}
540
vitaliii45941152016-09-05 08:58:13541// history::HistoryServiceObserver implementation.
542void ContentSuggestionsService::OnURLsDeleted(
543 history::HistoryService* history_service,
544 bool all_history,
545 bool expired,
546 const history::URLRows& deleted_rows,
547 const std::set<GURL>& favicon_urls) {
548 // We don't care about expired entries.
vitaliii4408d6c2016-11-21 14:16:24549 if (expired) {
vitaliii45941152016-09-05 08:58:13550 return;
vitaliii4408d6c2016-11-21 14:16:24551 }
vitaliii45941152016-09-05 08:58:13552
vitaliii45941152016-09-05 08:58:13553 if (all_history) {
vitaliii45941152016-09-05 08:58:13554 base::Callback<bool(const GURL& url)> filter =
555 base::Bind([](const GURL& url) { return true; });
tschumann5829c3412017-01-09 21:45:43556 ClearHistory(base::Time(), base::Time::Max(), filter);
vitaliii45941152016-09-05 08:58:13557 } else {
tschumann5829c3412017-01-09 21:45:43558 // If a user deletes a single URL, we don't consider this a clear user
559 // intend to clear our data.
560 // TODO(tschumann): Single URL deletions should be handled on a case-by-case
561 // basis. However this depends on the provider's details and thus cannot be
562 // done here. Introduce a OnURLsDeleted() method on the providers to move
563 // this decision further down.
564 if (deleted_rows.size() < 2) {
vitaliii45941152016-09-05 08:58:13565 return;
vitaliii4408d6c2016-11-21 14:16:24566 }
vitaliii45941152016-09-05 08:58:13567 std::set<GURL> deleted_urls;
568 for (const history::URLRow& row : deleted_rows) {
vitaliii45941152016-09-05 08:58:13569 deleted_urls.insert(row.url());
570 }
sfierae8969bc2017-03-26 18:38:41571 base::Callback<bool(const GURL& url)> filter =
572 base::Bind([](const std::set<GURL>& set,
573 const GURL& url) { return set.count(url) != 0; },
574 deleted_urls);
tschumann5829c3412017-01-09 21:45:43575 // We usually don't have any time-related information (the URLRow objects
576 // usually don't provide a |last_visit()| timestamp. Hence we simply clear
577 // the whole history for the selected URLs.
578 ClearHistory(base::Time(), base::Time::Max(), filter);
vitaliii45941152016-09-05 08:58:13579 }
580}
581
582void ContentSuggestionsService::HistoryServiceBeingDeleted(
583 history::HistoryService* history_service) {
584 history_service_observer_.RemoveAll();
585}
586
dgn52914722016-10-18 10:28:42587bool ContentSuggestionsService::TryRegisterProviderForCategory(
pke4d3a4d62016-08-02 09:06:21588 ContentSuggestionsProvider* provider,
589 Category category) {
590 auto it = providers_by_category_.find(category);
591 if (it != providers_by_category_.end()) {
592 DCHECK_EQ(it->second, provider);
593 return false;
594 }
595
mvanouwerkerk52783d92016-10-12 11:03:40596 auto dismissed_it = dismissed_providers_by_category_.find(category);
597 if (dismissed_it != dismissed_providers_by_category_.end()) {
dgn52914722016-10-18 10:28:42598 // The initialisation of dismissed categories registers them with |nullptr|
599 // for providers, we need to check for that to see if the provider is
600 // already registered or not.
601 if (!dismissed_it->second) {
602 dismissed_it->second = provider;
603 } else {
604 DCHECK_EQ(dismissed_it->second, provider);
605 }
606 return false;
mvanouwerkerk52783d92016-10-12 11:03:40607 }
608
dgn52914722016-10-18 10:28:42609 RegisterCategory(category, provider);
610 return true;
611}
612
613void ContentSuggestionsService::RegisterCategory(
614 Category category,
615 ContentSuggestionsProvider* provider) {
616 DCHECK(!base::ContainsKey(providers_by_category_, category));
617 DCHECK(!IsCategoryDismissed(category));
618
pke4d3a4d62016-08-02 09:06:21619 providers_by_category_[category] = provider;
620 categories_.push_back(category);
pke4d3a4d62016-08-02 09:06:21621 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
622 suggestions_by_category_.insert(
623 std::make_pair(category, std::vector<ContentSuggestion>()));
624 }
dgn52914722016-10-18 10:28:42625}
626
627void ContentSuggestionsService::UnregisterCategory(
628 Category category,
629 ContentSuggestionsProvider* provider) {
630 auto providers_it = providers_by_category_.find(category);
631 if (providers_it == providers_by_category_.end()) {
632 DCHECK(IsCategoryDismissed(category));
633 return;
634 }
635
636 DCHECK_EQ(provider, providers_it->second);
637 providers_by_category_.erase(providers_it);
638 categories_.erase(
639 std::find(categories_.begin(), categories_.end(), category));
640 suggestions_by_category_.erase(category);
pke6dbb90af2016-07-08 14:00:46641}
642
pke2a48f852016-08-18 13:33:52643bool ContentSuggestionsService::RemoveSuggestionByID(
treib4bbc54922016-09-28 17:26:44644 const ContentSuggestion::ID& suggestion_id) {
pke2a48f852016-08-18 13:33:52645 std::vector<ContentSuggestion>* suggestions =
treib4bbc54922016-09-28 17:26:44646 &suggestions_by_category_[suggestion_id.category()];
pke2a48f852016-08-18 13:33:52647 auto position =
648 std::find_if(suggestions->begin(), suggestions->end(),
649 [&suggestion_id](const ContentSuggestion& suggestion) {
650 return suggestion_id == suggestion.id();
651 });
vitaliii4408d6c2016-11-21 14:16:24652 if (position == suggestions->end()) {
pke2a48f852016-08-18 13:33:52653 return false;
vitaliii4408d6c2016-11-21 14:16:24654 }
pke2a48f852016-08-18 13:33:52655 suggestions->erase(position);
treib063e6a62016-08-25 11:34:29656
pke2a48f852016-08-18 13:33:52657 return true;
658}
659
pke9c5095ac2016-08-01 13:53:12660void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) {
vitaliii4408d6c2016-11-21 14:16:24661 for (Observer& observer : observers_) {
ericwilligers42b92c12016-10-24 20:21:13662 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category));
vitaliii4408d6c2016-11-21 14:16:24663 }
pke6dbb90af2016-07-08 14:00:46664}
665
dgnf5708892016-11-22 10:36:22666void ContentSuggestionsService::OnSignInStateChanged() {
667 // First notify the providers, so they can make the required changes.
668 for (const auto& provider : providers_) {
669 provider->OnSignInStateChanged();
670 }
671
672 // Finally notify the observers so they refresh only after the backend is
673 // ready.
674 for (Observer& observer : observers_) {
675 observer.OnFullRefreshRequired();
676 }
677}
678
dgn52914722016-10-18 10:28:42679bool ContentSuggestionsService::IsCategoryDismissed(Category category) const {
680 return base::ContainsKey(dismissed_providers_by_category_, category);
681}
682
683void ContentSuggestionsService::RestoreDismissedCategory(Category category) {
684 auto dismissed_it = dismissed_providers_by_category_.find(category);
685 DCHECK(base::ContainsKey(dismissed_providers_by_category_, category));
686
687 // Keep the reference to the provider and remove it from the dismissed ones,
688 // because the category registration enforces that it's not dismissed.
689 ContentSuggestionsProvider* provider = dismissed_it->second;
690 dismissed_providers_by_category_.erase(dismissed_it);
691
vitaliii4408d6c2016-11-21 14:16:24692 if (provider) {
dgn52914722016-10-18 10:28:42693 RegisterCategory(category, provider);
vitaliii4408d6c2016-11-21 14:16:24694 }
dgn52914722016-10-18 10:28:42695}
696
697void ContentSuggestionsService::RestoreDismissedCategoriesFromPrefs() {
698 // This must only be called at startup.
699 DCHECK(dismissed_providers_by_category_.empty());
700 DCHECK(providers_by_category_.empty());
701
702 const base::ListValue* list =
703 pref_service_->GetList(prefs::kDismissedCategories);
jdoerriea5676c62017-04-11 18:09:14704 for (const base::Value& entry : *list) {
dgn52914722016-10-18 10:28:42705 int id = 0;
jdoerriea5676c62017-04-11 18:09:14706 if (!entry.GetAsInteger(&id)) {
707 DLOG(WARNING) << "Invalid category pref value: " << entry;
dgn52914722016-10-18 10:28:42708 continue;
709 }
710
711 // When the provider is registered, it will be stored in this map.
vitaliii7456f5a2016-12-19 11:13:25712 dismissed_providers_by_category_[Category::FromIDValue(id)] = nullptr;
dgn52914722016-10-18 10:28:42713 }
714}
715
716void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() {
717 base::ListValue list;
718 for (const auto& category_provider_pair : dismissed_providers_by_category_) {
719 list.AppendInteger(category_provider_pair.first.id());
720 }
721
722 pref_service_->Set(prefs::kDismissedCategories, list);
723}
724
pke6dbb90af2016-07-08 14:00:46725} // namespace ntp_snippets