Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/base/delayed_unique_notifier.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/sequenced_task_runner.h" | |
| 11 | |
| 12 namespace cc { | |
| 13 | |
| 14 DelayedUniqueNotifier::DelayedUniqueNotifier( | |
| 15 base::SequencedTaskRunner* task_runner, | |
| 16 const base::Closure& closure, | |
| 17 const base::TimeDelta& delay) | |
| 18 : task_runner_(task_runner), | |
| 19 closure_(closure), | |
| 20 delay_(delay), | |
| 21 notification_pending_(false), | |
| 22 weak_ptr_factory_(this) { | |
| 23 } | |
| 24 | |
| 25 DelayedUniqueNotifier::~DelayedUniqueNotifier() { | |
| 26 } | |
| 27 | |
| 28 void DelayedUniqueNotifier::Schedule() { | |
| 29 if (notification_pending_) { | |
| 30 next_notification_time_ = base::TimeTicks::Now() + delay_; | |
| 31 return; | |
| 32 } | |
| 33 | |
| 34 next_notification_time_ = base::TimeTicks::Now() + delay_; | |
| 35 task_runner_->PostDelayedTask(FROM_HERE, | |
| 36 base::Bind(&DelayedUniqueNotifier::NotifyIfTime, | |
| 37 weak_ptr_factory_.GetWeakPtr()), | |
| 38 delay_); | |
| 39 notification_pending_ = true; | |
| 40 } | |
| 41 | |
| 42 void DelayedUniqueNotifier::Cancel() { | |
| 43 weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
reveman
2014/05/22 23:24:28
This is going to be a problem as calling Schedule(
vmpstr
2014/05/27 18:14:59
Makes sense. Done. Now it should only ever have on
| |
| 44 notification_pending_ = false; | |
| 45 } | |
| 46 | |
| 47 void DelayedUniqueNotifier::NotifyIfTime() { | |
| 48 base::TimeTicks now = base::TimeTicks::Now(); | |
| 49 if (next_notification_time_ > now) { | |
| 50 task_runner_->PostDelayedTask( | |
| 51 FROM_HERE, | |
| 52 base::Bind(&DelayedUniqueNotifier::NotifyIfTime, | |
| 53 weak_ptr_factory_.GetWeakPtr()), | |
| 54 next_notification_time_ - now); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 // Note the order here is important since closure might schedule another run. | |
| 59 notification_pending_ = false; | |
| 60 closure_.Run(); | |
| 61 } | |
| 62 | |
| 63 } // namespace cc | |
| OLD | NEW |