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#include "modules/EventTargetModules.h"
29#include "platform/Timer.h"
30#include "platform/heap/Handle.h"
31#include "public/platform/WebRTCDataChannelHandler.h"
32#include "public/platform/WebRTCDataChannelHandlerClient.h"
33
34namespace blink {
35
36class Blob;
37class ExceptionState;
38class RTCPeerConnection;
39class WebRTCDataChannelHandler;
40class WebRTCPeerConnectionHandler;
41struct WebRTCDataChannelInit;
42
43class RTCDataChannel FINAL
44    : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<RTCDataChannel>
45    , public EventTargetWithInlineData
46    , public WebRTCDataChannelHandlerClient {
47    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<RTCDataChannel>);
48    DEFINE_WRAPPERTYPEINFO();
49    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(RTCDataChannel);
50public:
51    static RTCDataChannel* create(ExecutionContext*, RTCPeerConnection*, PassOwnPtr<WebRTCDataChannelHandler>);
52    static RTCDataChannel* create(ExecutionContext*, RTCPeerConnection*, WebRTCPeerConnectionHandler*, const String& label, const WebRTCDataChannelInit&, ExceptionState&);
53    virtual ~RTCDataChannel();
54
55    String label() const;
56
57    // DEPRECATED
58    bool reliable() const;
59
60    bool ordered() const;
61    unsigned short maxRetransmitTime() const;
62    unsigned short maxRetransmits() const;
63    String protocol() const;
64    bool negotiated() const;
65    unsigned short id() const;
66    String readyState() const;
67    unsigned long bufferedAmount() const;
68
69    String binaryType() const;
70    void setBinaryType(const String&, ExceptionState&);
71
72    void send(const String&, ExceptionState&);
73    void send(PassRefPtr<ArrayBuffer>, ExceptionState&);
74    void send(PassRefPtr<ArrayBufferView>, ExceptionState&);
75    void send(PassRefPtrWillBeRawPtr<Blob>, ExceptionState&);
76
77    void close();
78
79    DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
80    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
81    DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
82    DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
83
84    void stop();
85
86    // EventTarget
87    virtual const AtomicString& interfaceName() const OVERRIDE;
88    virtual ExecutionContext* executionContext() const OVERRIDE;
89
90    void clearWeakMembers(Visitor*);
91    virtual void trace(Visitor*) OVERRIDE;
92
93private:
94    RTCDataChannel(ExecutionContext*, RTCPeerConnection*, PassOwnPtr<WebRTCDataChannelHandler>);
95
96    void scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event>);
97    void scheduledEventTimerFired(Timer<RTCDataChannel>*);
98
99    ExecutionContext* m_executionContext;
100
101    // WebRTCDataChannelHandlerClient
102    virtual void didChangeReadyState(WebRTCDataChannelHandlerClient::ReadyState) OVERRIDE;
103    virtual void didReceiveStringData(const WebString&) OVERRIDE;
104    virtual void didReceiveRawData(const char*, size_t) OVERRIDE;
105    virtual void didDetectError() OVERRIDE;
106
107    OwnPtr<WebRTCDataChannelHandler> m_handler;
108
109    bool m_stopped;
110
111    WebRTCDataChannelHandlerClient::ReadyState m_readyState;
112
113    enum BinaryType {
114        BinaryTypeBlob,
115        BinaryTypeArrayBuffer
116    };
117    BinaryType m_binaryType;
118
119    Timer<RTCDataChannel> m_scheduledEventTimer;
120    WillBeHeapVector<RefPtrWillBeMember<Event> > m_scheduledEvents;
121
122    WeakMember<RTCPeerConnection> m_connection;
123};
124
125} // namespace blink
126
127#endif // RTCDataChannel_h
128