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

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

Issue 23464050: Implement suspend / resume in NewWebSocketChannelImpl. (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
« no previous file with comments | « Source/modules/websockets/NewWebSocketChannelImpl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 26 matching lines...) Expand all
37 #include "core/platform/NotImplemented.h" 37 #include "core/platform/NotImplemented.h"
38 #include "modules/websockets/WebSocketChannel.h" 38 #include "modules/websockets/WebSocketChannel.h"
39 #include "modules/websockets/WebSocketChannelClient.h" 39 #include "modules/websockets/WebSocketChannelClient.h"
40 #include "public/platform/Platform.h" 40 #include "public/platform/Platform.h"
41 #include "public/platform/WebSocketHandle.h" 41 #include "public/platform/WebSocketHandle.h"
42 #include "public/platform/WebString.h" 42 #include "public/platform/WebString.h"
43 #include "public/platform/WebURL.h" 43 #include "public/platform/WebURL.h"
44 #include "public/platform/WebVector.h" 44 #include "public/platform/WebVector.h"
45 #include "weborigin/SecurityOrigin.h" 45 #include "weborigin/SecurityOrigin.h"
46 #include "wtf/ArrayBuffer.h" 46 #include "wtf/ArrayBuffer.h"
47 #include "wtf/Vector.h"
47 #include "wtf/text/WTFString.h" 48 #include "wtf/text/WTFString.h"
48 49
49 // FIXME: We should implement Inspector notification. 50 // FIXME: We should implement Inspector notification.
50 // FIXME: We should implement send(Blob). 51 // FIXME: We should implement send(Blob).
51 // FIXME: We should implement suspend / resume.
52 // FIXME: We should add log messages. 52 // FIXME: We should add log messages.
53 53
54 using WebKit::WebSocketHandle; 54 using WebKit::WebSocketHandle;
55 55
56 namespace WebCore { 56 namespace WebCore {
57 57
58 namespace { 58 namespace {
59 59
60 bool isClean(int code) 60 bool isClean(int code)
61 { 61 {
62 return code == WebSocketChannel::CloseEventCodeNormalClosure 62 return code == WebSocketChannel::CloseEventCodeNormalClosure
63 || (WebSocketChannel::CloseEventCodeMinimumUserDefined <= code 63 || (WebSocketChannel::CloseEventCodeMinimumUserDefined <= code
64 && code <= WebSocketChannel::CloseEventCodeMaximumUserDefined); 64 && code <= WebSocketChannel::CloseEventCodeMaximumUserDefined);
65 } 65 }
66 66
67 } // namespace 67 } // namespace
68 68
69 class NewWebSocketChannelImpl::Resumer {
70 public:
71 enum State {
72 Active,
tyoshino (SeeGerritForStatus) 2013/09/10 09:39:29 could you consider moving state to the channel? i
yhirano 2013/09/10 12:25:17 I divided the state to two booleans: NewWebSocketC
73 Suspended,
74 Resuming,
75 Aborted,
76 };
77
78 struct PendingEvent {
79 enum Type {
80 DidConnectComplete,
81 DidReceiveTextMessage,
82 DidReceiveBinaryMessage,
83 DidReceiveError,
84 DidClose,
85 };
86 Type type;
87 Vector<char> message; // for DidReceiveTextMessage / DidReceiveBinaryMes sage
88 int closingCode; // for DidClose
89 String closingReason; // for DidClose
90
91 PendingEvent(Type type) : type(type), closingCode(0) { }
92 PendingEvent(Type, Vector<char>*);
93 PendingEvent(int code, const String& reason) : type(DidClose), closingCo de(code), closingReason(reason) { }
94 };
95
96 Resumer(NewWebSocketChannelImpl*);
97 ~Resumer();
98
99 State state() const { return m_state; }
100 void append(const PendingEvent&);
101 void suspend();
102 void resumeLater();
103 void abort();
104
105 private:
106 class PendingEventProcessor : public RefCounted<PendingEventProcessor> {
107 public:
108 PendingEventProcessor() : m_isDetached(false) { }
109 virtual ~PendingEventProcessor() { }
110 void detach() { m_isDetached = true; }
111 void append(const PendingEvent& e) { m_events.append(e); }
112 void process(NewWebSocketChannelImpl*);
113
114 private:
115 bool m_isDetached;
116 Vector<PendingEvent> m_events;
117 };
118
119 void resumeNow(Timer<Resumer>*);
120
121 State m_state;
122 NewWebSocketChannelImpl* m_channel;
123 RefPtr<PendingEventProcessor> m_pendingEventProcessor;
124 Timer<Resumer> m_timer;
125 };
126
127 NewWebSocketChannelImpl::Resumer::PendingEvent::PendingEvent(Type type, Vector<c har>* data) : type(type), closingCode(0)
128 {
129 ASSERT(type == DidReceiveTextMessage || type == DidReceiveBinaryMessage);
130 message.swap(*data);
131 }
132
133 NewWebSocketChannelImpl::Resumer::Resumer(NewWebSocketChannelImpl* channel)
134 : m_state(Active)
135 , m_channel(channel)
136 , m_pendingEventProcessor(adoptRef(new PendingEventProcessor))
137 , m_timer(this, &Resumer::resumeNow) { }
138
139 NewWebSocketChannelImpl::Resumer::~Resumer()
140 {
141 abort();
142 }
143
144 void NewWebSocketChannelImpl::Resumer::append(const PendingEvent& event)
145 {
146 if (m_state == Aborted)
147 return;
148 m_pendingEventProcessor->append(event);
149 }
150
151 void NewWebSocketChannelImpl::Resumer::suspend()
152 {
153 if (m_state == Aborted)
154 return;
155 m_timer.stop();
156 m_state = Suspended;
157 }
158
159 void NewWebSocketChannelImpl::Resumer::resumeLater()
160 {
161 if (m_state == Aborted)
162 return;
163 m_state = Resuming;
164 if (!m_timer.isActive())
165 m_timer.startOneShot(0);
166 }
167
168 void NewWebSocketChannelImpl::Resumer::abort()
169 {
170 if (m_state == Aborted)
171 return;
172 m_state = Aborted;
173 m_timer.stop();
174 m_pendingEventProcessor->detach();
175 m_pendingEventProcessor = 0;
176 }
177
178 void NewWebSocketChannelImpl::Resumer::resumeNow(Timer<Resumer>*)
179 {
180 ASSERT(m_state == Resuming);
181 m_state = Active;
182
183 ASSERT(m_channel->m_client);
184 if (m_channel->m_handle) {
185 m_channel->sendInternal();
186 m_channel->flowControlIfNecessary();
187 }
188 m_pendingEventProcessor->process(m_channel);
189 // |this| can be aborted here.
190 // |this| can be deleted here.
191 }
192
193 void NewWebSocketChannelImpl::Resumer::PendingEventProcessor::process(NewWebSock etChannelImpl* channel)
194 {
195 RefPtr<PendingEventProcessor> protect(this);
196 for (size_t i = 0; i < m_events.size() && !m_isDetached; ++i) {
197 PendingEvent& event = m_events[i];
198 switch (event.type) {
199 case PendingEvent::DidConnectComplete:
200 channel->handleDidConnect();
201 // |this| can be detached here.
202 break;
203 case PendingEvent::DidReceiveTextMessage:
204 channel->handleTextMessage(&event.message);
205 // |this| can be detached here.
206 break;
207 case PendingEvent::DidReceiveBinaryMessage:
208 channel->handleBinaryMessage(&event.message);
209 // |this| can be detached here.
210 break;
211 case PendingEvent::DidReceiveError:
212 channel->handleDidReceiveMessageError();
213 // |this| can be detached here.
214 break;
215 case PendingEvent::DidClose:
216 channel->handleDidClose(event.closingCode, event.closingReason);
217 // |this| can be detached here.
218 break;
219 }
220 }
221 m_events.clear();
222 }
223
69 NewWebSocketChannelImpl::NewWebSocketChannelImpl(ScriptExecutionContext* context , WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber) 224 NewWebSocketChannelImpl::NewWebSocketChannelImpl(ScriptExecutionContext* context , WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber)
70 : ContextLifecycleObserver(context) 225 : ContextLifecycleObserver(context)
71 , m_handle(adoptPtr(WebKit::Platform::current()->createWebSocketHandle())) 226 , m_handle(adoptPtr(WebKit::Platform::current()->createWebSocketHandle()))
72 , m_client(client) 227 , m_client(client)
228 , m_resumer(adoptPtr(new Resumer(this)))
73 , m_sendingQuota(0) 229 , m_sendingQuota(0)
74 , m_receivedDataSizeForFlowControl(receivedDataSizeForFlowControlHighWaterMa rk * 2) // initial quota 230 , m_receivedDataSizeForFlowControl(receivedDataSizeForFlowControlHighWaterMa rk * 2) // initial quota
75 , m_bufferedAmount(0) 231 , m_bufferedAmount(0)
76 , m_sentSizeOfTopMessage(0) 232 , m_sentSizeOfTopMessage(0)
77 { 233 {
78 } 234 }
79 235
236 NewWebSocketChannelImpl::~NewWebSocketChannelImpl()
237 {
238 }
239
80 void NewWebSocketChannelImpl::connect(const KURL& url, const String& protocol) 240 void NewWebSocketChannelImpl::connect(const KURL& url, const String& protocol)
81 { 241 {
82 if (!m_handle) 242 if (!m_handle)
83 return; 243 return;
84 m_url = url; 244 m_url = url;
85 Vector<String> protocols; 245 Vector<String> protocols;
86 // Since protocol is already verified and escaped, we can simply split it. 246 // Since protocol is already verified and escaped, we can simply split it.
87 protocol.split(", ", true, protocols); 247 protocol.split(", ", true, protocols);
88 WebKit::WebVector<WebKit::WebString> webProtocols(protocols.size()); 248 WebKit::WebVector<WebKit::WebString> webProtocols(protocols.size());
89 for (size_t i = 0; i < protocols.size(); ++i) { 249 for (size_t i = 0; i < protocols.size(); ++i) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 { 295 {
136 ASSERT(m_handle); 296 ASSERT(m_handle);
137 m_handle->close(static_cast<unsigned short>(code), reason); 297 m_handle->close(static_cast<unsigned short>(code), reason);
138 } 298 }
139 299
140 void NewWebSocketChannelImpl::fail(const String& reason, MessageLevel level, con st String& sourceURL, unsigned lineNumber) 300 void NewWebSocketChannelImpl::fail(const String& reason, MessageLevel level, con st String& sourceURL, unsigned lineNumber)
141 { 301 {
142 // m_handle and m_client can be null here. 302 // m_handle and m_client can be null here.
143 if (m_client) 303 if (m_client)
144 m_client->didReceiveMessageError(); 304 m_client->didReceiveMessageError();
145 handleDidClose(CloseEventCodeAbnormalClosure, reason); 305 if (m_resumer->state() == Resumer::Active) {
146 // handleDidClose may delete this object. 306 handleDidClose(CloseEventCodeAbnormalClosure, reason);
307 // handleDidClose may delete this object.
308 } else {
309 m_resumer->append(Resumer::PendingEvent(CloseEventCodeAbnormalClosure, r eason));
310 }
147 } 311 }
148 312
149 void NewWebSocketChannelImpl::disconnect() 313 void NewWebSocketChannelImpl::disconnect()
150 { 314 {
151 if (m_handle) 315 if (m_handle)
152 m_handle->close(CloseEventCodeAbnormalClosure, ""); 316 m_handle->close(CloseEventCodeAbnormalClosure, "");
153 m_handle.clear(); 317 m_handle.clear();
154 m_client = 0; 318 m_client = 0;
319 m_resumer->abort();
155 } 320 }
156 321
157 void NewWebSocketChannelImpl::suspend() 322 void NewWebSocketChannelImpl::suspend()
158 { 323 {
159 notImplemented(); 324 m_resumer->suspend();
160 } 325 }
161 326
162 void NewWebSocketChannelImpl::resume() 327 void NewWebSocketChannelImpl::resume()
163 { 328 {
164 notImplemented(); 329 m_resumer->resumeLater();
165 } 330 }
166 331
167 NewWebSocketChannelImpl::Message::Message(const String& text) 332 NewWebSocketChannelImpl::Message::Message(const String& text)
168 : type(MessageTypeText) 333 : type(MessageTypeText)
169 , text(text.utf8(String::StrictConversionReplacingUnpairedSurrogatesWithFFFD )) { } 334 , text(text.utf8(String::StrictConversionReplacingUnpairedSurrogatesWithFFFD )) { }
170 335
171 NewWebSocketChannelImpl::Message::Message(const Blob& blob) 336 NewWebSocketChannelImpl::Message::Message(const Blob& blob)
172 : type(MessageTypeBlob) 337 : type(MessageTypeBlob)
173 , blob(Blob::create(blob.url(), blob.type(), blob.size())) { } 338 , blob(Blob::create(blob.url(), blob.type(), blob.size())) { }
174 339
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 { 390 {
226 if (!m_handle || m_receivedDataSizeForFlowControl < receivedDataSizeForFlowC ontrolHighWaterMark) { 391 if (!m_handle || m_receivedDataSizeForFlowControl < receivedDataSizeForFlowC ontrolHighWaterMark) {
227 return; 392 return;
228 } 393 }
229 m_handle->flowControl(m_receivedDataSizeForFlowControl); 394 m_handle->flowControl(m_receivedDataSizeForFlowControl);
230 m_receivedDataSizeForFlowControl = 0; 395 m_receivedDataSizeForFlowControl = 0;
231 } 396 }
232 397
233 void NewWebSocketChannelImpl::didConnect(WebSocketHandle* handle, bool succeed, const WebKit::WebString& selectedProtocol, const WebKit::WebString& extensions) 398 void NewWebSocketChannelImpl::didConnect(WebSocketHandle* handle, bool succeed, const WebKit::WebString& selectedProtocol, const WebKit::WebString& extensions)
234 { 399 {
235 if (!m_handle) { 400 ASSERT(m_handle);
236 return;
237 }
238 ASSERT(handle == m_handle); 401 ASSERT(handle == m_handle);
239 ASSERT(m_client); 402 ASSERT(m_client);
240 if (!succeed) { 403 if (!succeed) {
241 failAsError("Cannot connect to " + m_url.string() + "."); 404 failAsError("Cannot connect to " + m_url.string() + ".");
242 // failAsError may delete this object. 405 // failAsError may delete this object.
243 return; 406 return;
244 } 407 }
245 m_subprotocol = selectedProtocol; 408 m_subprotocol = selectedProtocol;
246 m_extensions = extensions; 409 m_extensions = extensions;
247 m_client->didConnect(); 410 if (m_resumer->state() == Resumer::Active)
411 m_client->didConnect();
412 else
413 m_resumer->append(Resumer::PendingEvent(Resumer::PendingEvent::DidConnec tComplete));
248 } 414 }
249 415
250 void NewWebSocketChannelImpl::didReceiveData(WebSocketHandle* handle, WebSocketH andle::MessageType type, const char* data, size_t size, bool fin) 416 void NewWebSocketChannelImpl::didReceiveData(WebSocketHandle* handle, WebSocketH andle::MessageType type, const char* data, size_t size, bool fin)
251 { 417 {
252 if (!m_handle) { 418 ASSERT(m_handle);
253 return;
254 }
255 ASSERT(handle == m_handle); 419 ASSERT(handle == m_handle);
256 ASSERT(m_client); 420 ASSERT(m_client);
257 // Non-final frames cannot be empty. 421 // Non-final frames cannot be empty.
258 ASSERT(fin || size); 422 ASSERT(fin || size);
259 switch (type) { 423 switch (type) {
260 case WebSocketHandle::MessageTypeText: 424 case WebSocketHandle::MessageTypeText:
261 ASSERT(m_receivingMessageData.isEmpty()); 425 ASSERT(m_receivingMessageData.isEmpty());
262 m_receivingMessageTypeIsText = true; 426 m_receivingMessageTypeIsText = true;
263 break; 427 break;
264 case WebSocketHandle::MessageTypeBinary: 428 case WebSocketHandle::MessageTypeBinary:
265 ASSERT(m_receivingMessageData.isEmpty()); 429 ASSERT(m_receivingMessageData.isEmpty());
266 m_receivingMessageTypeIsText = false; 430 m_receivingMessageTypeIsText = false;
267 break; 431 break;
268 case WebSocketHandle::MessageTypeContinuation: 432 case WebSocketHandle::MessageTypeContinuation:
269 ASSERT(!m_receivingMessageData.isEmpty()); 433 ASSERT(!m_receivingMessageData.isEmpty());
270 break; 434 break;
271 } 435 }
272 m_receivingMessageData.append(data, size); 436 m_receivingMessageData.append(data, size);
273 m_receivedDataSizeForFlowControl += size; 437 m_receivedDataSizeForFlowControl += size;
274 flowControlIfNecessary(); 438 flowControlIfNecessary();
275 if (!fin) { 439 if (!fin) {
276 return; 440 return;
277 } 441 }
442 if (m_resumer->state() != Resumer::Active) {
443 Resumer::PendingEvent::Type type =
444 m_receivingMessageTypeIsText ? Resumer::PendingEvent::DidReceiveText Message : Resumer::PendingEvent::DidReceiveBinaryMessage;
445 m_resumer->append(Resumer::PendingEvent(type, &m_receivingMessageData));
446 return;
447 }
278 Vector<char> messageData; 448 Vector<char> messageData;
279 messageData.swap(m_receivingMessageData); 449 messageData.swap(m_receivingMessageData);
280 if (m_receivingMessageTypeIsText) { 450 if (m_receivingMessageTypeIsText) {
281 handleTextMessage(&messageData); 451 handleTextMessage(&messageData);
282 // handleTextMessage may delete this object. 452 // handleTextMessage may delete this object.
283 } else { 453 } else {
284 handleBinaryMessage(&messageData); 454 handleBinaryMessage(&messageData);
285 } 455 }
286 } 456 }
287 457
288 458
289 void NewWebSocketChannelImpl::didClose(WebSocketHandle* handle, unsigned short c ode, const WebKit::WebString& reason) 459 void NewWebSocketChannelImpl::didClose(WebSocketHandle* handle, unsigned short c ode, const WebKit::WebString& reason)
290 { 460 {
461 ASSERT(m_handle);
462 m_handle.clear();
291 // FIXME: Maybe we should notify an error to m_client for some didClose mess ages. 463 // FIXME: Maybe we should notify an error to m_client for some didClose mess ages.
292 handleDidClose(code, reason); 464 if (m_resumer->state() == Resumer::Active) {
293 // handleDidClose may delete this object. 465 handleDidClose(code, reason);
466 // handleDidClose may delete this object.
467 } else {
468 m_resumer->append(Resumer::PendingEvent(code, reason));
469 }
470 }
471
472 void NewWebSocketChannelImpl::handleDidConnect()
473 {
474 ASSERT(m_client);
475 ASSERT(m_resumer->state() == Resumer::Active);
476 m_client->didConnect();
294 } 477 }
295 478
296 void NewWebSocketChannelImpl::handleTextMessage(Vector<char>* messageData) 479 void NewWebSocketChannelImpl::handleTextMessage(Vector<char>* messageData)
297 { 480 {
298 ASSERT(m_client); 481 ASSERT(m_client);
482 ASSERT(m_resumer->state() == Resumer::Active);
299 ASSERT(messageData); 483 ASSERT(messageData);
300 String message = ""; 484 String message = "";
301 if (m_receivingMessageData.size() > 0) { 485 if (m_receivingMessageData.size() > 0) {
302 message = String::fromUTF8(messageData->data(), messageData->size()); 486 message = String::fromUTF8(messageData->data(), messageData->size());
303 } 487 }
304 // For consistency with handleBinaryMessage, we clear |messageData|. 488 // For consistency with handleBinaryMessage, we clear |messageData|.
305 messageData->clear(); 489 messageData->clear();
306 if (message.isNull()) { 490 if (message.isNull()) {
307 failAsError("Could not decode a text frame as UTF-8."); 491 failAsError("Could not decode a text frame as UTF-8.");
308 // failAsError may delete this object. 492 // failAsError may delete this object.
309 } else { 493 } else {
310 m_client->didReceiveMessage(message); 494 m_client->didReceiveMessage(message);
311 } 495 }
312 } 496 }
313 497
314 void NewWebSocketChannelImpl::handleBinaryMessage(Vector<char>* messageData) 498 void NewWebSocketChannelImpl::handleBinaryMessage(Vector<char>* messageData)
315 { 499 {
316 ASSERT(m_client); 500 ASSERT(m_client);
501 ASSERT(m_resumer->state() == Resumer::Active);
317 ASSERT(messageData); 502 ASSERT(messageData);
318 OwnPtr<Vector<char> > binaryData = adoptPtr(new Vector<char>); 503 OwnPtr<Vector<char> > binaryData = adoptPtr(new Vector<char>);
319 messageData->swap(*binaryData); 504 messageData->swap(*binaryData);
320 m_client->didReceiveBinaryData(binaryData.release()); 505 m_client->didReceiveBinaryData(binaryData.release());
321 } 506 }
322 507
508 void NewWebSocketChannelImpl::handleDidReceiveMessageError()
509 {
510 ASSERT(m_client);
511 ASSERT(m_resumer->state() == Resumer::Active);
512 m_client->didReceiveMessageError();
513 }
514
323 void NewWebSocketChannelImpl::handleDidClose(unsigned short code, const String& reason) 515 void NewWebSocketChannelImpl::handleDidClose(unsigned short code, const String& reason)
324 { 516 {
517 ASSERT(m_resumer->state() == Resumer::Active);
325 m_handle.clear(); 518 m_handle.clear();
519 m_resumer->abort();
326 if (!m_client) { 520 if (!m_client) {
327 return; 521 return;
328 } 522 }
329 WebSocketChannelClient* client = m_client; 523 WebSocketChannelClient* client = m_client;
330 m_client = 0; 524 m_client = 0;
331 WebSocketChannelClient::ClosingHandshakeCompletionStatus status = 525 WebSocketChannelClient::ClosingHandshakeCompletionStatus status =
332 isClean(code) ? WebSocketChannelClient::ClosingHandshakeComplete : WebSo cketChannelClient::ClosingHandshakeIncomplete; 526 isClean(code) ? WebSocketChannelClient::ClosingHandshakeComplete : WebSo cketChannelClient::ClosingHandshakeIncomplete;
333 client->didClose(m_bufferedAmount, status, code, reason); 527 client->didClose(m_bufferedAmount, status, code, reason);
334 // client->didClose may delete this object. 528 // client->didClose may delete this object.
335 } 529 }
336 530
337 } // namespace WebCore 531 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/websockets/NewWebSocketChannelImpl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698