Chromium Code Reviews| Index: components/proximity_auth/cryptauth/sync_scheduler.h |
| diff --git a/components/proximity_auth/cryptauth/sync_scheduler.h b/components/proximity_auth/cryptauth/sync_scheduler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b0ab73ea4a4d9a3fcc892746d759725876810c0a |
| --- /dev/null |
| +++ b/components/proximity_auth/cryptauth/sync_scheduler.h |
| @@ -0,0 +1,101 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_H |
| +#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.
|
| + |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/time/time.h" |
| + |
| +namespace proximity_auth { |
| + |
| +// Interface for scheduling the next CryptAuth sync (e.g. enrollment or device |
| +// sync). The scheduler has two different strategies affecting when to perform |
| +// the next operation: |
| +// PERIODIC_REFRESH: The last sync was made successfully, so we can wait a |
| +// relatively long time before making another sync. |
| +// AGGRESSIVE_RECOVERY: The last sync failed, so we try more aggressively to |
| +// make enrollment attempts with subsequent backoff for repeated |
| +// failures. |
| +// A random jitter is applied to each sync period to smooth qps to the server. |
| +class SyncScheduler { |
| + public: |
| + // The states that the scheduler can be in. |
| + 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.
|
| + NOT_STARTED = 0, |
| + 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
|
| + SYNCING_FOR_PERIODIC_REFRESH, |
| + AGGRESSIVE_RECOVERY, |
| + SYNCING_FOR_AGGRESSIVE_RECOVERY, |
| + }; |
| + |
| + // An instance is passed to the delegate when the scheduler fires for each |
| + // sync attempt. The delegate should call |Complete()| when the sync succeeds |
| + // or fails to resume the scheduler. |
| + class SyncRequest { |
| + public: |
| + SyncRequest(base::WeakPtr<SyncScheduler> sync_scheduler, |
| + bool is_aggressive_recovery); |
| + ~SyncRequest(); |
| + |
| + 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.
|
| + |
| + bool is_aggressive_recovery() { return is_aggressive_recovery_; } |
| + |
| + protected: |
| + // The parent scheduler that dispatched this request. |
| + base::WeakPtr<SyncScheduler> sync_scheduler_; |
| + |
| + // True if the parent scheduler's strategy is AGGRESSIVE_RECOVERY. |
| + 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.
|
| + |
| + // True if |Complete()| has been called. |
| + bool completed_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SyncRequest); |
| + }; |
| + |
| + // Handles the actual sync operation. |
| + class Delegate { |
| + public: |
| + virtual ~Delegate() {} |
| + |
| + // Called when the scheduler fires and requests a sync attempt. The delegate |
| + // should call sync_request->Complete() when the request finishes. |
| + virtual void OnSyncRequested(scoped_ptr<SyncRequest> sync_request) = 0; |
| + }; |
| + |
| + virtual ~SyncScheduler() {} |
| + |
| + // Starts the scheduler with an aggressive recovery strategy if |
| + // |is_aggressive_recovery| is true; otherwise, it will be started with |
| + // periodic refresh. |
| + // |
| + // |elapsed_time_since_last_sync| is the time since the last successful sync, |
| + // so we can determine the duration of the first sync period. For example, the |
| + // scheduler will immediately issue a sync request if the elapsed time is |
| + // greater than the refresh period. |
| + virtual void Start(const base::TimeDelta& elapsed_time_since_last_sync, |
| + bool is_aggressive_recovery) = 0; |
| + |
| + // Cancels the current scheduled sync, and forces a sync immediately. Note |
| + // that if this sync fails, the scheduler will adopt the AGGRESSIVE_RECOVERY |
| + // strategy. |
| + virtual void ForceSync() = 0; |
| + |
| + // Returns the time until the next scheduled sync operation. If no sync is |
| + // scheduled, a TimeDelta of zero will be returned. |
| + virtual base::TimeDelta GetTimeToNextSync() const = 0; |
| + |
| + // Returns the current state of the scheduler. |
| + virtual State GetState() const = 0; |
| + |
| + protected: |
| + // Called by SyncRequest instances when the sync completes. |
| + virtual void OnSyncCompleted(bool success) = 0; |
| +}; |
| + |
| +} // namespace proximity_auth |
| + |
| +#endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_H |