1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#ifndef RTCDataChannel_h
26#define RTCDataChannel_h
27
28#if ENABLE(MEDIA_STREAM)
29
30#include "EventTarget.h"
31#include "RTCDataChannelHandlerClient.h"
32#include "ScriptWrappable.h"
33#include "Timer.h"
34#include <wtf/RefCounted.h>
35
36namespace JSC {
37    class ArrayBuffer;
38    class ArrayBufferView;
39}
40
41namespace WebCore {
42
43class Blob;
44class Dictionary;
45class RTCDataChannelHandler;
46class RTCPeerConnectionHandler;
47
48class RTCDataChannel final : public RefCounted<RTCDataChannel>, public ScriptWrappable, public EventTargetWithInlineData, public RTCDataChannelHandlerClient {
49public:
50    static PassRefPtr<RTCDataChannel> create(ScriptExecutionContext*, std::unique_ptr<RTCDataChannelHandler>);
51    static PassRefPtr<RTCDataChannel> create(ScriptExecutionContext*, RTCPeerConnectionHandler*, const String& , const Dictionary&, ExceptionCode&);
52    ~RTCDataChannel();
53
54    String label() const;
55    bool ordered() const;
56    unsigned short maxRetransmitTime() const;
57    unsigned short maxRetransmits() const;
58    String protocol() const;
59    bool negotiated() const;
60    unsigned short id() const;
61    AtomicString readyState() const;
62    unsigned long bufferedAmount() const;
63
64    AtomicString binaryType() const;
65    void setBinaryType(const AtomicString&, ExceptionCode&);
66
67    void send(const String&, ExceptionCode&);
68    void send(PassRefPtr<JSC::ArrayBuffer>, ExceptionCode&);
69    void send(PassRefPtr<JSC::ArrayBufferView>, ExceptionCode&);
70    void send(PassRefPtr<Blob>, ExceptionCode&);
71
72    void close();
73
74    DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
75    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
76    DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
77    DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
78
79    void stop();
80
81    // EventTarget
82    virtual EventTargetInterface eventTargetInterface() const override { return RTCDataChannelEventTargetInterfaceType; }
83    virtual ScriptExecutionContext* scriptExecutionContext() const override { return m_scriptExecutionContext; }
84
85    using RefCounted<RTCDataChannel>::ref;
86    using RefCounted<RTCDataChannel>::deref;
87
88private:
89    RTCDataChannel(ScriptExecutionContext*, std::unique_ptr<RTCDataChannelHandler>);
90
91    void scheduleDispatchEvent(PassRefPtr<Event>);
92    void scheduledEventTimerFired(Timer<RTCDataChannel>*);
93
94    // EventTarget
95    virtual void refEventTarget() override { ref(); }
96    virtual void derefEventTarget() override { deref(); }
97
98    ScriptExecutionContext* m_scriptExecutionContext;
99
100    // RTCDataChannelHandlerClient
101    virtual void didChangeReadyState(ReadyState) override;
102    virtual void didReceiveStringData(const String&) override;
103    virtual void didReceiveRawData(const char*, size_t) override;
104    virtual void didDetectError() override;
105
106    std::unique_ptr<RTCDataChannelHandler> m_handler;
107
108    bool m_stopped;
109
110    ReadyState m_readyState;
111    enum BinaryType {
112        BinaryTypeBlob,
113        BinaryTypeArrayBuffer
114    };
115    BinaryType m_binaryType;
116
117    Timer<RTCDataChannel> m_scheduledEventTimer;
118    Vector<RefPtr<Event>> m_scheduledEvents;
119};
120
121} // namespace WebCore
122
123#endif // ENABLE(MEDIA_STREAM)
124
125#endif // RTCDataChannel_h
126