Avi Drissman | 3f7a9d8 | 2022-09-08 20:55:42 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CC_BASE_UNIQUE_NOTIFIER_H_ |
| 6 | #define CC_BASE_UNIQUE_NOTIFIER_H_ |
| 7 | |
Avi Drissman | 453cf7a7 | 2023-01-07 00:52:17 | [diff] [blame] | 8 | #include "base/functional/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 9 | #include "base/memory/raw_ptr.h" |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 11 | #include "cc/base/base_export.h" |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | class SequencedTaskRunner; |
| 15 | } // namespace base |
| 16 | |
| 17 | namespace cc { |
| 18 | |
Xu Xing | d36a6cd | 2018-09-26 01:56:02 | [diff] [blame] | 19 | // Callers must ensure that they only schedule the notifier on the same thread |
| 20 | // that the provided |task_runner| runs on. |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 21 | class CC_BASE_EXPORT UniqueNotifier { |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 22 | public: |
| 23 | // Configure this notifier to issue the |closure| notification when scheduled. |
| 24 | UniqueNotifier(base::SequencedTaskRunner* task_runner, |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 25 | base::RepeatingClosure closure); |
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame] | 26 | UniqueNotifier(const UniqueNotifier&) = delete; |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 27 | |
| 28 | // Destroying the notifier will ensure that no further notifications will |
| 29 | // happen from this class. |
| 30 | ~UniqueNotifier(); |
| 31 | |
Vladimir Levin | f06d1cd7 | 2019-03-13 18:24:10 | [diff] [blame] | 32 | UniqueNotifier& operator=(const UniqueNotifier&) = delete; |
| 33 | |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 34 | // Schedule a notification to be run. If another notification is already |
| 35 | // pending, then only one notification will take place. |
| 36 | void Schedule(); |
| 37 | |
vmpstr | 8c53b7f9 | 2015-07-01 21:47:47 | [diff] [blame] | 38 | // Cancel a pending notification, if one was scheduled. |
| 39 | void Cancel(); |
| 40 | |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 41 | private: |
| 42 | void Notify(); |
| 43 | |
dcheng | fba63314 | 2014-08-28 18:56:33 | [diff] [blame] | 44 | // TODO(dcheng): How come this doesn't need to hold a ref to the task runner? |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 45 | const raw_ptr<base::SequencedTaskRunner> task_runner_; |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 46 | const base::RepeatingClosure closure_; |
ericrk | 9c57a2d | 2016-08-22 19:39:56 | [diff] [blame] | 47 | |
| 48 | // Lock should be held before modifying |notification_pending_|. |
| 49 | base::Lock lock_; |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 50 | bool notification_pending_; |
| 51 | |
Jeremy Roman | 31f36b7 | 2019-07-09 16:33:41 | [diff] [blame] | 52 | base::WeakPtrFactory<UniqueNotifier> weak_ptr_factory_{this}; |
vmpstr@chromium.org | afb84fc | 2014-05-29 01:47:53 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | } // namespace cc |
| 56 | |
| 57 | #endif // CC_BASE_UNIQUE_NOTIFIER_H_ |