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_SYNC_SCHEDULER_H | |
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_SYNC_SCHEDULER_H | |
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 sync strategies mentioned in the class comments. | |
25 enum class Strategy { PERIODIC_REFRESH, AGGRESSIVE_RECOVERY }; | |
26 | |
27 // The states that the scheduler can be in. | |
28 enum class SyncState { NOT_STARTED, WAITING_FOR_REFRESH, SYNC_IN_PROGRESS }; | |
29 | |
30 // An instance is passed to the delegate when the scheduler fires for each | |
31 // sync attempt. The delegate should call |Complete()| when the sync succeeds | |
32 // or fails to resume the scheduler. | |
33 class SyncRequest { | |
34 public: | |
35 explicit SyncRequest(base::WeakPtr<SyncScheduler> sync_scheduler); | |
36 ~SyncRequest(); | |
37 | |
38 void OnDidComplete(bool success); | |
39 | |
40 protected: | |
41 // The parent scheduler that dispatched this request. | |
42 base::WeakPtr<SyncScheduler> sync_scheduler_; | |
43 | |
44 // True if |OnDidComplete()| has been called. | |
45 bool completed_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(SyncRequest); | |
48 }; | |
49 | |
50 // Handles the actual sync operation. | |
51 class Delegate { | |
52 public: | |
53 virtual ~Delegate() {} | |
54 | |
55 // Called when the scheduler fires and requests a sync attempt. The delegate | |
56 // should call sync_request->Complete() when the request finishes. | |
57 virtual void OnSyncRequested(scoped_ptr<SyncRequest> sync_request) = 0; | |
58 }; | |
59 | |
60 virtual ~SyncScheduler() {} | |
61 | |
62 // Starts the scheduler with an aggressive recovery strategy if | |
63 // |strategy| is true; otherwise, it will be started with | |
64 // periodic refresh. | |
65 // | |
66 // |elapsed_time_since_last_sync| is the time since the last successful sync, | |
67 // so we can determine the duration of the first sync period. For example, the | |
68 // scheduler will immediately issue a sync request if the elapsed time is | |
69 // greater than the refresh period. | |
70 virtual void Start(const base::TimeDelta& elapsed_time_since_last_sync, | |
71 Strategy strategy) = 0; | |
72 | |
73 // Cancels the current scheduled sync, and forces a sync immediately. Note | |
74 // that if this sync fails, the scheduler will adopt the AGGRESSIVE_RECOVERY | |
75 // strategy. | |
76 virtual void ForceSync() = 0; | |
77 | |
78 // Returns the time until the next scheduled sync operation. If no sync is | |
79 // scheduled, a TimeDelta of zero will be returned. | |
80 virtual base::TimeDelta GetTimeToNextSync() const = 0; | |
81 | |
82 // Returns the current sync strategy. | |
83 virtual Strategy GetStrategy() const = 0; | |
84 | |
85 // Returns the current state of the scheduler. | |
86 virtual SyncState GetSyncState() const = 0; | |
87 | |
88 protected: | |
89 // Called by SyncRequest instances when the sync completes. | |
90 virtual void OnSyncCompleted(bool success) = 0; | |
91 }; | |
92 | |
93 } // namespace proximity_auth | |
94 | |
95 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_SYNC_SCHEDULER_H | |
OLD | NEW |