2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "ActiveDOMObject.h"
32 #include "EventTarget.h"
33 #include "ExceptionOr.h"
34 #include "NetworkSendQueue.h"
35 #include "RTCDataChannelHandler.h"
36 #include "RTCDataChannelHandlerClient.h"
37 #include "ScriptWrappable.h"
42 class ArrayBufferView;
48 class RTCPeerConnectionHandler;
50 class RTCDataChannel final : public ActiveDOMObject, public RTCDataChannelHandlerClient, public EventTargetWithInlineData {
51 WTF_MAKE_ISO_ALLOCATED(RTCDataChannel);
53 static Ref<RTCDataChannel> create(Document&, std::unique_ptr<RTCDataChannelHandler>&&, String&&, RTCDataChannelInit&&);
55 bool ordered() const { return *m_options.ordered; }
56 Optional<unsigned short> maxPacketLifeTime() const { return m_options.maxPacketLifeTime; }
57 Optional<unsigned short> maxRetransmits() const { return m_options.maxRetransmits; }
58 String protocol() const { return m_options.protocol; }
59 bool negotiated() const { return *m_options.negotiated; };
60 Optional<unsigned short> id() const { return m_options.id; };
62 String label() const { return m_label; }
63 RTCDataChannelState readyState() const {return m_readyState; }
64 size_t bufferedAmount() const;
65 size_t bufferedAmountLowThreshold() const { return m_bufferedAmountLowThreshold; }
66 void setBufferedAmountLowThreshold(size_t value) { m_bufferedAmountLowThreshold = value; }
68 const AtomString& binaryType() const;
69 ExceptionOr<void> setBinaryType(const AtomString&);
71 ExceptionOr<void> send(const String&);
72 ExceptionOr<void> send(JSC::ArrayBuffer&);
73 ExceptionOr<void> send(JSC::ArrayBufferView&);
74 ExceptionOr<void> send(Blob&);
78 using RTCDataChannelHandlerClient::ref;
79 using RTCDataChannelHandlerClient::deref;
82 RTCDataChannel(Document&, std::unique_ptr<RTCDataChannelHandler>&&, String&&, RTCDataChannelInit&&);
84 static NetworkSendQueue createMessageQueue(Document&, RTCDataChannel&);
86 void scheduleDispatchEvent(Ref<Event>&&);
88 EventTargetInterface eventTargetInterface() const final { return RTCDataChannelEventTargetInterfaceType; }
89 ScriptExecutionContext* scriptExecutionContext() const final { return m_scriptExecutionContext; }
91 void refEventTarget() final { ref(); }
92 void derefEventTarget() final { deref(); }
94 // ActiveDOMObject API
96 const char* activeDOMObjectName() const final { return "RTCDataChannel"; }
98 // FIXME: This should never prevent entering the back/forward cache.
99 bool shouldPreventEnteringBackForwardCache_DEPRECATED() const final { return m_readyState == RTCDataChannelState::Open; }
101 // RTCDataChannelHandlerClient API
102 void didChangeReadyState(RTCDataChannelState) final;
103 void didReceiveStringData(const String&) final;
104 void didReceiveRawData(const char*, size_t) final;
105 void didDetectError() final;
106 void bufferedAmountIsDecreasing(size_t) final;
108 std::unique_ptr<RTCDataChannelHandler> m_handler;
110 // FIXME: m_stopped is probably redundant with m_readyState.
111 bool m_stopped { false };
112 RTCDataChannelState m_readyState { RTCDataChannelState::Connecting };
114 enum class BinaryType { Blob, ArrayBuffer };
115 BinaryType m_binaryType { BinaryType::ArrayBuffer };
118 RTCDataChannelInit m_options;
119 size_t m_bufferedAmountLowThreshold { 0 };
121 NetworkSendQueue m_messageQueue;
124 } // namespace WebCore
126 #endif // ENABLE(WEB_RTC)