Chromium Code Reviews| Index: ppapi/utility/websocket/websocket_api.cc |
| diff --git a/ppapi/utility/websocket/websocket_api.cc b/ppapi/utility/websocket/websocket_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a576b94ed76406e62f3452bbffc830156a5c2dc7 |
| --- /dev/null |
| +++ b/ppapi/utility/websocket/websocket_api.cc |
| @@ -0,0 +1,147 @@ |
| +// Copyright (c) 2012 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/utility/websocket/websocket_api.h" |
| + |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/c/pp_macros.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" |
| +#include "ppapi/utility/completion_callback_factory.h" |
| + |
| +namespace pp { |
| + |
| +class WebSocketAPI::Implement : public WebSocket_Dev { |
| + public: |
| + Implement(Instance* instance, WebSocketAPI* api) |
| + : WebSocket_Dev(instance), |
| + api_(api), |
| + callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| + } |
| + |
| + ~Implement() {} |
|
dmichael (off chromium)
2012/02/01 23:05:18
style nit: virtual
Takashi Toyoshima
2012/02/03 07:58:27
Done.
|
| + |
| + int32_t Connect(const Var& url, const Var protocols[], |
| + uint32_t protocol_count) { |
| + CompletionCallback callback = |
| + callback_factory_.NewOptionalCallback(&Implement::DidConnect); |
| + int32_t result = |
| + WebSocket_Dev::Connect(url, protocols, protocol_count, callback); |
| + if (result != PP_OK_COMPLETIONPENDING) { |
|
dmichael (off chromium)
2012/02/01 23:05:18
PP_OK is not possible here, right? If this class d
Takashi Toyoshima
2012/02/03 07:58:27
Yes. In successful cases it always returns asynchr
|
| + // Consumes callback here and uses PP_ERROR_ABORTED instead of result |
| + // in order to avoid side effects in DidConnect. |
| + callback.Run(PP_ERROR_ABORTED); |
|
dmichael (off chromium)
2012/02/01 23:05:18
Do all your errors that are reported immediately h
Takashi Toyoshima
2012/02/03 07:58:27
Caller catches the error code as a return value of
|
| + } |
| + 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) { |
|
dmichael (off chromium)
2012/02/01 23:05:18
ditto about documenting that PP_OK won't happen.
Takashi Toyoshima
2012/02/03 07:58:27
Ditto. I revise these comments to describe more cl
|
| + // 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(); |
| + if (!was_clean) |
| + api_->OnError(); |
| + api_->OnClose(was_clean, GetCloseCode(), GetCloseReason()); |
| + } |
| + |
| + private: |
| + WebSocketAPI* api_; |
| + CompletionCallbackFactory<Implement> callback_factory_; |
| + Var receive_message_var_; |
| +}; |
| + |
| +WebSocketAPI::WebSocketAPI(Instance* instance) |
| + : impl_(new Implement(instance, PP_ALLOW_THIS_IN_INITIALIZER_LIST(this))) { |
| +} |
| + |
| +WebSocketAPI::~WebSocketAPI() { |
| + delete impl_; |
| +} |
| + |
| +int32_t WebSocketAPI::Connect(const Var& url, const Var protocols[], |
| + uint32_t protocol_count) { |
| + return impl_->Connect(url, protocols, protocol_count); |
| +} |
| + |
| +int32_t WebSocketAPI::Close(uint16_t code, const Var& reason) { |
| + return impl_->Close(code, reason); |
| +} |
| + |
| +int32_t WebSocketAPI::Send(const Var& data) { |
| + return impl_->SendMessage(data); |
| +} |
| + |
| +uint64_t WebSocketAPI::GetBufferedAmount() { |
| + return impl_->GetBufferedAmount(); |
| +} |
| + |
| +Var WebSocketAPI::GetExtensions() { |
| + return impl_->GetExtensions(); |
| +} |
| + |
| +Var WebSocketAPI::GetProtocol() { |
| + return impl_->GetProtocol(); |
| +} |
| + |
| +PP_WebSocketReadyState_Dev WebSocketAPI::GetReadyState() { |
| + return impl_->GetReadyState(); |
| +} |
| + |
| +Var WebSocketAPI::GetURL() { |
| + return impl_->GetURL(); |
| +} |
| + |
| +bool WebSocketAPI::SetBinaryType(PP_WebSocketBinaryType_Dev binary_type) { |
| + return impl_->SetBinaryType(binary_type); |
| +} |
| + |
| +PP_WebSocketBinaryType_Dev WebSocketAPI::GetBinaryType() { |
| + return impl_->GetBinaryType(); |
| +} |
| + |
| +} // namespace pp |