Consolidate creation of DataChannel proxy to a single place

Change-Id: I707733f521a4fda1536741b204a559dd511d0c00
Bug: webrtc:11547
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/177344
Reviewed-by: Taylor <deadbeef@webrtc.org>
Commit-Queue: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31535}
diff --git a/pc/data_channel.cc b/pc/data_channel.cc
index 2265510..ca6b614 100644
--- a/pc/data_channel.cc
+++ b/pc/data_channel.cc
@@ -14,6 +14,7 @@
 #include <string>
 #include <utility>
 
+#include "api/proxy.h"
 #include "media/sctp/sctp_transport_internal.h"
 #include "pc/sctp_utils.h"
 #include "rtc_base/checks.h"
@@ -149,6 +150,46 @@
   return channel;
 }
 
+// Define proxy for DataChannelInterface.
+BEGIN_SIGNALING_PROXY_MAP(DataChannel)
+PROXY_SIGNALING_THREAD_DESTRUCTOR()
+PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
+PROXY_METHOD0(void, UnregisterObserver)
+BYPASS_PROXY_CONSTMETHOD0(std::string, label)
+BYPASS_PROXY_CONSTMETHOD0(bool, reliable)
+BYPASS_PROXY_CONSTMETHOD0(bool, ordered)
+BYPASS_PROXY_CONSTMETHOD0(uint16_t, maxRetransmitTime)
+BYPASS_PROXY_CONSTMETHOD0(uint16_t, maxRetransmits)
+BYPASS_PROXY_CONSTMETHOD0(absl::optional<int>, maxRetransmitsOpt)
+BYPASS_PROXY_CONSTMETHOD0(absl::optional<int>, maxPacketLifeTime)
+BYPASS_PROXY_CONSTMETHOD0(std::string, protocol)
+BYPASS_PROXY_CONSTMETHOD0(bool, negotiated)
+// Can't bypass the proxy since the id may change.
+PROXY_CONSTMETHOD0(int, id)
+BYPASS_PROXY_CONSTMETHOD0(Priority, priority)
+PROXY_CONSTMETHOD0(DataState, state)
+PROXY_CONSTMETHOD0(RTCError, error)
+PROXY_CONSTMETHOD0(uint32_t, messages_sent)
+PROXY_CONSTMETHOD0(uint64_t, bytes_sent)
+PROXY_CONSTMETHOD0(uint32_t, messages_received)
+PROXY_CONSTMETHOD0(uint64_t, bytes_received)
+PROXY_CONSTMETHOD0(uint64_t, buffered_amount)
+PROXY_METHOD0(void, Close)
+// TODO(bugs.webrtc.org/11547): Change to run on the network thread.
+PROXY_METHOD1(bool, Send, const DataBuffer&)
+END_PROXY_MAP()
+
+// static
+rtc::scoped_refptr<DataChannelInterface> DataChannel::CreateProxy(
+    rtc::scoped_refptr<DataChannel> channel) {
+  // TODO(bugs.webrtc.org/11547): incorporate the network thread in the proxy.
+  // Also, consider allowing the proxy object to own the reference (std::move).
+  // As is, the proxy has a raw pointer and no reference to the channel object
+  // and trusting that the lifetime management aligns with the
+  // sctp_data_channels_ array in DataChannelController.
+  return DataChannelProxy::Create(channel->signaling_thread_, channel.get());
+}
+
 bool DataChannel::IsSctpLike(cricket::DataChannelType type) {
   return type == cricket::DCT_SCTP || type == cricket::DCT_MEDIA_TRANSPORT ||
          type == cricket::DCT_DATA_CHANNEL_TRANSPORT ||
diff --git a/pc/data_channel.h b/pc/data_channel.h
index c1e855d..210b8ce 100644
--- a/pc/data_channel.h
+++ b/pc/data_channel.h
@@ -18,7 +18,6 @@
 
 #include "api/data_channel_interface.h"
 #include "api/priority.h"
-#include "api/proxy.h"
 #include "api/scoped_refptr.h"
 #include "media/base/media_channel.h"
 #include "pc/channel.h"
@@ -133,6 +132,11 @@
       rtc::Thread* signaling_thread,
       rtc::Thread* network_thread);
 
+  // Instantiates an API proxy for a DataChannel instance that will be handed
+  // out to external callers.
+  static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
+      rtc::scoped_refptr<DataChannel> channel);
+
   static bool IsSctpLike(cricket::DataChannelType type);
 
   void RegisterObserver(DataChannelObserver* observer) override;
