Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_scheduler_impl.cc

Issue 2794313002: [Remote suggestions] Prioritize wifi for soft fetches. (Closed)
Patch Set: Minor polish Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/metrics/field_trial_params.h" 13 #include "base/metrics/field_trial_params.h"
14 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
15 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
16 #include "base/time/clock.h" 16 #include "base/time/clock.h"
17 #include "components/ntp_snippets/features.h" 17 #include "components/ntp_snippets/features.h"
18 #include "components/ntp_snippets/pref_names.h" 18 #include "components/ntp_snippets/pref_names.h"
19 #include "components/ntp_snippets/remote/persistent_scheduler.h" 19 #include "components/ntp_snippets/remote/persistent_scheduler.h"
20 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" 20 #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
21 #include "components/ntp_snippets/status.h" 21 #include "components/ntp_snippets/status.h"
22 #include "components/ntp_snippets/user_classifier.h" 22 #include "components/ntp_snippets/user_classifier.h"
23 #include "components/prefs/pref_registry_simple.h" 23 #include "components/prefs/pref_registry_simple.h"
24 #include "components/prefs/pref_service.h" 24 #include "components/prefs/pref_service.h"
25 #include "net/base/network_change_notifier.h"
25 26
26 namespace ntp_snippets { 27 namespace ntp_snippets {
27 28
28 namespace { 29 namespace {
29 30
30 // The FetchingInterval enum specifies overlapping time intervals that are used 31 // The FetchingInterval enum specifies overlapping time intervals that are used
31 // 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
32 // 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.
33 // Fetches are 34 // Fetches are
34 // only performed when certain conditions associated with the intervals are 35 // only performed when certain conditions associated with the intervals are
35 // met. If a fetch failed, then only the corresponding timer is reset. The 36 // met. If a fetch failed, then only the corresponding timer is reset. The
36 // other timers are not touched. 37 // other timers are not touched.
37 // TODO(markusheintz): Describe the individual intervals. 38 // TODO(markusheintz): Describe the individual intervals.
38 enum class FetchingInterval { 39 enum class FetchingInterval {
39 PERSISTENT_FALLBACK, 40 PERSISTENT_FALLBACK,
40 PERSISTENT_WIFI, 41 PERSISTENT_WIFI,
41 SOFT_ON_USAGE_EVENT, 42 SOFT_FALLBACK,
42 SOFT_ON_NTP_OPENED, 43 SOFT_WIFI,
43 COUNT 44 COUNT
44 }; 45 };
45 46
46 // The following arrays specify default values for remote suggestions fetch 47 // The following arrays specify default values for remote suggestions fetch
47 // intervals corresponding to individual user classes. The user classes are 48 // intervals corresponding to individual user classes. The user classes are
48 // defined by the user classifier. There must be an array for each user class. 49 // defined by the user classifier. There must be an array for each user class.
49 // The values of each array specify a default time interval for the intervals 50 // The values of each array specify a default time interval for the intervals
50 // defined by the enum FetchingInterval. The default time intervals defined in 51 // defined by the enum FetchingInterval. The default time intervals defined in
51 // the arrays can be overridden using different variation parameters. 52 // the arrays can be overridden using different variation parameters.
52 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, 53 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0,
53 6.0}; 54 6.0};
54 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0, 55 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 4.0,
Marc Treib 2017/04/04 13:28:16 Any particular reason for this change?
jkrcal 2017/04/05 17:32:56 Yes, for two reasons: - I would like to be cautio
Marc Treib 2017/04/06 08:29:23 Then, arguably, all the changes to the defaults sh
jkrcal 2017/04/06 12:04:07 Acknowledged.
55 2.0}; 56 2.0};
56 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { 57 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = {
57 24.0, 6.0, 2.0, 1.0}; 58 24.0, 6.0, 2.0, 1.0};
58 59
59 // Variation parameters than can be used to override the default fetching 60 // Variation parameters than can be used to override the default fetching
60 // intervals. 61 // intervals.
61 const char* kFetchingIntervalParamNameRareNtpUser[] = { 62 const char* kFetchingIntervalParamNameRareNtpUser[] = {
62 "fetching_interval_hours-fallback-rare_ntp_user", 63 "fetching_interval_hours-fallback-rare_ntp_user",
63 "fetching_interval_hours-wifi-rare_ntp_user", 64 "fetching_interval_hours-wifi-rare_ntp_user",
64 "soft_fetching_interval_hours-active-rare_ntp_user", 65 "soft_fetching_interval_hours-fallback-rare_ntp_user",
65 "soft_on_ntp_opened_interval_hours-rare_ntp_user"}; 66 "soft_fetching_interval_hours-wifi-rare_ntp_user"};
Marc Treib 2017/04/04 13:28:15 Removing the existing params means we'll have to u
jkrcal 2017/04/05 17:32:56 I'll update the current finch configs asap and cre
66 const char* kFetchingIntervalParamNameActiveNtpUser[] = { 67 const char* kFetchingIntervalParamNameActiveNtpUser[] = {
67 "fetching_interval_hours-fallback-active_ntp_user", 68 "fetching_interval_hours-fallback-active_ntp_user",
68 "fetching_interval_hours-wifi-active_ntp_user", 69 "fetching_interval_hours-wifi-active_ntp_user",
69 "soft_fetching_interval_hours-active-active_ntp_user", 70 "soft_fetching_interval_hours-fallback-active_ntp_user",
70 "soft_on_ntp_opened_interval_hours-active_ntp_user"}; 71 "soft_fetching_interval_hours-wifi-active_ntp_user"};
71 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { 72 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = {
72 "fetching_interval_hours-fallback-active_suggestions_consumer", 73 "fetching_interval_hours-fallback-active_suggestions_consumer",
73 "fetching_interval_hours-wifi-active_suggestions_consumer", 74 "fetching_interval_hours-wifi-active_suggestions_consumer",
74 "soft_fetching_interval_hours-active-active_suggestions_consumer", 75 "soft_fetching_interval_hours-fallback-active_suggestions_consumer",
75 "soft_on_ntp_opened_interval_hours-active_suggestions_consumer"}; 76 "soft_fetching_interval_hours-wifi-active_suggestions_consumer"};
76 77
77 static_assert( 78 static_assert(
78 static_cast<unsigned int>(FetchingInterval::COUNT) == 79 static_cast<unsigned int>(FetchingInterval::COUNT) ==
79 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && 80 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) &&
80 static_cast<unsigned int>(FetchingInterval::COUNT) == 81 static_cast<unsigned int>(FetchingInterval::COUNT) ==
81 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && 82 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) &&
82 static_cast<unsigned int>(FetchingInterval::COUNT) == 83 static_cast<unsigned int>(FetchingInterval::COUNT) ==
83 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && 84 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) &&
84 static_cast<unsigned int>(FetchingInterval::COUNT) == 85 static_cast<unsigned int>(FetchingInterval::COUNT) ==
85 arraysize(kFetchingIntervalParamNameRareNtpUser) && 86 arraysize(kFetchingIntervalParamNameRareNtpUser) &&
86 static_cast<unsigned int>(FetchingInterval::COUNT) == 87 static_cast<unsigned int>(FetchingInterval::COUNT) ==
87 arraysize(kFetchingIntervalParamNameActiveNtpUser) && 88 arraysize(kFetchingIntervalParamNameActiveNtpUser) &&
88 static_cast<unsigned int>(FetchingInterval::COUNT) == 89 static_cast<unsigned int>(FetchingInterval::COUNT) ==
89 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), 90 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer),
90 "Fill in all the info for fetching intervals."); 91 "Fill in all the info for fetching intervals.");
91 92
92 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", 93 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened",
93 "browser_foregrounded", 94 "browser_foregrounded",
94 "browser_cold_start"}; 95 "browser_cold_start"};
95 96
96 const char* kTriggerTypesParamName = "scheduler_trigger_types"; 97 const char* kTriggerTypesParamName = "scheduler_trigger_types";
97 const char* kTriggerTypesParamValueForEmptyList = "-"; 98 const char* kTriggerTypesParamValueForEmptyList = "-";
98 99
99 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30; 100 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30;
100 101
102 const char kSnippetSoftFetchingIntervalOnUsageEventDeprecated[] =
103 "ntp_snippets.soft_fetching_interval_on_usage_event";
104 const char kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated[] =
105 "ntp_snippets.soft_fetching_interval_on_ntp_opened";
Marc Treib 2017/04/04 13:28:15 I recently learned of a good place for that kind o
jkrcal 2017/04/05 17:32:56 Ah, interesting, thanks for the pointer! I'd keep
Marc Treib 2017/04/06 08:29:23 FWIW, bauerb@ and battre@ are both owners, so no t
jkrcal 2017/04/06 12:04:08 Acknowledged.
106
101 // Returns the time interval to use for scheduling remote suggestion fetches for 107 // Returns the time interval to use for scheduling remote suggestion fetches for
102 // the given interval and user_class. 108 // the given interval and user_class.
103 base::TimeDelta GetDesiredFetchingInterval( 109 base::TimeDelta GetDesiredFetchingInterval(
104 FetchingInterval interval, 110 FetchingInterval interval,
105 UserClassifier::UserClass user_class) { 111 UserClassifier::UserClass user_class) {
106 DCHECK(interval != FetchingInterval::COUNT); 112 DCHECK(interval != FetchingInterval::COUNT);
107 const unsigned int index = static_cast<unsigned int>(interval); 113 const unsigned int index = static_cast<unsigned int>(interval);
108 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); 114 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser));
109 115
110 double default_value_hours = 0.0; 116 double default_value_hours = 0.0;
(...skipping 14 matching lines...) Expand all
125 break; 131 break;
126 } 132 }
127 133
128 double value_hours = base::GetFieldTrialParamByFeatureAsDouble( 134 double value_hours = base::GetFieldTrialParamByFeatureAsDouble(
129 ntp_snippets::kArticleSuggestionsFeature, param_name, 135 ntp_snippets::kArticleSuggestionsFeature, param_name,
130 default_value_hours); 136 default_value_hours);
131 137
132 return base::TimeDelta::FromSecondsD(value_hours * 3600.0); 138 return base::TimeDelta::FromSecondsD(value_hours * 3600.0);
133 } 139 }
134 140
141 bool IsOnWifi() {
142 net::NetworkChangeNotifier::ConnectionType connection_type =
143 net::NetworkChangeNotifier::GetConnectionType();
144 // Return true for Wi-Fi (and any other likely unmetered connection type).
Marc Treib 2017/04/04 13:28:16 Hm yeah, "wifi" is really a misnomer, also for the
jkrcal 2017/04/05 17:32:56 Yeah, I've updated the comments to explain what we
145 return (connection_type ==
146 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI ||
147 connection_type ==
148 net::NetworkChangeNotifier::ConnectionType::CONNECTION_ETHERNET);
149 }
150
135 } // namespace 151 } // namespace
136 152
137 class EulaState : public web_resource::EulaAcceptedNotifier::Observer { 153 class EulaState : public web_resource::EulaAcceptedNotifier::Observer {
138 public: 154 public:
139 EulaState(PrefService* local_state_prefs, 155 EulaState(PrefService* local_state_prefs,
140 RemoteSuggestionsScheduler* scheduler) 156 RemoteSuggestionsScheduler* scheduler)
141 : eula_notifier_( 157 : eula_notifier_(
142 web_resource::EulaAcceptedNotifier::Create(local_state_prefs)), 158 web_resource::EulaAcceptedNotifier::Create(local_state_prefs)),
143 scheduler_(scheduler) { 159 scheduler_(scheduler) {
144 // EulaNotifier is not constructed on some platforms (such as desktop). 160 // EulaNotifier is not constructed on some platforms (such as desktop).
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 RemoteSuggestionsSchedulerImpl::FetchingSchedule 192 RemoteSuggestionsSchedulerImpl::FetchingSchedule
177 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { 193 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() {
178 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), 194 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(),
179 base::TimeDelta(), base::TimeDelta()}; 195 base::TimeDelta(), base::TimeDelta()};
180 } 196 }
181 197
182 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( 198 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==(
183 const FetchingSchedule& other) const { 199 const FetchingSchedule& other) const {
184 return interval_persistent_wifi == other.interval_persistent_wifi && 200 return interval_persistent_wifi == other.interval_persistent_wifi &&
185 interval_persistent_fallback == other.interval_persistent_fallback && 201 interval_persistent_fallback == other.interval_persistent_fallback &&
186 interval_soft_on_usage_event == other.interval_soft_on_usage_event && 202 interval_soft_wifi == other.interval_soft_wifi &&
187 interval_soft_on_ntp_opened == other.interval_soft_on_ntp_opened; 203 interval_soft_fallback == other.interval_soft_fallback;
188 } 204 }
189 205
190 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( 206 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=(
191 const FetchingSchedule& other) const { 207 const FetchingSchedule& other) const {
192 return !operator==(other); 208 return !operator==(other);
193 } 209 }
194 210
195 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { 211 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const {
196 return interval_persistent_wifi.is_zero() && 212 return interval_persistent_wifi.is_zero() &&
197 interval_persistent_fallback.is_zero() && 213 interval_persistent_fallback.is_zero() &&
198 interval_soft_on_usage_event.is_zero() && 214 interval_soft_wifi.is_zero() && interval_soft_fallback.is_zero();
199 interval_soft_on_ntp_opened.is_zero();
200 } 215 }
201 216
202 // The TriggerType enum specifies values for the events that can trigger 217 // The TriggerType enum specifies values for the events that can trigger
203 // fetching remote suggestions. These values are written to logs. New enum 218 // fetching remote suggestions. These values are written to logs. New enum
204 // values can be added, but existing enums must never be renumbered or deleted 219 // values can be added, but existing enums must never be renumbered or deleted
205 // and reused. When adding new entries, also update the array 220 // and reused. When adding new entries, also update the array
206 // |kTriggerTypeNames| above. 221 // |kTriggerTypeNames| above.
207 enum class RemoteSuggestionsSchedulerImpl::TriggerType { 222 enum class RemoteSuggestionsSchedulerImpl::TriggerType {
208 PERSISTENT_SCHEDULER_WAKE_UP = 0, 223 PERSISTENT_SCHEDULER_WAKE_UP = 0,
209 NTP_OPENED = 1, 224 NTP_OPENED = 1,
(...skipping 24 matching lines...) Expand all
234 profile_prefs, 249 profile_prefs,
235 RequestThrottler::RequestType:: 250 RequestThrottler::RequestType::
236 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), 251 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER),
237 eula_state_(base::MakeUnique<EulaState>(local_state_prefs, this)), 252 eula_state_(base::MakeUnique<EulaState>(local_state_prefs, this)),
238 profile_prefs_(profile_prefs), 253 profile_prefs_(profile_prefs),
239 clock_(std::move(clock)), 254 clock_(std::move(clock)),
240 enabled_triggers_(GetEnabledTriggerTypes()) { 255 enabled_triggers_(GetEnabledTriggerTypes()) {
241 DCHECK(user_classifier); 256 DCHECK(user_classifier);
242 DCHECK(profile_prefs); 257 DCHECK(profile_prefs);
243 258
259 // Cleanup procedure in M59. Remove for M60.
Marc Treib 2017/04/04 13:28:16 I'd leave it for a bit longer, maybe M62?
jkrcal 2017/04/05 17:32:56 Done.
260 profile_prefs_->ClearPref(kSnippetSoftFetchingIntervalOnUsageEventDeprecated);
261 profile_prefs_->ClearPref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated);
262
244 LoadLastFetchingSchedule(); 263 LoadLastFetchingSchedule();
245 } 264 }
246 265
247 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; 266 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default;
248 267
249 // static 268 // static
250 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( 269 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs(
251 PrefRegistrySimple* registry) { 270 PrefRegistrySimple* registry) {
252 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); 271 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0);
253 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, 272 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback,
254 0); 273 0);
255 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent, 274 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalWifi, 0);
256 0); 275 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalFallback, 0);
257 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); 276 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0);
258 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnNtpOpened, 277
278 // Cleanup procedure in M59. Remove for M60.
279 registry->RegisterInt64Pref(
280 kSnippetSoftFetchingIntervalOnUsageEventDeprecated, 0);
281 registry->RegisterInt64Pref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated,
259 0); 282 0);
260 } 283 }
261 284
262 void RemoteSuggestionsSchedulerImpl::SetProvider( 285 void RemoteSuggestionsSchedulerImpl::SetProvider(
263 RemoteSuggestionsProvider* provider) { 286 RemoteSuggestionsProvider* provider) {
264 DCHECK(provider); 287 DCHECK(provider);
265 provider_ = provider; 288 provider_ = provider;
266 } 289 }
267 290
268 void RemoteSuggestionsSchedulerImpl::OnProviderActivated() { 291 void RemoteSuggestionsSchedulerImpl::OnProviderActivated() {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 OnFetchCompleted(fetch_status); 330 OnFetchCompleted(fetch_status);
308 } 331 }
309 332
310 void RemoteSuggestionsSchedulerImpl::OnPersistentSchedulerWakeUp() { 333 void RemoteSuggestionsSchedulerImpl::OnPersistentSchedulerWakeUp() {
311 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); 334 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP);
312 } 335 }
313 336
314 void RemoteSuggestionsSchedulerImpl::OnBrowserForegrounded() { 337 void RemoteSuggestionsSchedulerImpl::OnBrowserForegrounded() {
315 // TODO(jkrcal): Consider that this is called whenever we open or return to an 338 // TODO(jkrcal): Consider that this is called whenever we open or return to an
316 // Activity. Therefore, keep work light for fast start up calls. 339 // Activity. Therefore, keep work light for fast start up calls.
317 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_FOREGROUNDED)) { 340 if (!ShouldRefetchInTheBackgroundNow()) {
Marc Treib 2017/04/04 13:28:16 Doesn't this mean that all triggers will be treate
jkrcal 2017/04/05 17:32:56 I've rewritten the code. Hope the structure is now
Marc Treib 2017/04/06 08:29:23 Yup, much better, thanks!
318 return; 341 return;
319 } 342 }
320 343
321 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED); 344 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED);
322 } 345 }
323 346
324 void RemoteSuggestionsSchedulerImpl::OnBrowserColdStart() { 347 void RemoteSuggestionsSchedulerImpl::OnBrowserColdStart() {
325 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast 348 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast
326 // cold start ups. 349 // cold start ups.
327 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_COLD_START)) { 350 if (!ShouldRefetchInTheBackgroundNow()) {
328 return; 351 return;
329 } 352 }
330 353
331 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START); 354 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START);
332 } 355 }
333 356
334 void RemoteSuggestionsSchedulerImpl::OnNTPOpened() { 357 void RemoteSuggestionsSchedulerImpl::OnNTPOpened() {
335 if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { 358 if (!ShouldRefetchInTheBackgroundNow()) {
336 return; 359 return;
337 } 360 }
338 361
339 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); 362 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED);
340 } 363 }
341 364
342 void RemoteSuggestionsSchedulerImpl::StartScheduling() { 365 void RemoteSuggestionsSchedulerImpl::StartScheduling() {
343 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); 366 FetchingSchedule new_schedule = GetDesiredFetchingSchedule();
344 367
345 if (schedule_ == new_schedule) { 368 if (schedule_ == new_schedule) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 400
378 RemoteSuggestionsSchedulerImpl::FetchingSchedule 401 RemoteSuggestionsSchedulerImpl::FetchingSchedule
379 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { 402 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const {
380 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); 403 UserClassifier::UserClass user_class = user_classifier_->GetUserClass();
381 404
382 FetchingSchedule schedule; 405 FetchingSchedule schedule;
383 schedule.interval_persistent_wifi = 406 schedule.interval_persistent_wifi =
384 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); 407 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class);
385 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( 408 schedule.interval_persistent_fallback = GetDesiredFetchingInterval(
386 FetchingInterval::PERSISTENT_FALLBACK, user_class); 409 FetchingInterval::PERSISTENT_FALLBACK, user_class);
387 schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval( 410 schedule.interval_soft_wifi =
388 FetchingInterval::SOFT_ON_USAGE_EVENT, user_class); 411 GetDesiredFetchingInterval(FetchingInterval::SOFT_WIFI, user_class);
389 schedule.interval_soft_on_ntp_opened = GetDesiredFetchingInterval( 412 schedule.interval_soft_fallback =
390 FetchingInterval::SOFT_ON_NTP_OPENED, user_class); 413 GetDesiredFetchingInterval(FetchingInterval::SOFT_FALLBACK, user_class);
391 414
392 return schedule; 415 return schedule;
393 } 416 }
394 417
395 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { 418 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() {
396 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( 419 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue(
397 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); 420 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi));
398 schedule_.interval_persistent_fallback = 421 schedule_.interval_persistent_fallback =
399 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( 422 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64(
400 prefs::kSnippetPersistentFetchingIntervalFallback)); 423 prefs::kSnippetPersistentFetchingIntervalFallback));
401 schedule_.interval_soft_on_usage_event = 424 schedule_.interval_soft_wifi = base::TimeDelta::FromInternalValue(
402 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( 425 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalWifi));
403 prefs::kSnippetSoftFetchingIntervalOnUsageEvent)); 426 schedule_.interval_soft_fallback = base::TimeDelta::FromInternalValue(
404 schedule_.interval_soft_on_ntp_opened = base::TimeDelta::FromInternalValue( 427 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalFallback));
405 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnNtpOpened));
406 } 428 }
407 429
408 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { 430 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() {
409 profile_prefs_->SetInt64( 431 profile_prefs_->SetInt64(
410 prefs::kSnippetPersistentFetchingIntervalWifi, 432 prefs::kSnippetPersistentFetchingIntervalWifi,
411 schedule_.interval_persistent_wifi.ToInternalValue()); 433 schedule_.interval_persistent_wifi.ToInternalValue());
412 profile_prefs_->SetInt64( 434 profile_prefs_->SetInt64(
413 prefs::kSnippetPersistentFetchingIntervalFallback, 435 prefs::kSnippetPersistentFetchingIntervalFallback,
414 schedule_.interval_persistent_fallback.ToInternalValue()); 436 schedule_.interval_persistent_fallback.ToInternalValue());
415 profile_prefs_->SetInt64( 437 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalWifi,
416 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, 438 schedule_.interval_soft_wifi.ToInternalValue());
417 schedule_.interval_soft_on_usage_event.ToInternalValue()); 439 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalFallback,
418 profile_prefs_->SetInt64( 440 schedule_.interval_soft_fallback.ToInternalValue());
419 prefs::kSnippetSoftFetchingIntervalOnNtpOpened,
420 schedule_.interval_soft_on_ntp_opened.ToInternalValue());
421 } 441 }
422 442
423 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfEnabled( 443 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfEnabled(
424 TriggerType trigger) { 444 TriggerType trigger) {
425 if (BackgroundFetchesDisabled(trigger)) { 445 if (BackgroundFetchesDisabled(trigger)) {
426 return; 446 return;
427 } 447 }
428 448
429 UMA_HISTOGRAM_ENUMERATION( 449 UMA_HISTOGRAM_ENUMERATION(
430 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", 450 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger",
(...skipping 10 matching lines...) Expand all
441 if (!AcquireQuota(/*interactive_request=*/false)) { 461 if (!AcquireQuota(/*interactive_request=*/false)) {
442 return; 462 return;
443 } 463 }
444 464
445 background_fetch_in_progress_ = true; 465 background_fetch_in_progress_ = true;
446 provider_->RefetchInTheBackground(base::Bind( 466 provider_->RefetchInTheBackground(base::Bind(
447 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, 467 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished,
448 base::Unretained(this))); 468 base::Unretained(this)));
449 } 469 }
450 470
451 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow( 471 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow() {
452 TriggerType trigger) {
453 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( 472 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue(
454 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); 473 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt));
455 base::Time first_allowed_fetch_time; 474 base::Time first_allowed_fetch_time;
456 switch (trigger) { 475
457 case TriggerType::NTP_OPENED: 476 if (IsOnWifi()) {
Marc Treib 2017/04/04 13:28:15 nit: IMO this would be simpler as an arithmetic if
jkrcal 2017/04/05 17:32:56 Done.
458 first_allowed_fetch_time = 477 first_allowed_fetch_time =
459 last_fetch_attempt_time + schedule_.interval_soft_on_ntp_opened; 478 last_fetch_attempt_time + schedule_.interval_soft_wifi;
460 break; 479 } else {
461 case TriggerType::BROWSER_FOREGROUNDED: 480 first_allowed_fetch_time =
462 case TriggerType::BROWSER_COLD_START: 481 last_fetch_attempt_time + schedule_.interval_soft_fallback;
463 first_allowed_fetch_time =
464 last_fetch_attempt_time + schedule_.interval_soft_on_usage_event;
465 break;
466 case TriggerType::PERSISTENT_SCHEDULER_WAKE_UP:
467 case TriggerType::COUNT:
468 NOTREACHED();
469 break;
470 } 482 }
483
471 base::Time now = clock_->Now(); 484 base::Time now = clock_->Now();
472
473 return background_fetches_allowed_after_ <= now && 485 return background_fetches_allowed_after_ <= now &&
474 first_allowed_fetch_time <= now; 486 first_allowed_fetch_time <= now;
475 } 487 }
476 488
477 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( 489 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled(
478 TriggerType trigger) const { 490 TriggerType trigger) const {
479 if (!provider_) { 491 if (!provider_) {
480 return true; // Cannot fetch as remote suggestions provider does not exist. 492 return true; // Cannot fetch as remote suggestions provider does not exist.
481 } 493 }
482 494
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 return enabled_types; 586 return enabled_types;
575 } 587 }
576 588
577 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> 589 std::set<RemoteSuggestionsSchedulerImpl::TriggerType>
578 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { 590 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() {
579 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, 591 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED,
580 TriggerType::BROWSER_FOREGROUNDED}; 592 TriggerType::BROWSER_FOREGROUNDED};
581 } 593 }
582 594
583 } // namespace ntp_snippets 595 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698