Index: webkit/plugins/ppapi/ppb_websocket_impl.cc |
diff --git a/webkit/plugins/ppapi/ppb_websocket_impl.cc b/webkit/plugins/ppapi/ppb_websocket_impl.cc |
index 48bc1f56580a883572947b659e5b2d5f67e5657d..0c2cf54b87c39ed21efae1c8ee27b48d68df35d2 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; |
@@ -320,8 +319,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)); |
@@ -329,19 +335,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 |
+ // directory. |
dmichael (off chromium)
2011/12/20 17:11:06
directory->directly?
Takashi Toyoshima
2011/12/21 07:45:57
Done.
|
+ 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; |
} |
@@ -417,8 +437,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() { |