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 = ""; | |
Marc Treib
2016/12/09 12:25:27
nullptr? The "empty" case can't happen anyway
jkrcal
2016/12/14 09:42:10
Done.
| |
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 user_classifier_(user_classifier), | |
76 pref_service_(pref_service) { | |
77 DCHECK(user_classifier) << "non-null UserClassifier must be provided"; | |
78 DCHECK(pref_service) << "non-null pref service is needed"; | |
79 } | |
80 | |
81 RemoteSuggestionsScheduler::~RemoteSuggestionsScheduler() = default; | |
82 | |
83 void RemoteSuggestionsScheduler::SetUpdater(Updater* updater) { | |
84 DCHECK(updater) << "non-null Updater must be provided"; | |
Marc Treib
2016/12/09 12:25:27
Since we have to handle a null updater_ anyway, ma
jkrcal
2016/12/14 09:42:10
Done.
| |
85 updater_ = updater; | |
86 } | |
87 | |
88 // static | |
89 void RemoteSuggestionsScheduler::RegisterProfilePrefs( | |
90 PrefRegistrySimple* registry) { | |
91 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0); | |
92 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback, | |
93 0); | |
94 } | |
95 | |
96 void RemoteSuggestionsScheduler::PerformHardUpdate() { | |
tschumann
2016/12/09 17:27:28
i'm a bit confused. Does this now perform an updat
jkrcal
2016/12/14 09:42:09
It performs it. Is is clearer now in the new desig
| |
97 if (!updater_) { | |
98 DLOG(WARNING) << "An update is to be performed without having the updater."; | |
99 return; | |
100 } | |
101 | |
102 updater_->UpdateRemoteSuggestionsBySchedule(); | |
Marc Treib
2016/12/09 12:25:27
Hm. So in practice, the OS-scheduled update gets h
markusheintz_
2016/12/12 14:36:16
+1 this hides a simple call and somehow implies th
jkrcal
2016/12/14 09:42:10
Done.
| |
103 } | |
104 | |
105 void RemoteSuggestionsScheduler::Schedule() { | |
106 // The scheduler only exists on Android so far, it's null on other platforms. | |
107 if (!hard_scheduler_) { | |
108 return; | |
109 } | |
110 | |
111 UpdateSchedule last_schedule = GetLastUpdateSchedule(); | |
112 UpdateSchedule schedule = GetCurrentUpdateSchedule(); | |
113 | |
114 // Reset the schedule only if the parameters have changed. | |
115 if (last_schedule.interval_wifi != schedule.interval_wifi || | |
116 last_schedule.interval_fallback != schedule.interval_fallback) { | |
Marc Treib
2016/12/09 12:25:26
optional: Could put operator==/!= on the struct? I
jkrcal
2016/12/14 09:42:10
Done.
| |
117 ResetUpdateScheduleFromNow(schedule); | |
118 } | |
119 } | |
120 | |
121 void RemoteSuggestionsScheduler::Unschedule() { | |
122 // The scheduler only exists on Android so far, it's null on other platforms. | |
123 if (!hard_scheduler_) { | |
124 return; | |
125 } | |
126 | |
127 // Do not unschedule if already switched off | |
128 UpdateSchedule last_schedule = GetLastUpdateSchedule(); | |
129 if (last_schedule.interval_fallback.is_zero() && | |
130 last_schedule.interval_fallback.is_zero()) { | |
Marc Treib
2016/12/09 12:25:27
Similar here, could put an accessor on the struct.
jkrcal
2016/12/14 09:42:10
Done.
| |
131 return; | |
132 } | |
133 | |
134 hard_scheduler_->Unschedule(); | |
135 | |
136 StoreLastUpdateSchedule(UpdateSchedule{base::TimeDelta::FromHours(0), | |
137 base::TimeDelta::FromHours(0)}); | |
Marc Treib
2016/12/09 12:25:26
Maybe make an UpdateSchedule::Empty() ?
jkrcal
2016/12/14 09:42:10
Done.
| |
138 } | |
139 | |
140 void RemoteSuggestionsScheduler::OnSuccessfulUpdate() { | |
141 // The scheduler only exists on Android so far, it's null on other platforms. | |
142 if (!hard_scheduler_) { | |
143 return; | |
144 } | |
145 | |
146 ResetUpdateScheduleFromNow(GetLastUpdateSchedule()); | |
147 } | |
148 | |
149 void RemoteSuggestionsScheduler::ResetUpdateScheduleFromNow( | |
tschumann
2016/12/09 17:27:28
the 'fromNow' suffix seems superfluous.
Actually,
jkrcal
2016/12/14 09:42:10
Renamed to ApplyFetchingSchedule(). WDYT?
| |
150 UpdateSchedule schedule) { | |
151 hard_scheduler_->Schedule(schedule.interval_wifi, schedule.interval_fallback); | |
152 | |
153 StoreLastUpdateSchedule(schedule); | |
154 } | |
155 | |
156 RemoteSuggestionsScheduler::UpdateSchedule | |
157 RemoteSuggestionsScheduler::GetCurrentUpdateSchedule() { | |
Marc Treib
2016/12/09 12:25:27
Hm, IMO this name is a bit misleading. I'd interpr
jkrcal
2016/12/14 09:42:10
Renamed to GetDesiredUpdateSchedule(). Better?
Marc Treib
2016/12/14 10:47:01
Yup, thanks!
| |
158 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); | |
159 | |
160 UpdateSchedule schedule; | |
161 schedule.interval_wifi = | |
162 GetCurrentUpdateInterval(/*is_wifi=*/true, user_class); | |
163 schedule.interval_fallback = | |
164 GetCurrentUpdateInterval(/*is_wifi=*/false, user_class); | |
165 return schedule; | |
166 } | |
167 | |
168 RemoteSuggestionsScheduler::UpdateSchedule | |
169 RemoteSuggestionsScheduler::GetLastUpdateSchedule() { | |
170 UpdateSchedule schedule; | |
171 schedule.interval_wifi = base::TimeDelta::FromInternalValue( | |
172 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi)); | |
173 schedule.interval_fallback = | |
174 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( | |
175 prefs::kSnippetBackgroundFetchingIntervalFallback)); | |
176 return schedule; | |
177 } | |
178 | |
179 void RemoteSuggestionsScheduler::StoreLastUpdateSchedule( | |
180 RemoteSuggestionsScheduler::UpdateSchedule schedule) { | |
181 pref_service_->SetInt64( | |
182 prefs::kSnippetBackgroundFetchingIntervalWifi, | |
183 schedule.interval_wifi.ToInternalValue()); | |
184 pref_service_->SetInt64( | |
185 prefs::kSnippetBackgroundFetchingIntervalFallback, | |
186 schedule.interval_fallback.ToInternalValue()); | |
187 } | |
188 | |
189 } // namespace ntp_snippets | |
OLD | NEW |