Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
| 22 #include "base/time/default_tick_clock.h" | |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "base/values.h" | 24 #include "base/values.h" |
| 24 #include "components/data_use_measurement/core/data_use_user_data.h" | 25 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 25 #include "components/history/core/browser/history_service.h" | 26 #include "components/history/core/browser/history_service.h" |
| 26 #include "components/image_fetcher/image_decoder.h" | 27 #include "components/image_fetcher/image_decoder.h" |
| 27 #include "components/image_fetcher/image_fetcher.h" | 28 #include "components/image_fetcher/image_fetcher.h" |
| 28 #include "components/ntp_snippets/features.h" | 29 #include "components/ntp_snippets/features.h" |
| 29 #include "components/ntp_snippets/pref_names.h" | 30 #include "components/ntp_snippets/pref_names.h" |
| 30 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 31 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
| 31 #include "components/ntp_snippets/switches.h" | 32 #include "components/ntp_snippets/switches.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 scheduler_(scheduler), | 243 scheduler_(scheduler), |
| 243 snippets_fetcher_(std::move(snippets_fetcher)), | 244 snippets_fetcher_(std::move(snippets_fetcher)), |
| 244 image_fetcher_(std::move(image_fetcher)), | 245 image_fetcher_(std::move(image_fetcher)), |
| 245 image_decoder_(std::move(image_decoder)), | 246 image_decoder_(std::move(image_decoder)), |
| 246 database_(std::move(database)), | 247 database_(std::move(database)), |
| 247 status_service_(std::move(status_service)), | 248 status_service_(std::move(status_service)), |
| 248 fetch_when_ready_(false), | 249 fetch_when_ready_(false), |
| 249 nuke_when_initialized_(false), | 250 nuke_when_initialized_(false), |
| 250 thumbnail_requests_throttler_( | 251 thumbnail_requests_throttler_( |
| 251 pref_service, | 252 pref_service, |
| 252 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { | 253 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL), |
| 254 tick_clock_(new base::DefaultTickClock()) { | |
|
Marc Treib
2016/12/07 14:42:13
nit: base::MakeUnique<base::DefaultTickClock>()
(t
markusheintz_
2016/12/08 09:47:41
Done.
| |
| 253 pref_service_->ClearPref(kDeprecatedSnippetHostsPref); | 255 pref_service_->ClearPref(kDeprecatedSnippetHostsPref); |
| 254 | 256 |
| 255 RestoreCategoriesFromPrefs(); | 257 RestoreCategoriesFromPrefs(); |
| 256 // The articles category always exists. Add it if we didn't get it from prefs. | 258 // The articles category always exists. Add it if we didn't get it from prefs. |
| 257 // TODO(treib): Rethink this. | 259 // TODO(treib): Rethink this. |
| 258 category_contents_.insert( | 260 category_contents_.insert( |
| 259 std::make_pair(articles_category_, | 261 std::make_pair(articles_category_, |
| 260 CategoryContent(BuildArticleCategoryInfo(base::nullopt)))); | 262 CategoryContent(BuildArticleCategoryInfo(base::nullopt)))); |
| 261 // Tell the observer about all the categories. | 263 // Tell the observer about all the categories. |
| 262 for (const auto& entry : category_contents_) { | 264 for (const auto& entry : category_contents_) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 283 | 285 |
| 284 // static | 286 // static |
| 285 void RemoteSuggestionsProvider::RegisterProfilePrefs( | 287 void RemoteSuggestionsProvider::RegisterProfilePrefs( |
| 286 PrefRegistrySimple* registry) { | 288 PrefRegistrySimple* registry) { |
| 287 // TODO(treib): Remove after M57. | 289 // TODO(treib): Remove after M57. |
| 288 registry->RegisterListPref(kDeprecatedSnippetHostsPref); | 290 registry->RegisterListPref(kDeprecatedSnippetHostsPref); |
| 289 registry->RegisterListPref(prefs::kRemoteSuggestionCategories); | 291 registry->RegisterListPref(prefs::kRemoteSuggestionCategories); |
| 290 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0); | 292 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0); |
| 291 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback, | 293 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback, |
| 292 0); | 294 0); |
| 295 registry->RegisterInt64Pref(prefs::kLastSuccessfullBackgroundFetchTime, 0); | |
| 293 | 296 |
| 294 RemoteSuggestionsStatusService::RegisterProfilePrefs(registry); | 297 RemoteSuggestionsStatusService::RegisterProfilePrefs(registry); |
| 295 } | 298 } |
| 296 | 299 |
| 297 void RemoteSuggestionsProvider::FetchSnippetsInTheBackground() { | 300 void RemoteSuggestionsProvider::FetchSnippetsInTheBackground() { |
| 298 FetchSnippets(/*interactive_request=*/false); | 301 FetchSnippets(/*interactive_request=*/false); |
| 299 } | 302 } |
| 300 | 303 |
| 301 void RemoteSuggestionsProvider::FetchSnippetsForAllCategories() { | 304 void RemoteSuggestionsProvider::FetchSnippetsForAllCategories() { |
| 302 // TODO(markusheintz): Investigate whether we can call the Fetch method | 305 // TODO(markusheintz): Investigate whether we can call the Fetch method |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 324 if (!ready()) { | 327 if (!ready()) { |
| 325 return; | 328 return; |
| 326 } | 329 } |
| 327 MarkEmptyCategoriesAsLoading(); | 330 MarkEmptyCategoriesAsLoading(); |
| 328 | 331 |
| 329 NTPSnippetsFetcher::Params params = BuildFetchParams(); | 332 NTPSnippetsFetcher::Params params = BuildFetchParams(); |
| 330 params.hosts = hosts; | 333 params.hosts = hosts; |
| 331 params.interactive_request = interactive_request; | 334 params.interactive_request = interactive_request; |
| 332 snippets_fetcher_->FetchSnippets( | 335 snippets_fetcher_->FetchSnippets( |
| 333 params, base::BindOnce(&RemoteSuggestionsProvider::OnFetchFinished, | 336 params, base::BindOnce(&RemoteSuggestionsProvider::OnFetchFinished, |
| 334 base::Unretained(this))); | 337 base::Unretained(this), interactive_request)); |
| 335 } | 338 } |
| 336 | 339 |
| 337 void RemoteSuggestionsProvider::Fetch( | 340 void RemoteSuggestionsProvider::Fetch( |
| 338 const Category& category, | 341 const Category& category, |
| 339 const std::set<std::string>& known_suggestion_ids, | 342 const std::set<std::string>& known_suggestion_ids, |
| 340 const FetchDoneCallback& callback) { | 343 const FetchDoneCallback& callback) { |
| 341 if (!ready()) { | 344 if (!ready()) { |
| 342 CallWithEmptyResults(callback, | 345 CallWithEmptyResults(callback, |
| 343 Status(StatusCode::TEMPORARY_ERROR, | 346 Status(StatusCode::TEMPORARY_ERROR, |
| 344 "RemoteSuggestionsProvider is not ready!")); | 347 "RemoteSuggestionsProvider is not ready!")); |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 634 FinishInitialization(); | 637 FinishInitialization(); |
| 635 } | 638 } |
| 636 | 639 |
| 637 void RemoteSuggestionsProvider::OnDatabaseError() { | 640 void RemoteSuggestionsProvider::OnDatabaseError() { |
| 638 EnterState(State::ERROR_OCCURRED); | 641 EnterState(State::ERROR_OCCURRED); |
| 639 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); | 642 UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR); |
| 640 } | 643 } |
| 641 | 644 |
| 642 void RemoteSuggestionsProvider::OnFetchMoreFinished( | 645 void RemoteSuggestionsProvider::OnFetchMoreFinished( |
| 643 const FetchDoneCallback& fetching_callback, | 646 const FetchDoneCallback& fetching_callback, |
| 644 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) { | 647 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories, |
| 648 NTPSnippetsFetcher::FetchResult fetch_result) { | |
| 645 if (!fetched_categories) { | 649 if (!fetched_categories) { |
| 646 // TODO(fhorschig): Disambiguate the kind of error that led here. | 650 // TODO(fhorschig): Disambiguate the kind of error that led here. |
| 647 CallWithEmptyResults(fetching_callback, | 651 CallWithEmptyResults(fetching_callback, |
| 648 Status(StatusCode::PERMANENT_ERROR, | 652 Status(StatusCode::PERMANENT_ERROR, |
| 649 "The NTPSnippetsFetcher did not " | 653 "The NTPSnippetsFetcher did not " |
| 650 "complete the fetching successfully.")); | 654 "complete the fetching successfully.")); |
| 651 return; | 655 return; |
| 652 } | 656 } |
| 653 if (fetched_categories->size() != 1u) { | 657 if (fetched_categories->size() != 1u) { |
| 654 LOG(DFATAL) << "Requested one exclusive category but received " | 658 LOG(DFATAL) << "Requested one exclusive category but received " |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 // once the snippets fetcher supports concurrent requests. We can then see if | 699 // once the snippets fetcher supports concurrent requests. We can then see if |
| 696 // Nuke should also cancel outstanding requests or we want to check the | 700 // Nuke should also cancel outstanding requests or we want to check the |
| 697 // status. | 701 // status. |
| 698 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); | 702 UpdateCategoryStatus(category, CategoryStatus::AVAILABLE); |
| 699 // Notify callers and observers. | 703 // Notify callers and observers. |
| 700 fetching_callback.Run(Status(StatusCode::SUCCESS), std::move(result)); | 704 fetching_callback.Run(Status(StatusCode::SUCCESS), std::move(result)); |
| 701 NotifyNewSuggestions(category, *existing_content); | 705 NotifyNewSuggestions(category, *existing_content); |
| 702 } | 706 } |
| 703 | 707 |
| 704 void RemoteSuggestionsProvider::OnFetchFinished( | 708 void RemoteSuggestionsProvider::OnFetchFinished( |
| 705 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) { | 709 bool interactive_request, |
| 710 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories, | |
| 711 NTPSnippetsFetcher::FetchResult fetch_result) { | |
| 706 if (!ready()) { | 712 if (!ready()) { |
| 707 // TODO(tschumann): What happens if this was a user-triggered, interactive | 713 // TODO(tschumann): What happens if this was a user-triggered, interactive |
| 708 // request? Is the UI waiting indefinitely now? | 714 // request? Is the UI waiting indefinitely now? |
| 709 return; | 715 return; |
| 710 } | 716 } |
| 711 | 717 |
| 718 // Record the fetch time of a successfull background fetch. | |
| 719 if (!interactive_request && | |
| 720 fetch_result == NTPSnippetsFetcher::FetchResult::SUCCESS) { | |
| 721 pref_service_->SetInt64(prefs::kLastSuccessfullBackgroundFetchTime, | |
| 722 tick_clock_->NowTicks().ToInternalValue()); | |
| 723 } | |
| 724 | |
| 712 // Mark all categories as not provided by the server in the latest fetch. The | 725 // Mark all categories as not provided by the server in the latest fetch. The |
| 713 // ones we got will be marked again below. | 726 // ones we got will be marked again below. |
| 714 for (auto& item : category_contents_) { | 727 for (auto& item : category_contents_) { |
| 715 CategoryContent* content = &item.second; | 728 CategoryContent* content = &item.second; |
| 716 content->included_in_last_server_response = false; | 729 content->included_in_last_server_response = false; |
| 717 } | 730 } |
| 718 | 731 |
| 719 // Clear up expired dismissed snippets before we use them to filter new ones. | 732 // Clear up expired dismissed snippets before we use them to filter new ones. |
| 720 ClearExpiredDismissedSnippets(); | 733 ClearExpiredDismissedSnippets(); |
| 721 | 734 |
| (...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1332 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = | 1345 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1333 default; | 1346 default; |
| 1334 | 1347 |
| 1335 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; | 1348 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; |
| 1336 | 1349 |
| 1337 RemoteSuggestionsProvider::CategoryContent& | 1350 RemoteSuggestionsProvider::CategoryContent& |
| 1338 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = | 1351 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = |
| 1339 default; | 1352 default; |
| 1340 | 1353 |
| 1341 } // namespace ntp_snippets | 1354 } // namespace ntp_snippets |
| OLD | NEW |