summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortoyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-13 22:48:07 +0000
committertoyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-13 22:48:07 +0000
commitfade7dbc68d63c727ad9dabaaac168f407f31ce9 (patch)
tree745d605a72b5cc6c24bc54fb89b0abdc3101f45d
parentbb1e96746a4bc7a0857f530068a1adfa0438a757 (diff)
downloadchromium_src-fade7dbc68d63c727ad9dabaaac168f407f31ce9.zip
chromium_src-fade7dbc68d63c727ad9dabaaac168f407f31ce9.tar.gz
chromium_src-fade7dbc68d63c727ad9dabaaac168f407f31ce9.tar.bz2
WebSocket Pepper API: Cleanup for more readability.
BUG=none TEST=ui_tests Review URL: http://codereview.chromium.org/9316055 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126490 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--ppapi/api/ppb_websocket.idl2
-rw-r--r--ppapi/c/ppb_websocket.h2
-rw-r--r--ppapi/tests/test_websocket.cc129
-rw-r--r--ppapi/tests/test_websocket.h16
-rw-r--r--ppapi/thunk/ppb_websocket_api.h38
-rw-r--r--webkit/plugins/ppapi/ppb_websocket_impl.cc28
-rw-r--r--webkit/plugins/ppapi/ppb_websocket_impl.h55
7 files changed, 180 insertions, 90 deletions
diff --git a/ppapi/api/ppb_websocket.idl b/ppapi/api/ppb_websocket.idl
index 95dd1ec..38a6470 100644
--- a/ppapi/api/ppb_websocket.idl
+++ b/ppapi/api/ppb_websocket.idl
@@ -11,7 +11,6 @@ label Chrome {
M18 = 1.0
};
-
/**
* This enumeration contains the types representing the WebSocket ready state
* and these states are based on the JavaScript WebSocket API specification.
@@ -23,6 +22,7 @@ enum PP_WebSocketReadyState {
* Ready state is queried on an invalid resource.
*/
PP_WEBSOCKETREADYSTATE_INVALID = -1,
+
/**
* Ready state that the connection has not yet been established.
*/
diff --git a/ppapi/c/ppb_websocket.h b/ppapi/c/ppb_websocket.h
index d8f1fd9..bec5182 100644
--- a/ppapi/c/ppb_websocket.h
+++ b/ppapi/c/ppb_websocket.h
@@ -3,7 +3,7 @@
* found in the LICENSE file.
*/
-/* From ppb_websocket.idl modified Thu Mar 8 05:35:06 2012. */
+/* From ppb_websocket.idl modified Wed Mar 14 01:49:27 2012. */
#ifndef PPAPI_C_PPB_WEBSOCKET_H_
#define PPAPI_C_PPB_WEBSOCKET_H_
diff --git a/ppapi/tests/test_websocket.cc b/ppapi/tests/test_websocket.cc
index 7d8f9a0..d4a9fc5 100644
--- a/ppapi/tests/test_websocket.cc
+++ b/ppapi/tests/test_websocket.cc
@@ -5,12 +5,17 @@
#include "ppapi/tests/test_websocket.h"
#include <string.h>
+#include <algorithm>
+#include <string>
#include <vector>
#include "ppapi/c/dev/ppb_testing_dev.h"
+#include "ppapi/c/pp_bool.h"
+#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/pp_instance.h"
+#include "ppapi/c/pp_resource.h"
#include "ppapi/c/pp_var.h"
-#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/ppb_core.h"
#include "ppapi/c/ppb_var.h"
#include "ppapi/c/ppb_var_array_buffer.h"
@@ -63,7 +68,8 @@ struct WebSocketEvent {
: event_type(type),
was_clean(was_clean),
close_code(close_code),
- var(var) {}
+ var(var) {
+ }
EventType event_type;
bool was_clean;
uint16_t close_code;
@@ -80,7 +86,8 @@ class TestWebSocketAPI : public pp::WebSocketAPI {
wait_for_connected_(false),
wait_for_received_(false),
wait_for_closed_(false),
- instance_(instance->pp_instance()) {}
+ instance_(instance->pp_instance()) {
+ }
virtual void WebSocketDidOpen() {
events_.push_back(
@@ -206,14 +213,14 @@ void TestWebSocket::RunTests(const std::string& filter) {
RUN_TEST_WITH_REFERENCE_CHECK(UtilityBufferedAmount, filter);
}
-PP_Var TestWebSocket::CreateVarString(const char* string) {
- return var_interface_->VarFromUtf8(string, strlen(string));
+PP_Var TestWebSocket::CreateVarString(const std::string& string) {
+ return var_interface_->VarFromUtf8(string.c_str(), string.size());
}
-PP_Var TestWebSocket::CreateVarBinary(const uint8_t* data, uint32_t size) {
- PP_Var var = arraybuffer_interface_->Create(size);
- void* var_data = arraybuffer_interface_->Map(var);
- memcpy(var_data, data, size);
+PP_Var TestWebSocket::CreateVarBinary(const std::vector<uint8_t>& binary) {
+ PP_Var var = arraybuffer_interface_->Create(binary.size());
+ uint8_t* var_data = static_cast<uint8_t*>(arraybuffer_interface_->Map(var));
+ std::copy(binary.begin(), binary.end(), var_data);
return var;
}
@@ -221,33 +228,34 @@ void TestWebSocket::ReleaseVar(const PP_Var& var) {
var_interface_->Release(var);
}
-bool TestWebSocket::AreEqualWithString(const PP_Var& var, const char* string) {
+bool TestWebSocket::AreEqualWithString(const PP_Var& var,
+ const std::string& string) {
if (var.type != PP_VARTYPE_STRING)
return false;
uint32_t utf8_length;
const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length);
- uint32_t string_length = strlen(string);
- if (utf8_length != string_length)
+ if (utf8_length != string.size())
return false;
- if (strncmp(utf8, string, utf8_length))
+ if (string.compare(utf8))
return false;
return true;
}
bool TestWebSocket::AreEqualWithBinary(const PP_Var& var,
- const uint8_t* data,
- uint32_t size) {
+ const std::vector<uint8_t>& binary) {
uint32_t buffer_size = 0;
PP_Bool success = arraybuffer_interface_->ByteLength(var, &buffer_size);
- if (!success || buffer_size != size)
+ if (!success || buffer_size != binary.size())
return false;
- if (memcmp(arraybuffer_interface_->Map(var), data, size))
+ if (!std::equal(binary.begin(), binary.end(),
+ static_cast<uint8_t*>(arraybuffer_interface_->Map(var))))
return false;
return true;
}
-PP_Resource TestWebSocket::Connect(
- const char* url, int32_t* result, const char* protocol) {
+PP_Resource TestWebSocket::Connect(const std::string& url,
+ int32_t* result,
+ const std::string& protocol) {
PP_Var protocols[] = { PP_MakeUndefined() };
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
if (!ws)
@@ -255,7 +263,7 @@ PP_Resource TestWebSocket::Connect(
PP_Var url_var = CreateVarString(url);
TestCompletionCallback callback(instance_->pp_instance(), force_async_);
uint32_t protocol_count = 0U;
- if (protocol) {
+ if (protocol.size()) {
protocols[0] = CreateVarString(protocol);
protocol_count = 1U;
}
@@ -263,7 +271,7 @@ PP_Resource TestWebSocket::Connect(
ws, url_var, protocols, protocol_count,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ReleaseVar(url_var);
- if (protocol)
+ if (protocol.size())
ReleaseVar(protocols[0]);
if (*result == PP_OK_COMPLETIONPENDING)
*result = callback.WaitForResult();
@@ -346,7 +354,7 @@ std::string TestWebSocket::TestInvalidConnect() {
core_interface_->ReleaseResource(ws);
for (int i = 0; kInvalidURLs[i]; ++i) {
- ws = Connect(kInvalidURLs[i], &result, NULL);
+ ws = Connect(kInvalidURLs[i], &result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
@@ -398,7 +406,7 @@ std::string TestWebSocket::TestProtocols() {
std::string TestWebSocket::TestGetURL() {
for (int i = 0; kInvalidURLs[i]; ++i) {
int32_t result;
- PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL);
+ PP_Resource ws = Connect(kInvalidURLs[i], &result, "");
ASSERT_TRUE(ws);
PP_Var url = websocket_interface_->GetURL(ws);
ASSERT_TRUE(AreEqualWithString(url, kInvalidURLs[i]));
@@ -413,7 +421,7 @@ std::string TestWebSocket::TestGetURL() {
std::string TestWebSocket::TestValidConnect() {
int32_t result;
- PP_Resource ws = Connect(kEchoServerURL, &result, NULL);
+ PP_Resource ws = Connect(kEchoServerURL, &result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
PP_Var extensions = websocket_interface_->GetExtensions(ws);
@@ -436,7 +444,7 @@ std::string TestWebSocket::TestInvalidClose() {
core_interface_->ReleaseResource(ws);
// Close with bad arguments.
- ws = Connect(kEchoServerURL, &result, NULL);
+ ws = Connect(kEchoServerURL, &result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
result = websocket_interface_->Close(ws, 1U, reason,
@@ -458,7 +466,7 @@ std::string TestWebSocket::TestValidClose() {
// Close.
int32_t result;
- PP_Resource ws = Connect(kEchoServerURL, &result, NULL);
+ PP_Resource ws = Connect(kEchoServerURL, &result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
result = websocket_interface_->Close(ws,
@@ -490,7 +498,7 @@ std::string TestWebSocket::TestValidClose() {
// Close in closing.
// The first close will be done successfully, then the second one failed with
// with PP_ERROR_INPROGRESS immediately.
- ws = Connect(kEchoServerURL, &result, NULL);
+ ws = Connect(kEchoServerURL, &result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
result = websocket_interface_->Close(ws,
@@ -507,7 +515,7 @@ std::string TestWebSocket::TestValidClose() {
core_interface_->ReleaseResource(ws);
// Close with ongoing receive message.
- ws = Connect(kEchoServerURL, &result, NULL);
+ ws = Connect(kEchoServerURL, &result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
PP_Var receive_message_var;
@@ -558,7 +566,7 @@ std::string TestWebSocket::TestGetProtocol() {
std::string TestWebSocket::TestTextSendReceive() {
// Connect to test echo server.
int32_t connect_result;
- PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL);
+ PP_Resource ws = Connect(kEchoServerURL, &connect_result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, connect_result);
@@ -588,16 +596,15 @@ std::string TestWebSocket::TestTextSendReceive() {
std::string TestWebSocket::TestBinarySendReceive() {
// Connect to test echo server.
int32_t connect_result;
- PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL);
+ PP_Resource ws = Connect(kEchoServerURL, &connect_result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, connect_result);
// Send binary message.
- uint32_t len = 256;
- uint8_t data[256];
- for (uint32_t i = 0; i < len; ++i)
- data[i] = i;
- PP_Var message_var = CreateVarBinary(data, len);
+ std::vector<uint8_t> binary(256);
+ for (uint32_t i = 0; i < binary.size(); ++i)
+ binary[i] = i;
+ PP_Var message_var = CreateVarBinary(binary);
int32_t result = websocket_interface_->SendMessage(ws, message_var);
ReleaseVar(message_var);
ASSERT_EQ(PP_OK, result);
@@ -611,7 +618,7 @@ std::string TestWebSocket::TestBinarySendReceive() {
if (result == PP_OK_COMPLETIONPENDING)
result = callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
- ASSERT_TRUE(AreEqualWithBinary(received_message, data, len));
+ ASSERT_TRUE(AreEqualWithBinary(received_message, binary));
ReleaseVar(received_message);
core_interface_->ReleaseResource(ws);
@@ -621,15 +628,13 @@ std::string TestWebSocket::TestBinarySendReceive() {
std::string TestWebSocket::TestBufferedAmount() {
// Connect to test echo server.
int32_t connect_result;
- PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL);
+ PP_Resource ws = Connect(kEchoServerURL, &connect_result, "");
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, connect_result);
// Prepare a large message that is not aligned with the internal buffer
// sizes.
- char message[8194];
- memset(message, 'x', 8193);
- message[8193] = 0;
+ std::string message(8193, 'x');
PP_Var message_var = CreateVarString(message);
uint64_t buffered_amount = 0;
@@ -711,12 +716,11 @@ std::string TestWebSocket::TestCcInterfaces() {
result = ws.SendMessage(pp::Var(text_message));
ASSERT_EQ(PP_OK, result);
- uint32_t binary_length = 256;
- uint8_t binary_message[256];
- for (uint32_t i = 0; i < binary_length; ++i)
- binary_message[i] = i;
- result = ws.SendMessage(pp::Var(
- pp::PASS_REF, CreateVarBinary(binary_message, binary_length)));
+ std::vector<uint8_t> binary(256);
+ for (uint32_t i = 0; i < binary.size(); ++i)
+ binary[i] = i;
+ result = ws.SendMessage(
+ pp::Var(pp::PASS_REF, CreateVarBinary(binary)));
ASSERT_EQ(PP_OK, result);
pp::Var text_receive_var;
@@ -734,8 +738,7 @@ std::string TestWebSocket::TestCcInterfaces() {
if (result == PP_OK_COMPLETIONPENDING)
result = binary_receive_callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
- ASSERT_TRUE(AreEqualWithBinary(
- binary_receive_var.pp_var(), binary_message, binary_length));
+ ASSERT_TRUE(AreEqualWithBinary(binary_receive_var.pp_var(), binary));
TestCompletionCallback close_callback(instance_->pp_instance());
std::string reason("bye");
@@ -1009,11 +1012,13 @@ std::string TestWebSocket::TestUtilityBinarySendReceive() {
websocket.WaitForConnected();
// Send binary message.
- pp::VarArrayBuffer message(256);
- uint32_t len = message.ByteLength();
- uint8_t* data = static_cast<uint8_t*>(message.Map());
+ uint32_t len = 256;
+ std::vector<uint8_t> binary(len);
for (uint32_t i = 0; i < len; ++i)
- data[i] = i;
+ binary[i] = i;
+ pp::VarArrayBuffer message(len);
+ uint8_t* var_data = static_cast<uint8_t*>(message.Map());
+ std::copy(binary.begin(), binary.end(), var_data);
result = websocket.Send(message);
ASSERT_EQ(PP_OK, result);
@@ -1024,7 +1029,7 @@ std::string TestWebSocket::TestUtilityBinarySendReceive() {
ASSERT_EQ(2U, events.size());
ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type);
ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[1].event_type);
- ASSERT_TRUE(AreEqualWithBinary(events[1].var.pp_var(), data, len));
+ ASSERT_TRUE(AreEqualWithBinary(events[1].var.pp_var(), binary));
PASS();
}
@@ -1040,15 +1045,11 @@ std::string TestWebSocket::TestUtilityBufferedAmount() {
// Prepare a large message that is not aligned with the internal buffer
// sizes.
- char message_char[8194];
- memset(message_char, 'x', 8193);
- message_char[8193] = 0;
- std::string message_str(message_char);
-
+ std::string message(8193, 'x');
uint64_t buffered_amount = 0;
uint32_t sent;
for (sent = 0; sent < 100; sent++) {
- result = websocket.Send(pp::Var(message_str));
+ result = websocket.Send(pp::Var(message));
ASSERT_EQ(PP_OK, result);
buffered_amount = websocket.GetBufferedAmount();
// Buffered amount size 262144 is too big for the internal buffer size.
@@ -1057,9 +1058,9 @@ std::string TestWebSocket::TestUtilityBufferedAmount() {
}
// Close connection.
- std::string reason_str = "close while busy";
+ std::string reason = "close while busy";
result = websocket.Close(
- PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE, pp::Var(reason_str));
+ PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE, pp::Var(reason));
ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING, websocket.GetReadyState());
websocket.WaitForClosed();
ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED, websocket.GetReadyState());
@@ -1075,10 +1076,10 @@ std::string TestWebSocket::TestUtilityBufferedAmount() {
ASSERT_EQ(base_buffered_amount + kMessageFrameOverhead, buffered_amount);
base_buffered_amount = buffered_amount;
- result = websocket.Send(pp::Var(reason_str));
+ result = websocket.Send(pp::Var(reason));
ASSERT_EQ(PP_ERROR_FAILED, result);
buffered_amount = websocket.GetBufferedAmount();
- uint64_t reason_frame_size = kMessageFrameOverhead + reason_str.length();
+ uint64_t reason_frame_size = kMessageFrameOverhead + reason.length();
ASSERT_EQ(base_buffered_amount + reason_frame_size, buffered_amount);
const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents();
@@ -1087,7 +1088,7 @@ std::string TestWebSocket::TestUtilityBufferedAmount() {
size_t last_event = events_on_closed - 1;
for (uint32_t i = 1; i < last_event; ++i) {
ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[i].event_type);
- ASSERT_TRUE(AreEqualWithString(events[i].var.pp_var(), message_char));
+ ASSERT_TRUE(AreEqualWithString(events[i].var.pp_var(), message));
}
ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[last_event].event_type);
ASSERT_TRUE(events[last_event].was_clean);
diff --git a/ppapi/tests/test_websocket.h b/ppapi/tests/test_websocket.h
index 8024bb7..027b3be 100644
--- a/ppapi/tests/test_websocket.h
+++ b/ppapi/tests/test_websocket.h
@@ -22,15 +22,16 @@ class TestWebSocket : public TestCase {
virtual bool Init();
virtual void RunTests(const std::string& filter);
- PP_Var CreateVarString(const char* string);
- PP_Var CreateVarBinary(const uint8_t* data, uint32_t size);
+ PP_Var CreateVarString(const std::string& string);
+ PP_Var CreateVarBinary(const std::vector<uint8_t>& binary);
void ReleaseVar(const PP_Var& var);
- bool AreEqualWithString(const PP_Var& var, const char* string);
+ bool AreEqualWithString(const PP_Var& var, const std::string& string);
bool AreEqualWithBinary(const PP_Var& var,
- const uint8_t* data,
- uint32_t size);
+ const std::vector<uint8_t>& binary);
- PP_Resource Connect(const char* url, int32_t* result, const char* protocol);
+ PP_Resource Connect(const std::string& url,
+ int32_t* result,
+ const std::string& protocol);
std::string TestIsWebSocket();
std::string TestUninitializedPropertiesAccess();
@@ -58,7 +59,8 @@ class TestWebSocket : public TestCase {
std::string TestUtilityBinarySendReceive();
std::string TestUtilityBufferedAmount();
- // Used by the tests that access the C API directly.
+ // Keeps Pepper API interfaces. These are used by the tests that access the C
+ // API directly.
const PPB_WebSocket* websocket_interface_;
const PPB_Var* var_interface_;
const PPB_VarArrayBuffer* arraybuffer_interface_;
diff --git a/ppapi/thunk/ppb_websocket_api.h b/ppapi/thunk/ppb_websocket_api.h
index c769cec..04fc960 100644
--- a/ppapi/thunk/ppb_websocket_api.h
+++ b/ppapi/thunk/ppb_websocket_api.h
@@ -11,27 +11,65 @@
namespace ppapi {
namespace thunk {
+// Some arguments and attributes are based on The WebSocket Protocol and The
+// WebSocket API. See also following official specifications.
+// - The WebSocket Protocol http://tools.ietf.org/html/rfc6455
+// - The WebSocket API http://dev.w3.org/html5/websockets/
class PPB_WebSocket_API {
public:
virtual ~PPB_WebSocket_API() {}
+ // Connects to the specified WebSocket server with |protocols| argument
+ // defined by the WebSocket API. Returns an int32_t error code from
+ // pp_errors.h.
virtual int32_t Connect(PP_Var url,
const PP_Var protocols[],
uint32_t protocol_count,
PP_CompletionCallback callback) = 0;
+
+ // Closes the established connection with specified |code| and |reason|.
+ // Returns an int32_t error code from pp_errors.h.
virtual int32_t Close(uint16_t code,
PP_Var reason,
PP_CompletionCallback callback) = 0;
+
+ // Receives a message from the WebSocket server. Caller must keep specified
+ // |message| object as valid until completion callback is invoked. Returns an
+ // int32_t error code from pp_errors.h.
virtual int32_t ReceiveMessage(PP_Var* message,
PP_CompletionCallback callback) = 0;
+
+ // Sends a message to the WebSocket server. Returns an int32_t error code
+ // from pp_errors.h.
virtual int32_t SendMessage(PP_Var message) = 0;
+
+ // Returns the bufferedAmount attribute of The WebSocket API.
virtual uint64_t GetBufferedAmount() = 0;
+
+ // Returns the CloseEvent code attribute of The WebSocket API. Returned code
+ // is valid if the connection is already closed and wasClean attribute is
+ // true.
virtual uint16_t GetCloseCode() = 0;
+
+ // Returns the CloseEvent reason attribute of The WebSocket API. Returned
+ // code is valid if the connection is already closed and wasClean attribute
+ // is true.
virtual PP_Var GetCloseReason() = 0;
+
+ // Returns the CloseEvent wasClean attribute of The WebSocket API. Returned
+ // code is valid if the connection is already closed.
virtual PP_Bool GetCloseWasClean() = 0;
+
+ // Returns the extensions attribute of The WebSocket API.
virtual PP_Var GetExtensions() = 0;
+
+ // Returns the protocol attribute of The WebSocket API.
virtual PP_Var GetProtocol() = 0;
+
+ // Returns the readState attribute of The WebSocket API.
virtual PP_WebSocketReadyState GetReadyState() = 0;
+
+ // Returns the url attribute of The WebSocket API.
virtual PP_Var GetURL() = 0;
};
diff --git a/webkit/plugins/ppapi/ppb_websocket_impl.cc b/webkit/plugins/ppapi/ppb_websocket_impl.cc
index e71145f..9157b67c 100644
--- a/webkit/plugins/ppapi/ppb_websocket_impl.cc
+++ b/webkit/plugins/ppapi/ppb_websocket_impl.cc
@@ -17,13 +17,13 @@
#include "ppapi/c/ppb_var_array_buffer.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/shared_impl/var_tracker.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.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"
@@ -144,23 +144,22 @@ int32_t PPB_WebSocket_Impl::Connect(PP_Var url,
// TODO(toyoshim): Similar function exist in WebKit::WebSocket.
// We must rearrange them into WebKit::WebChannel and share its protocol
// related implementation via WebKit API.
- scoped_refptr<StringVar> string_var;
- string_var = StringVar::FromPPVar(protocols[i]);
+ scoped_refptr<StringVar> protocol(StringVar::FromPPVar(protocols[i]));
- // Check duplicated protocol entries.
- if (protocol_set.find(string_var->value()) != protocol_set.end())
+ // Check invalid and empty entries.
+ if (!protocol || !protocol->value().length())
return PP_ERROR_BADARGUMENT;
- protocol_set.insert(string_var->value());
- // Check invalid and empty entries.
- if (!string_var || !string_var->value().length())
+ // Check duplicated protocol entries.
+ if (protocol_set.find(protocol->value()) != protocol_set.end())
return PP_ERROR_BADARGUMENT;
+ protocol_set.insert(protocol->value());
// Check containing characters.
- for (std::string::const_iterator it = string_var->value().begin();
- it != string_var->value().end();
+ for (std::string::const_iterator it = protocol->value().begin();
+ it != protocol->value().end();
++it) {
- uint8_t character = static_cast<uint8_t>(*it);
+ uint8_t character = *it;
// WebSocket specification says "(Subprotocol string must consist of)
// characters in the range U+0021 to U+007E not including separator
// characters as defined in [RFC2616]."
@@ -178,7 +177,7 @@ int32_t PPB_WebSocket_Impl::Connect(PP_Var url,
// Join protocols with the comma separator.
if (i != 0)
protocol_string.append(",");
- protocol_string.append(string_var->value());
+ protocol_string.append(protocol->value());
}
WebString web_protocols = WebString::fromUTF8(protocol_string);
@@ -485,8 +484,7 @@ void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount,
const WebString& reason) {
// Store code and reason.
close_code_ = code;
- std::string reason_string = reason.utf8();
- close_reason_ = new StringVar(reason_string);
+ close_reason_ = new StringVar(reason.utf8());
// Set close_was_clean_.
bool was_clean =
diff --git a/webkit/plugins/ppapi/ppb_websocket_impl.h b/webkit/plugins/ppapi/ppb_websocket_impl.h
index e429918..765e6c9 100644
--- a/webkit/plugins/ppapi/ppb_websocket_impl.h
+++ b/webkit/plugins/ppapi/ppb_websocket_impl.h
@@ -7,6 +7,7 @@
#include <queue>
+#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/tracked_callback.h"
@@ -17,7 +18,7 @@
namespace ppapi {
class StringVar;
class Var;
-}
+} // namespace ppapi
namespace webkit {
namespace ppapi {
@@ -34,6 +35,7 @@ class PPB_WebSocket_Impl : public ::ppapi::Resource,
static PP_Resource Create(PP_Instance instance);
// Resource overrides.
+ // Returns the pointer to the thunk::PPB_WebSocket_API if it's supported.
virtual ::ppapi::thunk::PPB_WebSocket_API* AsPPB_WebSocket_API() OVERRIDE;
// PPB_WebSocket_API implementation.
@@ -45,7 +47,7 @@ class PPB_WebSocket_Impl : public ::ppapi::Resource,
PP_Var reason,
PP_CompletionCallback callback) OVERRIDE;
virtual int32_t ReceiveMessage(PP_Var* message,
- PP_CompletionCallback callbac) OVERRIDE;
+ PP_CompletionCallback callback) OVERRIDE;
virtual int32_t SendMessage(PP_Var message) OVERRIDE;
virtual uint64_t GetBufferedAmount() OVERRIDE;
virtual uint16_t GetCloseCode() OVERRIDE;
@@ -68,28 +70,77 @@ class PPB_WebSocket_Impl : public ::ppapi::Resource,
unsigned short code,
const WebKit::WebString& reason);
private:
+ // Picks up a received message and moves it to user receiving buffer. This
+ // function is used in both ReceiveMessage for fast returning path and
+ // didReceiveMessage callback.
int32_t DoReceive();
+ // Keeps the WebKit side WebSocket object. This is used for calling WebKit
+ // side functions via WebKit API.
scoped_ptr<WebKit::WebSocket> websocket_;
+
+ // Represents readyState described in the WebSocket API specification. It can
+ // be read via GetReadyState().
PP_WebSocketReadyState state_;
+
+ // Becomes true if any error is detected. Incoming data will be disposed
+ // if this variable is true, then ReceiveMessage() returns PP_ERROR_FAILED
+ // after returning all received data.
bool error_was_received_;
+ // Keeps a completion callback for asynchronous Connect().
scoped_refptr< ::ppapi::TrackedCallback> connect_callback_;
+ // Keeps a completion callback for asynchronous ReceiveMessage().
scoped_refptr< ::ppapi::TrackedCallback> receive_callback_;
+
+ // Keeps a pointer to PP_Var which is provided via ReceiveMessage().
+ // Received data will be copied to this PP_Var on ready.
PP_Var* receive_callback_var_;
+
+ // Becomes true when asynchronous ReceiveMessage() is processed.
bool wait_for_receive_;
+
+ // Keeps received data until ReceiveMessage() requests.
std::queue< scoped_refptr< ::ppapi::Var> > received_messages_;
+ // Keeps a completion callback for asynchronous Close().
scoped_refptr< ::ppapi::TrackedCallback> close_callback_;
+
+ // Keeps the status code field of closing handshake. It can be read via
+ // GetCloseCode().
uint16_t close_code_;
+
+ // Keeps the reason field of closing handshake. It can be read via
+ // GetCloseReason().
scoped_refptr< ::ppapi::StringVar> close_reason_;
+
+ // Becomes true when closing handshake is performed successfully. It can be
+ // read via GetCloseWasClean().
PP_Bool close_was_clean_;
+ // Keeps empty string for functions to return empty string.
scoped_refptr< ::ppapi::StringVar> empty_string_;
+
+ // Represents extensions described in the WebSocket API specification. It can
+ // be read via GetExtensions().
+ scoped_refptr< ::ppapi::StringVar> extensions_;
+
+ // Represents url described in the WebSocket API specification. It can be
+ // read via GetURL().
scoped_refptr< ::ppapi::StringVar> url_;
+ // Keeps the number of bytes of application data that have been queued using
+ // SendMessage(). WebKit side implementation calculates the actual amount.
+ // This is a cached value which is notified through a WebKit callback.
+ // This value is used to calculate bufferedAmount in the WebSocket API
+ // specification. The calculated value can be read via GetBufferedAmount().
uint64_t buffered_amount_;
+
+ // Keeps the number of bytes of application data that have been ignored
+ // because the connection was already closed.
+ // This value is used to calculate bufferedAmount in the WebSocket API
+ // specification. The calculated value can be read via GetBufferedAmount().
uint64_t buffered_amount_after_close_;
DISALLOW_COPY_AND_ASSIGN(PPB_WebSocket_Impl);