File: sync_scheduler_impl.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 (104 lines) | stat: -rw-r--r-- 3,550 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
// 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_IMPL_H_
#define COMPONENTS_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H_

#include <stddef.h>

#include "base/macros.h"
#include "base/timer/timer.h"
#include "components/cryptauth/sync_scheduler.h"

namespace cryptauth {

// Implementation of SyncScheduler.
class SyncSchedulerImpl : public SyncScheduler {
 public:
  // Creates the scheduler:
  // |delegate|: Handles sync requests and must outlive the scheduler.
  // |refresh_period|: The time to wait for the PERIODIC_REFRESH strategy.
  // |base_recovery_period|: The initial time to wait for the
  //    AGGRESSIVE_RECOVERY strategy. The time delta is increased for each
  //    subsequent failure.
  // |max_jitter_ratio|: The maximum ratio that the time to next sync can be
  //    jittered (both positively and negatively).
  // |scheduler_name|: The name of the scheduler for debugging purposes.
  SyncSchedulerImpl(Delegate* delegate,
                    base::TimeDelta refresh_period,
                    base::TimeDelta base_recovery_period,
                    double max_jitter_ratio,
                    const std::string& scheduler_name);

  ~SyncSchedulerImpl() override;

  // SyncScheduler:
  void Start(const base::TimeDelta& elapsed_time_since_last_sync,
             Strategy strategy) override;
  void ForceSync() override;
  base::TimeDelta GetTimeToNextSync() const override;
  Strategy GetStrategy() const override;
  SyncState GetSyncState() const override;

 protected:
  // Creates and returns a base::Timer object. Exposed for testing.
  virtual std::unique_ptr<base::Timer> CreateTimer();

 private:
  // SyncScheduler:
  void OnSyncCompleted(bool success) override;

  // Called when |timer_| is fired.
  void OnTimerFired();

  // Schedules |timer_| for the next sync request.
  void ScheduleNextSync(const base::TimeDelta& sync_delta);

  // Adds a random jitter to the value of GetPeriod(). The returned
  // TimeDelta will be clamped to be non-negative.
  base::TimeDelta GetJitteredPeriod();

  // Returns the time to wait for the current strategy.
  base::TimeDelta GetPeriod();

  // The delegate handling sync requests when they are fired.
  Delegate* const delegate_;

  // The time to wait until the next refresh when the last sync attempt was
  // successful.
  const base::TimeDelta refresh_period_;

  // The base recovery period for the AGGRESSIVE_RECOVERY strategy before
  // backoffs are applied.
  const base::TimeDelta base_recovery_period_;

  // The maximum percentage (both positively and negatively) that the time to
  // wait between each sync request is jittered. The jitter is randomly applied
  // to each period so we can avoid synchronous calls to the server.
  const double max_jitter_ratio_;

  // The name of the scheduler, used for debugging purposes.
  const std::string scheduler_name_;

  // The current strategy of the scheduler.
  Strategy strategy_;

  // The current state of the scheduler.
  SyncState sync_state_;

  // The number of failed syncs made in a row. Once a sync request succeeds,
  // this counter is reset.
  size_t failure_count_;

  // Timer firing for the next sync request.
  std::unique_ptr<base::Timer> timer_;

  base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl);
};

}  // namespace cryptauth

#endif  // COMPONENTS_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H_