| 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 825a4e94f92ee5cab56ebfcb8042cc66041ad07f..529423890cb6c69fc0e9aa19ecaa73ff726a92db 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"
|
| @@ -18,20 +19,24 @@
|
| #include "ppapi/shared_impl/var.h"
|
| #include "ppapi/shared_impl/var_tracker.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebData.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBuffer.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocket.h"
|
| #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
|
| +#include "webkit/plugins/ppapi/host_array_buffer_var.h"
|
| #include "webkit/plugins/ppapi/host_globals.h"
|
| #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;
|
| using ppapi::VarTracker;
|
| +using WebKit::WebArrayBuffer;
|
| using WebKit::WebData;
|
| using WebKit::WebDocument;
|
| using WebKit::WebString;
|
| @@ -54,9 +59,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;
|
| @@ -97,14 +99,6 @@ PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance)
|
| PPB_WebSocket_Impl::~PPB_WebSocket_Impl() {
|
| if (websocket_.get())
|
| websocket_->disconnect();
|
| -
|
| - // Clean up received and unread messages
|
| - VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker();
|
| - while (!received_messages_.empty()) {
|
| - PP_Var var = received_messages_.front();
|
| - received_messages_.pop();
|
| - var_tracker->ReleaseVar(var);
|
| - }
|
| }
|
|
|
| // static
|
| @@ -206,6 +200,9 @@ int32_t PPB_WebSocket_Impl::Connect(PP_Var url,
|
| if (!websocket_.get())
|
| return PP_ERROR_NOTSUPPORTED;
|
|
|
| + // TODO(toyoshim): Add an interface to specify binary format types.
|
| + websocket_->setBinaryType("arraybuffer");
|
| +
|
| websocket_->connect(web_url, web_protocols);
|
| state_ = PP_WEBSOCKETREADYSTATE_CONNECTING_DEV;
|
|
|
| @@ -326,8 +323,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));
|
| @@ -335,19 +339,29 @@ 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 WebArrayBuffer.
|
| + scoped_refptr<HostArrayBufferVar> host_message =
|
| + static_cast<HostArrayBufferVar*>(ArrayBufferVar::FromPPVar(message));
|
| + if (!host_message)
|
| + return PP_ERROR_BADARGUMENT;
|
| + WebArrayBuffer& web_message = host_message->webkit_buffer();
|
| + if (!websocket_->sendArrayBuffer(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;
|
| }
|
|
|
| @@ -409,8 +423,7 @@ void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) {
|
|
|
| // Append received data to queue.
|
| std::string string = message.utf8();
|
| - PP_Var var = StringVar::StringToPPVar(string);
|
| - received_messages_.push(var);
|
| + received_messages_.push(new StringVar(string));
|
|
|
| if (!wait_for_receive_)
|
| return;
|
| @@ -423,8 +436,33 @@ 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.
|
| + scoped_refptr<ArrayBufferVar> var =
|
| + new HostArrayBufferVar(binaryData.size());
|
| + void* data = var->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::didReceiveArrayBuffer(
|
| + const WebArrayBuffer& binaryData) {
|
| + // Dispose packets after receiving an error or in invalid state.
|
| + if (error_was_received_ || !InValidStateToReceive(state_))
|
| + return;
|
| +
|
| + // Append received data to queue.
|
| + scoped_refptr<ArrayBufferVar> var = new HostArrayBufferVar(binaryData);
|
| + received_messages_.push(var);
|
| +
|
| + if (!wait_for_receive_)
|
| + return;
|
| +
|
| + PP_RunAndClearCompletionCallback(&receive_callback_, DoReceive());
|
| }
|
|
|
| void PPB_WebSocket_Impl::didReceiveMessageError() {
|
| @@ -502,7 +540,7 @@ int32_t PPB_WebSocket_Impl::DoReceive() {
|
| if (!receive_callback_var_)
|
| return PP_OK;
|
|
|
| - *receive_callback_var_ = received_messages_.front();
|
| + *receive_callback_var_ = received_messages_.front()->GetPPVar();
|
| received_messages_.pop();
|
| receive_callback_var_ = NULL;
|
| wait_for_receive_ = false;
|
|
|