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/scheduling_remote_suggestions_provider. h" | 5 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider. h" |
| 6 | 6 |
| 7 #include <random> | |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "components/ntp_snippets/features.h" | 12 #include "components/ntp_snippets/features.h" |
| 12 #include "components/ntp_snippets/pref_names.h" | 13 #include "components/ntp_snippets/pref_names.h" |
| 13 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 14 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
| 14 #include "components/ntp_snippets/status.h" | 15 #include "components/ntp_snippets/status.h" |
| 15 #include "components/ntp_snippets/user_classifier.h" | 16 #include "components/ntp_snippets/user_classifier.h" |
| 16 #include "components/prefs/pref_registry_simple.h" | 17 #include "components/prefs/pref_registry_simple.h" |
| 17 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
| 18 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
| 19 | 20 |
| 20 namespace ntp_snippets { | 21 namespace ntp_snippets { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 25 enum class FetchingInterval { | |
| 26 BACKGROUND_FALLBACK, | |
| 27 BACKGROUND_WIFI, | |
| 28 SOFT_ACTIVE, | |
|
tschumann
2017/01/03 14:35:04
hmm... what about: 'SOFT_ON_USAGE_EVENT' or just '
jkrcal
2017/01/03 18:36:04
Done.
| |
| 29 MAX | |
| 30 }; | |
| 31 | |
| 24 // Default values for fetching intervals, fallback and wifi. | 32 // Default values for fetching intervals, fallback and wifi. |
| 25 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0}; | 33 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0, 12.0}; |
| 26 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0}; | 34 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0, 2.0}; |
| 27 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0}; | 35 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0, |
| 36 2.0}; | |
| 28 | 37 |
| 29 // Variation parameters than can the default fetching intervals. | 38 // Variation parameters than can the default fetching intervals. |
| 30 const char* kFetchingIntervalParamNameRareNtpUser[] = { | 39 const char* kFetchingIntervalParamNameRareNtpUser[] = { |
| 31 "fetching_interval_hours-fallback-rare_ntp_user", | 40 "fetching_interval_hours-fallback-rare_ntp_user", |
| 32 "fetching_interval_hours-wifi-rare_ntp_user"}; | 41 "fetching_interval_hours-wifi-rare_ntp_user", |
| 42 "soft_fetching_interval_hours-active-rare_ntp_user"}; | |
| 33 const char* kFetchingIntervalParamNameActiveNtpUser[] = { | 43 const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
| 34 "fetching_interval_hours-fallback-active_ntp_user", | 44 "fetching_interval_hours-fallback-active_ntp_user", |
| 35 "fetching_interval_hours-wifi-active_ntp_user"}; | 45 "fetching_interval_hours-wifi-active_ntp_user", |
| 46 "soft_fetching_interval_hours-active-active_ntp_user"}; | |
| 36 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { | 47 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
| 37 "fetching_interval_hours-fallback-active_suggestions_consumer", | 48 "fetching_interval_hours-fallback-active_suggestions_consumer", |
| 38 "fetching_interval_hours-wifi-active_suggestions_consumer"}; | 49 "fetching_interval_hours-wifi-active_suggestions_consumer", |
| 50 "soft_fetching_interval_hours-active-active_suggestions_consumer"}; | |
| 39 | 51 |
| 40 base::TimeDelta GetDesiredUpdateInterval( | 52 // Default value for the maximum value of the random jitter that is added to the |
| 41 bool is_wifi, | 53 // soft fetching interval (the minimum value is 0.0). |
| 54 const double kDefaultSoftFetchingJitterInHours = 0.5; | |
| 55 const char* kSoftFetchingJitterParamName = | |
| 56 "soft_fetching_interval_jitter_hours"; | |
| 57 | |
| 58 base::TimeDelta GetDesiredFetchingInterval( | |
| 59 FetchingInterval interval, | |
| 42 UserClassifier::UserClass user_class) { | 60 UserClassifier::UserClass user_class) { |
| 43 double default_value_hours = 0.0; | 61 double default_value_hours = 0.0; |
| 44 | 62 |
| 45 const int index = is_wifi ? 1 : 0; | 63 const int index = static_cast<int>(interval); |
| 64 DCHECK(index >=0 && index < static_cast<int>(FetchingInterval::MAX)); | |
|
tschumann
2017/01/03 14:35:04
hmm... i guess what you really want to check is:
D
jkrcal
2017/01/03 18:36:04
Done.
| |
| 65 | |
| 46 const char* param_name = nullptr; | 66 const char* param_name = nullptr; |
| 47 switch (user_class) { | 67 switch (user_class) { |
| 48 case UserClassifier::UserClass::RARE_NTP_USER: | 68 case UserClassifier::UserClass::RARE_NTP_USER: |
| 49 default_value_hours = kDefaultFetchingIntervalRareNtpUser[index]; | 69 default_value_hours = kDefaultFetchingIntervalRareNtpUser[index]; |
| 50 param_name = kFetchingIntervalParamNameRareNtpUser[index]; | 70 param_name = kFetchingIntervalParamNameRareNtpUser[index]; |
| 51 break; | 71 break; |
| 52 case UserClassifier::UserClass::ACTIVE_NTP_USER: | 72 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 53 default_value_hours = kDefaultFetchingIntervalActiveNtpUser[index]; | 73 default_value_hours = kDefaultFetchingIntervalActiveNtpUser[index]; |
| 54 param_name = kFetchingIntervalParamNameActiveNtpUser[index]; | 74 param_name = kFetchingIntervalParamNameActiveNtpUser[index]; |
| 55 break; | 75 break; |
| 56 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 76 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 57 default_value_hours = | 77 default_value_hours = |
| 58 kDefaultFetchingIntervalActiveSuggestionsConsumer[index]; | 78 kDefaultFetchingIntervalActiveSuggestionsConsumer[index]; |
| 59 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index]; | 79 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index]; |
| 60 break; | 80 break; |
| 61 } | 81 } |
| 62 | 82 |
| 63 double value_hours = variations::GetVariationParamByFeatureAsDouble( | 83 double value_hours = variations::GetVariationParamByFeatureAsDouble( |
| 64 ntp_snippets::kArticleSuggestionsFeature, param_name, | 84 ntp_snippets::kArticleSuggestionsFeature, param_name, |
| 65 default_value_hours); | 85 default_value_hours); |
| 66 | 86 |
| 67 return base::TimeDelta::FromSecondsD(value_hours * 3600.0); | 87 return base::TimeDelta::FromSecondsD(value_hours * 3600.0); |
| 68 } | 88 } |
| 69 | 89 |
| 90 base::TimeDelta GetDesiredFetchingJitter() { | |
| 91 double value_hours = variations::GetVariationParamByFeatureAsDouble( | |
| 92 ntp_snippets::kArticleSuggestionsFeature, | |
| 93 kSoftFetchingJitterParamName, kDefaultSoftFetchingJitterInHours); | |
| 94 | |
| 95 return base::TimeDelta::FromSecondsD(value_hours * 3600.0); | |
| 96 } | |
| 97 | |
| 70 } // namespace | 98 } // namespace |
| 71 | 99 |
| 72 struct SchedulingRemoteSuggestionsProvider::FetchingSchedule { | 100 struct SchedulingRemoteSuggestionsProvider::FetchingSchedule { |
| 73 base::TimeDelta interval_wifi; | 101 base::TimeDelta interval_wifi; |
| 74 base::TimeDelta interval_fallback; | 102 base::TimeDelta interval_fallback; |
| 103 base::TimeDelta soft_interval_active; | |
| 75 | 104 |
| 76 static FetchingSchedule Empty() { | 105 static FetchingSchedule Empty() { |
| 77 return FetchingSchedule{base::TimeDelta(), | 106 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), |
| 78 base::TimeDelta()}; | 107 base::TimeDelta()}; |
| 79 } | 108 } |
| 80 | 109 |
| 81 bool operator==(const FetchingSchedule& other) const { | 110 bool operator==(const FetchingSchedule& other) const { |
| 82 return interval_wifi == other.interval_wifi && | 111 return interval_wifi == other.interval_wifi && |
| 83 interval_fallback == other.interval_fallback; | 112 interval_fallback == other.interval_fallback && |
| 113 soft_interval_active == other.soft_interval_active; | |
| 84 } | 114 } |
| 85 | 115 |
| 86 bool operator!=(const FetchingSchedule& other) const { | 116 bool operator!=(const FetchingSchedule& other) const { |
| 87 return !operator==(other); | 117 return !operator==(other); |
| 88 } | 118 } |
| 89 | 119 |
| 90 bool is_empty() const { | 120 bool is_empty() const { |
| 91 return interval_wifi.is_zero() && interval_fallback.is_zero(); | 121 return interval_wifi.is_zero() && interval_fallback.is_zero() && |
| 122 soft_interval_active.is_zero(); | |
| 92 } | 123 } |
| 93 }; | 124 }; |
| 94 | 125 |
| 95 SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider( | 126 SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider( |
| 96 Observer* observer, | 127 Observer* observer, |
| 97 std::unique_ptr<RemoteSuggestionsProvider> provider, | 128 std::unique_ptr<RemoteSuggestionsProvider> provider, |
| 98 PersistentScheduler* persistent_scheduler, | 129 PersistentScheduler* persistent_scheduler, |
| 99 const UserClassifier* user_classifier, | 130 const UserClassifier* user_classifier, |
| 100 PrefService* pref_service) | 131 PrefService* pref_service) |
| 101 : RemoteSuggestionsProvider(observer), | 132 : RemoteSuggestionsProvider(observer), |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 125 0); | 156 0); |
| 126 } | 157 } |
| 127 | 158 |
| 128 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { | 159 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { |
| 129 // Force the reschedule by stopping and starting it again. | 160 // Force the reschedule by stopping and starting it again. |
| 130 StopScheduling(); | 161 StopScheduling(); |
| 131 StartScheduling(); | 162 StartScheduling(); |
| 132 } | 163 } |
| 133 | 164 |
| 134 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { | 165 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { |
| 135 provider_->RefetchInTheBackground( | 166 RefetchInTheBackground(nullptr); |
|
tschumann
2017/01/03 15:16:13
please add /*callback=*/nullptr
to document the pa
jkrcal
2017/01/03 18:36:04
Done.
| |
| 136 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>( | |
| 137 base::Bind(&SchedulingRemoteSuggestionsProvider::OnFetchCompleted, | |
| 138 base::Unretained(this)))); | |
| 139 } | 167 } |
| 140 | 168 |
| 141 void SchedulingRemoteSuggestionsProvider::OnBrowserStartup() { | 169 void SchedulingRemoteSuggestionsProvider::OnBrowserStartup() { |
| 142 // TODO(jkrcal): Implement. | 170 // TODO(jkrcal): Implement. |
| 143 } | 171 } |
| 144 | 172 |
| 145 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { | 173 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { |
| 146 // TODO(jkrcal): Implement. | 174 if (!ShouldRefetchInTheBackgroundNow()) { |
| 175 return; | |
| 176 } | |
| 177 RefetchInTheBackground(nullptr); | |
| 147 } | 178 } |
| 148 | 179 |
| 149 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( | 180 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( |
| 150 std::unique_ptr<ProviderStatusCallback> callback) { | 181 std::unique_ptr<ProviderStatusCallback> callback) { |
| 151 provider_->SetProviderStatusCallback(std::move(callback)); | 182 provider_->SetProviderStatusCallback(std::move(callback)); |
| 152 } | 183 } |
| 153 | 184 |
| 154 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( | 185 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( |
| 155 std::unique_ptr<FetchStatusCallback> callback) { | 186 std::unique_ptr<FetchStatusCallback> callback) { |
| 156 provider_->RefetchInTheBackground(std::move(callback)); | 187 if (background_fetch_in_progress_) { |
|
jkrcal
2017/01/03 14:02:09
This was a previous bug that OnFetchCompleted() (n
| |
| 188 if (callback) { | |
| 189 callback->Run( | |
| 190 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress")); | |
| 191 } | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind( | |
| 196 &SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished, | |
| 197 base::Unretained(this), base::Passed(&callback)); | |
| 198 provider_->RefetchInTheBackground( | |
| 199 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>( | |
| 200 std::move(wrapper_callback))); | |
| 157 } | 201 } |
| 158 | 202 |
| 159 const NTPSnippetsFetcher* SchedulingRemoteSuggestionsProvider:: | 203 const NTPSnippetsFetcher* SchedulingRemoteSuggestionsProvider:: |
| 160 snippets_fetcher_for_testing_and_debugging() const { | 204 snippets_fetcher_for_testing_and_debugging() const { |
| 161 return provider_->snippets_fetcher_for_testing_and_debugging(); | 205 return provider_->snippets_fetcher_for_testing_and_debugging(); |
| 162 } | 206 } |
| 163 | 207 |
| 164 CategoryStatus SchedulingRemoteSuggestionsProvider::GetCategoryStatus( | 208 CategoryStatus SchedulingRemoteSuggestionsProvider::GetCategoryStatus( |
| 165 Category category) { | 209 Category category) { |
| 166 return provider_->GetCategoryStatus(category); | 210 return provider_->GetCategoryStatus(category); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 StartScheduling(); | 274 StartScheduling(); |
| 231 return; | 275 return; |
| 232 case RemoteSuggestionsProvider::ProviderStatus::INACTIVE: | 276 case RemoteSuggestionsProvider::ProviderStatus::INACTIVE: |
| 233 StopScheduling(); | 277 StopScheduling(); |
| 234 return; | 278 return; |
| 235 } | 279 } |
| 236 NOTREACHED(); | 280 NOTREACHED(); |
| 237 } | 281 } |
| 238 | 282 |
| 239 void SchedulingRemoteSuggestionsProvider::StartScheduling() { | 283 void SchedulingRemoteSuggestionsProvider::StartScheduling() { |
| 240 // The scheduler only exists on Android so far, it's null on other platforms. | |
| 241 if (!persistent_scheduler_) { | |
| 242 return; | |
| 243 } | |
| 244 | |
| 245 FetchingSchedule last_schedule = GetLastFetchingSchedule(); | 284 FetchingSchedule last_schedule = GetLastFetchingSchedule(); |
| 246 FetchingSchedule schedule = GetDesiredFetchingSchedule(); | 285 FetchingSchedule schedule = GetDesiredFetchingSchedule(); |
| 247 | 286 |
| 248 // Reset the schedule only if the parameters have changed. | 287 // Reset the schedule only if the parameters have changed. |
| 249 if (last_schedule != schedule) { | 288 if (last_schedule != schedule) { |
| 250 ApplyFetchingSchedule(schedule); | 289 ApplyFetchingSchedule(schedule, /*also_apply_persistent_schedule=*/true); |
| 251 } | 290 } |
| 252 } | 291 } |
| 253 | 292 |
| 254 void SchedulingRemoteSuggestionsProvider::StopScheduling() { | 293 void SchedulingRemoteSuggestionsProvider::StopScheduling() { |
| 255 // The scheduler only exists on Android so far, it's null on other platforms. | |
| 256 if (!persistent_scheduler_) { | |
| 257 return; | |
| 258 } | |
| 259 | |
| 260 // Do not unschedule if already switched off | 294 // Do not unschedule if already switched off |
| 261 FetchingSchedule last_schedule = GetLastFetchingSchedule(); | 295 FetchingSchedule last_schedule = GetLastFetchingSchedule(); |
| 262 if (last_schedule.is_empty()) { | 296 if (last_schedule.is_empty()) { |
| 263 return; | 297 return; |
| 264 } | 298 } |
| 265 | 299 |
| 266 persistent_scheduler_->Unschedule(); | 300 pref_service_->ClearPref(prefs::kSnippetNextActiveSoftFetch); |
| 301 | |
| 302 // The scheduler only exists on Android so far, it's null on other platforms. | |
| 303 if (persistent_scheduler_) { | |
| 304 persistent_scheduler_->Unschedule(); | |
| 305 } | |
| 267 | 306 |
| 268 StoreLastFetchingSchedule(FetchingSchedule::Empty()); | 307 StoreLastFetchingSchedule(FetchingSchedule::Empty()); |
| 269 } | 308 } |
| 270 | 309 |
| 271 void SchedulingRemoteSuggestionsProvider::ApplyFetchingSchedule( | 310 void SchedulingRemoteSuggestionsProvider::ApplyFetchingSchedule( |
| 272 const FetchingSchedule& schedule) { | 311 const FetchingSchedule& schedule, |
| 273 persistent_scheduler_->Schedule(schedule.interval_wifi, | 312 bool also_apply_persistent_schedule) { |
| 274 schedule.interval_fallback); | 313 |
| 314 std::default_random_engine generator; | |
| 315 std::uniform_real_distribution<double> distribution(0.0,1.0); | |
| 316 // generates a random double in the range 0.0 .. 1.0 | |
| 317 double random_factor = distribution(generator); | |
| 318 | |
| 319 base::Time next_active_soft_fetch = | |
| 320 base::Time::Now() + schedule.soft_interval_active + | |
| 321 (GetDesiredFetchingJitter() * random_factor); | |
| 322 pref_service_->SetInt64(prefs::kSnippetNextActiveSoftFetch, | |
| 323 next_active_soft_fetch.ToInternalValue()); | |
| 324 | |
| 325 // The scheduler only exists on Android so far, it's null on other platforms. | |
| 326 if (persistent_scheduler_ && also_apply_persistent_schedule) { | |
| 327 persistent_scheduler_->Schedule(schedule.interval_wifi, | |
| 328 schedule.interval_fallback); | |
| 329 } | |
| 275 | 330 |
| 276 StoreLastFetchingSchedule(schedule); | 331 StoreLastFetchingSchedule(schedule); |
| 277 } | 332 } |
| 278 | 333 |
| 279 SchedulingRemoteSuggestionsProvider::FetchingSchedule | 334 SchedulingRemoteSuggestionsProvider::FetchingSchedule |
| 280 SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const { | 335 SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const { |
| 281 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); | 336 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); |
| 282 | 337 |
| 283 FetchingSchedule schedule; | 338 FetchingSchedule schedule; |
| 284 schedule.interval_wifi = | 339 schedule.interval_wifi = |
| 285 GetDesiredUpdateInterval(/*is_wifi=*/true, user_class); | 340 GetDesiredFetchingInterval(FetchingInterval::BACKGROUND_WIFI, user_class); |
| 286 schedule.interval_fallback = | 341 schedule.interval_fallback = GetDesiredFetchingInterval( |
| 287 GetDesiredUpdateInterval(/*is_wifi=*/false, user_class); | 342 FetchingInterval::BACKGROUND_FALLBACK, user_class); |
| 343 schedule.soft_interval_active = | |
| 344 GetDesiredFetchingInterval(FetchingInterval::SOFT_ACTIVE, user_class); | |
| 345 | |
| 288 return schedule; | 346 return schedule; |
| 289 } | 347 } |
| 290 | 348 |
| 291 SchedulingRemoteSuggestionsProvider::FetchingSchedule | 349 SchedulingRemoteSuggestionsProvider::FetchingSchedule |
| 292 SchedulingRemoteSuggestionsProvider::GetLastFetchingSchedule() const { | 350 SchedulingRemoteSuggestionsProvider::GetLastFetchingSchedule() const { |
| 293 FetchingSchedule schedule; | 351 FetchingSchedule schedule; |
| 294 schedule.interval_wifi = base::TimeDelta::FromInternalValue( | 352 schedule.interval_wifi = base::TimeDelta::FromInternalValue( |
| 295 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi)); | 353 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi)); |
| 296 schedule.interval_fallback = | 354 schedule.interval_fallback = |
| 297 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( | 355 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( |
| 298 prefs::kSnippetBackgroundFetchingIntervalFallback)); | 356 prefs::kSnippetBackgroundFetchingIntervalFallback)); |
| 357 schedule.soft_interval_active = base::TimeDelta::FromInternalValue( | |
| 358 pref_service_->GetInt64(prefs::kSnippetSoftFetchingIntervalActive)); | |
| 359 | |
| 299 return schedule; | 360 return schedule; |
| 300 } | 361 } |
| 301 | 362 |
| 302 void SchedulingRemoteSuggestionsProvider::StoreLastFetchingSchedule( | 363 void SchedulingRemoteSuggestionsProvider::StoreLastFetchingSchedule( |
| 303 const FetchingSchedule& schedule) { | 364 const FetchingSchedule& schedule) { |
| 304 pref_service_->SetInt64( | 365 pref_service_->SetInt64( |
| 305 prefs::kSnippetBackgroundFetchingIntervalWifi, | 366 prefs::kSnippetBackgroundFetchingIntervalWifi, |
| 306 schedule.interval_wifi.ToInternalValue()); | 367 schedule.interval_wifi.ToInternalValue()); |
| 307 pref_service_->SetInt64( | 368 pref_service_->SetInt64( |
| 308 prefs::kSnippetBackgroundFetchingIntervalFallback, | 369 prefs::kSnippetBackgroundFetchingIntervalFallback, |
| 309 schedule.interval_fallback.ToInternalValue()); | 370 schedule.interval_fallback.ToInternalValue()); |
| 371 pref_service_->SetInt64( | |
| 372 prefs::kSnippetSoftFetchingIntervalActive, | |
| 373 schedule.soft_interval_active.ToInternalValue()); | |
| 374 } | |
| 375 | |
| 376 bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow() { | |
| 377 if (!pref_service_->HasPrefPath(prefs::kSnippetNextActiveSoftFetch)) { | |
|
tschumann
2017/01/03 15:16:13
i guess this checks whether the path has been regi
jkrcal
2017/01/03 18:36:04
No, a path should always be registered (I've forgo
tschumann
2017/01/03 18:54:24
Ah, got it -- that's what ClearPref() is doing :-)
jkrcal
2017/01/04 10:06:30
I fixed it, in the end.
| |
| 378 // Soft background fetches are switched off. | |
| 379 return false; | |
| 380 } | |
| 381 | |
| 382 base::Time next_active_soft_fetch = base::Time::FromInternalValue( | |
| 383 pref_service_->GetInt64(prefs::kSnippetNextActiveSoftFetch)); | |
| 384 return next_active_soft_fetch <= base::Time::Now(); | |
| 310 } | 385 } |
| 311 | 386 |
| 312 void SchedulingRemoteSuggestionsProvider::FetchFinished( | 387 void SchedulingRemoteSuggestionsProvider::FetchFinished( |
| 313 const FetchDoneCallback& callback, | 388 const FetchDoneCallback& callback, |
| 314 Status status_code, | 389 Status fetch_status, |
| 315 std::vector<ContentSuggestion> suggestions) { | 390 std::vector<ContentSuggestion> suggestions) { |
| 316 OnFetchCompleted(status_code); | 391 // Reschedule after a fetch. The persistent schedule is applied only after a |
| 317 callback.Run(status_code, std::move(suggestions)); | 392 // successful fetch. After a failed fetch, we want to keep the previous |
| 393 // persistent schedule intact so that we eventually get a persistent fallback | |
| 394 // fetch (if the wifi persistent fetches keep failing). | |
| 395 ApplyFetchingSchedule(GetLastFetchingSchedule(), | |
| 396 /*also_apply_persistent_schedule=*/fetch_status.code == | |
| 397 StatusCode::SUCCESS); | |
| 398 | |
| 399 callback.Run(fetch_status, std::move(suggestions)); | |
| 318 } | 400 } |
| 319 | 401 |
| 320 void SchedulingRemoteSuggestionsProvider::OnFetchCompleted( | 402 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished( |
| 403 std::unique_ptr<FetchStatusCallback> callback, | |
| 321 Status fetch_status) { | 404 Status fetch_status) { |
| 322 // The scheduler only exists on Android so far, it's null on other platforms. | 405 background_fetch_in_progress_ = false; |
| 323 if (!persistent_scheduler_) { | 406 |
| 324 return; | 407 // Reschedule after a fetch. The persistent schedule is applied only after a |
| 408 // successful fetch. After a failed fetch, we want to keep the previous | |
| 409 // persistent schedule intact so that we eventually get a persistent fallback | |
| 410 // fetch (if the wifi persistent fetches keep failing). | |
| 411 ApplyFetchingSchedule(GetLastFetchingSchedule(), | |
| 412 /*also_apply_persistent_schedule=*/fetch_status.code == | |
| 413 StatusCode::SUCCESS); | |
| 414 | |
| 415 if (callback) { | |
| 416 callback->Run(fetch_status); | |
| 325 } | 417 } |
| 326 | |
| 327 if (fetch_status.code != StatusCode::SUCCESS) { | |
| 328 return; | |
| 329 } | |
| 330 | |
| 331 // Reschedule after a successful fetch. This resets all currently scheduled | |
| 332 // fetches, to make sure the fallback interval triggers only if no wifi fetch | |
| 333 // succeeded, and also that we don't do a background fetch immediately after | |
| 334 // a user-initiated one. | |
| 335 ApplyFetchingSchedule(GetLastFetchingSchedule()); | |
| 336 } | 418 } |
| 337 | 419 |
| 338 } // namespace ntp_snippets | 420 } // namespace ntp_snippets |
| OLD | NEW |