| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef MainThreadWebSocketChannel_h | 31 #ifndef MainThreadWebSocketChannel_h |
| 32 #define MainThreadWebSocketChannel_h | 32 #define MainThreadWebSocketChannel_h |
| 33 | 33 |
| 34 #include "core/fileapi/FileReaderLoaderClient.h" | 34 #include "core/fileapi/FileReaderLoaderClient.h" |
| 35 #include "core/inspector/ScriptCallFrame.h" | 35 #include "core/inspector/ScriptCallFrame.h" |
| 36 #include "core/page/ConsoleTypes.h" |
| 36 #include "core/platform/Timer.h" | 37 #include "core/platform/Timer.h" |
| 37 #include "core/platform/network/SocketStreamHandleClient.h" | 38 #include "core/platform/network/SocketStreamHandleClient.h" |
| 38 #include "modules/websockets/WebSocketChannel.h" | 39 #include "modules/websockets/WebSocketChannel.h" |
| 39 #include "modules/websockets/WebSocketDeflateFramer.h" | 40 #include "modules/websockets/WebSocketDeflateFramer.h" |
| 40 #include "modules/websockets/WebSocketFrame.h" | 41 #include "modules/websockets/WebSocketFrame.h" |
| 41 #include "modules/websockets/WebSocketHandshake.h" | 42 #include "modules/websockets/WebSocketHandshake.h" |
| 42 #include "modules/websockets/WebSocketPerMessageDeflate.h" | 43 #include "modules/websockets/WebSocketPerMessageDeflate.h" |
| 43 #include "wtf/Deque.h" | 44 #include "wtf/Deque.h" |
| 44 #include "wtf/Forward.h" | 45 #include "wtf/Forward.h" |
| 46 #include "wtf/PassOwnPtr.h" |
| 45 #include "wtf/RefCounted.h" | 47 #include "wtf/RefCounted.h" |
| 46 #include "wtf/Vector.h" | 48 #include "wtf/Vector.h" |
| 47 #include "wtf/text/CString.h" | 49 #include "wtf/text/CString.h" |
| 48 | 50 |
| 49 namespace WebCore { | 51 namespace WebCore { |
| 50 | 52 |
| 51 class Blob; | 53 class Blob; |
| 52 class Document; | 54 class Document; |
| 53 class FileReaderLoader; | 55 class FileReaderLoader; |
| 54 class SocketStreamHandle; | 56 class SocketStreamHandle; |
| 55 class SocketStreamError; | 57 class SocketStreamError; |
| 56 class WebSocketChannelClient; | 58 class WebSocketChannelClient; |
| 57 | 59 |
| 58 class MainThreadWebSocketChannel : public RefCounted<MainThreadWebSocketChannel>
, public SocketStreamHandleClient, public WebSocketChannel | 60 class MainThreadWebSocketChannel : public RefCounted<MainThreadWebSocketChannel>
, public SocketStreamHandleClient, public WebSocketChannel |
| 59 , public FileReaderLoaderClient { | 61 , public FileReaderLoaderClient { |
| 60 WTF_MAKE_FAST_ALLOCATED; | 62 WTF_MAKE_FAST_ALLOCATED; |
| 61 public: | 63 public: |
| 62 static PassRefPtr<MainThreadWebSocketChannel> create(Document* document, Web
SocketChannelClient* client) { return adoptRef(new MainThreadWebSocketChannel(do
cument, client)); } | 64 // You can specify the source file and the line number information |
| 65 // explicitly by passing the last parameter. |
| 66 // In the usual case, they are set automatically and you don't have to |
| 67 // pass it. |
| 68 static PassRefPtr<MainThreadWebSocketChannel> create(Document* document, Web
SocketChannelClient* client, const ScriptCallFrame& frame = ScriptCallFrame("",
"", 0)) { return adoptRef(new MainThreadWebSocketChannel(document, client, frame
)); } |
| 63 virtual ~MainThreadWebSocketChannel(); | 69 virtual ~MainThreadWebSocketChannel(); |
| 64 | 70 |
| 65 bool send(const char* data, int length); | 71 bool send(const char* data, int length); |
| 66 | 72 |
| 67 // WebSocketChannel functions. | 73 // WebSocketChannel functions. |
| 68 virtual void connect(const KURL&, const String& protocol) OVERRIDE; | 74 virtual void connect(const KURL&, const String& protocol) OVERRIDE; |
| 69 virtual String subprotocol() OVERRIDE; | 75 virtual String subprotocol() OVERRIDE; |
| 70 virtual String extensions() OVERRIDE; | 76 virtual String extensions() OVERRIDE; |
| 71 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; | 77 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; |
| 72 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; | 78 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; |
| 73 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; | 79 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; |
| 74 virtual unsigned long bufferedAmount() const OVERRIDE; | 80 virtual unsigned long bufferedAmount() const OVERRIDE; |
| 75 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code | 81 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code |
| 76 // argument to omit payload. | 82 // argument to omit payload. |
| 77 virtual void close(int code, const String& reason) OVERRIDE; | 83 virtual void close(int code, const String& reason) OVERRIDE; |
| 78 virtual void fail(const String& reason) OVERRIDE; | 84 virtual void fail(const String& reason, MessageLevel) OVERRIDE; |
| 85 virtual void fail(const String& reason, MessageLevel, PassOwnPtr<CallStackWr
apper>) OVERRIDE; |
| 79 virtual void disconnect() OVERRIDE; | 86 virtual void disconnect() OVERRIDE; |
| 80 | 87 |
| 81 virtual void suspend() OVERRIDE; | 88 virtual void suspend() OVERRIDE; |
| 82 virtual void resume() OVERRIDE; | 89 virtual void resume() OVERRIDE; |
| 83 | 90 |
| 84 // SocketStreamHandleClient functions. | 91 // SocketStreamHandleClient functions. |
| 85 virtual void willOpenSocketStream(SocketStreamHandle*) OVERRIDE; | 92 virtual void willOpenSocketStream(SocketStreamHandle*) OVERRIDE; |
| 86 virtual void didOpenSocketStream(SocketStreamHandle*) OVERRIDE; | 93 virtual void didOpenSocketStream(SocketStreamHandle*) OVERRIDE; |
| 87 virtual void didCloseSocketStream(SocketStreamHandle*) OVERRIDE; | 94 virtual void didCloseSocketStream(SocketStreamHandle*) OVERRIDE; |
| 88 virtual void didReceiveSocketStreamData(SocketStreamHandle*, const char*, in
t) OVERRIDE; | 95 virtual void didReceiveSocketStreamData(SocketStreamHandle*, const char*, in
t) OVERRIDE; |
| 89 virtual void didUpdateBufferedAmount(SocketStreamHandle*, size_t bufferedAmo
unt) OVERRIDE; | 96 virtual void didUpdateBufferedAmount(SocketStreamHandle*, size_t bufferedAmo
unt) OVERRIDE; |
| 90 virtual void didFailSocketStream(SocketStreamHandle*, const SocketStreamErro
r&) OVERRIDE; | 97 virtual void didFailSocketStream(SocketStreamHandle*, const SocketStreamErro
r&) OVERRIDE; |
| 91 | 98 |
| 92 // FileReaderLoaderClient functions. | 99 // FileReaderLoaderClient functions. |
| 93 virtual void didStartLoading(); | 100 virtual void didStartLoading(); |
| 94 virtual void didReceiveData(); | 101 virtual void didReceiveData(); |
| 95 virtual void didFinishLoading(); | 102 virtual void didFinishLoading(); |
| 96 virtual void didFail(int errorCode); | 103 virtual void didFail(int errorCode); |
| 97 | 104 |
| 98 using RefCounted<MainThreadWebSocketChannel>::ref; | 105 using RefCounted<MainThreadWebSocketChannel>::ref; |
| 99 using RefCounted<MainThreadWebSocketChannel>::deref; | 106 using RefCounted<MainThreadWebSocketChannel>::deref; |
| 100 | 107 |
| 101 protected: | 108 protected: |
| 102 // WebSocketChannel functions. | 109 // WebSocketChannel functions. |
| 103 virtual void refWebSocketChannel() OVERRIDE { ref(); } | 110 virtual void refWebSocketChannel() OVERRIDE { ref(); } |
| 104 virtual void derefWebSocketChannel() OVERRIDE { deref(); } | 111 virtual void derefWebSocketChannel() OVERRIDE { deref(); } |
| 105 | 112 |
| 106 private: | 113 private: |
| 107 MainThreadWebSocketChannel(Document*, WebSocketChannelClient*); | 114 MainThreadWebSocketChannel(Document*, WebSocketChannelClient*, const ScriptC
allFrame&); |
| 108 | 115 |
| 109 bool appendToBuffer(const char* data, size_t len); | 116 bool appendToBuffer(const char* data, size_t len); |
| 110 void skipBuffer(size_t len); | 117 void skipBuffer(size_t len); |
| 111 bool processBuffer(); | 118 bool processBuffer(); |
| 112 void resumeTimerFired(Timer<MainThreadWebSocketChannel>*); | 119 void resumeTimerFired(Timer<MainThreadWebSocketChannel>*); |
| 113 void startClosingHandshake(int code, const String& reason); | 120 void startClosingHandshake(int code, const String& reason); |
| 114 void closingTimerFired(Timer<MainThreadWebSocketChannel>*); | 121 void closingTimerFired(Timer<MainThreadWebSocketChannel>*); |
| 122 void failInternal(); |
| 115 | 123 |
| 116 bool processFrame(); | 124 bool processFrame(); |
| 117 | 125 |
| 118 // It is allowed to send a Blob as a binary frame if hybi-10 protocol is in
use. Sending a Blob | 126 // It is allowed to send a Blob as a binary frame if hybi-10 protocol is in
use. Sending a Blob |
| 119 // can be delayed because it must be read asynchronously. Other types of dat
a (String or | 127 // can be delayed because it must be read asynchronously. Other types of dat
a (String or |
| 120 // ArrayBuffer) may also be blocked by preceding sending request of a Blob. | 128 // ArrayBuffer) may also be blocked by preceding sending request of a Blob. |
| 121 // | 129 // |
| 122 // To address this situation, messages to be sent need to be stored in a que
ue. Whenever a new | 130 // To address this situation, messages to be sent need to be stored in a que
ue. Whenever a new |
| 123 // data frame is going to be sent, it first must go to the queue. Items in t
he queue are processed | 131 // data frame is going to be sent, it first must go to the queue. Items in t
he queue are processed |
| 124 // in the order they were put into the queue. Sending request of a Blob bloc
ks further processing | 132 // in the order they were put into the queue. Sending request of a Blob bloc
ks further processing |
| 125 // until the Blob is completely read and sent to the socket stream. | 133 // until the Blob is completely read and sent to the socket stream. |
| 126 enum QueuedFrameType { | 134 enum QueuedFrameType { |
| 127 QueuedFrameTypeString, | 135 QueuedFrameTypeString, |
| 128 QueuedFrameTypeVector, | 136 QueuedFrameTypeVector, |
| 129 QueuedFrameTypeBlob | 137 QueuedFrameTypeBlob |
| 130 }; | 138 }; |
| 131 struct QueuedFrame { | 139 struct QueuedFrame { |
| 132 WebSocketFrame::OpCode opCode; | 140 WebSocketFrame::OpCode opCode; |
| 133 QueuedFrameType frameType; | 141 QueuedFrameType frameType; |
| 134 // Only one of the following items is used, according to the value of fr
ameType. | 142 // Only one of the following items is used, according to the value of fr
ameType. |
| 135 CString stringData; | 143 CString stringData; |
| 136 Vector<char> vectorData; | 144 Vector<char> vectorData; |
| 137 RefPtr<Blob> blobData; | 145 RefPtr<Blob> blobData; |
| 138 }; | 146 }; |
| 139 void enqueueTextFrame(const CString&); | 147 void enqueueTextFrame(const CString&); |
| 140 void enqueueRawFrame(WebSocketFrame::OpCode, const char* data, size_t dataLe
ngth); | 148 void enqueueRawFrame(WebSocketFrame::OpCode, const char* data, size_t dataLe
ngth); |
| 141 void enqueueBlobFrame(WebSocketFrame::OpCode, const Blob&); | 149 void enqueueBlobFrame(WebSocketFrame::OpCode, const Blob&); |
| 142 | 150 |
| 151 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel); } |
| 143 void processOutgoingFrameQueue(); | 152 void processOutgoingFrameQueue(); |
| 144 void abortOutgoingFrameQueue(); | 153 void abortOutgoingFrameQueue(); |
| 145 | 154 |
| 146 enum OutgoingFrameQueueStatus { | 155 enum OutgoingFrameQueueStatus { |
| 147 // It is allowed to put a new item into the queue. | 156 // It is allowed to put a new item into the queue. |
| 148 OutgoingFrameQueueOpen, | 157 OutgoingFrameQueueOpen, |
| 149 // Close frame has already been put into the queue but may not have been
sent yet; | 158 // Close frame has already been put into the queue but may not have been
sent yet; |
| 150 // m_handle->close() will be called as soon as the queue is cleared. It
is not | 159 // m_handle->close() will be called as soon as the queue is cleared. It
is not |
| 151 // allowed to put a new item into the queue. | 160 // allowed to put a new item into the queue. |
| 152 OutgoingFrameQueueClosing, | 161 OutgoingFrameQueueClosing, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 BlobLoaderStatus m_blobLoaderStatus; | 209 BlobLoaderStatus m_blobLoaderStatus; |
| 201 | 210 |
| 202 ScriptCallFrame m_callFrameAtConnection; | 211 ScriptCallFrame m_callFrameAtConnection; |
| 203 WebSocketPerMessageDeflate m_perMessageDeflate; | 212 WebSocketPerMessageDeflate m_perMessageDeflate; |
| 204 WebSocketDeflateFramer m_deflateFramer; | 213 WebSocketDeflateFramer m_deflateFramer; |
| 205 }; | 214 }; |
| 206 | 215 |
| 207 } // namespace WebCore | 216 } // namespace WebCore |
| 208 | 217 |
| 209 #endif // MainThreadWebSocketChannel_h | 218 #endif // MainThreadWebSocketChannel_h |
| OLD | NEW |