<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[211371] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/211371">211371</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2017-01-30 09:36:43 -0800 (Mon, 30 Jan 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[WebRTC] Add support for libwebrtc data channel
https://bugs.webkit.org/show_bug.cgi?id=167489

Patch by Youenn Fablet &lt;youennf@gmail.com&gt; on 2017-01-30
Reviewed by Alex Christensen.

Partially covered by webrtc/datachannel/basic.html but not yet enabled on bots.

Introducing LibWebRTCDataChannelHandler as the integration layer between WebCore (RTCDataChannel)
and libwebrtc (DataChannelObserver).

* Modules/mediastream/RTCDataChannel.cpp:
(WebCore::RTCDataChannel::create):
(WebCore::RTCDataChannel::RTCDataChannel):
(WebCore::RTCDataChannel::bufferedAmount):
(WebCore::RTCDataChannel::bufferedAmountIsDecreasing):
* Modules/mediastream/RTCDataChannel.h:
* Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.cpp: Added.
(WebCore::LibWebRTCDataChannelHandler::~LibWebRTCDataChannelHandler):
(WebCore::LibWebRTCDataChannelHandler::setClient):
(WebCore::LibWebRTCDataChannelHandler::sendStringData):
(WebCore::LibWebRTCDataChannelHandler::sendRawData):
(WebCore::LibWebRTCDataChannelHandler::close):
(WebCore::LibWebRTCDataChannelHandler::OnStateChange):
(WebCore::LibWebRTCDataChannelHandler::OnMessage):
* Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.h:
* Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp:
(WebCore::LibWebRTCMediaEndpoint::OnRemoveStream):
(WebCore::LibWebRTCMediaEndpoint::addDataChannel):
(): Deleted.
* Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp:
(WebCore::LibWebRTCPeerConnectionBackend::createDataChannelHandler):
* dom/EventNames.h:
* platform/mediastream/RTCDataChannelHandler.h:
* platform/mediastream/RTCDataChannelHandlerClient.h:
* platform/mock/RTCDataChannelHandlerMock.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesmediastreamRTCDataChannelcpp">trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediastreamRTCDataChannelh">trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.h</a></li>
<li><a href="#trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCMediaEndpointcpp">trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCMediaEndpointh">trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.h</a></li>
<li><a href="#trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCPeerConnectionBackendcpp">trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp</a></li>
<li><a href="#trunkSourceWebCoredomEventNamesh">trunk/Source/WebCore/dom/EventNames.h</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreamRTCDataChannelHandlerh">trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandler.h</a></li>
<li><a href="#trunkSourceWebCoreplatformmediastreamRTCDataChannelHandlerClienth">trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandlerClient.h</a></li>
<li><a href="#trunkSourceWebCoreplatformmockRTCDataChannelHandlerMockh">trunk/Source/WebCore/platform/mock/RTCDataChannelHandlerMock.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCDataChannelHandlercpp">trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCDataChannelHandlerh">trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/ChangeLog        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2017-01-30  Youenn Fablet  &lt;youennf@gmail.com&gt;
+
+        [WebRTC] Add support for libwebrtc data channel
+        https://bugs.webkit.org/show_bug.cgi?id=167489
+
+        Reviewed by Alex Christensen.
+
+        Partially covered by webrtc/datachannel/basic.html but not yet enabled on bots.
+
+        Introducing LibWebRTCDataChannelHandler as the integration layer between WebCore (RTCDataChannel)
+        and libwebrtc (DataChannelObserver).
+
+        * Modules/mediastream/RTCDataChannel.cpp:
+        (WebCore::RTCDataChannel::create):
+        (WebCore::RTCDataChannel::RTCDataChannel):
+        (WebCore::RTCDataChannel::bufferedAmount):
+        (WebCore::RTCDataChannel::bufferedAmountIsDecreasing):
+        * Modules/mediastream/RTCDataChannel.h:
+        * Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.cpp: Added.
+        (WebCore::LibWebRTCDataChannelHandler::~LibWebRTCDataChannelHandler):
+        (WebCore::LibWebRTCDataChannelHandler::setClient):
+        (WebCore::LibWebRTCDataChannelHandler::sendStringData):
+        (WebCore::LibWebRTCDataChannelHandler::sendRawData):
+        (WebCore::LibWebRTCDataChannelHandler::close):
+        (WebCore::LibWebRTCDataChannelHandler::OnStateChange):
+        (WebCore::LibWebRTCDataChannelHandler::OnMessage):
+        * Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.h:
+        * Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp:
+        (WebCore::LibWebRTCMediaEndpoint::OnRemoveStream):
+        (WebCore::LibWebRTCMediaEndpoint::addDataChannel):
+        (): Deleted.
+        * Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp:
+        (WebCore::LibWebRTCPeerConnectionBackend::createDataChannelHandler):
+        * dom/EventNames.h:
+        * platform/mediastream/RTCDataChannelHandler.h:
+        * platform/mediastream/RTCDataChannelHandlerClient.h:
+        * platform/mock/RTCDataChannelHandlerMock.h:
+
</ins><span class="cx"> 2017-01-30  Carlos Garcia Campos  &lt;cgarcia@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Several web timing tests crash in GTK+ and AppleWin bots
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamRTCDataChannelcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -55,7 +55,9 @@
</span><span class="cx"> Ref&lt;RTCDataChannel&gt; RTCDataChannel::create(ScriptExecutionContext&amp; context, std::unique_ptr&lt;RTCDataChannelHandler&gt;&amp;&amp; handler, String&amp;&amp; label, RTCDataChannelInit&amp;&amp; options)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(handler);
</span><del>-    return adoptRef(*new RTCDataChannel(context, WTFMove(handler), WTFMove(label), WTFMove(options)));
</del><ins>+    auto channel = adoptRef(*new RTCDataChannel(context, WTFMove(handler), WTFMove(label), WTFMove(options)));
+    channel-&gt;m_handler-&gt;setClient(&amp;channel.get());
+    return channel;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> RTCDataChannel::RTCDataChannel(ScriptExecutionContext&amp; context, std::unique_ptr&lt;RTCDataChannelHandler&gt;&amp;&amp; handler, String&amp;&amp; label, RTCDataChannelInit&amp;&amp; options)
</span><span class="lines">@@ -65,7 +67,6 @@
</span><span class="cx">     , m_label(WTFMove(label))
</span><span class="cx">     , m_options(WTFMove(options))
</span><span class="cx"> {
</span><del>-    m_handler-&gt;setClient(this);
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> const AtomicString&amp; RTCDataChannel::readyState() const
</span><span class="lines">@@ -90,9 +91,9 @@
</span><span class="cx">     return emptyAtom;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-unsigned RTCDataChannel::bufferedAmount() const
</del><ins>+size_t RTCDataChannel::bufferedAmount() const
</ins><span class="cx"> {
</span><del>-    return 0;
</del><ins>+    return m_handler-&gt;bufferedAmount();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> const AtomicString&amp; RTCDataChannel::binaryType() const
</span><span class="lines">@@ -222,6 +223,15 @@
</span><span class="cx">     scheduleDispatchEvent(Event::create(eventNames().errorEvent, false, false));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void RTCDataChannel::bufferedAmountIsDecreasing()
+{
+    if (m_stopped)
+        return;
+
+    if (bufferedAmount() &lt;= m_bufferedAmountLowThreshold)
+        scheduleDispatchEvent(Event::create(eventNames().bufferedAmountLowThresholdEvent, false, false));
+}
+
</ins><span class="cx"> void RTCDataChannel::stop()
</span><span class="cx"> {
</span><span class="cx">     m_stopped = true;
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamRTCDataChannelh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.h (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.h        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/Modules/mediastream/RTCDataChannel.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -44,7 +44,7 @@
</span><span class="cx"> class Blob;
</span><span class="cx"> class RTCPeerConnectionHandler;
</span><span class="cx"> 
</span><del>-class RTCDataChannel final : public RefCounted&lt;RTCDataChannel&gt;, public EventTargetWithInlineData, public RTCDataChannelHandlerClient {
</del><ins>+class RTCDataChannel final : public RTCDataChannelHandlerClient, public EventTargetWithInlineData {
</ins><span class="cx"> public:
</span><span class="cx">     static Ref&lt;RTCDataChannel&gt; create(ScriptExecutionContext&amp;, std::unique_ptr&lt;RTCDataChannelHandler&gt;&amp;&amp;, String&amp;&amp;, RTCDataChannelInit&amp;&amp;);
</span><span class="cx"> 
</span><span class="lines">@@ -57,7 +57,9 @@
</span><span class="cx"> 
</span><span class="cx">     String label() const { return m_label; }
</span><span class="cx">     const AtomicString&amp; readyState() const;
</span><del>-    unsigned bufferedAmount() const;
</del><ins>+    size_t bufferedAmount() const;
+    size_t bufferedAmountLowThreshold() const { return m_bufferedAmountLowThreshold; }
+    void setBufferedAmountLowThreshold(size_t value) { m_bufferedAmountLowThreshold = value; }
</ins><span class="cx"> 
</span><span class="cx">     const AtomicString&amp; binaryType() const;
</span><span class="cx">     ExceptionOr&lt;void&gt; setBinaryType(const AtomicString&amp;);
</span><span class="lines">@@ -71,8 +73,8 @@
</span><span class="cx"> 
</span><span class="cx">     void stop();
</span><span class="cx"> 
</span><del>-    using RefCounted::ref;
-    using RefCounted::deref;
</del><ins>+    using RTCDataChannelHandlerClient::ref;
+    using RTCDataChannelHandlerClient::deref;
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     RTCDataChannel(ScriptExecutionContext&amp;, std::unique_ptr&lt;RTCDataChannelHandler&gt;&amp;&amp;, String&amp;&amp;, RTCDataChannelInit&amp;&amp;);
</span><span class="lines">@@ -88,12 +90,12 @@
</span><span class="cx"> 
</span><span class="cx">     ScriptExecutionContext* m_scriptExecutionContext;
</span><span class="cx"> 
</span><ins>+    // RTCDataChannelHandlerClient API
</ins><span class="cx">     void didChangeReadyState(ReadyState) final;
</span><span class="cx">     void didReceiveStringData(const String&amp;) final;
</span><span class="cx">     void didReceiveRawData(const char*, size_t) final;
</span><span class="cx">     void didDetectError() final;
</span><del>-    void protect() final { ref(); }
-    void unprotect() final { deref(); }
</del><ins>+    void bufferedAmountIsDecreasing() final;
</ins><span class="cx"> 
</span><span class="cx">     std::unique_ptr&lt;RTCDataChannelHandler&gt; m_handler;
</span><span class="cx"> 
</span><span class="lines">@@ -109,6 +111,7 @@
</span><span class="cx"> 
</span><span class="cx">     String m_label;
</span><span class="cx">     RTCDataChannelInit m_options;
</span><ins>+    size_t m_bufferedAmountLowThreshold { 0 };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCDataChannelHandlercpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.cpp (0 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.cpp                                (rev 0)
+++ trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.cpp        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -0,0 +1,114 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;LibWebRTCDataChannelHandler.h&quot;
+
+#if USE(LIBWEBRTC)
+
+#include &quot;RTCDataChannel.h&quot;
+#include &lt;wtf/MainThread.h&gt;
+
+namespace WebCore {
+
+LibWebRTCDataChannelHandler::~LibWebRTCDataChannelHandler()
+{
+    if (m_client)
+        m_channel-&gt;UnregisterObserver();
+}
+
+void LibWebRTCDataChannelHandler::setClient(RTCDataChannelHandlerClient* client)
+{
+    m_client = client;
+    if (m_client)
+        m_channel-&gt;RegisterObserver(this);
+    else
+        m_channel-&gt;UnregisterObserver();
+}
+
+bool LibWebRTCDataChannelHandler::sendStringData(const String&amp; text)
+{
+    return m_channel-&gt;Send({rtc::CopyOnWriteBuffer(text.utf8().data(), text.length()), false});
+}
+
+bool LibWebRTCDataChannelHandler::sendRawData(const char* data, size_t length)
+{
+    return m_channel-&gt;Send({rtc::CopyOnWriteBuffer(data, length), true});
+}
+
+void LibWebRTCDataChannelHandler::close()
+{
+    m_channel-&gt;Close();
+}
+
+void LibWebRTCDataChannelHandler::OnStateChange()
+{
+    RTCDataChannel::ReadyState state;
+    switch (m_channel-&gt;state()) {
+    case webrtc::DataChannelInterface::kConnecting:
+        state = RTCDataChannel::ReadyStateConnecting;
+        break;
+    case webrtc::DataChannelInterface::kOpen:
+        state = RTCDataChannel::ReadyStateOpen;
+        break;
+    case webrtc::DataChannelInterface::kClosing:
+        state = RTCDataChannel::ReadyStateClosing;
+        break;
+    case webrtc::DataChannelInterface::kClosed:
+        state = RTCDataChannel::ReadyStateClosed;
+        break;
+    }
+    ASSERT(m_client);
+    callOnMainThread([protectedClient = makeRef(*m_client), state] {
+        protectedClient-&gt;didChangeReadyState(state);
+    });
+}
+
+void LibWebRTCDataChannelHandler::OnMessage(const webrtc::DataBuffer&amp; buffer)
+{
+    ASSERT(m_client);
+    std::unique_ptr&lt;webrtc::DataBuffer&gt; protectedBuffer(new webrtc::DataBuffer(buffer));
+    callOnMainThread([protectedClient = makeRef(*m_client), buffer = WTFMove(protectedBuffer)] {
+        // FIXME: Ensure this is correct by adding some tests with non-ASCII characters.
+        const char* data = reinterpret_cast&lt;const char*&gt;(buffer-&gt;data.data());
+        if (buffer-&gt;binary)
+            protectedClient-&gt;didReceiveRawData(data, buffer-&gt;size());
+        else
+            protectedClient-&gt;didReceiveStringData(String(data, buffer-&gt;size()));
+    });
+}
+
+void LibWebRTCDataChannelHandler::OnBufferedAmountChange(uint64_t previousAmount)
+{
+    if (previousAmount &lt;= m_channel-&gt;buffered_amount())
+        return;
+    ASSERT(m_client);
+    callOnMainThread([protectedClient = makeRef(*m_client)] {
+        protectedClient-&gt;bufferedAmountIsDecreasing();
+    });
+}
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCDataChannelHandlerhfromrev211370trunkSourceWebCoreplatformmediastreamRTCDataChannelHandlerh"></a>
<div class="copfile"><h4>Copied: trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.h (from rev 211370, trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandler.h) (0 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.h                                (rev 0)
+++ trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCDataChannelHandler.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -0,0 +1,61 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if USE(LIBWEBRTC)
+
+#include &quot;LibWebRTCMacros.h&quot;
+#include &quot;RTCDataChannelHandler.h&quot;
+#include &lt;webrtc/api/datachannelinterface.h&gt;
+
+namespace WebCore {
+
+class RTCDataChannelHandlerClient;
+
+class LibWebRTCDataChannelHandler final : public RTCDataChannelHandler, private webrtc::DataChannelObserver {
+public:
+    explicit LibWebRTCDataChannelHandler(rtc::scoped_refptr&lt;webrtc::DataChannelInterface&gt;&amp;&amp; channel) : m_channel(WTFMove(channel)) { ASSERT(m_channel); }
+    ~LibWebRTCDataChannelHandler();
+
+private:
+    // RTCDataChannelHandler API
+    void setClient(RTCDataChannelHandlerClient*) final;
+    bool sendStringData(const String&amp;) final;
+    bool sendRawData(const char*, size_t) final;
+    void close() final;
+    size_t bufferedAmount() const final { return m_channel-&gt;buffered_amount(); }
+
+    // webrtc::DataChannelObserver API
+    void OnStateChange();
+    void OnMessage(const webrtc::DataBuffer&amp;);
+    void OnBufferedAmountChange(uint64_t);
+
+    rtc::scoped_refptr&lt;webrtc::DataChannelInterface&gt; m_channel;
+    RTCDataChannelHandlerClient* m_client { nullptr };
+};
+
+} // namespace WebCore
+
+#endif // USE(LIBWEBRTC)
</ins></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCMediaEndpointcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> #if USE(LIBWEBRTC)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;EventNames.h&quot;
</span><ins>+#include &quot;LibWebRTCDataChannelHandler.h&quot;
</ins><span class="cx"> #include &quot;LibWebRTCPeerConnectionBackend.h&quot;
</span><span class="cx"> #include &quot;LibWebRTCProvider.h&quot;
</span><span class="cx"> #include &quot;LibWebRTCUtils.h&quot;
</span><span class="lines">@@ -285,9 +286,52 @@
</span><span class="cx"> 
</span><span class="cx"> void LibWebRTCMediaEndpoint::OnRemoveStream(rtc::scoped_refptr&lt;webrtc::MediaStreamInterface&gt;)
</span><span class="cx"> {
</span><del>-    ASSERT_NOT_REACHED();
</del><ins>+    notImplemented();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+std::unique_ptr&lt;RTCDataChannelHandler&gt; LibWebRTCMediaEndpoint::createDataChannel(const String&amp; label, const RTCDataChannelInit&amp; options)
+{
+    webrtc::DataChannelInit init;
+    init.ordered = options.ordered;
+    init.maxRetransmitTime = options.maxRetransmitTime;
+    init.maxRetransmits = options.maxRetransmits;
+    init.protocol = options.protocol.utf8().data();
+    init.negotiated = options.negotiated;
+    init.id = options.id;
+
+    return std::make_unique&lt;LibWebRTCDataChannelHandler&gt;(m_backend-&gt;CreateDataChannel(label.utf8().data(), &amp;init));
+}
+
+void LibWebRTCMediaEndpoint::addDataChannel(rtc::scoped_refptr&lt;webrtc::DataChannelInterface&gt;&amp;&amp; dataChannel)
+{
+    auto protocol = dataChannel-&gt;protocol();
+    auto label = dataChannel-&gt;label();
+
+    RTCDataChannelInit init;
+    init.ordered = dataChannel-&gt;ordered();
+    init.maxRetransmitTime = dataChannel-&gt;maxRetransmitTime();
+    init.maxRetransmits = dataChannel-&gt;maxRetransmits();
+    init.protocol = String(protocol.data(), protocol.size());
+    init.negotiated = dataChannel-&gt;negotiated();
+    init.id = dataChannel-&gt;id();
+
+    bool isOpened = dataChannel-&gt;state() == webrtc::DataChannelInterface::kOpen;
+
+    auto handler =  std::make_unique&lt;LibWebRTCDataChannelHandler&gt;(WTFMove(dataChannel));
+    ASSERT(m_peerConnectionBackend.connection().scriptExecutionContext());
+    auto channel = RTCDataChannel::create(*m_peerConnectionBackend.connection().scriptExecutionContext(), WTFMove(handler), String(label.data(), label.size()), WTFMove(init));
+
+    if (isOpened) {
+        callOnMainThread([channel = channel.copyRef()] {
+            // FIXME: We should be able to write channel-&gt;didChangeReadyState(...)
+            RTCDataChannelHandlerClient&amp; client = channel.get();
+            client.didChangeReadyState(RTCDataChannel::ReadyStateOpen);
+        });
+    }
+
+    m_peerConnectionBackend.connection().fireEvent(RTCDataChannelEvent::create(eventNames().datachannelEvent, false, false, WTFMove(channel)));
+}
+
</ins><span class="cx"> void LibWebRTCMediaEndpoint::OnDataChannel(rtc::scoped_refptr&lt;webrtc::DataChannelInterface&gt; dataChannel)
</span><span class="cx"> {
</span><span class="cx">     callOnMainThread([protectedThis = makeRef(*this), dataChannel = WTFMove(dataChannel)] {
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCMediaEndpointh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.h (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.h        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -66,6 +66,7 @@
</span><span class="cx">     void doCreateOffer();
</span><span class="cx">     void doCreateAnswer();
</span><span class="cx">     void getStats(MediaStreamTrack*, PeerConnection::StatsPromise&amp;&amp;);
</span><ins>+    std::unique_ptr&lt;RTCDataChannelHandler&gt; createDataChannel(const String&amp;, const RTCDataChannelInit&amp;);
</ins><span class="cx"> 
</span><span class="cx">     void storeIceCandidate(std::unique_ptr&lt;webrtc::IceCandidateInterface&gt;&amp;&amp; candidate) { m_pendingCandidates.append(WTFMove(candidate)); }
</span><span class="cx">     void addPendingIceCandidates();
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediastreamlibwebrtcLibWebRTCPeerConnectionBackendcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;Document.h&quot;
</span><span class="cx"> #include &quot;IceCandidate.h&quot;
</span><ins>+#include &quot;LibWebRTCDataChannelHandler.h&quot;
</ins><span class="cx"> #include &quot;LibWebRTCMediaEndpoint.h&quot;
</span><span class="cx"> #include &quot;MediaEndpointConfiguration.h&quot;
</span><span class="cx"> #include &quot;Page.h&quot;
</span><span class="lines">@@ -179,10 +180,7 @@
</span><span class="cx"> 
</span><span class="cx"> std::unique_ptr&lt;RTCDataChannelHandler&gt; LibWebRTCPeerConnectionBackend::createDataChannelHandler(const String&amp; label, const RTCDataChannelInit&amp; options)
</span><span class="cx"> {
</span><del>-    UNUSED_PARAM(label);
-    UNUSED_PARAM(options);
-    ASSERT_NOT_REACHED();
-    return nullptr;
</del><ins>+    return m_endpoint-&gt;createDataChannel(label, options);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCoredomEventNamesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/EventNames.h (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/EventNames.h        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/dom/EventNames.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -67,6 +67,7 @@
</span><span class="cx">     macro(blocked) \
</span><span class="cx">     macro(blur) \
</span><span class="cx">     macro(boundary) \
</span><ins>+    macro(bufferedAmountLowThreshold) \
</ins><span class="cx">     macro(cached) \
</span><span class="cx">     macro(cancel) \
</span><span class="cx">     macro(canplay) \
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreamRTCDataChannelHandlerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandler.h (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandler.h        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandler.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -50,6 +50,8 @@
</span><span class="cx">     virtual bool sendStringData(const String&amp;) = 0;
</span><span class="cx">     virtual bool sendRawData(const char*, size_t) = 0;
</span><span class="cx">     virtual void close() = 0;
</span><ins>+
+    virtual size_t bufferedAmount() const = 0;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformmediastreamRTCDataChannelHandlerClienth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandlerClient.h (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandlerClient.h        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/platform/mediastream/RTCDataChannelHandlerClient.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -22,16 +22,16 @@
</span><span class="cx">  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</span><span class="cx">  */
</span><span class="cx"> 
</span><del>-#ifndef RTCDataChannelHandlerClient_h
-#define RTCDataChannelHandlerClient_h
</del><ins>+#pragma once
</ins><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEB_RTC)
</span><span class="cx"> 
</span><ins>+#include &lt;wtf/ThreadSafeRefCounted.h&gt;
</ins><span class="cx"> #include &lt;wtf/text/WTFString.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-class RTCDataChannelHandlerClient {
</del><ins>+class RTCDataChannelHandlerClient : public ThreadSafeRefCounted&lt;RTCDataChannelHandlerClient&gt; {
</ins><span class="cx"> public:
</span><span class="cx">     enum ReadyState {
</span><span class="cx">         ReadyStateConnecting = 0,
</span><span class="lines">@@ -46,13 +46,9 @@
</span><span class="cx">     virtual void didReceiveStringData(const String&amp;) = 0;
</span><span class="cx">     virtual void didReceiveRawData(const char*, size_t) = 0;
</span><span class="cx">     virtual void didDetectError() = 0;
</span><del>-
-    virtual void protect() = 0;
-    virtual void unprotect() = 0;
</del><ins>+    virtual void bufferedAmountIsDecreasing() = 0;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(WEB_RTC)
</span><del>-
-#endif // RTCDataChannelHandlerClient_h
</del></span></pre></div>
<a id="trunkSourceWebCoreplatformmockRTCDataChannelHandlerMockh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/mock/RTCDataChannelHandlerMock.h (211370 => 211371)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/mock/RTCDataChannelHandlerMock.h        2017-01-30 17:31:47 UTC (rev 211370)
+++ trunk/Source/WebCore/platform/mock/RTCDataChannelHandlerMock.h        2017-01-30 17:36:43 UTC (rev 211371)
</span><span class="lines">@@ -42,6 +42,7 @@
</span><span class="cx">     bool sendStringData(const String&amp;) final;
</span><span class="cx">     bool sendRawData(const char*, size_t) final;
</span><span class="cx">     void close() final;
</span><ins>+    size_t bufferedAmount() const final { return 0; }
</ins><span class="cx"> 
</span><span class="cx">     RTCDataChannelHandlerClient* m_client;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>