gunsch | ee5a889 | 2015-08-20 20:03:48 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chromecast/base/system_time_change_notifier.h" |
| 6 | |
jameswest | 90b8e5b2 | 2017-06-07 23:31:42 | [diff] [blame] | 7 | #include "base/bind.h" |
gunsch | ee5a889 | 2015-08-20 20:03:48 | [diff] [blame] | 8 | #include "base/location.h" |
| 9 | #include "base/sequenced_task_runner.h" |
| 10 | |
| 11 | namespace chromecast { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // Limits for periodic system time monitoring. |
| 16 | const int kLimitForMonitorPer1Sec = 60; // 1 minute |
| 17 | const int kLimitForMonitorPer10Sec = 600; // 10 minutes |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | SystemTimeChangeNotifier::SystemTimeChangeNotifier() |
| 22 | : observer_list_(new base::ObserverListThreadSafe<Observer>()) { |
| 23 | } |
| 24 | |
| 25 | SystemTimeChangeNotifier::~SystemTimeChangeNotifier() { |
| 26 | } |
| 27 | |
| 28 | void SystemTimeChangeNotifier::AddObserver(Observer* observer) { |
| 29 | observer_list_->AddObserver(observer); |
| 30 | } |
| 31 | |
| 32 | void SystemTimeChangeNotifier::RemoveObserver(Observer* observer) { |
| 33 | observer_list_->RemoveObserver(observer); |
| 34 | } |
| 35 | |
| 36 | void SystemTimeChangeNotifier::NotifySystemTimeChanged() { |
| 37 | observer_list_->Notify(FROM_HERE, &Observer::OnSystemTimeChanged); |
| 38 | } |
| 39 | |
| 40 | SystemTimeChangeNotifierPeriodicMonitor:: |
| 41 | SystemTimeChangeNotifierPeriodicMonitor( |
| 42 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 43 | : task_runner_(task_runner), |
| 44 | weak_factory_(this) { |
| 45 | DCHECK(task_runner_); |
| 46 | } |
| 47 | |
| 48 | SystemTimeChangeNotifierPeriodicMonitor:: |
| 49 | ~SystemTimeChangeNotifierPeriodicMonitor() { |
| 50 | } |
| 51 | |
| 52 | void SystemTimeChangeNotifierPeriodicMonitor::Initialize() { |
| 53 | base::Time now = Now(); |
| 54 | ResetTimeAndLimits(now); |
| 55 | ScheduleNextMonitor(now); |
| 56 | } |
| 57 | |
| 58 | void SystemTimeChangeNotifierPeriodicMonitor::Finalize() { |
| 59 | } |
| 60 | |
| 61 | void 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 | |
| 72 | void 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, |
jameswest | 90b8e5b2 | 2017-06-07 23:31:42 | [diff] [blame] | 84 | base::BindOnce(&SystemTimeChangeNotifierPeriodicMonitor::CheckSystemTime, |
| 85 | weak_factory_.GetWeakPtr()), |
gunsch | ee5a889 | 2015-08-20 20:03:48 | [diff] [blame] | 86 | next_checking_interval); |
| 87 | } |
| 88 | |
| 89 | void 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 | |
| 100 | base::Time SystemTimeChangeNotifierPeriodicMonitor::Now() const { |
| 101 | if (!fake_now_.is_null()) |
| 102 | return fake_now_; |
| 103 | return base::Time::Now(); |
| 104 | } |
| 105 | |
| 106 | } // namespace chromecast |