OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/base/delayed_unique_notifier.h" | 5 #include "cc/base/delayed_unique_notifier.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 base::Bind(&DelayedUniqueNotifier::NotifyIfTime, | 36 base::Bind(&DelayedUniqueNotifier::NotifyIfTime, |
37 weak_ptr_factory_.GetWeakPtr()), | 37 weak_ptr_factory_.GetWeakPtr()), |
38 delay_); | 38 delay_); |
39 notification_pending_ = true; | 39 notification_pending_ = true; |
40 } | 40 } |
41 | 41 |
42 void DelayedUniqueNotifier::Cancel() { | 42 void DelayedUniqueNotifier::Cancel() { |
43 next_notification_time_ = base::TimeTicks(); | 43 next_notification_time_ = base::TimeTicks(); |
44 } | 44 } |
45 | 45 |
| 46 bool DelayedUniqueNotifier::HasPendingNotification() const { |
| 47 return notification_pending_ && !next_notification_time_.is_null(); |
| 48 } |
| 49 |
46 base::TimeTicks DelayedUniqueNotifier::Now() const { | 50 base::TimeTicks DelayedUniqueNotifier::Now() const { |
47 return base::TimeTicks::Now(); | 51 return base::TimeTicks::Now(); |
48 } | 52 } |
49 | 53 |
50 void DelayedUniqueNotifier::NotifyIfTime() { | 54 void DelayedUniqueNotifier::NotifyIfTime() { |
51 // If next notifiaction time is not valid, then this schedule was canceled. | 55 // If next notifiaction time is not valid, then this schedule was canceled. |
52 if (next_notification_time_.is_null()) { | 56 if (next_notification_time_.is_null()) { |
53 notification_pending_ = false; | 57 notification_pending_ = false; |
54 return; | 58 return; |
55 } | 59 } |
56 | 60 |
57 // If the notification was rescheduled or arrived too early for any other | 61 // If the notification was rescheduled or arrived too early for any other |
58 // reason, then post another task instead of running the callback. | 62 // reason, then post another task instead of running the callback. |
59 base::TimeTicks now = Now(); | 63 base::TimeTicks now = Now(); |
60 if (next_notification_time_ > now) { | 64 if (next_notification_time_ > now) { |
61 task_runner_->PostDelayedTask( | 65 task_runner_->PostDelayedTask( |
62 FROM_HERE, | 66 FROM_HERE, |
63 base::Bind(&DelayedUniqueNotifier::NotifyIfTime, | 67 base::Bind(&DelayedUniqueNotifier::NotifyIfTime, |
64 weak_ptr_factory_.GetWeakPtr()), | 68 weak_ptr_factory_.GetWeakPtr()), |
65 next_notification_time_ - now); | 69 next_notification_time_ - now); |
66 return; | 70 return; |
67 } | 71 } |
68 | 72 |
69 // Note the order here is important since closure might schedule another run. | 73 // Note the order here is important since closure might schedule another run. |
70 notification_pending_ = false; | 74 notification_pending_ = false; |
71 closure_.Run(); | 75 closure_.Run(); |
72 } | 76 } |
73 | 77 |
74 } // namespace cc | 78 } // namespace cc |
OLD | NEW |