Home | History | Annotate | Download | only in mediastream
      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 "bindings/v8/ScriptWrappable.h"
     29 #include "core/dom/EventTarget.h"
     30 #include "core/platform/Timer.h"
     31 #include "core/platform/mediastream/RTCDataChannelHandlerClient.h"
     32 #include "wtf/RefCounted.h"
     33 
     34 namespace WebKit {
     35 struct WebRTCDataChannelInit;
     36 }
     37 
     38 namespace WebCore {
     39 
     40 class Blob;
     41 class ExceptionState;
     42 class RTCDataChannelHandler;
     43 class RTCPeerConnectionHandler;
     44 
     45 class RTCDataChannel : public RefCounted<RTCDataChannel>, public ScriptWrappable, public EventTarget, public RTCDataChannelHandlerClient {
     46 public:
     47     static PassRefPtr<RTCDataChannel> create(ScriptExecutionContext*, PassOwnPtr<RTCDataChannelHandler>);
     48     static PassRefPtr<RTCDataChannel> create(ScriptExecutionContext*, RTCPeerConnectionHandler*, const String& label, const WebKit::WebRTCDataChannelInit&, ExceptionState&);
     49     ~RTCDataChannel();
     50 
     51     String label() const;
     52 
     53     // DEPRECATED
     54     bool reliable() const;
     55 
     56     bool ordered() const;
     57     unsigned short maxRetransmitTime() const;
     58     unsigned short maxRetransmits() const;
     59     String protocol() const;
     60     bool negotiated() const;
     61     unsigned short id() const;
     62     String readyState() const;
     63     unsigned long bufferedAmount() const;
     64 
     65     String binaryType() const;
     66     void setBinaryType(const String&, ExceptionState&);
     67 
     68     void send(const String&, ExceptionState&);
     69     void send(PassRefPtr<ArrayBuffer>, ExceptionState&);
     70     void send(PassRefPtr<ArrayBufferView>, ExceptionState&);
     71     void send(PassRefPtr<Blob>, ExceptionState&);
     72 
     73     void close();
     74 
     75     DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
     76     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
     77     DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
     78     DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
     79 
     80     void stop();
     81 
     82     // EventTarget
     83     virtual const AtomicString& interfaceName() const OVERRIDE;
     84     virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
     85 
     86     using RefCounted<RTCDataChannel>::ref;
     87     using RefCounted<RTCDataChannel>::deref;
     88 
     89 private:
     90     RTCDataChannel(ScriptExecutionContext*, PassOwnPtr<RTCDataChannelHandler>);
     91 
     92     void scheduleDispatchEvent(PassRefPtr<Event>);
     93     void scheduledEventTimerFired(Timer<RTCDataChannel>*);
     94 
     95     // EventTarget
     96     virtual EventTargetData* eventTargetData();
     97     virtual EventTargetData* ensureEventTargetData();
     98     virtual void refEventTarget() { ref(); }
     99     virtual void derefEventTarget() { deref(); }
    100     EventTargetData m_eventTargetData;
    101     ScriptExecutionContext* m_scriptExecutionContext;
    102 
    103     // RTCDataChannelHandlerClient
    104     virtual void didChangeReadyState(ReadyState) OVERRIDE;
    105     virtual void didReceiveStringData(const String&) OVERRIDE;
    106     virtual void didReceiveRawData(const char*, size_t) OVERRIDE;
    107     virtual void didDetectError() OVERRIDE;
    108 
    109     OwnPtr<RTCDataChannelHandler> m_handler;
    110 
    111     bool m_stopped;
    112 
    113     ReadyState m_readyState;
    114     enum BinaryType {
    115         BinaryTypeBlob,
    116         BinaryTypeArrayBuffer
    117     };
    118     BinaryType m_binaryType;
    119 
    120     Timer<RTCDataChannel> m_scheduledEventTimer;
    121     Vector<RefPtr<Event> > m_scheduledEvents;
    122 };
    123 
    124 } // namespace WebCore
    125 
    126 #endif // RTCDataChannel_h
    127