Correct stats for RTCPeerConnectionStats.dataChannels[Opened/Closed].

DataChannel.SignalOpened and unittests added.
PeerConnection.SignalDataChannelCreated added and wired up to
RTCStatsCollector.OnDataChannelCreated on RTCStatsCollector
construction.
RTCStatsCollector.OnSignalOpened/Closed added and wired up on
OnDataChannelCreated.
rtcstatscollector_unittest.cc updated, faking that channels are opened
and closed.

I did not want to use DataChannelObserver because it is used for more
than state changes and there can only be one observer (unless code is
updated). Since DataChannel already had a SignalClosed it made sense to
add a SignalOpened.

Having OnSignalBlah in RTCStatsCollector is new in this CL but will
likely be needed to correctly handle RTPMediaStreamTracks being added
and detached independently of getStats. This CL establishes this
pattern.

(An integration test will be needed for this and all the other stats to
make sure everything is wired up correctly and test outside of a
mock/fake environment, but this is not news.)

BUG=chromium:636818, chromium:627816

Review-Url: https://codereview.webrtc.org/2472113002
Cr-Commit-Position: refs/heads/master@{#15059}
diff --git a/webrtc/api/rtcstatscollector.h b/webrtc/api/rtcstatscollector.h
index ae4ba19..08bab80 100644
--- a/webrtc/api/rtcstatscollector.h
+++ b/webrtc/api/rtcstatscollector.h
@@ -13,14 +13,17 @@
 
 #include <map>
 #include <memory>
+#include <set>
 #include <vector>
 
+#include "webrtc/api/datachannel.h"
 #include "webrtc/api/datachannelinterface.h"
 #include "webrtc/api/stats/rtcstats_objects.h"
 #include "webrtc/api/stats/rtcstatsreport.h"
 #include "webrtc/base/asyncinvoker.h"
 #include "webrtc/base/refcount.h"
 #include "webrtc/base/scoped_ref_ptr.h"
+#include "webrtc/base/sigslot.h"
 #include "webrtc/base/sslidentity.h"
 #include "webrtc/base/timeutils.h"
 
@@ -49,7 +52,8 @@
 // Stats are gathered on the signaling, worker and network threads
 // asynchronously. The callback is invoked on the signaling thread. Resulting
 // reports are cached for |cache_lifetime_| ms.
-class RTCStatsCollector : public virtual rtc::RefCountInterface {
+class RTCStatsCollector : public virtual rtc::RefCountInterface,
+                          public sigslot::has_slots<> {
  public:
   static rtc::scoped_refptr<RTCStatsCollector> Create(
       PeerConnection* pc,
@@ -118,6 +122,12 @@
   std::map<std::string, CertificateStatsPair>
   PrepareTransportCertificateStats_s(const SessionStats& session_stats) const;
 
+  // Slots for signals (sigslot) that are wired up to |pc_|.
+  void OnDataChannelCreated(DataChannel* channel);
+  // Slots for signals (sigslot) that are wired up to |channel|.
+  void OnDataChannelOpened(DataChannel* channel);
+  void OnDataChannelClosed(DataChannel* channel);
+
   PeerConnection* const pc_;
   rtc::Thread* const signaling_thread_;
   rtc::Thread* const worker_thread_;
@@ -136,6 +146,25 @@
   int64_t cache_timestamp_us_;
   int64_t cache_lifetime_us_;
   rtc::scoped_refptr<const RTCStatsReport> cached_report_;
+
+  // Data recorded and maintained by the stats collector during its lifetime.
+  // Some stats are produced from this record instead of other components.
+  struct InternalRecord {
+    InternalRecord() : data_channels_opened(0),
+                       data_channels_closed(0) {}
+
+    // The opened count goes up when a channel is fully opened and the closed
+    // count goes up if a previously opened channel has fully closed. The opened
+    // count does not go down when a channel closes, meaning (opened - closed)
+    // is the number of channels currently opened. A channel that is closed
+    // before reaching the open state does not affect these counters.
+    uint32_t data_channels_opened;
+    uint32_t data_channels_closed;
+    // Identifies by address channels that have been opened, which remain in the
+    // set until they have been fully closed.
+    std::set<uintptr_t> opened_data_channels;
+  };
+  InternalRecord internal_record_;
 };
 
 const char* CandidateTypeToRTCIceCandidateTypeForTesting(