File: sync_scheduler.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (98 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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_CRYPTAUTH_SYNC_SCHEDULER_H_
#define COMPONENTS_CRYPTAUTH_SYNC_SCHEDULER_H_

#include <memory>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"

namespace cryptauth {

// 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 { PERIODIC_REFRESH, AGGRESSIVE_RECOVERY };

  // The states that the scheduler can be in.
  enum class SyncState { NOT_STARTED, 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(std::unique_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 cryptauth

#endif  // COMPONENTS_CRYPTAUTH_SYNC_SCHEDULER_H_