1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROMECAST_BASE_SYSTEM_TIME_CHANGE_NOTIFIER_H_
6 #define CHROMECAST_BASE_SYSTEM_TIME_CHANGE_NOTIFIER_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list_threadsafe.h"
13 #include "base/time/time.h"
16 class SequencedTaskRunner
;
19 namespace chromecast
{
21 // Abstract class to notify system time changes.
22 class SystemTimeChangeNotifier
{
26 // Called when current system time has changed.
27 virtual void OnSystemTimeChanged() = 0;
30 virtual ~Observer() {}
33 virtual ~SystemTimeChangeNotifier();
35 virtual void Initialize() = 0;
36 virtual void Finalize() = 0;
38 void AddObserver(Observer
* observer
);
39 void RemoveObserver(Observer
* observer
);
42 SystemTimeChangeNotifier();
44 // Implementations are expected to call this to notify system time changes.
45 void NotifySystemTimeChanged();
48 scoped_refptr
<base::ObserverListThreadSafe
<Observer
>> observer_list_
;
50 DISALLOW_COPY_AND_ASSIGN(SystemTimeChangeNotifier
);
53 // Default implementation of SystemTimeChangeNotifier for most platform.
54 // It monitors system time changes periodically and notifies if the current
55 // system time is different from the expected one by more than 10 seconds.
56 // After reboot or system time has changed, it checks for time changes every
57 // second for first 1 minute. Then, checks every 10 seconds for next 10 minutes.
58 // Then, checks every 10 minutes.
59 class SystemTimeChangeNotifierPeriodicMonitor
60 : public SystemTimeChangeNotifier
{
62 explicit SystemTimeChangeNotifierPeriodicMonitor(
63 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
64 ~SystemTimeChangeNotifierPeriodicMonitor() override
;
66 // SystemTimeChangeNotifier implementation:
67 void Initialize() override
;
68 void Finalize() override
;
71 void set_fake_now_for_testing(base::Time now
) { fake_now_
= now
; }
74 // Helper functions to monitor system time changes.
75 void ResetTimeAndLimits(base::Time now
);
76 void ScheduleNextMonitor(base::Time now
);
77 void CheckSystemTime();
79 // Returns base::Time::Now() unless fake_now is set.
80 base::Time
Now() const;
82 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
84 base::Time expected_system_time_
;
85 base::Time monitoring_limit_time_1sec_
;
86 base::Time monitoring_limit_time_10sec_
;
90 base::WeakPtrFactory
<SystemTimeChangeNotifierPeriodicMonitor
> weak_factory_
;
92 DISALLOW_COPY_AND_ASSIGN(SystemTimeChangeNotifierPeriodicMonitor
);
95 } // namespace chromecast
97 #endif // CHROMECAST_BASE_SYSTEM_TIME_CHANGE_NOTIFIER_H_