Update rtc_base to not use implicit T* --> scoped_refptr<T> conversion

Also updated the OperationsChain and CallbackHandle classes to not use
any virtual methods.

Bug: webrtc:13464
Change-Id: I3437d1b7b043339e66411f5a46c226624b7ff9a5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/246102
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35682}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index e9d1bdb..5513830 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -499,6 +499,7 @@
     ":checks",
     ":macromagic",
     ":refcount",
+    "../api:refcountedbase",
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "system:no_unique_address",
diff --git a/rtc_base/operations_chain.cc b/rtc_base/operations_chain.cc
index 59d30d3..f42482b 100644
--- a/rtc_base/operations_chain.cc
+++ b/rtc_base/operations_chain.cc
@@ -37,10 +37,11 @@
 
 // static
 scoped_refptr<OperationsChain> OperationsChain::Create() {
-  return new OperationsChain();
+  // Explicit new, to access private constructor.
+  return rtc::scoped_refptr<OperationsChain>(new OperationsChain());
 }
 
-OperationsChain::OperationsChain() : RefCountedObject() {
+OperationsChain::OperationsChain() {
   RTC_DCHECK_RUN_ON(&sequence_checker_);
 }
 
@@ -63,8 +64,10 @@
 }
 
 std::function<void()> OperationsChain::CreateOperationsChainCallback() {
-  return [handle = rtc::scoped_refptr<CallbackHandle>(
-              new CallbackHandle(this))]() { handle->OnOperationComplete(); };
+  return [handle = rtc::make_ref_counted<CallbackHandle>(
+              rtc::scoped_refptr<OperationsChain>(this))]() {
+    handle->OnOperationComplete();
+  };
 }
 
 void OperationsChain::OnOperationComplete() {
diff --git a/rtc_base/operations_chain.h b/rtc_base/operations_chain.h
index 7823f6e..e6fb4f9 100644
--- a/rtc_base/operations_chain.h
+++ b/rtc_base/operations_chain.h
@@ -19,6 +19,7 @@
 #include <utility>
 
 #include "absl/types/optional.h"
+#include "api/ref_counted_base.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
 #include "rtc_base/checks.h"
@@ -113,7 +114,7 @@
 // The OperationsChain is kept-alive through reference counting if there are
 // operations pending. This, together with the contract, guarantees that all
 // operations that are chained get executed.
-class OperationsChain final : public RefCountedObject<RefCountInterface> {
+class OperationsChain final : public RefCountedNonVirtual<OperationsChain> {
  public:
   static scoped_refptr<OperationsChain> Create();
   ~OperationsChain();
@@ -163,7 +164,7 @@
   // std::function<void()>, which is a copyable type. To allow the callback to
   // be copyable, it is backed up by this reference counted handle. See
   // CreateOperationsChainCallback().
-  class CallbackHandle final : public RefCountedObject<RefCountInterface> {
+  class CallbackHandle final : public RefCountedNonVirtual<CallbackHandle> {
    public:
     explicit CallbackHandle(scoped_refptr<OperationsChain> operations_chain);
     ~CallbackHandle();
diff --git a/rtc_base/ref_counted_object.h b/rtc_base/ref_counted_object.h
index 37612ad..274eb9e 100644
--- a/rtc_base/ref_counted_object.h
+++ b/rtc_base/ref_counted_object.h
@@ -148,7 +148,7 @@
           typename std::enable_if<std::is_convertible_v<T*, RefCountInterface*>,
                                   T>::type* = nullptr>
 scoped_refptr<T> make_ref_counted(Args&&... args) {
-  return new RefCountedObject<T>(std::forward<Args>(args)...);
+  return scoped_refptr<T>(new RefCountedObject<T>(std::forward<Args>(args)...));
 }
 
 // `make_ref_counted` for complete classes that are not convertible to
@@ -175,7 +175,8 @@
 
         T>::type* = nullptr>
 scoped_refptr<FinalRefCountedObject<T>> make_ref_counted(Args&&... args) {
-  return new FinalRefCountedObject<T>(std::forward<Args>(args)...);
+  return scoped_refptr<FinalRefCountedObject<T>>(
+      new FinalRefCountedObject<T>(std::forward<Args>(args)...));
 }
 
 // `Ref<>`, `Ref<>::Type` and `Ref<>::Ptr`:
diff --git a/rtc_base/rtc_certificate.cc b/rtc_base/rtc_certificate.cc
index 496b4ac..e9137f4 100644
--- a/rtc_base/rtc_certificate.cc
+++ b/rtc_base/rtc_certificate.cc
@@ -13,6 +13,7 @@
 #include <memory>
 
 #include "rtc_base/checks.h"
+#include "rtc_base/ref_counted_object.h"
 #include "rtc_base/ssl_certificate.h"
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/time_utils.h"
@@ -21,7 +22,9 @@
 
 scoped_refptr<RTCCertificate> RTCCertificate::Create(
     std::unique_ptr<SSLIdentity> identity) {
-  return new RTCCertificate(identity.release());
+  // Explicit new to access proteced constructor.
+  return rtc::scoped_refptr<RTCCertificate>(
+      new RTCCertificate(identity.release()));
 }
 
 RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) {
@@ -61,7 +64,7 @@
       SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate()));
   if (!identity)
     return nullptr;
-  return new RTCCertificate(identity.release());
+  return RTCCertificate::Create(std::move(identity));
 }
 
 bool RTCCertificate::operator==(const RTCCertificate& certificate) const {
diff --git a/rtc_base/task_utils/pending_task_safety_flag.cc b/rtc_base/task_utils/pending_task_safety_flag.cc
index 57b3f6c..8bff213 100644
--- a/rtc_base/task_utils/pending_task_safety_flag.cc
+++ b/rtc_base/task_utils/pending_task_safety_flag.cc
@@ -13,22 +13,28 @@
 namespace webrtc {
 
 // static
+rtc::scoped_refptr<PendingTaskSafetyFlag> PendingTaskSafetyFlag::CreateInternal(
+    bool alive) {
+  // Explicit new, to access private constructor.
+  return rtc::scoped_refptr<PendingTaskSafetyFlag>(
+      new PendingTaskSafetyFlag(alive));
+}
+
+// static
 rtc::scoped_refptr<PendingTaskSafetyFlag> PendingTaskSafetyFlag::Create() {
-  return new PendingTaskSafetyFlag(true);
+  return CreateInternal(true);
 }
 
 rtc::scoped_refptr<PendingTaskSafetyFlag>
 PendingTaskSafetyFlag::CreateDetached() {
-  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag(
-      new PendingTaskSafetyFlag(true));
+  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag = CreateInternal(true);
   safety_flag->main_sequence_.Detach();
   return safety_flag;
 }
 
 rtc::scoped_refptr<PendingTaskSafetyFlag>
 PendingTaskSafetyFlag::CreateDetachedInactive() {
-  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag(
-      new PendingTaskSafetyFlag(false));
+  rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag = CreateInternal(false);
   safety_flag->main_sequence_.Detach();
   return safety_flag;
 }
diff --git a/rtc_base/task_utils/pending_task_safety_flag.h b/rtc_base/task_utils/pending_task_safety_flag.h
index 6446bfe..58772bc 100644
--- a/rtc_base/task_utils/pending_task_safety_flag.h
+++ b/rtc_base/task_utils/pending_task_safety_flag.h
@@ -93,6 +93,8 @@
   explicit PendingTaskSafetyFlag(bool alive) : alive_(alive) {}
 
  private:
+  static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateInternal(bool alive);
+
   bool alive_ = true;
   RTC_NO_UNIQUE_ADDRESS SequenceChecker main_sequence_;
 };