Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(634)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_provider.cc

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Rebase Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" 5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
6 6
7 #include <algorithm>
8 #include <iterator>
9 #include <utility>
10
11 #include "base/command_line.h"
12 #include "base/feature_list.h"
13 #include "base/location.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/metrics/sparse_histogram.h"
17 #include "base/path_service.h"
18 #include "base/stl_util.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/default_clock.h"
22 #include "base/time/time.h"
23 #include "base/values.h"
24 #include "components/data_use_measurement/core/data_use_user_data.h"
25 #include "components/history/core/browser/history_service.h"
26 #include "components/image_fetcher/image_decoder.h"
27 #include "components/image_fetcher/image_fetcher.h"
28 #include "components/ntp_snippets/category_rankers/category_ranker.h"
29 #include "components/ntp_snippets/features.h"
30 #include "components/ntp_snippets/pref_names.h"
31 #include "components/ntp_snippets/remote/remote_suggestions_database.h"
32 #include "components/ntp_snippets/switches.h"
33 #include "components/ntp_snippets/user_classifier.h"
34 #include "components/prefs/pref_registry_simple.h"
35 #include "components/prefs/pref_service.h"
36 #include "components/variations/variations_associated_data.h"
37 #include "grit/components_strings.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/gfx/image/image.h"
40
41 namespace ntp_snippets { 7 namespace ntp_snippets {
42 8
43 namespace { 9 RemoteSuggestionsProvider::RemoteSuggestionsProvider(Observer* observer)
44 10 : ContentSuggestionsProvider(observer) {}
45 // Number of snippets requested to the server. Consider replacing sparse UMA
46 // histograms with COUNTS() if this number increases beyond 50.
47 const int kMaxSnippetCount = 10;
48
49 // Number of archived snippets we keep around in memory.
50 const int kMaxArchivedSnippetCount = 200;
51
52 // Default values for fetching intervals, fallback and wifi.
53 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0};
54 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0};
55 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0};
56
57 // Variation parameters than can override the default fetching intervals.
58 const char* kFetchingIntervalParamNameRareNtpUser[] = {
59 "fetching_interval_hours-fallback-rare_ntp_user",
60 "fetching_interval_hours-wifi-rare_ntp_user"};
61 const char* kFetchingIntervalParamNameActiveNtpUser[] = {
62 "fetching_interval_hours-fallback-active_ntp_user",
63 "fetching_interval_hours-wifi-active_ntp_user"};
64 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = {
65 "fetching_interval_hours-fallback-active_suggestions_consumer",
66 "fetching_interval_hours-wifi-active_suggestions_consumer"};
67
68 // Keys for storing CategoryContent info in prefs.
69 const char kCategoryContentId[] = "id";
70 const char kCategoryContentTitle[] = "title";
71 const char kCategoryContentProvidedByServer[] = "provided_by_server";
72 const char kCategoryContentAllowFetchingMore[] = "allow_fetching_more";
73
74 // TODO(treib): Remove after M57.
75 const char kDeprecatedSnippetHostsPref[] = "ntp_snippets.hosts";
76
77 base::TimeDelta GetFetchingInterval(bool is_wifi,
78 UserClassifier::UserClass user_class) {
79 double value_hours = 0.0;
80
81 const int index = is_wifi ? 1 : 0;
82 const char* param_name = "";
83 switch (user_class) {
84 case UserClassifier::UserClass::RARE_NTP_USER:
85 value_hours = kDefaultFetchingIntervalRareNtpUser[index];
86 param_name = kFetchingIntervalParamNameRareNtpUser[index];
87 break;
88 case UserClassifier::UserClass::ACTIVE_NTP_USER:
89 value_hours = kDefaultFetchingIntervalActiveNtpUser[index];
90 param_name = kFetchingIntervalParamNameActiveNtpUser[index];
91 break;
92 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
93 value_hours = kDefaultFetchingIntervalActiveSuggestionsConsumer[index];
94 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index];
95 break;
96 }
97
98 // The default value can be overridden by a variation parameter.
99 std::string param_value_str = variations::GetVariationParamValueByFeature(
100 ntp_snippets::kArticleSuggestionsFeature, param_name);
101 if (!param_value_str.empty()) {
102 double param_value_hours = 0.0;
103 if (base::StringToDouble(param_value_str, &param_value_hours)) {
104 value_hours = param_value_hours;
105 } else {
106 LOG(WARNING) << "Invalid value for variation parameter " << param_name;
107 }
108 }
109
110 return base::TimeDelta::FromSecondsD(value_hours * 3600.0);
111 }
112
113 std::unique_ptr<std::vector<std::string>> GetSnippetIDVector(
114 const NTPSnippet::PtrVector& snippets) {
115 auto result = base::MakeUnique<std::vector<std::string>>();
116 for (const auto& snippet : snippets) {
117 result->push_back(snippet->id());
118 }
119 return result;
120 }
121
122 bool HasIntersection(const std::vector<std::string>& a,
123 const std::set<std::string>& b) {
124 for (const std::string& item : a) {
125 if (base::ContainsValue(b, item)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 void EraseByPrimaryID(NTPSnippet::PtrVector* snippets,
133 const std::vector<std::string>& ids) {
134 std::set<std::string> ids_lookup(ids.begin(), ids.end());
135 snippets->erase(
136 std::remove_if(snippets->begin(), snippets->end(),
137 [&ids_lookup](const std::unique_ptr<NTPSnippet>& snippet) {
138 return base::ContainsValue(ids_lookup, snippet->id());
139 }),
140 snippets->end());
141 }
142
143 void EraseMatchingSnippets(NTPSnippet::PtrVector* snippets,
144 const NTPSnippet::PtrVector& compare_against) {
145 std::set<std::string> compare_against_ids;
146 for (const std::unique_ptr<NTPSnippet>& snippet : compare_against) {
147 const std::vector<std::string>& snippet_ids = snippet->GetAllIDs();
148 compare_against_ids.insert(snippet_ids.begin(), snippet_ids.end());
149 }
150 snippets->erase(
151 std::remove_if(
152 snippets->begin(), snippets->end(),
153 [&compare_against_ids](const std::unique_ptr<NTPSnippet>& snippet) {
154 return HasIntersection(snippet->GetAllIDs(), compare_against_ids);
155 }),
156 snippets->end());
157 }
158
159 void RemoveNullPointers(NTPSnippet::PtrVector* snippets) {
160 snippets->erase(
161 std::remove_if(
162 snippets->begin(), snippets->end(),
163 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }),
164 snippets->end());
165 }
166
167 void RemoveIncompleteSnippets(NTPSnippet::PtrVector* snippets) {
168 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
169 switches::kAddIncompleteSnippets)) {
170 return;
171 }
172 int num_snippets = snippets->size();
173 // Remove snippets that do not have all the info we need to display it to
174 // the user.
175 snippets->erase(
176 std::remove_if(snippets->begin(), snippets->end(),
177 [](const std::unique_ptr<NTPSnippet>& snippet) {
178 return !snippet->is_complete();
179 }),
180 snippets->end());
181 int num_snippets_removed = num_snippets - snippets->size();
182 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch",
183 num_snippets_removed > 0);
184 if (num_snippets_removed > 0) {
185 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets",
186 num_snippets_removed);
187 }
188 }
189
190 std::vector<ContentSuggestion> ConvertToContentSuggestions(
191 Category category,
192 const NTPSnippet::PtrVector& snippets) {
193 std::vector<ContentSuggestion> result;
194 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) {
195 // TODO(sfiera): if a snippet is not going to be displayed, move it
196 // directly to content.dismissed on fetch. Otherwise, we might prune
197 // other snippets to get down to kMaxSnippetCount, only to hide one of the
198 // incomplete ones we kept.
199 if (!snippet->is_complete()) {
200 continue;
201 }
202 ContentSuggestion suggestion(category, snippet->id(), snippet->url());
203 suggestion.set_amp_url(snippet->amp_url());
204 suggestion.set_title(base::UTF8ToUTF16(snippet->title()));
205 suggestion.set_snippet_text(base::UTF8ToUTF16(snippet->snippet()));
206 suggestion.set_publish_date(snippet->publish_date());
207 suggestion.set_publisher_name(base::UTF8ToUTF16(snippet->publisher_name()));
208 suggestion.set_score(snippet->score());
209 result.emplace_back(std::move(suggestion));
210 }
211 return result;
212 }
213
214 void CallWithEmptyResults(const FetchDoneCallback& callback,
215 const Status& status) {
216 if (callback.is_null()) {
217 return;
218 }
219 callback.Run(status, std::vector<ContentSuggestion>());
220 }
221
222 } // namespace
223
224 CachedImageFetcher::CachedImageFetcher(
225 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
226 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder,
227 PrefService* pref_service,
228 RemoteSuggestionsDatabase* database)
229 : image_fetcher_(std::move(image_fetcher)),
230 image_decoder_(std::move(image_decoder)),
231 database_(database),
232 thumbnail_requests_throttler_(
233 pref_service,
234 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) {
235 // |image_fetcher_| can be null in tests.
236 if (image_fetcher_) {
237 image_fetcher_->SetImageFetcherDelegate(this);
238 image_fetcher_->SetDataUseServiceName(
239 data_use_measurement::DataUseUserData::NTP_SNIPPETS);
240 }
241 }
242
243 CachedImageFetcher::~CachedImageFetcher() {}
244
245 void CachedImageFetcher::FetchSuggestionImage(
246 const ContentSuggestion::ID& suggestion_id,
247 const GURL& url,
248 const ImageFetchedCallback& callback) {
249 database_->LoadImage(
250 suggestion_id.id_within_category(),
251 base::Bind(&CachedImageFetcher::OnSnippetImageFetchedFromDatabase,
252 base::Unretained(this), callback, suggestion_id, url));
253 }
254
255 // This function gets only called for caching the image data received from the
256 // network. The actual decoding is done in OnSnippetImageDecodedFromDatabase().
257 void CachedImageFetcher::OnImageDataFetched(
258 const std::string& id_within_category,
259 const std::string& image_data) {
260 if (image_data.empty()) {
261 return;
262 }
263 database_->SaveImage(id_within_category, image_data);
264 }
265
266 void CachedImageFetcher::OnImageDecodingDone(
267 const ImageFetchedCallback& callback,
268 const std::string& id_within_category,
269 const gfx::Image& image) {
270 callback.Run(image);
271 }
272
273 void CachedImageFetcher::OnSnippetImageFetchedFromDatabase(
274 const ImageFetchedCallback& callback,
275 const ContentSuggestion::ID& suggestion_id,
276 const GURL& url,
277 std::string data) { // SnippetImageCallback requires nonconst reference.
278 // |image_decoder_| is null in tests.
279 if (image_decoder_ && !data.empty()) {
280 image_decoder_->DecodeImage(
281 data, base::Bind(
282 &CachedImageFetcher::OnSnippetImageDecodedFromDatabase,
283 base::Unretained(this), callback, suggestion_id, url));
284 return;
285 }
286 // Fetching from the DB failed; start a network fetch.
287 FetchSnippetImageFromNetwork(suggestion_id, url, callback);
288 }
289
290 void CachedImageFetcher::OnSnippetImageDecodedFromDatabase(
291 const ImageFetchedCallback& callback,
292 const ContentSuggestion::ID& suggestion_id,
293 const GURL& url,
294 const gfx::Image& image) {
295 if (!image.IsEmpty()) {
296 callback.Run(image);
297 return;
298 }
299 // If decoding the image failed, delete the DB entry.
300 database_->DeleteImage(suggestion_id.id_within_category());
301 FetchSnippetImageFromNetwork(suggestion_id, url, callback);
302 }
303
304 void CachedImageFetcher::FetchSnippetImageFromNetwork(
305 const ContentSuggestion::ID& suggestion_id,
306 const GURL& url,
307 const ImageFetchedCallback& callback) {
308 if (url.is_empty() ||
309 !thumbnail_requests_throttler_.DemandQuotaForRequest(
310 /*interactive_request=*/true)) {
311 // Return an empty image. Directly, this is never synchronous with the
312 // original FetchSuggestionImage() call - an asynchronous database query has
313 // happened in the meantime.
314 callback.Run(gfx::Image());
315 return;
316 }
317
318 image_fetcher_->StartOrQueueNetworkRequest(
319 suggestion_id.id_within_category(), url,
320 base::Bind(&CachedImageFetcher::OnImageDecodingDone,
321 base::Unretained(this), callback));
322 }
323
324 RemoteSuggestionsProvider::RemoteSuggestionsProvider(
325 Observer* observer,
326 PrefService* pref_service,
327 const std::string& application_language_code,
328 CategoryRanker* category_ranker,
329 const UserClassifier* user_classifier,
330 NTPSnippetsScheduler* scheduler,
331 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
332 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
333 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder,
334 std::unique_ptr<RemoteSuggestionsDatabase> database,
335 std::unique_ptr<RemoteSuggestionsStatusService> status_service)
336 : ContentSuggestionsProvider(observer),
337 state_(State::NOT_INITED),
338 pref_service_(pref_service),
339 articles_category_(
340 Category::FromKnownCategory(KnownCategories::ARTICLES)),
341 application_language_code_(application_language_code),
342 category_ranker_(category_ranker),
343 user_classifier_(user_classifier),
344 scheduler_(scheduler),
345 snippets_fetcher_(std::move(snippets_fetcher)),
346 database_(std::move(database)),
347 image_fetcher_(std::move(image_fetcher),
348 std::move(image_decoder),
349 pref_service,
350 database_.get()),
351 status_service_(std::move(status_service)),
352 fetch_when_ready_(false),
353 nuke_when_initialized_(false),
354 clock_(base::MakeUnique<base::DefaultClock>()) {
355 pref_service_->ClearPref(kDeprecatedSnippetHostsPref);
356
357 RestoreCategoriesFromPrefs();
358 // The articles category always exists. Add it if we didn't get it from prefs.
359 // TODO(treib): Rethink this.
360 category_contents_.insert(
361 std::make_pair(articles_category_,
362 CategoryContent(BuildArticleCategoryInfo(base::nullopt))));
363 // Tell the observer about all the categories.
364 for (const auto& entry : category_contents_) {
365 observer->OnCategoryStatusChanged(this, entry.first, entry.second.status);
366 }
367
368 if (database_->IsErrorState()) {
369 EnterState(State::ERROR_OCCURRED);
370 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR);
371 return;
372 }
373
374 database_->SetErrorCallback(base::Bind(
375 &RemoteSuggestionsProvider::OnDatabaseError, base::Unretained(this)));
376
377 // We transition to other states while finalizing the initialization, when the
378 // database is done loading.
379 database_load_start_ = base::TimeTicks::Now();
380 database_->LoadSnippets(base::Bind(
381 &RemoteSuggestionsProvider::OnDatabaseLoaded, base::Unretained(this)));
382 }
383 11
384 RemoteSuggestionsProvider::~RemoteSuggestionsProvider() = default; 12 RemoteSuggestionsProvider::~RemoteSuggestionsProvider() = default;
385 13
386 // static
387 void RemoteSuggestionsProvider::RegisterProfilePrefs(
388 PrefRegistrySimple* registry) {
389 // TODO(treib): Remove after M57.
390 registry->RegisterListPref(kDeprecatedSnippetHostsPref);
391 registry->RegisterListPref(prefs::kRemoteSuggestionCategories);
392 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0);
393 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback,
394 0);
395 registry->RegisterInt64Pref(prefs::kLastSuccessfulBackgroundFetchTime, 0);
396
397 RemoteSuggestionsStatusService::RegisterProfilePrefs(registry);
398 }
399
400 void RemoteSuggestionsProvider::FetchSnippetsInTheBackground() {
401 FetchSnippets(/*interactive_request=*/false);
402 }
403
404 void RemoteSuggestionsProvider::FetchSnippetsForAllCategories() {
405 // TODO(markusheintz): Investigate whether we can call the Fetch method
406 // instead of the FetchSnippets.
407 FetchSnippets(/*interactive_request=*/true);
408 }
409
410 void RemoteSuggestionsProvider::FetchSnippets(
411 bool interactive_request) {
412 if (!ready()) {
413 fetch_when_ready_ = true;
414 return;
415 }
416
417 MarkEmptyCategoriesAsLoading();
418
419 NTPSnippetsFetcher::Params params = BuildFetchParams();
420 params.interactive_request = interactive_request;
421 snippets_fetcher_->FetchSnippets(
422 params, base::BindOnce(&RemoteSuggestionsProvider::OnFetchFinished,
423 base::Unretained(this), interactive_request));
424 }
425
426 void RemoteSuggestionsProvider::Fetch(
427 const Category& category,
428 const std::set<std::string>& known_suggestion_ids,
429 const FetchDoneCallback& callback) {
430 if (!ready()) {
431 CallWithEmptyResults(callback,
432 Status(StatusCode::TEMPORARY_ERROR,
433 "RemoteSuggestionsProvider is not ready!"));
434 return;
435 }
436 NTPSnippetsFetcher::Params params = BuildFetchParams();
437 params.excluded_ids.insert(known_suggestion_ids.begin(),
438 known_suggestion_ids.end());
439 params.interactive_request = true;
440 params.exclusive_category = category;
441
442 snippets_fetcher_->FetchSnippets(
443 params, base::BindOnce(&RemoteSuggestionsProvider::OnFetchMoreFinished,
444 base::Unretained(this), callback));
445 }
446
447 // Builds default fetcher params.
448 NTPSnippetsFetcher::Params RemoteSuggestionsProvider::BuildFetchParams() const {
449 NTPSnippetsFetcher::Params result;
450 result.language_code = application_language_code_;
451 result.count_to_fetch = kMaxSnippetCount;
452 for (const auto& map_entry : category_contents_) {
453 const CategoryContent& content = map_entry.second;
454 for (const auto& dismissed_snippet : content.dismissed) {
455 result.excluded_ids.insert(dismissed_snippet->id());
456 }
457 }
458 return result;
459 }
460
461 void RemoteSuggestionsProvider::MarkEmptyCategoriesAsLoading() {
462 for (const auto& item : category_contents_) {
463 Category category = item.first;
464 const CategoryContent& content = item.second;
465 if (content.snippets.empty()) {
466 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE_LOADING);
467 }
468 }
469 }
470
471 void RemoteSuggestionsProvider::RescheduleFetching(bool force) {
472 // The scheduler only exists on Android so far, it's null on other platforms.
473 if (!scheduler_) {
474 return;
475 }
476
477 if (ready()) {
478 base::TimeDelta old_interval_wifi = base::TimeDelta::FromInternalValue(
479 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi));
480 base::TimeDelta old_interval_fallback =
481 base::TimeDelta::FromInternalValue(pref_service_->GetInt64(
482 prefs::kSnippetBackgroundFetchingIntervalFallback));
483 UserClassifier::UserClass user_class = user_classifier_->GetUserClass();
484 base::TimeDelta interval_wifi =
485 GetFetchingInterval(/*is_wifi=*/true, user_class);
486 base::TimeDelta interval_fallback =
487 GetFetchingInterval(/*is_wifi=*/false, user_class);
488 if (force || interval_wifi != old_interval_wifi ||
489 interval_fallback != old_interval_fallback) {
490 scheduler_->Schedule(interval_wifi, interval_fallback);
491 pref_service_->SetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi,
492 interval_wifi.ToInternalValue());
493 pref_service_->SetInt64(prefs::kSnippetBackgroundFetchingIntervalFallback,
494 interval_fallback.ToInternalValue());
495 }
496 } else {
497 // If we're NOT_INITED, we don't know whether to schedule or unschedule.
498 // If |force| is false, all is well: We'll reschedule on the next state
499 // change anyway. If it's true, then unschedule here, to make sure that the
500 // next reschedule actually happens.
501 if (state_ != State::NOT_INITED || force) {
502 scheduler_->Unschedule();
503 pref_service_->ClearPref(prefs::kSnippetBackgroundFetchingIntervalWifi);
504 pref_service_->ClearPref(
505 prefs::kSnippetBackgroundFetchingIntervalFallback);
506 }
507 }
508 }
509
510 CategoryStatus RemoteSuggestionsProvider::GetCategoryStatus(Category category) {
511 auto content_it = category_contents_.find(category);
512 DCHECK(content_it != category_contents_.end());
513 return content_it->second.status;
514 }
515
516 CategoryInfo RemoteSuggestionsProvider::GetCategoryInfo(Category category) {
517 auto content_it = category_contents_.find(category);
518 DCHECK(content_it != category_contents_.end());
519 return content_it->second.info;
520 }
521
522 void RemoteSuggestionsProvider::DismissSuggestion(
523 const ContentSuggestion::ID& suggestion_id) {
524 if (!ready()) {
525 return;
526 }
527
528 auto content_it = category_contents_.find(suggestion_id.category());
529 DCHECK(content_it != category_contents_.end());
530 CategoryContent* content = &content_it->second;
531 DismissSuggestionFromCategoryContent(content,
532 suggestion_id.id_within_category());
533 }
534
535 void RemoteSuggestionsProvider::ClearHistory(
536 base::Time begin,
537 base::Time end,
538 const base::Callback<bool(const GURL& url)>& filter) {
539 // Both time range and the filter are ignored and all suggestions are removed,
540 // because it is not known which history entries were used for the suggestions
541 // personalization.
542 if (!ready()) {
543 nuke_when_initialized_ = true;
544 } else {
545 NukeAllSnippets();
546 }
547 }
548
549 void RemoteSuggestionsProvider::ClearCachedSuggestions(Category category) {
550 if (!initialized()) {
551 return;
552 }
553
554 auto content_it = category_contents_.find(category);
555 if (content_it == category_contents_.end()) {
556 return;
557 }
558 CategoryContent* content = &content_it->second;
559 if (content->snippets.empty()) {
560 return;
561 }
562
563 database_->DeleteSnippets(GetSnippetIDVector(content->snippets));
564 database_->DeleteImages(GetSnippetIDVector(content->snippets));
565 content->snippets.clear();
566
567 if (IsCategoryStatusAvailable(content->status)) {
568 NotifyNewSuggestions(category, *content);
569 }
570 }
571
572 void RemoteSuggestionsProvider::OnSignInStateChanged() {
573 // Make sure the status service is registered and we already initialised its
574 // start state.
575 if (!initialized()) {
576 return;
577 }
578
579 status_service_->OnSignInStateChanged();
580 }
581
582 void RemoteSuggestionsProvider::GetDismissedSuggestionsForDebugging(
583 Category category,
584 const DismissedSuggestionsCallback& callback) {
585 auto content_it = category_contents_.find(category);
586 DCHECK(content_it != category_contents_.end());
587 callback.Run(
588 ConvertToContentSuggestions(category, content_it->second.dismissed));
589 }
590
591 void RemoteSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
592 Category category) {
593 auto content_it = category_contents_.find(category);
594 DCHECK(content_it != category_contents_.end());
595 CategoryContent* content = &content_it->second;
596
597 if (!initialized()) {
598 return;
599 }
600
601 if (content->dismissed.empty()) {
602 return;
603 }
604
605 database_->DeleteSnippets(GetSnippetIDVector(content->dismissed));
606 // The image got already deleted when the suggestion was dismissed.
607
608 content->dismissed.clear();
609 }
610
611 // static
612 int RemoteSuggestionsProvider::GetMaxSnippetCountForTesting() {
613 return kMaxSnippetCount;
614 }
615
616 ////////////////////////////////////////////////////////////////////////////////
617 // Private methods
618
619 GURL RemoteSuggestionsProvider::FindSnippetImageUrl(
620 const ContentSuggestion::ID& suggestion_id) const {
621 DCHECK(base::ContainsKey(category_contents_, suggestion_id.category()));
622
623 const CategoryContent& content =
624 category_contents_.at(suggestion_id.category());
625 const NTPSnippet* snippet =
626 content.FindSnippet(suggestion_id.id_within_category());
627 if (!snippet) {
628 return GURL();
629 }
630 return snippet->salient_image_url();
631 }
632
633 void RemoteSuggestionsProvider::OnDatabaseLoaded(
634 NTPSnippet::PtrVector snippets) {
635 if (state_ == State::ERROR_OCCURRED) {
636 return;
637 }
638 DCHECK(state_ == State::NOT_INITED);
639 DCHECK(base::ContainsKey(category_contents_, articles_category_));
640
641 base::TimeDelta database_load_time =
642 base::TimeTicks::Now() - database_load_start_;
643 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime",
644 database_load_time);
645
646 NTPSnippet::PtrVector to_delete;
647 for (std::unique_ptr<NTPSnippet>& snippet : snippets) {
648 Category snippet_category =
649 Category::FromRemoteCategory(snippet->remote_category_id());
650 auto content_it = category_contents_.find(snippet_category);
651 // We should already know about the category.
652 if (content_it == category_contents_.end()) {
653 DLOG(WARNING) << "Loaded a suggestion for unknown category "
654 << snippet_category << " from the DB; deleting";
655 to_delete.emplace_back(std::move(snippet));
656 continue;
657 }
658 CategoryContent* content = &content_it->second;
659 if (snippet->is_dismissed()) {
660 content->dismissed.emplace_back(std::move(snippet));
661 } else {
662 content->snippets.emplace_back(std::move(snippet));
663 }
664 }
665 if (!to_delete.empty()) {
666 database_->DeleteSnippets(GetSnippetIDVector(to_delete));
667 database_->DeleteImages(GetSnippetIDVector(to_delete));
668 }
669
670 // Sort the suggestions in each category.
671 // TODO(treib): Persist the actual order in the DB somehow? crbug.com/654409
672 for (auto& entry : category_contents_) {
673 CategoryContent* content = &entry.second;
674 std::sort(content->snippets.begin(), content->snippets.end(),
675 [](const std::unique_ptr<NTPSnippet>& lhs,
676 const std::unique_ptr<NTPSnippet>& rhs) {
677 return lhs->score() > rhs->score();
678 });
679 }
680
681 // TODO(tschumann): If I move ClearExpiredDismissedSnippets() to the beginning
682 // of the function, it essentially does nothing but tests are still green. Fix
683 // this!
684 ClearExpiredDismissedSnippets();
685 ClearOrphanedImages();
686 FinishInitialization();
687 }
688
689 void RemoteSuggestionsProvider::OnDatabaseError() {
690 EnterState(State::ERROR_OCCURRED);
691 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR);
692 }
693
694 void RemoteSuggestionsProvider::OnFetchMoreFinished(
695 const FetchDoneCallback& fetching_callback,
696 Status status,
697 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) {
698 if (!fetched_categories) {
699 DCHECK(!status.IsSuccess());
700 CallWithEmptyResults(fetching_callback, status);
701 return;
702 }
703 if (fetched_categories->size() != 1u) {
704 LOG(DFATAL) << "Requested one exclusive category but received "
705 << fetched_categories->size() << " categories.";
706 CallWithEmptyResults(fetching_callback,
707 Status(StatusCode::PERMANENT_ERROR,
708 "RemoteSuggestionsProvider received more "
709 "categories than requested."));
710 return;
711 }
712 auto& fetched_category = (*fetched_categories)[0];
713 Category category = fetched_category.category;
714 CategoryContent* existing_content =
715 UpdateCategoryInfo(category, fetched_category.info);
716 SanitizeReceivedSnippets(existing_content->dismissed,
717 &fetched_category.snippets);
718 // We compute the result now before modifying |fetched_category.snippets|.
719 // However, we wait with notifying the caller until the end of the method when
720 // all state is updated.
721 std::vector<ContentSuggestion> result =
722 ConvertToContentSuggestions(category, fetched_category.snippets);
723
724 // Fill up the newly fetched snippets with existing ones, store them, and
725 // notify observers about new data.
726 while (fetched_category.snippets.size() <
727 static_cast<size_t>(kMaxSnippetCount) &&
728 !existing_content->snippets.empty()) {
729 fetched_category.snippets.emplace(
730 fetched_category.snippets.begin(),
731 std::move(existing_content->snippets.back()));
732 existing_content->snippets.pop_back();
733 }
734 std::vector<std::string> to_dismiss =
735 *GetSnippetIDVector(existing_content->snippets);
736 for (const auto& id : to_dismiss) {
737 DismissSuggestionFromCategoryContent(existing_content, id);
738 }
739 DCHECK(existing_content->snippets.empty());
740
741 IntegrateSnippets(existing_content, std::move(fetched_category.snippets));
742
743 // TODO(tschumann): We should properly honor the existing category state,
744 // e.g. to make sure we don't serve results after the sign-out. Revisit this
745 // once the snippets fetcher supports concurrent requests. We can then see if
746 // Nuke should also cancel outstanding requests or we want to check the
747 // status.
748 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE);
749 // Notify callers and observers.
750 fetching_callback.Run(Status::Success(), std::move(result));
751 NotifyNewSuggestions(category, *existing_content);
752 }
753
754 void RemoteSuggestionsProvider::OnFetchFinished(
755 bool interactive_request,
756 Status status,
757 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) {
758 if (!ready()) {
759 // TODO(tschumann): What happens if this was a user-triggered, interactive
760 // request? Is the UI waiting indefinitely now?
761 return;
762 }
763
764 // Record the fetch time of a successfull background fetch.
765 if (!interactive_request && status.IsSuccess()) {
766 pref_service_->SetInt64(prefs::kLastSuccessfulBackgroundFetchTime,
767 clock_->Now().ToInternalValue());
768 }
769
770 // Mark all categories as not provided by the server in the latest fetch. The
771 // ones we got will be marked again below.
772 for (auto& item : category_contents_) {
773 CategoryContent* content = &item.second;
774 content->included_in_last_server_response = false;
775 }
776
777 // Clear up expired dismissed snippets before we use them to filter new ones.
778 ClearExpiredDismissedSnippets();
779
780 // If snippets were fetched successfully, update our |category_contents_| from
781 // each category provided by the server.
782 if (fetched_categories) {
783 // TODO(treib): Reorder |category_contents_| to match the order we received
784 // from the server. crbug.com/653816
785 for (NTPSnippetsFetcher::FetchedCategory& fetched_category :
786 *fetched_categories) {
787 // TODO(tschumann): Remove this histogram once we only talk to the content
788 // suggestions cloud backend.
789 if (fetched_category.category == articles_category_) {
790 UMA_HISTOGRAM_SPARSE_SLOWLY(
791 "NewTabPage.Snippets.NumArticlesFetched",
792 std::min(fetched_category.snippets.size(),
793 static_cast<size_t>(kMaxSnippetCount + 1)));
794 }
795 category_ranker_->AppendCategoryIfNecessary(fetched_category.category);
796 CategoryContent* content =
797 UpdateCategoryInfo(fetched_category.category, fetched_category.info);
798 content->included_in_last_server_response = true;
799 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets);
800 IntegrateSnippets(content, std::move(fetched_category.snippets));
801 }
802 }
803
804 // TODO(tschumann): The snippets fetcher needs to signal errors so that we
805 // know why we received no data. If an error occured, none of the following
806 // should take place.
807
808 // We might have gotten new categories (or updated the titles of existing
809 // ones), so update the pref.
810 StoreCategoriesToPrefs();
811
812 for (const auto& item : category_contents_) {
813 Category category = item.first;
814 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE);
815 // TODO(sfiera): notify only when a category changed above.
816 NotifyNewSuggestions(category, item.second);
817 }
818
819 // TODO(sfiera): equivalent metrics for non-articles.
820 auto content_it = category_contents_.find(articles_category_);
821 DCHECK(content_it != category_contents_.end());
822 const CategoryContent& content = content_it->second;
823 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles",
824 content.snippets.size());
825 if (content.snippets.empty() && !content.dismissed.empty()) {
826 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded",
827 content.dismissed.size());
828 }
829
830 // Reschedule after a successful fetch. This resets all currently scheduled
831 // fetches, to make sure the fallback interval triggers only if no wifi fetch
832 // succeeded, and also that we don't do a background fetch immediately after
833 // a user-initiated one.
834 if (fetched_categories) {
835 RescheduleFetching(true);
836 }
837 }
838
839 void RemoteSuggestionsProvider::ArchiveSnippets(
840 CategoryContent* content,
841 NTPSnippet::PtrVector* to_archive) {
842 // Archive previous snippets - move them at the beginning of the list.
843 content->archived.insert(content->archived.begin(),
844 std::make_move_iterator(to_archive->begin()),
845 std::make_move_iterator(to_archive->end()));
846 to_archive->clear();
847
848 // If there are more archived snippets than we want to keep, delete the
849 // oldest ones by their fetch time (which are always in the back).
850 if (content->archived.size() > kMaxArchivedSnippetCount) {
851 NTPSnippet::PtrVector to_delete(
852 std::make_move_iterator(content->archived.begin() +
853 kMaxArchivedSnippetCount),
854 std::make_move_iterator(content->archived.end()));
855 content->archived.resize(kMaxArchivedSnippetCount);
856 database_->DeleteImages(GetSnippetIDVector(to_delete));
857 }
858 }
859
860 void RemoteSuggestionsProvider::SanitizeReceivedSnippets(
861 const NTPSnippet::PtrVector& dismissed,
862 NTPSnippet::PtrVector* snippets) {
863 DCHECK(ready());
864 EraseMatchingSnippets(snippets, dismissed);
865 RemoveIncompleteSnippets(snippets);
866 }
867
868 void RemoteSuggestionsProvider::IntegrateSnippets(
869 CategoryContent* content,
870 NTPSnippet::PtrVector new_snippets) {
871 DCHECK(ready());
872
873 // Do not touch the current set of snippets if the newly fetched one is empty.
874 // TODO(tschumann): This should go. If we get empty results we should update
875 // accordingly and remove the old one (only of course if this was not received
876 // through a fetch-more).
877 if (new_snippets.empty()) {
878 return;
879 }
880
881 // It's entirely possible that the newly fetched snippets contain articles
882 // that have been present before.
883 // We need to make sure to only delete and archive snippets that don't
884 // appear with the same ID in the new suggestions (it's fine for additional
885 // IDs though).
886 EraseByPrimaryID(&content->snippets, *GetSnippetIDVector(new_snippets));
887 // Do not delete the thumbnail images as they are still handy on open NTPs.
888 database_->DeleteSnippets(GetSnippetIDVector(content->snippets));
889 // Note, that ArchiveSnippets will clear |content->snippets|.
890 ArchiveSnippets(content, &content->snippets);
891
892 database_->SaveSnippets(new_snippets);
893
894 content->snippets = std::move(new_snippets);
895 }
896
897 void RemoteSuggestionsProvider::DismissSuggestionFromCategoryContent(
898 CategoryContent* content,
899 const std::string& id_within_category) {
900 auto it = std::find_if(
901 content->snippets.begin(), content->snippets.end(),
902 [&id_within_category](const std::unique_ptr<NTPSnippet>& snippet) {
903 return snippet->id() == id_within_category;
904 });
905 if (it == content->snippets.end()) {
906 return;
907 }
908
909 (*it)->set_dismissed(true);
910
911 database_->SaveSnippet(**it);
912
913 content->dismissed.push_back(std::move(*it));
914 content->snippets.erase(it);
915 }
916
917 void RemoteSuggestionsProvider::ClearExpiredDismissedSnippets() {
918 std::vector<Category> categories_to_erase;
919
920 const base::Time now = base::Time::Now();
921
922 for (auto& item : category_contents_) {
923 Category category = item.first;
924 CategoryContent* content = &item.second;
925
926 NTPSnippet::PtrVector to_delete;
927 // Move expired dismissed snippets over into |to_delete|.
928 for (std::unique_ptr<NTPSnippet>& snippet : content->dismissed) {
929 if (snippet->expiry_date() <= now) {
930 to_delete.emplace_back(std::move(snippet));
931 }
932 }
933 RemoveNullPointers(&content->dismissed);
934
935 // Delete the images.
936 database_->DeleteImages(GetSnippetIDVector(to_delete));
937 // Delete the removed article suggestions from the DB.
938 database_->DeleteSnippets(GetSnippetIDVector(to_delete));
939
940 if (content->snippets.empty() && content->dismissed.empty() &&
941 category != articles_category_ &&
942 !content->included_in_last_server_response) {
943 categories_to_erase.push_back(category);
944 }
945 }
946
947 for (Category category : categories_to_erase) {
948 UpdateCategoryStatus(category, CategoryStatus::NOT_PROVIDED);
949 category_contents_.erase(category);
950 }
951
952 StoreCategoriesToPrefs();
953 }
954
955 void RemoteSuggestionsProvider::ClearOrphanedImages() {
956 auto alive_snippets = base::MakeUnique<std::set<std::string>>();
957 for (const auto& entry : category_contents_) {
958 const CategoryContent& content = entry.second;
959 for (const auto& snippet_ptr : content.snippets) {
960 alive_snippets->insert(snippet_ptr->id());
961 }
962 for (const auto& snippet_ptr : content.dismissed) {
963 alive_snippets->insert(snippet_ptr->id());
964 }
965 }
966 database_->GarbageCollectImages(std::move(alive_snippets));
967 }
968
969 void RemoteSuggestionsProvider::NukeAllSnippets() {
970 std::vector<Category> categories_to_erase;
971
972 // Empty the ARTICLES category and remove all others, since they may or may
973 // not be personalized.
974 for (const auto& item : category_contents_) {
975 Category category = item.first;
976
977 ClearCachedSuggestions(category);
978 ClearDismissedSuggestionsForDebugging(category);
979
980 UpdateCategoryStatus(category, CategoryStatus::NOT_PROVIDED);
981
982 // Remove the category entirely; it may or may not reappear.
983 if (category != articles_category_) {
984 categories_to_erase.push_back(category);
985 }
986 }
987
988 for (Category category : categories_to_erase) {
989 category_contents_.erase(category);
990 }
991
992 StoreCategoriesToPrefs();
993 }
994
995 void RemoteSuggestionsProvider::FetchSuggestionImage(
996 const ContentSuggestion::ID& suggestion_id,
997 const ImageFetchedCallback& callback) {
998 if (!base::ContainsKey(category_contents_, suggestion_id.category())) {
999 base::ThreadTaskRunnerHandle::Get()->PostTask(
1000 FROM_HERE, base::Bind(callback, gfx::Image()));
1001 return;
1002 }
1003 GURL image_url = FindSnippetImageUrl(suggestion_id);
1004 if (image_url.is_empty()) {
1005 // As we don't know the corresponding snippet anymore, we don't expect to
1006 // find it in the database (and also can't fetch it remotely). Cut the
1007 // lookup short and return directly.
1008 base::ThreadTaskRunnerHandle::Get()->PostTask(
1009 FROM_HERE, base::Bind(callback, gfx::Image()));
1010 return;
1011 }
1012 image_fetcher_.FetchSuggestionImage(suggestion_id, image_url, callback);
1013 }
1014
1015 void RemoteSuggestionsProvider::EnterStateReady() {
1016 if (nuke_when_initialized_) {
1017 NukeAllSnippets();
1018 nuke_when_initialized_ = false;
1019 }
1020
1021 auto article_category_it = category_contents_.find(articles_category_);
1022 DCHECK(article_category_it != category_contents_.end());
1023 if (article_category_it->second.snippets.empty() || fetch_when_ready_) {
1024 // TODO(jkrcal): Fetching snippets automatically upon creation of this
1025 // lazily created service can cause troubles, e.g. in unit tests where
1026 // network I/O is not allowed.
1027 // Either add a DCHECK here that we actually are allowed to do network I/O
1028 // or change the logic so that some explicit call is always needed for the
1029 // network request.
1030 FetchSnippets(/*interactive_request=*/false);
1031 fetch_when_ready_ = false;
1032 }
1033
1034 for (const auto& item : category_contents_) {
1035 Category category = item.first;
1036 const CategoryContent& content = item.second;
1037 // FetchSnippets has set the status to |AVAILABLE_LOADING| if relevant,
1038 // otherwise we transition to |AVAILABLE| here.
1039 if (content.status != CategoryStatus::AVAILABLE_LOADING) {
1040 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE);
1041 }
1042 }
1043 }
1044
1045 void RemoteSuggestionsProvider::EnterStateDisabled() {
1046 NukeAllSnippets();
1047 }
1048
1049 void RemoteSuggestionsProvider::EnterStateError() {
1050 status_service_.reset();
1051 }
1052
1053 void RemoteSuggestionsProvider::FinishInitialization() {
1054 if (nuke_when_initialized_) {
1055 // We nuke here in addition to EnterStateReady, so that it happens even if
1056 // we enter the DISABLED state below.
1057 NukeAllSnippets();
1058 nuke_when_initialized_ = false;
1059 }
1060
1061 // Note: Initializing the status service will run the callback right away with
1062 // the current state.
1063 status_service_->Init(base::Bind(&RemoteSuggestionsProvider::OnStatusChanged,
1064 base::Unretained(this)));
1065
1066 // Always notify here even if we got nothing from the database, because we
1067 // don't know how long the fetch will take or if it will even complete.
1068 for (const auto& item : category_contents_) {
1069 Category category = item.first;
1070 const CategoryContent& content = item.second;
1071 // Note: We might be in a non-available status here, e.g. DISABLED due to
1072 // enterprise policy.
1073 if (IsCategoryStatusAvailable(content.status)) {
1074 NotifyNewSuggestions(category, content);
1075 }
1076 }
1077 }
1078
1079 void RemoteSuggestionsProvider::OnStatusChanged(
1080 RemoteSuggestionsStatus old_status,
1081 RemoteSuggestionsStatus new_status) {
1082 switch (new_status) {
1083 case RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN:
1084 if (old_status == RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT) {
1085 DCHECK(state_ == State::READY);
1086 // Clear nonpersonalized suggestions.
1087 NukeAllSnippets();
1088 // Fetch personalized ones.
1089 FetchSnippets(/*interactive_request=*/true);
1090 } else {
1091 // Do not change the status. That will be done in EnterStateReady().
1092 EnterState(State::READY);
1093 }
1094 break;
1095
1096 case RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT:
1097 if (old_status == RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN) {
1098 DCHECK(state_ == State::READY);
1099 // Clear personalized suggestions.
1100 NukeAllSnippets();
1101 // Fetch nonpersonalized ones.
1102 FetchSnippets(/*interactive_request=*/true);
1103 } else {
1104 // Do not change the status. That will be done in EnterStateReady().
1105 EnterState(State::READY);
1106 }
1107 break;
1108
1109 case RemoteSuggestionsStatus::EXPLICITLY_DISABLED:
1110 EnterState(State::DISABLED);
1111 UpdateAllCategoryStatus(CategoryStatus::CATEGORY_EXPLICITLY_DISABLED);
1112 break;
1113
1114 case RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED:
1115 EnterState(State::DISABLED);
1116 UpdateAllCategoryStatus(CategoryStatus::SIGNED_OUT);
1117 break;
1118 }
1119 }
1120
1121 void RemoteSuggestionsProvider::EnterState(State state) {
1122 if (state == state_) {
1123 return;
1124 }
1125
1126 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.EnteredState",
1127 static_cast<int>(state),
1128 static_cast<int>(State::COUNT));
1129
1130 switch (state) {
1131 case State::NOT_INITED:
1132 // Initial state, it should not be possible to get back there.
1133 NOTREACHED();
1134 break;
1135
1136 case State::READY:
1137 DCHECK(state_ == State::NOT_INITED || state_ == State::DISABLED);
1138
1139 DVLOG(1) << "Entering state: READY";
1140 state_ = State::READY;
1141 EnterStateReady();
1142 break;
1143
1144 case State::DISABLED:
1145 DCHECK(state_ == State::NOT_INITED || state_ == State::READY);
1146
1147 DVLOG(1) << "Entering state: DISABLED";
1148 state_ = State::DISABLED;
1149 EnterStateDisabled();
1150 break;
1151
1152 case State::ERROR_OCCURRED:
1153 DVLOG(1) << "Entering state: ERROR_OCCURRED";
1154 state_ = State::ERROR_OCCURRED;
1155 EnterStateError();
1156 break;
1157
1158 case State::COUNT:
1159 NOTREACHED();
1160 break;
1161 }
1162
1163 // Schedule or un-schedule background fetching after each state change.
1164 RescheduleFetching(false);
1165 }
1166
1167 void RemoteSuggestionsProvider::NotifyNewSuggestions(
1168 Category category,
1169 const CategoryContent& content) {
1170 DCHECK(IsCategoryStatusAvailable(content.status));
1171
1172 std::vector<ContentSuggestion> result =
1173 ConvertToContentSuggestions(category, content.snippets);
1174
1175 DVLOG(1) << "NotifyNewSuggestions(): " << result.size()
1176 << " items in category " << category;
1177 observer()->OnNewSuggestions(this, category, std::move(result));
1178 }
1179
1180 void RemoteSuggestionsProvider::UpdateCategoryStatus(Category category,
1181 CategoryStatus status) {
1182 auto content_it = category_contents_.find(category);
1183 DCHECK(content_it != category_contents_.end());
1184 CategoryContent& content = content_it->second;
1185
1186 if (status == content.status) {
1187 return;
1188 }
1189
1190 DVLOG(1) << "UpdateCategoryStatus(): " << category.id() << ": "
1191 << static_cast<int>(content.status) << " -> "
1192 << static_cast<int>(status);
1193 content.status = status;
1194 observer()->OnCategoryStatusChanged(this, category, content.status);
1195 }
1196
1197 void RemoteSuggestionsProvider::UpdateAllCategoryStatus(CategoryStatus status) {
1198 for (const auto& category : category_contents_) {
1199 UpdateCategoryStatus(category.first, status);
1200 }
1201 }
1202
1203 namespace {
1204
1205 template <typename T>
1206 typename T::const_iterator FindSnippetInContainer(
1207 const T& container,
1208 const std::string& id_within_category) {
1209 return std::find_if(
1210 container.begin(), container.end(),
1211 [&id_within_category](const std::unique_ptr<NTPSnippet>& snippet) {
1212 return snippet->id() == id_within_category;
1213 });
1214 }
1215
1216 } // namespace
1217
1218 const NTPSnippet* RemoteSuggestionsProvider::CategoryContent::FindSnippet(
1219 const std::string& id_within_category) const {
1220 // Search for the snippet in current and archived snippets.
1221 auto it = FindSnippetInContainer(snippets, id_within_category);
1222 if (it != snippets.end()) {
1223 return it->get();
1224 }
1225 auto archived_it = FindSnippetInContainer(archived, id_within_category);
1226 if (archived_it != archived.end()) {
1227 return archived_it->get();
1228 }
1229 auto dismissed_it = FindSnippetInContainer(dismissed, id_within_category);
1230 if (dismissed_it != dismissed.end()) {
1231 return dismissed_it->get();
1232 }
1233 return nullptr;
1234 }
1235
1236 RemoteSuggestionsProvider::CategoryContent*
1237 RemoteSuggestionsProvider::UpdateCategoryInfo(Category category,
1238 const CategoryInfo& info) {
1239 auto content_it = category_contents_.find(category);
1240 if (content_it == category_contents_.end()) {
1241 content_it = category_contents_
1242 .insert(std::make_pair(category, CategoryContent(info)))
1243 .first;
1244 } else {
1245 content_it->second.info = info;
1246 }
1247 return &content_it->second;
1248 }
1249
1250 void RemoteSuggestionsProvider::RestoreCategoriesFromPrefs() {
1251 // This must only be called at startup, before there are any categories.
1252 DCHECK(category_contents_.empty());
1253
1254 const base::ListValue* list =
1255 pref_service_->GetList(prefs::kRemoteSuggestionCategories);
1256 for (const std::unique_ptr<base::Value>& entry : *list) {
1257 const base::DictionaryValue* dict = nullptr;
1258 if (!entry->GetAsDictionary(&dict)) {
1259 DLOG(WARNING) << "Invalid category pref value: " << *entry;
1260 continue;
1261 }
1262 int id = 0;
1263 if (!dict->GetInteger(kCategoryContentId, &id)) {
1264 DLOG(WARNING) << "Invalid category pref value, missing '"
1265 << kCategoryContentId << "': " << *entry;
1266 continue;
1267 }
1268 base::string16 title;
1269 if (!dict->GetString(kCategoryContentTitle, &title)) {
1270 DLOG(WARNING) << "Invalid category pref value, missing '"
1271 << kCategoryContentTitle << "': " << *entry;
1272 continue;
1273 }
1274 bool included_in_last_server_response = false;
1275 if (!dict->GetBoolean(kCategoryContentProvidedByServer,
1276 &included_in_last_server_response)) {
1277 DLOG(WARNING) << "Invalid category pref value, missing '"
1278 << kCategoryContentProvidedByServer << "': " << *entry;
1279 continue;
1280 }
1281 bool allow_fetching_more_results = false;
1282 // This wasn't always around, so it's okay if it's missing.
1283 dict->GetBoolean(kCategoryContentAllowFetchingMore,
1284 &allow_fetching_more_results);
1285
1286 Category category = Category::FromIDValue(id);
1287 // The ranker may not persist the order of remote categories.
1288 category_ranker_->AppendCategoryIfNecessary(category);
1289 // TODO(tschumann): The following has a bad smell that category
1290 // serialization / deserialization should not be done inside this
1291 // class. We should move that into a central place that also knows how to
1292 // parse data we received from remote backends.
1293 CategoryInfo info =
1294 category == articles_category_
1295 ? BuildArticleCategoryInfo(title)
1296 : BuildRemoteCategoryInfo(title, allow_fetching_more_results);
1297 CategoryContent* content = UpdateCategoryInfo(category, info);
1298 content->included_in_last_server_response =
1299 included_in_last_server_response;
1300 }
1301 }
1302
1303 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() {
1304 // Collect all the CategoryContents.
1305 std::vector<std::pair<Category, const CategoryContent*>> to_store;
1306 for (const auto& entry : category_contents_) {
1307 to_store.emplace_back(entry.first, &entry.second);
1308 }
1309 // The ranker may not persist the order, thus, it is stored by the provider.
1310 std::sort(to_store.begin(), to_store.end(),
1311 [this](const std::pair<Category, const CategoryContent*>& left,
1312 const std::pair<Category, const CategoryContent*>& right) {
1313 return category_ranker_->Compare(left.first, right.first);
1314 });
1315 // Convert the relevant info into a base::ListValue for storage.
1316 base::ListValue list;
1317 for (const auto& entry : to_store) {
1318 const Category& category = entry.first;
1319 const CategoryContent& content = *entry.second;
1320 auto dict = base::MakeUnique<base::DictionaryValue>();
1321 dict->SetInteger(kCategoryContentId, category.id());
1322 // TODO(tschumann): Persist other properties of the CategoryInfo.
1323 dict->SetString(kCategoryContentTitle, content.info.title());
1324 dict->SetBoolean(kCategoryContentProvidedByServer,
1325 content.included_in_last_server_response);
1326 dict->SetBoolean(kCategoryContentAllowFetchingMore,
1327 content.info.has_more_action());
1328 list.Append(std::move(dict));
1329 }
1330 // Finally, store the result in the pref service.
1331 pref_service_->Set(prefs::kRemoteSuggestionCategories, list);
1332 }
1333
1334 RemoteSuggestionsProvider::CategoryContent::CategoryContent(
1335 const CategoryInfo& info)
1336 : info(info) {}
1337
1338 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) =
1339 default;
1340
1341 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default;
1342
1343 RemoteSuggestionsProvider::CategoryContent&
1344 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) =
1345 default;
1346
1347 } // namespace ntp_snippets 14 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698