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 <random> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/metrics/histogram_macros.h" | |
13 #include "base/strings/string_split.h" | |
12 #include "base/time/clock.h" | 14 #include "base/time/clock.h" |
13 #include "components/ntp_snippets/features.h" | 15 #include "components/ntp_snippets/features.h" |
14 #include "components/ntp_snippets/pref_names.h" | 16 #include "components/ntp_snippets/pref_names.h" |
15 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 17 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
16 #include "components/ntp_snippets/status.h" | 18 #include "components/ntp_snippets/status.h" |
17 #include "components/ntp_snippets/user_classifier.h" | 19 #include "components/ntp_snippets/user_classifier.h" |
18 #include "components/prefs/pref_registry_simple.h" | 20 #include "components/prefs/pref_registry_simple.h" |
19 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
20 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
21 | 23 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 static_cast<unsigned int>(FetchingInterval::COUNT) == | 60 static_cast<unsigned int>(FetchingInterval::COUNT) == |
59 arraysize(kDefaultFetchingIntervalActiveSuggestionsConsumer) && | 61 arraysize(kDefaultFetchingIntervalActiveSuggestionsConsumer) && |
60 static_cast<unsigned int>(FetchingInterval::COUNT) == | 62 static_cast<unsigned int>(FetchingInterval::COUNT) == |
61 arraysize(kFetchingIntervalParamNameRareNtpUser) && | 63 arraysize(kFetchingIntervalParamNameRareNtpUser) && |
62 static_cast<unsigned int>(FetchingInterval::COUNT) == | 64 static_cast<unsigned int>(FetchingInterval::COUNT) == |
63 arraysize(kFetchingIntervalParamNameActiveNtpUser) && | 65 arraysize(kFetchingIntervalParamNameActiveNtpUser) && |
64 static_cast<unsigned int>(FetchingInterval::COUNT) == | 66 static_cast<unsigned int>(FetchingInterval::COUNT) == |
65 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), | 67 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer), |
66 "Fill in all the info for fetching intervals."); | 68 "Fill in all the info for fetching intervals."); |
67 | 69 |
70 const char* kTriggerTypeNames[] = {"persistent_scheduler_wake_up", "ntp_opened", | |
71 "browser_foregrounded", | |
72 "browser_cold_start"}; | |
73 | |
74 const char* kTriggerTypesParamName = "scheduler_trigger_types"; | |
75 const char* kTriggerTypesParamValueForEmptyList = "-"; | |
tschumann
2017/01/12 12:44:20
would be curious to understand why the empty strin
jkrcal
2017/01/12 15:35:28
No, there is no way. This is something to fix, in
| |
76 | |
68 base::TimeDelta GetDesiredFetchingInterval( | 77 base::TimeDelta GetDesiredFetchingInterval( |
69 FetchingInterval interval, | 78 FetchingInterval interval, |
70 UserClassifier::UserClass user_class) { | 79 UserClassifier::UserClass user_class) { |
71 double default_value_hours = 0.0; | 80 double default_value_hours = 0.0; |
72 | 81 |
73 DCHECK(interval != FetchingInterval::COUNT); | 82 DCHECK(interval != FetchingInterval::COUNT); |
74 const unsigned int index = static_cast<unsigned int>(interval); | 83 const unsigned int index = static_cast<unsigned int>(interval); |
75 DCHECK(index < arraysize(kDefaultFetchingIntervalRareNtpUser)); | 84 DCHECK(index < arraysize(kDefaultFetchingIntervalRareNtpUser)); |
76 | 85 |
77 const char* param_name = nullptr; | 86 const char* param_name = nullptr; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 const FetchingSchedule& other) const { | 127 const FetchingSchedule& other) const { |
119 return !operator==(other); | 128 return !operator==(other); |
120 } | 129 } |
121 | 130 |
122 bool SchedulingRemoteSuggestionsProvider::FetchingSchedule::is_empty() const { | 131 bool SchedulingRemoteSuggestionsProvider::FetchingSchedule::is_empty() const { |
123 return interval_persistent_wifi.is_zero() && | 132 return interval_persistent_wifi.is_zero() && |
124 interval_persistent_fallback.is_zero() && | 133 interval_persistent_fallback.is_zero() && |
125 interval_soft_on_usage_event.is_zero(); | 134 interval_soft_on_usage_event.is_zero(); |
126 } | 135 } |
127 | 136 |
137 // These values are written to logs. New enum values can be added, but existing | |
138 // enums must never be renumbered or deleted and reused. When adding new | |
139 // entries, also update the array |kTriggerTypeNames| above. | |
140 enum class SchedulingRemoteSuggestionsProvider::TriggerType { | |
141 PERSISTENT_SCHEDULER_WAKE_UP = 0, | |
142 NTP_OPENED = 1, | |
143 BROWSER_FOREGROUNDED = 2, | |
144 BROWSER_COLD_START = 3, | |
145 COUNT | |
146 }; | |
147 | |
128 SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider( | 148 SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider( |
129 Observer* observer, | 149 Observer* observer, |
130 std::unique_ptr<RemoteSuggestionsProvider> provider, | 150 std::unique_ptr<RemoteSuggestionsProvider> provider, |
131 PersistentScheduler* persistent_scheduler, | 151 PersistentScheduler* persistent_scheduler, |
132 const UserClassifier* user_classifier, | 152 const UserClassifier* user_classifier, |
133 PrefService* pref_service, | 153 PrefService* pref_service, |
134 std::unique_ptr<base::Clock> clock) | 154 std::unique_ptr<base::Clock> clock) |
135 : RemoteSuggestionsProvider(observer), | 155 : RemoteSuggestionsProvider(observer), |
136 RemoteSuggestionsScheduler(), | 156 RemoteSuggestionsScheduler(), |
137 provider_(std::move(provider)), | 157 provider_(std::move(provider)), |
138 persistent_scheduler_(persistent_scheduler), | 158 persistent_scheduler_(persistent_scheduler), |
139 background_fetch_in_progress_(false), | 159 background_fetch_in_progress_(false), |
140 user_classifier_(user_classifier), | 160 user_classifier_(user_classifier), |
141 pref_service_(pref_service), | 161 pref_service_(pref_service), |
142 clock_(std::move(clock)) { | 162 clock_(std::move(clock)), |
163 enabled_triggers_(GetEnabledTriggerTypes()) { | |
143 DCHECK(user_classifier); | 164 DCHECK(user_classifier); |
144 DCHECK(pref_service); | 165 DCHECK(pref_service); |
145 | 166 |
146 LoadLastFetchingSchedule(); | 167 LoadLastFetchingSchedule(); |
147 | 168 |
148 provider_->SetProviderStatusCallback( | 169 provider_->SetProviderStatusCallback( |
149 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | 170 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( |
150 base::BindRepeating( | 171 base::BindRepeating( |
151 &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged, | 172 &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged, |
152 base::Unretained(this)))); | 173 base::Unretained(this)))); |
(...skipping 13 matching lines...) Expand all Loading... | |
166 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); | 187 registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttempt, 0); |
167 } | 188 } |
168 | 189 |
169 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { | 190 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { |
170 // Force the reschedule by stopping and starting it again. | 191 // Force the reschedule by stopping and starting it again. |
171 StopScheduling(); | 192 StopScheduling(); |
172 StartScheduling(); | 193 StartScheduling(); |
173 } | 194 } |
174 | 195 |
175 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { | 196 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { |
176 if (BackgroundFetchesDisabled()) { | 197 RefetchInTheBackgroundIfEnabled(TriggerType::PERSISTENT_SCHEDULER_WAKE_UP); |
177 return; | |
178 } | |
179 | |
180 RefetchInTheBackground(/*callback=*/nullptr); | |
181 } | 198 } |
182 | 199 |
183 void SchedulingRemoteSuggestionsProvider::OnBrowserForegrounded() { | 200 void SchedulingRemoteSuggestionsProvider::OnBrowserForegrounded() { |
184 // TODO(jkrcal): Consider that this is called whenever we open or return to an | 201 // TODO(jkrcal): Consider that this is called whenever we open or return to an |
185 // Activity. Therefore, keep work light for fast start up calls. | 202 // Activity. Therefore, keep work light for fast start up calls. |
186 // TODO(jkrcal): Implement. | 203 if (!ShouldRefetchInTheBackgroundNow()) { |
204 return; | |
205 } | |
206 | |
207 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_FOREGROUNDED); | |
187 } | 208 } |
188 | 209 |
189 void SchedulingRemoteSuggestionsProvider::OnBrowserColdStart() { | 210 void SchedulingRemoteSuggestionsProvider::OnBrowserColdStart() { |
190 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast | 211 // TODO(fhorschig|jkrcal): Consider that work here must be kept light for fast |
191 // cold start ups. | 212 // cold start ups. |
192 // TODO(jkrcal): Implement. | 213 if (!ShouldRefetchInTheBackgroundNow()) { |
214 return; | |
215 } | |
216 | |
217 RefetchInTheBackgroundIfEnabled(TriggerType::BROWSER_COLD_START); | |
193 } | 218 } |
194 | 219 |
195 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { | 220 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { |
196 if (BackgroundFetchesDisabled() || !ShouldRefetchInTheBackgroundNow()) { | 221 if (!ShouldRefetchInTheBackgroundNow()) { |
197 return; | 222 return; |
198 } | 223 } |
199 | 224 |
200 RefetchInTheBackground(/*callback=*/nullptr); | 225 RefetchInTheBackgroundIfEnabled(TriggerType::NTP_OPENED); |
201 } | 226 } |
202 | 227 |
203 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( | 228 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( |
204 std::unique_ptr<ProviderStatusCallback> callback) { | 229 std::unique_ptr<ProviderStatusCallback> callback) { |
205 provider_->SetProviderStatusCallback(std::move(callback)); | 230 provider_->SetProviderStatusCallback(std::move(callback)); |
206 } | 231 } |
207 | 232 |
208 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( | 233 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( |
209 std::unique_ptr<FetchStatusCallback> callback) { | 234 std::unique_ptr<FetchStatusCallback> callback) { |
210 if (background_fetch_in_progress_) { | 235 if (background_fetch_in_progress_) { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 pref_service_->SetInt64(prefs::kSnippetPersistentFetchingIntervalWifi, | 394 pref_service_->SetInt64(prefs::kSnippetPersistentFetchingIntervalWifi, |
370 schedule_.interval_persistent_wifi.ToInternalValue()); | 395 schedule_.interval_persistent_wifi.ToInternalValue()); |
371 pref_service_->SetInt64( | 396 pref_service_->SetInt64( |
372 prefs::kSnippetPersistentFetchingIntervalFallback, | 397 prefs::kSnippetPersistentFetchingIntervalFallback, |
373 schedule_.interval_persistent_fallback.ToInternalValue()); | 398 schedule_.interval_persistent_fallback.ToInternalValue()); |
374 pref_service_->SetInt64( | 399 pref_service_->SetInt64( |
375 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, | 400 prefs::kSnippetSoftFetchingIntervalOnUsageEvent, |
376 schedule_.interval_soft_on_usage_event.ToInternalValue()); | 401 schedule_.interval_soft_on_usage_event.ToInternalValue()); |
377 } | 402 } |
378 | 403 |
379 bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled() const { | 404 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundIfEnabled( |
380 return schedule_.is_empty(); | 405 SchedulingRemoteSuggestionsProvider::TriggerType trigger) { |
406 if (BackgroundFetchesDisabled(trigger)) { | |
407 return; | |
408 } | |
409 | |
410 UMA_HISTOGRAM_ENUMERATION( | |
411 "NewTabPage.ContentSuggestions.BackgroundFetchTrigger", | |
412 static_cast<int>(trigger), static_cast<int>(TriggerType::COUNT)); | |
413 | |
414 RefetchInTheBackground(/*callback=*/nullptr); | |
381 } | 415 } |
382 | 416 |
383 bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow() { | 417 bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow() { |
384 base::Time first_allowed_fetch_time = | 418 base::Time first_allowed_fetch_time = |
385 base::Time::FromInternalValue( | 419 base::Time::FromInternalValue( |
386 pref_service_->GetInt64(prefs::kSnippetLastFetchAttempt)) + | 420 pref_service_->GetInt64(prefs::kSnippetLastFetchAttempt)) + |
387 schedule_.interval_soft_on_usage_event; | 421 schedule_.interval_soft_on_usage_event; |
388 return first_allowed_fetch_time <= clock_->Now(); | 422 return first_allowed_fetch_time <= clock_->Now(); |
389 } | 423 } |
390 | 424 |
425 bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled( | |
426 SchedulingRemoteSuggestionsProvider::TriggerType trigger) const { | |
427 if (schedule_.is_empty()) { | |
428 return true; // Background fetches are disabled in general. | |
429 } | |
430 | |
431 if (enabled_triggers_.count(trigger) == 0) { | |
432 return true; // Background fetches for |trigger| are not enabled. | |
433 } | |
434 return false; | |
435 } | |
436 | |
391 void SchedulingRemoteSuggestionsProvider::FetchFinished( | 437 void SchedulingRemoteSuggestionsProvider::FetchFinished( |
392 const FetchDoneCallback& callback, | 438 const FetchDoneCallback& callback, |
393 Status fetch_status, | 439 Status fetch_status, |
394 std::vector<ContentSuggestion> suggestions) { | 440 std::vector<ContentSuggestion> suggestions) { |
395 OnFetchCompleted(fetch_status); | 441 OnFetchCompleted(fetch_status); |
396 if (callback) { | 442 if (callback) { |
397 callback.Run(fetch_status, std::move(suggestions)); | 443 callback.Run(fetch_status, std::move(suggestions)); |
398 } | 444 } |
399 } | 445 } |
400 | 446 |
(...skipping 15 matching lines...) Expand all Loading... | |
416 // Reschedule after a fetch. The persistent schedule is applied only after a | 462 // Reschedule after a fetch. The persistent schedule is applied only after a |
417 // successful fetch. After a failed fetch, we want to keep the previous | 463 // successful fetch. After a failed fetch, we want to keep the previous |
418 // persistent schedule intact so that we eventually get a persistent | 464 // persistent schedule intact so that we eventually get a persistent |
419 // fallback fetch (if the wifi persistent fetches keep failing). | 465 // fallback fetch (if the wifi persistent fetches keep failing). |
420 if (fetch_status.code != StatusCode::SUCCESS) { | 466 if (fetch_status.code != StatusCode::SUCCESS) { |
421 return; | 467 return; |
422 } | 468 } |
423 ApplyPersistentFetchingSchedule(); | 469 ApplyPersistentFetchingSchedule(); |
424 } | 470 } |
425 | 471 |
472 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> | |
473 SchedulingRemoteSuggestionsProvider::GetEnabledTriggerTypes() { | |
474 static_assert(static_cast<unsigned int>(TriggerType::COUNT) == | |
475 arraysize(kTriggerTypeNames), | |
476 "Fill in names for trigger types."); | |
477 | |
478 std::string param_value = variations::GetVariationParamValueByFeature( | |
479 ntp_snippets::kArticleSuggestionsFeature, kTriggerTypesParamName); | |
480 if (param_value == kTriggerTypesParamValueForEmptyList) { | |
481 return std::set<TriggerType>(); | |
482 } | |
483 | |
484 std::vector<std::string> tokens = base::SplitString( | |
485 param_value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
486 if (tokens.empty()) { | |
487 return GetDefaultEnabledTriggerTypes(); | |
488 } | |
489 | |
490 std::set<TriggerType> enabled_types; | |
491 for (const auto& token : tokens) { | |
492 auto it = std::find(std::begin(kTriggerTypeNames), | |
493 std::end(kTriggerTypeNames), token); | |
494 if (it == std::end(kTriggerTypeNames)) { | |
495 DLOG(WARNING) << "Failed to parse variation param " | |
496 << kTriggerTypesParamName << " with string value " | |
497 << param_value | |
498 << " into a comma-separated list of keywords. " | |
499 << "Unknown token " << token | |
500 << " found. Falling back to default value."; | |
501 return GetDefaultEnabledTriggerTypes(); | |
502 } | |
503 | |
504 // Add the enabled type represented by |token| into the result set. | |
505 enabled_types.insert( | |
506 static_cast<TriggerType>(it - std::begin(kTriggerTypeNames))); | |
507 } | |
508 return enabled_types; | |
509 } | |
510 | |
511 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> | |
512 SchedulingRemoteSuggestionsProvider::GetDefaultEnabledTriggerTypes() { | |
513 return {TriggerType::PERSISTENT_SCHEDULER_WAKE_UP, TriggerType::NTP_OPENED, | |
514 TriggerType::BROWSER_FOREGROUNDED}; | |
515 } | |
516 | |
426 } // namespace ntp_snippets | 517 } // namespace ntp_snippets |
OLD | NEW |