1 /* 2 * Copyright (C) 2013 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 NewWebSocketChannelImpl_h 32 #define NewWebSocketChannelImpl_h 33 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" 52 53 namespace blink { 54 55 class WebSocketHandshakeRequestInfo; 56 class WebSocketHandshakeResponseInfo; 57 58 } // namespace blink 59 60 namespace WebCore { 61 62 class Document; 63 64 // This class may replace MainThreadWebSocketChannel. 65 class NewWebSocketChannelImpl : public WebSocketChannel, public RefCounted<NewWebSocketChannelImpl>, public blink::WebSocketHandleClient, public ContextLifecycleObserver { 66 WTF_MAKE_FAST_ALLOCATED; 67 public: 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 71 // pass it. 72 static PassRefPtr<NewWebSocketChannelImpl> create(ExecutionContext* context, WebSocketChannelClient* client, const String& sourceURL = String(), unsigned lineNumber = 0) 73 { 74 return adoptRef(new NewWebSocketChannelImpl(context, client, sourceURL, lineNumber)); 75 } 76 virtual ~NewWebSocketChannelImpl(); 77 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; 92 93 using RefCounted<NewWebSocketChannelImpl>::ref; 94 using RefCounted<NewWebSocketChannelImpl>::deref; 95 96 virtual void suspend() OVERRIDE; 97 virtual void resume() OVERRIDE; 98 99 private: 100 enum MessageType { 101 MessageTypeText, 102 MessageTypeBlob, 103 MessageTypeArrayBuffer, 104 }; 105 106 struct Message { 107 explicit Message(const String&); 108 explicit Message(PassRefPtr<BlobDataHandle>); 109 explicit Message(PassRefPtr<ArrayBuffer>); 110 MessageType type; 111 CString text; 112 RefPtr<BlobDataHandle> blobDataHandle; 113 RefPtr<ArrayBuffer> arrayBuffer; 114 }; 115 116 struct ReceivedMessage { 117 bool isMessageText; 118 Vector<char> data; 119 }; 120 121 class BlobLoader; 122 123 NewWebSocketChannelImpl(ExecutionContext*, WebSocketChannelClient*, const String&, unsigned); 124 void sendInternal(); 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. 130 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 140 // Methods for BlobLoader. 141 void didFinishLoadingBlob(PassRefPtr<ArrayBuffer>); 142 void didFailLoadingBlob(FileError::ErrorCode); 143 144 // WebSocketChannel functions. 145 virtual void refWebSocketChannel() OVERRIDE { ref(); } 146 virtual void derefWebSocketChannel() OVERRIDE { deref(); } 147 148 // LifecycleObserver functions. 149 // This object must be destroyed before the context. 150 virtual void contextDestroyed() OVERRIDE { ASSERT_NOT_REACHED(); } 151 152 // m_handle is a handle of the connection. 153 // m_handle == 0 means this channel is closed. 154 OwnPtr<blink::WebSocketHandle> m_handle; 155 156 // m_client can be deleted while this channel is alive, but this class 157 // expects that disconnect() is called before the deletion. 158 WebSocketChannelClient* m_client; 159 KURL m_url; 160 // m_identifier > 0 means calling scriptContextExecution() returns a Document. 161 unsigned long m_identifier; 162 OwnPtr<BlobLoader> m_blobLoader; 163 Deque<Message> m_messages; 164 Vector<char> m_receivingMessageData; 165 166 bool m_receivingMessageTypeIsText; 167 int64_t m_sendingQuota; 168 int64_t m_receivedDataSizeForFlowControl; 169 unsigned long m_bufferedAmount; 170 size_t m_sentSizeOfTopMessage; 171 String m_subprotocol; 172 String m_extensions; 173 174 String m_sourceURLAtConstruction; 175 unsigned m_lineNumberAtConstruction; 176 177 static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15; 178 }; 179 180 } // namespace WebCore 181 182 #endif // NewWebSocketChannelImpl_h 183