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

Unified Diff: components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc

Issue 2702223004: [Remote suggestions] Move all decisions to fetch to the scheduler (Closed)
Patch Set: Fixing an embarassing bug :) Created 3 years, 10 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/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 d4ec38e483049b7edb8a35507ff5fd1a9ddb778f..19b651c9e686b87e5a345e15189626fed344d033 100644
--- a/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc
+++ b/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc
@@ -95,6 +95,8 @@ const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened",
const char* kTriggerTypesParamName = "scheduler_trigger_types";
const char* kTriggerTypesParamValueForEmptyList = "-";
+const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30;
+
// Returns the time interval to use for scheduling remote suggestion fetches for
// the given interval and user_class.
base::TimeDelta GetDesiredFetchingInterval(
@@ -191,12 +193,6 @@ SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider(
DCHECK(pref_service);
LoadLastFetchingSchedule();
-
- provider_->SetProviderStatusCallback(
- base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>(
- base::BindRepeating(
- &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged,
- base::Unretained(this))));
}
SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() =
@@ -215,6 +211,30 @@ void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs(
0);
}
+void SchedulingRemoteSuggestionsProvider::OnProviderActivated() {
+ StartScheduling();
+}
+
+void SchedulingRemoteSuggestionsProvider::OnProviderDeactivated() {
+ StopScheduling();
+}
+
+void SchedulingRemoteSuggestionsProvider::OnSuggestionsCleared() {
+ // There are no suggestions, be as eager to fetch as possible.
+ ClearLastFetchAttemptTime();
+}
+
+void SchedulingRemoteSuggestionsProvider::OnHistoryCleared() {
+ // Due to privacy, we should not fetch for a while (unless the user explicitly
+ // asks for new suggestions) to give sync the time to propagate the changes in
+ // history to the server.
+ background_fetches_allowed_after_ =
+ clock_->Now() + base::TimeDelta::FromMinutes(
+ kBlockBackgroundFetchesMinutesAfterClearingHistory);
+ // After that time elapses, we should fetch as soon as possible.
+ ClearLastFetchAttemptTime();
+}
+
void SchedulingRemoteSuggestionsProvider::RescheduleFetching() {
// Force the reschedule by stopping and starting it again.
StopScheduling();
@@ -253,11 +273,6 @@ void SchedulingRemoteSuggestionsProvider::OnNTPOpened() {
RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED);
}
-void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback(
- std::unique_ptr<ProviderStatusCallback> callback) {
- provider_->SetProviderStatusCallback(std::move(callback));
-}
-
void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground(
std::unique_ptr<FetchStatusCallback> callback) {
if (background_fetch_in_progress_) {
@@ -344,19 +359,6 @@ void SchedulingRemoteSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
provider_->ClearDismissedSuggestionsForDebugging(category);
}
-void SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged(
- RemoteSuggestionsProvider::ProviderStatus status) {
- switch (status) {
- case RemoteSuggestionsProvider::ProviderStatus::ACTIVE:
- StartScheduling();
- return;
- case RemoteSuggestionsProvider::ProviderStatus::INACTIVE:
- StopScheduling();
- return;
- }
- NOTREACHED();
-}
-
void SchedulingRemoteSuggestionsProvider::StartScheduling() {
FetchingSchedule new_schedule = GetDesiredFetchingSchedule();
@@ -469,7 +471,10 @@ bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow(
NOTREACHED();
break;
}
- return first_allowed_fetch_time <= clock_->Now();
+ base::Time now = clock_->Now();
+
+ return background_fetches_allowed_after_ <= now &&
+ first_allowed_fetch_time <= now;
}
bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled(
@@ -519,6 +524,10 @@ void SchedulingRemoteSuggestionsProvider::OnFetchCompleted(
ApplyPersistentFetchingSchedule();
}
+void SchedulingRemoteSuggestionsProvider::ClearLastFetchAttemptTime() {
+ pref_service_->ClearPref(prefs::kSnippetLastFetchAttempt);
+}
+
std::set<SchedulingRemoteSuggestionsProvider::TriggerType>
SchedulingRemoteSuggestionsProvider::GetEnabledTriggerTypes() {
static_assert(static_cast<unsigned int>(TriggerType::COUNT) ==

Powered by Google App Engine
This is Rietveld 408576698