OLD | NEW |
(Empty) | |
| 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 #include "ActiveDOMObject.h" |
| 35 #include "AtomicStringHash.h" |
| 36 #include "EventListener.h" |
| 37 #include "EventTarget.h" |
| 38 #include "KURL.h" |
| 39 #include "WebSocketChannelClient.h" |
| 40 #include <wtf/OwnPtr.h> |
| 41 #include <wtf/RefCounted.h> |
| 42 |
| 43 namespace WebCore { |
| 44 |
| 45 class String; |
| 46 class WebSocketChannel; |
| 47 |
| 48 class WebSocket : public RefCounted<WebSocket>, public EventTarget, public Activ
eDOMObject, private WebSocketChannelClient { |
| 49 public: |
| 50 static PassRefPtr<WebSocket> create(ScriptExecutionContext* context) { retur
n adoptRef(new WebSocket(context)); } |
| 51 virtual ~WebSocket(); |
| 52 |
| 53 enum State { |
| 54 CONNECTING = 0, |
| 55 OPEN = 1, |
| 56 CLOSED = 2 |
| 57 }; |
| 58 |
| 59 void connect(const KURL& url, ExceptionCode&); |
| 60 void connect(const KURL& url, const String& protocol, ExceptionCode&); |
| 61 |
| 62 bool send(const String& message, ExceptionCode&); |
| 63 |
| 64 void close(); |
| 65 |
| 66 const KURL& url() const; |
| 67 State readyState() const; |
| 68 unsigned long bufferedAmount() const; |
| 69 |
| 70 void setOnopen(PassRefPtr<EventListener> eventListener) { m_onopen = eventLi
stener; } |
| 71 EventListener* onopen() const { return m_onopen.get(); } |
| 72 void setOnmessage(PassRefPtr<EventListener> eventListener) { m_onmessage = e
ventListener; } |
| 73 EventListener* onmessage() const { return m_onmessage.get(); } |
| 74 void setOnclose(PassRefPtr<EventListener> eventListener) { m_onclose = event
Listener; } |
| 75 EventListener* onclose() const { return m_onclose.get(); } |
| 76 |
| 77 // EventTarget |
| 78 virtual WebSocket* toWebSocket() { return this; } |
| 79 |
| 80 virtual ScriptExecutionContext* scriptExecutionContext() const; |
| 81 |
| 82 virtual void addEventListener(const AtomicString& eventType, PassRefPtr<Even
tListener>, bool useCapture); |
| 83 virtual void removeEventListener(const AtomicString& eventType, EventListene
r*, bool useCapture); |
| 84 virtual bool dispatchEvent(PassRefPtr<Event>, ExceptionCode&); |
| 85 |
| 86 // ActiveDOMObject |
| 87 // virtual bool hasPendingActivity() const; |
| 88 // virtual void contextDestroyed(); |
| 89 // virtual bool canSuspend() const; |
| 90 // virtual void suspend(); |
| 91 // virtual void resume(); |
| 92 // virtual void stop(); |
| 93 |
| 94 using RefCounted<WebSocket>::ref; |
| 95 using RefCounted<WebSocket>::deref; |
| 96 |
| 97 private: |
| 98 WebSocket(ScriptExecutionContext*); |
| 99 |
| 100 virtual void refEventTarget() { ref(); } |
| 101 virtual void derefEventTarget() { deref(); } |
| 102 |
| 103 // WebSocketChannelClient |
| 104 void didConnect(); |
| 105 void didReceiveMessage(const String& msg); |
| 106 void didClose(); |
| 107 |
| 108 void dispatchOpenEvent(); |
| 109 void dispatchMessageEvent(const String& msg); |
| 110 void dispatchCloseEvent(); |
| 111 |
| 112 RefPtr<WebSocketChannel> m_channel; |
| 113 |
| 114 RefPtr<EventListener> m_onopen; |
| 115 RefPtr<EventListener> m_onmessage; |
| 116 RefPtr<EventListener> m_onclose; |
| 117 |
| 118 State m_state; |
| 119 KURL m_url; |
| 120 String m_protocol; |
| 121 }; |
| 122 |
| 123 } // namespace WebCore |
| 124 |
| 125 #endif // WebSocket_h |
OLD | NEW |