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

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: 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 deb218a3197f5f1238b96408cbdfc9d0eb35c2ff..a6ba52298a8a4e0c0996dc57561574807a6aae0d 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(
@@ -192,11 +194,7 @@ SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider(
LoadLastFetchingSchedule();
- provider_->SetProviderStatusCallback(
- base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>(
- base::BindRepeating(
- &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged,
- base::Unretained(this))));
+ provider_->SetRemoteSuggestionsScheduler(this);
}
SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() =
@@ -215,6 +213,28 @@ void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs(
0);
}
+void SchedulingRemoteSuggestionsProvider::OnProviderActivated() {
+ StartScheduling();
+}
+
+void SchedulingRemoteSuggestionsProvider::OnProviderInactivated() {
+ StopScheduling();
+}
+
+void SchedulingRemoteSuggestionsProvider::OnSuggestionsCleared() {
+ // Be ready to fetch any time (upon a trigger) as we have no suggestions.
+ pref_service_->ClearPref(prefs::kSnippetLastFetchAttempt);
+}
+
+void SchedulingRemoteSuggestionsProvider::OnHistoryCleared() {
+ // Due to privacy, we should not fetch for a while (unless the user explicitly
+ // asks for new suggestions) do give sync the time to propagate the changes in
+ // history to the server.
+ background_fetches_allowed_after_ =
+ clock_->Now() + base::TimeDelta::FromMinutes(
+ kBlockBackgroundFetchesMinutesAfterClearingHistory);
+}
+
void SchedulingRemoteSuggestionsProvider::RescheduleFetching() {
// Force the reschedule by stopping and starting it again.
StopScheduling();
@@ -253,9 +273,9 @@ void SchedulingRemoteSuggestionsProvider::OnNTPOpened() {
RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED);
}
-void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback(
- std::unique_ptr<ProviderStatusCallback> callback) {
- provider_->SetProviderStatusCallback(std::move(callback));
+void SchedulingRemoteSuggestionsProvider::SetRemoteSuggestionsScheduler(
+ RemoteSuggestionsScheduler* scheduler) {
+ provider_->SetRemoteSuggestionsScheduler(scheduler);
}
void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground(
@@ -344,19 +364,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 +476,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(

Powered by Google App Engine
This is Rietveld 408576698