OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_websocket_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_websocket_impl.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
14 #include "ppapi/c/pp_completion_callback.h" | 14 #include "ppapi/c/pp_completion_callback.h" |
15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
16 #include "ppapi/c/pp_var.h" | 16 #include "ppapi/c/pp_var.h" |
| 17 #include "ppapi/c/ppb_var.h" |
17 #include "ppapi/c/ppb_var_array_buffer.h" | 18 #include "ppapi/c/ppb_var_array_buffer.h" |
18 #include "ppapi/c/ppb_var.h" | |
19 #include "ppapi/shared_impl/var.h" | 19 #include "ppapi/shared_impl/var.h" |
20 #include "ppapi/shared_impl/var_tracker.h" | 20 #include "ppapi/shared_impl/var_tracker.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBuffer.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBuffer.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocket.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocket.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
28 #include "webkit/plugins/ppapi/host_array_buffer_var.h" | 28 #include "webkit/plugins/ppapi/host_array_buffer_var.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 uint64_t GetFrameSize(uint64_t payload_size) { | 61 uint64_t GetFrameSize(uint64_t payload_size) { |
62 uint64_t overhead = kHybiBaseFramingOverhead + kHybiMaskingKeyLength; | 62 uint64_t overhead = kHybiBaseFramingOverhead + kHybiMaskingKeyLength; |
63 if (payload_size > kMinimumPayloadSizeWithEightByteExtendedPayloadLength) | 63 if (payload_size > kMinimumPayloadSizeWithEightByteExtendedPayloadLength) |
64 overhead += 8; | 64 overhead += 8; |
65 else if (payload_size > kMinimumPayloadSizeWithTwoByteExtendedPayloadLength) | 65 else if (payload_size > kMinimumPayloadSizeWithTwoByteExtendedPayloadLength) |
66 overhead += 2; | 66 overhead += 2; |
67 return SaturateAdd(payload_size, overhead); | 67 return SaturateAdd(payload_size, overhead); |
68 } | 68 } |
69 | 69 |
70 bool InValidStateToReceive(PP_WebSocketReadyState_Dev state) { | 70 bool InValidStateToReceive(PP_WebSocketReadyState state) { |
71 return state == PP_WEBSOCKETREADYSTATE_OPEN_DEV || | 71 return state == PP_WEBSOCKETREADYSTATE_OPEN || |
72 state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 72 state == PP_WEBSOCKETREADYSTATE_CLOSING; |
73 } | 73 } |
74 | 74 |
75 } // namespace | 75 } // namespace |
76 | 76 |
77 namespace webkit { | 77 namespace webkit { |
78 namespace ppapi { | 78 namespace ppapi { |
79 | 79 |
80 PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance) | 80 PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance) |
81 : Resource(instance), | 81 : Resource(instance), |
82 state_(PP_WEBSOCKETREADYSTATE_INVALID_DEV), | 82 state_(PP_WEBSOCKETREADYSTATE_INVALID), |
83 error_was_received_(false), | 83 error_was_received_(false), |
84 receive_callback_var_(NULL), | 84 receive_callback_var_(NULL), |
85 wait_for_receive_(false), | 85 wait_for_receive_(false), |
86 close_code_(0), | 86 close_code_(0), |
87 close_was_clean_(PP_FALSE), | 87 close_was_clean_(PP_FALSE), |
88 empty_string_(new StringVar("", 0)), | 88 empty_string_(new StringVar("", 0)), |
89 buffered_amount_(0), | 89 buffered_amount_(0), |
90 buffered_amount_after_close_(0) { | 90 buffered_amount_after_close_(0) { |
91 } | 91 } |
92 | 92 |
(...skipping 18 matching lines...) Expand all Loading... |
111 PP_CompletionCallback callback) { | 111 PP_CompletionCallback callback) { |
112 // Check mandatory interfaces. | 112 // Check mandatory interfaces. |
113 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); | 113 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); |
114 DCHECK(plugin_instance); | 114 DCHECK(plugin_instance); |
115 if (!plugin_instance) | 115 if (!plugin_instance) |
116 return PP_ERROR_FAILED; | 116 return PP_ERROR_FAILED; |
117 | 117 |
118 // Connect() can be called at most once. | 118 // Connect() can be called at most once. |
119 if (websocket_.get()) | 119 if (websocket_.get()) |
120 return PP_ERROR_INPROGRESS; | 120 return PP_ERROR_INPROGRESS; |
121 if (state_ != PP_WEBSOCKETREADYSTATE_INVALID_DEV) | 121 if (state_ != PP_WEBSOCKETREADYSTATE_INVALID) |
122 return PP_ERROR_INPROGRESS; | 122 return PP_ERROR_INPROGRESS; |
123 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; | 123 state_ = PP_WEBSOCKETREADYSTATE_CLOSED; |
124 | 124 |
125 // Validate url and convert it to WebURL. | 125 // Validate url and convert it to WebURL. |
126 scoped_refptr<StringVar> url_string = StringVar::FromPPVar(url); | 126 scoped_refptr<StringVar> url_string = StringVar::FromPPVar(url); |
127 if (!url_string) | 127 if (!url_string) |
128 return PP_ERROR_BADARGUMENT; | 128 return PP_ERROR_BADARGUMENT; |
129 GURL gurl(url_string->value()); | 129 GURL gurl(url_string->value()); |
130 url_ = new StringVar(gurl.spec()); | 130 url_ = new StringVar(gurl.spec()); |
131 if (!gurl.is_valid()) | 131 if (!gurl.is_valid()) |
132 return PP_ERROR_BADARGUMENT; | 132 return PP_ERROR_BADARGUMENT; |
133 if (!gurl.SchemeIs("ws") && !gurl.SchemeIs("wss")) | 133 if (!gurl.SchemeIs("ws") && !gurl.SchemeIs("wss")) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 WebDocument document = plugin_instance->container()->element().document(); | 191 WebDocument document = plugin_instance->container()->element().document(); |
192 websocket_.reset(WebSocket::create(document, this)); | 192 websocket_.reset(WebSocket::create(document, this)); |
193 DCHECK(websocket_.get()); | 193 DCHECK(websocket_.get()); |
194 if (!websocket_.get()) | 194 if (!websocket_.get()) |
195 return PP_ERROR_NOTSUPPORTED; | 195 return PP_ERROR_NOTSUPPORTED; |
196 | 196 |
197 // Set receiving binary object type. | 197 // Set receiving binary object type. |
198 websocket_->setBinaryType(WebSocket::BinaryTypeArrayBuffer); | 198 websocket_->setBinaryType(WebSocket::BinaryTypeArrayBuffer); |
199 | 199 |
200 websocket_->connect(web_url, web_protocols); | 200 websocket_->connect(web_url, web_protocols); |
201 state_ = PP_WEBSOCKETREADYSTATE_CONNECTING_DEV; | 201 state_ = PP_WEBSOCKETREADYSTATE_CONNECTING; |
202 | 202 |
203 // Install callback. | 203 // Install callback. |
204 connect_callback_ = new TrackedCallback(this, callback); | 204 connect_callback_ = new TrackedCallback(this, callback); |
205 | 205 |
206 return PP_OK_COMPLETIONPENDING; | 206 return PP_OK_COMPLETIONPENDING; |
207 } | 207 } |
208 | 208 |
209 int32_t PPB_WebSocket_Impl::Close(uint16_t code, | 209 int32_t PPB_WebSocket_Impl::Close(uint16_t code, |
210 PP_Var reason, | 210 PP_Var reason, |
211 PP_CompletionCallback callback) { | 211 PP_CompletionCallback callback) { |
(...skipping 14 matching lines...) Expand all Loading... |
226 } | 226 } |
227 | 227 |
228 // Validate |reason|. | 228 // Validate |reason|. |
229 // TODO(toyoshim): Returns PP_ERROR_BADARGUMENT if |reason| contains any | 229 // TODO(toyoshim): Returns PP_ERROR_BADARGUMENT if |reason| contains any |
230 // surrogates. | 230 // surrogates. |
231 scoped_refptr<StringVar> reason_string = StringVar::FromPPVar(reason); | 231 scoped_refptr<StringVar> reason_string = StringVar::FromPPVar(reason); |
232 if (!reason_string || reason_string->value().size() > kMaxReasonSizeInBytes) | 232 if (!reason_string || reason_string->value().size() > kMaxReasonSizeInBytes) |
233 return PP_ERROR_BADARGUMENT; | 233 return PP_ERROR_BADARGUMENT; |
234 | 234 |
235 // Check state. | 235 // Check state. |
236 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV || | 236 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING || |
237 state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) | 237 state_ == PP_WEBSOCKETREADYSTATE_CLOSED) |
238 return PP_ERROR_INPROGRESS; | 238 return PP_ERROR_INPROGRESS; |
239 | 239 |
240 // Validate |callback| (Doesn't support blocking callback) | 240 // Validate |callback| (Doesn't support blocking callback) |
241 if (!callback.func) | 241 if (!callback.func) |
242 return PP_ERROR_BLOCKS_MAIN_THREAD; | 242 return PP_ERROR_BLOCKS_MAIN_THREAD; |
243 | 243 |
244 // Install |callback|. | 244 // Install |callback|. |
245 close_callback_ = new TrackedCallback(this, callback); | 245 close_callback_ = new TrackedCallback(this, callback); |
246 | 246 |
247 // Abort ongoing connect. | 247 // Abort ongoing connect. |
248 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) { | 248 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING) { |
249 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 249 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; |
250 // Need to do a "Post" to avoid reentering the plugin. | 250 // Need to do a "Post" to avoid reentering the plugin. |
251 connect_callback_->PostAbort(); | 251 connect_callback_->PostAbort(); |
252 connect_callback_ = NULL; | 252 connect_callback_ = NULL; |
253 websocket_->fail( | 253 websocket_->fail( |
254 "WebSocket was closed before the connection was established."); | 254 "WebSocket was closed before the connection was established."); |
255 return PP_OK_COMPLETIONPENDING; | 255 return PP_OK_COMPLETIONPENDING; |
256 } | 256 } |
257 | 257 |
258 // Abort ongoing receive. | 258 // Abort ongoing receive. |
259 if (wait_for_receive_) { | 259 if (wait_for_receive_) { |
260 wait_for_receive_ = false; | 260 wait_for_receive_ = false; |
261 receive_callback_var_ = NULL; | 261 receive_callback_var_ = NULL; |
262 | 262 |
263 // Need to do a "Post" to avoid reentering the plugin. | 263 // Need to do a "Post" to avoid reentering the plugin. |
264 receive_callback_->PostAbort(); | 264 receive_callback_->PostAbort(); |
265 receive_callback_ = NULL; | 265 receive_callback_ = NULL; |
266 } | 266 } |
267 | 267 |
268 // Close connection. | 268 // Close connection. |
269 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 269 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; |
270 WebString web_reason = WebString::fromUTF8(reason_string->value()); | 270 WebString web_reason = WebString::fromUTF8(reason_string->value()); |
271 websocket_->close(code, web_reason); | 271 websocket_->close(code, web_reason); |
272 | 272 |
273 return PP_OK_COMPLETIONPENDING; | 273 return PP_OK_COMPLETIONPENDING; |
274 } | 274 } |
275 | 275 |
276 int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, | 276 int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, |
277 PP_CompletionCallback callback) { | 277 PP_CompletionCallback callback) { |
278 // Check state. | 278 // Check state. |
279 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID_DEV || | 279 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID || |
280 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) | 280 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING) |
281 return PP_ERROR_BADARGUMENT; | 281 return PP_ERROR_BADARGUMENT; |
282 | 282 |
283 // Just return received message if any received message is queued. | 283 // Just return received message if any received message is queued. |
284 if (!received_messages_.empty()) { | 284 if (!received_messages_.empty()) { |
285 receive_callback_var_ = message; | 285 receive_callback_var_ = message; |
286 return DoReceive(); | 286 return DoReceive(); |
287 } | 287 } |
288 | 288 |
289 // Check state again. In CLOSED state, no more messages will be received. | 289 // Check state again. In CLOSED state, no more messages will be received. |
290 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) | 290 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED) |
291 return PP_ERROR_BADARGUMENT; | 291 return PP_ERROR_BADARGUMENT; |
292 | 292 |
293 // Returns PP_ERROR_FAILED after an error is received and received messages | 293 // Returns PP_ERROR_FAILED after an error is received and received messages |
294 // is exhausted. | 294 // is exhausted. |
295 if (error_was_received_) | 295 if (error_was_received_) |
296 return PP_ERROR_FAILED; | 296 return PP_ERROR_FAILED; |
297 | 297 |
298 // Validate |callback| (Doesn't support blocking callback) | 298 // Validate |callback| (Doesn't support blocking callback) |
299 if (!callback.func) | 299 if (!callback.func) |
300 return PP_ERROR_BLOCKS_MAIN_THREAD; | 300 return PP_ERROR_BLOCKS_MAIN_THREAD; |
301 | 301 |
302 // Or retain |message| as buffer to store and install |callback|. | 302 // Or retain |message| as buffer to store and install |callback|. |
303 wait_for_receive_ = true; | 303 wait_for_receive_ = true; |
304 receive_callback_var_ = message; | 304 receive_callback_var_ = message; |
305 receive_callback_ = new TrackedCallback(this, callback); | 305 receive_callback_ = new TrackedCallback(this, callback); |
306 | 306 |
307 return PP_OK_COMPLETIONPENDING; | 307 return PP_OK_COMPLETIONPENDING; |
308 } | 308 } |
309 | 309 |
310 int32_t PPB_WebSocket_Impl::SendMessage(PP_Var message) { | 310 int32_t PPB_WebSocket_Impl::SendMessage(PP_Var message) { |
311 // Check mandatory interfaces. | 311 // Check mandatory interfaces. |
312 if (!websocket_.get()) | 312 if (!websocket_.get()) |
313 return PP_ERROR_FAILED; | 313 return PP_ERROR_FAILED; |
314 | 314 |
315 // Check state. | 315 // Check state. |
316 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID_DEV || | 316 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID || |
317 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) | 317 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING) |
318 return PP_ERROR_BADARGUMENT; | 318 return PP_ERROR_BADARGUMENT; |
319 | 319 |
320 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV || | 320 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING || |
321 state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) { | 321 state_ == PP_WEBSOCKETREADYSTATE_CLOSED) { |
322 // Handle buffered_amount_after_close_. | 322 // Handle buffered_amount_after_close_. |
323 uint64_t payload_size = 0; | 323 uint64_t payload_size = 0; |
324 if (message.type == PP_VARTYPE_STRING) { | 324 if (message.type == PP_VARTYPE_STRING) { |
325 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); | 325 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); |
326 if (message_string) | 326 if (message_string) |
327 payload_size += message_string->value().length(); | 327 payload_size += message_string->value().length(); |
328 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { | 328 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { |
329 scoped_refptr<ArrayBufferVar> message_array_buffer = | 329 scoped_refptr<ArrayBufferVar> message_array_buffer = |
330 ArrayBufferVar::FromPPVar(message); | 330 ArrayBufferVar::FromPPVar(message); |
331 if (message_array_buffer) | 331 if (message_array_buffer) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 395 |
396 PP_Var PPB_WebSocket_Impl::GetProtocol() { | 396 PP_Var PPB_WebSocket_Impl::GetProtocol() { |
397 // Check mandatory interfaces. | 397 // Check mandatory interfaces. |
398 if (!websocket_.get()) | 398 if (!websocket_.get()) |
399 return empty_string_->GetPPVar(); | 399 return empty_string_->GetPPVar(); |
400 | 400 |
401 std::string protocol = websocket_->subprotocol().utf8(); | 401 std::string protocol = websocket_->subprotocol().utf8(); |
402 return StringVar::StringToPPVar(protocol); | 402 return StringVar::StringToPPVar(protocol); |
403 } | 403 } |
404 | 404 |
405 PP_WebSocketReadyState_Dev PPB_WebSocket_Impl::GetReadyState() { | 405 PP_WebSocketReadyState PPB_WebSocket_Impl::GetReadyState() { |
406 return state_; | 406 return state_; |
407 } | 407 } |
408 | 408 |
409 PP_Var PPB_WebSocket_Impl::GetURL() { | 409 PP_Var PPB_WebSocket_Impl::GetURL() { |
410 if (!url_) | 410 if (!url_) |
411 return empty_string_->GetPPVar(); | 411 return empty_string_->GetPPVar(); |
412 return url_->GetPPVar(); | 412 return url_->GetPPVar(); |
413 } | 413 } |
414 | 414 |
415 void PPB_WebSocket_Impl::didConnect() { | 415 void PPB_WebSocket_Impl::didConnect() { |
416 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING_DEV, state_); | 416 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING, state_); |
417 state_ = PP_WEBSOCKETREADYSTATE_OPEN_DEV; | 417 state_ = PP_WEBSOCKETREADYSTATE_OPEN; |
418 TrackedCallback::ClearAndRun(&connect_callback_, PP_OK); | 418 TrackedCallback::ClearAndRun(&connect_callback_, PP_OK); |
419 } | 419 } |
420 | 420 |
421 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { | 421 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { |
422 // Dispose packets after receiving an error or in invalid state. | 422 // Dispose packets after receiving an error or in invalid state. |
423 if (error_was_received_ || !InValidStateToReceive(state_)) | 423 if (error_was_received_ || !InValidStateToReceive(state_)) |
424 return; | 424 return; |
425 | 425 |
426 // Append received data to queue. | 426 // Append received data to queue. |
427 std::string string = message.utf8(); | 427 std::string string = message.utf8(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 | 463 |
464 // But, if no messages are queued and ReceiveMessage() is now on going. | 464 // But, if no messages are queued and ReceiveMessage() is now on going. |
465 // We must invoke the callback with error code here. | 465 // We must invoke the callback with error code here. |
466 wait_for_receive_ = false; | 466 wait_for_receive_ = false; |
467 receive_callback_var_ = NULL; | 467 receive_callback_var_ = NULL; |
468 TrackedCallback::ClearAndRun(&receive_callback_, PP_ERROR_FAILED); | 468 TrackedCallback::ClearAndRun(&receive_callback_, PP_ERROR_FAILED); |
469 } | 469 } |
470 | 470 |
471 void PPB_WebSocket_Impl::didUpdateBufferedAmount( | 471 void PPB_WebSocket_Impl::didUpdateBufferedAmount( |
472 unsigned long buffered_amount) { | 472 unsigned long buffered_amount) { |
473 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) | 473 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED) |
474 return; | 474 return; |
475 buffered_amount_ = buffered_amount; | 475 buffered_amount_ = buffered_amount; |
476 } | 476 } |
477 | 477 |
478 void PPB_WebSocket_Impl::didStartClosingHandshake() { | 478 void PPB_WebSocket_Impl::didStartClosingHandshake() { |
479 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 479 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; |
480 } | 480 } |
481 | 481 |
482 void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount, | 482 void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount, |
483 ClosingHandshakeCompletionStatus status, | 483 ClosingHandshakeCompletionStatus status, |
484 unsigned short code, | 484 unsigned short code, |
485 const WebString& reason) { | 485 const WebString& reason) { |
486 // Store code and reason. | 486 // Store code and reason. |
487 close_code_ = code; | 487 close_code_ = code; |
488 std::string reason_string = reason.utf8(); | 488 std::string reason_string = reason.utf8(); |
489 close_reason_ = new StringVar(reason_string); | 489 close_reason_ = new StringVar(reason_string); |
490 | 490 |
491 // Set close_was_clean_. | 491 // Set close_was_clean_. |
492 bool was_clean = | 492 bool was_clean = |
493 state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV && | 493 state_ == PP_WEBSOCKETREADYSTATE_CLOSING && |
494 !unhandled_buffered_amount && | 494 !unhandled_buffered_amount && |
495 status == WebSocketClient::ClosingHandshakeComplete; | 495 status == WebSocketClient::ClosingHandshakeComplete; |
496 close_was_clean_ = was_clean ? PP_TRUE : PP_FALSE; | 496 close_was_clean_ = was_clean ? PP_TRUE : PP_FALSE; |
497 | 497 |
498 // Update buffered_amount_. | 498 // Update buffered_amount_. |
499 buffered_amount_ = unhandled_buffered_amount; | 499 buffered_amount_ = unhandled_buffered_amount; |
500 | 500 |
501 // Handle state transition and invoking callback. | 501 // Handle state transition and invoking callback. |
502 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_); | 502 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED, state_); |
503 PP_WebSocketReadyState_Dev state = state_; | 503 PP_WebSocketReadyState state = state_; |
504 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; | 504 state_ = PP_WEBSOCKETREADYSTATE_CLOSED; |
505 | 505 |
506 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) | 506 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING) |
507 TrackedCallback::ClearAndRun(&connect_callback_, PP_ERROR_FAILED); | 507 TrackedCallback::ClearAndRun(&connect_callback_, PP_ERROR_FAILED); |
508 | 508 |
509 if (wait_for_receive_) { | 509 if (wait_for_receive_) { |
510 wait_for_receive_ = false; | 510 wait_for_receive_ = false; |
511 receive_callback_var_ = NULL; | 511 receive_callback_var_ = NULL; |
512 TrackedCallback::ClearAndAbort(&receive_callback_); | 512 TrackedCallback::ClearAndAbort(&receive_callback_); |
513 } | 513 } |
514 | 514 |
515 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV) | 515 if (state == PP_WEBSOCKETREADYSTATE_CLOSING) |
516 TrackedCallback::ClearAndRun(&close_callback_, PP_OK); | 516 TrackedCallback::ClearAndRun(&close_callback_, PP_OK); |
517 | 517 |
518 // Disconnect. | 518 // Disconnect. |
519 if (websocket_.get()) | 519 if (websocket_.get()) |
520 websocket_->disconnect(); | 520 websocket_->disconnect(); |
521 } | 521 } |
522 | 522 |
523 int32_t PPB_WebSocket_Impl::DoReceive() { | 523 int32_t PPB_WebSocket_Impl::DoReceive() { |
524 if (!receive_callback_var_) | 524 if (!receive_callback_var_) |
525 return PP_OK; | 525 return PP_OK; |
526 | 526 |
527 *receive_callback_var_ = received_messages_.front()->GetPPVar(); | 527 *receive_callback_var_ = received_messages_.front()->GetPPVar(); |
528 received_messages_.pop(); | 528 received_messages_.pop(); |
529 receive_callback_var_ = NULL; | 529 receive_callback_var_ = NULL; |
530 wait_for_receive_ = false; | 530 wait_for_receive_ = false; |
531 return PP_OK; | 531 return PP_OK; |
532 } | 532 } |
533 | 533 |
534 } // namespace ppapi | 534 } // namespace ppapi |
535 } // namespace webkit | 535 } // namespace webkit |
OLD | NEW |