| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 WTF_MAKE_FAST_ALLOCATED; | 56 WTF_MAKE_FAST_ALLOCATED; |
| 57 public: | 57 public: |
| 58 // You can specify the source file and the line number information | 58 // You can specify the source file and the line number information |
| 59 // explicitly by passing the last parameter. | 59 // explicitly by passing the last parameter. |
| 60 // In the usual case, they are set automatically and you don't have to | 60 // In the usual case, they are set automatically and you don't have to |
| 61 // pass it. | 61 // pass it. |
| 62 static PassRefPtr<NewWebSocketChannelImpl> create(ScriptExecutionContext* co
ntext, WebSocketChannelClient* client, const String& sourceURL = String(), unsig
ned lineNumber = 0) | 62 static PassRefPtr<NewWebSocketChannelImpl> create(ScriptExecutionContext* co
ntext, WebSocketChannelClient* client, const String& sourceURL = String(), unsig
ned lineNumber = 0) |
| 63 { | 63 { |
| 64 return adoptRef(new NewWebSocketChannelImpl(context, client, sourceURL,
lineNumber)); | 64 return adoptRef(new NewWebSocketChannelImpl(context, client, sourceURL,
lineNumber)); |
| 65 } | 65 } |
| 66 virtual ~NewWebSocketChannelImpl() { } | 66 virtual ~NewWebSocketChannelImpl(); |
| 67 | 67 |
| 68 // WebSocketChannel functions. | 68 // WebSocketChannel functions. |
| 69 virtual void connect(const KURL&, const String& protocol) OVERRIDE; | 69 virtual void connect(const KURL&, const String& protocol) OVERRIDE; |
| 70 virtual String subprotocol() OVERRIDE; | 70 virtual String subprotocol() OVERRIDE; |
| 71 virtual String extensions() OVERRIDE; | 71 virtual String extensions() OVERRIDE; |
| 72 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; | 72 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; |
| 73 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; | 73 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; |
| 74 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; | 74 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; |
| 75 virtual unsigned long bufferedAmount() const OVERRIDE; | 75 virtual unsigned long bufferedAmount() const OVERRIDE; |
| 76 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code | 76 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code |
| 77 // argument to omit payload. | 77 // argument to omit payload. |
| 78 virtual void close(int code, const String& reason) OVERRIDE; | 78 virtual void close(int code, const String& reason) OVERRIDE; |
| 79 virtual void fail(const String& reason, MessageLevel, const String&, unsigne
d lineNumber) OVERRIDE; | 79 virtual void fail(const String& reason, MessageLevel, const String&, unsigne
d lineNumber) OVERRIDE; |
| 80 using WebSocketChannel::fail; | 80 using WebSocketChannel::fail; |
| 81 virtual void disconnect() OVERRIDE; | 81 virtual void disconnect() OVERRIDE; |
| 82 | 82 |
| 83 using RefCounted<NewWebSocketChannelImpl>::ref; | 83 using RefCounted<NewWebSocketChannelImpl>::ref; |
| 84 using RefCounted<NewWebSocketChannelImpl>::deref; | 84 using RefCounted<NewWebSocketChannelImpl>::deref; |
| 85 | 85 |
| 86 virtual void suspend() OVERRIDE; | 86 virtual void suspend() OVERRIDE; |
| 87 virtual void resume() OVERRIDE; | 87 virtual void resume() OVERRIDE; |
| 88 | 88 |
| 89 private: | 89 private: |
| 90 enum MessageType { | 90 enum MessageType { |
| 91 MessageTypeText, | 91 MessageTypeText, |
| 92 MessageTypeBlob, | 92 MessageTypeBlob, |
| 93 MessageTypeArrayBuffer, | 93 MessageTypeArrayBuffer, |
| 94 }; | 94 }; |
| 95 |
| 95 struct Message { | 96 struct Message { |
| 96 explicit Message(const String&); | 97 explicit Message(const String&); |
| 97 explicit Message(const Blob&); | 98 explicit Message(const Blob&); |
| 98 explicit Message(PassRefPtr<ArrayBuffer>); | 99 explicit Message(PassRefPtr<ArrayBuffer>); |
| 99 MessageType type; | 100 MessageType type; |
| 100 CString text; | 101 CString text; |
| 101 RefPtr<Blob> blob; | 102 RefPtr<Blob> blob; |
| 102 RefPtr<ArrayBuffer> arrayBuffer; | 103 RefPtr<ArrayBuffer> arrayBuffer; |
| 103 }; | 104 }; |
| 104 | 105 |
| 105 struct ReceivedMessage { | 106 struct ReceivedMessage { |
| 106 bool isMessageText; | 107 bool isMessageText; |
| 107 Vector<char> data; | 108 Vector<char> data; |
| 108 }; | 109 }; |
| 109 | 110 |
| 111 class Resumer; |
| 112 |
| 110 NewWebSocketChannelImpl(ScriptExecutionContext*, WebSocketChannelClient*, co
nst String&, unsigned); | 113 NewWebSocketChannelImpl(ScriptExecutionContext*, WebSocketChannelClient*, co
nst String&, unsigned); |
| 111 void sendInternal(); | 114 void sendInternal(); |
| 112 void flowControlIfNecessary(); | 115 void flowControlIfNecessary(); |
| 113 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, "",
0); } | 116 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, "",
0); } |
| 114 | 117 |
| 115 // WebSocketHandleClient functions. | 118 // WebSocketHandleClient functions. |
| 116 virtual void didConnect(WebKit::WebSocketHandle*, bool succeed, const WebKit
::WebString& selectedProtocol, const WebKit::WebString& extensions) OVERRIDE; | 119 virtual void didConnect(WebKit::WebSocketHandle*, bool succeed, const WebKit
::WebString& selectedProtocol, const WebKit::WebString& extensions) OVERRIDE; |
| 117 virtual void didReceiveData(WebKit::WebSocketHandle*, WebKit::WebSocketHandl
e::MessageType, const char* data, size_t /* size */, bool fin) OVERRIDE; | 120 virtual void didReceiveData(WebKit::WebSocketHandle*, WebKit::WebSocketHandl
e::MessageType, const char* data, size_t /* size */, bool fin) OVERRIDE; |
| 118 virtual void didClose(WebKit::WebSocketHandle*, unsigned short code, const W
ebKit::WebString& reason) OVERRIDE; | 121 virtual void didClose(WebKit::WebSocketHandle*, unsigned short code, const W
ebKit::WebString& reason) OVERRIDE; |
| 119 virtual void didReceiveFlowControl(WebKit::WebSocketHandle*, int64_t quota)
OVERRIDE { m_sendingQuota += quota; } | 122 virtual void didReceiveFlowControl(WebKit::WebSocketHandle*, int64_t quota)
OVERRIDE { m_sendingQuota += quota; } |
| 120 | 123 |
| 124 void handleDidConnect(); |
| 121 void handleTextMessage(Vector<char>*); | 125 void handleTextMessage(Vector<char>*); |
| 122 void handleBinaryMessage(Vector<char>*); | 126 void handleBinaryMessage(Vector<char>*); |
| 127 void handleDidReceiveMessageError(); |
| 123 void handleDidClose(unsigned short code, const String& reason); | 128 void handleDidClose(unsigned short code, const String& reason); |
| 124 | 129 |
| 125 // WebSocketChannel functions. | 130 // WebSocketChannel functions. |
| 126 virtual void refWebSocketChannel() OVERRIDE { ref(); } | 131 virtual void refWebSocketChannel() OVERRIDE { ref(); } |
| 127 virtual void derefWebSocketChannel() OVERRIDE { deref(); } | 132 virtual void derefWebSocketChannel() OVERRIDE { deref(); } |
| 128 | 133 |
| 129 // LifecycleObserver functions. | 134 // LifecycleObserver functions. |
| 130 // This object must be destroyed before the context. | 135 // This object must be destroyed before the context. |
| 131 virtual void contextDestroyed() OVERRIDE { ASSERT_NOT_REACHED(); } | 136 virtual void contextDestroyed() OVERRIDE { ASSERT_NOT_REACHED(); } |
| 132 | 137 |
| 133 // m_handle is a handle of the connection. | 138 // m_handle is a handle of the connection. |
| 134 // m_handle == 0 means this channel is closed. | 139 // m_handle == 0 means this channel is closed. |
| 135 OwnPtr<WebKit::WebSocketHandle> m_handle; | 140 OwnPtr<WebKit::WebSocketHandle> m_handle; |
| 136 | 141 |
| 137 // m_client can be deleted while this channel is alive, but this class | 142 // m_client can be deleted while this channel is alive, but this class |
| 138 // expects that disconnect() is called before the deletion. | 143 // expects that disconnect() is called before the deletion. |
| 139 WebSocketChannelClient* m_client; | 144 WebSocketChannelClient* m_client; |
| 140 KURL m_url; | 145 KURL m_url; |
| 146 OwnPtr<Resumer> m_resumer; |
| 147 bool m_isSuspended; |
| 141 Deque<Message> m_messages; | 148 Deque<Message> m_messages; |
| 142 Vector<char> m_receivingMessageData; | 149 Vector<char> m_receivingMessageData; |
| 143 | 150 |
| 144 bool m_receivingMessageTypeIsText; | 151 bool m_receivingMessageTypeIsText; |
| 145 int64_t m_sendingQuota; | 152 int64_t m_sendingQuota; |
| 146 int64_t m_receivedDataSizeForFlowControl; | 153 int64_t m_receivedDataSizeForFlowControl; |
| 147 unsigned long m_bufferedAmount; | 154 unsigned long m_bufferedAmount; |
| 148 size_t m_sentSizeOfTopMessage; | 155 size_t m_sentSizeOfTopMessage; |
| 149 String m_subprotocol; | 156 String m_subprotocol; |
| 150 String m_extensions; | 157 String m_extensions; |
| 151 | 158 |
| 152 static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15; | 159 static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15; |
| 153 }; | 160 }; |
| 154 | 161 |
| 155 } // namespace WebCore | 162 } // namespace WebCore |
| 156 | 163 |
| 157 #endif // NewWebSocketChannelImpl_h | 164 #endif // NewWebSocketChannelImpl_h |
| OLD | NEW |