| 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 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
| 25 #include "net/base/network_change_notifier.h" | 25 #include "net/base/network_change_notifier.h" |
| 26 | 26 |
| 27 namespace ntp_snippets { | 27 namespace ntp_snippets { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // The FetchingInterval enum specifies overlapping time intervals that are used | 31 // The FetchingInterval enum specifies overlapping time intervals that are used |
| 32 // 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 |
| 33 // 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. |
| 34 // Fetches are only performed when conditions associated with the intervals are | 34 // A fetch for a given interval is only performed at the moment (after the |
| 35 // met: | 35 // interval has elapsed) when the combination of two conditions associated with |
| 36 // - A "soft" fetch is performed only at a moment when the user actively uses | 36 // the interval is met. |
| 37 // Chrome after the interval has elapsed and causes a trigger that is currently | 37 // 1. Trigger contidion: |
| 38 // enabled while | 38 // - STARTUP_*: the user starts / switches to Chrome; |
| 39 // - a "persistent" fetch is initiated automatically by the OS after the | 39 // - SHOWN_*: the suggestions surface is shown to the user; |
| 40 // interval elapses. | 40 // - PERSISTENT_*: the OS initiates the scheduled fetching task (which has |
| 41 // - A "wifi" fetch is performed only when the user is on a connection that | 41 // been scheduled according to this interval). |
| 42 // the OS classifies as unmetered while | 42 // 2. Connectivity condition: |
| 43 // - a "fallback" fetch happens on any connection. | 43 // - *_WIFI: the user is on a connection that the OS classifies as unmetered; |
| 44 // - *_FALLBACK: holds for any functional internet connection. |
| 45 // |
| 44 // If a fetch failed, then only the corresponding timer is reset. The other | 46 // If a fetch failed, then only the corresponding timer is reset. The other |
| 45 // timers are not touched. | 47 // timers are not touched. |
| 46 enum class FetchingInterval { | 48 enum class FetchingInterval { |
| 47 PERSISTENT_FALLBACK, | 49 PERSISTENT_FALLBACK, |
| 48 PERSISTENT_WIFI, | 50 PERSISTENT_WIFI, |
| 49 SOFT_FALLBACK, | 51 STARTUP_FALLBACK, |
| 50 SOFT_WIFI, | 52 STARTUP_WIFI, |
| 53 SHOWN_FALLBACK, |
| 54 SHOWN_WIFI, |
| 51 COUNT | 55 COUNT |
| 52 }; | 56 }; |
| 53 | 57 |
| 54 // The following arrays specify default values for remote suggestions fetch | 58 // The following arrays specify default values for remote suggestions fetch |
| 55 // intervals corresponding to individual user classes. The user classes are | 59 // intervals corresponding to individual user classes. The user classes are |
| 56 // defined by the user classifier. There must be an array for each user class. | 60 // defined by the user classifier. There must be an array for each user class. |
| 57 // The values of each array specify a default time interval for the intervals | 61 // The values of each array specify a default time interval for the intervals |
| 58 // defined by the enum FetchingInterval. The default time intervals defined in | 62 // defined by the enum FetchingInterval. The default time intervals defined in |
| 59 // the arrays can be overridden using different variation parameters. | 63 // the arrays can be overridden using different variation parameters. |
| 60 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 8.0, | 64 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 48.0, |
| 61 4.0}; | 65 24.0, 8.0, 4.0}; |
| 62 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 8.0, 6.0, | 66 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 8.0, 24.0, |
| 63 3.0}; | 67 8.0, 6.0, 3.0}; |
| 64 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { | 68 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { |
| 65 24.0, 6.0, 2.0, 1.0}; | 69 24.0, 6.0, 24.0, 6.0, 2.0, 1.0}; |
| 66 | 70 |
| 67 // Variation parameters than can be used to override the default fetching | 71 // Variation parameters than can be used to override the default fetching |
| 68 // intervals. | 72 // intervals. For backwards compatibility, we do not rename |
| 73 // - the "fetching_" params (should be "persistent_fetching_"); |
| 74 // - the "soft_" params (should be "shown_"). |
| 69 const char* kFetchingIntervalParamNameRareNtpUser[] = { | 75 const char* kFetchingIntervalParamNameRareNtpUser[] = { |
| 70 "fetching_interval_hours-fallback-rare_ntp_user", | 76 "fetching_interval_hours-fallback-rare_ntp_user", |
| 71 "fetching_interval_hours-wifi-rare_ntp_user", | 77 "fetching_interval_hours-wifi-rare_ntp_user", |
| 78 "startup_fetching_interval_hours-fallback-rare_ntp_user", |
| 79 "startup_fetching_interval_hours-wifi-rare_ntp_user", |
| 72 "soft_fetching_interval_hours-fallback-rare_ntp_user", | 80 "soft_fetching_interval_hours-fallback-rare_ntp_user", |
| 73 "soft_fetching_interval_hours-wifi-rare_ntp_user"}; | 81 "soft_fetching_interval_hours-wifi-rare_ntp_user"}; |
| 74 const char* kFetchingIntervalParamNameActiveNtpUser[] = { | 82 const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
| 75 "fetching_interval_hours-fallback-active_ntp_user", | 83 "fetching_interval_hours-fallback-active_ntp_user", |
| 76 "fetching_interval_hours-wifi-active_ntp_user", | 84 "fetching_interval_hours-wifi-active_ntp_user", |
| 85 "startup_fetching_interval_hours-fallback-active_ntp_user", |
| 86 "startup_fetching_interval_hours-wifi-active_ntp_user", |
| 77 "soft_fetching_interval_hours-fallback-active_ntp_user", | 87 "soft_fetching_interval_hours-fallback-active_ntp_user", |
| 78 "soft_fetching_interval_hours-wifi-active_ntp_user"}; | 88 "soft_fetching_interval_hours-wifi-active_ntp_user"}; |
| 79 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { | 89 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
| 80 "fetching_interval_hours-fallback-active_suggestions_consumer", | 90 "fetching_interval_hours-fallback-active_suggestions_consumer", |
| 81 "fetching_interval_hours-wifi-active_suggestions_consumer", | 91 "fetching_interval_hours-wifi-active_suggestions_consumer", |
| 92 "startup_fetching_interval_hours-fallback-active_suggestions_consumer", |
| 93 "startup_fetching_interval_hours-wifi-active_suggestions_consumer", |
| 82 "soft_fetching_interval_hours-fallback-active_suggestions_consumer", | 94 "soft_fetching_interval_hours-fallback-active_suggestions_consumer", |
| 83 "soft_fetching_interval_hours-wifi-active_suggestions_consumer"}; | 95 "soft_fetching_interval_hours-wifi-active_suggestions_consumer"}; |
| 84 | 96 |
| 85 static_assert( | 97 static_assert( |
| 86 static_cast<unsigned int>(FetchingInterval::COUNT) == | 98 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 87 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && | 99 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && |
| 88 static_cast<unsigned int>(FetchingInterval::COUNT) == | 100 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 89 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && | 101 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && |
| 90 static_cast<unsigned int>(FetchingInterval::COUNT) == | 102 static_cast<unsigned int>(FetchingInterval::COUNT) == |
| 91 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && | 103 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 UMA_HISTOGRAM_CUSTOM_TIMES( | 179 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 168 "NewTabPage.ContentSuggestions.TimeUntilFirstSoftTrigger." | 180 "NewTabPage.ContentSuggestions.TimeUntilFirstSoftTrigger." |
| 169 "ActiveSuggestionsConsumer", | 181 "ActiveSuggestionsConsumer", |
| 170 time_until_first_trigger, base::TimeDelta::FromSeconds(1), | 182 time_until_first_trigger, base::TimeDelta::FromSeconds(1), |
| 171 base::TimeDelta::FromDays(7), | 183 base::TimeDelta::FromDays(7), |
| 172 /*bucket_count=*/50); | 184 /*bucket_count=*/50); |
| 173 break; | 185 break; |
| 174 } | 186 } |
| 175 } | 187 } |
| 176 | 188 |
| 177 void ReportTimeUntilSoftFetch(UserClassifier::UserClass user_class, | 189 void ReportTimeUntilShownFetch(UserClassifier::UserClass user_class, |
| 178 base::TimeDelta time_until_soft_fetch) { | 190 base::TimeDelta time_until_shown_fetch) { |
| 179 switch (user_class) { | 191 switch (user_class) { |
| 180 case UserClassifier::UserClass::RARE_NTP_USER: | 192 case UserClassifier::UserClass::RARE_NTP_USER: |
| 181 UMA_HISTOGRAM_CUSTOM_TIMES( | 193 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 182 "NewTabPage.ContentSuggestions.TimeUntilSoftFetch." | 194 "NewTabPage.ContentSuggestions.TimeUntilSoftFetch." |
| 183 "RareNTPUser", | 195 "RareNTPUser", |
| 184 time_until_soft_fetch, base::TimeDelta::FromSeconds(1), | 196 time_until_shown_fetch, base::TimeDelta::FromSeconds(1), |
| 185 base::TimeDelta::FromDays(7), | 197 base::TimeDelta::FromDays(7), |
| 186 /*bucket_count=*/50); | 198 /*bucket_count=*/50); |
| 187 break; | 199 break; |
| 188 case UserClassifier::UserClass::ACTIVE_NTP_USER: | 200 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 189 UMA_HISTOGRAM_CUSTOM_TIMES( | 201 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 190 "NewTabPage.ContentSuggestions.TimeUntilSoftFetch." | 202 "NewTabPage.ContentSuggestions.TimeUntilSoftFetch." |
| 191 "ActiveNTPUser", | 203 "ActiveNTPUser", |
| 192 time_until_soft_fetch, base::TimeDelta::FromSeconds(1), | 204 time_until_shown_fetch, base::TimeDelta::FromSeconds(1), |
| 193 base::TimeDelta::FromDays(7), | 205 base::TimeDelta::FromDays(7), |
| 194 /*bucket_count=*/50); | 206 /*bucket_count=*/50); |
| 195 break; | 207 break; |
| 196 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 208 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 197 UMA_HISTOGRAM_CUSTOM_TIMES( | 209 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 198 "NewTabPage.ContentSuggestions.TimeUntilSoftFetch." | 210 "NewTabPage.ContentSuggestions.TimeUntilSoftFetch." |
| 199 "ActiveSuggestionsConsumer", | 211 "ActiveSuggestionsConsumer", |
| 200 time_until_soft_fetch, base::TimeDelta::FromSeconds(1), | 212 time_until_shown_fetch, base::TimeDelta::FromSeconds(1), |
| 201 base::TimeDelta::FromDays(7), | 213 base::TimeDelta::FromDays(7), |
| 202 /*bucket_count=*/50); | 214 /*bucket_count=*/50); |
| 203 break; | 215 break; |
| 216 } |
| 217 } |
| 218 |
| 219 void ReportTimeUntilStartupFetch(UserClassifier::UserClass user_class, |
| 220 base::TimeDelta time_until_startup_fetch) { |
| 221 switch (user_class) { |
| 222 case UserClassifier::UserClass::RARE_NTP_USER: |
| 223 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 224 "NewTabPage.ContentSuggestions.TimeUntilStartupFetch." |
| 225 "RareNTPUser", |
| 226 time_until_startup_fetch, base::TimeDelta::FromSeconds(1), |
| 227 base::TimeDelta::FromDays(7), |
| 228 /*bucket_count=*/50); |
| 229 break; |
| 230 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 231 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 232 "NewTabPage.ContentSuggestions.TimeUntilStartupFetch." |
| 233 "ActiveNTPUser", |
| 234 time_until_startup_fetch, base::TimeDelta::FromSeconds(1), |
| 235 base::TimeDelta::FromDays(7), |
| 236 /*bucket_count=*/50); |
| 237 break; |
| 238 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 239 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 240 "NewTabPage.ContentSuggestions.TimeUntilStartupFetch." |
| 241 "ActiveSuggestionsConsumer", |
| 242 time_until_startup_fetch, base::TimeDelta::FromSeconds(1), |
| 243 base::TimeDelta::FromDays(7), |
| 244 /*bucket_count=*/50); |
| 245 break; |
| 204 } | 246 } |
| 205 } | 247 } |
| 206 | 248 |
| 207 void ReportTimeUntilPersistentFetch( | 249 void ReportTimeUntilPersistentFetch( |
| 208 UserClassifier::UserClass user_class, | 250 UserClassifier::UserClass user_class, |
| 209 base::TimeDelta time_until_persistent_fetch) { | 251 base::TimeDelta time_until_persistent_fetch) { |
| 210 switch (user_class) { | 252 switch (user_class) { |
| 211 case UserClassifier::UserClass::RARE_NTP_USER: | 253 case UserClassifier::UserClass::RARE_NTP_USER: |
| 212 UMA_HISTOGRAM_CUSTOM_TIMES( | 254 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 213 "NewTabPage.ContentSuggestions.TimeUntilPersistentFetch." | 255 "NewTabPage.ContentSuggestions.TimeUntilPersistentFetch." |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 RemoteSuggestionsSchedulerImpl::FetchingSchedule | 323 RemoteSuggestionsSchedulerImpl::FetchingSchedule |
| 282 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { | 324 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { |
| 283 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), | 325 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), |
| 284 base::TimeDelta(), base::TimeDelta()}; | 326 base::TimeDelta(), base::TimeDelta()}; |
| 285 } | 327 } |
| 286 | 328 |
| 287 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( | 329 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( |
| 288 const FetchingSchedule& other) const { | 330 const FetchingSchedule& other) const { |
| 289 return interval_persistent_wifi == other.interval_persistent_wifi && | 331 return interval_persistent_wifi == other.interval_persistent_wifi && |
| 290 interval_persistent_fallback == other.interval_persistent_fallback && | 332 interval_persistent_fallback == other.interval_persistent_fallback && |
| 291 interval_soft_wifi == other.interval_soft_wifi && | 333 interval_startup_wifi == other.interval_startup_wifi && |
| 292 interval_soft_fallback == other.interval_soft_fallback; | 334 interval_startup_fallback == other.interval_startup_fallback && |
| 335 interval_shown_wifi == other.interval_shown_wifi && |
| 336 interval_shown_fallback == other.interval_shown_fallback; |
| 293 } | 337 } |
| 294 | 338 |
| 295 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( | 339 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( |
| 296 const FetchingSchedule& other) const { | 340 const FetchingSchedule& other) const { |
| 297 return !operator==(other); | 341 return !operator==(other); |
| 298 } | 342 } |
| 299 | 343 |
| 300 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { | 344 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { |
| 301 return interval_persistent_wifi.is_zero() && | 345 return interval_persistent_wifi.is_zero() && |
| 302 interval_persistent_fallback.is_zero() && | 346 interval_persistent_fallback.is_zero() && |
| 303 interval_soft_wifi.is_zero() && interval_soft_fallback.is_zero(); | 347 interval_startup_wifi.is_zero() && |
| 348 interval_startup_fallback.is_zero() && interval_shown_wifi.is_zero() && |
| 349 interval_shown_fallback.is_zero(); |
| 304 } | 350 } |
| 305 | 351 |
| 306 // The TriggerType enum specifies values for the events that can trigger | 352 // The TriggerType enum specifies values for the events that can trigger |
| 307 // fetching remote suggestions. These values are written to logs. New enum | 353 // fetching remote suggestions. These values are written to logs. New enum |
| 308 // values can be added, but existing enums must never be renumbered or deleted | 354 // values can be added, but existing enums must never be renumbered or deleted |
| 309 // and reused. When adding new entries, also update the array | 355 // and reused. When adding new entries, also update the array |
| 310 // |kTriggerTypeNames| above. | 356 // |kTriggerTypeNames| above. |
| 311 enum class RemoteSuggestionsSchedulerImpl::TriggerType { | 357 enum class RemoteSuggestionsSchedulerImpl::TriggerType { |
| 312 PERSISTENT_SCHEDULER_WAKE_UP = 0, | 358 PERSISTENT_SCHEDULER_WAKE_UP = 0, |
| 313 NTP_OPENED = 1, | 359 NTP_OPENED = 1, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 } | 400 } |
| 355 | 401 |
| 356 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; | 402 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; |
| 357 | 403 |
| 358 // static | 404 // static |
| 359 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( | 405 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( |
| 360 PrefRegistrySimple* registry) { | 406 PrefRegistrySimple* registry) { |
| 361 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); | 407 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); |
| 362 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, | 408 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, |
| 363 0); | 409 0); |
| 364 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalWifi, 0); | 410 registry->RegisterInt64Pref(prefs::kSnippetStartupFetchingIntervalWifi, 0); |
| 365 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalFallback, 0); | 411 registry->RegisterInt64Pref(prefs::kSnippetStartupFetchingIntervalFallback, |
| 412 0); |
| 413 registry->RegisterInt64Pref(prefs::kSnippetShownFetchingIntervalWifi, 0); |
| 414 registry->RegisterInt64Pref(prefs::kSnippetShownFetchingIntervalFallback, 0); |
| 366 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); | 415 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); |
| 367 | 416 |
| 368 // Cleanup procedure in M59. Remove for M62. | 417 // Cleanup procedure in M59. Remove for M62. |
| 369 registry->RegisterInt64Pref( | 418 registry->RegisterInt64Pref( |
| 370 kSnippetSoftFetchingIntervalOnUsageEventDeprecated, 0); | 419 kSnippetSoftFetchingIntervalOnUsageEventDeprecated, 0); |
| 371 registry->RegisterInt64Pref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated, | 420 registry->RegisterInt64Pref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated, |
| 372 0); | 421 0); |
| 373 } | 422 } |
| 374 | 423 |
| 375 void RemoteSuggestionsSchedulerImpl::SetProvider( | 424 void RemoteSuggestionsSchedulerImpl::SetProvider( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 | 530 |
| 482 RemoteSuggestionsSchedulerImpl::FetchingSchedule | 531 RemoteSuggestionsSchedulerImpl::FetchingSchedule |
| 483 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { | 532 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { |
| 484 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); | 533 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); |
| 485 | 534 |
| 486 FetchingSchedule schedule; | 535 FetchingSchedule schedule; |
| 487 schedule.interval_persistent_wifi = | 536 schedule.interval_persistent_wifi = |
| 488 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); | 537 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); |
| 489 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( | 538 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( |
| 490 FetchingInterval::PERSISTENT_FALLBACK, user_class); | 539 FetchingInterval::PERSISTENT_FALLBACK, user_class); |
| 491 schedule.interval_soft_wifi = | 540 schedule.interval_startup_wifi = |
| 492 GetDesiredFetchingInterval(FetchingInterval::SOFT_WIFI, user_class); | 541 GetDesiredFetchingInterval(FetchingInterval::STARTUP_WIFI, user_class); |
| 493 schedule.interval_soft_fallback = | 542 schedule.interval_startup_fallback = GetDesiredFetchingInterval( |
| 494 GetDesiredFetchingInterval(FetchingInterval::SOFT_FALLBACK, user_class); | 543 FetchingInterval::STARTUP_FALLBACK, user_class); |
| 544 schedule.interval_shown_wifi = |
| 545 GetDesiredFetchingInterval(FetchingInterval::SHOWN_WIFI, user_class); |
| 546 schedule.interval_shown_fallback = |
| 547 GetDesiredFetchingInterval(FetchingInterval::SHOWN_FALLBACK, user_class); |
| 495 | 548 |
| 496 return schedule; | 549 return schedule; |
| 497 } | 550 } |
| 498 | 551 |
| 499 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { | 552 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { |
| 500 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( | 553 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( |
| 501 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); | 554 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); |
| 502 schedule_.interval_persistent_fallback = | 555 schedule_.interval_persistent_fallback = |
| 503 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( | 556 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( |
| 504 prefs::kSnippetPersistentFetchingIntervalFallback)); | 557 prefs::kSnippetPersistentFetchingIntervalFallback)); |
| 505 schedule_.interval_soft_wifi = base::TimeDelta::FromInternalValue( | 558 schedule_.interval_startup_wifi = base::TimeDelta::FromInternalValue( |
| 506 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalWifi)); | 559 profile_prefs_->GetInt64(prefs::kSnippetStartupFetchingIntervalWifi)); |
| 507 schedule_.interval_soft_fallback = base::TimeDelta::FromInternalValue( | 560 schedule_.interval_startup_fallback = base::TimeDelta::FromInternalValue( |
| 508 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalFallback)); | 561 profile_prefs_->GetInt64(prefs::kSnippetStartupFetchingIntervalFallback)); |
| 562 schedule_.interval_shown_wifi = base::TimeDelta::FromInternalValue( |
| 563 profile_prefs_->GetInt64(prefs::kSnippetShownFetchingIntervalWifi)); |
| 564 schedule_.interval_shown_fallback = base::TimeDelta::FromInternalValue( |
| 565 profile_prefs_->GetInt64(prefs::kSnippetShownFetchingIntervalFallback)); |
| 509 } | 566 } |
| 510 | 567 |
| 511 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { | 568 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { |
| 512 profile_prefs_->SetInt64( | 569 profile_prefs_->SetInt64( |
| 513 prefs::kSnippetPersistentFetchingIntervalWifi, | 570 prefs::kSnippetPersistentFetchingIntervalWifi, |
| 514 schedule_.interval_persistent_wifi.ToInternalValue()); | 571 schedule_.interval_persistent_wifi.ToInternalValue()); |
| 515 profile_prefs_->SetInt64( | 572 profile_prefs_->SetInt64( |
| 516 prefs::kSnippetPersistentFetchingIntervalFallback, | 573 prefs::kSnippetPersistentFetchingIntervalFallback, |
| 517 schedule_.interval_persistent_fallback.ToInternalValue()); | 574 schedule_.interval_persistent_fallback.ToInternalValue()); |
| 518 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalWifi, | 575 profile_prefs_->SetInt64(prefs::kSnippetStartupFetchingIntervalWifi, |
| 519 schedule_.interval_soft_wifi.ToInternalValue()); | 576 schedule_.interval_startup_wifi.ToInternalValue()); |
| 520 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalFallback, | 577 profile_prefs_->SetInt64( |
| 521 schedule_.interval_soft_fallback.ToInternalValue()); | 578 prefs::kSnippetStartupFetchingIntervalFallback, |
| 579 schedule_.interval_startup_fallback.ToInternalValue()); |
| 580 profile_prefs_->SetInt64(prefs::kSnippetShownFetchingIntervalWifi, |
| 581 schedule_.interval_shown_wifi.ToInternalValue()); |
| 582 profile_prefs_->SetInt64(prefs::kSnippetShownFetchingIntervalFallback, |
| 583 schedule_.interval_shown_fallback.ToInternalValue()); |
| 522 } | 584 } |
| 523 | 585 |
| 524 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfAppropriate( | 586 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfAppropriate( |
| 525 TriggerType trigger) { | 587 TriggerType trigger) { |
| 526 if (background_fetch_in_progress_) { | 588 if (background_fetch_in_progress_) { |
| 527 return; | 589 return; |
| 528 } | 590 } |
| 529 | 591 |
| 530 if (BackgroundFetchesDisabled(trigger)) { | 592 if (BackgroundFetchesDisabled(trigger)) { |
| 531 return; | 593 return; |
| 532 } | 594 } |
| 533 | 595 |
| 534 bool is_soft = trigger != TriggerType::PERSISTENT_SCHEDULER_WAKE_UP; | 596 bool is_soft = trigger != TriggerType::PERSISTENT_SCHEDULER_WAKE_UP; |
| 535 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( | 597 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( |
| 536 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); | 598 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); |
| 537 | 599 |
| 538 if (is_soft && !time_until_first_trigger_reported_) { | 600 if (is_soft && !time_until_first_trigger_reported_) { |
| 539 time_until_first_trigger_reported_ = true; | 601 time_until_first_trigger_reported_ = true; |
| 540 ReportTimeUntilFirstSoftTrigger(user_classifier_->GetUserClass(), | 602 ReportTimeUntilFirstSoftTrigger(user_classifier_->GetUserClass(), |
| 541 clock_->Now() - last_fetch_attempt_time); | 603 clock_->Now() - last_fetch_attempt_time); |
| 542 } | 604 } |
| 543 | 605 |
| 544 if (is_soft && !ShouldRefetchInTheBackgroundNow(last_fetch_attempt_time)) { | 606 if (is_soft && |
| 607 !ShouldRefetchInTheBackgroundNow(last_fetch_attempt_time, trigger)) { |
| 545 return; | 608 return; |
| 546 } | 609 } |
| 547 | 610 |
| 548 if (!AcquireQuota(/*interactive_request=*/false)) { | 611 if (!AcquireQuota(/*interactive_request=*/false)) { |
| 549 return; | 612 return; |
| 550 } | 613 } |
| 551 | 614 |
| 552 if (is_soft) { | 615 base::TimeDelta diff = clock_->Now() - last_fetch_attempt_time; |
| 553 ReportTimeUntilSoftFetch(user_classifier_->GetUserClass(), | 616 switch (trigger) { |
| 554 clock_->Now() - last_fetch_attempt_time); | 617 case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP: |
| 555 } else { | 618 ReportTimeUntilPersistentFetch(user_classifier_->GetUserClass(), diff); |
| 556 ReportTimeUntilPersistentFetch(user_classifier_->GetUserClass(), | 619 break; |
| 557 clock_->Now() - last_fetch_attempt_time); | 620 case TriggerType::NTP_OPENED: |
| 621 ReportTimeUntilShownFetch(user_classifier_->GetUserClass(), diff); |
| 622 break; |
| 623 case TriggerType::BROWSER_FOREGROUNDED: |
| 624 case TriggerType::BROWSER_COLD_START: |
| 625 ReportTimeUntilStartupFetch(user_classifier_->GetUserClass(), diff); |
| 626 break; |
| 627 case TriggerType::COUNT: |
| 628 NOTREACHED(); |
| 558 } | 629 } |
| 559 | 630 |
| 560 UMA_HISTOGRAM_ENUMERATION( | 631 UMA_HISTOGRAM_ENUMERATION( |
| 561 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", | 632 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", |
| 562 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); | 633 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); |
| 563 | 634 |
| 564 background_fetch_in_progress_ = true; | 635 background_fetch_in_progress_ = true; |
| 565 provider_->RefetchInTheBackground(base::Bind( | 636 provider_->RefetchInTheBackground(base::Bind( |
| 566 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, | 637 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, |
| 567 base::Unretained(this))); | 638 base::Unretained(this))); |
| 568 } | 639 } |
| 569 | 640 |
| 570 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow( | 641 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow( |
| 571 base::Time last_fetch_attempt_time) { | 642 base::Time last_fetch_attempt_time, |
| 643 TriggerType trigger) { |
| 572 // If we have no persistent scheduler to ask, err on the side of caution. | 644 // If we have no persistent scheduler to ask, err on the side of caution. |
| 573 bool wifi = false; | 645 bool wifi = false; |
| 574 if (persistent_scheduler_) { | 646 if (persistent_scheduler_) { |
| 575 wifi = persistent_scheduler_->IsOnUnmeteredConnection(); | 647 wifi = persistent_scheduler_->IsOnUnmeteredConnection(); |
| 576 } | 648 } |
| 577 | 649 |
| 578 base::Time first_allowed_fetch_time = | 650 base::Time first_allowed_fetch_time = last_fetch_attempt_time; |
| 579 last_fetch_attempt_time + | 651 if (trigger == TriggerType::NTP_OPENED) { |
| 580 (wifi ? schedule_.interval_soft_wifi : schedule_.interval_soft_fallback); | 652 first_allowed_fetch_time += (wifi ? schedule_.interval_shown_wifi |
| 653 : schedule_.interval_shown_fallback); |
| 654 } else { |
| 655 first_allowed_fetch_time += (wifi ? schedule_.interval_startup_wifi |
| 656 : schedule_.interval_startup_fallback); |
| 657 } |
| 658 |
| 581 base::Time now = clock_->Now(); | 659 base::Time now = clock_->Now(); |
| 582 return background_fetches_allowed_after_ <= now && | 660 return background_fetches_allowed_after_ <= now && |
| 583 first_allowed_fetch_time <= now; | 661 first_allowed_fetch_time <= now; |
| 584 } | 662 } |
| 585 | 663 |
| 586 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( | 664 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( |
| 587 TriggerType trigger) const { | 665 TriggerType trigger) const { |
| 588 if (!provider_) { | 666 if (!provider_) { |
| 589 return true; // Cannot fetch as remote suggestions provider does not exist. | 667 return true; // Cannot fetch as remote suggestions provider does not exist. |
| 590 } | 668 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 683 } | 761 } |
| 684 return enabled_types; | 762 return enabled_types; |
| 685 } | 763 } |
| 686 | 764 |
| 687 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> | 765 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> |
| 688 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { | 766 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { |
| 689 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED}; | 767 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED}; |
| 690 } | 768 } |
| 691 | 769 |
| 692 } // namespace ntp_snippets | 770 } // namespace ntp_snippets |
| OLD | NEW |