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 19 matching lines...) Expand all Loading... | |
30 // for each interval. Initially all the timers are started at the same time. | 30 // for each interval. Initially all the timers are started at the same time. |
31 // Fetches are | 31 // Fetches are |
32 // only performed when certain conditions associated with the intervals are | 32 // only performed when certain conditions associated with the intervals are |
33 // met. If a fetch failed, then only the corresponding timer is reset. The | 33 // met. If a fetch failed, then only the corresponding timer is reset. The |
34 // other timers are not touched. | 34 // other timers are not touched. |
35 // TODO(markusheintz): Describe the individual intervals. | 35 // TODO(markusheintz): Describe the individual intervals. |
36 enum class FetchingInterval { | 36 enum class FetchingInterval { |
37 PERSISTENT_FALLBACK, | 37 PERSISTENT_FALLBACK, |
38 PERSISTENT_WIFI, | 38 PERSISTENT_WIFI, |
39 SOFT_ON_USAGE_EVENT, | 39 SOFT_ON_USAGE_EVENT, |
40 NTP_OPEN, | |
jkrcal
2017/02/15 16:35:34
nit: please update consistently with the FetchingS
markusheintz_
2017/02/15 21:19:29
Done.
| |
40 COUNT | 41 COUNT |
41 }; | 42 }; |
42 | 43 |
43 // The following arrays specify default values for remote suggestions fetch | 44 // The following arrays specify default values for remote suggestions fetch |
44 // intervals corresponding to individual user classes. The user classes are | 45 // intervals corresponding to individual user classes. The user classes are |
45 // defined by the user classifier. There must be an array for each user class. | 46 // defined by the user classifier. There must be an array for each user class. |
46 // The values of each array specify a default time interval for the intervals | 47 // The values of each array specify a default time interval for the intervals |
47 // defined by the enum FetchingInterval. The default time intervals defined in | 48 // defined by the enum FetchingInterval. The default time intervals defined in |
48 // the arrays can be overridden using different variation parameters. | 49 // the arrays can be overridden using different variation parameters. |
49 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0}; | 50 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, |
50 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0}; | 51 6.0}; |
52 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0, | |
53 2.0}; | |
51 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { | 54 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { |
52 24.0, 6.0, 2.0}; | 55 24.0, 6.0, 2.0, 1.0}; |
53 | 56 |
54 // Variation parameters than can be used to override the default fetching | 57 // Variation parameters than can be used to override the default fetching |
55 // intervals. | 58 // intervals. |
56 const char* kFetchingIntervalParamNameRareNtpUser[] = { | 59 const char* kFetchingIntervalParamNameRareNtpUser[] = { |
57 "fetching_interval_hours-fallback-rare_ntp_user", | 60 "fetching_interval_hours-fallback-rare_ntp_user", |
58 "fetching_interval_hours-wifi-rare_ntp_user", | 61 "fetching_interval_hours-wifi-rare_ntp_user", |
59 "soft_fetching_interval_hours-active-rare_ntp_user"}; | 62 "soft_fetching_interval_hours-active-rare_ntp_user", |
63 "ntp_open_interval_hours-rare_ntp_user"}; | |
jkrcal
2017/02/15 16:35:34
nit: please update consistently with the FetchingS
markusheintz_
2017/02/15 21:19:29
Done.
| |
60 const char* kFetchingIntervalParamNameActiveNtpUser[] = { | 64 const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
61 "fetching_interval_hours-fallback-active_ntp_user", | 65 "fetching_interval_hours-fallback-active_ntp_user", |
62 "fetching_interval_hours-wifi-active_ntp_user", | 66 "fetching_interval_hours-wifi-active_ntp_user", |
63 "soft_fetching_interval_hours-active-active_ntp_user"}; | 67 "soft_fetching_interval_hours-active-active_ntp_user", |
68 "ntp_open_interval_hours-active_ntp_user"}; | |
64 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { | 69 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
65 "fetching_interval_hours-fallback-active_suggestions_consumer", | 70 "fetching_interval_hours-fallback-active_suggestions_consumer", |
66 "fetching_interval_hours-wifi-active_suggestions_consumer", | 71 "fetching_interval_hours-wifi-active_suggestions_consumer", |
67 "soft_fetching_interval_hours-active-active_suggestions_consumer"}; | 72 "soft_fetching_interval_hours-active-active_suggestions_consumer", |
73 "ntp_open_interval_hours-active_suggestions_consumer"}; | |
68 | 74 |
69 static_assert( | 75 static_assert( |
70 static_cast<unsigned int>(FetchingInterval::COUNT) == | 76 static_cast<unsigned int>(FetchingInterval::COUNT) == |
71 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && | 77 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && |
72 static_cast<unsigned int>(FetchingInterval::COUNT) == | 78 static_cast<unsigned int>(FetchingInterval::COUNT) == |
73 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && | 79 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && |
74 static_cast<unsigned int>(FetchingInterval::COUNT) == | 80 static_cast<unsigned int>(FetchingInterval::COUNT) == |
75 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && | 81 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && |
76 static_cast<unsigned int>(FetchingInterval::COUNT) == | 82 static_cast<unsigned int>(FetchingInterval::COUNT) == |
77 arraysize(kFetchingIntervalParamNameRareNtpUser) && | 83 arraysize(kFetchingIntervalParamNameRareNtpUser) && |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 StartScheduling(); | 219 StartScheduling(); |
214 } | 220 } |
215 | 221 |
216 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { | 222 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { |
217 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); | 223 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); |
218 } | 224 } |
219 | 225 |
220 void SchedulingRemoteSuggestionsProvider::OnBrowserForegrounded() { | 226 void SchedulingRemoteSuggestionsProvider::OnBrowserForegrounded() { |
221 // TODO(jkrcal): Consider that this is called whenever we open or return to an | 227 // TODO(jkrcal): Consider that this is called whenever we open or return to an |
222 // Activity. Therefore, keep work light for fast start up calls. | 228 // Activity. Therefore, keep work light for fast start up calls. |
223 if (!ShouldRefetchInTheBackgroundNow()) { | 229 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_FOREGROUNDED)) { |
224 return; | 230 return; |
225 } | 231 } |
226 | 232 |
227 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED); | 233 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED); |
228 } | 234 } |
229 | 235 |
230 void SchedulingRemoteSuggestionsProvider::OnBrowserColdStart() { | 236 void SchedulingRemoteSuggestionsProvider::OnBrowserColdStart() { |
231 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast | 237 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast |
232 // cold start ups. | 238 // cold start ups. |
233 if (!ShouldRefetchInTheBackgroundNow()) { | 239 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_COLD_START)) { |
234 return; | 240 return; |
235 } | 241 } |
236 | 242 |
237 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START); | 243 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START); |
238 } | 244 } |
239 | 245 |
240 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { | 246 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { |
241 if (!ShouldRefetchInTheBackgroundNow()) { | 247 if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { |
242 return; | 248 return; |
243 } | 249 } |
244 | 250 |
245 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); | 251 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); |
246 } | 252 } |
247 | 253 |
248 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( | 254 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( |
249 std::unique_ptr<ProviderStatusCallback> callback) { | 255 std::unique_ptr<ProviderStatusCallback> callback) { |
250 provider_->SetProviderStatusCallback(std::move(callback)); | 256 provider_->SetProviderStatusCallback(std::move(callback)); |
251 } | 257 } |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
389 SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const { | 395 SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const { |
390 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); | 396 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); |
391 | 397 |
392 FetchingSchedule schedule; | 398 FetchingSchedule schedule; |
393 schedule.interval_persistent_wifi = | 399 schedule.interval_persistent_wifi = |
394 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); | 400 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); |
395 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( | 401 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( |
396 FetchingInterval::PERSISTENT_FALLBACK, user_class); | 402 FetchingInterval::PERSISTENT_FALLBACK, user_class); |
397 schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval( | 403 schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval( |
398 FetchingInterval::SOFT_ON_USAGE_EVENT, user_class); | 404 FetchingInterval::SOFT_ON_USAGE_EVENT, user_class); |
405 schedule.interval_ntp_open = | |
406 GetDesiredFetchingInterval(FetchingInterval::NTP_OPEN, user_class); | |
399 | 407 |
400 return schedule; | 408 return schedule; |
401 } | 409 } |
402 | 410 |
403 void SchedulingRemoteSuggestionsProvider::LoadLastFetchingSchedule() { | 411 void SchedulingRemoteSuggestionsProvider::LoadLastFetchingSchedule() { |
404 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( | 412 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( |
405 pref_service_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); | 413 pref_service_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); |
406 schedule_.interval_persistent_fallback = | 414 schedule_.interval_persistent_fallback = |
407 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( | 415 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( |
408 prefs::kSnippetPersistentFetchingIntervalFallback)); | 416 prefs::kSnippetPersistentFetchingIntervalFallback)); |
409 schedule_.interval_soft_on_usage_event = base::TimeDelta::FromInternalValue( | 417 schedule_.interval_soft_on_usage_event = base::TimeDelta::FromInternalValue( |
jkrcal
2017/02/15 16:35:34
we need another pref for the new interval to load
markusheintz_
2017/02/15 21:19:29
Done.
| |
410 pref_service_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnUsageEvent)); | 418 pref_service_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnUsageEvent)); |
411 } | 419 } |
412 | 420 |
413 void SchedulingRemoteSuggestionsProvider::StoreFetchingSchedule() { | 421 void SchedulingRemoteSuggestionsProvider::StoreFetchingSchedule() { |
414 pref_service_->SetInt64(prefs::kSnippetPersistentFetchingIntervalWifi, | 422 pref_service_->SetInt64(prefs::kSnippetPersistentFetchingIntervalWifi, |
415 schedule_.interval_persistent_wifi.ToInternalValue()); | 423 schedule_.interval_persistent_wifi.ToInternalValue()); |
416 pref_service_->SetInt64( | 424 pref_service_->SetInt64( |
417 prefs::kSnippetPersistentFetchingIntervalFallback, | 425 prefs::kSnippetPersistentFetchingIntervalFallback, |
418 schedule_.interval_persistent_fallback.ToInternalValue()); | 426 schedule_.interval_persistent_fallback.ToInternalValue()); |
419 pref_service_->SetInt64( | 427 pref_service_->SetInt64( |
420 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 428 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, |
421 schedule_.interval_soft_on_usage_event.ToInternalValue()); | 429 schedule_.interval_soft_on_usage_event.ToInternalValue()); |
jkrcal
2017/02/15 16:35:34
...and to store it here
markusheintz_
2017/02/15 21:19:29
Done.
| |
422 } | 430 } |
423 | 431 |
424 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundIfEnabled( | 432 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundIfEnabled( |
425 SchedulingRemoteSuggestionsProvider::TriggerType trigger) { | 433 SchedulingRemoteSuggestionsProvider::TriggerType trigger) { |
426 if (BackgroundFetchesDisabled(trigger)) { | 434 if (BackgroundFetchesDisabled(trigger)) { |
427 return; | 435 return; |
428 } | 436 } |
429 | 437 |
430 UMA_HISTOGRAM_ENUMERATION( | 438 UMA_HISTOGRAM_ENUMERATION( |
431 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", | 439 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", |
432 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); | 440 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); |
433 | 441 |
434 RefetchInTheBackground(/*callback=*/nullptr); | 442 RefetchInTheBackground(/*callback=*/nullptr); |
435 } | 443 } |
436 | 444 |
437 bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow() { | 445 bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow( |
438 base::Time first_allowed_fetch_time = | 446 SchedulingRemoteSuggestionsProvider::TriggerType trigger) { |
439 base::Time::FromInternalValue( | 447 final base::Time last_fetch_attempt_time = base::Time::FromInternalValue( |
440 pref_service_->GetInt64(prefs::kSnippetLastFetchAttempt)) + | 448 pref_service_->GetInt64(prefs::kSnippetLastFetchAttempt)); |
441 schedule_.interval_soft_on_usage_event; | 449 base::Time first_allowed_fetch_time; |
450 switch (trigger) { | |
451 case TriggerType::NTP_OPENED: | |
452 first_allowed_fetch_time = | |
453 last_fetch_attempt_time + schedule_.interval_ntp_open; | |
454 break; | |
455 case TriggerType::BROWSER_FOREGROUNDED: | |
456 case TriggerType::BROWSER_COLD_START: | |
457 first_allowed_fetch_time = | |
458 last_fetch_attempt_time + schedule_.interval_soft_on_usage_event; | |
459 break; | |
460 case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP: | |
markusheintz_
2017/02/15 21:19:29
Changed to default in order to prevent having to u
jkrcal
2017/02/16 07:32:54
I would actually rather have both of them listed h
markusheintz_
2017/02/16 13:45:53
Done
| |
461 NOTREACHED(); | |
462 break; | |
463 } | |
442 return first_allowed_fetch_time <= clock_->Now(); | 464 return first_allowed_fetch_time <= clock_->Now(); |
443 } | 465 } |
444 | 466 |
445 bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled( | 467 bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled( |
446 SchedulingRemoteSuggestionsProvider::TriggerType trigger) const { | 468 SchedulingRemoteSuggestionsProvider::TriggerType trigger) const { |
447 if (schedule_.is_empty()) { | 469 if (schedule_.is_empty()) { |
448 return true; // Background fetches are disabled in general. | 470 return true; // Background fetches are disabled in general. |
449 } | 471 } |
450 | 472 |
451 if (enabled_triggers_.count(trigger) == 0) { | 473 if (enabled_triggers_.count(trigger) == 0) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 return enabled_types; | 550 return enabled_types; |
529 } | 551 } |
530 | 552 |
531 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> | 553 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> |
532 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { | 554 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { |
533 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | 555 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, |
534 TriggerType::BROWSER_FOREGROUNDED}; | 556 TriggerType::BROWSER_FOREGROUNDED}; |
535 } | 557 } |
536 | 558 |
537 } // namespace ntp_snippets | 559 } // namespace ntp_snippets |
OLD | NEW |