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_H | |
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_H | |
Ilya Sherman
2015/05/18 23:25:38
nit: Duplicate cryptauth?
Tim Song
2015/05/19 22:13:18
Done.
| |
7 | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/time/time.h" | |
10 | |
11 namespace proximity_auth { | |
12 | |
13 // Interface for scheduling the next CryptAuth sync (e.g. enrollment or device | |
14 // sync). The scheduler has two different strategies affecting when to perform | |
15 // the next operation: | |
16 // PERIODIC_REFRESH: The last sync was made successfully, so we can wait a | |
17 // relatively long time before making another sync. | |
18 // AGGRESSIVE_RECOVERY: The last sync failed, so we try more aggressively to | |
19 // make enrollment attempts with subsequent backoff for repeated | |
20 // failures. | |
21 // A random jitter is applied to each sync period to smooth qps to the server. | |
22 class SyncScheduler { | |
23 public: | |
24 // The states that the scheduler can be in. | |
25 enum class State : int { | |
Ilya Sherman
2015/05/18 23:25:38
nit: Probably no need to specify that the backing
Tim Song
2015/05/19 22:13:18
This is mainly for the logs, so I can cast the enu
Ilya Sherman
2015/05/19 22:46:54
I think you can cast to an int even without this -
Tim Song
2015/05/20 00:13:22
Done.
| |
26 NOT_STARTED = 0, | |
27 PERIODIC_REFRESH, | |
Ilya Sherman
2015/05/18 23:25:38
nit: Maybe add "IDLE" somewhere to this state name
Tim Song
2015/05/19 22:13:18
Done. I split this State enum into a SyncState and
| |
28 SYNCING_FOR_PERIODIC_REFRESH, | |
29 AGGRESSIVE_RECOVERY, | |
30 SYNCING_FOR_AGGRESSIVE_RECOVERY, | |
31 }; | |
32 | |
33 // An instance is passed to the delegate when the scheduler fires for each | |
34 // sync attempt. The delegate should call |Complete()| when the sync succeeds | |
35 // or fails to resume the scheduler. | |
36 class SyncRequest { | |
37 public: | |
38 SyncRequest(base::WeakPtr<SyncScheduler> sync_scheduler, | |
39 bool is_aggressive_recovery); | |
40 ~SyncRequest(); | |
41 | |
42 void Complete(bool success); | |
Ilya Sherman
2015/05/18 23:25:38
nit: Perhaps name this "OnDidComplete"?
Tim Song
2015/05/19 22:13:18
Done.
| |
43 | |
44 bool is_aggressive_recovery() { return is_aggressive_recovery_; } | |
45 | |
46 protected: | |
47 // The parent scheduler that dispatched this request. | |
48 base::WeakPtr<SyncScheduler> sync_scheduler_; | |
49 | |
50 // True if the parent scheduler's strategy is AGGRESSIVE_RECOVERY. | |
51 bool is_aggressive_recovery_; | |
Ilya Sherman
2015/05/18 23:25:38
nit: Can this be const?
Tim Song
2015/05/19 22:13:18
I removed this field.
| |
52 | |
53 // True if |Complete()| has been called. | |
54 bool completed_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(SyncRequest); | |
57 }; | |
58 | |
59 // Handles the actual sync operation. | |
60 class Delegate { | |
61 public: | |
62 virtual ~Delegate() {} | |
63 | |
64 // Called when the scheduler fires and requests a sync attempt. The delegate | |
65 // should call sync_request->Complete() when the request finishes. | |
66 virtual void OnSyncRequested(scoped_ptr<SyncRequest> sync_request) = 0; | |
67 }; | |
68 | |
69 virtual ~SyncScheduler() {} | |
70 | |
71 // Starts the scheduler with an aggressive recovery strategy if | |
72 // |is_aggressive_recovery| is true; otherwise, it will be started with | |
73 // periodic refresh. | |
74 // | |
75 // |elapsed_time_since_last_sync| is the time since the last successful sync, | |
76 // so we can determine the duration of the first sync period. For example, the | |
77 // scheduler will immediately issue a sync request if the elapsed time is | |
78 // greater than the refresh period. | |
79 virtual void Start(const base::TimeDelta& elapsed_time_since_last_sync, | |
80 bool is_aggressive_recovery) = 0; | |
81 | |
82 // Cancels the current scheduled sync, and forces a sync immediately. Note | |
83 // that if this sync fails, the scheduler will adopt the AGGRESSIVE_RECOVERY | |
84 // strategy. | |
85 virtual void ForceSync() = 0; | |
86 | |
87 // Returns the time until the next scheduled sync operation. If no sync is | |
88 // scheduled, a TimeDelta of zero will be returned. | |
89 virtual base::TimeDelta GetTimeToNextSync() const = 0; | |
90 | |
91 // Returns the current state of the scheduler. | |
92 virtual State GetState() const = 0; | |
93 | |
94 protected: | |
95 // Called by SyncRequest instances when the sync completes. | |
96 virtual void OnSyncCompleted(bool success) = 0; | |
97 }; | |
98 | |
99 } // namespace proximity_auth | |
100 | |
101 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_H | |
OLD | NEW |