Refactor RepeatingTask to use absl::AnyInvocable functions of TaskQueue

Bug: webrtc:14245
Change-Id: Ie02755a4bb732cc25b3a22511e6d8920fc434c65
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/267847
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37461}
diff --git a/rtc_base/task_utils/repeating_task.cc b/rtc_base/task_utils/repeating_task.cc
index fa6ae79..222ab1a 100644
--- a/rtc_base/task_utils/repeating_task.cc
+++ b/rtc_base/task_utils/repeating_task.cc
@@ -11,16 +11,13 @@
 #include "rtc_base/task_utils/repeating_task.h"
 
 #include "absl/functional/any_invocable.h"
-#include "absl/memory/memory.h"
 #include "api/task_queue/pending_task_safety_flag.h"
-#include "api/task_queue/to_queued_task.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/time_utils.h"
 
 namespace webrtc {
 namespace {
 
-class RepeatingTask : public QueuedTask {
+class RepeatingTask {
  public:
   RepeatingTask(TaskQueueBase* task_queue,
                 TaskQueueBase::DelayPrecision precision,
@@ -28,11 +25,13 @@
                 absl::AnyInvocable<TimeDelta()> task,
                 Clock* clock,
                 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag);
-  ~RepeatingTask() override = default;
+  RepeatingTask(RepeatingTask&&) = default;
+  RepeatingTask& operator=(RepeatingTask&&) = delete;
+  ~RepeatingTask() = default;
+
+  void operator()() &&;
 
  private:
-  bool Run() final;
-
   TaskQueueBase* const task_queue_;
   const TaskQueueBase::DelayPrecision precision_;
   Clock* const clock_;
@@ -57,33 +56,27 @@
       next_run_time_(clock_->CurrentTime() + first_delay),
       alive_flag_(std::move(alive_flag)) {}
 
-bool RepeatingTask::Run() {
+void RepeatingTask::operator()() && {
   RTC_DCHECK_RUN_ON(task_queue_);
-  // Return true to tell the TaskQueue to destruct this object.
   if (!alive_flag_->alive())
-    return true;
+    return;
 
   webrtc_repeating_task_impl::RepeatingTaskImplDTraceProbeRun();
   TimeDelta delay = task_();
   RTC_DCHECK_GE(delay, TimeDelta::Zero());
 
   // A delay of +infinity means that the task should not be run again.
-  // Alternatively, the closure might have stopped this task. In either which
-  // case we return true to destruct this object.
+  // Alternatively, the closure might have stopped this task.
   if (delay.IsPlusInfinity() || !alive_flag_->alive())
-    return true;
+    return;
 
   TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
   next_run_time_ += delay;
   delay -= lost_time;
   delay = std::max(delay, TimeDelta::Zero());
 
-  task_queue_->PostDelayedTaskWithPrecision(precision_, absl::WrapUnique(this),
-                                            delay.ms());
-
-  // Return false to tell the TaskQueue to not destruct this object since we
-  // have taken ownership with absl::WrapUnique.
-  return false;
+  task_queue_->PostDelayedTaskWithPrecision(precision_, std::move(*this),
+                                            delay);
 }
 
 }  // namespace
@@ -95,9 +88,8 @@
     Clock* clock) {
   auto alive_flag = PendingTaskSafetyFlag::CreateDetached();
   webrtc_repeating_task_impl::RepeatingTaskHandleDTraceProbeStart();
-  task_queue->PostTask(
-      std::make_unique<RepeatingTask>(task_queue, precision, TimeDelta::Zero(),
-                                      std::move(closure), clock, alive_flag));
+  task_queue->PostTask(RepeatingTask(task_queue, precision, TimeDelta::Zero(),
+                                     std::move(closure), clock, alive_flag));
   return RepeatingTaskHandle(std::move(alive_flag));
 }
 
@@ -113,9 +105,9 @@
   webrtc_repeating_task_impl::RepeatingTaskHandleDTraceProbeDelayedStart();
   task_queue->PostDelayedTaskWithPrecision(
       precision,
-      std::make_unique<RepeatingTask>(task_queue, precision, first_delay,
-                                      std::move(closure), clock, alive_flag),
-      first_delay.ms());
+      RepeatingTask(task_queue, precision, first_delay, std::move(closure),
+                    clock, alive_flag),
+      first_delay);
   return RepeatingTaskHandle(std::move(alive_flag));
 }