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