2 * Copyright (C) 2011, 2012 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 WebSocketChannel_h
32 #define WebSocketChannel_h
34 #if ENABLE(WEB_SOCKETS)
36 #include "FileReaderLoaderClient.h"
37 #include "SocketStreamHandleClient.h"
38 #include "ThreadableWebSocketChannel.h"
40 #include "WebSocketDeflateFramer.h"
41 #include "WebSocketFrame.h"
42 #include "WebSocketHandshake.h"
43 #include <wtf/Deque.h>
44 #include <wtf/Forward.h>
45 #include <wtf/RefCounted.h>
46 #include <wtf/Vector.h>
52 class FileReaderLoader;
53 class SocketStreamHandle;
54 class SocketStreamError;
55 class WebSocketChannelClient;
57 class WebSocketChannel : public RefCounted<WebSocketChannel>, public SocketStreamHandleClient, public ThreadableWebSocketChannel
59 , public FileReaderLoaderClient
62 WTF_MAKE_FAST_ALLOCATED;
64 static PassRefPtr<WebSocketChannel> create(Document* document, WebSocketChannelClient* client) { return adoptRef(new WebSocketChannel(document, client)); }
65 virtual ~WebSocketChannel();
67 bool send(const char* data, int length);
69 // ThreadableWebSocketChannel functions.
70 virtual bool useHixie76Protocol() OVERRIDE;
71 virtual void connect(const KURL&, const String& protocol) OVERRIDE;
72 virtual String subprotocol() OVERRIDE;
73 virtual String extensions() OVERRIDE;
74 virtual bool send(const String& message) OVERRIDE;
75 virtual bool send(const ArrayBuffer&) OVERRIDE;
76 virtual bool send(const Blob&) OVERRIDE;
77 virtual unsigned long bufferedAmount() const OVERRIDE;
78 virtual void close(int code, const String& reason) OVERRIDE; // Start closing handshake.
79 virtual void fail(const String& reason) OVERRIDE;
80 virtual void disconnect() OVERRIDE;
82 virtual void suspend() OVERRIDE;
83 virtual void resume() OVERRIDE;
85 // SocketStreamHandleClient functions.
86 virtual void didOpenSocketStream(SocketStreamHandle*) OVERRIDE;
87 virtual void didCloseSocketStream(SocketStreamHandle*) OVERRIDE;
88 virtual void didReceiveSocketStreamData(SocketStreamHandle*, const char*, int) OVERRIDE;
89 virtual void didUpdateBufferedAmount(SocketStreamHandle*, size_t bufferedAmount) OVERRIDE;
90 virtual void didFailSocketStream(SocketStreamHandle*, const SocketStreamError&) OVERRIDE;
91 virtual void didReceiveAuthenticationChallenge(SocketStreamHandle*, const AuthenticationChallenge&) OVERRIDE;
92 virtual void didCancelAuthenticationChallenge(SocketStreamHandle*, const AuthenticationChallenge&) OVERRIDE;
95 CloseEventCodeNotSpecified = -1,
96 CloseEventCodeNormalClosure = 1000,
97 CloseEventCodeGoingAway = 1001,
98 CloseEventCodeProtocolError = 1002,
99 CloseEventCodeUnsupportedData = 1003,
100 CloseEventCodeFrameTooLarge = 1004,
101 CloseEventCodeNoStatusRcvd = 1005,
102 CloseEventCodeAbnormalClosure = 1006,
103 CloseEventCodeInvalidUTF8 = 1007,
104 CloseEventCodeMinimumUserDefined = 3000,
105 CloseEventCodeMaximumUserDefined = 4999
109 // FileReaderLoaderClient functions.
110 virtual void didStartLoading();
111 virtual void didReceiveData();
112 virtual void didFinishLoading();
113 virtual void didFail(int errorCode);
116 using RefCounted<WebSocketChannel>::ref;
117 using RefCounted<WebSocketChannel>::deref;
120 virtual void refThreadableWebSocketChannel() { ref(); }
121 virtual void derefThreadableWebSocketChannel() { deref(); }
124 WebSocketChannel(Document*, WebSocketChannelClient*);
126 bool appendToBuffer(const char* data, size_t len);
127 void skipBuffer(size_t len);
128 bool processBuffer();
129 void resumeTimerFired(Timer<WebSocketChannel>*);
130 void startClosingHandshake(int code, const String& reason);
131 void closingTimerFired(Timer<WebSocketChannel>*);
133 enum ParseFrameResult {
139 ParseFrameResult parseFrame(WebSocketFrame&, const char*& frameEnd); // May modify part of m_buffer to unmask the frame.
142 bool processFrameHixie76();
144 // It is allowed to send a Blob as a binary frame if hybi-10 protocol is in use. Sending a Blob
145 // can be delayed because it must be read asynchronously. Other types of data (String or
146 // ArrayBuffer) may also be blocked by preceding sending request of a Blob.
148 // To address this situation, messages to be sent need to be stored in a queue. Whenever a new
149 // data frame is going to be sent, it first must go to the queue. Items in the queue are processed
150 // in the order they were put into the queue. Sending request of a Blob blocks further processing
151 // until the Blob is completely read and sent to the socket stream.
153 // When hixie-76 protocol is chosen, the queue is not used and messages are sent directly.
154 enum QueuedFrameType {
155 QueuedFrameTypeString,
156 QueuedFrameTypeVector,
160 WebSocketFrame::OpCode opCode;
161 QueuedFrameType frameType;
162 // Only one of the following items is used, according to the value of frameType.
164 Vector<char> vectorData;
165 RefPtr<Blob> blobData;
167 void enqueueTextFrame(const String&);
168 void enqueueRawFrame(WebSocketFrame::OpCode, const char* data, size_t dataLength);
169 void enqueueBlobFrame(WebSocketFrame::OpCode, const Blob&);
171 void processOutgoingFrameQueue();
172 void abortOutgoingFrameQueue();
174 enum OutgoingFrameQueueStatus {
175 // It is allowed to put a new item into the queue.
176 OutgoingFrameQueueOpen,
177 // Close frame has already been put into the queue but may not have been sent yet;
178 // m_handle->close() will be called as soon as the queue is cleared. It is not
179 // allowed to put a new item into the queue.
180 OutgoingFrameQueueClosing,
181 // Close frame has been sent or the queue was aborted. It is not allowed to put
182 // a new item to the queue.
183 OutgoingFrameQueueClosed
186 // If you are going to send a hybi-10 frame, you need to use the outgoing frame queue
187 // instead of call sendFrame() directly.
188 bool sendFrame(WebSocketFrame::OpCode, const char* data, size_t dataLength);
189 bool sendFrameHixie76(const char* data, size_t dataLength);
192 enum BlobLoaderStatus {
193 BlobLoaderNotStarted,
200 Document* m_document;
201 WebSocketChannelClient* m_client;
202 OwnPtr<WebSocketHandshake> m_handshake;
203 RefPtr<SocketStreamHandle> m_handle;
207 Timer<WebSocketChannel> m_resumeTimer;
210 bool m_receivedClosingHandshake;
211 Timer<WebSocketChannel> m_closingTimer;
213 bool m_shouldDiscardReceivedData;
214 unsigned long m_unhandledBufferedAmount;
216 unsigned long m_identifier; // m_identifier == 0 means that we could not obtain a valid identifier.
218 bool m_useHixie76Protocol;
220 // Private members only for hybi-10 protocol.
221 bool m_hasContinuousFrame;
222 WebSocketFrame::OpCode m_continuousFrameOpCode;
223 Vector<char> m_continuousFrameData;
224 unsigned short m_closeEventCode;
225 String m_closeEventReason;
227 Deque<OwnPtr<QueuedFrame> > m_outgoingFrameQueue;
228 OutgoingFrameQueueStatus m_outgoingFrameQueueStatus;
231 // FIXME: Load two or more Blobs simultaneously for better performance.
232 OwnPtr<FileReaderLoader> m_blobLoader;
233 BlobLoaderStatus m_blobLoaderStatus;
236 WebSocketDeflateFramer m_deflateFramer;
239 } // namespace WebCore
241 #endif // ENABLE(WEB_SOCKETS)
243 #endif // WebSocketChannel_h