Chromium Code Reviews| 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..55832bb820d5cda53c494ad9082510b4db8821ed |
| --- /dev/null |
| +++ b/ppapi/cpp/dev/websocket_dev.cc |
| @@ -0,0 +1,213 @@ |
| +// 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 <set> |
| + |
| +#include "ppapi/c/pp_errors.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" |
| + |
| +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) |
| + : connect_callback_(new CompletionCallback(DidConnectCallback, this, |
| + PP_COMPLETIONCALLBACK_FLAG_OPTIONAL)), |
| + close_callback_(new CompletionCallback(DidCloseCallback, this)), |
| + receive_callback_(new CompletionCallback(DidReceiveCallback, this)), |
|
dmichael (off chromium)
2011/12/07 23:31:28
You should probably just use CompletionCallbackFac
Takashi Toyoshima
2011/12/08 14:40:10
Done.
|
| + ws_(ws) {} |
| + ~WebSocketPrivate() { |
| + delete connect_callback_; |
| + delete close_callback_; |
| + delete receive_callback_; |
| + } |
| + void Receive() { |
| + int32_t result; |
| + do { |
| + result = get_interface<PPB_WebSocket_Dev>()->ReceiveMessage( |
| + ws_->pp_resource(), |
| + &receive_message_var_, |
| + receive_callback_->pp_completion_callback()); |
|
dmichael (off chromium)
2011/12/07 23:31:28
I think you need to set the PP_COMPLETIONCALLBACK_
Takashi Toyoshima
2011/12/08 14:40:10
Done.
|
| + 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); |
| + } |
| + static void DidConnectCallback(void* user_data, int32_t result) { |
| + WebSocketPrivate* instance = FindLiveInstance(user_data); |
| + if (instance) |
| + instance->DidConnect(result); |
| + } |
| + static void DidCloseCallback(void* user_data, int32_t result) { |
| + WebSocketPrivate* instance = FindLiveInstance(user_data); |
| + if (instance) |
| + instance->DidClose(result); |
| + } |
| + static void DidReceiveCallback(void* user_data, int32_t result) { |
| + WebSocketPrivate* instance = FindLiveInstance(user_data); |
| + if (instance) |
| + instance->DidReceive(result); |
| + } |
| + static void AddLiveInstance(WebSocketPrivate* instance) { |
| + instance_set_.insert(instance); |
| + } |
| + static void RemoveLiveInstance(WebSocketPrivate* instance) { |
| + instance_set_.erase(instance); |
| + } |
| + static WebSocketPrivate* FindLiveInstance(void* user_data) { |
| + WebSocketPrivate* instance = static_cast<WebSocketPrivate*>(user_data); |
| + if (!instance || instance_set_.find(instance) == instance_set_.end()) |
| + return NULL; |
| + return instance; |
| + } |
| + |
| + CompletionCallback* connect_callback_; |
| + CompletionCallback* close_callback_; |
| + CompletionCallback* receive_callback_; |
| + |
| + private: |
| + WebSocket_Dev* ws_; |
| + PP_Var receive_message_var_; |
| + |
| + static std::set<WebSocketPrivate*> instance_set_; |
| +}; |
| + |
| +std::set<WebSocket_Dev::WebSocketPrivate*> |
| + WebSocket_Dev::WebSocketPrivate::instance_set_; |
| + |
| +WebSocket_Dev::WebSocket_Dev(Instance* instance) |
| + : impl(new WebSocketPrivate(this)) { |
| + WebSocketPrivate::AddLiveInstance(impl); |
| + if (!has_interface<PPB_WebSocket_Dev>()) |
| + return; |
| + PassRefFromConstructor(get_interface<PPB_WebSocket_Dev>()->Create( |
| + instance->pp_instance())); |
| +} |
| + |
| +WebSocket_Dev::~WebSocket_Dev() { |
| + WebSocketPrivate::RemoveLiveInstance(impl); |
| + delete impl; |
| +} |
| + |
| +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->connect_callback_->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->close_callback_->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; |
| + |
| + 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 |