Delete QueuedTask and ToQueuedTask as no longer needed

Bug: webrtc:14245
Change-Id: I4b36c8d6f0709202e01d22386644c20cad58450f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/269403
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37718}
diff --git a/BUILD.gn b/BUILD.gn
index 04db39a..31ee99b 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -557,7 +557,6 @@
       "api/audio_codecs/test:audio_codecs_api_unittests",
       "api/numerics:numerics_unittests",
       "api/task_queue:pending_task_safety_flag_unittests",
-      "api/task_queue:to_queued_task_unittests",
       "api/transport:stun_unittest",
       "api/video/test:rtc_api_video_unittests",
       "api/video_codecs/test:video_codecs_api_unittests",
diff --git a/api/task_queue/BUILD.gn b/api/task_queue/BUILD.gn
index 62a4a61..8daa01d 100644
--- a/api/task_queue/BUILD.gn
+++ b/api/task_queue/BUILD.gn
@@ -11,7 +11,6 @@
 rtc_library("task_queue") {
   visibility = [ "*" ]
   public = [
-    "queued_task.h",
     "task_queue_base.h",
     "task_queue_factory.h",
   ]
@@ -99,15 +98,6 @@
   }
 }
 
-rtc_source_set("to_queued_task") {
-  visibility = [ "*" ]
-  sources = [ "to_queued_task.h" ]
-  deps = [
-    ":task_queue",
-    "../../api/task_queue:pending_task_safety_flag",
-  ]
-}
-
 rtc_library("pending_task_safety_flag") {
   visibility = [ "*" ]
   sources = [
@@ -135,23 +125,11 @@
     ]
   }
 
-  rtc_library("to_queued_task_unittests") {
-    testonly = true
-    sources = [ "to_queued_task_unittest.cc" ]
-    deps = [
-      ":to_queued_task",
-      "../../api/task_queue",
-      "../../test:test_support",
-    ]
-    absl_deps = [ "//third_party/abseil-cpp/absl/memory" ]
-  }
-
   rtc_library("pending_task_safety_flag_unittests") {
     testonly = true
     sources = [ "pending_task_safety_flag_unittest.cc" ]
     deps = [
       ":pending_task_safety_flag",
-      ":to_queued_task",
       "../../rtc_base:logging",
       "../../rtc_base:rtc_event",
       "../../rtc_base:rtc_task_queue",
diff --git a/api/task_queue/queued_task.h b/api/task_queue/queued_task.h
deleted file mode 100644
index 27a5eda..0000000
--- a/api/task_queue/queued_task.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- *  Copyright 2018 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-#ifndef API_TASK_QUEUE_QUEUED_TASK_H_
-#define API_TASK_QUEUE_QUEUED_TASK_H_
-
-namespace webrtc {
-
-// Base interface for asynchronously executed tasks.
-// The interface basically consists of a single function, Run(), that executes
-// on the target queue.  For more details see the Run() method and TaskQueue.
-class QueuedTask {
- public:
-  virtual ~QueuedTask() = default;
-
-  // Main routine that will run when the task is executed on the desired queue.
-  // The task should return `true` to indicate that it should be deleted or
-  // `false` to indicate that the queue should consider ownership of the task
-  // having been transferred.  Returning `false` can be useful if a task has
-  // re-posted itself to a different queue or is otherwise being re-used.
-  virtual bool Run() = 0;
-};
-
-}  // namespace webrtc
-
-#endif  // API_TASK_QUEUE_QUEUED_TASK_H_
diff --git a/api/task_queue/task_queue_base.cc b/api/task_queue/task_queue_base.cc
index 11231a0..ecdc7f7 100644
--- a/api/task_queue/task_queue_base.cc
+++ b/api/task_queue/task_queue_base.cc
@@ -79,69 +79,3 @@
 #else
 #error Unsupported platform
 #endif
-
-// Functions to support transition from std::unique_ptr<QueuedTask>
-// representation of a task to absl::AnyInvocable<void() &&> representation. In
-// the base interface older and newer functions call each other. This way
-// TaskQueue would work when classes derived from TaskQueueBase implements
-// either old set of Post functions (before the transition) or new set of Post
-// functions. Thus callers of the interface can be updated independently of all
-// of the implementations of that TaskQueueBase interface.
-
-// TODO(bugs.webrtc.org/14245): Delete these functions when transition is
-// complete.
-namespace webrtc {
-namespace {
-class Task : public QueuedTask {
- public:
-  explicit Task(absl::AnyInvocable<void() &&> task) : task_(std::move(task)) {}
-  ~Task() override = default;
-
-  bool Run() override {
-    std::move(task_)();
-    return true;
-  }
-
- private:
-  absl::AnyInvocable<void() &&> task_;
-};
-
-std::unique_ptr<QueuedTask> ToLegacy(absl::AnyInvocable<void() &&> task) {
-  return std::make_unique<Task>(std::move(task));
-}
-
-absl::AnyInvocable<void() &&> FromLegacy(std::unique_ptr<QueuedTask> task) {
-  return [task = std::move(task)]() mutable {
-    if (!task->Run()) {
-      task.release();
-    }
-  };
-}
-
-}  // namespace
-
-void TaskQueueBase::PostTask(std::unique_ptr<QueuedTask> task) {
-  PostTask(FromLegacy(std::move(task)));
-}
-
-void TaskQueueBase::PostTask(absl::AnyInvocable<void() &&> task) {
-  PostTask(ToLegacy(std::move(task)));
-}
-
-void TaskQueueBase::PostDelayedTask(std::unique_ptr<QueuedTask> task,
-                                    uint32_t milliseconds) {
-  PostDelayedTask(FromLegacy(std::move(task)), TimeDelta::Millis(milliseconds));
-}
-
-void TaskQueueBase::PostDelayedTask(absl::AnyInvocable<void() &&> task,
-                                    TimeDelta delay) {
-  PostDelayedTask(ToLegacy(std::move(task)), delay.ms());
-}
-
-void TaskQueueBase::PostDelayedHighPrecisionTask(
-    absl::AnyInvocable<void() &&> task,
-    TimeDelta delay) {
-  PostDelayedHighPrecisionTask(ToLegacy(std::move(task)), delay.ms());
-}
-
-}  // namespace webrtc
diff --git a/api/task_queue/task_queue_base.h b/api/task_queue/task_queue_base.h
index 870637c..a2cff9c 100644
--- a/api/task_queue/task_queue_base.h
+++ b/api/task_queue/task_queue_base.h
@@ -14,7 +14,6 @@
 #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"
@@ -57,14 +56,7 @@
   // 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.
-  // 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);
+  virtual void PostTask(absl::AnyInvocable<void() &&> task) = 0;
 
   // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
   // possible.
@@ -89,16 +81,8 @@
   // 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);
+                               TimeDelta delay) = 0;
 
   // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
   // possible.
@@ -116,18 +100,8 @@
   // 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) {
-    PostDelayedTask(std::move(task), milliseconds);
-  }
+                                            TimeDelta delay) = 0;
 
   // As specified by `precision`, calls either PostDelayedTask() or
   // PostDelayedHighPrecisionTask().
