OLD | NEW |
(Empty) | |
| 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/fileapi/Blob.h" |
| 35 #include "core/fileapi/FileReaderLoader.h" |
| 36 #include "core/fileapi/FileReaderLoaderClient.h" |
| 37 #include "core/platform/SharedBuffer.h" |
| 38 #include "modules/websockets/WebSocketChannel.h" |
| 39 #include "public/platform/WebSocketHandle.h" |
| 40 #include "public/platform/WebSocketHandleClient.h" |
| 41 #include "wtf/ArrayBuffer.h" |
| 42 #include "wtf/PassRefPtr.h" |
| 43 #include "wtf/RefCounted.h" |
| 44 #include "wtf/RefPtr.h" |
| 45 #include "wtf/Vector.h" |
| 46 #include "wtf/text/WTFString.h" |
| 47 |
| 48 namespace WebCore { |
| 49 |
| 50 // This class may replace MainThreadWebSocketChannel. |
| 51 class NewWebSocketChannelImpl : public WebSocketChannel, public RefCounted<NewWe
bSocketChannelImpl>, public FileReaderLoaderClient, public WebKit::WebSocketHand
leClient { |
| 52 WTF_MAKE_FAST_ALLOCATED; |
| 53 public: |
| 54 // You can specify the source file and the line number information |
| 55 // explicitly by passing the last parameter. |
| 56 // In the usual case, they are set automatically and you don't have to |
| 57 // pass it. |
| 58 static PassRefPtr<NewWebSocketChannelImpl> create(ScriptExecutionContext* co
ntext, WebSocketChannelClient* client, const String& sourceURL = String(), unsig
ned lineNumber = 0) |
| 59 { |
| 60 return adoptRef(new NewWebSocketChannelImpl(context, client, sourceURL,
lineNumber)); |
| 61 } |
| 62 virtual ~NewWebSocketChannelImpl() { } |
| 63 |
| 64 // WebSocketChannel functions. |
| 65 virtual void connect(const KURL&, const String& protocol) OVERRIDE; |
| 66 virtual String subprotocol() OVERRIDE; |
| 67 virtual String extensions() OVERRIDE; |
| 68 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; |
| 69 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; |
| 70 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; |
| 71 virtual unsigned long bufferedAmount() const OVERRIDE; |
| 72 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code |
| 73 // argument to omit payload. |
| 74 virtual void close(int code, const String& reason) OVERRIDE; |
| 75 virtual void fail(const String& reason, MessageLevel, const String&, unsigne
d lineNumber) OVERRIDE; |
| 76 using WebSocketChannel::fail; |
| 77 virtual void disconnect() OVERRIDE; |
| 78 |
| 79 using RefCounted<NewWebSocketChannelImpl>::ref; |
| 80 using RefCounted<NewWebSocketChannelImpl>::deref; |
| 81 |
| 82 virtual void suspend() OVERRIDE; |
| 83 virtual void resume() OVERRIDE; |
| 84 |
| 85 private: |
| 86 enum MessageType { |
| 87 MessageTypeText, |
| 88 MessageTypeBlob, |
| 89 MessageTypeArrayBuffer, |
| 90 }; |
| 91 struct Message { |
| 92 Message(const String&); |
| 93 Message(const Blob&); |
| 94 Message(PassRefPtr<ArrayBuffer>); |
| 95 MessageType type; |
| 96 CString text; |
| 97 RefPtr<Blob> blob; |
| 98 RefPtr<ArrayBuffer> arrayBuffer; |
| 99 }; |
| 100 |
| 101 struct ReceivedMessage { |
| 102 bool isMessageText; |
| 103 Vector<char> data; |
| 104 }; |
| 105 |
| 106 enum State { |
| 107 NotConnected, |
| 108 Connecting, |
| 109 Open, |
| 110 Closing, |
| 111 Closed, |
| 112 }; |
| 113 |
| 114 struct PendingEvent { |
| 115 enum Type { |
| 116 DidConnectComplete, |
| 117 DidReceiveTextMessage, |
| 118 DidReceiveBinaryMessage, |
| 119 DidReceiveError, |
| 120 DidClose, |
| 121 }; |
| 122 Type type; |
| 123 Vector<char> message; // for DidReceive*Message |
| 124 int closingCode; // for DidClose |
| 125 String closingReason; // for DidClose |
| 126 |
| 127 PendingEvent(Type type) : type(type) { } |
| 128 PendingEvent(int code, const String& reason) : type(DidClose), closingCo
de(code), closingReason(reason) { } |
| 129 }; |
| 130 |
| 131 NewWebSocketChannelImpl(ScriptExecutionContext*, WebSocketChannelClient*, co
nst String&, unsigned); |
| 132 void sendInternal(); |
| 133 void flowControlIfNecessary(); |
| 134 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_s
ourceURLAtConnection, m_lineNumberAtConnection); } |
| 135 |
| 136 // WebSocketHandleClient functions. |
| 137 virtual void didConnect(WebKit::WebSocketHandle*, bool succeed, const WebKit
::WebString& selectedProtocol, const WebKit::WebString& extensions) OVERRIDE; |
| 138 virtual void didReceiveData(WebKit::WebSocketHandle*, WebKit::WebSocketHandl
e::MessageType, const char* data, size_t /* size */, bool fin) OVERRIDE; |
| 139 virtual void didClose(WebKit::WebSocketHandle*, unsigned short code, const W
ebKit::WebString& reason) OVERRIDE; |
| 140 virtual void didReceiveFlowControl(WebKit::WebSocketHandle*, int64_t quota)
OVERRIDE { m_sendingQuota += quota; } |
| 141 |
| 142 // FileReaderLoaderClient functions. |
| 143 virtual void didStartLoading() OVERRIDE { } |
| 144 virtual void didReceiveData() OVERRIDE { } |
| 145 virtual void didFinishLoading() OVERRIDE; |
| 146 virtual void didFail(FileError::ErrorCode) OVERRIDE; |
| 147 |
| 148 void startLoadingBlob(const Blob&); |
| 149 // Calling this function may delete this object. |
| 150 void processPendingEvents(); |
| 151 |
| 152 // WebSocketChannel functions. |
| 153 virtual void refWebSocketChannel() OVERRIDE { ref(); } |
| 154 virtual void derefWebSocketChannel() OVERRIDE { deref(); } |
| 155 |
| 156 // m_context can be accessed when m_state != Closed. |
| 157 ScriptExecutionContext* m_context; |
| 158 WebSocketChannel* m_channel; |
| 159 OwnPtr<WebKit::WebSocketHandle> m_handle; |
| 160 WebSocketChannelClient* m_client; |
| 161 // An identifier which is used for identifying this WebSocketChannel |
| 162 // in devtools. |
| 163 // m_identifier == 0 means that we could not obtain a valid identifier. |
| 164 unsigned long m_identifier; |
| 165 KURL m_url; |
| 166 OwnPtr<FileReaderLoader> m_blobLoader; |
| 167 bool m_isLoadingBlob; |
| 168 Vector<Message> m_messages; |
| 169 Vector<PendingEvent> m_pendingEvents; |
| 170 Vector<char> m_receivingMessageData; |
| 171 |
| 172 State m_state; |
| 173 bool m_receivingMessageTypeIsText; |
| 174 int64_t m_sendingQuota; |
| 175 int64_t m_receivedDataSizeForFlowControl; |
| 176 unsigned long m_bufferedAmount; |
| 177 size_t m_sentSizeOfTopMessage; |
| 178 String m_subprotocol; |
| 179 String m_extensions; |
| 180 bool m_hasAlreadyFailed; |
| 181 bool m_isSuspended; |
| 182 String m_sourceURLAtConnection; |
| 183 int m_lineNumberAtConnection; |
| 184 |
| 185 static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 13; |
| 186 }; |
| 187 |
| 188 } // namespace WebCore |
| 189 |
| 190 #endif // NewWebSocketChannelImpl_h |
OLD | NEW |