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..4c238e0e84c8f2ee359b52f0401e0f6adb04642d |
| --- /dev/null |
| +++ b/ppapi/utility/websocket/websocket_api.cc |
| @@ -0,0 +1,143 @@ |
| +// 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/instance.h" |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/cpp/module_impl.h" |
| +#include "ppapi/cpp/var.h" |
| +#include "ppapi/cpp/websocket.h" |
| +#include "ppapi/utility/completion_callback_factory.h" |
| + |
| +namespace pp { |
| + |
| +class WebSocketAPI::Implement : public WebSocket { |
| + public: |
| + Implement(Instance* instance, WebSocketAPI* api) |
| + : WebSocket(instance), |
| + api_(api), |
| + callback_factory_(PP_ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| + } |
| + |
| + virtual ~Implement() {} |
| + |
| + int32_t Connect(const Var& url, const Var protocols[], |
| + uint32_t protocol_count) { |
| + CompletionCallback callback = |
| + callback_factory_.NewOptionalCallback(&Implement::DidConnect); |
| + int32_t result = |
| + WebSocket::Connect(url, protocols, protocol_count, callback); |
| + if (result != PP_OK_COMPLETIONPENDING) { |
| + // In synchronous cases, sonsumes callback here and invokes callback |
|
dmichael (off chromium)
2012/02/14 17:59:57
nit: sonsumes->consumes
Takashi Toyoshima
2012/02/23 07:15:09
Done.
|
| + // with PP_ERROR_ABORTED instead of result in order to avoid side effects |
| + // in DidConnect. DidConnect ignores this invocation and doesn't call |
| + // any deligate virtual method. |
|
dmichael (off chromium)
2012/02/14 17:59:57
deligate->delegate
Takashi Toyoshima
2012/02/23 07:15:09
Done.
|
| + 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::Close(code, reason, callback); |
| + if (result != PP_OK_COMPLETIONPENDING) { |
| + // In synchronous cases, sonsumes callback here and invokes callback |
|
dmichael (off chromium)
2012/02/14 17:59:57
sonsumes->consumes
Takashi Toyoshima
2012/02/23 07:15:09
Done.
|
| + // with PP_ERROR_ABORTED instead of result in order to avoid side effects |
| + // in DidConnect. DidConnect ignores this invocation and doesn't call |
| + // any deligate virtual method. |
|
dmichael (off chromium)
2012/02/14 17:59:57
deligate->delegate
Takashi Toyoshima
2012/02/23 07:15:09
Done.
|
| + callback.Run(PP_ERROR_ABORTED); |
| + } |
| + return result; |
| + } |
| + |
| + void Receive() { |
| + int32_t result; |
| + do { |
| + CompletionCallback callback = |
| + callback_factory_.NewOptionalCallback(&Implement::DidReceive); |
| + result = WebSocket::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_->DidOpen(); |
| + Receive(); |
| + } else if (result != PP_ERROR_ABORTED) { |
| + DidClose(result); |
| + } |
| + } |
| + |
| + void DidReceive(int32_t result) { |
| + if (result == PP_OK) { |
| + api_->HandleMessage(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_->HandleError(); |
| + api_->DidClose(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 WebSocketAPI::GetReadyState() { |
| + return impl_->GetReadyState(); |
| +} |
| + |
| +Var WebSocketAPI::GetURL() { |
| + return impl_->GetURL(); |
| +} |
| + |
| +} // namespace pp |