Chromium Code Reviews| Index: ppapi/proxy/ppb_tcp_server_socket_private_proxy.cc |
| diff --git a/ppapi/proxy/ppb_tcp_server_socket_private_proxy.cc b/ppapi/proxy/ppb_tcp_server_socket_private_proxy.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fc79699638cea265b1cf30eb40ad235cf4d595f0 |
| --- /dev/null |
| +++ b/ppapi/proxy/ppb_tcp_server_socket_private_proxy.cc |
| @@ -0,0 +1,222 @@ |
| +// 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/proxy/ppb_tcp_server_socket_private_proxy.h" |
| + |
| +#include <cstddef> |
| +#include <map> |
| + |
| +#include "base/logging.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/proxy/plugin_dispatcher.h" |
| +#include "ppapi/proxy/plugin_globals.h" |
| +#include "ppapi/proxy/plugin_proxy_delegate.h" |
| +#include "ppapi/proxy/plugin_resource_tracker.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/proxy/ppb_tcp_socket_private_proxy.h" |
| +#include "ppapi/shared_impl/private/ppb_tcp_server_socket_shared.h" |
| +#include "ppapi/shared_impl/resource.h" |
| +#include "ppapi/thunk/thunk.h" |
| + |
| +namespace ppapi { |
| +namespace proxy { |
| + |
| +namespace { |
| + |
| +typedef std::map<uint32, PPB_TCPServerSocket_Shared*> IDToServerSocketMap; |
| +IDToServerSocketMap* g_id_to_uninitialized_server_socket = NULL; |
|
brettw
2012/03/12 06:27:21
You shouldn't need this uninitialized socket ID ma
ygorshenin1
2012/03/12 12:59:38
In the PROXY case, host_resource().host_resource()
brettw
2012/03/12 15:22:49
Oh, sorry. You should just do plain EnterResource<
ygorshenin1
2012/03/13 11:33:17
Done.
|
| +IDToServerSocketMap* g_id_to_initialized_server_socket = NULL; |
| + |
| +class TCPServerSocket : public PPB_TCPServerSocket_Shared { |
| + public: |
| + TCPServerSocket(const HostResource& resource, uint32 plugin_dispatcher_id); |
| + virtual ~TCPServerSocket(); |
| + |
| + virtual void OnAcceptCompleted( |
| + bool succeeded, |
| + uint32 tcp_socket_id, |
| + const PP_NetAddress_Private& local_addr, |
| + const PP_NetAddress_Private& remote_addr) OVERRIDE; |
| + |
| + virtual void SendListen(uint32 temp_socket_id, |
| + const PP_NetAddress_Private& addr, |
| + int32_t backlog) OVERRIDE; |
| + virtual void SendAccept() OVERRIDE; |
| + virtual void SendStopListening() OVERRIDE; |
| + |
| + private: |
| + void SendToBrowser(IPC::Message* msg); |
| + |
| + uint32 plugin_dispatcher_id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TCPServerSocket); |
| +}; |
| + |
| +TCPServerSocket::TCPServerSocket(const HostResource& resource, |
| + uint32 plugin_dispatcher_id) |
| + : PPB_TCPServerSocket_Shared(resource), |
| + plugin_dispatcher_id_(plugin_dispatcher_id) { |
| + if (!g_id_to_uninitialized_server_socket) |
| + g_id_to_uninitialized_server_socket = new IDToServerSocketMap(); |
| + if (!g_id_to_initialized_server_socket) |
| + g_id_to_initialized_server_socket = new IDToServerSocketMap(); |
| +} |
| + |
| +TCPServerSocket::~TCPServerSocket() { |
| + StopListening(); |
| +} |
| + |
| +void TCPServerSocket::OnAcceptCompleted( |
| + bool succeeded, |
| + uint32 tcp_socket_id, |
| + const PP_NetAddress_Private& local_addr, |
| + const PP_NetAddress_Private& remote_addr) { |
| + if (!::ppapi::TrackedCallback::IsPending(accept_callback_) || |
| + !tcp_socket_buffer_) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + if (succeeded) { |
| + *tcp_socket_buffer_ = |
| + PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket( |
| + pp_instance(), |
| + tcp_socket_id, |
| + local_addr, |
| + remote_addr); |
| + } |
| + tcp_socket_buffer_ = NULL; |
| + |
| + ::ppapi::TrackedCallback::ClearAndRun(&accept_callback_, |
|
brettw
2012/03/12 06:27:21
You shouldn't need the ::ppapi:: anywhere in this
ygorshenin1
2012/03/13 11:33:17
Done.
|
| + succeeded ? PP_OK : PP_ERROR_FAILED); |
| +} |
| + |
| +void TCPServerSocket::SendListen(uint32 temp_socket_id, |
| + const PP_NetAddress_Private& addr, |
| + int32_t backlog) { |
| + if (!g_id_to_uninitialized_server_socket || |
| + !g_id_to_initialized_server_socket) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + (*g_id_to_uninitialized_server_socket)[temp_socket_id] = this; |
| + SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Listen( |
| + API_ID_PPB_TCPSERVERSOCKET_PRIVATE, |
| + plugin_dispatcher_id_, |
| + temp_socket_id, |
| + addr, |
| + backlog)); |
| +} |
| + |
| +void TCPServerSocket::SendAccept() { |
| + SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Accept( |
| + API_ID_PPB_TCPSOCKET_PRIVATE, |
| + real_socket_id_)); |
| +} |
| + |
| +void TCPServerSocket::SendStopListening() { |
| + if (!g_id_to_uninitialized_server_socket || |
| + !g_id_to_initialized_server_socket) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + if (real_socket_id_ == 0) { |
| + g_id_to_uninitialized_server_socket->erase(temp_socket_id_); |
| + } else { |
| + g_id_to_initialized_server_socket->erase(real_socket_id_); |
| + SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Destroy(real_socket_id_)); |
| + } |
| +} |
| + |
| +void TCPServerSocket::SendToBrowser(IPC::Message* msg) { |
| + PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(msg); |
| +} |
| + |
| +} // namespace |
| + |
| +//------------------------------------------------------------------------------ |
| + |
| +PPB_TCPServerSocket_Private_Proxy::PPB_TCPServerSocket_Private_Proxy( |
| + Dispatcher* dispatcher) |
| + : InterfaceProxy(dispatcher) { |
| +} |
| + |
| +PPB_TCPServerSocket_Private_Proxy::~PPB_TCPServerSocket_Private_Proxy() { |
| +} |
| + |
| +PP_Resource PPB_TCPServerSocket_Private_Proxy::CreateProxyResource( |
| + PP_Instance instance) { |
| + PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| + if (!dispatcher) |
| + return 0; |
| + |
| + TCPServerSocket* server_socket = |
| + new TCPServerSocket(HostResource::MakeInstanceOnly(instance), |
| + dispatcher->plugin_dispatcher_id()); |
| + return server_socket->GetReference(); |
| +} |
| + |
| +bool PPB_TCPServerSocket_Private_Proxy::OnMessageReceived( |
| + const IPC::Message& msg) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(PPB_TCPServerSocket_Private_Proxy, msg) |
| + IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPServerSocket_ListenACK, OnMsgListenACK) |
| + IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPServerSocket_AcceptACK, OnMsgAcceptACK) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void PPB_TCPServerSocket_Private_Proxy::OnMsgListenACK( |
| + uint32 plugin_dispatcher_id, |
| + uint32 real_socket_id, |
| + uint32 temp_socket_id, |
| + int32_t status) { |
|
brettw
2012/03/12 06:27:21
I think you only need g_id_to_initialized_server_s
ygorshenin1
2012/03/13 11:33:17
Done.
|
| + if (!g_id_to_uninitialized_server_socket || |
| + !g_id_to_initialized_server_socket) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + IDToServerSocketMap::iterator it = |
| + g_id_to_uninitialized_server_socket->find(temp_socket_id); |
| + if (it == g_id_to_uninitialized_server_socket->end()) { |
| + IPC::Message* msg = |
| + new PpapiHostMsg_PPBTCPServerSocket_Destroy(real_socket_id); |
| + PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(msg); |
| + } else { |
| + PPB_TCPServerSocket_Shared* server_socket = it->second; |
| + g_id_to_uninitialized_server_socket->erase(it); |
| + |
| + if (status == PP_OK) |
| + (*g_id_to_initialized_server_socket)[real_socket_id] = server_socket; |
| + server_socket->OnListenCompleted(real_socket_id, status); |
| + } |
| +} |
| + |
| +void PPB_TCPServerSocket_Private_Proxy::OnMsgAcceptACK( |
| + uint32 plugin_dispatcher_id, |
| + uint32 real_socket_id, |
| + uint32 accepted_socket_id, |
| + const PP_NetAddress_Private& local_addr, |
| + const PP_NetAddress_Private& remote_addr) { |
| + if (!g_id_to_uninitialized_server_socket || |
| + !g_id_to_initialized_server_socket) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + IDToServerSocketMap::iterator it = |
| + g_id_to_initialized_server_socket->find(real_socket_id); |
| + if (it != g_id_to_initialized_server_socket->end()) { |
| + bool succeeded = accepted_socket_id != 0; |
| + it->second->OnAcceptCompleted(succeeded, |
| + accepted_socket_id, |
| + local_addr, |
| + remote_addr); |
| + } |
| +} |
| + |
| +} // namespace proxy |
| +} // namespace ppapi |