xref: /aosp_15_r20/external/cronet/base/task/delayed_task_handle.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors
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 <memory>
9 
10 #include "base/base_export.h"
11 
12 namespace base {
13 
14 // A handle to a delayed task which can be used to cancel the posted task. Not
15 // thread-safe, can only be held and invoked from the posting sequence.
16 class BASE_EXPORT DelayedTaskHandle {
17  public:
18   // The delegate that allows each SequencedTaskRunners to have different
19   // implementations.
20   class Delegate {
21    public:
22     virtual ~Delegate() = default;
23 
24     // Returns true if the task handle is valid. Canceling or running the task
25     // will mark it as invalid.
26     virtual bool IsValid() const = 0;
27 
28     // Cancels the task. A canceled task, whether removed from the underlying
29     // queue or only marked as canceled, will never be Run().
30     virtual void CancelTask() = 0;
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(std::unique_ptr<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   std::unique_ptr<Delegate> delegate_;
52 };
53 
54 }  // namespace base
55 
56 #endif  // BASE_TASK_DELAYED_TASK_HANDLE_H_
57