Revert of Fix cancellation of a pair of URLRequestJob subclasses (patchset #6 id...
[chromium-blink-merge.git] / cc / base / delayed_unique_notifier.cc
blob1824226c2a6d19838d817c06a0824714784ff00e
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.
5 #include "cc/base/delayed_unique_notifier.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/sequenced_task_runner.h"
12 namespace cc {
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) {
25 DelayedUniqueNotifier::~DelayedUniqueNotifier() {
28 void DelayedUniqueNotifier::Schedule() {
29 if (notification_pending_) {
30 next_notification_time_ = Now() + delay_;
31 return;
34 next_notification_time_ = Now() + delay_;
35 task_runner_->PostDelayedTask(FROM_HERE,
36 base::Bind(&DelayedUniqueNotifier::NotifyIfTime,
37 weak_ptr_factory_.GetWeakPtr()),
38 delay_);
39 notification_pending_ = true;
42 void DelayedUniqueNotifier::Cancel() {
43 next_notification_time_ = base::TimeTicks();
46 void DelayedUniqueNotifier::Shutdown() {
47 // This function must destroy any weak ptrs since after being cancelled, this
48 // class may be destroyed on another thread during compositor shutdown.
49 weak_ptr_factory_.InvalidateWeakPtrs();
50 // Deliberately leaves notification_pending_ = true forever so new tasks with
51 // weak ptrs can not be created.
52 notification_pending_ = true;
55 bool DelayedUniqueNotifier::HasPendingNotification() const {
56 return notification_pending_ && !next_notification_time_.is_null();
59 base::TimeTicks DelayedUniqueNotifier::Now() const {
60 return base::TimeTicks::Now();
63 void DelayedUniqueNotifier::NotifyIfTime() {
64 // If next notifiaction time is not valid, then this schedule was canceled.
65 if (next_notification_time_.is_null()) {
66 notification_pending_ = false;
67 return;
70 // If the notification was rescheduled or arrived too early for any other
71 // reason, then post another task instead of running the callback.
72 base::TimeTicks now = Now();
73 if (next_notification_time_ > now) {
74 task_runner_->PostDelayedTask(
75 FROM_HERE,
76 base::Bind(&DelayedUniqueNotifier::NotifyIfTime,
77 weak_ptr_factory_.GetWeakPtr()),
78 next_notification_time_ - now);
79 return;
82 // Note the order here is important since closure might schedule another run.
83 notification_pending_ = false;
84 closure_.Run();
87 } // namespace cc