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.
31 #ifndef DOMWebSocket_h
32 #define DOMWebSocket_h
34 #include "core/dom/ActiveDOMObject.h"
35 #include "core/events/EventListener.h"
36 #include "modules/EventTargetModules.h"
37 #include "modules/websockets/WebSocketChannel.h"
38 #include "modules/websockets/WebSocketChannelClient.h"
39 #include "platform/Timer.h"
40 #include "platform/heap/Handle.h"
41 #include "platform/weborigin/KURL.h"
42 #include "wtf/Deque.h"
43 #include "wtf/Forward.h"
44 #include "wtf/text/AtomicStringHash.h"
50 class DOMArrayBufferView;
53 class DOMWebSocket : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<DOMWebSocket>, public EventTargetWithInlineData, public ActiveDOMObject, public WebSocketChannelClient {
54 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<DOMWebSocket>);
55 DEFINE_WRAPPERTYPEINFO();
56 USING_GARBAGE_COLLECTED_MIXIN(DOMWebSocket);
58 static const char* subprotocolSeperator();
59 // DOMWebSocket instances must be used with a wrapper since this class's
60 // lifetime management is designed assuming the V8 holds a ref on it while
61 // hasPendingActivity() returns true.
62 static DOMWebSocket* create(ExecutionContext*, const String& url, ExceptionState&);
63 static DOMWebSocket* create(ExecutionContext*, const String& url, const String& protocol, ExceptionState&);
64 static DOMWebSocket* create(ExecutionContext*, const String& url, const Vector<String>& protocols, ExceptionState&);
65 virtual ~DOMWebSocket();
74 void connect(const String& url, const Vector<String>& protocols, ExceptionState&);
76 void send(const String& message, ExceptionState&);
77 void send(DOMArrayBuffer*, ExceptionState&);
78 void send(DOMArrayBufferView*, ExceptionState&);
79 void send(Blob*, ExceptionState&);
81 // To distinguish close method call with the code parameter from one
82 // without, we have these three signatures. Use of
83 // Optional=DefaultIsUndefined in the IDL file doesn't help for now since
84 // it's bound to a value of 0 which is indistinguishable from the case 0
85 // is passed as code parameter.
86 void close(unsigned short code, const String& reason, ExceptionState&);
87 void close(ExceptionState&);
88 void close(unsigned short code, ExceptionState&);
90 const KURL& url() const;
91 State readyState() const;
92 unsigned long bufferedAmount() const;
94 String protocol() const;
95 String extensions() const;
97 String binaryType() const;
98 void setBinaryType(const String&);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
101 DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
103 DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
105 // EventTarget functions.
106 virtual const AtomicString& interfaceName() const override;
107 virtual ExecutionContext* executionContext() const override;
109 // ActiveDOMObject functions.
110 virtual void contextDestroyed() override;
111 // Prevent this instance from being collected while it's not in CLOSED
113 virtual bool hasPendingActivity() const override;
114 virtual void suspend() override;
115 virtual void resume() override;
116 virtual void stop() override;
118 // WebSocketChannelClient functions.
119 virtual void didConnect(const String& subprotocol, const String& extensions) override;
120 virtual void didReceiveTextMessage(const String& message) override;
121 virtual void didReceiveBinaryMessage(PassOwnPtr<Vector<char> >) override;
122 virtual void didError() override;
123 virtual void didConsumeBufferedAmount(unsigned long) override;
124 virtual void didStartClosingHandshake() override;
125 virtual void didClose(ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) override;
127 virtual void trace(Visitor*) override;
129 static bool isValidSubprotocolString(const String&);
132 explicit DOMWebSocket(ExecutionContext*);
135 // FIXME: This should inherit blink::EventQueue.
136 class EventQueue final : public GarbageCollectedFinalized<EventQueue> {
138 static EventQueue* create(EventTarget* target)
140 return new EventQueue(target);
144 // Dispatches the event if this queue is active.
145 // Queues the event if this queue is suspended.
146 // Does nothing otherwise.
147 void dispatch(PassRefPtrWillBeRawPtr<Event> /* event */);
149 bool isEmpty() const;
155 void trace(Visitor*);
164 explicit EventQueue(EventTarget*);
166 // Dispatches queued events if this queue is active.
167 // Does nothing otherwise.
168 void dispatchQueuedEvents();
169 void resumeTimerFired(Timer<EventQueue>*);
172 EventTarget* m_target;
173 WillBeHeapDeque<RefPtrWillBeMember<Event> > m_events;
174 Timer<EventQueue> m_resumeTimer;
177 enum WebSocketSendType {
178 WebSocketSendTypeString,
179 WebSocketSendTypeArrayBuffer,
180 WebSocketSendTypeArrayBufferView,
181 WebSocketSendTypeBlob,
182 WebSocketSendTypeMax,
185 // This function is virtual for unittests.
186 // FIXME: Move WebSocketChannel::create here.
187 virtual WebSocketChannel* createChannel(ExecutionContext* context, WebSocketChannelClient* client)
189 return WebSocketChannel::create(context, client);
192 // Adds a console message with JSMessageSource and ErrorMessageLevel.
193 void logError(const String& message);
195 // Handle the JavaScript close method call. close() methods on this class
196 // are just for determining if the optional code argument is supplied or
198 void closeInternal(int, const String&, ExceptionState&);
200 size_t getFramingOverhead(size_t payloadSize);
202 // Updates m_bufferedAmountAfterClose given the amount of data passed to
203 // send() method after the state changed to CLOSING or CLOSED.
204 void updateBufferedAmountAfterClose(unsigned long);
205 void reflectBufferedAmountConsumption(Timer<DOMWebSocket>*);
207 void releaseChannel();
211 BinaryTypeArrayBuffer
214 Member<WebSocketChannel> m_channel;
218 unsigned long m_bufferedAmount;
219 // The consumed buffered amount that will be reflected to m_bufferedAmount
220 // later. It will be cleared once reflected.
221 unsigned long m_consumedBufferedAmount;
222 unsigned long m_bufferedAmountAfterClose;
223 BinaryType m_binaryType;
224 // The subprotocol the server selected.
225 String m_subprotocol;
228 Member<EventQueue> m_eventQueue;
229 Timer<DOMWebSocket> m_bufferedAmountConsumeTimer;
234 #endif // DOMWebSocket_h