@@ -144,22 +118,6 @@
     }
   }
 
-  // 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) {
-      case DelayPrecision::kLow:
-        PostDelayedTask(std::move(task), milliseconds);
-        break;
-      case DelayPrecision::kHigh:
-        PostDelayedHighPrecisionTask(std::move(task), milliseconds);
-        break;
-    }
-  }
-
   // Returns the task queue that is running the current thread.
   // Returns nullptr if this thread is not associated with any task queue.
   // May be called on any thread or task queue, including this task queue.
diff --git a/api/task_queue/to_queued_task.h b/api/task_queue/to_queued_task.h
deleted file mode 100644
index 658eecc..0000000
--- a/api/task_queue/to_queued_task.h
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef API_TASK_QUEUE_TO_QUEUED_TASK_H_
-#define API_TASK_QUEUE_TO_QUEUED_TASK_H_
-
-#include <memory>
-#include <type_traits>
-#include <utility>
-
-#include "api/task_queue/pending_task_safety_flag.h"
-#include "api/task_queue/queued_task.h"
-
-namespace webrtc {
-namespace webrtc_new_closure_impl {
-// Simple implementation of QueuedTask for use with lambdas.
-template <typename Closure>
-class ClosureTask : public QueuedTask {
- public:
-  explicit ClosureTask(Closure&& closure)
-      : closure_(std::forward<Closure>(closure)) {}
-
- private:
-  bool Run() override {
-    closure_();
-    return true;
-  }
-
-  typename std::decay<Closure>::type closure_;
-};
-
-template <typename Closure>
-class SafetyClosureTask : public QueuedTask {
- public:
-  explicit SafetyClosureTask(rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
-                             Closure&& closure)
-      : closure_(std::forward<Closure>(closure)),
-        safety_flag_(std::move(safety)) {}
-
- private:
-  bool Run() override {
-    if (safety_flag_->alive())
-      closure_();
-    return true;
-  }
-
-  typename std::decay<Closure>::type closure_;
-  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_;
-};
-
-// Extends ClosureTask to also allow specifying cleanup code.
-// This is useful when using lambdas if guaranteeing cleanup, even if a task
-// was dropped (queue is too full), is required.
-template <typename Closure, typename Cleanup>
-class ClosureTaskWithCleanup : public ClosureTask<Closure> {
- public:
-  ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup)
-      : ClosureTask<Closure>(std::forward<Closure>(closure)),
-        cleanup_(std::forward<Cleanup>(cleanup)) {}
-  ~ClosureTaskWithCleanup() override { cleanup_(); }
-
- private:
-  typename std::decay<Cleanup>::type cleanup_;
-};
-}  // namespace webrtc_new_closure_impl
-
-// Convenience function to construct closures that can be passed directly
-// to methods that support std::unique_ptr<QueuedTask> but not template
-// based parameters.
-template <typename Closure>
-std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure) {
-  return std::make_unique<webrtc_new_closure_impl::ClosureTask<Closure>>(
-      std::forward<Closure>(closure));
-}
-
-template <typename Closure>
-std::unique_ptr<QueuedTask> ToQueuedTask(
-    rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
-    Closure&& closure) {
-  return std::make_unique<webrtc_new_closure_impl::SafetyClosureTask<Closure>>(
-      std::move(safety), std::forward<Closure>(closure));
-}
-
-template <typename Closure>
-std::unique_ptr<QueuedTask> ToQueuedTask(const ScopedTaskSafety& safety,
-                                         Closure&& closure) {
-  return ToQueuedTask(safety.flag(), std::forward<Closure>(closure));
-}
-
-template <typename Closure,
-          typename Cleanup,
-          typename std::enable_if<!std::is_same<
-              typename std::remove_const<
-                  typename std::remove_reference<Closure>::type>::type,
-              ScopedTaskSafety>::value>::type* = nullptr>
-std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure, Cleanup&& cleanup) {
-  return std::make_unique<
-      webrtc_new_closure_impl::ClosureTaskWithCleanup<Closure, Cleanup>>(
-      std::forward<Closure>(closure), std::forward<Cleanup>(cleanup));
-}
-
-}  // namespace webrtc
-
-#endif  // API_TASK_QUEUE_TO_QUEUED_TASK_H_
diff --git a/api/task_queue/to_queued_task_unittest.cc b/api/task_queue/to_queued_task_unittest.cc
deleted file mode 100644
index aa11041..0000000
--- a/api/task_queue/to_queued_task_unittest.cc
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include "api/task_queue/to_queued_task.h"
-
-#include <memory>
-
-#include "absl/memory/memory.h"
-#include "api/task_queue/queued_task.h"
-#include "test/gmock.h"
-#include "test/gtest.h"
-
-namespace webrtc {
-namespace {
-
-using ::testing::InSequence;
-using ::testing::MockFunction;
-
-void RunTask(std::unique_ptr<QueuedTask> task) {
-  // Simulate how task queue suppose to run tasks.
-  QueuedTask* raw = task.release();
-  if (raw->Run())
-    delete raw;
-}
-
-TEST(ToQueuedTaskTest, AcceptsLambda) {
-  bool run = false;
-  std::unique_ptr<QueuedTask> task = ToQueuedTask([&run] { run = true; });
-  EXPECT_FALSE(run);
-  RunTask(std::move(task));
-  EXPECT_TRUE(run);
-}
-
-TEST(ToQueuedTaskTest, AcceptsCopyableClosure) {
-  struct CopyableClosure {
-    CopyableClosure(int* num_copies, int* num_moves, int* num_runs)
-        : num_copies(num_copies), num_moves(num_moves), num_runs(num_runs) {}
-    CopyableClosure(const CopyableClosure& other)
-        : num_copies(other.num_copies),
-          num_moves(other.num_moves),
-          num_runs(other.num_runs) {
-      ++*num_copies;
-    }
-    CopyableClosure(CopyableClosure&& other)
-        : num_copies(other.num_copies),
-          num_moves(other.num_moves),
-          num_runs(other.num_runs) {
-      ++*num_moves;
-    }
-    void operator()() { ++*num_runs; }
-
-    int* num_copies;
-    int* num_moves;
-    int* num_runs;
-  };
-
-  int num_copies = 0;
-  int num_moves = 0;
-  int num_runs = 0;
-
-  std::unique_ptr<QueuedTask> task;
-  {
-    CopyableClosure closure(&num_copies, &num_moves, &num_runs);
-    task = ToQueuedTask(closure);
-    // Destroy closure to check with msan task has own copy.
-  }
-  EXPECT_EQ(num_copies, 1);
-  EXPECT_EQ(num_runs, 0);
-  RunTask(std::move(task));
-  EXPECT_EQ(num_copies, 1);
-  EXPECT_EQ(num_moves, 0);
-  EXPECT_EQ(num_runs, 1);
-}
-
-TEST(ToQueuedTaskTest, AcceptsMoveOnlyClosure) {
-  struct MoveOnlyClosure {
-    MoveOnlyClosure(int* num_moves, std::function<void()> trigger)
-        : num_moves(num_moves), trigger(std::move(trigger)) {}
-    MoveOnlyClosure(const MoveOnlyClosure&) = delete;
-    MoveOnlyClosure(MoveOnlyClosure&& other)
-        : num_moves(other.num_moves), trigger(std::move(other.trigger)) {
-      ++*num_moves;
-    }
-    void operator()() { trigger(); }
-
-    int* num_moves;
-    std::function<void()> trigger;
-  };
-
-  int num_moves = 0;
-  MockFunction<void()> run;
-
-  auto task = ToQueuedTask(MoveOnlyClosure(&num_moves, run.AsStdFunction()));
-  EXPECT_EQ(num_moves, 1);
-  EXPECT_CALL(run, Call);
-  RunTask(std::move(task));
-  EXPECT_EQ(num_moves, 1);
-}
-
-TEST(ToQueuedTaskTest, AcceptsMoveOnlyCleanup) {
-  struct MoveOnlyClosure {
-    MoveOnlyClosure(const MoveOnlyClosure&) = delete;
-    MoveOnlyClosure(MoveOnlyClosure&&) = default;
-    void operator()() { trigger(); }
-
-    std::function<void()> trigger;
-  };
-
-  MockFunction<void()> run;
-  MockFunction<void()> cleanup;
-
-  auto task = ToQueuedTask(MoveOnlyClosure{run.AsStdFunction()},
-                           MoveOnlyClosure{cleanup.AsStdFunction()});
-
-  // Expect run closure to complete before cleanup closure.
-  InSequence in_sequence;
-  EXPECT_CALL(run, Call);
-  EXPECT_CALL(cleanup, Call);
-  RunTask(std::move(task));
-}
-
-TEST(ToQueuedTaskTest, PendingTaskSafetyFlag) {
-  rtc::scoped_refptr<PendingTaskSafetyFlag> flag =
-      PendingTaskSafetyFlag::Create();
-
-  int count = 0;
-  // Create two identical tasks that increment the `count`.
-  auto task1 = ToQueuedTask(flag, [&count]() { ++count; });
-  auto task2 = ToQueuedTask(flag, [&count]() { ++count; });
-
-  EXPECT_EQ(0, count);
-  RunTask(std::move(task1));
-  EXPECT_EQ(1, count);
-  flag->SetNotAlive();
-  // Now task2 should actually not run.
-  RunTask(std::move(task2));
-  EXPECT_EQ(1, count);
-}
-
-}  // namespace
-}  // namespace webrtc
diff --git a/rtc_base/task_queue.h b/rtc_base/task_queue.h
index d0c0551..0ef0ffe 100644
--- a/rtc_base/task_queue.h
+++ b/rtc_base/task_queue.h
@@ -18,7 +18,6 @@
 
 #include "absl/functional/any_invocable.h"
 #include "absl/memory/memory.h"
