Chromium Code Reviews| 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/remote_suggestions_scheduler_impl.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h" |
| 6 | 6 |
| 7 #include <random> | 7 #include <random> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/field_trial_params.h" | 13 #include "base/metrics/field_trial_params.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 16 #include "base/time/clock.h" | 16 #include "base/time/clock.h" |
| 17 #include "components/ntp_snippets/features.h" | 17 #include "components/ntp_snippets/features.h" |
| 18 #include "components/ntp_snippets/pref_names.h" | 18 #include "components/ntp_snippets/pref_names.h" |
| 19 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 19 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
| 20 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | 20 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 21 #include "components/ntp_snippets/status.h" | 21 #include "components/ntp_snippets/status.h" |
| 22 #include "components/ntp_snippets/user_classifier.h" | 22 #include "components/ntp_snippets/user_classifier.h" |
| 23 #include "components/prefs/pref_registry_simple.h" | 23 #include "components/prefs/pref_registry_simple.h" |
| 24 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
| 25 #include "net/base/network_change_notifier.h" | |
| 25 | 26 |
| 26 namespace ntp_snippets { | 27 namespace ntp_snippets { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 // The FetchingInterval enum specifies overlapping time intervals that are used | 31 // The FetchingInterval enum specifies overlapping time intervals that are used |
| 31 // for scheduling the next remote suggestion fetch. Therefore a timer is created | 32 // for scheduling the next remote suggestion fetch. Therefore a timer is created |
| 32 // for each interval. Initially all the timers are started at the same time. | 33 // for each interval. Initially all the timers are started at the same time. |
| 33 // Fetches are | 34 // Fetches are only performed when conditions associated with the intervals are |
| 34 // only performed when certain conditions associated with the intervals are | 35 // met: |
| 35 // met. If a fetch failed, then only the corresponding timer is reset. The | 36 // - a "soft" fetch is performed only at a moment when the user actively uses |
| 36 // other timers are not touched. | 37 // Chrome after the interval has elapsed and causes a trigger that is currently |
| 37 // TODO(markusheintz): Describe the individual intervals. | 38 // enabled (a "persistent" fetch is initiated automatically by the OS after the |
|
Marc Treib
2017/04/06 08:29:23
nitty nit: Should the part in parens be its own bu
jkrcal
2017/04/06 12:04:08
Done.
| |
| 39 // interval elapses); | |
| 40 // - a "wifi" fetch is performed only when the user is on a connection that | |
| 41 // the OS classifies as unmetered (a "fallback" fetch happens on any | |
| 42 // connection). | |
| 43 // If a fetch failed, then only the corresponding timer is reset. The other | |
| 44 // timers are not touched. | |
| 38 enum class FetchingInterval { | 45 enum class FetchingInterval { |
| 39 PERSISTENT_FALLBACK, | 46 PERSISTENT_FALLBACK, |
| 40 PERSISTENT_WIFI, | 47 PERSISTENT_WIFI, |
| 41 SOFT_ON_USAGE_EVENT, | 48 SOFT_FALLBACK, |
| 42 SOFT_ON_NTP_OPENED, | 49 SOFT_WIFI, |
| 43 COUNT | 50 COUNT |
| 44 }; | 51 }; |
| 45 | 52 |
| 46 // The following arrays specify default values for remote suggestions fetch | 53 // The following arrays specify default values for remote suggestions fetch |
| 47 // intervals corresponding to individual user classes. The user classes are | 54 // intervals corresponding to individual user classes. The user classes are |
| 48 // defined by the user classifier. There must be an array for each user class. | 55 // defined by the user classifier. There must be an array for each user class. |
| 49 // The values of each array specify a default time interval for the intervals | 56 // The values of each array specify a default time interval for the intervals |
| 50 // defined by the enum FetchingInterval. The default time intervals defined in | 57 // defined by the enum FetchingInterval. The default time intervals defined in |
| 51 // the arrays can be overridden using different variation parameters. | 58 // the arrays can be overridden using different variation parameters. |
| 52 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, | 59 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, |
| 53 6.0}; | 60 6.0}; |
| 54 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0, | 61 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 4.0, |
| 55 2.0}; | 62 2.0}; |
| 56 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { | 63 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { |
| 57 24.0, 6.0, 2.0, 1.0}; | 64 24.0, 6.0, 2.0, 1.0}; |
| 58 | 65 |
| 59 // Variation parameters than can be used to override the default fetching | 66 // Variation parameters than can be used to override the default fetching |
| 60 // intervals. | 67 // intervals. |
| 61 const char* kFetchingIntervalParamNameRareNtpUser[] = { | 68 const char* kFetchingIntervalParamNameRareNtpUser[] = { |
| 62 "fetching_interval_hours-fallback-rare_ntp_user", | 69 "fetching_interval_hours-fallback-rare_ntp_user", |
| 63 "fetching_interval_hours-wifi-rare_ntp_user", | 70 "fetching_interval_hours-wifi-rare_ntp_user", |
| 64 "soft_fetching_interval_hours-active-rare_ntp_user", | 71 "soft_fetching_interval_hours-fallback-rare_ntp_user", |
| 65 "soft_on_ntp_opened_interval_hours-rare_ntp_user"}; | 72 "soft_fetching_interval_hours-wifi-rare_ntp_user"}; |
| 66 const char* kFetchingIntervalParamNameActiveNtpUser[] = { | 73 const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
| 67 "fetching_interval_hours-fallback-active_ntp_user", | 74 "fetching_interval_hours-fallback-active_ntp_user", |
| 68 "fetching_interval_hours-wifi-active_ntp_user", | 75 "fetching_interval_hours-wifi-active_ntp_user", |
| 69 "soft_fetching_interval_hours-active-active_ntp_user", | 76 "soft_fetching_interval_hours-fallback-active_ntp_user", |
| 70 "soft_on_ntp_opened_interval_hours-active_ntp_user"}; | 77 "soft_fetching_interval_hours-wifi-active_ntp_user"}; |
| 71 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { | 78 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
| 72 "fetching_interval_hours-fallback-active_suggestions_consumer", | 79 "fetching_interval_hours-fallback-active_suggestions_consumer", |
| 73 "fetching_interval_hours-wifi-active_suggestions_consumer", | 80 "fetching_interval_hours-wifi-active_suggestions_consumer", |
| 74 "soft_fetching_interval_hours-active-active_suggestions_consumer", | 81 "soft_fetching_interval_hours-fallback-active_suggestions_consumer", |
| 75 "soft_on_ntp_opened_interval_hours-active_suggestions_consumer"}; | 82 "soft_fetching_interval_hours-wifi-active_suggestions_consumer"}; |
| 76 | 83 |
| 77 static_assert( | 84 static_assert( |
| 78 static_cast<unsigned int>(FetchingInterval::COUNT) == | 85 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 79 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && | 86 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && |
| 80 static_cast<unsigned int>(FetchingInterval::COUNT) == | 87 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 81 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && | 88 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && |
| 82 static_cast<unsigned int>(FetchingInterval::COUNT) == | 89 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 83 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && | 90 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && |
| 84 static_cast<unsigned int>(FetchingInterval::COUNT) == | 91 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 85 arraysize(kFetchingIntervalParamNameRareNtpUser) && | 92 arraysize(kFetchingIntervalParamNameRareNtpUser) && |
| 86 static_cast<unsigned int>(FetchingInterval::COUNT) == | 93 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 87 arraysize(kFetchingIntervalParamNameActiveNtpUser) && | 94 arraysize(kFetchingIntervalParamNameActiveNtpUser) && |
| 88 static_cast<unsigned int>(FetchingInterval::COUNT) == | 95 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 89 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), | 96 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), |
| 90 "Fill in all the info for fetching intervals."); | 97 "Fill in all the info for fetching intervals."); |
| 91 | 98 |
| 92 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", | 99 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", |
| 93 "browser_foregrounded", | 100 "browser_foregrounded", |
| 94 "browser_cold_start"}; | 101 "browser_cold_start"}; |
| 95 | 102 |
| 96 const char* kTriggerTypesParamName = "scheduler_trigger_types"; | 103 const char* kTriggerTypesParamName = "scheduler_trigger_types"; |
| 97 const char* kTriggerTypesParamValueForEmptyList = "-"; | 104 const char* kTriggerTypesParamValueForEmptyList = "-"; |
| 98 | 105 |
| 99 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30; | 106 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30; |
| 100 | 107 |
| 108 const char kSnippetSoftFetchingIntervalOnUsageEventDeprecated[] = | |
| 109 "ntp_snippets.soft_fetching_interval_on_usage_event"; | |
| 110 const char kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated[] = | |
| 111 "ntp_snippets.soft_fetching_interval_on_ntp_opened"; | |
| 112 | |
| 101 // Returns the time interval to use for scheduling remote suggestion fetches for | 113 // Returns the time interval to use for scheduling remote suggestion fetches for |
| 102 // the given interval and user_class. | 114 // the given interval and user_class. |
| 103 base::TimeDelta GetDesiredFetchingInterval( | 115 base::TimeDelta GetDesiredFetchingInterval( |
| 104 FetchingInterval interval, | 116 FetchingInterval interval, |
| 105 UserClassifier::UserClass user_class) { | 117 UserClassifier::UserClass user_class) { |
| 106 DCHECK(interval != FetchingInterval::COUNT); | 118 DCHECK(interval != FetchingInterval::COUNT); |
| 107 const unsigned int index = static_cast<unsigned int>(interval); | 119 const unsigned int index = static_cast<unsigned int>(interval); |
| 108 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); | 120 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); |
| 109 | 121 |
| 110 double default_value_hours = 0.0; | 122 double default_value_hours = 0.0; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 RemoteSuggestionsSchedulerImpl::FetchingSchedule | 188 RemoteSuggestionsSchedulerImpl::FetchingSchedule |
| 177 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { | 189 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { |
| 178 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), | 190 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), |
| 179 base::TimeDelta(), base::TimeDelta()}; | 191 base::TimeDelta(), base::TimeDelta()}; |
| 180 } | 192 } |
| 181 | 193 |
| 182 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( | 194 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( |
| 183 const FetchingSchedule& other) const { | 195 const FetchingSchedule& other) const { |
| 184 return interval_persistent_wifi == other.interval_persistent_wifi && | 196 return interval_persistent_wifi == other.interval_persistent_wifi && |
| 185 interval_persistent_fallback == other.interval_persistent_fallback && | 197 interval_persistent_fallback == other.interval_persistent_fallback && |
| 186 interval_soft_on_usage_event == other.interval_soft_on_usage_event && | 198 interval_soft_wifi == other.interval_soft_wifi && |
| 187 interval_soft_on_ntp_opened == other.interval_soft_on_ntp_opened; | 199 interval_soft_fallback == other.interval_soft_fallback; |
| 188 } | 200 } |
| 189 | 201 |
| 190 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( | 202 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( |
| 191 const FetchingSchedule& other) const { | 203 const FetchingSchedule& other) const { |
| 192 return !operator==(other); | 204 return !operator==(other); |
| 193 } | 205 } |
| 194 | 206 |
| 195 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { | 207 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { |
| 196 return interval_persistent_wifi.is_zero() && | 208 return interval_persistent_wifi.is_zero() && |
| 197 interval_persistent_fallback.is_zero() && | 209 interval_persistent_fallback.is_zero() && |
| 198 interval_soft_on_usage_event.is_zero() && | 210 interval_soft_wifi.is_zero() && interval_soft_fallback.is_zero(); |
| 199 interval_soft_on_ntp_opened.is_zero(); | |
| 200 } | 211 } |
| 201 | 212 |
| 202 // The TriggerType enum specifies values for the events that can trigger | 213 // The TriggerType enum specifies values for the events that can trigger |
| 203 // fetching remote suggestions. These values are written to logs. New enum | 214 // fetching remote suggestions. These values are written to logs. New enum |
| 204 // values can be added, but existing enums must never be renumbered or deleted | 215 // values can be added, but existing enums must never be renumbered or deleted |
| 205 // and reused. When adding new entries, also update the array | 216 // and reused. When adding new entries, also update the array |
| 206 // |kTriggerTypeNames| above. | 217 // |kTriggerTypeNames| above. |
| 207 enum class RemoteSuggestionsSchedulerImpl::TriggerType { | 218 enum class RemoteSuggestionsSchedulerImpl::TriggerType { |
| 208 PERSISTENT_SCHEDULER_WAKE_UP = 0, | 219 PERSISTENT_SCHEDULER_WAKE_UP = 0, |
| 209 NTP_OPENED = 1, | 220 NTP_OPENED = 1, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 234 profile_prefs, | 245 profile_prefs, |
| 235 RequestThrottler::RequestType:: | 246 RequestThrottler::RequestType:: |
| 236 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), | 247 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), |
| 237 eula_state_(base::MakeUnique<EulaState>(local_state_prefs, this)), | 248 eula_state_(base::MakeUnique<EulaState>(local_state_prefs, this)), |
| 238 profile_prefs_(profile_prefs), | 249 profile_prefs_(profile_prefs), |
| 239 clock_(std::move(clock)), | 250 clock_(std::move(clock)), |
| 240 enabled_triggers_(GetEnabledTriggerTypes()) { | 251 enabled_triggers_(GetEnabledTriggerTypes()) { |
| 241 DCHECK(user_classifier); | 252 DCHECK(user_classifier); |
| 242 DCHECK(profile_prefs); | 253 DCHECK(profile_prefs); |
| 243 | 254 |
| 255 // Cleanup procedure in M59. Remove for M62. | |
| 256 profile_prefs_->ClearPref(kSnippetSoftFetchingIntervalOnUsageEventDeprecated); | |
| 257 profile_prefs_->ClearPref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated); | |
| 258 | |
| 244 LoadLastFetchingSchedule(); | 259 LoadLastFetchingSchedule(); |
| 245 } | 260 } |
| 246 | 261 |
| 247 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; | 262 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; |
| 248 | 263 |
| 249 // static | 264 // static |
| 250 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( | 265 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( |
| 251 PrefRegistrySimple* registry) { | 266 PrefRegistrySimple* registry) { |
| 252 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); | 267 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); |
| 253 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, | 268 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, |
| 254 0); | 269 0); |
| 255 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 270 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalWifi, 0); |
| 256 0); | 271 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalFallback, 0); |
| 257 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); | 272 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); |
| 258 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnNtpOpened, | 273 |
| 274 // Cleanup procedure in M59. Remove for M62. | |
| 275 registry->RegisterInt64Pref( | |
| 276 kSnippetSoftFetchingIntervalOnUsageEventDeprecated, 0); | |
| 277 registry->RegisterInt64Pref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated, | |
| 259 0); | 278 0); |
| 260 } | 279 } |
| 261 | 280 |
| 262 void RemoteSuggestionsSchedulerImpl::SetProvider( | 281 void RemoteSuggestionsSchedulerImpl::SetProvider( |
| 263 RemoteSuggestionsProvider* provider) { | 282 RemoteSuggestionsProvider* provider) { |
| 264 DCHECK(provider); | 283 DCHECK(provider); |
| 265 provider_ = provider; | 284 provider_ = provider; |
| 266 } | 285 } |
| 267 | 286 |
| 268 void RemoteSuggestionsSchedulerImpl::OnProviderActivated() { | 287 void RemoteSuggestionsSchedulerImpl::OnProviderActivated() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 bool RemoteSuggestionsSchedulerImpl::AcquireQuotaForInteractiveFetch() { | 320 bool RemoteSuggestionsSchedulerImpl::AcquireQuotaForInteractiveFetch() { |
| 302 return AcquireQuota(/*interactive_request=*/true); | 321 return AcquireQuota(/*interactive_request=*/true); |
| 303 } | 322 } |
| 304 | 323 |
| 305 void RemoteSuggestionsSchedulerImpl::OnInteractiveFetchFinished( | 324 void RemoteSuggestionsSchedulerImpl::OnInteractiveFetchFinished( |
| 306 Status fetch_status) { | 325 Status fetch_status) { |
| 307 OnFetchCompleted(fetch_status); | 326 OnFetchCompleted(fetch_status); |
| 308 } | 327 } |
| 309 | 328 |
| 310 void RemoteSuggestionsSchedulerImpl::OnPersistentSchedulerWakeUp() { | 329 void RemoteSuggestionsSchedulerImpl::OnPersistentSchedulerWakeUp() { |
| 311 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); | 330 RefetchInTheBackgroundIfAppropriate( |
| 331 TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); | |
| 312 } | 332 } |
| 313 | 333 |
| 314 void RemoteSuggestionsSchedulerImpl::OnBrowserForegrounded() { | 334 void RemoteSuggestionsSchedulerImpl::OnBrowserForegrounded() { |
| 315 // TODO(jkrcal): Consider that this is called whenever we open or return to an | 335 // TODO(jkrcal): Consider that this is called whenever we open or return to an |
| 316 // Activity. Therefore, keep work light for fast start up calls. | 336 // Activity. Therefore, keep work light for fast start up calls. |
| 317 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_FOREGROUNDED)) { | 337 RefetchInTheBackgroundIfAppropriate(TriggerType::BROWSER_FOREGROUNDED); |
| 318 return; | |
| 319 } | |
| 320 | |
| 321 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED); | |
| 322 } | 338 } |
| 323 | 339 |
| 324 void RemoteSuggestionsSchedulerImpl::OnBrowserColdStart() { | 340 void RemoteSuggestionsSchedulerImpl::OnBrowserColdStart() { |
| 325 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast | 341 // TODO(jkrcal): Consider that work here must be kept light for fast |
| 326 // cold start ups. | 342 // cold start ups. |
| 327 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_COLD_START)) { | 343 RefetchInTheBackgroundIfAppropriate(TriggerType::BROWSER_COLD_START); |
| 328 return; | |
| 329 } | |
| 330 | |
| 331 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START); | |
| 332 } | 344 } |
| 333 | 345 |
| 334 void RemoteSuggestionsSchedulerImpl::OnNTPOpened() { | 346 void RemoteSuggestionsSchedulerImpl::OnNTPOpened() { |
| 335 if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { | 347 // TODO(jkrcal): Consider that this is called whenever we open an NTP. |
| 336 return; | 348 // Therefore, keep work light for fast start up calls. |
| 337 } | 349 RefetchInTheBackgroundIfAppropriate(TriggerType::NTP_OPENED); |
| 338 | |
| 339 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); | |
| 340 } | 350 } |
| 341 | 351 |
| 342 void RemoteSuggestionsSchedulerImpl::StartScheduling() { | 352 void RemoteSuggestionsSchedulerImpl::StartScheduling() { |
| 343 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); | 353 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); |
| 344 | 354 |
| 345 if (schedule_ == new_schedule) { | 355 if (schedule_ == new_schedule) { |
| 346 // Do not schedule if nothing has changed; | 356 // Do not schedule if nothing has changed; |
| 347 return; | 357 return; |
| 348 } | 358 } |
| 349 | 359 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 377 | 387 |
| 378 RemoteSuggestionsSchedulerImpl::FetchingSchedule | 388 RemoteSuggestionsSchedulerImpl::FetchingSchedule |
| 379 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { | 389 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { |
| 380 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); | 390 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); |
| 381 | 391 |
| 382 FetchingSchedule schedule; | 392 FetchingSchedule schedule; |
| 383 schedule.interval_persistent_wifi = | 393 schedule.interval_persistent_wifi = |
| 384 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); | 394 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); |
| 385 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( | 395 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( |
| 386 FetchingInterval::PERSISTENT_FALLBACK, user_class); | 396 FetchingInterval::PERSISTENT_FALLBACK, user_class); |
| 387 schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval( | 397 schedule.interval_soft_wifi = |
| 388 FetchingInterval::SOFT_ON_USAGE_EVENT, user_class); | 398 GetDesiredFetchingInterval(FetchingInterval::SOFT_WIFI, user_class); |
| 389 schedule.interval_soft_on_ntp_opened = GetDesiredFetchingInterval( | 399 schedule.interval_soft_fallback = |
| 390 FetchingInterval::SOFT_ON_NTP_OPENED, user_class); | 400 GetDesiredFetchingInterval(FetchingInterval::SOFT_FALLBACK, user_class); |
| 391 | 401 |
| 392 return schedule; | 402 return schedule; |
| 393 } | 403 } |
| 394 | 404 |
| 395 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { | 405 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { |
| 396 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( | 406 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( |
| 397 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); | 407 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); |
| 398 schedule_.interval_persistent_fallback = | 408 schedule_.interval_persistent_fallback = |
| 399 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( | 409 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( |
| 400 prefs::kSnippetPersistentFetchingIntervalFallback)); | 410 prefs::kSnippetPersistentFetchingIntervalFallback)); |
| 401 schedule_.interval_soft_on_usage_event = | 411 schedule_.interval_soft_wifi = base::TimeDelta::FromInternalValue( |
| 402 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( | 412 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalWifi)); |
| 403 prefs::kSnippetSoftFetchingIntervalOnUsageEvent)); | 413 schedule_.interval_soft_fallback = base::TimeDelta::FromInternalValue( |
| 404 schedule_.interval_soft_on_ntp_opened = base::TimeDelta::FromInternalValue( | 414 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalFallback)); |
| 405 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnNtpOpened)); | |
| 406 } | 415 } |
| 407 | 416 |
| 408 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { | 417 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { |
| 409 profile_prefs_->SetInt64( | 418 profile_prefs_->SetInt64( |
| 410 prefs::kSnippetPersistentFetchingIntervalWifi, | 419 prefs::kSnippetPersistentFetchingIntervalWifi, |
| 411 schedule_.interval_persistent_wifi.ToInternalValue()); | 420 schedule_.interval_persistent_wifi.ToInternalValue()); |
| 412 profile_prefs_->SetInt64( | 421 profile_prefs_->SetInt64( |
| 413 prefs::kSnippetPersistentFetchingIntervalFallback, | 422 prefs::kSnippetPersistentFetchingIntervalFallback, |
| 414 schedule_.interval_persistent_fallback.ToInternalValue()); | 423 schedule_.interval_persistent_fallback.ToInternalValue()); |
| 415 profile_prefs_->SetInt64( | 424 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalWifi, |
| 416 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 425 schedule_.interval_soft_wifi.ToInternalValue()); |
| 417 schedule_.interval_soft_on_usage_event.ToInternalValue()); | 426 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalFallback, |
| 418 profile_prefs_->SetInt64( | 427 schedule_.interval_soft_fallback.ToInternalValue()); |
| 419 prefs::kSnippetSoftFetchingIntervalOnNtpOpened, | |
| 420 schedule_.interval_soft_on_ntp_opened.ToInternalValue()); | |
| 421 } | 428 } |
| 422 | 429 |
| 423 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfEnabled( | 430 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfAppropriate( |
| 424 TriggerType trigger) { | 431 TriggerType trigger) { |
| 432 if (background_fetch_in_progress_) { | |
| 433 return; | |
| 434 } | |
| 435 | |
| 425 if (BackgroundFetchesDisabled(trigger)) { | 436 if (BackgroundFetchesDisabled(trigger)) { |
| 426 return; | 437 return; |
| 427 } | 438 } |
| 428 | 439 |
| 429 UMA_HISTOGRAM_ENUMERATION( | 440 if (trigger != TriggerType::PERSISTENT_SCHEDULER_WAKE_UP && |
| 430 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", | 441 !ShouldRefetchInTheBackgroundNow()) { |
| 431 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); | |
| 432 | |
| 433 RefetchInTheBackground(); | |
| 434 } | |
| 435 | |
| 436 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackground() { | |
| 437 if (background_fetch_in_progress_) { | |
| 438 return; | 442 return; |
| 439 } | 443 } |
| 440 | 444 |
| 441 if (!AcquireQuota(/*interactive_request=*/false)) { | 445 if (!AcquireQuota(/*interactive_request=*/false)) { |
| 442 return; | 446 return; |
| 443 } | 447 } |
| 444 | 448 |
| 449 UMA_HISTOGRAM_ENUMERATION( | |
| 450 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", | |
| 451 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); | |
| 452 | |
| 445 background_fetch_in_progress_ = true; | 453 background_fetch_in_progress_ = true; |
| 446 provider_->RefetchInTheBackground(base::Bind( | 454 provider_->RefetchInTheBackground(base::Bind( |
| 447 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, | 455 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, |
| 448 base::Unretained(this))); | 456 base::Unretained(this))); |
| 449 } | 457 } |
| 450 | 458 |
| 451 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow( | 459 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow() { |
| 452 TriggerType trigger) { | |
| 453 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( | 460 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( |
| 454 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); | 461 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); |
| 455 base::Time first_allowed_fetch_time; | 462 |
| 456 switch (trigger) { | 463 // If we have no persistent scheduler to ask, err on the side of caution. |
| 457 case TriggerType::NTP_OPENED: | 464 bool wifi = false; |
| 458 first_allowed_fetch_time = | 465 if (persistent_scheduler_) { |
| 459 last_fetch_attempt_time + schedule_.interval_soft_on_ntp_opened; | 466 wifi = persistent_scheduler_->IsOnUnmeteredConnection(); |
| 460 break; | |
| 461 case TriggerType::BROWSER_FOREGROUNDED: | |
| 462 case TriggerType::BROWSER_COLD_START: | |
| 463 first_allowed_fetch_time = | |
| 464 last_fetch_attempt_time + schedule_.interval_soft_on_usage_event; | |
| 465 break; | |
| 466 case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP: | |
| 467 case TriggerType::COUNT: | |
| 468 NOTREACHED(); | |
| 469 break; | |
| 470 } | 467 } |
| 468 | |
| 469 base::Time first_allowed_fetch_time = | |
| 470 last_fetch_attempt_time + | |
| 471 (wifi ? schedule_.interval_soft_wifi : schedule_.interval_soft_fallback); | |
| 471 base::Time now = clock_->Now(); | 472 base::Time now = clock_->Now(); |
| 472 | |
| 473 return background_fetches_allowed_after_ <= now && | 473 return background_fetches_allowed_after_ <= now && |
| 474 first_allowed_fetch_time <= now; | 474 first_allowed_fetch_time <= now; |
| 475 } | 475 } |
| 476 | 476 |
| 477 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( | 477 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( |
| 478 TriggerType trigger) const { | 478 TriggerType trigger) const { |
| 479 if (!provider_) { | 479 if (!provider_) { |
| 480 return true; // Cannot fetch as remote suggestions provider does not exist. | 480 return true; // Cannot fetch as remote suggestions provider does not exist. |
| 481 } | 481 } |
| 482 | 482 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 return enabled_types; | 574 return enabled_types; |
| 575 } | 575 } |
| 576 | 576 |
| 577 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> | 577 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> |
| 578 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { | 578 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { |
| 579 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | 579 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, |
| 580 TriggerType::BROWSER_FOREGROUNDED}; | 580 TriggerType::BROWSER_FOREGROUNDED}; |
| 581 } | 581 } |
| 582 | 582 |
| 583 } // namespace ntp_snippets | 583 } // namespace ntp_snippets |
| OLD | NEW |