Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3833)

Unified Diff: components/proximity_auth/cryptauth/sync_scheduler.h

Issue 1147563002: Add SyncScheduler for scheduling CryptAuth enrollments and syncing devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactor out Strategy enum Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bd55747832bf6c763f9dea45cf8ad90294587698
--- /dev/null
+++ b/components/proximity_auth/cryptauth/sync_scheduler.h
@@ -0,0 +1,99 @@
+// 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_SYNC_SCHEDULER_H
+#define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_SYNC_SCHEDULER_H
+
+#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 sync strategies mentioned in the class comments.
+ enum class Strategy : int { PERIODIC_REFRESH = 0, AGGRESSIVE_RECOVERY };
+
+ // The states that the scheduler can be in.
+ enum class SyncState : int {
+ NOT_STARTED = 0,
Ilya Sherman 2015/05/19 22:46:55 nit: Is the "= 0" important? I'd omit it if not.
+ WAITING_FOR_REFRESH,
+ SYNC_IN_PROGRESS
+ };
+
+ // 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:
+ explicit SyncRequest(base::WeakPtr<SyncScheduler> sync_scheduler);
+ ~SyncRequest();
+
+ void OnDidComplete(bool success);
+
+ protected:
+ // The parent scheduler that dispatched this request.
+ base::WeakPtr<SyncScheduler> sync_scheduler_;
+
+ // True if |OnDidComplete()| 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
+ // |strategy| 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,
+ Strategy strategy) = 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 sync strategy.
+ virtual Strategy GetStrategy() const = 0;
+
+ // Returns the current state of the scheduler.
+ virtual SyncState GetSyncState() const = 0;
+
+ protected:
+ // Called by SyncRequest instances when the sync completes.
+ virtual void OnSyncCompleted(bool success) = 0;
+};
+
+} // namespace proximity_auth
+
+#endif // COMPONENTS_PROXIMITY_CRYPTAUTH_SYNC_SCHEDULER_H

Powered by Google App Engine
This is Rietveld 408576698