| Index: components/ntp_snippets/remote/remote_suggestions_provider_impl.cc
|
| diff --git a/components/ntp_snippets/remote/remote_suggestions_provider.cc b/components/ntp_snippets/remote/remote_suggestions_provider_impl.cc
|
| similarity index 81%
|
| copy from components/ntp_snippets/remote/remote_suggestions_provider.cc
|
| copy to components/ntp_snippets/remote/remote_suggestions_provider_impl.cc
|
| index ea4551e9518b6f0b9ee066d93f785de4b2c3a5bd..dd88b934054f9c4bb56e90f182e31bfef37b2898 100644
|
| --- a/components/ntp_snippets/remote/remote_suggestions_provider.cc
|
| +++ b/components/ntp_snippets/remote/remote_suggestions_provider_impl.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "components/ntp_snippets/remote/remote_suggestions_provider.h"
|
| +#include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h"
|
|
|
| #include <algorithm>
|
| #include <iterator>
|
| @@ -30,7 +30,6 @@
|
| #include "components/ntp_snippets/pref_names.h"
|
| #include "components/ntp_snippets/remote/remote_suggestions_database.h"
|
| #include "components/ntp_snippets/switches.h"
|
| -#include "components/ntp_snippets/user_classifier.h"
|
| #include "components/prefs/pref_registry_simple.h"
|
| #include "components/prefs/pref_service.h"
|
| #include "components/variations/variations_associated_data.h"
|
| @@ -49,22 +48,6 @@ const int kMaxSnippetCount = 10;
|
| // Number of archived snippets we keep around in memory.
|
| const int kMaxArchivedSnippetCount = 200;
|
|
|
| -// Default values for fetching intervals, fallback and wifi.
|
| -const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0};
|
| -const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0};
|
| -const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0};
|
| -
|
| -// Variation parameters than can override the default fetching intervals.
|
| -const char* kFetchingIntervalParamNameRareNtpUser[] = {
|
| - "fetching_interval_hours-fallback-rare_ntp_user",
|
| - "fetching_interval_hours-wifi-rare_ntp_user"};
|
| -const char* kFetchingIntervalParamNameActiveNtpUser[] = {
|
| - "fetching_interval_hours-fallback-active_ntp_user",
|
| - "fetching_interval_hours-wifi-active_ntp_user"};
|
| -const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = {
|
| - "fetching_interval_hours-fallback-active_suggestions_consumer",
|
| - "fetching_interval_hours-wifi-active_suggestions_consumer"};
|
| -
|
| // Keys for storing CategoryContent info in prefs.
|
| const char kCategoryContentId[] = "id";
|
| const char kCategoryContentTitle[] = "title";
|
| @@ -74,42 +57,6 @@ const char kCategoryContentAllowFetchingMore[] = "allow_fetching_more";
|
| // TODO(treib): Remove after M57.
|
| const char kDeprecatedSnippetHostsPref[] = "ntp_snippets.hosts";
|
|
|
| -base::TimeDelta GetFetchingInterval(bool is_wifi,
|
| - UserClassifier::UserClass user_class) {
|
| - double value_hours = 0.0;
|
| -
|
| - const int index = is_wifi ? 1 : 0;
|
| - const char* param_name = "";
|
| - switch (user_class) {
|
| - case UserClassifier::UserClass::RARE_NTP_USER:
|
| - value_hours = kDefaultFetchingIntervalRareNtpUser[index];
|
| - param_name = kFetchingIntervalParamNameRareNtpUser[index];
|
| - break;
|
| - case UserClassifier::UserClass::ACTIVE_NTP_USER:
|
| - value_hours = kDefaultFetchingIntervalActiveNtpUser[index];
|
| - param_name = kFetchingIntervalParamNameActiveNtpUser[index];
|
| - break;
|
| - case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
|
| - value_hours = kDefaultFetchingIntervalActiveSuggestionsConsumer[index];
|
| - param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index];
|
| - break;
|
| - }
|
| -
|
| - // The default value can be overridden by a variation parameter.
|
| - std::string param_value_str = variations::GetVariationParamValueByFeature(
|
| - ntp_snippets::kArticleSuggestionsFeature, param_name);
|
| - if (!param_value_str.empty()) {
|
| - double param_value_hours = 0.0;
|
| - if (base::StringToDouble(param_value_str, ¶m_value_hours)) {
|
| - value_hours = param_value_hours;
|
| - } else {
|
| - LOG(WARNING) << "Invalid value for variation parameter " << param_name;
|
| - }
|
| - }
|
| -
|
| - return base::TimeDelta::FromSecondsD(value_hours * 3600.0);
|
| -}
|
| -
|
| std::unique_ptr<std::vector<std::string>> GetSnippetIDVector(
|
| const NTPSnippet::PtrVector& snippets) {
|
| auto result = base::MakeUnique<std::vector<std::string>>();
|
| @@ -278,9 +225,8 @@ void CachedImageFetcher::OnSnippetImageFetchedFromDatabase(
|
| // |image_decoder_| is null in tests.
|
| if (image_decoder_ && !data.empty()) {
|
| image_decoder_->DecodeImage(
|
| - data, base::Bind(
|
| - &CachedImageFetcher::OnSnippetImageDecodedFromDatabase,
|
| - base::Unretained(this), callback, suggestion_id, url));
|
| + data, base::Bind(&CachedImageFetcher::OnSnippetImageDecodedFromDatabase,
|
| + base::Unretained(this), callback, suggestion_id, url));
|
| return;
|
| }
|
| // Fetching from the DB failed; start a network fetch.
|
| @@ -321,27 +267,23 @@ void CachedImageFetcher::FetchSnippetImageFromNetwork(
|
| base::Unretained(this), callback));
|
| }
|
|
|
| -RemoteSuggestionsProvider::RemoteSuggestionsProvider(
|
| +RemoteSuggestionsProviderImpl::RemoteSuggestionsProviderImpl(
|
| Observer* observer,
|
| PrefService* pref_service,
|
| const std::string& application_language_code,
|
| CategoryRanker* category_ranker,
|
| - const UserClassifier* user_classifier,
|
| - NTPSnippetsScheduler* scheduler,
|
| std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
|
| std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
|
| std::unique_ptr<image_fetcher::ImageDecoder> image_decoder,
|
| std::unique_ptr<RemoteSuggestionsDatabase> database,
|
| std::unique_ptr<RemoteSuggestionsStatusService> status_service)
|
| - : ContentSuggestionsProvider(observer),
|
| + : RemoteSuggestionsProvider(observer),
|
| state_(State::NOT_INITED),
|
| pref_service_(pref_service),
|
| articles_category_(
|
| Category::FromKnownCategory(KnownCategories::ARTICLES)),
|
| application_language_code_(application_language_code),
|
| category_ranker_(category_ranker),
|
| - user_classifier_(user_classifier),
|
| - scheduler_(scheduler),
|
| snippets_fetcher_(std::move(snippets_fetcher)),
|
| database_(std::move(database)),
|
| image_fetcher_(std::move(image_fetcher),
|
| @@ -350,6 +292,7 @@ RemoteSuggestionsProvider::RemoteSuggestionsProvider(
|
| database_.get()),
|
| status_service_(std::move(status_service)),
|
| fetch_when_ready_(false),
|
| + fetch_when_ready_interactive_(false),
|
| nuke_when_initialized_(false),
|
| clock_(base::MakeUnique<base::DefaultClock>()) {
|
| pref_service_->ClearPref(kDeprecatedSnippetHostsPref);
|
| @@ -372,45 +315,57 @@ RemoteSuggestionsProvider::RemoteSuggestionsProvider(
|
| }
|
|
|
| database_->SetErrorCallback(base::Bind(
|
| - &RemoteSuggestionsProvider::OnDatabaseError, base::Unretained(this)));
|
| + &RemoteSuggestionsProviderImpl::OnDatabaseError, base::Unretained(this)));
|
|
|
| // We transition to other states while finalizing the initialization, when the
|
| // database is done loading.
|
| database_load_start_ = base::TimeTicks::Now();
|
| - database_->LoadSnippets(base::Bind(
|
| - &RemoteSuggestionsProvider::OnDatabaseLoaded, base::Unretained(this)));
|
| + database_->LoadSnippets(
|
| + base::Bind(&RemoteSuggestionsProviderImpl::OnDatabaseLoaded,
|
| + base::Unretained(this)));
|
| }
|
|
|
| -RemoteSuggestionsProvider::~RemoteSuggestionsProvider() = default;
|
| +RemoteSuggestionsProviderImpl::~RemoteSuggestionsProviderImpl() = default;
|
|
|
| // static
|
| -void RemoteSuggestionsProvider::RegisterProfilePrefs(
|
| +void RemoteSuggestionsProviderImpl::RegisterProfilePrefs(
|
| PrefRegistrySimple* registry) {
|
| // TODO(treib): Remove after M57.
|
| registry->RegisterListPref(kDeprecatedSnippetHostsPref);
|
| registry->RegisterListPref(prefs::kRemoteSuggestionCategories);
|
| - registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0);
|
| - registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback,
|
| - 0);
|
| registry->RegisterInt64Pref(prefs::kLastSuccessfulBackgroundFetchTime, 0);
|
|
|
| RemoteSuggestionsStatusService::RegisterProfilePrefs(registry);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::FetchSnippetsInTheBackground() {
|
| - FetchSnippets(/*interactive_request=*/false);
|
| +void RemoteSuggestionsProviderImpl::SetProviderStatusCallback(
|
| + const ProviderStatusCallback& callback) {
|
| + provider_status_callback_ = callback;
|
| + // Call the observer right away if we've reached any final state.
|
| +}
|
| +
|
| +void RemoteSuggestionsProviderImpl::ReloadSuggestions() {
|
| + FetchSnippets(/*interactive_request=*/true, FetchStatusCallback());
|
| }
|
|
|
| -void RemoteSuggestionsProvider::FetchSnippetsForAllCategories() {
|
| - // TODO(markusheintz): Investigate whether we can call the Fetch method
|
| - // instead of the FetchSnippets.
|
| - FetchSnippets(/*interactive_request=*/true);
|
| +void RemoteSuggestionsProviderImpl::RefetchInTheBackground(
|
| + const FetchStatusCallback& callback) {
|
| + FetchSnippets(/*interactive_request=*/false, callback);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::FetchSnippets(
|
| - bool interactive_request) {
|
| +const NTPSnippetsFetcher*
|
| +RemoteSuggestionsProviderImpl::snippets_fetcher_for_testing_and_debugging()
|
| + const {
|
| + return snippets_fetcher_.get();
|
| +}
|
| +
|
| +void RemoteSuggestionsProviderImpl::FetchSnippets(
|
| + bool interactive_request,
|
| + const FetchStatusCallback& callback) {
|
| if (!ready()) {
|
| fetch_when_ready_ = true;
|
| + fetch_when_ready_interactive_ = interactive_request;
|
| + fetch_when_ready_callback_ = callback;
|
| return;
|
| }
|
|
|
| @@ -419,11 +374,12 @@ void RemoteSuggestionsProvider::FetchSnippets(
|
| NTPSnippetsFetcher::Params params = BuildFetchParams();
|
| params.interactive_request = interactive_request;
|
| snippets_fetcher_->FetchSnippets(
|
| - params, base::BindOnce(&RemoteSuggestionsProvider::OnFetchFinished,
|
| - base::Unretained(this), interactive_request));
|
| + params, base::BindOnce(&RemoteSuggestionsProviderImpl::OnFetchFinished,
|
| + base::Unretained(this), callback,
|
| + interactive_request));
|
| }
|
|
|
| -void RemoteSuggestionsProvider::Fetch(
|
| +void RemoteSuggestionsProviderImpl::Fetch(
|
| const Category& category,
|
| const std::set<std::string>& known_suggestion_ids,
|
| const FetchDoneCallback& callback) {
|
| @@ -440,12 +396,14 @@ void RemoteSuggestionsProvider::Fetch(
|
| params.exclusive_category = category;
|
|
|
| snippets_fetcher_->FetchSnippets(
|
| - params, base::BindOnce(&RemoteSuggestionsProvider::OnFetchMoreFinished,
|
| - base::Unretained(this), callback));
|
| + params,
|
| + base::BindOnce(&RemoteSuggestionsProviderImpl::OnFetchMoreFinished,
|
| + base::Unretained(this), callback));
|
| }
|
|
|
| // Builds default fetcher params.
|
| -NTPSnippetsFetcher::Params RemoteSuggestionsProvider::BuildFetchParams() const {
|
| +NTPSnippetsFetcher::Params RemoteSuggestionsProviderImpl::BuildFetchParams()
|
| + const {
|
| NTPSnippetsFetcher::Params result;
|
| result.language_code = application_language_code_;
|
| result.count_to_fetch = kMaxSnippetCount;
|
| @@ -458,7 +416,7 @@ NTPSnippetsFetcher::Params RemoteSuggestionsProvider::BuildFetchParams() const {
|
| return result;
|
| }
|
|
|
| -void RemoteSuggestionsProvider::MarkEmptyCategoriesAsLoading() {
|
| +void RemoteSuggestionsProviderImpl::MarkEmptyCategoriesAsLoading() {
|
| for (const auto& item : category_contents_) {
|
| Category category = item.first;
|
| const CategoryContent& content = item.second;
|
| @@ -468,58 +426,20 @@ void RemoteSuggestionsProvider::MarkEmptyCategoriesAsLoading() {
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::RescheduleFetching(bool force) {
|
| - // The scheduler only exists on Android so far, it's null on other platforms.
|
| - if (!scheduler_) {
|
| - return;
|
| - }
|
| -
|
| - if (ready()) {
|
| - base::TimeDelta old_interval_wifi = base::TimeDelta::FromInternalValue(
|
| - pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi));
|
| - base::TimeDelta old_interval_fallback =
|
| - base::TimeDelta::FromInternalValue(pref_service_->GetInt64(
|
| - prefs::kSnippetBackgroundFetchingIntervalFallback));
|
| - UserClassifier::UserClass user_class = user_classifier_->GetUserClass();
|
| - base::TimeDelta interval_wifi =
|
| - GetFetchingInterval(/*is_wifi=*/true, user_class);
|
| - base::TimeDelta interval_fallback =
|
| - GetFetchingInterval(/*is_wifi=*/false, user_class);
|
| - if (force || interval_wifi != old_interval_wifi ||
|
| - interval_fallback != old_interval_fallback) {
|
| - scheduler_->Schedule(interval_wifi, interval_fallback);
|
| - pref_service_->SetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi,
|
| - interval_wifi.ToInternalValue());
|
| - pref_service_->SetInt64(prefs::kSnippetBackgroundFetchingIntervalFallback,
|
| - interval_fallback.ToInternalValue());
|
| - }
|
| - } else {
|
| - // If we're NOT_INITED, we don't know whether to schedule or unschedule.
|
| - // If |force| is false, all is well: We'll reschedule on the next state
|
| - // change anyway. If it's true, then unschedule here, to make sure that the
|
| - // next reschedule actually happens.
|
| - if (state_ != State::NOT_INITED || force) {
|
| - scheduler_->Unschedule();
|
| - pref_service_->ClearPref(prefs::kSnippetBackgroundFetchingIntervalWifi);
|
| - pref_service_->ClearPref(
|
| - prefs::kSnippetBackgroundFetchingIntervalFallback);
|
| - }
|
| - }
|
| -}
|
| -
|
| -CategoryStatus RemoteSuggestionsProvider::GetCategoryStatus(Category category) {
|
| +CategoryStatus RemoteSuggestionsProviderImpl::GetCategoryStatus(
|
| + Category category) {
|
| auto content_it = category_contents_.find(category);
|
| DCHECK(content_it != category_contents_.end());
|
| return content_it->second.status;
|
| }
|
|
|
| -CategoryInfo RemoteSuggestionsProvider::GetCategoryInfo(Category category) {
|
| +CategoryInfo RemoteSuggestionsProviderImpl::GetCategoryInfo(Category category) {
|
| auto content_it = category_contents_.find(category);
|
| DCHECK(content_it != category_contents_.end());
|
| return content_it->second.info;
|
| }
|
|
|
| -void RemoteSuggestionsProvider::DismissSuggestion(
|
| +void RemoteSuggestionsProviderImpl::DismissSuggestion(
|
| const ContentSuggestion::ID& suggestion_id) {
|
| if (!ready()) {
|
| return;
|
| @@ -532,7 +452,7 @@ void RemoteSuggestionsProvider::DismissSuggestion(
|
| suggestion_id.id_within_category());
|
| }
|
|
|
| -void RemoteSuggestionsProvider::ClearHistory(
|
| +void RemoteSuggestionsProviderImpl::ClearHistory(
|
| base::Time begin,
|
| base::Time end,
|
| const base::Callback<bool(const GURL& url)>& filter) {
|
| @@ -546,7 +466,7 @@ void RemoteSuggestionsProvider::ClearHistory(
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::ClearCachedSuggestions(Category category) {
|
| +void RemoteSuggestionsProviderImpl::ClearCachedSuggestions(Category category) {
|
| if (!initialized()) {
|
| return;
|
| }
|
| @@ -569,7 +489,7 @@ void RemoteSuggestionsProvider::ClearCachedSuggestions(Category category) {
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::OnSignInStateChanged() {
|
| +void RemoteSuggestionsProviderImpl::OnSignInStateChanged() {
|
| // Make sure the status service is registered and we already initialised its
|
| // start state.
|
| if (!initialized()) {
|
| @@ -579,7 +499,7 @@ void RemoteSuggestionsProvider::OnSignInStateChanged() {
|
| status_service_->OnSignInStateChanged();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::GetDismissedSuggestionsForDebugging(
|
| +void RemoteSuggestionsProviderImpl::GetDismissedSuggestionsForDebugging(
|
| Category category,
|
| const DismissedSuggestionsCallback& callback) {
|
| auto content_it = category_contents_.find(category);
|
| @@ -588,7 +508,7 @@ void RemoteSuggestionsProvider::GetDismissedSuggestionsForDebugging(
|
| ConvertToContentSuggestions(category, content_it->second.dismissed));
|
| }
|
|
|
| -void RemoteSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
|
| +void RemoteSuggestionsProviderImpl::ClearDismissedSuggestionsForDebugging(
|
| Category category) {
|
| auto content_it = category_contents_.find(category);
|
| DCHECK(content_it != category_contents_.end());
|
| @@ -609,14 +529,14 @@ void RemoteSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
|
| }
|
|
|
| // static
|
| -int RemoteSuggestionsProvider::GetMaxSnippetCountForTesting() {
|
| +int RemoteSuggestionsProviderImpl::GetMaxSnippetCountForTesting() {
|
| return kMaxSnippetCount;
|
| }
|
|
|
| ////////////////////////////////////////////////////////////////////////////////
|
| // Private methods
|
|
|
| -GURL RemoteSuggestionsProvider::FindSnippetImageUrl(
|
| +GURL RemoteSuggestionsProviderImpl::FindSnippetImageUrl(
|
| const ContentSuggestion::ID& suggestion_id) const {
|
| DCHECK(base::ContainsKey(category_contents_, suggestion_id.category()));
|
|
|
| @@ -630,7 +550,7 @@ GURL RemoteSuggestionsProvider::FindSnippetImageUrl(
|
| return snippet->salient_image_url();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::OnDatabaseLoaded(
|
| +void RemoteSuggestionsProviderImpl::OnDatabaseLoaded(
|
| NTPSnippet::PtrVector snippets) {
|
| if (state_ == State::ERROR_OCCURRED) {
|
| return;
|
| @@ -686,12 +606,12 @@ void RemoteSuggestionsProvider::OnDatabaseLoaded(
|
| FinishInitialization();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::OnDatabaseError() {
|
| +void RemoteSuggestionsProviderImpl::OnDatabaseError() {
|
| EnterState(State::ERROR_OCCURRED);
|
| UpdateAllCategoryStatus(CategoryStatus::LOADING_ERROR);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::OnFetchMoreFinished(
|
| +void RemoteSuggestionsProviderImpl::OnFetchMoreFinished(
|
| const FetchDoneCallback& fetching_callback,
|
| Status status,
|
| NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) {
|
| @@ -751,7 +671,8 @@ void RemoteSuggestionsProvider::OnFetchMoreFinished(
|
| NotifyNewSuggestions(category, *existing_content);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::OnFetchFinished(
|
| +void RemoteSuggestionsProviderImpl::OnFetchFinished(
|
| + const FetchStatusCallback& callback,
|
| bool interactive_request,
|
| Status status,
|
| NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories) {
|
| @@ -827,16 +748,10 @@ void RemoteSuggestionsProvider::OnFetchFinished(
|
| content.dismissed.size());
|
| }
|
|
|
| - // Reschedule after a successful fetch. This resets all currently scheduled
|
| - // fetches, to make sure the fallback interval triggers only if no wifi fetch
|
| - // succeeded, and also that we don't do a background fetch immediately after
|
| - // a user-initiated one.
|
| - if (fetched_categories) {
|
| - RescheduleFetching(true);
|
| - }
|
| + callback.Run(status);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::ArchiveSnippets(
|
| +void RemoteSuggestionsProviderImpl::ArchiveSnippets(
|
| CategoryContent* content,
|
| NTPSnippet::PtrVector* to_archive) {
|
| // Archive previous snippets - move them at the beginning of the list.
|
| @@ -857,7 +772,7 @@ void RemoteSuggestionsProvider::ArchiveSnippets(
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::SanitizeReceivedSnippets(
|
| +void RemoteSuggestionsProviderImpl::SanitizeReceivedSnippets(
|
| const NTPSnippet::PtrVector& dismissed,
|
| NTPSnippet::PtrVector* snippets) {
|
| DCHECK(ready());
|
| @@ -865,7 +780,7 @@ void RemoteSuggestionsProvider::SanitizeReceivedSnippets(
|
| RemoveIncompleteSnippets(snippets);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::IntegrateSnippets(
|
| +void RemoteSuggestionsProviderImpl::IntegrateSnippets(
|
| CategoryContent* content,
|
| NTPSnippet::PtrVector new_snippets) {
|
| DCHECK(ready());
|
| @@ -894,7 +809,7 @@ void RemoteSuggestionsProvider::IntegrateSnippets(
|
| content->snippets = std::move(new_snippets);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::DismissSuggestionFromCategoryContent(
|
| +void RemoteSuggestionsProviderImpl::DismissSuggestionFromCategoryContent(
|
| CategoryContent* content,
|
| const std::string& id_within_category) {
|
| auto it = std::find_if(
|
| @@ -914,7 +829,7 @@ void RemoteSuggestionsProvider::DismissSuggestionFromCategoryContent(
|
| content->snippets.erase(it);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::ClearExpiredDismissedSnippets() {
|
| +void RemoteSuggestionsProviderImpl::ClearExpiredDismissedSnippets() {
|
| std::vector<Category> categories_to_erase;
|
|
|
| const base::Time now = base::Time::Now();
|
| @@ -952,7 +867,7 @@ void RemoteSuggestionsProvider::ClearExpiredDismissedSnippets() {
|
| StoreCategoriesToPrefs();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::ClearOrphanedImages() {
|
| +void RemoteSuggestionsProviderImpl::ClearOrphanedImages() {
|
| auto alive_snippets = base::MakeUnique<std::set<std::string>>();
|
| for (const auto& entry : category_contents_) {
|
| const CategoryContent& content = entry.second;
|
| @@ -966,7 +881,7 @@ void RemoteSuggestionsProvider::ClearOrphanedImages() {
|
| database_->GarbageCollectImages(std::move(alive_snippets));
|
| }
|
|
|
| -void RemoteSuggestionsProvider::NukeAllSnippets() {
|
| +void RemoteSuggestionsProviderImpl::NukeAllSnippets() {
|
| std::vector<Category> categories_to_erase;
|
|
|
| // Empty the ARTICLES category and remove all others, since they may or may
|
| @@ -992,7 +907,7 @@ void RemoteSuggestionsProvider::NukeAllSnippets() {
|
| StoreCategoriesToPrefs();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::FetchSuggestionImage(
|
| +void RemoteSuggestionsProviderImpl::FetchSuggestionImage(
|
| const ContentSuggestion::ID& suggestion_id,
|
| const ImageFetchedCallback& callback) {
|
| if (!base::ContainsKey(category_contents_, suggestion_id.category())) {
|
| @@ -1012,7 +927,7 @@ void RemoteSuggestionsProvider::FetchSuggestionImage(
|
| image_fetcher_.FetchSuggestionImage(suggestion_id, image_url, callback);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::EnterStateReady() {
|
| +void RemoteSuggestionsProviderImpl::EnterStateReady() {
|
| if (nuke_when_initialized_) {
|
| NukeAllSnippets();
|
| nuke_when_initialized_ = false;
|
| @@ -1027,8 +942,10 @@ void RemoteSuggestionsProvider::EnterStateReady() {
|
| // Either add a DCHECK here that we actually are allowed to do network I/O
|
| // or change the logic so that some explicit call is always needed for the
|
| // network request.
|
| - FetchSnippets(/*interactive_request=*/false);
|
| + FetchSnippets(fetch_when_ready_interactive_,
|
| + fetch_when_ready_callback_);
|
| fetch_when_ready_ = false;
|
| + fetch_when_ready_callback_ = FetchStatusCallback();
|
| }
|
|
|
| for (const auto& item : category_contents_) {
|
| @@ -1042,15 +959,15 @@ void RemoteSuggestionsProvider::EnterStateReady() {
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::EnterStateDisabled() {
|
| +void RemoteSuggestionsProviderImpl::EnterStateDisabled() {
|
| NukeAllSnippets();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::EnterStateError() {
|
| +void RemoteSuggestionsProviderImpl::EnterStateError() {
|
| status_service_.reset();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::FinishInitialization() {
|
| +void RemoteSuggestionsProviderImpl::FinishInitialization() {
|
| if (nuke_when_initialized_) {
|
| // We nuke here in addition to EnterStateReady, so that it happens even if
|
| // we enter the DISABLED state below.
|
| @@ -1060,8 +977,8 @@ void RemoteSuggestionsProvider::FinishInitialization() {
|
|
|
| // Note: Initializing the status service will run the callback right away with
|
| // the current state.
|
| - status_service_->Init(base::Bind(&RemoteSuggestionsProvider::OnStatusChanged,
|
| - base::Unretained(this)));
|
| + status_service_->Init(base::Bind(
|
| + &RemoteSuggestionsProviderImpl::OnStatusChanged, base::Unretained(this)));
|
|
|
| // Always notify here even if we got nothing from the database, because we
|
| // don't know how long the fetch will take or if it will even complete.
|
| @@ -1076,7 +993,7 @@ void RemoteSuggestionsProvider::FinishInitialization() {
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::OnStatusChanged(
|
| +void RemoteSuggestionsProviderImpl::OnStatusChanged(
|
| RemoteSuggestionsStatus old_status,
|
| RemoteSuggestionsStatus new_status) {
|
| switch (new_status) {
|
| @@ -1086,7 +1003,8 @@ void RemoteSuggestionsProvider::OnStatusChanged(
|
| // Clear nonpersonalized suggestions.
|
| NukeAllSnippets();
|
| // Fetch personalized ones.
|
| - FetchSnippets(/*interactive_request=*/true);
|
| + // TODO(jkrcal): Loop in SchedulingRemoteSuggestionsProvider somehow.
|
| + FetchSnippets(/*interactive_request=*/true, FetchStatusCallback());
|
| } else {
|
| // Do not change the status. That will be done in EnterStateReady().
|
| EnterState(State::READY);
|
| @@ -1099,7 +1017,8 @@ void RemoteSuggestionsProvider::OnStatusChanged(
|
| // Clear personalized suggestions.
|
| NukeAllSnippets();
|
| // Fetch nonpersonalized ones.
|
| - FetchSnippets(/*interactive_request=*/true);
|
| + // TODO(jkrcal): Loop in SchedulingRemoteSuggestionsProvider somehow.
|
| + FetchSnippets(/*interactive_request=*/true, FetchStatusCallback());
|
| } else {
|
| // Do not change the status. That will be done in EnterStateReady().
|
| EnterState(State::READY);
|
| @@ -1118,7 +1037,7 @@ void RemoteSuggestionsProvider::OnStatusChanged(
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::EnterState(State state) {
|
| +void RemoteSuggestionsProviderImpl::EnterState(State state) {
|
| if (state == state_) {
|
| return;
|
| }
|
| @@ -1160,11 +1079,30 @@ void RemoteSuggestionsProvider::EnterState(State state) {
|
| break;
|
| }
|
|
|
| - // Schedule or un-schedule background fetching after each state change.
|
| - RescheduleFetching(false);
|
| + NotifyStateChanged();
|
| }
|
|
|
| -void RemoteSuggestionsProvider::NotifyNewSuggestions(
|
| +void RemoteSuggestionsProviderImpl::NotifyStateChanged() {
|
| + switch (state_) {
|
| + case State::NOT_INITED:
|
| + // Initial state, not sure yet whether active or not.
|
| + break;
|
| + case State::READY:
|
| + provider_status_callback_.Run(ProviderStatus::ACTIVE);
|
| + break;
|
| + case State::DISABLED:
|
| + provider_status_callback_.Run(ProviderStatus::INACTIVE);
|
| + break;
|
| + case State::ERROR_OCCURRED:
|
| + provider_status_callback_.Run(ProviderStatus::INACTIVE);
|
| + break;
|
| + case State::COUNT:
|
| + NOTREACHED();
|
| + break;
|
| + }
|
| +}
|
| +
|
| +void RemoteSuggestionsProviderImpl::NotifyNewSuggestions(
|
| Category category,
|
| const CategoryContent& content) {
|
| DCHECK(IsCategoryStatusAvailable(content.status));
|
| @@ -1177,8 +1115,9 @@ void RemoteSuggestionsProvider::NotifyNewSuggestions(
|
| observer()->OnNewSuggestions(this, category, std::move(result));
|
| }
|
|
|
| -void RemoteSuggestionsProvider::UpdateCategoryStatus(Category category,
|
| - CategoryStatus status) {
|
| +void RemoteSuggestionsProviderImpl::UpdateCategoryStatus(
|
| + Category category,
|
| + CategoryStatus status) {
|
| auto content_it = category_contents_.find(category);
|
| DCHECK(content_it != category_contents_.end());
|
| CategoryContent& content = content_it->second;
|
| @@ -1194,7 +1133,8 @@ void RemoteSuggestionsProvider::UpdateCategoryStatus(Category category,
|
| observer()->OnCategoryStatusChanged(this, category, content.status);
|
| }
|
|
|
| -void RemoteSuggestionsProvider::UpdateAllCategoryStatus(CategoryStatus status) {
|
| +void RemoteSuggestionsProviderImpl::UpdateAllCategoryStatus(
|
| + CategoryStatus status) {
|
| for (const auto& category : category_contents_) {
|
| UpdateCategoryStatus(category.first, status);
|
| }
|
| @@ -1215,7 +1155,7 @@ typename T::const_iterator FindSnippetInContainer(
|
|
|
| } // namespace
|
|
|
| -const NTPSnippet* RemoteSuggestionsProvider::CategoryContent::FindSnippet(
|
| +const NTPSnippet* RemoteSuggestionsProviderImpl::CategoryContent::FindSnippet(
|
| const std::string& id_within_category) const {
|
| // Search for the snippet in current and archived snippets.
|
| auto it = FindSnippetInContainer(snippets, id_within_category);
|
| @@ -1233,9 +1173,9 @@ const NTPSnippet* RemoteSuggestionsProvider::CategoryContent::FindSnippet(
|
| return nullptr;
|
| }
|
|
|
| -RemoteSuggestionsProvider::CategoryContent*
|
| -RemoteSuggestionsProvider::UpdateCategoryInfo(Category category,
|
| - const CategoryInfo& info) {
|
| +RemoteSuggestionsProviderImpl::CategoryContent*
|
| +RemoteSuggestionsProviderImpl::UpdateCategoryInfo(Category category,
|
| + const CategoryInfo& info) {
|
| auto content_it = category_contents_.find(category);
|
| if (content_it == category_contents_.end()) {
|
| content_it = category_contents_
|
| @@ -1247,7 +1187,7 @@ RemoteSuggestionsProvider::UpdateCategoryInfo(Category category,
|
| return &content_it->second;
|
| }
|
|
|
| -void RemoteSuggestionsProvider::RestoreCategoriesFromPrefs() {
|
| +void RemoteSuggestionsProviderImpl::RestoreCategoriesFromPrefs() {
|
| // This must only be called at startup, before there are any categories.
|
| DCHECK(category_contents_.empty());
|
|
|
| @@ -1300,7 +1240,7 @@ void RemoteSuggestionsProvider::RestoreCategoriesFromPrefs() {
|
| }
|
| }
|
|
|
| -void RemoteSuggestionsProvider::StoreCategoriesToPrefs() {
|
| +void RemoteSuggestionsProviderImpl::StoreCategoriesToPrefs() {
|
| // Collect all the CategoryContents.
|
| std::vector<std::pair<Category, const CategoryContent*>> to_store;
|
| for (const auto& entry : category_contents_) {
|
| @@ -1331,17 +1271,17 @@ void RemoteSuggestionsProvider::StoreCategoriesToPrefs() {
|
| pref_service_->Set(prefs::kRemoteSuggestionCategories, list);
|
| }
|
|
|
| -RemoteSuggestionsProvider::CategoryContent::CategoryContent(
|
| +RemoteSuggestionsProviderImpl::CategoryContent::CategoryContent(
|
| const CategoryInfo& info)
|
| : info(info) {}
|
|
|
| -RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) =
|
| - default;
|
| +RemoteSuggestionsProviderImpl::CategoryContent::CategoryContent(
|
| + CategoryContent&&) = default;
|
|
|
| -RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default;
|
| +RemoteSuggestionsProviderImpl::CategoryContent::~CategoryContent() = default;
|
|
|
| -RemoteSuggestionsProvider::CategoryContent&
|
| -RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) =
|
| +RemoteSuggestionsProviderImpl::CategoryContent&
|
| +RemoteSuggestionsProviderImpl::CategoryContent::operator=(CategoryContent&&) =
|
| default;
|
|
|
| } // namespace ntp_snippets
|
|
|