1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/variations/variations_request_scheduler_mobile.h"
7 #include "base/prefs/pref_service.h"
8 #include "components/variations/pref_names.h"
10 namespace variations
{
14 // Time before attempting a seed fetch after a ScheduleFetch(), in seconds.
15 const int kScheduleFetchDelaySeconds
= 5;
19 VariationsRequestSchedulerMobile::VariationsRequestSchedulerMobile(
20 const base::Closure
& task
,
21 PrefService
* local_state
) :
22 VariationsRequestScheduler(task
), local_state_(local_state
) {
25 VariationsRequestSchedulerMobile::~VariationsRequestSchedulerMobile() {
28 void VariationsRequestSchedulerMobile::Start() {
29 // Check the time of the last request. If it has been longer than the fetch
30 // period, run the task. Otherwise, do nothing. Note that no future requests
31 // are scheduled since it is unlikely that the mobile process would live long
32 // enough for the timer to fire.
33 const base::Time last_fetch_time
= base::Time::FromInternalValue(
34 local_state_
->GetInt64(prefs::kVariationsLastFetchTime
));
35 if (base::Time::Now() > last_fetch_time
+ GetFetchPeriod()) {
36 last_request_time_
= base::Time::Now();
41 void VariationsRequestSchedulerMobile::Reset() {
44 void VariationsRequestSchedulerMobile::OnAppEnterForeground() {
45 // Verify we haven't just attempted a fetch (which has not completed). This
46 // is mainly used to verify we don't trigger a second fetch for the
47 // OnAppEnterForeground right after startup.
48 if (base::Time::Now() < last_request_time_
+ GetFetchPeriod())
51 // Since Start() launches a one-off execution, we can reuse it here. Also
52 // note that since Start() verifies that the seed needs to be refreshed, we
53 // do not verify here.
54 schedule_fetch_timer_
.Start(
56 base::TimeDelta::FromSeconds(kScheduleFetchDelaySeconds
),
58 &VariationsRequestSchedulerMobile::Start
);
62 VariationsRequestScheduler
* VariationsRequestScheduler::Create(
63 const base::Closure
& task
,
64 PrefService
* local_state
) {
65 return new VariationsRequestSchedulerMobile(task
, local_state
);
68 } // namespace variations