-#include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/task_queue/task_queue_factory.h"
 #include "rtc_base/system/rtc_export.h"
diff --git a/rtc_base/task_utils/BUILD.gn b/rtc_base/task_utils/BUILD.gn
index fef30eb..1b3a3b5 100644
--- a/rtc_base/task_utils/BUILD.gn
+++ b/rtc_base/task_utils/BUILD.gn
@@ -26,16 +26,6 @@
   absl_deps = [ "//third_party/abseil-cpp/absl/functional:any_invocable" ]
 }
 
-rtc_library("pending_task_safety_flag") {
-  sources = [ "pending_task_safety_flag.h" ]
-  deps = [ "../../api/task_queue:pending_task_safety_flag" ]
-}
-
-rtc_source_set("to_queued_task") {
-  sources = [ "to_queued_task.h" ]
-  deps = [ "../../api/task_queue:to_queued_task" ]
-}
-
 if (rtc_include_tests) {
   rtc_library("repeating_task_unittests") {
     testonly = true
diff --git a/rtc_base/task_utils/pending_task_safety_flag.h b/rtc_base/task_utils/pending_task_safety_flag.h
deleted file mode 100644
index 8321276..0000000
--- a/rtc_base/task_utils/pending_task_safety_flag.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_
-#define RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_
-
-#include "api/task_queue/pending_task_safety_flag.h"
-
-#endif  // RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_
diff --git a/rtc_base/task_utils/to_queued_task.h b/rtc_base/task_utils/to_queued_task.h
deleted file mode 100644
index 049fb01..0000000
--- a/rtc_base/task_utils/to_queued_task.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_
-#define RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_
-
-#include "api/task_queue/to_queued_task.h"
-
-#endif  // RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_