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