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