Index: components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc |
diff --git a/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc b/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc |
index 788ac534fe4bbe6901bd49e23bfeaf21c35f8e9a..2c2e6b7171f722a07b6e6c4993a62a809412a441 100644 |
--- a/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc |
+++ b/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc |
@@ -37,6 +37,7 @@ enum class FetchingInterval { |
PERSISTENT_FALLBACK, |
PERSISTENT_WIFI, |
SOFT_ON_USAGE_EVENT, |
+ SOFT_ON_NTP_OPENED, |
COUNT |
}; |
@@ -46,25 +47,30 @@ enum class FetchingInterval { |
// The values of each array specify a default time interval for the intervals |
// defined by the enum FetchingInterval. The default time intervals defined in |
// the arrays can be overridden using different variation parameters. |
-const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0}; |
-const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0}; |
+const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, |
+ 6.0}; |
+const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0, |
+ 2.0}; |
const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { |
- 24.0, 6.0, 2.0}; |
+ 24.0, 6.0, 2.0, 1.0}; |
// Variation parameters than can be used to override the default fetching |
// intervals. |
const char* kFetchingIntervalParamNameRareNtpUser[] = { |
"fetching_interval_hours-fallback-rare_ntp_user", |
"fetching_interval_hours-wifi-rare_ntp_user", |
- "soft_fetching_interval_hours-active-rare_ntp_user"}; |
+ "soft_fetching_interval_hours-active-rare_ntp_user", |
+ "soft_on_ntp_opened_interval_hours-rare_ntp_user"}; |
const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
"fetching_interval_hours-fallback-active_ntp_user", |
"fetching_interval_hours-wifi-active_ntp_user", |
- "soft_fetching_interval_hours-active-active_ntp_user"}; |
+ "soft_fetching_interval_hours-active-active_ntp_user", |
+ "soft_on_ntp_opened_interval_hours-active_ntp_user"}; |
const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
"fetching_interval_hours-fallback-active_suggestions_consumer", |
"fetching_interval_hours-wifi-active_suggestions_consumer", |
- "soft_fetching_interval_hours-active-active_suggestions_consumer"}; |
+ "soft_fetching_interval_hours-active-active_suggestions_consumer", |
+ "soft_on_ntp_opened_interval_hours-active_suggestions_consumer"}; |
static_assert( |
static_cast<unsigned int>(FetchingInterval::COUNT) == |
@@ -205,6 +211,8 @@ void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( |
registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent, |
0); |
registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); |
+ registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnNtpOpened, |
+ 0); |
} |
void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { |
@@ -220,7 +228,7 @@ void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { |
void SchedulingRemoteSuggestionsProvider::OnBrowserForegrounded() { |
// TODO(jkrcal): Consider that this is called whenever we open or return to an |
// Activity. Therefore, keep work light for fast start up calls. |
- if (!ShouldRefetchInTheBackgroundNow()) { |
+ if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_FOREGROUNDED)) { |
return; |
} |
@@ -230,7 +238,7 @@ void SchedulingRemoteSuggestionsProvider::OnBrowserForegrounded() { |
void SchedulingRemoteSuggestionsProvider::OnBrowserColdStart() { |
// TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast |
// cold start ups. |
- if (!ShouldRefetchInTheBackgroundNow()) { |
+ if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_COLD_START)) { |
return; |
} |
@@ -238,7 +246,7 @@ void SchedulingRemoteSuggestionsProvider::OnBrowserColdStart() { |
} |
void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { |
- if (!ShouldRefetchInTheBackgroundNow()) { |
+ if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { |
return; |
} |
@@ -396,6 +404,8 @@ SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const { |
FetchingInterval::PERSISTENT_FALLBACK, user_class); |
schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval( |
FetchingInterval::SOFT_ON_USAGE_EVENT, user_class); |
+ schedule.interval_soft_on_ntp_opened = GetDesiredFetchingInterval( |
+ FetchingInterval::SOFT_ON_NTP_OPENED, user_class); |
return schedule; |
} |
@@ -408,6 +418,8 @@ void SchedulingRemoteSuggestionsProvider::LoadLastFetchingSchedule() { |
prefs::kSnippetPersistentFetchingIntervalFallback)); |
schedule_.interval_soft_on_usage_event = base::TimeDelta::FromInternalValue( |
pref_service_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnUsageEvent)); |
+ schedule_.interval_soft_on_ntp_opened = base::TimeDelta::FromInternalValue( |
+ pref_service_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnNtpOpened)); |
} |
void SchedulingRemoteSuggestionsProvider::StoreFetchingSchedule() { |
@@ -419,6 +431,9 @@ void SchedulingRemoteSuggestionsProvider::StoreFetchingSchedule() { |
pref_service_->SetInt64( |
prefs::kSnippetSoftFetchingIntervalOnUsageEvent, |
schedule_.interval_soft_on_usage_event.ToInternalValue()); |
+ pref_service_->SetInt64( |
+ prefs::kSnippetSoftFetchingIntervalOnNtpOpened, |
+ schedule_.interval_soft_on_ntp_opened.ToInternalValue()); |
} |
void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundIfEnabled( |
@@ -434,11 +449,26 @@ void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundIfEnabled( |
RefetchInTheBackground(/*callback=*/nullptr); |
} |
-bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow() { |
- base::Time first_allowed_fetch_time = |
- base::Time::FromInternalValue( |
- pref_service_->GetInt64(prefs::kSnippetLastFetchAttempt)) + |
- schedule_.interval_soft_on_usage_event; |
+bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow( |
+ SchedulingRemoteSuggestionsProvider::TriggerType trigger) { |
+ const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( |
+ pref_service_->GetInt64(prefs::kSnippetLastFetchAttempt)); |
+ base::Time first_allowed_fetch_time; |
+ switch (trigger) { |
+ case TriggerType::NTP_OPENED: |
+ first_allowed_fetch_time = |
+ last_fetch_attempt_time + schedule_.interval_soft_on_ntp_opened; |
+ break; |
+ case TriggerType::BROWSER_FOREGROUNDED: |
+ case TriggerType::BROWSER_COLD_START: |
+ first_allowed_fetch_time = |
+ last_fetch_attempt_time + schedule_.interval_soft_on_usage_event; |
+ break; |
+ case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP: |
+ case TriggerType::COUNT: |
+ NOTREACHED(); |
+ break; |
+ } |
return first_allowed_fetch_time <= clock_->Now(); |
} |