Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 PP_CompletionCallback callback) { | 205 PP_CompletionCallback callback) { |
| 206 // Check mandarory interfaces. | 206 // Check mandarory interfaces. |
| 207 if (!websocket_.get()) | 207 if (!websocket_.get()) |
| 208 return PP_ERROR_FAILED; | 208 return PP_ERROR_FAILED; |
| 209 | 209 |
| 210 // Validate |code|. | 210 // Validate |code|. |
| 211 if (code != WebSocket::CloseEventCodeNotSpecified) { | 211 if (code != WebSocket::CloseEventCodeNotSpecified) { |
| 212 if (!(code == WebSocket::CloseEventCodeNormalClosure || | 212 if (!(code == WebSocket::CloseEventCodeNormalClosure || |
| 213 (WebSocket::CloseEventCodeMinimumUserDefined <= code && | 213 (WebSocket::CloseEventCodeMinimumUserDefined <= code && |
| 214 code <= WebSocket::CloseEventCodeMaximumUserDefined))) | 214 code <= WebSocket::CloseEventCodeMaximumUserDefined))) |
| 215 return PP_ERROR_NOACCESS; | 215 return PP_ERROR_NOACCESS; |
|
dmichael (off chromium)
2011/12/06 17:07:34
This should probably be PP_ERROR_BADARGUMENT.
PP_
Takashi Toyoshima
2011/12/06 17:47:18
WebSocket protocol spec forbid applications to use
dmichael (off chromium)
2011/12/06 17:59:01
Can you add a comment referencing that?
Takashi Toyoshima
2011/12/06 18:27:27
Done.
| |
| 216 } | 216 } |
| 217 | 217 |
| 218 // Validate |reason|. | 218 // Validate |reason|. |
| 219 // TODO(toyoshim): Returns PP_ERROR_BADARGUMENT if |reason| contains any | 219 // TODO(toyoshim): Returns PP_ERROR_BADARGUMENT if |reason| contains any |
| 220 // surrogates. | 220 // surrogates. |
| 221 scoped_refptr<StringVar> reason_string = StringVar::FromPPVar(reason); | 221 scoped_refptr<StringVar> reason_string = StringVar::FromPPVar(reason); |
| 222 if (!reason_string || reason_string->value().size() > kMaxReasonSizeInBytes) | 222 if (!reason_string || reason_string->value().size() > kMaxReasonSizeInBytes) |
| 223 return PP_ERROR_BADARGUMENT; | 223 return PP_ERROR_BADARGUMENT; |
| 224 | 224 |
| 225 // Check state. | 225 // Check state. |
| 226 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV || | 226 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV || |
| 227 state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) | 227 state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) |
| 228 return PP_ERROR_INPROGRESS; | 228 return PP_ERROR_INPROGRESS; |
| 229 | 229 |
| 230 // Validate |callback| (Doesn't support blocking callback) | 230 // Validate |callback| (Doesn't support blocking callback) |
| 231 if (!callback.func) | 231 if (!callback.func) |
| 232 return PP_ERROR_BLOCKS_MAIN_THREAD; | 232 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 233 | 233 |
| 234 // Install |callback|. | 234 // Install |callback|. |
| 235 close_callback_ = callback; | 235 close_callback_ = callback; |
| 236 | 236 |
| 237 // Abort ongoing connect. | |
| 237 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) { | 238 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) { |
| 238 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 239 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; |
| 239 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED); | 240 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED); |
| 240 websocket_->fail( | 241 websocket_->fail( |
| 241 "WebSocket was closed before the connection was established."); | 242 "WebSocket was closed before the connection was established."); |
| 242 return PP_OK_COMPLETIONPENDING; | 243 return PP_OK_COMPLETIONPENDING; |
| 243 } | 244 } |
| 244 | 245 |
| 246 // Abort ongoing receive. | |
| 247 if (wait_for_receive_) { | |
| 248 wait_for_receive_ = false; | |
| 249 receive_callback_var_ = NULL; | |
| 250 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED); | |
| 251 } | |
| 252 | |
| 245 // Close connection. | 253 // Close connection. |
| 246 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 254 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; |
| 247 WebString web_reason = WebString::fromUTF8(reason_string->value()); | 255 WebString web_reason = WebString::fromUTF8(reason_string->value()); |
| 248 websocket_->close(code, web_reason); | 256 websocket_->close(code, web_reason); |
| 249 | 257 |
| 250 return PP_OK_COMPLETIONPENDING; | 258 return PP_OK_COMPLETIONPENDING; |
| 251 } | 259 } |
| 252 | 260 |
| 253 int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, | 261 int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, |
| 254 PP_CompletionCallback callback) { | 262 PP_CompletionCallback callback) { |
| 255 // Check state. | 263 // Check state. |
| 256 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID_DEV || | 264 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID_DEV || |
| 257 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) | 265 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) |
| 258 return PP_ERROR_BADARGUMENT; | 266 return PP_ERROR_BADARGUMENT; |
| 259 | 267 |
| 260 // Just return received message if any received message is queued. | 268 // Just return received message if any received message is queued. |
| 261 if (!received_messages_.empty()) | 269 if (!received_messages_.empty()) |
| 262 return DoReceive(); | 270 return DoReceive(); |
| 263 | 271 |
| 272 // Check state again. In CLOSED state, no more messages will be received. | |
| 273 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) | |
| 274 return PP_ERROR_BADARGUMENT; | |
| 275 | |
| 264 // Returns PP_ERROR_FAILED after an error is received and received messages | 276 // Returns PP_ERROR_FAILED after an error is received and received messages |
| 265 // is exhausted. | 277 // is exhausted. |
| 266 if (error_was_received_) | 278 if (error_was_received_) |
| 267 return PP_ERROR_FAILED; | 279 return PP_ERROR_FAILED; |
| 268 | 280 |
| 269 // Validate |callback| (Doesn't support blocking callback) | 281 // Validate |callback| (Doesn't support blocking callback) |
| 270 if (!callback.func) | 282 if (!callback.func) |
| 271 return PP_ERROR_BLOCKS_MAIN_THREAD; | 283 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 272 | 284 |
| 273 // Or retain |message| as buffer to store and install |callback|. | 285 // Or retain |message| as buffer to store and install |callback|. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 // Records the error, then stops receiving any frames after this error. | 419 // Records the error, then stops receiving any frames after this error. |
| 408 // The error will be notified after all queued messages are read via | 420 // The error will be notified after all queued messages are read via |
| 409 // ReceiveMessage(). | 421 // ReceiveMessage(). |
| 410 error_was_received_ = true; | 422 error_was_received_ = true; |
| 411 if (!wait_for_receive_) | 423 if (!wait_for_receive_) |
| 412 return; | 424 return; |
| 413 | 425 |
| 414 // But, if no messages are queued and ReceiveMessage() is now on going. | 426 // But, if no messages are queued and ReceiveMessage() is now on going. |
| 415 // We must invoke the callback with error code here. | 427 // We must invoke the callback with error code here. |
| 416 wait_for_receive_ = false; | 428 wait_for_receive_ = false; |
| 429 receive_callback_var_ = NULL; | |
| 417 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_FAILED); | 430 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_FAILED); |
| 418 } | 431 } |
| 419 | 432 |
| 420 void PPB_WebSocket_Impl::didUpdateBufferedAmount( | 433 void PPB_WebSocket_Impl::didUpdateBufferedAmount( |
| 421 unsigned long buffered_amount) { | 434 unsigned long buffered_amount) { |
| 422 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) | 435 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) |
| 423 return; | 436 return; |
| 424 buffered_amount_ = buffered_amount; | 437 buffered_amount_ = buffered_amount; |
| 425 } | 438 } |
| 426 | 439 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 447 | 460 |
| 448 // Update buffered_amount_. | 461 // Update buffered_amount_. |
| 449 buffered_amount_ = unhandled_buffered_amount; | 462 buffered_amount_ = unhandled_buffered_amount; |
| 450 | 463 |
| 451 // Handle state transition and invoking callback. | 464 // Handle state transition and invoking callback. |
| 452 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_); | 465 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_); |
| 453 PP_WebSocketReadyState_Dev state = state_; | 466 PP_WebSocketReadyState_Dev state = state_; |
| 454 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; | 467 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; |
| 455 | 468 |
| 456 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) | 469 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) |
| 457 PP_RunAndClearCompletionCallback(&connect_callback_, PP_OK); | 470 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_FAILED); |
| 471 | |
| 472 if (wait_for_receive_) { | |
| 473 wait_for_receive_ = false; | |
| 474 receive_callback_var_ = NULL; | |
| 475 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED); | |
| 476 } | |
| 458 | 477 |
| 459 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV) | 478 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV) |
| 460 PP_RunAndClearCompletionCallback(&close_callback_, PP_OK); | 479 PP_RunAndClearCompletionCallback(&close_callback_, PP_OK); |
| 461 | 480 |
| 462 // Disconnect. | 481 // Disconnect. |
| 463 if (websocket_.get()) | 482 if (websocket_.get()) |
| 464 websocket_->disconnect(); | 483 websocket_->disconnect(); |
| 465 } | 484 } |
| 466 | 485 |
| 467 int32_t PPB_WebSocket_Impl::DoReceive() { | 486 int32_t PPB_WebSocket_Impl::DoReceive() { |
| 468 if (!receive_callback_var_) | 487 if (!receive_callback_var_) |
| 469 return PP_OK; | 488 return PP_OK; |
| 470 | 489 |
| 471 *receive_callback_var_ = received_messages_.front(); | 490 *receive_callback_var_ = received_messages_.front(); |
| 472 received_messages_.pop(); | 491 received_messages_.pop(); |
| 473 receive_callback_var_ = NULL; | 492 receive_callback_var_ = NULL; |
| 474 wait_for_receive_ = false; | 493 wait_for_receive_ = false; |
| 475 return PP_OK; | 494 return PP_OK; |
| 476 } | 495 } |
| 477 | 496 |
| 478 } // namespace ppapi | 497 } // namespace ppapi |
| 479 } // namespace webkit | 498 } // namespace webkit |
| OLD | NEW |