2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "bindings/v8/ScriptWrappable.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/events/EventListener.h"
37 #include "core/events/EventTarget.h"
38 #include "core/events/ThreadLocalEventNames.h"
39 #include "modules/websockets/WebSocketChannel.h"
40 #include "modules/websockets/WebSocketChannelClient.h"
41 #include "platform/Timer.h"
42 #include "platform/weborigin/KURL.h"
43 #include "wtf/Deque.h"
44 #include "wtf/Forward.h"
45 #include "wtf/OwnPtr.h"
46 #include "wtf/RefCounted.h"
47 #include "wtf/text/AtomicStringHash.h"
54 class WebSocket FINAL : public RefCounted<WebSocket>, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject, public WebSocketChannelClient {
55 REFCOUNTED_EVENT_TARGET(WebSocket);
57 static const char* subProtocolSeperator();
58 // WebSocket instances must be used with a wrapper since this class's
59 // lifetime management is designed assuming the V8 holds a ref on it while
60 // hasPendingActivity() returns true.
61 static PassRefPtr<WebSocket> create(ExecutionContext*, const String& url, ExceptionState&);
62 static PassRefPtr<WebSocket> create(ExecutionContext*, const String& url, const String& protocol, ExceptionState&);
63 static PassRefPtr<WebSocket> create(ExecutionContext*, const String& url, const Vector<String>& protocols, ExceptionState&);
73 void connect(const String& url, ExceptionState&);
74 void connect(const String& url, const String& protocol, ExceptionState&);
75 void connect(const String& url, const Vector<String>& protocols, ExceptionState&);
77 void send(const String& message, ExceptionState&);
78 void send(ArrayBuffer*, ExceptionState&);
79 void send(ArrayBufferView*, ExceptionState&);
80 void send(Blob*, ExceptionState&);
82 // To distinguish close method call with the code parameter from one
83 // without, we have these three signatures. Use of
84 // Optional=DefaultIsUndefined in the IDL file doesn't help for now since
85 // it's bound to a value of 0 which is indistinguishable from the case 0
86 // is passed as code parameter.
87 void close(unsigned short code, const String& reason, ExceptionState&);
88 void close(ExceptionState&);
89 void close(unsigned short code, ExceptionState&);
91 const KURL& url() const;
92 State readyState() const;
93 unsigned long bufferedAmount() const;
95 String protocol() const;
96 String extensions() const;
98 String binaryType() const;
99 void setBinaryType(const String&);
101 DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
103 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
104 DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
106 // EventTarget functions.
107 virtual const AtomicString& interfaceName() const OVERRIDE;
108 virtual ExecutionContext* executionContext() const OVERRIDE;
110 // ActiveDOMObject functions.
111 virtual void contextDestroyed() OVERRIDE;
112 // Prevent this instance from being collected while it's not in CLOSED
114 virtual bool hasPendingActivity() const OVERRIDE;
115 virtual void suspend() OVERRIDE;
116 virtual void resume() OVERRIDE;
117 virtual void stop() OVERRIDE;
119 // WebSocketChannelClient functions.
120 virtual void didConnect() OVERRIDE;
121 virtual void didReceiveMessage(const String& message) OVERRIDE;
122 virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE;
123 virtual void didReceiveMessageError() OVERRIDE;
124 virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERRIDE;
125 virtual void didStartClosingHandshake() OVERRIDE;
126 virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE;
129 class EventQueue FINAL : public RefCounted<EventQueue> {
131 static PassRefPtr<EventQueue> create(EventTarget* target) { return adoptRef(new EventQueue(target)); }
134 // Dispatches the event if this queue is active.
135 // Queues the event if this queue is suspended.
136 // Does nothing otherwise.
137 void dispatch(PassRefPtr<Event> /* event */);
139 bool isEmpty() const;
152 explicit EventQueue(EventTarget*);
154 // Dispatches queued events if this queue is active.
155 // Does nothing otherwise.
156 void dispatchQueuedEvents();
157 void resumeTimerFired(Timer<EventQueue>*);
160 EventTarget* m_target;
161 Deque<RefPtr<Event> > m_events;
162 Timer<EventQueue> m_resumeTimer;
165 explicit WebSocket(ExecutionContext*);
167 // Adds a console message with JSMessageSource and ErrorMessageLevel.
168 void logError(const String& message);
170 // Handle the JavaScript close method call. close() methods on this class
171 // are just for determining if the optional code argument is supplied or
173 void closeInternal(int, const String&, ExceptionState&);
175 size_t getFramingOverhead(size_t payloadSize);
177 // Checks the result of WebSocketChannel::send() method, and shows console
178 // message and sets ec appropriately.
179 void handleSendResult(WebSocketChannel::SendResult, ExceptionState&);
181 // Updates m_bufferedAmountAfterClose given the amount of data passed to
182 // send() method after the state changed to CLOSING or CLOSED.
183 void updateBufferedAmountAfterClose(unsigned long);
185 void releaseChannel();
189 BinaryTypeArrayBuffer
192 RefPtr<WebSocketChannel> m_channel;
196 unsigned long m_bufferedAmount;
197 unsigned long m_bufferedAmountAfterClose;
198 BinaryType m_binaryType;
199 String m_subprotocol;
202 RefPtr<EventQueue> m_eventQueue;
205 } // namespace WebCore
207 #endif // WebSocket_h