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