| 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 <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/time/time.h" | |
| 13 | |
| 14 namespace proximity_auth { | |
| 15 | |
| 16 // Interface for scheduling the next CryptAuth sync (e.g. enrollment or device | |
| 17 // sync). The scheduler has two different strategies affecting when to perform | |
| 18 // the next operation: | |
| 19 // PERIODIC_REFRESH: The last sync was made successfully, so we can wait a | |
| 20 // relatively long time before making another sync. | |
| 21 // AGGRESSIVE_RECOVERY: The last sync failed, so we try more aggressively to | |
| 22 // make enrollment attempts with subsequent backoff for repeated | |
| 23 // failures. | |
| 24 // A random jitter is applied to each sync period to smooth qps to the server. | |
| 25 class SyncScheduler { | |
| 26 public: | |
| 27 // The sync strategies mentioned in the class comments. | |
| 28 enum class Strategy { PERIODIC_REFRESH, AGGRESSIVE_RECOVERY }; | |
| 29 | |
| 30 // The states that the scheduler can be in. | |
| 31 enum class SyncState { NOT_STARTED, WAITING_FOR_REFRESH, SYNC_IN_PROGRESS }; | |
| 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 explicit SyncRequest(base::WeakPtr<SyncScheduler> sync_scheduler); | |
| 39 ~SyncRequest(); | |
| 40 | |
| 41 void OnDidComplete(bool success); | |
| 42 | |
| 43 protected: | |
| 44 // The parent scheduler that dispatched this request. | |
| 45 base::WeakPtr<SyncScheduler> sync_scheduler_; | |
| 46 | |
| 47 // True if |OnDidComplete()| has been called. | |
| 48 bool completed_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(SyncRequest); | |
| 51 }; | |
| 52 | |
| 53 // Handles the actual sync operation. | |
| 54 class Delegate { | |
| 55 public: | |
| 56 virtual ~Delegate() {} | |
| 57 | |
| 58 // Called when the scheduler fires and requests a sync attempt. The delegate | |
| 59 // should call sync_request->Complete() when the request finishes. | |
| 60 virtual void OnSyncRequested(std::unique_ptr<SyncRequest> sync_request) = 0; | |
| 61 }; | |
| 62 | |
| 63 virtual ~SyncScheduler() {} | |
| 64 | |
| 65 // Starts the scheduler with an aggressive recovery strategy if | |
| 66 // |strategy| is true; otherwise, it will be started with | |
| 67 // periodic refresh. | |
| 68 // | |
| 69 // |elapsed_time_since_last_sync| is the time since the last successful sync, | |
| 70 // so we can determine the duration of the first sync period. For example, the | |
| 71 // scheduler will immediately issue a sync request if the elapsed time is | |
| 72 // greater than the refresh period. | |
| 73 virtual void Start(const base::TimeDelta& elapsed_time_since_last_sync, | |
| 74 Strategy strategy) = 0; | |
| 75 | |
| 76 // Cancels the current scheduled sync, and forces a sync immediately. Note | |
| 77 // that if this sync fails, the scheduler will adopt the AGGRESSIVE_RECOVERY | |
| 78 // strategy. | |
| 79 virtual void ForceSync() = 0; | |
| 80 | |
| 81 // Returns the time until the next scheduled sync operation. If no sync is | |
| 82 // scheduled, a TimeDelta of zero will be returned. | |
| 83 virtual base::TimeDelta GetTimeToNextSync() const = 0; | |
| 84 | |
| 85 // Returns the current sync strategy. | |
| 86 virtual Strategy GetStrategy() const = 0; | |
| 87 | |
| 88 // Returns the current state of the scheduler. | |
| 89 virtual SyncState GetSyncState() const = 0; | |
| 90 | |
| 91 protected: | |
| 92 // Called by SyncRequest instances when the sync completes. | |
| 93 virtual void OnSyncCompleted(bool success) = 0; | |
| 94 }; | |
| 95 | |
| 96 } // namespace proximity_auth | |
| 97 | |
| 98 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_SYNC_SCHEDULER_H | |
| OLD | NEW |