Patrick Monette | 88efc17 | 2021-10-29 16:17:36 | [diff] [blame^] | 1 | // Copyright 2021 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 | #ifndef BASE_TASK_DELAYED_TASK_HANDLE_H_ |
| 6 | #define BASE_TASK_DELAYED_TASK_HANDLE_H_ |
| 7 | |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/memory/ref_counted.h" |
| 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | // A handle to a delayed task which can be used to cancel the posted task. Not |
| 14 | // thread-safe, can only be held and invoked from the posting sequence. |
| 15 | class BASE_EXPORT DelayedTaskHandle { |
| 16 | public: |
| 17 | // The delegate that allows each SequencedTaskRunners to have different |
| 18 | // implementations. |
| 19 | class Delegate : public RefCounted<Delegate> { |
| 20 | public: |
| 21 | // Returns true if the task handle is valid. |
| 22 | virtual bool IsValid() const = 0; |
| 23 | |
| 24 | // Cancels the task. A canceled task, whether removed from the underlying |
| 25 | // queue or only marked as canceled, will never be Run(). |
| 26 | virtual void CancelTask() = 0; |
| 27 | |
| 28 | protected: |
| 29 | friend class RefCounted<Delegate>; |
| 30 | virtual ~Delegate() = default; |
| 31 | }; |
| 32 | |
| 33 | // Construct a default, invalid, task handle. |
| 34 | DelayedTaskHandle(); |
| 35 | |
| 36 | // Construct a valid task handle with the specified |delegate|. |
| 37 | explicit DelayedTaskHandle(scoped_refptr<Delegate> delegate); |
| 38 | |
| 39 | ~DelayedTaskHandle(); |
| 40 | |
| 41 | DelayedTaskHandle(DelayedTaskHandle&&); |
| 42 | DelayedTaskHandle& operator=(DelayedTaskHandle&&); |
| 43 | |
| 44 | // Returns true if the task handle is valid. |
| 45 | bool IsValid() const; |
| 46 | |
| 47 | // Cancels the task. |
| 48 | void CancelTask(); |
| 49 | |
| 50 | private: |
| 51 | scoped_refptr<Delegate> delegate_; |
| 52 | }; |
| 53 | |
| 54 | } // namespace base |
| 55 | |
| 56 | #endif // BASE_TASK_DELAYED_TASK_HANDLE_H_ |