summaryrefslogtreecommitdiffstats
path: root/chromium/cc/base/delayed_unique_notifier.h
blob: 1f93416dd25a57019846136c63c94867fdff52a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_BASE_DELAYED_UNIQUE_NOTIFIER_H_
#define CC_BASE_DELAYED_UNIQUE_NOTIFIER_H_

#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "cc/base/cc_export.h"

namespace base {
class SequencedTaskRunner;
}  // namespace base

namespace cc {

class CC_EXPORT DelayedUniqueNotifier {
 public:
  // Configure this notifier to issue the |closure| notification in |delay| time
  // from Schedule() call.
  DelayedUniqueNotifier(base::SequencedTaskRunner* task_runner,
                        const base::Closure& closure,
                        const base::TimeDelta& delay);

  // Destroying the notifier will ensure that no further notifications will
  // happen from this class.
  virtual ~DelayedUniqueNotifier();

  // Schedule a notification to be run. If another notification is already
  // pending, then it will happen in (at least) given delay from now. That is,
  // if delay is 16ms and a notification has been scheduled 10ms ago (ie, it
  // should trigger in no less than 6ms), then calling schedule will ensure that
  // the only notification that arrives will happen in (at least) 16ms from now.
  void Schedule();

  // Cancel any previously scheduled runs.
  void Cancel();

  // Returns true if a notification is currently scheduled to run.
  bool HasPendingNotification() const;

 protected:
  // Virtual for testing.
  virtual base::TimeTicks Now() const;

 private:
  void NotifyIfTime();

  base::SequencedTaskRunner* task_runner_;
  base::Closure closure_;
  base::TimeDelta delay_;
  base::TimeTicks next_notification_time_;
  bool notification_pending_;

  base::WeakPtrFactory<DelayedUniqueNotifier> weak_ptr_factory_;
};

}  // namespace cc

#endif  // CC_BASE_DELAYED_UNIQUE_NOTIFIER_H_