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_SEQUENCE_MANAGER_DELAYED_TASK_HANDLE_DELEGATE_H_ 6 #define BASE_TASK_SEQUENCE_MANAGER_DELAYED_TASK_HANDLE_DELEGATE_H_ 7 8 #include "base/containers/intrusive_heap.h" 9 #include "base/memory/raw_ptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "base/sequence_checker.h" 12 #include "base/task/delayed_task_handle.h" 13 14 namespace base { 15 namespace sequence_manager { 16 namespace internal { 17 18 class TaskQueueImpl; 19 20 class DelayedTaskHandleDelegate : public DelayedTaskHandle::Delegate { 21 public: 22 explicit DelayedTaskHandleDelegate(TaskQueueImpl* outer); 23 24 DelayedTaskHandleDelegate(const DelayedTaskHandleDelegate&) = delete; 25 DelayedTaskHandleDelegate& operator=(const DelayedTaskHandleDelegate&) = 26 delete; 27 28 ~DelayedTaskHandleDelegate() override; 29 30 WeakPtr<DelayedTaskHandleDelegate> AsWeakPtr(); 31 32 // DelayedTaskHandle::Delegate: 33 bool IsValid() const override; 34 void CancelTask() override; 35 36 void SetHeapHandle(HeapHandle heap_handle); 37 void ClearHeapHandle(); 38 HeapHandle GetHeapHandle(); 39 40 // Indicates that this task will be executed. This will invalidate the handle. 41 void WillRunTask(); 42 43 private: 44 // The TaskQueueImpl where the task was posted. 45 const raw_ptr<TaskQueueImpl, AcrossTasksDanglingUntriaged> outer_ 46 GUARDED_BY_CONTEXT(sequence_checker_); 47 48 // The HeapHandle to the task, if the task is in the DelayedIncomingQueue, 49 // invalid otherwise. 50 HeapHandle heap_handle_ GUARDED_BY_CONTEXT(sequence_checker_); 51 52 SEQUENCE_CHECKER(sequence_checker_); 53 54 // Allows TaskQueueImpl to retain a weak reference to |this|. An outstanding 55 // weak pointer indicates that the task is valid. 56 WeakPtrFactory<DelayedTaskHandleDelegate> weak_ptr_factory_ GUARDED_BY_CONTEXT(sequence_checker_)57 GUARDED_BY_CONTEXT(sequence_checker_){this}; 58 }; 59 60 } // namespace internal 61 } // namespace sequence_manager 62 } // namespace base 63 64 #endif // BASE_TASK_SEQUENCE_MANAGER_DELAYED_TASK_HANDLE_DELEGATE_H_ 65