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