| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "components/ntp_snippets/features.h" |
| 10 #include "components/ntp_snippets/pref_names.h" |
| 11 #include "components/ntp_snippets/remote/remote_suggestions_hard_scheduler.h" |
| 12 #include "components/ntp_snippets/user_classifier.h" |
| 13 #include "components/prefs/pref_registry_simple.h" |
| 14 #include "components/prefs/pref_service.h" |
| 15 #include "components/variations/variations_associated_data.h" |
| 16 |
| 17 namespace ntp_snippets { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Default values for fetching intervals, fallback and wifi. |
| 22 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0}; |
| 23 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0}; |
| 24 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0}; |
| 25 |
| 26 // Variation parameters than can override the default fetching intervals. |
| 27 const char* kFetchingIntervalParamNameRareNtpUser[] = { |
| 28 "fetching_interval_hours-fallback-rare_ntp_user", |
| 29 "fetching_interval_hours-wifi-rare_ntp_user"}; |
| 30 const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
| 31 "fetching_interval_hours-fallback-active_ntp_user", |
| 32 "fetching_interval_hours-wifi-active_ntp_user"}; |
| 33 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
| 34 "fetching_interval_hours-fallback-active_suggestions_consumer", |
| 35 "fetching_interval_hours-wifi-active_suggestions_consumer"}; |
| 36 |
| 37 base::TimeDelta GetCurrentUpdateInterval( |
| 38 bool is_wifi, |
| 39 UserClassifier::UserClass user_class) { |
| 40 double default_value_hours = 0.0; |
| 41 |
| 42 const int index = is_wifi ? 1 : 0; |
| 43 const char* param_name = ""; |
| 44 switch (user_class) { |
| 45 case UserClassifier::UserClass::RARE_NTP_USER: |
| 46 default_value_hours = kDefaultFetchingIntervalRareNtpUser[index]; |
| 47 param_name = kFetchingIntervalParamNameRareNtpUser[index]; |
| 48 break; |
| 49 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 50 default_value_hours = kDefaultFetchingIntervalActiveNtpUser[index]; |
| 51 param_name = kFetchingIntervalParamNameActiveNtpUser[index]; |
| 52 break; |
| 53 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 54 default_value_hours = |
| 55 kDefaultFetchingIntervalActiveSuggestionsConsumer[index]; |
| 56 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index]; |
| 57 break; |
| 58 } |
| 59 |
| 60 double value_hours = variations::GetVariationParamByFeatureAsDouble( |
| 61 ntp_snippets::kArticleSuggestionsFeature, param_name, |
| 62 default_value_hours); |
| 63 |
| 64 return base::TimeDelta::FromSecondsD(value_hours * 3600.0); |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 69 RemoteSuggestionsScheduler::RemoteSuggestionsScheduler( |
| 70 RemoteSuggestionsHardScheduler* hard_scheduler, |
| 71 const UserClassifier* user_classifier, |
| 72 PrefService* pref_service) |
| 73 : hard_scheduler_(hard_scheduler), |
| 74 updater_(nullptr), |
| 75 pref_service_(pref_service) { |
| 76 DCHECK(user_classifier) << "non-null UserClassifier must be provided"; |
| 77 DCHECK(pref_service) << "non-null pref service is needed"; |
| 78 } |
| 79 |
| 80 RemoteSuggestionsScheduler::~RemoteSuggestionsScheduler() = default; |
| 81 |
| 82 void RemoteSuggestionsScheduler::SetUpdater(Updater* updater) { |
| 83 DCHECK(updater) << "non-null Updater must be provided"; |
| 84 updater_ = updater; |
| 85 } |
| 86 |
| 87 // static |
| 88 void RemoteSuggestionsScheduler::RegisterProfilePrefs( |
| 89 PrefRegistrySimple* registry) { |
| 90 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0); |
| 91 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback, |
| 92 0); |
| 93 } |
| 94 |
| 95 void RemoteSuggestionsScheduler::PerformHardUpdate() { |
| 96 if (!updater_) { |
| 97 DLOG(WARNING) << "An update is to be performed without having the updater."; |
| 98 return; |
| 99 } |
| 100 |
| 101 updater_->UpdateRemoteSuggestionsBySchedule(); |
| 102 } |
| 103 |
| 104 void RemoteSuggestionsScheduler::Schedule() { |
| 105 // The scheduler only exists on Android so far, it's null on other platforms. |
| 106 if (!hard_scheduler_) { |
| 107 return; |
| 108 } |
| 109 |
| 110 UpdateSchedule last_schedule = GetLastUpdateSchedule(); |
| 111 UpdateSchedule schedule = GetCurrentUpdateSchedule(); |
| 112 |
| 113 // Reset the schedule only if the parameters have changed. |
| 114 if (last_schedule.interval_wifi != schedule.interval_wifi || |
| 115 last_schedule.interval_fallback != schedule.interval_fallback) { |
| 116 ResetUpdateScheduleFromNow(schedule); |
| 117 } |
| 118 } |
| 119 |
| 120 void RemoteSuggestionsScheduler::Unschedule() { |
| 121 // The scheduler only exists on Android so far, it's null on other platforms. |
| 122 if (!hard_scheduler_) { |
| 123 return; |
| 124 } |
| 125 |
| 126 // Do not unschedule if already switched off |
| 127 UpdateSchedule last_schedule = GetLastUpdateSchedule(); |
| 128 if (last_schedule.interval_fallback.is_zero() && |
| 129 last_schedule.interval_fallback.is_zero()) { |
| 130 return; |
| 131 } |
| 132 |
| 133 hard_scheduler_->Unschedule(); |
| 134 |
| 135 StoreLastUpdateSchedule(UpdateSchedule{base::TimeDelta::FromHours(0), |
| 136 base::TimeDelta::FromHours(0)}); |
| 137 } |
| 138 |
| 139 void RemoteSuggestionsScheduler::OnSuccessfulUpdate() { |
| 140 // The scheduler only exists on Android so far, it's null on other platforms. |
| 141 if (!hard_scheduler_) { |
| 142 return; |
| 143 } |
| 144 |
| 145 ResetUpdateScheduleFromNow(GetLastUpdateSchedule()); |
| 146 } |
| 147 |
| 148 void RemoteSuggestionsScheduler::ResetUpdateScheduleFromNow( |
| 149 UpdateSchedule schedule) { |
| 150 hard_scheduler_->Schedule(schedule.interval_wifi, schedule.interval_fallback); |
| 151 |
| 152 StoreLastUpdateSchedule(schedule); |
| 153 } |
| 154 |
| 155 RemoteSuggestionsScheduler::UpdateSchedule |
| 156 RemoteSuggestionsScheduler::GetCurrentUpdateSchedule() { |
| 157 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); |
| 158 |
| 159 UpdateSchedule schedule; |
| 160 schedule.interval_wifi = |
| 161 GetCurrentUpdateInterval(/*is_wifi=*/true, user_class); |
| 162 schedule.interval_fallback = |
| 163 GetCurrentUpdateInterval(/*is_wifi=*/false, user_class); |
| 164 return schedule; |
| 165 } |
| 166 |
| 167 RemoteSuggestionsScheduler::UpdateSchedule |
| 168 RemoteSuggestionsScheduler::GetLastUpdateSchedule() { |
| 169 UpdateSchedule schedule; |
| 170 schedule.interval_wifi = base::TimeDelta::FromInternalValue( |
| 171 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi)); |
| 172 schedule.interval_fallback = |
| 173 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( |
| 174 prefs::kSnippetBackgroundFetchingIntervalFallback)); |
| 175 return schedule; |
| 176 } |
| 177 |
| 178 void RemoteSuggestionsScheduler::StoreLastUpdateSchedule( |
| 179 RemoteSuggestionsScheduler::UpdateSchedule schedule) { |
| 180 pref_service_->SetInt64( |
| 181 prefs::kSnippetBackgroundFetchingIntervalWifi, |
| 182 schedule.interval_wifi.ToInternalValue()); |
| 183 pref_service_->SetInt64( |
| 184 prefs::kSnippetBackgroundFetchingIntervalFallback, |
| 185 schedule.interval_fallback.ToInternalValue()); |
| 186 } |
| 187 |
| 188 } // namespace ntp_snippets |
| OLD | NEW |