1 /* 2 * Copyright (C) 2009 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef WebSocket_h 32 #define WebSocket_h 33 34 #if ENABLE(WEB_SOCKETS) 35 36 #include "ActiveDOMObject.h" 37 #include "AtomicStringHash.h" 38 #include "EventListener.h" 39 #include "EventNames.h" 40 #include "EventTarget.h" 41 #include "KURL.h" 42 #include "WebSocketChannelClient.h" 43 #include <wtf/OwnPtr.h> 44 #include <wtf/RefCounted.h> 45 46 namespace WebCore { 47 48 class String; 49 class ThreadableWebSocketChannel; 50 51 class WebSocket : public RefCounted<WebSocket>, public EventTarget, public ActiveDOMObject, public WebSocketChannelClient { 52 public: 53 #if USE(V8) 54 static void setIsAvailable(bool); 55 static bool isAvailable(); 56 #endif 57 static PassRefPtr<WebSocket> create(ScriptExecutionContext* context) { return adoptRef(new WebSocket(context)); } 58 virtual ~WebSocket(); 59 60 enum State { 61 CONNECTING = 0, 62 OPEN = 1, 63 CLOSED = 2 64 }; 65 66 void connect(const KURL&, ExceptionCode&); 67 void connect(const KURL&, const String& protocol, ExceptionCode&); 68 69 bool send(const String& message, ExceptionCode&); 70 71 void close(); 72 73 const KURL& url() const; 74 State readyState() const; 75 unsigned long bufferedAmount() const; 76 77 DEFINE_ATTRIBUTE_EVENT_LISTENER(open); 78 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 79 DEFINE_ATTRIBUTE_EVENT_LISTENER(close); 80 81 // EventTarget 82 virtual WebSocket* toWebSocket() { return this; } 83 84 virtual ScriptExecutionContext* scriptExecutionContext() const; 85 virtual void contextDestroyed(); 86 virtual void stop(); 87 88 using RefCounted<WebSocket>::ref; 89 using RefCounted<WebSocket>::deref; 90 91 // WebSocketChannelClient 92 virtual void didConnect(); 93 virtual void didReceiveMessage(const String& message); 94 virtual void didClose(unsigned long unhandledBufferedAmount); 95 96 private: 97 WebSocket(ScriptExecutionContext*); 98 99 virtual void refEventTarget() { ref(); } 100 virtual void derefEventTarget() { deref(); } 101 virtual EventTargetData* eventTargetData(); 102 virtual EventTargetData* ensureEventTargetData(); 103 104 void dispatchOpenEvent(Event*); 105 void dispatchMessageEvent(Event*); 106 void dispatchCloseEvent(Event*); 107 108 RefPtr<ThreadableWebSocketChannel> m_channel; 109 110 State m_state; 111 KURL m_url; 112 String m_protocol; 113 EventTargetData m_eventTargetData; 114 unsigned long m_bufferedAmountAfterClose; 115 }; 116 117 } // namespace WebCore 118 119 #endif // ENABLE(WEB_SOCKETS) 120 121 #endif // WebSocket_h 122