Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(651)

Side by Side Diff: Source/modules/websockets/NewWebSocketChannelImpl.h

Issue 22914026: [ABANDONED] Introduce blink-side bridges for the new WebSocket implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 struct PendingEvent {
117 enum Type {
118 DidConnectComplete,
119 DidReceiveTextMessage,
120 DidReceiveBinaryMessage,
121 DidReceiveError,
122 DidClose,
123 };
124 Type type;
125 Vector<char> message; // for DidReceive*Message
126 int closingCode; // for DidClose
127 String closingReason; // for DidClose
128
129 PendingEvent(Type type) : type(type) { }
130 PendingEvent(int code, const String& reason) : type(DidClose), closingCo de(code), closingReason(reason) { }
131 };
132
133 NewWebSocketChannelImpl(ScriptExecutionContext*, WebSocketChannelClient*, co nst String&, unsigned);
134 void sendInternal();
135 void flowControlIfNecessary();
136 void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_s ourceURLAtConnection, m_lineNumberAtConnection); }
137
138 // WebSocketHandleClient functions.
139 virtual void didConnect(WebKit::WebSocketHandle*, bool succeed, const WebKit ::WebString& selectedProtocol, const WebKit::WebString& extensions) OVERRIDE;
140 virtual void didReceiveData(WebKit::WebSocketHandle*, WebKit::WebSocketHandl e::MessageType, const char* data, size_t /* size */, bool fin) OVERRIDE;
141 virtual void didClose(WebKit::WebSocketHandle*, unsigned short code, const W ebKit::WebString& reason) OVERRIDE;
142 virtual void didReceiveFlowControl(WebKit::WebSocketHandle*, int64_t quota) OVERRIDE { m_sendingQuota += quota; }
143
144 // FileReaderLoaderClient functions.
145 virtual void didStartLoading() OVERRIDE { }
146 virtual void didReceiveData() OVERRIDE { }
147 virtual void didFinishLoading() OVERRIDE;
148 virtual void didFail(FileError::ErrorCode) OVERRIDE;
149
150 void resumeTimerFired(Timer<NewWebSocketChannelImpl>*);
151 void handleTextMessage(Vector<char>*);
152 void handleBinaryMessage(Vector<char>*);
153 void startLoadingBlob(const Blob&);
154 // Calling this function may delete this object.
155 void processPendingEvents();
156
157 // WebSocketChannel functions.
158 virtual void refWebSocketChannel() OVERRIDE { ref(); }
159 virtual void derefWebSocketChannel() OVERRIDE { deref(); }
160
161 // m_context can be accessed when m_state != Closed.
162 ScriptExecutionContext* m_context;
163 WebSocketChannel* m_channel;
164 OwnPtr<WebKit::WebSocketHandle> m_handle;
165 WebSocketChannelClient* m_client;
166 // An identifier which is used for identifying this WebSocketChannel
167 // in devtools.
168 // m_identifier == 0 means that we could not obtain a valid identifier.
169 unsigned long m_identifier;
170 KURL m_url;
171 OwnPtr<FileReaderLoader> m_blobLoader;
172 bool m_isLoadingBlob;
173 Deque<Message> m_messages;
174 Vector<PendingEvent> m_pendingEvents;
175 Vector<char> m_receivingMessageData;
176
177 State m_state;
178 bool m_receivingMessageTypeIsText;
179 int64_t m_sendingQuota;
180 int64_t m_receivedDataSizeForFlowControl;
181 unsigned long m_bufferedAmount;
182 size_t m_sentSizeOfTopMessage;
183 String m_subprotocol;
184 String m_extensions;
185 bool m_hasAlreadyFailed;
tyoshino (SeeGerritForStatus) 2013/08/28 05:26:03 say that this is set when - already failed - or fa
yhirano 2013/08/28 06:29:09 m_hasAlreadyFailed is set when the channel is alre
186 bool m_isSuspended;
187 Timer<NewWebSocketChannelImpl> m_resumeTimer;
188 String m_sourceURLAtConnection;
189 int m_lineNumberAtConnection;
190
191 static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 13;
192 };
193
194 } // namespace WebCore
195
196 #endif // NewWebSocketChannelImpl_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698