default_delayed_task_handle_delegate.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. #ifndef BASE_TASK_DEFAULT_DELAYED_TASK_HANDLE_DELEGATE_H_
  5. #define BASE_TASK_DEFAULT_DELAYED_TASK_HANDLE_DELEGATE_H_
  6. #include "base/base_export.h"
  7. #include "base/callback.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/task/delayed_task_handle.h"
  10. namespace base {
  11. // A default implementation of DelayedTaskHandle::Delegate that can cancel the
  12. // delayed task by invalidating a weak pointer.
  13. class BASE_EXPORT DefaultDelayedTaskHandleDelegate
  14. : public DelayedTaskHandle::Delegate {
  15. public:
  16. DefaultDelayedTaskHandleDelegate();
  17. ~DefaultDelayedTaskHandleDelegate() override;
  18. // DelayedTaskHandle::Delegate:
  19. bool IsValid() const override;
  20. void CancelTask() override;
  21. // Returns a new callback bound to this object such that it can be cancelled
  22. // by invalidating |weak_ptr_factory_|.
  23. OnceClosure BindCallback(OnceClosure callback);
  24. private:
  25. // Runs |callback|.
  26. void RunTask(OnceClosure callback);
  27. WeakPtrFactory<DefaultDelayedTaskHandleDelegate> weak_ptr_factory_{this};
  28. };
  29. } // namespace base
  30. #endif // BASE_TASK_DEFAULT_DELAYED_TASK_HANDLE_DELEGATE_H_