blob: fbceae14be439658c8d7d68d65287b87af5d1f7d [file] [log] [blame]
gunschee5a8892015-08-20 20:03:481// 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.
4
5#include "chromecast/base/system_time_change_notifier.h"
6
jameswest90b8e5b22017-06-07 23:31:427#include "base/bind.h"
gunschee5a8892015-08-20 20:03:488#include "base/location.h"
9#include "base/sequenced_task_runner.h"
10
11namespace chromecast {
12
13namespace {
14
15// Limits for periodic system time monitoring.
16const int kLimitForMonitorPer1Sec = 60; // 1 minute
17const int kLimitForMonitorPer10Sec = 600; // 10 minutes
18
19} // namespace
20
21SystemTimeChangeNotifier::SystemTimeChangeNotifier()
22 : observer_list_(new base::ObserverListThreadSafe<Observer>()) {
23}
24
25SystemTimeChangeNotifier::~SystemTimeChangeNotifier() {
26}
27
28void SystemTimeChangeNotifier::AddObserver(Observer* observer) {
29 observer_list_->AddObserver(observer);
30}
31
32void SystemTimeChangeNotifier::RemoveObserver(Observer* observer) {
33 observer_list_->RemoveObserver(observer);
34}
35
36void SystemTimeChangeNotifier::NotifySystemTimeChanged() {
37 observer_list_->Notify(FROM_HERE, &Observer::OnSystemTimeChanged);
38}
39
40SystemTimeChangeNotifierPeriodicMonitor::
41SystemTimeChangeNotifierPeriodicMonitor(
42 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
43 : task_runner_(task_runner),
44 weak_factory_(this) {
45 DCHECK(task_runner_);
46}
47
48SystemTimeChangeNotifierPeriodicMonitor::
49~SystemTimeChangeNotifierPeriodicMonitor() {
50}
51
52void SystemTimeChangeNotifierPeriodicMonitor::Initialize() {
53 base::Time now = Now();
54 ResetTimeAndLimits(now);
55 ScheduleNextMonitor(now);
56}
57
58void SystemTimeChangeNotifierPeriodicMonitor::Finalize() {
59}
60
61void SystemTimeChangeNotifierPeriodicMonitor::ResetTimeAndLimits(
62 base::Time now) {
63 // ScheduleNextMonitor() will adjust actual expected_system_time.
64 expected_system_time_ = now;
65 monitoring_limit_time_1sec_ =
66 now + base::TimeDelta::FromSeconds(kLimitForMonitorPer1Sec);
67 monitoring_limit_time_10sec_ =
68 monitoring_limit_time_1sec_ +
69 base::TimeDelta::FromSeconds(kLimitForMonitorPer10Sec);
70}
71
72void SystemTimeChangeNotifierPeriodicMonitor::ScheduleNextMonitor(
73 base::Time now) {
74 base::TimeDelta next_checking_interval =
75 now <= monitoring_limit_time_1sec_ ? base::TimeDelta::FromSeconds(1) :
76 now <= monitoring_limit_time_10sec_ ? base::TimeDelta::FromSeconds(10) :
77 base::TimeDelta::FromMinutes(10);
78 // Adjusting expected_system_time based on now cannot detect continuous system
79 // time drift (false negative), but tolerates task delay (false positive).
80 // Task delay is expected more than system time drift.
81 expected_system_time_ = now + next_checking_interval;
82 task_runner_->PostDelayedTask(
83 FROM_HERE,
jameswest90b8e5b22017-06-07 23:31:4284 base::BindOnce(&SystemTimeChangeNotifierPeriodicMonitor::CheckSystemTime,
85 weak_factory_.GetWeakPtr()),
gunschee5a8892015-08-20 20:03:4886 next_checking_interval);
87}
88
89void SystemTimeChangeNotifierPeriodicMonitor::CheckSystemTime() {
90 base::Time now = Now();
91 const base::TimeDelta kInterval10Seconds(base::TimeDelta::FromSeconds(10));
92 if (now < expected_system_time_ - kInterval10Seconds ||
93 now > expected_system_time_ + kInterval10Seconds) { // Time changed!
94 ResetTimeAndLimits(now);
95 NotifySystemTimeChanged();
96 }
97 ScheduleNextMonitor(now);
98}
99
100base::Time SystemTimeChangeNotifierPeriodicMonitor::Now() const {
101 if (!fake_now_.is_null())
102 return fake_now_;
103 return base::Time::Now();
104}
105
106} // namespace chromecast