2 * Copyright (C) 2013 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 NewWebSocketChannelImpl_h
32 #define NewWebSocketChannelImpl_h
34 #include "core/dom/ContextLifecycleObserver.h"
35 #include "core/fileapi/Blob.h"
36 #include "core/fileapi/FileError.h"
37 #include "core/frame/ConsoleTypes.h"
38 #include "modules/websockets/WebSocketChannel.h"
39 #include "platform/weborigin/KURL.h"
40 #include "public/platform/WebSocketHandle.h"
41 #include "public/platform/WebSocketHandleClient.h"
42 #include "wtf/ArrayBuffer.h"
43 #include "wtf/Deque.h"
44 #include "wtf/FastAllocBase.h"
45 #include "wtf/OwnPtr.h"
46 #include "wtf/PassRefPtr.h"
47 #include "wtf/RefCounted.h"
48 #include "wtf/RefPtr.h"
49 #include "wtf/Vector.h"
50 #include "wtf/text/CString.h"
51 #include "wtf/text/WTFString.h"
55 class WebSocketHandshakeRequestInfo;
56 class WebSocketHandshakeResponseInfo;
64 // This class may replace MainThreadWebSocketChannel.
65 class NewWebSocketChannelImpl FINAL : public WebSocketChannel, public RefCounted<NewWebSocketChannelImpl>, public blink::WebSocketHandleClient, public ContextLifecycleObserver {
66 WTF_MAKE_FAST_ALLOCATED;
68 // You can specify the source file and the line number information
69 // explicitly by passing the last parameter.
70 // In the usual case, they are set automatically and you don't have to
72 static PassRefPtr<NewWebSocketChannelImpl> create(ExecutionContext* context, WebSocketChannelClient* client, const String& sourceURL = String(), unsigned lineNumber = 0)
74 return adoptRef(new NewWebSocketChannelImpl(context, client, sourceURL, lineNumber));
76 virtual ~NewWebSocketChannelImpl();
78 // WebSocketChannel functions.
79 virtual void connect(const KURL&, const String& protocol) OVERRIDE;
80 virtual String subprotocol() OVERRIDE;
81 virtual String extensions() OVERRIDE;
82 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE;
83 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE;
84 virtual WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>) OVERRIDE;
85 virtual unsigned long bufferedAmount() const OVERRIDE;
86 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code
87 // argument to omit payload.
88 virtual void close(int code, const String& reason) OVERRIDE;
89 virtual void fail(const String& reason, MessageLevel, const String&, unsigned lineNumber) OVERRIDE;
90 using WebSocketChannel::fail;
91 virtual void disconnect() OVERRIDE;
93 using RefCounted<NewWebSocketChannelImpl>::ref;
94 using RefCounted<NewWebSocketChannelImpl>::deref;
96 virtual void suspend() OVERRIDE;
97 virtual void resume() OVERRIDE;
103 MessageTypeArrayBuffer,
107 explicit Message(const String&);
108 explicit Message(PassRefPtr<BlobDataHandle>);
109 explicit Message(PassRefPtr<ArrayBuffer>);
112 RefPtr<BlobDataHandle> blobDataHandle;
113 RefPtr<ArrayBuffer> arrayBuffer;
116 struct ReceivedMessage {
123 NewWebSocketChannelImpl(ExecutionContext*, WebSocketChannelClient*, const String&, unsigned);
125 void flowControlIfNecessary();
126 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_sourceURLAtConstruction, m_lineNumberAtConstruction); }
127 void abortAsyncOperations();
128 void handleDidClose(bool wasClean, unsigned short code, const String& reason);
129 Document* document(); // can be called only when m_identifier > 0.
131 // WebSocketHandleClient functions.
132 virtual void didConnect(blink::WebSocketHandle*, bool fail, const blink::WebString& selectedProtocol, const blink::WebString& extensions) OVERRIDE;
133 virtual void didStartOpeningHandshake(blink::WebSocketHandle*, const blink::WebSocketHandshakeRequestInfo&) OVERRIDE;
134 virtual void didFinishOpeningHandshake(blink::WebSocketHandle*, const blink::WebSocketHandshakeResponseInfo&) OVERRIDE;
135 virtual void didFail(blink::WebSocketHandle*, const blink::WebString& message) OVERRIDE;
136 virtual void didReceiveData(blink::WebSocketHandle*, bool fin, blink::WebSocketHandle::MessageType, const char* data, size_t /* size */) OVERRIDE;
137 virtual void didClose(blink::WebSocketHandle*, bool wasClean, unsigned short code, const blink::WebString& reason) OVERRIDE;
138 virtual void didReceiveFlowControl(blink::WebSocketHandle*, int64_t quota) OVERRIDE;
139 virtual void didStartClosingHandshake(blink::WebSocketHandle*) OVERRIDE;
141 // Methods for BlobLoader.
142 void didFinishLoadingBlob(PassRefPtr<ArrayBuffer>);
143 void didFailLoadingBlob(FileError::ErrorCode);
145 // WebSocketChannel functions.
146 virtual void refWebSocketChannel() OVERRIDE { ref(); }
147 virtual void derefWebSocketChannel() OVERRIDE { deref(); }
149 // LifecycleObserver functions.
150 // This object must be destroyed before the context.
151 virtual void contextDestroyed() OVERRIDE { ASSERT_NOT_REACHED(); }
153 // m_handle is a handle of the connection.
154 // m_handle == 0 means this channel is closed.
155 OwnPtr<blink::WebSocketHandle> m_handle;
157 // m_client can be deleted while this channel is alive, but this class
158 // expects that disconnect() is called before the deletion.
159 WebSocketChannelClient* m_client;
161 // m_identifier > 0 means calling scriptContextExecution() returns a Document.
162 unsigned long m_identifier;
163 OwnPtr<BlobLoader> m_blobLoader;
164 Deque<Message> m_messages;
165 Vector<char> m_receivingMessageData;
167 bool m_receivingMessageTypeIsText;
168 int64_t m_sendingQuota;
169 int64_t m_receivedDataSizeForFlowControl;
170 unsigned long m_bufferedAmount;
171 size_t m_sentSizeOfTopMessage;
172 String m_subprotocol;
175 String m_sourceURLAtConstruction;
176 unsigned m_lineNumberAtConstruction;
178 static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15;
181 } // namespace WebCore
183 #endif // NewWebSocketChannelImpl_h