Delete MessageHandlerAutoCleanup and ThreadManager::Clear

Bug: webrtc:11988
Change-Id: Ic54d37fb802c9e55d417d65aa735fb07097d1b71
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/275768
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38102}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index efd6388..0e397df 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -927,7 +927,6 @@
     "async_resolver.h",
     "internal/default_socket_server.cc",
     "internal/default_socket_server.h",
-    "message_handler.cc",
     "message_handler.h",
     "network_monitor.cc",
     "network_monitor.h",
diff --git a/rtc_base/message_handler.cc b/rtc_base/message_handler.cc
deleted file mode 100644
index e6e973d..0000000
--- a/rtc_base/message_handler.cc
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- *  Copyright 2004 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 "rtc_base/message_handler.h"
-
-#include "rtc_base/thread.h"
-
-namespace rtc {
-
-MessageHandlerAutoCleanup::MessageHandlerAutoCleanup() {}
-
-MessageHandlerAutoCleanup::~MessageHandlerAutoCleanup() {
-  // Note that even though this clears currently pending messages for the
-  // message handler, it's still racy since it doesn't prevent threads that
-  // might be in the process of posting new messages with would-be dangling
-  // pointers.
-  // This is related to the design of Message having a raw pointer.
-  // We could consider whether it would be safer to require message handlers
-  // to be reference counted (as some are).
-  ThreadManager::Clear(this);
-}
-
-}  // namespace rtc
diff --git a/rtc_base/message_handler.h b/rtc_base/message_handler.h
index c5e05ca..8c47e0ab 100644
--- a/rtc_base/message_handler.h
+++ b/rtc_base/message_handler.h
@@ -11,9 +11,6 @@
 #ifndef RTC_BASE_MESSAGE_HANDLER_H_
 #define RTC_BASE_MESSAGE_HANDLER_H_
 
-#include <utility>
-
-#include "api/function_view.h"
 #include "rtc_base/system/rtc_export.h"
 
 namespace rtc {
@@ -27,23 +24,6 @@
   virtual void OnMessage(Message* msg) = 0;
 };
 
-// Warning: Provided for backwards compatibility.
-//
-// This class performs expensive cleanup in the dtor that will affect all
-// instances of Thread (and their pending message queues) and will block the
-// current thread as well as all other threads.
-class RTC_EXPORT MessageHandlerAutoCleanup : public MessageHandler {
- public:
-  ~MessageHandlerAutoCleanup() override;
-
-  MessageHandlerAutoCleanup(const MessageHandlerAutoCleanup&) = delete;
-  MessageHandlerAutoCleanup& operator=(const MessageHandlerAutoCleanup&) =
-      delete;
-
- protected:
-  MessageHandlerAutoCleanup();
-};
-
 }  // namespace rtc
 
 #endif  // RTC_BASE_MESSAGE_HANDLER_H_
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index da0ea4b..7525096 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -196,20 +196,6 @@
 #endif
 
 // static
-void ThreadManager::Clear(MessageHandler* handler) {
-  return Instance()->ClearInternal(handler);
-}
-void ThreadManager::ClearInternal(MessageHandler* handler) {
-  // Deleted objects may cause re-entrant calls to ClearInternal. This is
-  // allowed as the list of message queues does not change while queues are
-  // cleared.
-  MarkProcessingCritScope cs(&crit_, &processing_);
-  for (Thread* queue : message_queues_) {
-    queue->Clear(handler);
-  }
-}
-
-// static
 void ThreadManager::ProcessAllMessageQueuesForTesting() {
   return Instance()->ProcessAllMessageQueuesInternal();
 }
diff --git a/rtc_base/thread.h b/rtc_base/thread.h
index 65b9361..1519976 100644
--- a/rtc_base/thread.h
+++ b/rtc_base/thread.h
@@ -89,7 +89,6 @@
 
   static void Add(Thread* message_queue);
   static void Remove(Thread* message_queue);
-  static void Clear(MessageHandler* handler);
 
   // For testing purposes, for use with a simulated clock.
   // Ensures that all message queues have processed delayed messages
@@ -135,7 +134,6 @@
   void SetCurrentThreadInternal(Thread* thread);
   void AddInternal(Thread* message_queue);
   void RemoveInternal(Thread* message_queue);
-  void ClearInternal(MessageHandler* handler);
   void ProcessAllMessageQueuesInternal();
 #if RTC_DCHECK_IS_ON
   void RemoveFromSendGraph(Thread* thread) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
diff --git a/rtc_base/thread_unittest.cc b/rtc_base/thread_unittest.cc
index 778b89a..c0e93bc 100644
--- a/rtc_base/thread_unittest.cc
+++ b/rtc_base/thread_unittest.cc
@@ -579,37 +579,6 @@
   ThreadManager::ProcessAllMessageQueuesForTesting();
 }
 
-class RefCountedHandler : public MessageHandler, public rtc::RefCountInterface {
- public:
-  ~RefCountedHandler() override { ThreadManager::Clear(this); }
-
-  void OnMessage(Message* msg) override {}
-};
-
-class EmptyHandler : public MessageHandler {
- public:
-  ~EmptyHandler() override { ThreadManager::Clear(this); }
-
-  void OnMessage(Message* msg) override {}
-};
-
-TEST(ThreadManager, ClearReentrant) {
-  std::unique_ptr<Thread> t(Thread::Create());
-  EmptyHandler handler;
-  RefCountedHandler* inner_handler(
-      new rtc::RefCountedObject<RefCountedHandler>());
-  // When the empty handler is destroyed, it will clear messages queued for
-  // itself. The message to be cleared itself wraps a MessageHandler object
-  // (RefCountedHandler) so this will cause the message queue to be cleared
-  // again in a re-entrant fashion, which previously triggered a DCHECK.
-  // The inner handler will be removed in a re-entrant fashion from the
-  // message queue of the thread while the outer handler is removed, verifying
-  // that the iterator is not invalidated in "Thread::Clear".
-  t->Post(RTC_FROM_HERE, inner_handler, 0);
-  t->Post(RTC_FROM_HERE, &handler, 0,
-          new ScopedRefMessageData<RefCountedHandler>(inner_handler));
-}
-
 void WaitAndSetEvent(Event* wait_event, Event* set_event) {
   wait_event->Wait(Event::kForever);
   set_event->Set();