blob: b02dca944fda67088817c2f962dbdd5c7f00bfa9 [file] [log] [blame]
Patrick Monette88efc172021-10-29 16:17:361// 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
Patrick Monettef9eba9a2021-12-08 00:55:078#include <memory>
9
Patrick Monette88efc172021-10-29 16:17:3610#include "base/base_export.h"
Patrick Monette88efc172021-10-29 16:17:3611
12namespace 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.
16class BASE_EXPORT DelayedTaskHandle {
17 public:
18 // The delegate that allows each SequencedTaskRunners to have different
19 // implementations.
Patrick Monettef9eba9a2021-12-08 00:55:0720 class Delegate {
Patrick Monette88efc172021-10-29 16:17:3621 public:
Patrick Monettef9eba9a2021-12-08 00:55:0722 virtual ~Delegate() = default;
23
Patrick Monette88efc172021-10-29 16:17:3624 // Returns true if the task handle is valid.
25 virtual bool IsValid() const = 0;
26
27 // Cancels the task. A canceled task, whether removed from the underlying
28 // queue or only marked as canceled, will never be Run().
29 virtual void CancelTask() = 0;
Patrick Monette88efc172021-10-29 16:17:3630 };
31
32 // Construct a default, invalid, task handle.
33 DelayedTaskHandle();
34
35 // Construct a valid task handle with the specified |delegate|.
Patrick Monettef9eba9a2021-12-08 00:55:0736 explicit DelayedTaskHandle(std::unique_ptr<Delegate> delegate);
Patrick Monette88efc172021-10-29 16:17:3637
38 ~DelayedTaskHandle();
39
40 DelayedTaskHandle(DelayedTaskHandle&&);
41 DelayedTaskHandle& operator=(DelayedTaskHandle&&);
42
43 // Returns true if the task handle is valid.
44 bool IsValid() const;
45
46 // Cancels the task.
47 void CancelTask();
48
49 private:
Patrick Monettef9eba9a2021-12-08 00:55:0750 std::unique_ptr<Delegate> delegate_;
Patrick Monette88efc172021-10-29 16:17:3651};
52
53} // namespace base
54
55#endif // BASE_TASK_DELAYED_TASK_HANDLE_H_