[task] Support wakeup alignment in ThreadPool

This CL plumbs leeway and DelayedPolicy to DelayedTaskManager and
applies alignment the same way ThreadControllerWithMessagePump does.

Note: webrtc thread pool use cases must use kPrecise to avoid
being affected be prior to landing this.

Bug: 1153139
Change-Id: I6b16f247be2e675c527e64f2e2a9036c3113c31e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3530226
Reviewed-by: Francois Pierre Doray <[email protected]>
Commit-Queue: Etienne Pierre-Doray <[email protected]>
Cr-Commit-Position: refs/heads/main@{#996207}
diff --git a/base/pending_task.h b/base/pending_task.h
index d023bc101..aa49027 100644
--- a/base/pending_task.h
+++ b/base/pending_task.h
@@ -10,6 +10,7 @@
 #include "base/base_export.h"
 #include "base/callback.h"
 #include "base/location.h"
+#include "base/task/delay_policy.h"
 #include "base/time/time.h"
 
 namespace base {
@@ -22,12 +23,16 @@
 // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
 // for use by classes that queue and execute tasks.
 struct BASE_EXPORT PendingTask {
+  static constexpr TimeDelta kDefaultLeeway = Milliseconds(8);
+
   PendingTask();
-  PendingTask(const Location& posted_from, OnceClosure task);
   PendingTask(const Location& posted_from,
               OnceClosure task,
-              TimeTicks queue_time,
-              TimeTicks delayed_run_time);
+              TimeTicks queue_time = TimeTicks(),
+              TimeTicks delayed_run_time = TimeTicks(),
+              TimeDelta leeway = TimeDelta(),
+              subtle::DelayPolicy delay_policy =
+                  subtle::DelayPolicy::kFlexibleNoSooner);
   PendingTask(PendingTask&& other);
   ~PendingTask();
 
@@ -37,6 +42,9 @@
   // for a delayed task, |queue_time| otherwise.
   base::TimeTicks GetDesiredExecutionTime() const;
 
+  TimeTicks earliest_delayed_run_time() const;
+  TimeTicks latest_delayed_run_time() const;
+
   // The task to run.
   OnceClosure task;
 
@@ -54,6 +62,13 @@
   // The time when the task should be run. This is null for an immediate task.
   base::TimeTicks delayed_run_time;
 
+  // |leeway| and |delay_policy| determine the preferred time range for running
+  // the delayed task. A larger leeway provides more freedom to run the task at
+  // an optimal time for power consumption. These fields are ignored for an
+  // immediate (non-delayed) task.
+  TimeDelta leeway;
+  subtle::DelayPolicy delay_policy = subtle::DelayPolicy::kFlexibleNoSooner;
+
   // Chain of symbols of the parent tasks which led to this one being posted.
   static constexpr size_t kTaskBacktraceLength = 4;
   std::array<const void*, kTaskBacktraceLength> task_backtrace = {};