1 // Copyright 2015 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 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H
8 #include "base/timer/timer.h"
9 #include "components/proximity_auth/cryptauth/sync_scheduler.h"
11 namespace proximity_auth
{
13 // Implementation of SyncScheduler.
14 class SyncSchedulerImpl
: public SyncScheduler
{
16 // Creates the scheduler:
17 // |delegate|: Handles sync requests and must outlive the scheduler.
18 // |refresh_period|: The time to wait for the PERIODIC_REFRESH strategy.
19 // |base_recovery_period|: The initial time to wait for the
20 // AGGRESSIVE_RECOVERY strategy. The time delta is increased for each
21 // subsequent failure.
22 // |max_jitter_ratio|: The maximum ratio that the time to next sync can be
23 // jittered (both positively and negatively).
24 // |scheduler_name|: The name of the scheduler for debugging purposes.
25 SyncSchedulerImpl(Delegate
* delegate
,
26 base::TimeDelta refresh_period
,
27 base::TimeDelta base_recovery_period
,
28 double max_jitter_ratio
,
29 const std::string
& scheduler_name
);
31 ~SyncSchedulerImpl() override
;
34 void Start(const base::TimeDelta
& elapsed_time_since_last_sync
,
35 Strategy strategy
) override
;
36 void ForceSync() override
;
37 base::TimeDelta
GetTimeToNextSync() const override
;
38 Strategy
GetStrategy() const override
;
39 SyncState
GetSyncState() const override
;
42 // Creates and returns a base::Timer object. Exposed for testing.
43 virtual scoped_ptr
<base::Timer
> CreateTimer();
47 void OnSyncCompleted(bool success
) override
;
49 // Called when |timer_| is fired.
52 // Schedules |timer_| for the next sync request.
53 void ScheduleNextSync(const base::TimeDelta
& sync_delta
);
55 // Adds a random jitter to the value of GetPeriod(). The returned
56 // TimeDelta will be clamped to be non-negative.
57 base::TimeDelta
GetJitteredPeriod();
59 // Returns the time to wait for the current strategy.
60 base::TimeDelta
GetPeriod();
62 // The delegate handling sync requests when they are fired.
63 Delegate
* const delegate_
;
65 // The time to wait until the next refresh when the last sync attempt was
67 const base::TimeDelta refresh_period_
;
69 // The base recovery period for the AGGRESSIVE_RECOVERY strategy before
70 // backoffs are applied.
71 const base::TimeDelta base_recovery_period_
;
73 // The maximum percentage (both positively and negatively) that the time to
74 // wait between each sync request is jittered. The jitter is randomly applied
75 // to each period so we can avoid synchronous calls to the server.
76 const double max_jitter_ratio_
;
78 // The name of the scheduler, used for debugging purposes.
79 const std::string scheduler_name_
;
81 // The current strategy of the scheduler.
84 // The current state of the scheduler.
85 SyncState sync_state_
;
87 // The number of failed syncs made in a row. Once a sync request succeeds,
88 // this counter is reset.
89 size_t failure_count_
;
91 // Timer firing for the next sync request.
92 scoped_ptr
<base::Timer
> timer_
;
94 base::WeakPtrFactory
<SyncSchedulerImpl
> weak_ptr_factory_
;
96 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl
);
99 } // namespace proximity_auth
101 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H