Index: ppapi/cpp/dev/websocket_dev.cc |
diff --git a/ppapi/cpp/dev/websocket_dev.cc b/ppapi/cpp/dev/websocket_dev.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d7fc3131a848ac40279cba2a962d5a382861b5d6 |
--- /dev/null |
+++ b/ppapi/cpp/dev/websocket_dev.cc |
@@ -0,0 +1,187 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ppapi/cpp/dev/websocket_dev.h" |
+ |
+#include "ppapi/c/pp_errors.h" |
+#include "ppapi/c/pp_macros.h" |
+#include "ppapi/cpp/completion_callback.h" |
+#include "ppapi/cpp/instance.h" |
+#include "ppapi/cpp/module.h" |
+#include "ppapi/cpp/module_impl.h" |
+#include "ppapi/cpp/var.h" |
+ |
+// TODO(toyoshim): This macro is just for review until 8885012 is landed. |
+// I must remove it before landing. |
+#define PP_ALLOW_THIS_IN_INITIALIZER_LIST(x) x |
+ |
+namespace pp { |
+ |
+namespace { |
+ |
+template <> const char* interface_name<PPB_WebSocket_Dev>() { |
+ return PPB_WEBSOCKET_DEV_INTERFACE; |
+} |
+ |
+} // namespace |
+ |
+class WebSocket_Dev::WebSocketPrivate { |
+ public: |
+ WebSocketPrivate(WebSocket_Dev* ws) |
+ : callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)), ws_(ws) { |
+ } |
+ |
+ ~WebSocketPrivate() {} |
+ |
dmichael (off chromium)
2011/12/08 21:26:49
nit: I think everything below here except the 'New
|
+ void Receive() { |
+ int32_t result; |
+ do { |
+ result = get_interface<PPB_WebSocket_Dev>()->ReceiveMessage( |
+ ws_->pp_resource(), |
+ &receive_message_var_, |
+ callback_factory_.NewOptionalCallback( |
+ &WebSocketPrivate::DidReceive).pp_completion_callback()); |
+ if (result == PP_OK) |
+ ws_->OnMessage(Var(Var::PassRef(), receive_message_var_)); |
+ } while (result == PP_OK); |
+ if (result == PP_OK_COMPLETIONPENDING) |
+ return; |
+ if (result == PP_ERROR_BADARGUMENT) |
+ return; |
+ DidClose(result); |
+ } |
+ |
+ void DidConnect(int32_t result) { |
+ if (result == PP_OK) { |
+ ws_->OnOpen(); |
+ Receive(); |
+ } else { |
+ DidClose(result); |
+ } |
+ } |
+ |
+ void DidReceive(int32_t result) { |
+ if (result == PP_OK) { |
+ ws_->OnMessage(Var(Var::PassRef(), receive_message_var_)); |
+ Receive(); |
+ } else { |
+ DidClose(result); |
+ } |
+ } |
+ |
+ void DidClose(int32_t result) { |
+ PP_Resource resource = ws_->pp_resource(); |
+ PP_Bool was_clean = |
+ get_interface<PPB_WebSocket_Dev>()->GetCloseWasClean(resource); |
+ uint16_t code = |
+ get_interface<PPB_WebSocket_Dev>()->GetCloseCode(resource); |
+ Var reason = Var(Var::PassRef(), |
+ get_interface<PPB_WebSocket_Dev>()->GetCloseReason(resource)); |
+ bool was_fully_clean = was_clean == PP_TRUE && result == PP_OK; |
+ ws_->OnClose(was_fully_clean, code, reason); |
+ } |
+ |
+ CompletionCallback NewConnectCallback() { |
+ return callback_factory_.NewOptionalCallback( |
+ &WebSocketPrivate::DidConnect); |
+ } |
+ |
+ CompletionCallback NewCloseCallback() { |
+ return callback_factory_.NewOptionalCallback(&WebSocketPrivate::DidClose); |
+ } |
+ |
+ private: |
+ pp::CompletionCallbackFactory<WebSocketPrivate> callback_factory_; |
+ WebSocket_Dev* ws_; |
+ PP_Var receive_message_var_; |
+}; |
+ |
+WebSocket_Dev::WebSocket_Dev(Instance* instance) |
+ : impl_(new WebSocketPrivate(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return; |
+ PassRefFromConstructor(get_interface<PPB_WebSocket_Dev>()->Create( |
+ instance->pp_instance())); |
+} |
+ |
+WebSocket_Dev::~WebSocket_Dev() { |
+ delete impl_; |
dmichael (off chromium)
2011/12/08 21:26:49
Could you use scoped_ptr?
Takashi Toyoshima
2011/12/09 05:41:46
Can I use scoped_ptr in NaCl world?
brettw
2011/12/09 05:58:26
We don't use base from the C++ layer, so I'd avoid
|
+} |
+ |
+int32_t WebSocket_Dev::Connect(const Var& url, const Var protocols[], |
+ uint32_t protocol_count) { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return PP_ERROR_BADRESOURCE; |
+ |
+ // Convert protocols to C interface. |
+ uint32_t allocation_protocol_count = protocol_count ? protocol_count : 1; |
+ PP_Var *c_protocols = new PP_Var[allocation_protocol_count]; |
+ if (!c_protocols) |
+ return PP_ERROR_NOMEMORY; |
+ for (uint32_t i = 0; i < protocol_count; ++i) |
+ c_protocols[i] = protocols[i].pp_var(); |
+ |
+ int32_t result = get_interface<PPB_WebSocket_Dev>()->Connect( |
+ pp_resource(), url.pp_var(), c_protocols, protocol_count, |
+ impl_->NewConnectCallback().pp_completion_callback()); |
+ delete[] c_protocols; |
+ return result; |
+} |
+ |
+int32_t WebSocket_Dev::Close(uint16_t code, const Var& reason) { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return PP_ERROR_BADRESOURCE; |
+ |
+ return get_interface<PPB_WebSocket_Dev>()->Close( |
+ pp_resource(), code, reason.pp_var(), |
+ impl_->NewCloseCallback().pp_completion_callback()); |
+} |
+ |
+int32_t WebSocket_Dev::Send(const Var& data) { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return PP_ERROR_BADRESOURCE; |
+ |
+ return get_interface<PPB_WebSocket_Dev>()->SendMessage( |
+ pp_resource(), data.pp_var()); |
+} |
+ |
+uint64_t WebSocket_Dev::GetBufferedAmount() { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return PP_ERROR_BADRESOURCE; |
dmichael (off chromium)
2011/12/08 21:26:49
Your return type is uint64_t; you probably want to
|
+ |
+ return get_interface<PPB_WebSocket_Dev>()->GetBufferedAmount(pp_resource()); |
+} |
+ |
+Var WebSocket_Dev::GetExtensions() { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return Var(); |
+ |
+ return Var(Var::PassRef(), |
+ get_interface<PPB_WebSocket_Dev>()->GetExtensions(pp_resource())); |
+} |
+ |
+Var WebSocket_Dev::GetProtocol() { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return Var(); |
+ |
+ return Var(Var::PassRef(), |
+ get_interface<PPB_WebSocket_Dev>()->GetProtocol(pp_resource())); |
+} |
+ |
+PP_WebSocketReadyState_Dev WebSocket_Dev::GetReadyState() { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return PP_WEBSOCKETREADYSTATE_INVALID_DEV; |
+ |
+ return get_interface<PPB_WebSocket_Dev>()->GetReadyState(pp_resource()); |
+} |
+ |
+Var WebSocket_Dev::GetURL() { |
+ if (!has_interface<PPB_WebSocket_Dev>()) |
+ return Var(); |
+ |
+ return Var(Var::PassRef(), |
+ get_interface<PPB_WebSocket_Dev>()->GetURL(pp_resource())); |
+} |
+ |
+} // namespace pp |