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

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 2363753002: [NTP Snippets] Don't reschedule background fetching on every startup (Closed)
Patch Set: rebase Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/ntp_snippets_service.cc
diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc
index 20995ee0a75cb927314376771ea85492b42530b1..2313a15bf45cfcab344f8aeee3f6a83d5039e739 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -232,6 +232,9 @@ NTPSnippetsService::~NTPSnippetsService() {
// static
void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(prefs::kSnippetHosts);
+ registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0);
+ registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback,
+ 0);
NTPSnippetsStatusService::RegisterProfilePrefs(registry);
}
@@ -268,21 +271,40 @@ void NTPSnippetsService::FetchSnippetsFromHosts(
interactive_request);
}
-void NTPSnippetsService::RescheduleFetching() {
+void NTPSnippetsService::RescheduleFetching(bool force) {
// The scheduler only exists on Android so far, it's null on other platforms.
if (!scheduler_)
return;
- // If we're NOT_INITED, we don't know whether to schedule or un-schedule.
- // We'll reschedule on the next state change anyway, so do nothing here.
- if (state_ == State::NOT_INITED)
- return;
-
if (ready()) {
- scheduler_->Schedule(GetFetchingIntervalWifi(),
- GetFetchingIntervalFallback());
+ 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));
+ base::TimeDelta interval_wifi = GetFetchingIntervalWifi();
+ base::TimeDelta interval_fallback = GetFetchingIntervalFallback();
+ 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 {
- scheduler_->Unschedule();
+ // If we're NOT_INITED, we don't know whether to schedule or un-schedule.
+ // 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);
+ }
}
}
@@ -610,7 +632,7 @@ void NTPSnippetsService::OnFetchFinished(
// succeeded, and also that we don't do a background fetch immediately after
// a user-initiated one.
if (snippets)
- RescheduleFetching();
+ RescheduleFetching(true);
}
void NTPSnippetsService::ArchiveSnippets(Category category,
@@ -990,10 +1012,7 @@ void NTPSnippetsService::EnterState(State state) {
}
// Schedule or un-schedule background fetching after each state change.
- // TODO(treib): This resets all currently scheduled fetches on each Chrome
- // start. Maybe store the currently scheduled values in prefs, and only
- // reschedule if they have changed? crbug.com/646842
- RescheduleFetching();
+ RescheduleFetching(false);
}
void NTPSnippetsService::NotifyNewSuggestions() {
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698