Make stopping of the RepeatingTask safer

Previous implementation assumes that though RepeatingTask is owned by
the task queue, it will stay alive until RepeatingTaskHandler stops it.
That assumption doesn't hold by one of downstream TaskQueue implementaions.
That TaskQueue implementation shortly before destruction deletes
pending delayed tasks because it doesn't plan to run them,
and then runs remaining regular tasks.

Bug: None
Change-Id: Ic95fec2e9961b3f05727ff6fbdaf0664434a995b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/221984
Reviewed-by: Tommi <tommi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34274}
diff --git a/rtc_base/task_utils/repeating_task.cc b/rtc_base/task_utils/repeating_task.cc
index 574e633..9636680 100644
--- a/rtc_base/task_utils/repeating_task.cc
+++ b/rtc_base/task_utils/repeating_task.cc
@@ -12,32 +12,36 @@
 
 #include "absl/memory/memory.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/task_utils/pending_task_safety_flag.h"
 #include "rtc_base/task_utils/to_queued_task.h"
 #include "rtc_base/time_utils.h"
 
 namespace webrtc {
 namespace webrtc_repeating_task_impl {
 
-RepeatingTaskBase::RepeatingTaskBase(TaskQueueBase* task_queue,
-                                     TimeDelta first_delay,
-                                     Clock* clock)
+RepeatingTaskBase::RepeatingTaskBase(
+    TaskQueueBase* task_queue,
+    TimeDelta first_delay,
+    Clock* clock,
+    rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
     : task_queue_(task_queue),
       clock_(clock),
-      next_run_time_(clock_->CurrentTime() + first_delay) {}
+      next_run_time_(clock_->CurrentTime() + first_delay),
+      alive_flag_(std::move(alive_flag)) {}
 
 RepeatingTaskBase::~RepeatingTaskBase() = default;
 
 bool RepeatingTaskBase::Run() {
   RTC_DCHECK_RUN_ON(task_queue_);
   // Return true to tell the TaskQueue to destruct this object.
-  if (next_run_time_.IsPlusInfinity())
+  if (!alive_flag_->alive())
     return true;
 
   TimeDelta delay = RunClosure();
 
   // The closure might have stopped this task, in which case we return true to
   // destruct this object.
-  if (next_run_time_.IsPlusInfinity())
+  if (!alive_flag_->alive())
     return true;
 
   RTC_DCHECK(delay.IsFinite());
@@ -53,33 +57,11 @@
   return false;
 }
 
-void RepeatingTaskBase::Stop() {
-  RTC_DCHECK_RUN_ON(task_queue_);
-  RTC_DCHECK(next_run_time_.IsFinite());
-  next_run_time_ = Timestamp::PlusInfinity();
-}
-
 }  // namespace webrtc_repeating_task_impl
 
-RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other)
-    : repeating_task_(other.repeating_task_) {
-  other.repeating_task_ = nullptr;
-}
-
-RepeatingTaskHandle& RepeatingTaskHandle::operator=(
-    RepeatingTaskHandle&& other) {
-  repeating_task_ = other.repeating_task_;
-  other.repeating_task_ = nullptr;
-  return *this;
-}
-
-RepeatingTaskHandle::RepeatingTaskHandle(
-    webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task)
-    : repeating_task_(repeating_task) {}
-
 void RepeatingTaskHandle::Stop() {
   if (repeating_task_) {
-    repeating_task_->Stop();
+    repeating_task_->SetNotAlive();
     repeating_task_ = nullptr;
   }
 }