Introduce new interface for TaskQueueBase using absl::AnyInvocable

Bug: webrtc:14245
Change-Id: Ie4f47ea9753d6644aec2e95f531b521cc119a6c8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/267402
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37439}
diff --git a/api/task_queue/task_queue_base.h b/api/task_queue/task_queue_base.h
index c3e79b7..870637c 100644
--- a/api/task_queue/task_queue_base.h
+++ b/api/task_queue/task_queue_base.h
@@ -13,7 +13,9 @@
 #include <memory>
 #include <utility>
 
+#include "absl/functional/any_invocable.h"
 #include "api/task_queue/queued_task.h"
+#include "api/units/time_delta.h"
 #include "rtc_base/system/rtc_export.h"
 #include "rtc_base/thread_annotations.h"
 
@@ -48,27 +50,32 @@
   // was created on.
   virtual void Delete() = 0;
 
-  // Schedules a task to execute. Tasks are executed in FIFO order.
-  // If `task->Run()` returns true, task is deleted on the task queue
-  // before next QueuedTask starts executing.
+  // Schedules a `task` to execute. Tasks are executed in FIFO order.
   // When a TaskQueue is deleted, pending tasks will not be executed but they
   // will be deleted. The deletion of tasks may happen synchronously on the
   // TaskQueue or it may happen asynchronously after TaskQueue is deleted.
   // This may vary from one implementation to the next so assumptions about
   // lifetimes of pending tasks should not be made.
   // May be called on any thread or task queue, including this task queue.
-  virtual void PostTask(std::unique_ptr<QueuedTask> task) = 0;
+  // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all
+  // derived classes.
+  virtual void PostTask(absl::AnyInvocable<void() &&> task);
+
+  // Deprecated, use PostTask variant above in new code.
+  // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
+  // function above.
+  virtual void PostTask(std::unique_ptr<QueuedTask> task);
 
   // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
   // possible.
   //
-  // Schedules a task to execute a specified number of milliseconds from when
-  // the call is made, using "low" precision. All scheduling is affected by
-  // OS-specific leeway and current workloads which means that in terms of
-  // precision there are no hard guarantees, but in addition to the OS induced
-  // leeway, "low" precision adds up to a 17 ms additional leeway. The purpose
-  // of this leeway is to achieve more efficient CPU scheduling and reduce Idle
-  // Wake Up frequency.
+  // Schedules a `task` to execute a specified `delay` from when the call is
+  // made, using "low" precision. All scheduling is affected by OS-specific
+  // leeway and current workloads which means that in terms of precision there
+  // are no hard guarantees, but in addition to the OS induced leeway, "low"
+  // precision adds up to a 17 ms additional leeway. The purpose of this leeway
+  // is to achieve more efficient CPU scheduling and reduce Idle Wake Up
+  // frequency.
   //
   // The task may execute with [-1, 17 + OS induced leeway) ms additional delay.
   //
@@ -82,16 +89,24 @@
   // https://crbug.com/webrtc/13583 for more information.
   //
   // May be called on any thread or task queue, including this task queue.
+  // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all
+  // derived classes.
+  virtual void PostDelayedTask(absl::AnyInvocable<void() &&> task,
+                               TimeDelta delay);
+
+  // Deprecated, use PostDelayedTask variant above in new code.
+  // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
+  // function above.
   virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task,
-                               uint32_t milliseconds) = 0;
+                               uint32_t milliseconds);
 
   // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
   // possible.
   //
-  // Schedules a task to execute a specified number of milliseconds from when
-  // the call is made, using "high" precision. All scheduling is affected by
-  // OS-specific leeway and current workloads which means that in terms of
-  // precision there are no hard guarantees.
+  // Schedules a `task` to execute a specified `delay` from when the call is
+  // made, using "high" precision. All scheduling is affected by OS-specific
+  // leeway and current workloads which means that in terms of precision there
+  // are no hard guarantees.
   //
   // The task may execute with [-1, OS induced leeway] ms additional delay.
   //
@@ -101,16 +116,38 @@
   // battery, when the timer precision can be as poor as 15 ms.
   //
   // May be called on any thread or task queue, including this task queue.
+  // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all
+  // derived classes.
+  virtual void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
+                                            TimeDelta delay);
+
+  // Deprecated, use `PostDelayedHighPrecisionTask` variant above in new code.
+  // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
+  // function above.
   virtual void PostDelayedHighPrecisionTask(std::unique_ptr<QueuedTask> task,
                                             uint32_t milliseconds) {
-    // Remove default implementation when dependencies have implemented this
-    // method.
     PostDelayedTask(std::move(task), milliseconds);
   }
 
-  // As specified by |precision|, calls either PostDelayedTask() or
+  // As specified by `precision`, calls either PostDelayedTask() or
   // PostDelayedHighPrecisionTask().
   void PostDelayedTaskWithPrecision(DelayPrecision precision,
+                                    absl::AnyInvocable<void() &&> task,
+                                    TimeDelta delay) {
+    switch (precision) {
+      case DelayPrecision::kLow:
+        PostDelayedTask(std::move(task), delay);
+        break;
+      case DelayPrecision::kHigh:
+        PostDelayedHighPrecisionTask(std::move(task), delay);
+        break;
+    }
+  }
+
+  // Deprecated, use `PostDelayedTaskWithPrecision` variant above in new code.
+  // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the
+  // function above.
+  void PostDelayedTaskWithPrecision(DelayPrecision precision,
                                     std::unique_ptr<QueuedTask> task,
                                     uint32_t milliseconds) {
     switch (precision) {