default_delayed_task_handle_delegate.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #include "base/task/default_delayed_task_handle_delegate.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. namespace base {
  8. DefaultDelayedTaskHandleDelegate::DefaultDelayedTaskHandleDelegate() = default;
  9. DefaultDelayedTaskHandleDelegate::~DefaultDelayedTaskHandleDelegate() = default;
  10. bool DefaultDelayedTaskHandleDelegate::IsValid() const {
  11. return weak_ptr_factory_.HasWeakPtrs();
  12. }
  13. void DefaultDelayedTaskHandleDelegate::CancelTask() {
  14. weak_ptr_factory_.InvalidateWeakPtrs();
  15. }
  16. OnceClosure DefaultDelayedTaskHandleDelegate::BindCallback(
  17. OnceClosure callback) {
  18. DCHECK(!IsValid());
  19. return BindOnce(&DefaultDelayedTaskHandleDelegate::RunTask,
  20. weak_ptr_factory_.GetWeakPtr(), std::move(callback));
  21. }
  22. void DefaultDelayedTaskHandleDelegate::RunTask(OnceClosure user_task) {
  23. // Invalidate the weak pointer first so that the task handle is considered
  24. // invalid while running the task.
  25. weak_ptr_factory_.InvalidateWeakPtrs();
  26. std::move(user_task).Run();
  27. }
  28. } // namespace base