Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ppapi/proxy/ppb_tcp_server_socket_private_proxy.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "ppapi/c/pp_errors.h" | |
| 11 #include "ppapi/proxy/plugin_dispatcher.h" | |
| 12 #include "ppapi/proxy/plugin_globals.h" | |
| 13 #include "ppapi/proxy/plugin_proxy_delegate.h" | |
| 14 #include "ppapi/proxy/plugin_resource_tracker.h" | |
| 15 #include "ppapi/proxy/ppapi_messages.h" | |
| 16 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h" | |
| 17 #include "ppapi/shared_impl/private/ppb_tcp_server_socket_shared.h" | |
| 18 #include "ppapi/shared_impl/resource.h" | |
| 19 #include "ppapi/thunk/enter.h" | |
| 20 #include "ppapi/thunk/thunk.h" | |
| 21 | |
| 22 namespace ppapi { | |
| 23 namespace proxy { | |
| 24 | |
| 25 typedef thunk::EnterResource<thunk::PPB_TCPServerSocket_Private_API> | |
| 26 EnterTCPServerSocket; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 class TCPServerSocket : public PPB_TCPServerSocket_Shared { | |
| 31 public: | |
| 32 TCPServerSocket(const HostResource& resource, uint32 plugin_dispatcher_id); | |
| 33 virtual ~TCPServerSocket(); | |
| 34 | |
| 35 virtual void OnAcceptCompleted( | |
| 36 bool succeeded, | |
| 37 uint32 tcp_socket_id, | |
| 38 const PP_NetAddress_Private& local_addr, | |
| 39 const PP_NetAddress_Private& remote_addr) OVERRIDE; | |
| 40 | |
| 41 virtual void SendListen(const PP_NetAddress_Private& addr, | |
| 42 int32_t backlog) OVERRIDE; | |
| 43 virtual void SendAccept() OVERRIDE; | |
| 44 virtual void SendStopListening() OVERRIDE; | |
| 45 | |
| 46 private: | |
| 47 void SendToBrowser(IPC::Message* msg); | |
| 48 | |
| 49 uint32 plugin_dispatcher_id_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TCPServerSocket); | |
| 52 }; | |
| 53 | |
| 54 TCPServerSocket::TCPServerSocket(const HostResource& resource, | |
| 55 uint32 plugin_dispatcher_id) | |
| 56 : PPB_TCPServerSocket_Shared(resource), | |
| 57 plugin_dispatcher_id_(plugin_dispatcher_id) { | |
| 58 } | |
| 59 | |
| 60 TCPServerSocket::~TCPServerSocket() { | |
| 61 StopListening(); | |
| 62 } | |
| 63 | |
| 64 void TCPServerSocket::OnAcceptCompleted( | |
| 65 bool succeeded, | |
| 66 uint32 accepted_socket_id, | |
| 67 const PP_NetAddress_Private& local_addr, | |
| 68 const PP_NetAddress_Private& remote_addr) { | |
| 69 if (!TrackedCallback::IsPending(accept_callback_) || !tcp_socket_buffer_) { | |
| 70 NOTREACHED(); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 if (succeeded) { | |
| 75 *tcp_socket_buffer_ = | |
| 76 PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket( | |
| 77 pp_instance(), | |
| 78 accepted_socket_id, | |
| 79 local_addr, | |
| 80 remote_addr); | |
| 81 } | |
| 82 tcp_socket_buffer_ = NULL; | |
| 83 | |
| 84 TrackedCallback::ClearAndRun(&accept_callback_, | |
| 85 succeeded ? PP_OK : PP_ERROR_FAILED); | |
| 86 } | |
| 87 | |
| 88 void TCPServerSocket::SendListen(const PP_NetAddress_Private& addr, | |
| 89 int32_t backlog) { | |
| 90 SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Listen( | |
| 91 API_ID_PPB_TCPSERVERSOCKET_PRIVATE, | |
| 92 plugin_dispatcher_id_, | |
| 93 pp_resource(), | |
| 94 addr, | |
| 95 backlog)); | |
| 96 } | |
| 97 | |
| 98 void TCPServerSocket::SendAccept() { | |
| 99 SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Accept( | |
| 100 API_ID_PPB_TCPSOCKET_PRIVATE, socket_id_)); | |
| 101 } | |
| 102 | |
| 103 void TCPServerSocket::SendStopListening() { | |
| 104 if (socket_id_ != 0) { | |
| 105 SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Destroy(socket_id_)); | |
| 106 | |
| 107 PluginDispatcher* dispatcher = | |
| 108 PluginDispatcher::GetForInstance(host_resource().instance()); | |
| 109 if (dispatcher) { | |
| 110 InterfaceProxy* proxy = | |
| 111 dispatcher->GetInterfaceProxy(API_ID_PPB_TCPSERVERSOCKET_PRIVATE); | |
| 112 PPB_TCPServerSocket_Private_Proxy* server_socket_proxy = | |
| 113 static_cast<PPB_TCPServerSocket_Private_Proxy*>(proxy); | |
| 114 server_socket_proxy->ObjectDestroyed(socket_id_); | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void TCPServerSocket::SendToBrowser(IPC::Message* msg) { | |
| 120 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(msg); | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 125 //------------------------------------------------------------------------------ | |
| 126 | |
| 127 PPB_TCPServerSocket_Private_Proxy::PPB_TCPServerSocket_Private_Proxy( | |
| 128 Dispatcher* dispatcher) | |
| 129 : InterfaceProxy(dispatcher) { | |
| 130 } | |
| 131 | |
| 132 PPB_TCPServerSocket_Private_Proxy::~PPB_TCPServerSocket_Private_Proxy() { | |
| 133 } | |
| 134 | |
| 135 PP_Resource PPB_TCPServerSocket_Private_Proxy::CreateProxyResource( | |
| 136 PP_Instance instance) { | |
| 137 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
| 138 if (!dispatcher) | |
| 139 return 0; | |
| 140 | |
| 141 TCPServerSocket* server_socket = | |
| 142 new TCPServerSocket(HostResource::MakeInstanceOnly(instance), | |
| 143 dispatcher->plugin_dispatcher_id()); | |
| 144 return server_socket->GetReference(); | |
| 145 } | |
| 146 | |
| 147 void PPB_TCPServerSocket_Private_Proxy::ObjectDestroyed(uint32 socket_id) { | |
| 148 id_to_server_socket_.erase(socket_id); | |
| 149 } | |
| 150 | |
| 151 bool PPB_TCPServerSocket_Private_Proxy::OnMessageReceived( | |
| 152 const IPC::Message& msg) { | |
| 153 bool handled = true; | |
| 154 IPC_BEGIN_MESSAGE_MAP(PPB_TCPServerSocket_Private_Proxy, msg) | |
| 155 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPServerSocket_ListenACK, OnMsgListenACK) | |
| 156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPServerSocket_AcceptACK, OnMsgAcceptACK) | |
| 157 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 158 IPC_END_MESSAGE_MAP() | |
| 159 return handled; | |
| 160 } | |
| 161 | |
| 162 void PPB_TCPServerSocket_Private_Proxy::OnMsgListenACK( | |
| 163 uint32 plugin_dispatcher_id, | |
| 164 PP_Resource socket_resource, | |
| 165 uint32 socket_id, | |
| 166 int32_t status) { | |
| 167 EnterTCPServerSocket enter(socket_resource, true); | |
| 168 if (enter.succeeded()) { | |
| 169 PPB_TCPServerSocket_Shared* server_socket = | |
| 170 static_cast<PPB_TCPServerSocket_Shared*>(enter.object()); | |
| 171 if (status == PP_OK) | |
| 172 id_to_server_socket_[socket_id] = server_socket; | |
| 173 server_socket->OnListenCompleted(socket_id, status); | |
| 174 } else if (socket_id != 0 && status == PP_OK) { | |
| 175 IPC::Message* msg = | |
| 176 new PpapiHostMsg_PPBTCPServerSocket_Destroy(socket_id); | |
| 177 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(msg); | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 void PPB_TCPServerSocket_Private_Proxy::OnMsgAcceptACK( | |
| 182 uint32 plugin_dispatcher_id, | |
| 183 uint32 server_socket_id, | |
| 184 uint32 accepted_socket_id, | |
| 185 const PP_NetAddress_Private& local_addr, | |
| 186 const PP_NetAddress_Private& remote_addr) { | |
| 187 IDToServerSocketMap::iterator it = | |
| 188 id_to_server_socket_.find(server_socket_id); | |
| 189 if (it != id_to_server_socket_.end()) { | |
| 190 bool succeeded = (accepted_socket_id != 0); | |
| 191 it->second->OnAcceptCompleted(succeeded, | |
| 192 accepted_socket_id, | |
| 193 local_addr, | |
| 194 remote_addr); | |
| 195 } else { | |
| 196 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( | |
|
yzshen1
2012/03/14 17:05:04
Send Disconnect only if accepted_socket_id is vali
ygorshenin1
2012/03/15 07:08:04
Done.
| |
| 197 new PpapiHostMsg_PPBTCPSocket_Disconnect(accepted_socket_id)); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 } // namespace proxy | |
| 202 } // namespace ppapi | |
| OLD | NEW |