OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider.
h" | 5 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider.
h" |
6 | 6 |
7 #include <random> | 7 #include <random> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), | 88 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), |
89 "Fill in all the info for fetching intervals."); | 89 "Fill in all the info for fetching intervals."); |
90 | 90 |
91 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", | 91 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", |
92 "browser_foregrounded", | 92 "browser_foregrounded", |
93 "browser_cold_start"}; | 93 "browser_cold_start"}; |
94 | 94 |
95 const char* kTriggerTypesParamName = "scheduler_trigger_types"; | 95 const char* kTriggerTypesParamName = "scheduler_trigger_types"; |
96 const char* kTriggerTypesParamValueForEmptyList = "-"; | 96 const char* kTriggerTypesParamValueForEmptyList = "-"; |
97 | 97 |
| 98 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30; |
| 99 |
98 // Returns the time interval to use for scheduling remote suggestion fetches for | 100 // Returns the time interval to use for scheduling remote suggestion fetches for |
99 // the given interval and user_class. | 101 // the given interval and user_class. |
100 base::TimeDelta GetDesiredFetchingInterval( | 102 base::TimeDelta GetDesiredFetchingInterval( |
101 FetchingInterval interval, | 103 FetchingInterval interval, |
102 UserClassifier::UserClass user_class) { | 104 UserClassifier::UserClass user_class) { |
103 DCHECK(interval != FetchingInterval::COUNT); | 105 DCHECK(interval != FetchingInterval::COUNT); |
104 const unsigned int index = static_cast<unsigned int>(interval); | 106 const unsigned int index = static_cast<unsigned int>(interval); |
105 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); | 107 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); |
106 | 108 |
107 double default_value_hours = 0.0; | 109 double default_value_hours = 0.0; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 background_fetch_in_progress_(false), | 187 background_fetch_in_progress_(false), |
186 user_classifier_(user_classifier), | 188 user_classifier_(user_classifier), |
187 pref_service_(pref_service), | 189 pref_service_(pref_service), |
188 clock_(std::move(clock)), | 190 clock_(std::move(clock)), |
189 enabled_triggers_(GetEnabledTriggerTypes()) { | 191 enabled_triggers_(GetEnabledTriggerTypes()) { |
190 DCHECK(user_classifier); | 192 DCHECK(user_classifier); |
191 DCHECK(pref_service); | 193 DCHECK(pref_service); |
192 | 194 |
193 LoadLastFetchingSchedule(); | 195 LoadLastFetchingSchedule(); |
194 | 196 |
195 provider_->SetProviderStatusCallback( | 197 provider_->SetRemoteSuggestionsScheduler(this); |
196 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | |
197 base::BindRepeating( | |
198 &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged, | |
199 base::Unretained(this)))); | |
200 } | 198 } |
201 | 199 |
202 SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() = | 200 SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() = |
203 default; | 201 default; |
204 | 202 |
205 // static | 203 // static |
206 void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( | 204 void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( |
207 PrefRegistrySimple* registry) { | 205 PrefRegistrySimple* registry) { |
208 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); | 206 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); |
209 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, | 207 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, |
210 0); | 208 0); |
211 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 209 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent, |
212 0); | 210 0); |
213 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); | 211 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); |
214 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnNtpOpened, | 212 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnNtpOpened, |
215 0); | 213 0); |
216 } | 214 } |
217 | 215 |
| 216 void SchedulingRemoteSuggestionsProvider::OnProviderActivated() { |
| 217 StartScheduling(); |
| 218 } |
| 219 |
| 220 void SchedulingRemoteSuggestionsProvider::OnProviderInactivated() { |
| 221 StopScheduling(); |
| 222 } |
| 223 |
| 224 void SchedulingRemoteSuggestionsProvider::OnSuggestionsCleared() { |
| 225 // Be ready to fetch any time (upon a trigger) as we have no suggestions. |
| 226 pref_service_->ClearPref(prefs::kSnippetLastFetchAttempt); |
| 227 } |
| 228 |
| 229 void SchedulingRemoteSuggestionsProvider::OnHistoryCleared() { |
| 230 // Due to privacy, we should not fetch for a while (unless the user explicitly |
| 231 // asks for new suggestions) do give sync the time to propagate the changes in |
| 232 // history to the server. |
| 233 background_fetches_allowed_after_ = |
| 234 clock_->Now() + base::TimeDelta::FromMinutes( |
| 235 kBlockBackgroundFetchesMinutesAfterClearingHistory); |
| 236 } |
| 237 |
218 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { | 238 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { |
219 // Force the reschedule by stopping and starting it again. | 239 // Force the reschedule by stopping and starting it again. |
220 StopScheduling(); | 240 StopScheduling(); |
221 StartScheduling(); | 241 StartScheduling(); |
222 } | 242 } |
223 | 243 |
224 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { | 244 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { |
225 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); | 245 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); |
226 } | 246 } |
227 | 247 |
(...skipping 18 matching lines...) Expand all Loading... |
246 } | 266 } |
247 | 267 |
248 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { | 268 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { |
249 if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { | 269 if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { |
250 return; | 270 return; |
251 } | 271 } |
252 | 272 |
253 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); | 273 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); |
254 } | 274 } |
255 | 275 |
256 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( | 276 void SchedulingRemoteSuggestionsProvider::SetRemoteSuggestionsScheduler( |
257 std::unique_ptr<ProviderStatusCallback> callback) { | 277 RemoteSuggestionsScheduler* scheduler) { |
258 provider_->SetProviderStatusCallback(std::move(callback)); | 278 provider_->SetRemoteSuggestionsScheduler(scheduler); |
259 } | 279 } |
260 | 280 |
261 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( | 281 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( |
262 std::unique_ptr<FetchStatusCallback> callback) { | 282 std::unique_ptr<FetchStatusCallback> callback) { |
263 if (background_fetch_in_progress_) { | 283 if (background_fetch_in_progress_) { |
264 if (callback) { | 284 if (callback) { |
265 callback->Run( | 285 callback->Run( |
266 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); | 286 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); |
267 } | 287 } |
268 return; | 288 return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 Category category, | 357 Category category, |
338 const DismissedSuggestionsCallback& callback) { | 358 const DismissedSuggestionsCallback& callback) { |
339 provider_->GetDismissedSuggestionsForDebugging(category, callback); | 359 provider_->GetDismissedSuggestionsForDebugging(category, callback); |
340 } | 360 } |
341 | 361 |
342 void SchedulingRemoteSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | 362 void SchedulingRemoteSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
343 Category category) { | 363 Category category) { |
344 provider_->ClearDismissedSuggestionsForDebugging(category); | 364 provider_->ClearDismissedSuggestionsForDebugging(category); |
345 } | 365 } |
346 | 366 |
347 void SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged( | |
348 RemoteSuggestionsProvider::ProviderStatus status) { | |
349 switch (status) { | |
350 case RemoteSuggestionsProvider::ProviderStatus::ACTIVE: | |
351 StartScheduling(); | |
352 return; | |
353 case RemoteSuggestionsProvider::ProviderStatus::INACTIVE: | |
354 StopScheduling(); | |
355 return; | |
356 } | |
357 NOTREACHED(); | |
358 } | |
359 | |
360 void SchedulingRemoteSuggestionsProvider::StartScheduling() { | 367 void SchedulingRemoteSuggestionsProvider::StartScheduling() { |
361 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); | 368 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); |
362 | 369 |
363 if (schedule_ == new_schedule) { | 370 if (schedule_ == new_schedule) { |
364 // Do not schedule if nothing has changed; | 371 // Do not schedule if nothing has changed; |
365 return; | 372 return; |
366 } | 373 } |
367 | 374 |
368 schedule_ = new_schedule; | 375 schedule_ = new_schedule; |
369 StoreFetchingSchedule(); | 376 StoreFetchingSchedule(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 case TriggerType::BROWSER_FOREGROUNDED: | 469 case TriggerType::BROWSER_FOREGROUNDED: |
463 case TriggerType::BROWSER_COLD_START: | 470 case TriggerType::BROWSER_COLD_START: |
464 first_allowed_fetch_time = | 471 first_allowed_fetch_time = |
465 last_fetch_attempt_time + schedule_.interval_soft_on_usage_event; | 472 last_fetch_attempt_time + schedule_.interval_soft_on_usage_event; |
466 break; | 473 break; |
467 case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP: | 474 case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP: |
468 case TriggerType::COUNT: | 475 case TriggerType::COUNT: |
469 NOTREACHED(); | 476 NOTREACHED(); |
470 break; | 477 break; |
471 } | 478 } |
472 return first_allowed_fetch_time <= clock_->Now(); | 479 base::Time now = clock_->Now(); |
| 480 |
| 481 return background_fetches_allowed_after_ <= now && |
| 482 first_allowed_fetch_time <= now; |
473 } | 483 } |
474 | 484 |
475 bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled( | 485 bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled( |
476 SchedulingRemoteSuggestionsProvider::TriggerType trigger) const { | 486 SchedulingRemoteSuggestionsProvider::TriggerType trigger) const { |
477 if (schedule_.is_empty()) { | 487 if (schedule_.is_empty()) { |
478 return true; // Background fetches are disabled in general. | 488 return true; // Background fetches are disabled in general. |
479 } | 489 } |
480 | 490 |
481 if (enabled_triggers_.count(trigger) == 0) { | 491 if (enabled_triggers_.count(trigger) == 0) { |
482 return true; // Background fetches for |trigger| are not enabled. | 492 return true; // Background fetches for |trigger| are not enabled. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 return enabled_types; | 568 return enabled_types; |
559 } | 569 } |
560 | 570 |
561 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> | 571 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> |
562 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { | 572 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { |
563 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | 573 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, |
564 TriggerType::BROWSER_FOREGROUNDED}; | 574 TriggerType::BROWSER_FOREGROUNDED}; |
565 } | 575 } |
566 | 576 |
567 } // namespace ntp_snippets | 577 } // namespace ntp_snippets |
OLD | NEW |