Index: ppapi/cpp/helper/dev/websocket_api_dev.cc |
diff --git a/ppapi/cpp/helper/dev/websocket_api_dev.cc b/ppapi/cpp/helper/dev/websocket_api_dev.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b816d45dea5099a1746f72fda62a17028ae220c |
--- /dev/null |
+++ b/ppapi/cpp/helper/dev/websocket_api_dev.cc |
@@ -0,0 +1,143 @@ |
+// 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/helper/dev/websocket_api_dev.h" |
+ |
+#include "ppapi/c/pp_errors.h" |
+#include "ppapi/c/pp_macros.h" |
+#include "ppapi/cpp/completion_callback.h" |
+#include "ppapi/cpp/dev/websocket_dev.h" |
+#include "ppapi/cpp/instance.h" |
+#include "ppapi/cpp/module.h" |
+#include "ppapi/cpp/module_impl.h" |
+#include "ppapi/cpp/var.h" |
+ |
+namespace pp { |
+ |
+namespace helper { |
brettw
2011/12/21 18:31:57
There was a thread on the Pepper mailing list and
Takashi Toyoshima
2011/12/22 07:56:29
OK. I fix it.
|
+ |
+class WebSocketAPI_Dev::Implement : public WebSocket_Dev { |
+ public: |
+ Implement(Instance* instance, WebSocketAPI_Dev* api) |
+ : WebSocket_Dev(instance), |
+ api_(api), |
+ callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+ } |
+ |
+ ~Implement() {} |
+ |
+ int32_t Connect(const Var& url, const Var protocols[], |
+ uint32_t protocol_count) { |
+ CompletionCallback callback = |
+ callback_factory_.NewOptionalCallback(&Implement::DidConnect); |
brettw
2011/12/21 18:31:57
Sorry if I wasn't clear in my email, I think we wa
Takashi Toyoshima
2011/12/22 07:56:29
I understand that you afraid of calling Connect or
|
+ int32_t result = |
+ WebSocket_Dev::Connect(url, protocols, protocol_count, callback); |
+ if (result != PP_OK_COMPLETIONPENDING) { |
+ // Consumes callback here and uses PP_ERROR_ABORTED instead of result |
+ // in order to avoid side effects in DidConnect. |
+ callback.Run(PP_ERROR_ABORTED); |
+ } |
+ return result; |
+ } |
+ |
+ int32_t Close(uint16_t code, const Var& reason) { |
+ CompletionCallback callback = |
+ callback_factory_.NewOptionalCallback(&Implement::DidClose); |
+ int32_t result = WebSocket_Dev::Close(code, reason, callback); |
+ if (result != PP_OK_COMPLETIONPENDING) { |
+ // Consumes callback here and uses PP_ERROR_ABORTED instead of result |
+ // in order to avoid side effects in DidClose. |
+ callback.Run(PP_ERROR_ABORTED); |
+ } |
+ return result; |
+ } |
+ |
+ void Receive() { |
+ int32_t result; |
+ do { |
+ CompletionCallback callback = |
+ callback_factory_.NewOptionalCallback(&Implement::DidReceive); |
+ result = WebSocket_Dev::ReceiveMessage(&receive_message_var_, callback); |
+ if (result != PP_OK_COMPLETIONPENDING) |
+ callback.Run(result); |
+ } while (result == PP_OK); |
+ } |
+ |
+ void DidConnect(int32_t result) { |
+ if (result == PP_OK) { |
+ api_->OnOpen(); |
+ Receive(); |
+ } else if (result != PP_ERROR_ABORTED) { |
+ DidClose(result); |
+ } |
+ } |
+ |
+ void DidReceive(int32_t result) { |
+ if (result == PP_OK) { |
+ api_->OnMessage(receive_message_var_); |
+ Receive(); |
+ } else if (result != PP_ERROR_ABORTED) { |
+ DidClose(result); |
+ } |
+ } |
+ |
+ void DidClose(int32_t result) { |
+ if (result == PP_ERROR_ABORTED) |
+ return; |
+ bool was_clean = GetCloseWasClean() && result == PP_OK; |
+ if (!was_clean) |
+ api_->OnError(); |
brettw
2011/12/21 18:31:57
Maybe we can sent the Pepper error value to OnErro
Takashi Toyoshima
2011/12/22 07:56:29
To tell the truth, only PP_ERROR_ABORTED and PP_OK
|
+ api_->OnClose(was_clean, GetCloseCode(), GetCloseReason()); |
+ } |
+ |
+ private: |
+ WebSocketAPI_Dev* api_; |
+ CompletionCallbackFactory<Implement> callback_factory_; |
+ Var receive_message_var_; |
+}; |
+ |
+WebSocketAPI_Dev::WebSocketAPI_Dev(Instance* instance) |
+ : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) { |
+} |
+ |
+WebSocketAPI_Dev::~WebSocketAPI_Dev() { |
+ delete impl_; |
+} |
+ |
+int32_t WebSocketAPI_Dev::Connect(const Var& url, const Var protocols[], |
+ uint32_t protocol_count) { |
+ return impl_->Connect(url, protocols, protocol_count); |
+} |
+ |
+int32_t WebSocketAPI_Dev::Close(uint16_t code, const Var& reason) { |
+ return impl_->Close(code, reason); |
+} |
+ |
+int32_t WebSocketAPI_Dev::Send(const Var& data) { |
+ return impl_->SendMessage(data); |
+} |
+ |
+uint64_t WebSocketAPI_Dev::GetBufferedAmount() { |
+ return impl_->GetBufferedAmount(); |
+} |
+ |
+Var WebSocketAPI_Dev::GetExtensions() { |
+ return impl_->GetExtensions(); |
+} |
+ |
+Var WebSocketAPI_Dev::GetProtocol() { |
+ return impl_->GetProtocol(); |
+} |
+ |
+PP_WebSocketReadyState_Dev WebSocketAPI_Dev::GetReadyState() { |
+ return impl_->GetReadyState(); |
+} |
+ |
+Var WebSocketAPI_Dev::GetURL() { |
+ return impl_->GetURL(); |
+} |
+ |
+} // namespace helper |
+ |
+} // namespace pp |