summaryrefslogtreecommitdiffstats
path: root/chromecast/base/system_time_change_notifier.h
blob: 0b9668a94d3548c9d7fd9fac93c2fae1a9b26dd6 (plain)
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
// 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 CHROMECAST_BASE_SYSTEM_TIME_CHANGE_NOTIFIER_H_
#define CHROMECAST_BASE_SYSTEM_TIME_CHANGE_NOTIFIER_H_

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list_threadsafe.h"
#include "base/time/time.h"

namespace base {
class SequencedTaskRunner;
}

namespace chromecast {

// Abstract class to notify system time changes.
class SystemTimeChangeNotifier {
 public:
  class Observer {
   public:
    // Called when current system time has changed.
    virtual void OnSystemTimeChanged() = 0;

   protected:
    virtual ~Observer() {}
  };

  virtual ~SystemTimeChangeNotifier();

  virtual void Initialize() = 0;
  virtual void Finalize() = 0;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 protected:
  SystemTimeChangeNotifier();

  // Implementations are expected to call this to notify system time changes.
  void NotifySystemTimeChanged();

 private:
  scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;

  DISALLOW_COPY_AND_ASSIGN(SystemTimeChangeNotifier);
};

// Default implementation of SystemTimeChangeNotifier for most platform.
// It monitors system time changes periodically and notifies if the current
// system time is different from the expected one by more than 10 seconds.
// After reboot or system time has changed, it checks for time changes every
// second for first 1 minute. Then, checks every 10 seconds for next 10 minutes.
// Then, checks every 10 minutes.
class SystemTimeChangeNotifierPeriodicMonitor
    : public SystemTimeChangeNotifier {
 public:
  explicit SystemTimeChangeNotifierPeriodicMonitor(
      const scoped_refptr<base::SequencedTaskRunner>& task_runner);
  ~SystemTimeChangeNotifierPeriodicMonitor() override;

  // SystemTimeChangeNotifier implementation:
  void Initialize() override;
  void Finalize() override;

  // For unittests.
  void set_fake_now_for_testing(base::Time now) { fake_now_ = now; }

 private:
  // Helper functions to monitor system time changes.
  void ResetTimeAndLimits(base::Time now);
  void ScheduleNextMonitor(base::Time now);
  void CheckSystemTime();

  // Returns base::Time::Now() unless fake_now is set.
  base::Time Now() const;

  const scoped_refptr<base::SequencedTaskRunner> task_runner_;

  base::Time expected_system_time_;
  base::Time monitoring_limit_time_1sec_;
  base::Time monitoring_limit_time_10sec_;

  base::Time fake_now_;

  base::WeakPtrFactory<SystemTimeChangeNotifierPeriodicMonitor> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(SystemTimeChangeNotifierPeriodicMonitor);
};

}  // namespace chromecast

#endif  // CHROMECAST_BASE_SYSTEM_TIME_CHANGE_NOTIFIER_H_