@@ -338,35 +342,6 @@
   rtc::AsyncInvoker invoker_ RTC_GUARDED_BY(signaling_thread_);
 };
 
-// Define proxy for DataChannelInterface.
-BEGIN_SIGNALING_PROXY_MAP(DataChannel)
-PROXY_SIGNALING_THREAD_DESTRUCTOR()
-PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
-PROXY_METHOD0(void, UnregisterObserver)
-BYPASS_PROXY_CONSTMETHOD0(std::string, label)
-BYPASS_PROXY_CONSTMETHOD0(bool, reliable)
-BYPASS_PROXY_CONSTMETHOD0(bool, ordered)
-BYPASS_PROXY_CONSTMETHOD0(uint16_t, maxRetransmitTime)
-BYPASS_PROXY_CONSTMETHOD0(uint16_t, maxRetransmits)
-BYPASS_PROXY_CONSTMETHOD0(absl::optional<int>, maxRetransmitsOpt)
-BYPASS_PROXY_CONSTMETHOD0(absl::optional<int>, maxPacketLifeTime)
-BYPASS_PROXY_CONSTMETHOD0(std::string, protocol)
-BYPASS_PROXY_CONSTMETHOD0(bool, negotiated)
-// Can't bypass the proxy since the id may change.
-PROXY_CONSTMETHOD0(int, id)
-BYPASS_PROXY_CONSTMETHOD0(Priority, priority)
-PROXY_CONSTMETHOD0(DataState, state)
-PROXY_CONSTMETHOD0(RTCError, error)
-PROXY_CONSTMETHOD0(uint32_t, messages_sent)
-PROXY_CONSTMETHOD0(uint64_t, bytes_sent)
-PROXY_CONSTMETHOD0(uint32_t, messages_received)
-PROXY_CONSTMETHOD0(uint64_t, bytes_received)
-PROXY_CONSTMETHOD0(uint64_t, buffered_amount)
-PROXY_METHOD0(void, Close)
-// TODO(bugs.webrtc.org/11547): Change to run on the network thread.
-PROXY_METHOD1(bool, Send, const DataBuffer&)
-END_PROXY_MAP()
-
 }  // namespace webrtc
 
 #endif  // PC_DATA_CHANNEL_H_
diff --git a/pc/data_channel_controller.cc b/pc/data_channel_controller.cc
index a8a1491..710ca8e 100644
--- a/pc/data_channel_controller.cc
+++ b/pc/data_channel_controller.cc
@@ -251,9 +251,8 @@
     return;
   }
 
-  // TODO(bugs.webrtc.org/11547): Inject the network thread as well.
   rtc::scoped_refptr<DataChannelInterface> proxy_channel =
-      DataChannelProxy::Create(signaling_thread(), channel);
+      DataChannel::CreateProxy(std::move(channel));
   pc_->Observer()->OnDataChannel(std::move(proxy_channel));
   pc_->NoteDataAddedEvent();
 }
@@ -508,9 +507,8 @@
     return;
   }
   channel->SetReceiveSsrc(remote_ssrc);
-  // TODO(bugs.webrtc.org/11547): Inject the network thread as well.
   rtc::scoped_refptr<DataChannelInterface> proxy_channel =
-      DataChannelProxy::Create(signaling_thread(), channel);
+      DataChannel::CreateProxy(std::move(channel));
   pc_->Observer()->OnDataChannel(std::move(proxy_channel));
 }
 
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index 9b3b760..9e86753 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -2198,7 +2198,7 @@
   if (config) {
     internal_config.reset(new InternalDataChannelInit(*config));
   }
-  rtc::scoped_refptr<DataChannelInterface> channel(
+  rtc::scoped_refptr<DataChannel> channel(
       data_channel_controller_.InternalCreateDataChannel(
           label, internal_config.get()));
   if (!channel.get()) {
@@ -2211,8 +2211,7 @@
     UpdateNegotiationNeeded();
   }
   NoteUsageEvent(UsageEvent::DATA_ADDED);
-  // TODO(bugs.webrtc.org/11547): Inject the network thread as well.
-  return DataChannelProxy::Create(signaling_thread(), channel.get());
+  return DataChannel::CreateProxy(std::move(channel));
 }
 
 void PeerConnection::RestartIce() {