1 // Copyright 2011 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_PENDING_TASK_H_ 6 #define BASE_PENDING_TASK_H_ 7 8 #include <array> 9 10 #include "base/base_export.h" 11 #include "base/functional/callback.h" 12 #include "base/location.h" 13 #include "base/task/delay_policy.h" 14 #include "base/time/time.h" 15 16 namespace base { 17 18 enum class Nestable : uint8_t { 19 kNonNestable, 20 kNestable, 21 }; 22 23 // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue 24 // for use by classes that queue and execute tasks. 25 struct BASE_EXPORT PendingTask { 26 PendingTask(); 27 PendingTask(const Location& posted_from, 28 OnceClosure task, 29 TimeTicks queue_time = TimeTicks(), 30 TimeTicks delayed_run_time = TimeTicks(), 31 TimeDelta leeway = TimeDelta(), 32 subtle::DelayPolicy delay_policy = 33 subtle::DelayPolicy::kFlexibleNoSooner); 34 PendingTask(PendingTask&& other); 35 ~PendingTask(); 36 37 PendingTask& operator=(PendingTask&& other); 38 39 // Returns the time at which this task should run. This is |delayed_run_time| 40 // for a delayed task, |queue_time| otherwise. 41 base::TimeTicks GetDesiredExecutionTime() const; 42 43 TimeTicks earliest_delayed_run_time() const; 44 TimeTicks latest_delayed_run_time() const; 45 46 // The task to run. 47 OnceClosure task; 48 49 // The site this PendingTask was posted from. 50 Location posted_from; 51 52 // The time at which the task was queued, which happens at post time. For 53 // deferred non-nestable tasks, this is reset when the nested loop exits and 54 // the deferred tasks are pushed back at the front of the queue. This is not 55 // set for immediate SequenceManager tasks unless SetAddQueueTimeToTasks(true) 56 // was called. This defaults to a null TimeTicks if the task hasn't been 57 // inserted in a sequence yet. 58 TimeTicks queue_time; 59 60 // The time when the task should be run. This is null for an immediate task. 61 base::TimeTicks delayed_run_time; 62 63 // |leeway| and |delay_policy| determine the preferred time range for running 64 // the delayed task. A larger leeway provides more freedom to run the task at 65 // an optimal time for power consumption. These fields are ignored for an 66 // immediate (non-delayed) task. 67 TimeDelta leeway; 68 subtle::DelayPolicy delay_policy = subtle::DelayPolicy::kFlexibleNoSooner; 69 70 // Chain of symbols of the parent tasks which led to this one being posted. 71 static constexpr size_t kTaskBacktraceLength = 4; 72 std::array<const void*, kTaskBacktraceLength> task_backtrace = {}; 73 74 // The context of the IPC message that was being handled when this task was 75 // posted. This is a hash of the IPC message name that is set within the scope 76 // of an IPC handler and when symbolized uniquely identifies the message being 77 // processed. This property is not propagated from one PendingTask to the 78 // next. For example, if pending task A was posted while handling an IPC, 79 // and pending task B was posted from within pending task A, then pending task 80 // B will not inherit the |ipc_hash| of pending task A. 81 uint32_t ipc_hash = 0; 82 const char* ipc_interface_name = nullptr; 83 84 // Secondary sort key for run time. 85 int sequence_num = 0; 86 87 bool task_backtrace_overflow = false; 88 }; 89 90 } // namespace base 91 92 #endif // BASE_PENDING_TASK_H_ 93