Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: cc/base/unique_notifier.cc

Issue 296043005: cc: Add UniqueNotifier to cc/base. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: typo Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/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 UniqueNotifier::UniqueNotifier(
15 const scoped_refptr<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 UniqueNotifier::~UniqueNotifier() {
26 weak_ptr_factory_.InvalidateWeakPtrs();
reveman 2014/05/21 17:03:37 No need to do this explicitly. I think it's pretty
vmpstr 2014/05/21 17:41:02 Done.
27 }
28
29 void UniqueNotifier::Schedule() {
30 if (notification_pending_) {
31 if (delay_ != base::TimeDelta())
32 next_notification_time_ = base::TimeTicks::Now() + delay_;
33 return;
34 }
35
36 if (delay_ == base::TimeDelta()) {
37 task_runner_->PostTask(FROM_HERE,
38 base::Bind(&UniqueNotifier::NotifyIfTime,
39 weak_ptr_factory_.GetWeakPtr()));
40 } else {
41 next_notification_time_ = base::TimeTicks::Now() + delay_;
42 task_runner_->PostDelayedTask(FROM_HERE,
43 base::Bind(&UniqueNotifier::NotifyIfTime,
44 weak_ptr_factory_.GetWeakPtr()),
45 delay_);
46 }
47 notification_pending_ = true;
48 }
49
50 void UniqueNotifier::NotifyIfTime() {
51 if (!next_notification_time_.is_null()) {
52 base::TimeTicks now = base::TimeTicks::Now();
53 if (next_notification_time_ > now) {
54 task_runner_->PostDelayedTask(FROM_HERE,
55 base::Bind(&UniqueNotifier::NotifyIfTime,
56 weak_ptr_factory_.GetWeakPtr()),
57 next_notification_time_ - now);
58 return;
59 }
60 }
61
62 notification_pending_ = false;
63 closure_.Run();
64 }
65
66 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698