diff options
author | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-28 05:51:32 +0000 |
---|---|---|
committer | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-28 05:51:32 +0000 |
commit | e18637fbd2e55fb610d92bc061f10a0629826377 (patch) | |
tree | 79ce7b7f418ab575a51e4b04c99cb1edd0e15a0a /webkit/plugins | |
parent | 2c783bf371d91b5c5ce8ccafddd64d5e8c817d92 (diff) | |
download | chromium_src-e18637fbd2e55fb610d92bc061f10a0629826377.zip chromium_src-e18637fbd2e55fb610d92bc061f10a0629826377.tar.gz chromium_src-e18637fbd2e55fb610d92bc061f10a0629826377.tar.bz2 |
WebSocket Pepper API: ArrayBuffer support
This CL supports basic binary communication.
For now, communication between Chrome and WebKit uses WebData structure.
It must use WebArrayBuffer directly after WebKit API revision.
BUG=87310
TEST=ui_tests --gtest_filter='PPAPITest.WebSocket_BinarySendReceive'
Review URL: http://codereview.chromium.org/8774042
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@115871 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/plugins')
-rw-r--r-- | webkit/plugins/ppapi/ppb_websocket_impl.cc | 66 |
1 files changed, 49 insertions, 17 deletions
diff --git a/webkit/plugins/ppapi/ppb_websocket_impl.cc b/webkit/plugins/ppapi/ppb_websocket_impl.cc index 91da613..1282b13 100644 --- a/webkit/plugins/ppapi/ppb_websocket_impl.cc +++ b/webkit/plugins/ppapi/ppb_websocket_impl.cc @@ -11,6 +11,7 @@ #include "base/logging.h" #include "googleurl/src/gurl.h" #include "net/base/net_util.h" +#include "ppapi/c/dev/ppb_var_array_buffer_dev.h" #include "ppapi/c/pp_completion_callback.h" #include "ppapi/c/pp_errors.h" #include "ppapi/c/pp_var.h" @@ -28,6 +29,7 @@ #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource_helper.h" +using ppapi::ArrayBufferVar; using ppapi::PpapiGlobals; using ppapi::StringVar; using ppapi::thunk::PPB_WebSocket_API; @@ -54,9 +56,6 @@ uint64_t SaturateAdd(uint64_t a, uint64_t b) { } uint64_t GetFrameSize(uint64_t payload_size) { - if (!payload_size) - return 0; - uint64_t overhead = kHybiBaseFramingOverhead + kHybiMaskingKeyLength; if (payload_size > kMinimumPayloadSizeWithEightByteExtendedPayloadLength) overhead += 8; @@ -285,8 +284,10 @@ int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, return PP_ERROR_BADARGUMENT; // Just return received message if any received message is queued. - if (!received_messages_.empty()) + if (!received_messages_.empty()) { + receive_callback_var_ = message; return DoReceive(); + } // Check state again. In CLOSED state, no more messages will be received. if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED_DEV) @@ -327,8 +328,15 @@ int32_t PPB_WebSocket_Impl::SendMessage(PP_Var message) { scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); if (message_string) payload_size += message_string->value().length(); + } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { + scoped_refptr<ArrayBufferVar> message_array_buffer = + ArrayBufferVar::FromPPVar(message); + if (message_array_buffer) + payload_size += message_array_buffer->ByteLength(); + } else { + // TODO(toyoshim): Support Blob. + return PP_ERROR_NOTSUPPORTED; } - // TODO(toyoshim): Support binary data. buffered_amount_after_close_ = SaturateAdd(buffered_amount_after_close_, GetFrameSize(payload_size)); @@ -336,19 +344,33 @@ int32_t PPB_WebSocket_Impl::SendMessage(PP_Var message) { return PP_ERROR_FAILED; } - if (message.type != PP_VARTYPE_STRING) { - // TODO(toyoshim): Support binary data. + // Send the message. + if (message.type == PP_VARTYPE_STRING) { + // Convert message to WebString. + scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); + if (!message_string) + return PP_ERROR_BADARGUMENT; + WebString web_message = WebString::fromUTF8(message_string->value()); + if (!websocket_->sendText(web_message)) + return PP_ERROR_BADARGUMENT; + } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { + // Convert message to WebData. + // TODO(toyoshim): Must add a WebKit interface which handles WebArrayBuffer + // directly. + scoped_refptr<ArrayBufferVar> message_array_buffer = + ArrayBufferVar::FromPPVar(message); + if (!message_array_buffer) + return PP_ERROR_BADARGUMENT; + WebData web_message = WebData( + static_cast<const char*>(message_array_buffer->Map()), + static_cast<size_t>(message_array_buffer->ByteLength())); + if (!websocket_->sendBinary(web_message)) + return PP_ERROR_BADARGUMENT; + } else { + // TODO(toyoshim): Support Blob. return PP_ERROR_NOTSUPPORTED; } - // Convert message to WebString. - scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); - if (!message_string) - return PP_ERROR_BADARGUMENT; - WebString web_message = WebString::fromUTF8(message_string->value()); - if (!websocket_->sendText(web_message)) - return PP_ERROR_BADARGUMENT; - return PP_OK; } @@ -424,8 +446,18 @@ void PPB_WebSocket_Impl::didReceiveBinaryData(const WebData& binaryData) { if (error_was_received_ || !InValidStateToReceive(state_)) return; - // TODO(toyoshim): Support to receive binary data. - DLOG(INFO) << "didReceiveBinaryData is not implemented yet."; + // Append received data to queue. + PP_Var var = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( + binaryData.size()); + scoped_refptr<ArrayBufferVar> arraybuffer = ArrayBufferVar::FromPPVar(var); + void* data = arraybuffer->Map(); + memcpy(data, binaryData.data(), binaryData.size()); + received_messages_.push(var); + + if (!wait_for_receive_) + return; + + PP_RunAndClearCompletionCallback(&receive_callback_, DoReceive()); } void PPB_WebSocket_Impl::didReceiveMessageError() { |