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

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

Issue 2611523004: [Background fetching] Background fetching when opening an NTP. (Closed)
Patch Set: Tim's comments Created 3 years, 11 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
« no previous file with comments | « components/ntp_snippets/remote/scheduling_remote_suggestions_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/scheduling_remote_suggestions_provider. h" 5 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider. h"
6 6
7 #include <random>
7 #include <string> 8 #include <string>
8 #include <utility> 9 #include <utility>
9 10
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "components/ntp_snippets/features.h" 12 #include "components/ntp_snippets/features.h"
12 #include "components/ntp_snippets/pref_names.h" 13 #include "components/ntp_snippets/pref_names.h"
13 #include "components/ntp_snippets/remote/persistent_scheduler.h" 14 #include "components/ntp_snippets/remote/persistent_scheduler.h"
14 #include "components/ntp_snippets/status.h" 15 #include "components/ntp_snippets/status.h"
15 #include "components/ntp_snippets/user_classifier.h" 16 #include "components/ntp_snippets/user_classifier.h"
16 #include "components/prefs/pref_registry_simple.h" 17 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/pref_service.h" 18 #include "components/prefs/pref_service.h"
18 #include "components/variations/variations_associated_data.h" 19 #include "components/variations/variations_associated_data.h"
19 20
20 namespace ntp_snippets { 21 namespace ntp_snippets {
21 22
22 namespace { 23 namespace {
23 24
25 enum class FetchingInterval {
26 PERSISTENT_FALLBACK,
27 PERSISTENT_WIFI,
28 SOFT_ON_USAGE_EVENT,
29 COUNT
30 };
31
24 // Default values for fetching intervals, fallback and wifi. 32 // Default values for fetching intervals, fallback and wifi.
25 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0}; 33 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0, 12.0};
26 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0}; 34 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0, 2.0};
27 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0}; 35 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0,
36 2.0};
28 37
29 // Variation parameters than can the default fetching intervals. 38 // Variation parameters than can the default fetching intervals.
30 const char* kFetchingIntervalParamNameRareNtpUser[] = { 39 const char* kFetchingIntervalParamNameRareNtpUser[] = {
31 "fetching_interval_hours-fallback-rare_ntp_user", 40 "fetching_interval_hours-fallback-rare_ntp_user",
32 "fetching_interval_hours-wifi-rare_ntp_user"}; 41 "fetching_interval_hours-wifi-rare_ntp_user",
42 "soft_fetching_interval_hours-active-rare_ntp_user"};
33 const char* kFetchingIntervalParamNameActiveNtpUser[] = { 43 const char* kFetchingIntervalParamNameActiveNtpUser[] = {
34 "fetching_interval_hours-fallback-active_ntp_user", 44 "fetching_interval_hours-fallback-active_ntp_user",
35 "fetching_interval_hours-wifi-active_ntp_user"}; 45 "fetching_interval_hours-wifi-active_ntp_user",
46 "soft_fetching_interval_hours-active-active_ntp_user"};
36 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = { 47 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = {
37 "fetching_interval_hours-fallback-active_suggestions_consumer", 48 "fetching_interval_hours-fallback-active_suggestions_consumer",
38 "fetching_interval_hours-wifi-active_suggestions_consumer"}; 49 "fetching_interval_hours-wifi-active_suggestions_consumer",
50 "soft_fetching_interval_hours-active-active_suggestions_consumer"};
39 51
40 base::TimeDelta GetDesiredUpdateInterval( 52 static_assert(
41 bool is_wifi, 53 static_cast<unsigned int>(FetchingInterval::COUNT) ==
54 arraysize(kDefaultFetchingIntervalRareNtpUser) &&
55 static_cast<unsigned int>(FetchingInterval::COUNT) ==
56 arraysize(kDefaultFetchingIntervalActiveNtpUser) &&
57 static_cast<unsigned int>(FetchingInterval::COUNT) ==
58 arraysize(kDefaultFetchingIntervalActiveSuggestionsConsumer) &&
59 static_cast<unsigned int>(FetchingInterval::COUNT) ==
60 arraysize(kFetchingIntervalParamNameRareNtpUser) &&
61 static_cast<unsigned int>(FetchingInterval::COUNT) ==
62 arraysize(kFetchingIntervalParamNameActiveNtpUser) &&
63 static_cast<unsigned int>(FetchingInterval::COUNT) ==
64 arraysize(kFetchingIntervalParamNameActiveSuggestionsConsumer),
65 "Fill in all the info for fetching intervals.");
66
67 base::TimeDelta GetDesiredFetchingInterval(
68 FetchingInterval interval,
42 UserClassifier::UserClass user_class) { 69 UserClassifier::UserClass user_class) {
43 double default_value_hours = 0.0; 70 double default_value_hours = 0.0;
44 71
45 const int index = is_wifi ? 1 : 0; 72 DCHECK(interval != FetchingInterval::COUNT);
73 const unsigned int index = static_cast<unsigned int>(interval);
74 DCHECK(index < arraysize(kDefaultFetchingIntervalRareNtpUser));
75
46 const char* param_name = nullptr; 76 const char* param_name = nullptr;
47 switch (user_class) { 77 switch (user_class) {
48 case UserClassifier::UserClass::RARE_NTP_USER: 78 case UserClassifier::UserClass::RARE_NTP_USER:
49 default_value_hours = kDefaultFetchingIntervalRareNtpUser[index]; 79 default_value_hours = kDefaultFetchingIntervalRareNtpUser[index];
50 param_name = kFetchingIntervalParamNameRareNtpUser[index]; 80 param_name = kFetchingIntervalParamNameRareNtpUser[index];
51 break; 81 break;
52 case UserClassifier::UserClass::ACTIVE_NTP_USER: 82 case UserClassifier::UserClass::ACTIVE_NTP_USER:
53 default_value_hours = kDefaultFetchingIntervalActiveNtpUser[index]; 83 default_value_hours = kDefaultFetchingIntervalActiveNtpUser[index];
54 param_name = kFetchingIntervalParamNameActiveNtpUser[index]; 84 param_name = kFetchingIntervalParamNameActiveNtpUser[index];
55 break; 85 break;
56 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: 86 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
57 default_value_hours = 87 default_value_hours =
58 kDefaultFetchingIntervalActiveSuggestionsConsumer[index]; 88 kDefaultFetchingIntervalActiveSuggestionsConsumer[index];
59 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index]; 89 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index];
60 break; 90 break;
61 } 91 }
62 92
63 double value_hours = variations::GetVariationParamByFeatureAsDouble( 93 double value_hours = variations::GetVariationParamByFeatureAsDouble(
64 ntp_snippets::kArticleSuggestionsFeature, param_name, 94 ntp_snippets::kArticleSuggestionsFeature, param_name,
65 default_value_hours); 95 default_value_hours);
66 96
67 return base::TimeDelta::FromSecondsD(value_hours * 3600.0); 97 return base::TimeDelta::FromSecondsD(value_hours * 3600.0);
68 } 98 }
69 99
70 } // namespace 100 } // namespace
71 101
72 struct SchedulingRemoteSuggestionsProvider::FetchingSchedule { 102 struct SchedulingRemoteSuggestionsProvider::FetchingSchedule {
73 base::TimeDelta interval_wifi; 103 base::TimeDelta interval_persistent_wifi;
74 base::TimeDelta interval_fallback; 104 base::TimeDelta interval_persistent_fallback;
105 base::TimeDelta interval_soft_on_usage_event;
75 106
76 static FetchingSchedule Empty() { 107 static FetchingSchedule Empty() {
77 return FetchingSchedule{base::TimeDelta(), 108 return FetchingSchedule{base::TimeDelta(), base::TimeDelta(),
78 base::TimeDelta()}; 109 base::TimeDelta()};
79 } 110 }
80 111
81 bool operator==(const FetchingSchedule& other) const { 112 bool operator==(const FetchingSchedule& other) const {
82 return interval_wifi == other.interval_wifi && 113 return interval_persistent_wifi == other.interval_persistent_wifi &&
83 interval_fallback == other.interval_fallback; 114 interval_persistent_fallback == other.interval_persistent_fallback &&
115 interval_soft_on_usage_event == other.interval_soft_on_usage_event;
84 } 116 }
85 117
86 bool operator!=(const FetchingSchedule& other) const { 118 bool operator!=(const FetchingSchedule& other) const {
87 return !operator==(other); 119 return !operator==(other);
88 } 120 }
89 121
90 bool is_empty() const { 122 bool is_empty() const {
91 return interval_wifi.is_zero() && interval_fallback.is_zero(); 123 return interval_persistent_wifi.is_zero() &&
124 interval_persistent_fallback.is_zero() &&
125 interval_soft_on_usage_event.is_zero();
92 } 126 }
93 }; 127 };
94 128
95 SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider( 129 SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider(
96 Observer* observer, 130 Observer* observer,
97 std::unique_ptr<RemoteSuggestionsProvider> provider, 131 std::unique_ptr<RemoteSuggestionsProvider> provider,
98 PersistentScheduler* persistent_scheduler, 132 PersistentScheduler* persistent_scheduler,
99 const UserClassifier* user_classifier, 133 const UserClassifier* user_classifier,
100 PrefService* pref_service) 134 PrefService* pref_service)
101 : RemoteSuggestionsProvider(observer), 135 : RemoteSuggestionsProvider(observer),
(...skipping 11 matching lines...) Expand all
113 &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged, 147 &SchedulingRemoteSuggestionsProvider::OnProviderStatusChanged,
114 base::Unretained(this)))); 148 base::Unretained(this))));
115 } 149 }
116 150
117 SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() = 151 SchedulingRemoteSuggestionsProvider::~SchedulingRemoteSuggestionsProvider() =
118 default; 152 default;
119 153
120 // static 154 // static
121 void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( 155 void SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs(
122 PrefRegistrySimple* registry) { 156 PrefRegistrySimple* registry) {
123 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0); 157 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0);
124 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback, 158 registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback,
125 0); 159 0);
160 registry->RegisterInt64Pref(prefs::kSnippetSoftFetchingIntervalOnUsageEvent,
161 0);
162 registry->RegisterInt64Pref(prefs::kSnippetLastBackgroundFetchAttempt, 0);
126 } 163 }
127 164
128 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() { 165 void SchedulingRemoteSuggestionsProvider::RescheduleFetching() {
129 // Force the reschedule by stopping and starting it again. 166 // Force the reschedule by stopping and starting it again.
130 StopScheduling(); 167 StopScheduling();
131 StartScheduling(); 168 StartScheduling();
132 } 169 }
133 170
134 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() { 171 void SchedulingRemoteSuggestionsProvider::OnPersistentSchedulerWakeUp() {
135 provider_->RefetchInTheBackground( 172 RefetchInTheBackground(/*callback=*/nullptr);
136 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>(
137 base::Bind(&SchedulingRemoteSuggestionsProvider::OnFetchCompleted,
138 base::Unretained(this))));
139 } 173 }
140 174
141 void SchedulingRemoteSuggestionsProvider::OnBrowserStartup() { 175 void SchedulingRemoteSuggestionsProvider::OnBrowserStartup() {
142 // TODO(jkrcal): Implement. 176 // TODO(jkrcal): Implement.
143 } 177 }
144 178
145 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() { 179 void SchedulingRemoteSuggestionsProvider::OnNTPOpened() {
146 // TODO(jkrcal): Implement. 180 if (!ShouldRefetchInTheBackgroundNow()) {
181 return;
182 }
183 RefetchInTheBackground(/*callback=*/nullptr);
147 } 184 }
148 185
149 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback( 186 void SchedulingRemoteSuggestionsProvider::SetProviderStatusCallback(
150 std::unique_ptr<ProviderStatusCallback> callback) { 187 std::unique_ptr<ProviderStatusCallback> callback) {
151 provider_->SetProviderStatusCallback(std::move(callback)); 188 provider_->SetProviderStatusCallback(std::move(callback));
152 } 189 }
153 190
154 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground( 191 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground(
155 std::unique_ptr<FetchStatusCallback> callback) { 192 std::unique_ptr<FetchStatusCallback> callback) {
156 provider_->RefetchInTheBackground(std::move(callback)); 193 if (background_fetch_in_progress_) {
194 if (callback) {
195 callback->Run(
196 Status(StatusCode::TEMPORARY_ERROR, "Background fetch in progress"));
197 }
198 return;
199 }
200
201 RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind(
202 &SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished,
203 base::Unretained(this), base::Passed(&callback));
204 provider_->RefetchInTheBackground(
205 base::MakeUnique<RemoteSuggestionsProvider::FetchStatusCallback>(
206 std::move(wrapper_callback)));
157 } 207 }
158 208
159 const NTPSnippetsFetcher* SchedulingRemoteSuggestionsProvider:: 209 const NTPSnippetsFetcher* SchedulingRemoteSuggestionsProvider::
160 snippets_fetcher_for_testing_and_debugging() const { 210 snippets_fetcher_for_testing_and_debugging() const {
161 return provider_->snippets_fetcher_for_testing_and_debugging(); 211 return provider_->snippets_fetcher_for_testing_and_debugging();
162 } 212 }
163 213
164 CategoryStatus SchedulingRemoteSuggestionsProvider::GetCategoryStatus( 214 CategoryStatus SchedulingRemoteSuggestionsProvider::GetCategoryStatus(
165 Category category) { 215 Category category) {
166 return provider_->GetCategoryStatus(category); 216 return provider_->GetCategoryStatus(category);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 StartScheduling(); 280 StartScheduling();
231 return; 281 return;
232 case RemoteSuggestionsProvider::ProviderStatus::INACTIVE: 282 case RemoteSuggestionsProvider::ProviderStatus::INACTIVE:
233 StopScheduling(); 283 StopScheduling();
234 return; 284 return;
235 } 285 }
236 NOTREACHED(); 286 NOTREACHED();
237 } 287 }
238 288
239 void SchedulingRemoteSuggestionsProvider::StartScheduling() { 289 void SchedulingRemoteSuggestionsProvider::StartScheduling() {
240 // The scheduler only exists on Android so far, it's null on other platforms.
241 if (!persistent_scheduler_) {
242 return;
243 }
244
245 FetchingSchedule last_schedule = GetLastFetchingSchedule(); 290 FetchingSchedule last_schedule = GetLastFetchingSchedule();
246 FetchingSchedule schedule = GetDesiredFetchingSchedule(); 291 FetchingSchedule schedule = GetDesiredFetchingSchedule();
247 292
248 // Reset the schedule only if the parameters have changed. 293 // Reset the schedule only if the parameters have changed.
249 if (last_schedule != schedule) { 294 if (last_schedule != schedule) {
250 ApplyFetchingSchedule(schedule); 295 ApplyFetchingSchedule(schedule, /*also_apply_persistent_schedule=*/true);
251 } 296 }
252 } 297 }
253 298
254 void SchedulingRemoteSuggestionsProvider::StopScheduling() { 299 void SchedulingRemoteSuggestionsProvider::StopScheduling() {
255 // The scheduler only exists on Android so far, it's null on other platforms.
256 if (!persistent_scheduler_) {
257 return;
258 }
259
260 // Do not unschedule if already switched off 300 // Do not unschedule if already switched off
261 FetchingSchedule last_schedule = GetLastFetchingSchedule(); 301 FetchingSchedule last_schedule = GetLastFetchingSchedule();
262 if (last_schedule.is_empty()) { 302 if (last_schedule.is_empty()) {
263 return; 303 return;
264 } 304 }
265 305
266 persistent_scheduler_->Unschedule(); 306 pref_service_->ClearPref(prefs::kSnippetLastBackgroundFetchAttempt);
307
308 // The scheduler only exists on Android so far, it's null on other platforms.
309 if (persistent_scheduler_) {
310 persistent_scheduler_->Unschedule();
311 }
267 312
268 StoreLastFetchingSchedule(FetchingSchedule::Empty()); 313 StoreLastFetchingSchedule(FetchingSchedule::Empty());
269 } 314 }
270 315
271 void SchedulingRemoteSuggestionsProvider::ApplyFetchingSchedule( 316 void SchedulingRemoteSuggestionsProvider::ApplyFetchingSchedule(
272 const FetchingSchedule& schedule) { 317 const FetchingSchedule& schedule,
273 persistent_scheduler_->Schedule(schedule.interval_wifi, 318 bool also_apply_persistent_schedule) {
274 schedule.interval_fallback); 319 pref_service_->SetInt64(prefs::kSnippetLastBackgroundFetchAttempt,
tschumann 2017/01/03 18:54:25 from an offline discussion: We also call this on S
jkrcal 2017/01/04 10:06:30 Fixed, in the end.
320 base::Time::Now().ToInternalValue());
321
322 // The scheduler only exists on Android so far, it's null on other platforms.
323 if (persistent_scheduler_ && also_apply_persistent_schedule) {
324 persistent_scheduler_->Schedule(schedule.interval_persistent_wifi,
325 schedule.interval_persistent_fallback);
326 }
275 327
276 StoreLastFetchingSchedule(schedule); 328 StoreLastFetchingSchedule(schedule);
277 } 329 }
278 330
279 SchedulingRemoteSuggestionsProvider::FetchingSchedule 331 SchedulingRemoteSuggestionsProvider::FetchingSchedule
280 SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const { 332 SchedulingRemoteSuggestionsProvider::GetDesiredFetchingSchedule() const {
281 UserClassifier::UserClass user_class = user_classifier_->GetUserClass(); 333 UserClassifier::UserClass user_class = user_classifier_->GetUserClass();
282 334
283 FetchingSchedule schedule; 335 FetchingSchedule schedule;
284 schedule.interval_wifi = 336 schedule.interval_persistent_wifi =
285 GetDesiredUpdateInterval(/*is_wifi=*/true, user_class); 337 GetDesiredFetchingInterval(FetchingInterval::PERSISTENT_WIFI, user_class);
286 schedule.interval_fallback = 338 schedule.interval_persistent_fallback = GetDesiredFetchingInterval(
287 GetDesiredUpdateInterval(/*is_wifi=*/false, user_class); 339 FetchingInterval::PERSISTENT_FALLBACK, user_class);
340 schedule.interval_soft_on_usage_event = GetDesiredFetchingInterval(
341 FetchingInterval::SOFT_ON_USAGE_EVENT, user_class);
342
288 return schedule; 343 return schedule;
289 } 344 }
290 345
291 SchedulingRemoteSuggestionsProvider::FetchingSchedule 346 SchedulingRemoteSuggestionsProvider::FetchingSchedule
292 SchedulingRemoteSuggestionsProvider::GetLastFetchingSchedule() const { 347 SchedulingRemoteSuggestionsProvider::GetLastFetchingSchedule() const {
293 FetchingSchedule schedule; 348 FetchingSchedule schedule;
294 schedule.interval_wifi = base::TimeDelta::FromInternalValue( 349 schedule.interval_persistent_wifi = base::TimeDelta::FromInternalValue(
295 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi)); 350 pref_service_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi));
296 schedule.interval_fallback = 351 schedule.interval_persistent_fallback =
297 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( 352 base::TimeDelta::FromInternalValue(pref_service_->GetInt64(
298 prefs::kSnippetBackgroundFetchingIntervalFallback)); 353 prefs::kSnippetPersistentFetchingIntervalFallback));
354 schedule.interval_soft_on_usage_event = base::TimeDelta::FromInternalValue(
355 pref_service_->GetInt64(prefs::kSnippetSoftFetchingIntervalOnUsageEvent));
356
299 return schedule; 357 return schedule;
300 } 358 }
301 359
302 void SchedulingRemoteSuggestionsProvider::StoreLastFetchingSchedule( 360 void SchedulingRemoteSuggestionsProvider::StoreLastFetchingSchedule(
303 const FetchingSchedule& schedule) { 361 const FetchingSchedule& schedule) {
362 pref_service_->SetInt64(prefs::kSnippetPersistentFetchingIntervalWifi,
363 schedule.interval_persistent_wifi.ToInternalValue());
304 pref_service_->SetInt64( 364 pref_service_->SetInt64(
305 prefs::kSnippetBackgroundFetchingIntervalWifi, 365 prefs::kSnippetPersistentFetchingIntervalFallback,
306 schedule.interval_wifi.ToInternalValue()); 366 schedule.interval_persistent_fallback.ToInternalValue());
307 pref_service_->SetInt64( 367 pref_service_->SetInt64(
308 prefs::kSnippetBackgroundFetchingIntervalFallback, 368 prefs::kSnippetSoftFetchingIntervalOnUsageEvent,
309 schedule.interval_fallback.ToInternalValue()); 369 schedule.interval_soft_on_usage_event.ToInternalValue());
370 }
371
372 bool SchedulingRemoteSuggestionsProvider::ShouldRefetchInTheBackgroundNow() {
373 if (!pref_service_->HasPrefPath(prefs::kSnippetLastBackgroundFetchAttempt)) {
374 // Soft background fetches are switched off.
375 return false;
376 }
377
378 FetchingSchedule schedule = GetLastFetchingSchedule();
379
380 base::Time first_allowed_fetch_time =
381 base::Time::FromInternalValue(
382 pref_service_->GetInt64(prefs::kSnippetLastBackgroundFetchAttempt)) +
383 schedule.interval_soft_on_usage_event;
384 return first_allowed_fetch_time <= base::Time::Now();
310 } 385 }
311 386
312 void SchedulingRemoteSuggestionsProvider::FetchFinished( 387 void SchedulingRemoteSuggestionsProvider::FetchFinished(
313 const FetchDoneCallback& callback, 388 const FetchDoneCallback& callback,
314 Status status_code, 389 Status fetch_status,
315 std::vector<ContentSuggestion> suggestions) { 390 std::vector<ContentSuggestion> suggestions) {
316 OnFetchCompleted(status_code); 391 // Reschedule after a fetch. The persistent schedule is applied only after a
317 callback.Run(status_code, std::move(suggestions)); 392 // successful fetch. After a failed fetch, we want to keep the previous
393 // persistent schedule intact so that we eventually get a persistent fallback
394 // fetch (if the wifi persistent fetches keep failing).
395 ApplyFetchingSchedule(GetLastFetchingSchedule(),
396 /*also_apply_persistent_schedule=*/fetch_status.code ==
397 StatusCode::SUCCESS);
398
399 callback.Run(fetch_status, std::move(suggestions));
318 } 400 }
319 401
320 void SchedulingRemoteSuggestionsProvider::OnFetchCompleted( 402 void SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished(
403 std::unique_ptr<FetchStatusCallback> callback,
321 Status fetch_status) { 404 Status fetch_status) {
322 // The scheduler only exists on Android so far, it's null on other platforms. 405 background_fetch_in_progress_ = false;
323 if (!persistent_scheduler_) { 406
324 return; 407 // Reschedule after a fetch. The persistent schedule is applied only after a
408 // successful fetch. After a failed fetch, we want to keep the previous
409 // persistent schedule intact so that we eventually get a persistent fallback
410 // fetch (if the wifi persistent fetches keep failing).
411 ApplyFetchingSchedule(GetLastFetchingSchedule(),
412 /*also_apply_persistent_schedule=*/fetch_status.code ==
413 StatusCode::SUCCESS);
414
415 if (callback) {
416 callback->Run(fetch_status);
325 } 417 }
326
327 if (fetch_status.code != StatusCode::SUCCESS) {
328 return;
329 }
330
331 // Reschedule after a successful fetch. This resets all currently scheduled
332 // fetches, to make sure the fallback interval triggers only if no wifi fetch
333 // succeeded, and also that we don't do a background fetch immediately after
334 // a user-initiated one.
335 ApplyFetchingSchedule(GetLastFetchingSchedule());
336 } 418 }
337 419
338 } // namespace ntp_snippets 420 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/scheduling_remote_suggestions_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698