| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/proxy/ppb_udp_socket_private_proxy.h" | 5 #include "ppapi/proxy/ppb_udp_socket_private_proxy.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <cstring> | |
| 9 #include <map> | 7 #include <map> |
| 10 | 8 |
| 11 #include "base/bind.h" | |
| 12 #include "base/logging.h" | 9 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/task.h" | |
| 16 #include "ppapi/c/pp_errors.h" | |
| 17 #include "ppapi/proxy/plugin_dispatcher.h" | 10 #include "ppapi/proxy/plugin_dispatcher.h" |
| 18 #include "ppapi/proxy/plugin_resource_tracker.h" | 11 #include "ppapi/proxy/plugin_resource_tracker.h" |
| 19 #include "ppapi/proxy/ppapi_messages.h" | 12 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/shared_impl/private/udp_socket_private_impl.h" |
| 20 #include "ppapi/shared_impl/resource.h" | 14 #include "ppapi/shared_impl/resource.h" |
| 21 #include "ppapi/thunk/ppb_udp_socket_private_api.h" | |
| 22 #include "ppapi/thunk/thunk.h" | 15 #include "ppapi/thunk/thunk.h" |
| 23 | 16 |
| 24 using ppapi::thunk::PPB_UDPSocket_Private_API; | |
| 25 | |
| 26 namespace ppapi { | 17 namespace ppapi { |
| 27 namespace proxy { | 18 namespace proxy { |
| 28 | 19 |
| 29 const int32_t kUDPSocketMaxReadSize = 1024 * 1024; | |
| 30 const int32_t kUDPSocketMaxWriteSize = 1024 * 1024; | |
| 31 | |
| 32 namespace { | 20 namespace { |
| 33 | 21 |
| 34 class UDPSocket; | 22 typedef std::map<uint32, UDPSocketPrivateImpl*> IDToSocketMap; |
| 35 | |
| 36 typedef std::map<uint32, UDPSocket*> IDToSocketMap; | |
| 37 IDToSocketMap* g_id_to_socket = NULL; | 23 IDToSocketMap* g_id_to_socket = NULL; |
| 38 | 24 |
| 39 void AbortCallback(PP_CompletionCallback callback) { | 25 class UDPSocket : public UDPSocketPrivateImpl { |
| 40 if (callback.func) | |
| 41 PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED); | |
| 42 } | |
| 43 | |
| 44 class UDPSocket : public PPB_UDPSocket_Private_API, | |
| 45 public Resource { | |
| 46 public: | 26 public: |
| 47 UDPSocket(const HostResource& resource, uint32 socket_id); | 27 UDPSocket(const HostResource& resource, uint32 socket_id); |
| 48 virtual ~UDPSocket(); | 28 virtual ~UDPSocket(); |
| 49 | 29 |
| 50 // ResourceObjectBase overrides. | 30 virtual void SendBind(const PP_NetAddress_Private& addr) OVERRIDE; |
| 51 virtual PPB_UDPSocket_Private_API* AsPPB_UDPSocket_Private_API() OVERRIDE; | 31 virtual void SendRecvFrom(int32_t num_bytes) OVERRIDE; |
| 52 | 32 virtual void SendSendTo(const std::string& data, |
| 53 // PPB_UDPSocket_Private_API implementation. | 33 const PP_NetAddress_Private& addr) OVERRIDE; |
| 54 virtual int32_t Bind(const PP_NetAddress_Private* addr, | 34 virtual void SendClose() OVERRIDE; |
| 55 PP_CompletionCallback callback) OVERRIDE; | |
| 56 virtual int32_t RecvFrom(char* buffer, | |
| 57 int32_t num_bytes, | |
| 58 PP_CompletionCallback callback) OVERRIDE; | |
| 59 virtual PP_Bool GetRecvFromAddress(PP_NetAddress_Private* addr) OVERRIDE; | |
| 60 | |
| 61 virtual int32_t SendTo(const char* buffer, | |
| 62 int32_t num_bytes, | |
| 63 const PP_NetAddress_Private* addr, | |
| 64 PP_CompletionCallback callback) OVERRIDE; | |
| 65 virtual void Close() OVERRIDE; | |
| 66 | |
| 67 // Notifications from the proxy. | |
| 68 void OnBindCompleted(bool succeeded); | |
| 69 void OnRecvFromCompleted(bool succeeded, | |
| 70 const std::string& data, | |
| 71 const PP_NetAddress_Private& addr); | |
| 72 void OnSendToCompleted(bool succeeded, | |
| 73 int32_t bytes_written); | |
| 74 | 35 |
| 75 private: | 36 private: |
| 76 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback); | 37 void SendToBrowser(IPC::Message* msg); |
| 77 | |
| 78 PluginDispatcher* GetDispatcher() const { | |
| 79 return PluginDispatcher::GetForResource(this); | |
| 80 } | |
| 81 | |
| 82 uint32 socket_id_; | |
| 83 | |
| 84 bool binded_; | |
| 85 bool closed_; | |
| 86 | |
| 87 PP_CompletionCallback bind_callback_; | |
| 88 PP_CompletionCallback recvfrom_callback_; | |
| 89 PP_CompletionCallback sendto_callback_; | |
| 90 | |
| 91 char* read_buffer_; | |
| 92 int32_t bytes_to_read_; | |
| 93 | |
| 94 PP_NetAddress_Private recvfrom_addr_; | |
| 95 | 38 |
| 96 DISALLOW_COPY_AND_ASSIGN(UDPSocket); | 39 DISALLOW_COPY_AND_ASSIGN(UDPSocket); |
| 97 }; | 40 }; |
| 98 | 41 |
| 99 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) | 42 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) |
| 100 : Resource(resource), | 43 : UDPSocketPrivateImpl(resource, socket_id) { |
| 101 socket_id_(socket_id), | |
| 102 binded_(false), | |
| 103 closed_(false), | |
| 104 bind_callback_(PP_BlockUntilComplete()), | |
| 105 recvfrom_callback_(PP_BlockUntilComplete()), | |
| 106 sendto_callback_(PP_BlockUntilComplete()), | |
| 107 read_buffer_(NULL), | |
| 108 bytes_to_read_(-1) { | |
| 109 DCHECK(socket_id != 0); | |
| 110 | |
| 111 recvfrom_addr_.size = 0; | |
| 112 memset(recvfrom_addr_.data, 0, sizeof(recvfrom_addr_.data)); | |
| 113 | |
| 114 if (!g_id_to_socket) | 44 if (!g_id_to_socket) |
| 115 g_id_to_socket = new IDToSocketMap(); | 45 g_id_to_socket = new IDToSocketMap(); |
| 116 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); | 46 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); |
| 117 (*g_id_to_socket)[socket_id] = this; | 47 (*g_id_to_socket)[socket_id] = this; |
| 118 } | 48 } |
| 119 | 49 |
| 120 UDPSocket::~UDPSocket() { | 50 UDPSocket::~UDPSocket() { |
| 121 Close(); | 51 Close(); |
| 122 } | 52 } |
| 123 | 53 |
| 124 PPB_UDPSocket_Private_API* UDPSocket::AsPPB_UDPSocket_Private_API() { | 54 void UDPSocket::SendBind(const PP_NetAddress_Private& addr) { |
| 125 return this; | 55 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Bind(socket_id_, addr)); |
| 126 } | 56 } |
| 127 | 57 |
| 128 int32_t UDPSocket::Bind(const PP_NetAddress_Private* addr, | 58 void UDPSocket::SendRecvFrom(int32_t num_bytes) { |
| 129 PP_CompletionCallback callback) { | 59 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_RecvFrom(socket_id_, num_bytes)); |
| 130 if (!addr || !callback.func) | |
| 131 return PP_ERROR_BADARGUMENT; | |
| 132 if (binded_ || closed_) | |
| 133 return PP_ERROR_FAILED; | |
| 134 if (bind_callback_.func) | |
| 135 return PP_ERROR_INPROGRESS; | |
| 136 | |
| 137 bind_callback_ = callback; | |
| 138 | |
| 139 GetDispatcher()->SendToBrowser( | |
| 140 new PpapiHostMsg_PPBUDPSocket_Bind(socket_id_, *addr)); | |
| 141 | |
| 142 return PP_OK_COMPLETIONPENDING; | |
| 143 } | 60 } |
| 144 | 61 |
| 145 int32_t UDPSocket::RecvFrom(char* buffer, | 62 void UDPSocket::SendSendTo(const std::string& data, |
| 146 int32_t num_bytes, | 63 const PP_NetAddress_Private& addr) { |
| 147 PP_CompletionCallback callback) { | 64 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_SendTo(socket_id_, data, addr)); |
| 148 if (!buffer || num_bytes <= 0 || !callback.func) | |
| 149 return PP_ERROR_BADARGUMENT; | |
| 150 if (!binded_) | |
| 151 return PP_ERROR_FAILED; | |
| 152 if (recvfrom_callback_.func) | |
| 153 return PP_ERROR_INPROGRESS; | |
| 154 | |
| 155 read_buffer_ = buffer; | |
| 156 bytes_to_read_ = std::min(num_bytes, kUDPSocketMaxReadSize); | |
| 157 recvfrom_callback_ = callback; | |
| 158 | |
| 159 // Send the request, the browser will call us back via RecvFromACK. | |
| 160 GetDispatcher()->SendToBrowser( | |
| 161 new PpapiHostMsg_PPBUDPSocket_RecvFrom( | |
| 162 socket_id_, num_bytes)); | |
| 163 return PP_OK_COMPLETIONPENDING; | |
| 164 } | 65 } |
| 165 | 66 |
| 166 PP_Bool UDPSocket::GetRecvFromAddress(PP_NetAddress_Private* addr) { | 67 void UDPSocket::SendClose() { |
| 167 if (!addr) | 68 // After removed from the mapping, this object won't receive any notifications |
| 168 return PP_FALSE; | |
| 169 | |
| 170 *addr = recvfrom_addr_; | |
| 171 return PP_TRUE; | |
| 172 } | |
| 173 | |
| 174 int32_t UDPSocket::SendTo(const char* buffer, | |
| 175 int32_t num_bytes, | |
| 176 const PP_NetAddress_Private* addr, | |
| 177 PP_CompletionCallback callback) { | |
| 178 if (!buffer || num_bytes <= 0 || !addr || !callback.func) | |
| 179 return PP_ERROR_BADARGUMENT; | |
| 180 if (!binded_) | |
| 181 return PP_ERROR_FAILED; | |
| 182 if (sendto_callback_.func) | |
| 183 return PP_ERROR_INPROGRESS; | |
| 184 | |
| 185 if (num_bytes > kUDPSocketMaxWriteSize) | |
| 186 num_bytes = kUDPSocketMaxWriteSize; | |
| 187 | |
| 188 sendto_callback_ = callback; | |
| 189 | |
| 190 // Send the request, the browser will call us back via SendToACK. | |
| 191 GetDispatcher()->SendToBrowser( | |
| 192 new PpapiHostMsg_PPBUDPSocket_SendTo( | |
| 193 socket_id_, std::string(buffer, num_bytes), *addr)); | |
| 194 | |
| 195 return PP_OK_COMPLETIONPENDING; | |
| 196 } | |
| 197 | |
| 198 void UDPSocket::Close() { | |
| 199 if(closed_) | |
| 200 return; | |
| 201 | |
| 202 binded_ = false; | |
| 203 closed_ = true; | |
| 204 | |
| 205 // After removed from the mapping, this object won't receive any notfications | |
| 206 // from the proxy. | 69 // from the proxy. |
| 207 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); | 70 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); |
| 208 g_id_to_socket->erase(socket_id_); | 71 g_id_to_socket->erase(socket_id_); |
| 209 | 72 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Close(socket_id_)); |
| 210 GetDispatcher()->SendToBrowser( | |
| 211 new PpapiHostMsg_PPBUDPSocket_Close(socket_id_)); | |
| 212 socket_id_ = 0; | |
| 213 | |
| 214 PostAbortAndClearIfNecessary(&bind_callback_); | |
| 215 PostAbortAndClearIfNecessary(&recvfrom_callback_); | |
| 216 PostAbortAndClearIfNecessary(&sendto_callback_); | |
| 217 } | 73 } |
| 218 | 74 |
| 219 void UDPSocket::OnBindCompleted(bool succeeded) { | 75 void UDPSocket::SendToBrowser(IPC::Message* msg) { |
| 220 if (!bind_callback_.func) { | 76 PluginDispatcher::GetForResource(this)->SendToBrowser(msg); |
| 221 NOTREACHED(); | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 if (succeeded) | |
| 226 binded_ = true; | |
| 227 | |
| 228 PP_RunAndClearCompletionCallback(&bind_callback_, | |
| 229 succeeded ? PP_OK : PP_ERROR_FAILED); | |
| 230 } | 77 } |
| 231 | 78 |
| 232 void UDPSocket::OnRecvFromCompleted(bool succeeded, | 79 } // namespace |
| 233 const std::string& data, | |
| 234 const PP_NetAddress_Private& addr) { | |
| 235 if (!recvfrom_callback_.func || !read_buffer_) { | |
| 236 NOTREACHED(); | |
| 237 return; | |
| 238 } | |
| 239 | 80 |
| 240 if (succeeded) { | 81 //------------------------------------------------------------------------------ |
| 241 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_); | |
| 242 if (!data.empty()) | |
| 243 memcpy(read_buffer_, data.c_str(), data.size()); | |
| 244 } | |
| 245 read_buffer_ = NULL; | |
| 246 bytes_to_read_ = -1; | |
| 247 recvfrom_addr_ = addr; | |
| 248 | |
| 249 PP_RunAndClearCompletionCallback( | |
| 250 &recvfrom_callback_, | |
| 251 succeeded ? static_cast<int32_t>(data.size()) : | |
| 252 static_cast<int32_t>(PP_ERROR_FAILED)); | |
| 253 } | |
| 254 | |
| 255 void UDPSocket::OnSendToCompleted(bool succeeded, int32_t bytes_written) { | |
| 256 if (!sendto_callback_.func) { | |
| 257 NOTREACHED(); | |
| 258 return; | |
| 259 } | |
| 260 | |
| 261 PP_RunAndClearCompletionCallback( | |
| 262 &sendto_callback_, | |
| 263 succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED)); | |
| 264 } | |
| 265 | |
| 266 void UDPSocket::PostAbortAndClearIfNecessary( | |
| 267 PP_CompletionCallback* callback) { | |
| 268 DCHECK(callback); | |
| 269 | |
| 270 if (callback->func) { | |
| 271 MessageLoop::current()->PostTask( | |
| 272 FROM_HERE, base::Bind(&AbortCallback, *callback)); | |
| 273 *callback = PP_BlockUntilComplete(); | |
| 274 } | |
| 275 } | |
| 276 } // namespace | |
| 277 | 82 |
| 278 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) | 83 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) |
| 279 : InterfaceProxy(dispatcher) { | 84 : InterfaceProxy(dispatcher) { |
| 280 } | 85 } |
| 281 | 86 |
| 282 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { | 87 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { |
| 283 } | 88 } |
| 284 | 89 |
| 285 // static | 90 // static |
| 286 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( | 91 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 return; | 159 return; |
| 355 } | 160 } |
| 356 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | 161 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); |
| 357 if (iter == g_id_to_socket->end()) | 162 if (iter == g_id_to_socket->end()) |
| 358 return; | 163 return; |
| 359 iter->second->OnSendToCompleted(succeeded, bytes_written); | 164 iter->second->OnSendToCompleted(succeeded, bytes_written); |
| 360 } | 165 } |
| 361 | 166 |
| 362 } // namespace proxy | 167 } // namespace proxy |
| 363 } // namespace ppapi | 168 } // namespace ppapi |
| OLD | NEW |