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 |
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 only performed when conditions associated with the intervals are |
34 // only performed when certain conditions associated with the intervals are | 35 // met: |
35 // met. If a fetch failed, then only the corresponding timer is reset. The | 36 // - A "soft" fetch is performed only at a moment when the user actively uses |
36 // other timers are not touched. | 37 // Chrome after the interval has elapsed and causes a trigger that is currently |
37 // TODO(markusheintz): Describe the individual intervals. | 38 // enabled while |
| 39 // - a "persistent" fetch is initiated automatically by the OS after the |
| 40 // interval elapses. |
| 41 // - A "wifi" fetch is performed only when the user is on a connection that |
| 42 // the OS classifies as unmetered while |
| 43 // - a "fallback" fetch happens on any connection. |
| 44 // If a fetch failed, then only the corresponding timer is reset. The other |
| 45 // timers are not touched. |
38 enum class FetchingInterval { | 46 enum class FetchingInterval { |
39 PERSISTENT_FALLBACK, | 47 PERSISTENT_FALLBACK, |
40 PERSISTENT_WIFI, | 48 PERSISTENT_WIFI, |
41 SOFT_ON_USAGE_EVENT, | 49 SOFT_FALLBACK, |
42 SOFT_ON_NTP_OPENED, | 50 SOFT_WIFI, |
43 COUNT | 51 COUNT |
44 }; | 52 }; |
45 | 53 |
46 // The following arrays specify default values for remote suggestions fetch | 54 // The following arrays specify default values for remote suggestions fetch |
47 // intervals corresponding to individual user classes. The user classes are | 55 // 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. | 56 // 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 | 57 // 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 | 58 // defined by the enum FetchingInterval. The default time intervals defined in |
51 // the arrays can be overridden using different variation parameters. | 59 // the arrays can be overridden using different variation parameters. |
52 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, | 60 const double kDefaultFetchingIntervalHoursRareNtpUser[] = {48.0, 24.0, 12.0, |
53 6.0}; | 61 6.0}; |
54 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 2.0, | 62 const double kDefaultFetchingIntervalHoursActiveNtpUser[] = {24.0, 6.0, 4.0, |
55 2.0}; | 63 2.0}; |
56 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { | 64 const double kDefaultFetchingIntervalHoursActiveSuggestionsConsumer[] = { |
57 24.0, 6.0, 2.0, 1.0}; | 65 24.0, 6.0, 2.0, 1.0}; |
58 | 66 |
59 // Variation parameters than can be used to override the default fetching | 67 // Variation parameters than can be used to override the default fetching |
60 // intervals. | 68 // intervals. |
61 const char* kFetchingIntervalParamNameRareNtpUser[] = { | 69 const char* kFetchingIntervalParamNameRareNtpUser[] = { |
62 "fetching_interval_hours-fallback-rare_ntp_user", | 70 "fetching_interval_hours-fallback-rare_ntp_user", |
63 "fetching_interval_hours-wifi-rare_ntp_user", | 71 "fetching_interval_hours-wifi-rare_ntp_user", |
64 "soft_fetching_interval_hours-active-rare_ntp_user", | 72 "soft_fetching_interval_hours-fallback-rare_ntp_user", |
65 "soft_on_ntp_opened_interval_hours-rare_ntp_user"}; | 73 "soft_fetching_interval_hours-wifi-rare_ntp_user"}; |
66 const char* kFetchingIntervalParamNameActiveNtpUser[] = { | 74 const char* kFetchingIntervalParamNameActiveNtpUser[] = { |
67 "fetching_interval_hours-fallback-active_ntp_user", | 75 "fetching_interval_hours-fallback-active_ntp_user", |
68 "fetching_interval_hours-wifi-active_ntp_user", | 76 "fetching_interval_hours-wifi-active_ntp_user", |
69 "soft_fetching_interval_hours-active-active_ntp_user", | 77 "soft_fetching_interval_hours-fallback-active_ntp_user", |
70 "soft_on_ntp_opened_interval_hours-active_ntp_user"}; | 78 "soft_fetching_interval_hours-wifi-active_ntp_user"}; |
71 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { | 79 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { |
72 "fetching_interval_hours-fallback-active_suggestions_consumer", | 80 "fetching_interval_hours-fallback-active_suggestions_consumer", |
73 "fetching_interval_hours-wifi-active_suggestions_consumer", | 81 "fetching_interval_hours-wifi-active_suggestions_consumer", |
74 "soft_fetching_interval_hours-active-active_suggestions_consumer", | 82 "soft_fetching_interval_hours-fallback-active_suggestions_consumer", |
75 "soft_on_ntp_opened_interval_hours-active_suggestions_consumer"}; | 83 "soft_fetching_interval_hours-wifi-active_suggestions_consumer"}; |
76 | 84 |
77 static_assert( | 85 static_assert( |
78 static_cast<unsigned int>(FetchingInterval::COUNT) == | 86 static_cast<unsigned int>(FetchingInterval::COUNT) == |
79 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && | 87 arraysize(kDefaultFetchingIntervalHoursRareNtpUser) && |
80 static_cast<unsigned int>(FetchingInterval::COUNT) == | 88 static_cast<unsigned int>(FetchingInterval::COUNT) == |
81 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && | 89 arraysize(kDefaultFetchingIntervalHoursActiveNtpUser) && |
82 static_cast<unsigned int>(FetchingInterval::COUNT) == | 90 static_cast<unsigned int>(FetchingInterval::COUNT) == |
83 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && | 91 arraysize(kDefaultFetchingIntervalHoursActiveSuggestionsConsumer) && |
84 static_cast<unsigned int>(FetchingInterval::COUNT) == | 92 static_cast<unsigned int>(FetchingInterval::COUNT) == |
85 arraysize(kFetchingIntervalParamNameRareNtpUser) && | 93 arraysize(kFetchingIntervalParamNameRareNtpUser) && |
86 static_cast<unsigned int>(FetchingInterval::COUNT) == | 94 static_cast<unsigned int>(FetchingInterval::COUNT) == |
87 arraysize(kFetchingIntervalParamNameActiveNtpUser) && | 95 arraysize(kFetchingIntervalParamNameActiveNtpUser) && |
88 static_cast<unsigned int>(FetchingInterval::COUNT) == | 96 static_cast<unsigned int>(FetchingInterval::COUNT) == |
89 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), | 97 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), |
90 "Fill in all the info for fetching intervals."); | 98 "Fill in all the info for fetching intervals."); |
91 | 99 |
92 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", | 100 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", |
93 "browser_foregrounded", | 101 "browser_foregrounded", |
94 "browser_cold_start"}; | 102 "browser_cold_start"}; |
95 | 103 |
96 const char* kTriggerTypesParamName = "scheduler_trigger_types"; | 104 const char* kTriggerTypesParamName = "scheduler_trigger_types"; |
97 const char* kTriggerTypesParamValueForEmptyList = "-"; | 105 const char* kTriggerTypesParamValueForEmptyList = "-"; |
98 | 106 |
99 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30; | 107 const int kBlockBackgroundFetchesMinutesAfterClearingHistory = 30; |
100 | 108 |
| 109 const char kSnippetSoftFetchingIntervalOnUsageEventDeprecated[] = |
| 110 "ntp_snippets.soft_fetching_interval_on_usage_event"; |
| 111 const char kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated[] = |
| 112 "ntp_snippets.soft_fetching_interval_on_ntp_opened"; |
| 113 |
101 // Returns the time interval to use for scheduling remote suggestion fetches for | 114 // Returns the time interval to use for scheduling remote suggestion fetches for |
102 // the given interval and user_class. | 115 // the given interval and user_class. |
103 base::TimeDelta GetDesiredFetchingInterval( | 116 base::TimeDelta GetDesiredFetchingInterval( |
104 FetchingInterval interval, | 117 FetchingInterval interval, |
105 UserClassifier::UserClass user_class) { | 118 UserClassifier::UserClass user_class) { |
106 DCHECK(interval != FetchingInterval::COUNT); | 119 DCHECK(interval != FetchingInterval::COUNT); |
107 const unsigned int index = static_cast<unsigned int>(interval); | 120 const unsigned int index = static_cast<unsigned int>(interval); |
108 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); | 121 DCHECK(index < arraysize(kDefaultFetchingIntervalHoursRareNtpUser)); |
109 | 122 |
110 double default_value_hours = 0.0; | 123 double default_value_hours = 0.0; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 RemoteSuggestionsSchedulerImpl::FetchingSchedule | 189 RemoteSuggestionsSchedulerImpl::FetchingSchedule |
177 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { | 190 RemoteSuggestionsSchedulerImpl::FetchingSchedule::Empty() { |
178 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), | 191 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(), |
179 base::TimeDelta(), base::TimeDelta()}; | 192 base::TimeDelta(), base::TimeDelta()}; |
180 } | 193 } |
181 | 194 |
182 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( | 195 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator==( |
183 const FetchingSchedule& other) const { | 196 const FetchingSchedule& other) const { |
184 return interval_persistent_wifi == other.interval_persistent_wifi && | 197 return interval_persistent_wifi == other.interval_persistent_wifi && |
185 interval_persistent_fallback == other.interval_persistent_fallback && | 198 interval_persistent_fallback == other.interval_persistent_fallback && |
186 interval_soft_on_usage_event == other.interval_soft_on_usage_event && | 199 interval_soft_wifi == other.interval_soft_wifi && |
187 interval_soft_on_ntp_opened == other.interval_soft_on_ntp_opened; | 200 interval_soft_fallback == other.interval_soft_fallback; |
188 } | 201 } |
189 | 202 |
190 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( | 203 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::operator!=( |
191 const FetchingSchedule& other) const { | 204 const FetchingSchedule& other) const { |
192 return !operator==(other); | 205 return !operator==(other); |
193 } | 206 } |
194 | 207 |
195 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { | 208 bool RemoteSuggestionsSchedulerImpl::FetchingSchedule::is_empty() const { |
196 return interval_persistent_wifi.is_zero() && | 209 return interval_persistent_wifi.is_zero() && |
197 interval_persistent_fallback.is_zero() && | 210 interval_persistent_fallback.is_zero() && |
198 interval_soft_on_usage_event.is_zero() && | 211 interval_soft_wifi.is_zero() && interval_soft_fallback.is_zero(); |
199 interval_soft_on_ntp_opened.is_zero(); | |
200 } | 212 } |
201 | 213 |
202 // The TriggerType enum specifies values for the events that can trigger | 214 // The TriggerType enum specifies values for the events that can trigger |
203 // fetching remote suggestions. These values are written to logs. New enum | 215 // 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 | 216 // values can be added, but existing enums must never be renumbered or deleted |
205 // and reused. When adding new entries, also update the array | 217 // and reused. When adding new entries, also update the array |
206 // |kTriggerTypeNames| above. | 218 // |kTriggerTypeNames| above. |
207 enum class RemoteSuggestionsSchedulerImpl::TriggerType { | 219 enum class RemoteSuggestionsSchedulerImpl::TriggerType { |
208 PERSISTENT_SCHEDULER_WAKE_UP = 0, | 220 PERSISTENT_SCHEDULER_WAKE_UP = 0, |
209 NTP_OPENED = 1, | 221 NTP_OPENED = 1, |
(...skipping 24 matching lines...) Expand all Loading... |
234 profile_prefs, | 246 profile_prefs, |
235 RequestThrottler::RequestType:: | 247 RequestThrottler::RequestType:: |
236 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), | 248 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), |
237 eula_state_(base::MakeUnique<EulaState>(local_state_prefs, this)), | 249 eula_state_(base::MakeUnique<EulaState>(local_state_prefs, this)), |
238 profile_prefs_(profile_prefs), | 250 profile_prefs_(profile_prefs), |
239 clock_(std::move(clock)), | 251 clock_(std::move(clock)), |
240 enabled_triggers_(GetEnabledTriggerTypes()) { | 252 enabled_triggers_(GetEnabledTriggerTypes()) { |
241 DCHECK(user_classifier); | 253 DCHECK(user_classifier); |
242 DCHECK(profile_prefs); | 254 DCHECK(profile_prefs); |
243 | 255 |
| 256 // Cleanup procedure in M59. Remove for M62. |
| 257 profile_prefs_->ClearPref(kSnippetSoftFetchingIntervalOnUsageEventDeprecated); |
| 258 profile_prefs_->ClearPref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated); |
| 259 |
244 LoadLastFetchingSchedule(); | 260 LoadLastFetchingSchedule(); |
245 } | 261 } |
246 | 262 |
247 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; | 263 RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; |
248 | 264 |
249 // static | 265 // static |
250 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( | 266 void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( |
251 PrefRegistrySimple* registry) { | 267 PrefRegistrySimple* registry) { |
252 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); | 268 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); |
253 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, | 269 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, |
254 0); | 270 0); |
255 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 271 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalWifi, 0); |
256 0); | 272 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalFallback, 0); |
257 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); | 273 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); |
258 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnNtpOpened, | 274 |
| 275 // Cleanup procedure in M59. Remove for M62. |
| 276 registry->RegisterInt64Pref( |
| 277 kSnippetSoftFetchingIntervalOnUsageEventDeprecated, 0); |
| 278 registry->RegisterInt64Pref(kSnippetSoftFetchingIntervalOnNtpOpenedDeprecated, |
259 0); | 279 0); |
260 } | 280 } |
261 | 281 |
262 void RemoteSuggestionsSchedulerImpl::SetProvider( | 282 void RemoteSuggestionsSchedulerImpl::SetProvider( |
263 RemoteSuggestionsProvider* provider) { | 283 RemoteSuggestionsProvider* provider) { |
264 DCHECK(provider); | 284 DCHECK(provider); |
265 provider_ = provider; | 285 provider_ = provider; |
266 } | 286 } |
267 | 287 |
268 void RemoteSuggestionsSchedulerImpl::OnProviderActivated() { | 288 void RemoteSuggestionsSchedulerImpl::OnProviderActivated() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 bool RemoteSuggestionsSchedulerImpl::AcquireQuotaForInteractiveFetch() { | 321 bool RemoteSuggestionsSchedulerImpl::AcquireQuotaForInteractiveFetch() { |
302 return AcquireQuota(/*interactive_request=*/true); | 322 return AcquireQuota(/*interactive_request=*/true); |
303 } | 323 } |
304 | 324 |
305 void RemoteSuggestionsSchedulerImpl::OnInteractiveFetchFinished( | 325 void RemoteSuggestionsSchedulerImpl::OnInteractiveFetchFinished( |
306 Status fetch_status) { | 326 Status fetch_status) { |
307 OnFetchCompleted(fetch_status); | 327 OnFetchCompleted(fetch_status); |
308 } | 328 } |
309 | 329 |
310 void RemoteSuggestionsSchedulerImpl::OnPersistentSchedulerWakeUp() { | 330 void RemoteSuggestionsSchedulerImpl::OnPersistentSchedulerWakeUp() { |
311 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); | 331 RefetchInTheBackgroundIfAppropriate( |
| 332 TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); |
312 } | 333 } |
313 | 334 |
314 void RemoteSuggestionsSchedulerImpl::OnBrowserForegrounded() { | 335 void RemoteSuggestionsSchedulerImpl::OnBrowserForegrounded() { |
315 // TODO(jkrcal): Consider that this is called whenever we open or return to an | 336 // 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. | 337 // Activity. Therefore, keep work light for fast start up calls. |
317 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_FOREGROUNDED)) { | 338 RefetchInTheBackgroundIfAppropriate(TriggerType::BROWSER_FOREGROUNDED); |
318 return; | |
319 } | |
320 | |
321 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED); | |
322 } | 339 } |
323 | 340 |
324 void RemoteSuggestionsSchedulerImpl::OnBrowserColdStart() { | 341 void RemoteSuggestionsSchedulerImpl::OnBrowserColdStart() { |
325 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast | 342 // TODO(jkrcal): Consider that work here must be kept light for fast |
326 // cold start ups. | 343 // cold start ups. |
327 if (!ShouldRefetchInTheBackgroundNow(TriggerType::BROWSER_COLD_START)) { | 344 RefetchInTheBackgroundIfAppropriate(TriggerType::BROWSER_COLD_START); |
328 return; | |
329 } | |
330 | |
331 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START); | |
332 } | 345 } |
333 | 346 |
334 void RemoteSuggestionsSchedulerImpl::OnNTPOpened() { | 347 void RemoteSuggestionsSchedulerImpl::OnNTPOpened() { |
335 if (!ShouldRefetchInTheBackgroundNow(TriggerType::NTP_OPENED)) { | 348 // TODO(jkrcal): Consider that this is called whenever we open an NTP. |
336 return; | 349 // Therefore, keep work light for fast start up calls. |
337 } | 350 RefetchInTheBackgroundIfAppropriate(TriggerType::NTP_OPENED); |
338 | |
339 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); | |
340 } | 351 } |
341 | 352 |
342 void RemoteSuggestionsSchedulerImpl::StartScheduling() { | 353 void RemoteSuggestionsSchedulerImpl::StartScheduling() { |
343 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); | 354 FetchingSchedule new_schedule = GetDesiredFetchingSchedule(); |
344 | 355 |
345 if (schedule_ == new_schedule) { | 356 if (schedule_ == new_schedule) { |
346 // Do not schedule if nothing has changed; | 357 // Do not schedule if nothing has changed; |
347 return; | 358 return; |
348 } | 359 } |
349 | 360 |
(...skipping 27 matching lines...) Expand all Loading... |
377 | 388 |
378 RemoteSuggestionsSchedulerImpl::FetchingSchedule | 389 RemoteSuggestionsSchedulerImpl::FetchingSchedule |
379 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { | 390 RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { |
380 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); | 391 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); |
381 | 392 |
382 FetchingSchedule schedule; | 393 FetchingSchedule schedule; |
383 schedule.interval_persistent_wifi = | 394 schedule.interval_persistent_wifi = |
384 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); | 395 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class); |
385 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( | 396 schedule.interval_persistent_fallback = GetDesiredFetchingInterval( |
386 FetchingInterval::PERSISTENT_FALLBACK, user_class); | 397 FetchingInterval::PERSISTENT_FALLBACK, user_class); |
387 schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval( | 398 schedule.interval_soft_wifi = |
388 FetchingInterval::SOFT_ON_USAGE_EVENT, user_class); | 399 GetDesiredFetchingInterval(FetchingInterval::SOFT_WIFI, user_class); |
389 schedule.interval_soft_on_ntp_opened = GetDesiredFetchingInterval( | 400 schedule.interval_soft_fallback = |
390 FetchingInterval::SOFT_ON_NTP_OPENED, user_class); | 401 GetDesiredFetchingInterval(FetchingInterval::SOFT_FALLBACK, user_class); |
391 | 402 |
392 return schedule; | 403 return schedule; |
393 } | 404 } |
394 | 405 |
395 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { | 406 void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { |
396 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( | 407 schedule_.interval_persistent_wifi = base::TimeDelta::FromInternalValue( |
397 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); | 408 profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); |
398 schedule_.interval_persistent_fallback = | 409 schedule_.interval_persistent_fallback = |
399 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( | 410 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( |
400 prefs::kSnippetPersistentFetchingIntervalFallback)); | 411 prefs::kSnippetPersistentFetchingIntervalFallback)); |
401 schedule_.interval_soft_on_usage_event = | 412 schedule_.interval_soft_wifi = base::TimeDelta::FromInternalValue( |
402 base::TimeDelta::FromInternalValue(profile_prefs_->GetInt64( | 413 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalWifi)); |
403 prefs::kSnippetSoftFetchingIntervalOnUsageEvent)); | 414 schedule_.interval_soft_fallback = base::TimeDelta::FromInternalValue( |
404 schedule_.interval_soft_on_ntp_opened = base::TimeDelta::FromInternalValue( | 415 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalFallback)); |
405 profile_prefs_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnNtpOpened)); | |
406 } | 416 } |
407 | 417 |
408 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { | 418 void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { |
409 profile_prefs_->SetInt64( | 419 profile_prefs_->SetInt64( |
410 prefs::kSnippetPersistentFetchingIntervalWifi, | 420 prefs::kSnippetPersistentFetchingIntervalWifi, |
411 schedule_.interval_persistent_wifi.ToInternalValue()); | 421 schedule_.interval_persistent_wifi.ToInternalValue()); |
412 profile_prefs_->SetInt64( | 422 profile_prefs_->SetInt64( |
413 prefs::kSnippetPersistentFetchingIntervalFallback, | 423 prefs::kSnippetPersistentFetchingIntervalFallback, |
414 schedule_.interval_persistent_fallback.ToInternalValue()); | 424 schedule_.interval_persistent_fallback.ToInternalValue()); |
415 profile_prefs_->SetInt64( | 425 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalWifi, |
416 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 426 schedule_.interval_soft_wifi.ToInternalValue()); |
417 schedule_.interval_soft_on_usage_event.ToInternalValue()); | 427 profile_prefs_->SetInt64(prefs::kSnippetSoftFetchingIntervalFallback, |
418 profile_prefs_->SetInt64( | 428 schedule_.interval_soft_fallback.ToInternalValue()); |
419 prefs::kSnippetSoftFetchingIntervalOnNtpOpened, | |
420 schedule_.interval_soft_on_ntp_opened.ToInternalValue()); | |
421 } | 429 } |
422 | 430 |
423 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfEnabled( | 431 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundIfAppropriate( |
424 TriggerType trigger) { | 432 TriggerType trigger) { |
| 433 if (background_fetch_in_progress_) { |
| 434 return; |
| 435 } |
| 436 |
425 if (BackgroundFetchesDisabled(trigger)) { | 437 if (BackgroundFetchesDisabled(trigger)) { |
426 return; | 438 return; |
427 } | 439 } |
428 | 440 |
429 UMA_HISTOGRAM_ENUMERATION( | 441 if (trigger != TriggerType::PERSISTENT_SCHEDULER_WAKE_UP && |
430 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", | 442 !ShouldRefetchInTheBackgroundNow()) { |
431 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); | |
432 | |
433 RefetchInTheBackground(); | |
434 } | |
435 | |
436 void RemoteSuggestionsSchedulerImpl::RefetchInTheBackground() { | |
437 if (background_fetch_in_progress_) { | |
438 return; | 443 return; |
439 } | 444 } |
440 | 445 |
441 if (!AcquireQuota(/*interactive_request=*/false)) { | 446 if (!AcquireQuota(/*interactive_request=*/false)) { |
442 return; | 447 return; |
443 } | 448 } |
444 | 449 |
| 450 UMA_HISTOGRAM_ENUMERATION( |
| 451 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", |
| 452 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); |
| 453 |
445 background_fetch_in_progress_ = true; | 454 background_fetch_in_progress_ = true; |
446 provider_->RefetchInTheBackground(base::Bind( | 455 provider_->RefetchInTheBackground(base::Bind( |
447 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, | 456 &RemoteSuggestionsSchedulerImpl::RefetchInTheBackgroundFinished, |
448 base::Unretained(this))); | 457 base::Unretained(this))); |
449 } | 458 } |
450 | 459 |
451 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow( | 460 bool RemoteSuggestionsSchedulerImpl::ShouldRefetchInTheBackgroundNow() { |
452 TriggerType trigger) { | |
453 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( | 461 const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( |
454 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); | 462 profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttempt)); |
455 base::Time first_allowed_fetch_time; | 463 |
456 switch (trigger) { | 464 // If we have no persistent scheduler to ask, err on the side of caution. |
457 case TriggerType::NTP_OPENED: | 465 bool wifi = false; |
458 first_allowed_fetch_time = | 466 if (persistent_scheduler_) { |
459 last_fetch_attempt_time + schedule_.interval_soft_on_ntp_opened; | 467 wifi = persistent_scheduler_->IsOnUnmeteredConnection(); |
460 break; | |
461 case TriggerType::BROWSER_FOREGROUNDED: | |
462 case TriggerType::BROWSER_COLD_START: | |
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 } | 468 } |
| 469 |
| 470 base::Time first_allowed_fetch_time = |
| 471 last_fetch_attempt_time + |
| 472 (wifi ? schedule_.interval_soft_wifi : schedule_.interval_soft_fallback); |
471 base::Time now = clock_->Now(); | 473 base::Time now = clock_->Now(); |
472 | |
473 return background_fetches_allowed_after_ <= now && | 474 return background_fetches_allowed_after_ <= now && |
474 first_allowed_fetch_time <= now; | 475 first_allowed_fetch_time <= now; |
475 } | 476 } |
476 | 477 |
477 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( | 478 bool RemoteSuggestionsSchedulerImpl::BackgroundFetchesDisabled( |
478 TriggerType trigger) const { | 479 TriggerType trigger) const { |
479 if (!provider_) { | 480 if (!provider_) { |
480 return true; // Cannot fetch as remote suggestions provider does not exist. | 481 return true; // Cannot fetch as remote suggestions provider does not exist. |
481 } | 482 } |
482 | 483 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 return enabled_types; | 575 return enabled_types; |
575 } | 576 } |
576 | 577 |
577 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> | 578 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> |
578 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { | 579 RemoteSuggestionsSchedulerImpl::GetDefaultEnabledTriggerTypes() { |
579 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | 580 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, |
580 TriggerType::BROWSER_FOREGROUNDED}; | 581 TriggerType::BROWSER_FOREGROUNDED}; |
581 } | 582 } |
582 | 583 |
583 } // namespace ntp_snippets | 584 } // namespace ntp_snippets |
OLD | NEW |