1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#pragma once
27
28#if ENABLE(WEB_RTC)
29
30#include "ActiveDOMObject.h"
31#include "Event.h"
32#include "EventTarget.h"
33#include "ExceptionOr.h"
34#include "RTCDataChannelHandler.h"
35#include "RTCDataChannelHandlerClient.h"
36#include "ScriptWrappable.h"
37#include "Timer.h"
38
39namespace JSC {
40 class ArrayBuffer;
41 class ArrayBufferView;
42}
43
44namespace WebCore {
45
46class Blob;
47class RTCPeerConnectionHandler;
48
49class RTCDataChannel final : public ActiveDOMObject, public RTCDataChannelHandlerClient, public EventTargetWithInlineData {
50 WTF_MAKE_ISO_ALLOCATED(RTCDataChannel);
51public:
52 static Ref<RTCDataChannel> create(ScriptExecutionContext&, std::unique_ptr<RTCDataChannelHandler>&&, String&&, RTCDataChannelInit&&);
53
54 bool ordered() const { return *m_options.ordered; }
55 Optional<unsigned short> maxPacketLifeTime() const { return m_options.maxPacketLifeTime; }
56 Optional<unsigned short> maxRetransmits() const { return m_options.maxRetransmits; }
57 String protocol() const { return m_options.protocol; }
58 bool negotiated() const { return *m_options.negotiated; };
59 Optional<unsigned short> id() const { return m_options.id; };
60
61 String label() const { return m_label; }
62 RTCDataChannelState readyState() const {return m_readyState; }
63 size_t bufferedAmount() const;
64 size_t bufferedAmountLowThreshold() const { return m_bufferedAmountLowThreshold; }
65 void setBufferedAmountLowThreshold(size_t value) { m_bufferedAmountLowThreshold = value; }
66
67 const AtomString& binaryType() const;
68 ExceptionOr<void> setBinaryType(const AtomString&);
69
70 ExceptionOr<void> send(const String&);
71 ExceptionOr<void> send(JSC::ArrayBuffer&);
72 ExceptionOr<void> send(JSC::ArrayBufferView&);
73 ExceptionOr<void> send(Blob&);
74
75 void close();
76
77 using RTCDataChannelHandlerClient::ref;
78 using RTCDataChannelHandlerClient::deref;
79
80private:
81 RTCDataChannel(ScriptExecutionContext&, std::unique_ptr<RTCDataChannelHandler>&&, String&&, RTCDataChannelInit&&);
82
83 void scheduleDispatchEvent(Ref<Event>&&);
84 void scheduledEventTimerFired();
85
86 EventTargetInterface eventTargetInterface() const final { return RTCDataChannelEventTargetInterfaceType; }
87 ScriptExecutionContext* scriptExecutionContext() const final { return m_scriptExecutionContext; }
88
89 void refEventTarget() final { ref(); }
90 void derefEventTarget() final { deref(); }
91
92 ExceptionOr<void> sendRawData(const char* data, size_t length);
93
94 // ActiveDOMObject API
95 void stop() final;
96 const char* activeDOMObjectName() const final { return "RTCDataChannel"; }
97 bool canSuspendForDocumentSuspension() const final { return m_readyState == RTCDataChannelState::Closed; }
98
99 // RTCDataChannelHandlerClient API
100 void didChangeReadyState(RTCDataChannelState) final;
101 void didReceiveStringData(const String&) final;
102 void didReceiveRawData(const char*, size_t) final;
103 void didDetectError() final;
104 void bufferedAmountIsDecreasing(size_t) final;
105
106 std::unique_ptr<RTCDataChannelHandler> m_handler;
107
108 // FIXME: m_stopped is probably redundant with m_readyState.
109 bool m_stopped { false };
110 RTCDataChannelState m_readyState { RTCDataChannelState::Connecting };
111
112 enum class BinaryType { Blob, ArrayBuffer };
113 BinaryType m_binaryType { BinaryType::ArrayBuffer };
114
115 Timer m_scheduledEventTimer;
116 Vector<Ref<Event>> m_scheduledEvents;
117
118 String m_label;
119 RTCDataChannelInit m_options;
120 size_t m_bufferedAmountLowThreshold { 0 };
121};
122
123} // namespace WebCore
124
125#endif // ENABLE(WEB_RTC)
126