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 "EventListener.h" 38 #include "EventNames.h" 39 #include "EventTarget.h" 40 #include "KURL.h" 41 #include "WebSocketChannelClient.h" 42 #include <wtf/Forward.h> 43 #include <wtf/OwnPtr.h> 44 #include <wtf/RefCounted.h> 45 #include <wtf/text/AtomicStringHash.h> 46 47 namespace WebCore { 48 49 class ThreadableWebSocketChannel; 50 51 class WebSocket : public RefCounted<WebSocket>, public EventTarget, public ActiveDOMObject, public WebSocketChannelClient { 52 public: 53 static void setIsAvailable(bool); 54 static bool isAvailable(); 55 static PassRefPtr<WebSocket> create(ScriptExecutionContext* context) { return adoptRef(new WebSocket(context)); } 56 virtual ~WebSocket(); 57 58 enum State { 59 CONNECTING = 0, 60 OPEN = 1, 61 CLOSED = 2 62 }; 63 64 void connect(const KURL&, ExceptionCode&); 65 void connect(const KURL&, const String& protocol, ExceptionCode&); 66 67 bool send(const String& message, ExceptionCode&); 68 69 void close(); 70 71 const KURL& url() const; 72 State readyState() const; 73 unsigned long bufferedAmount() const; 74 75 DEFINE_ATTRIBUTE_EVENT_LISTENER(open); 76 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 77 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 78 DEFINE_ATTRIBUTE_EVENT_LISTENER(close); 79 80 // EventTarget 81 virtual WebSocket* toWebSocket() { return this; } 82 83 virtual ScriptExecutionContext* scriptExecutionContext() const; 84 virtual void contextDestroyed(); 85 virtual bool canSuspend() const; 86 virtual void suspend(ReasonForSuspension); 87 virtual void resume(); 88 virtual void stop(); 89 90 using RefCounted<WebSocket>::ref; 91 using RefCounted<WebSocket>::deref; 92 93 // WebSocketChannelClient 94 virtual void didConnect(); 95 virtual void didReceiveMessage(const String& message); 96 virtual void didReceiveMessageError(); 97 virtual void didClose(unsigned long unhandledBufferedAmount); 98 99 private: 100 WebSocket(ScriptExecutionContext*); 101 102 virtual void refEventTarget() { ref(); } 103 virtual void derefEventTarget() { deref(); } 104 virtual EventTargetData* eventTargetData(); 105 virtual EventTargetData* ensureEventTargetData(); 106 107 RefPtr<ThreadableWebSocketChannel> m_channel; 108 109 State m_state; 110 KURL m_url; 111 String m_protocol; 112 EventTargetData m_eventTargetData; 113 unsigned long m_bufferedAmountAfterClose; 114 }; 115 116 } // namespace WebCore 117 118 #endif // ENABLE(WEB_SOCKETS) 119 120 #endif // WebSocket_h 121