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> |
7 #include <map> | 9 #include <map> |
8 | 10 |
| 11 #include "base/bind.h" |
9 #include "base/logging.h" | 12 #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" |
10 #include "ppapi/proxy/plugin_dispatcher.h" | 17 #include "ppapi/proxy/plugin_dispatcher.h" |
11 #include "ppapi/proxy/plugin_resource_tracker.h" | 18 #include "ppapi/proxy/plugin_resource_tracker.h" |
12 #include "ppapi/proxy/ppapi_messages.h" | 19 #include "ppapi/proxy/ppapi_messages.h" |
13 #include "ppapi/shared_impl/private/udp_socket_private_impl.h" | |
14 #include "ppapi/shared_impl/resource.h" | 20 #include "ppapi/shared_impl/resource.h" |
| 21 #include "ppapi/thunk/ppb_udp_socket_private_api.h" |
15 #include "ppapi/thunk/thunk.h" | 22 #include "ppapi/thunk/thunk.h" |
16 | 23 |
| 24 using ppapi::thunk::PPB_UDPSocket_Private_API; |
| 25 |
17 namespace ppapi { | 26 namespace ppapi { |
18 namespace proxy { | 27 namespace proxy { |
19 | 28 |
| 29 const int32_t kUDPSocketMaxReadSize = 1024 * 1024; |
| 30 const int32_t kUDPSocketMaxWriteSize = 1024 * 1024; |
| 31 |
20 namespace { | 32 namespace { |
21 | 33 |
22 typedef std::map<uint32, UDPSocketPrivateImpl*> IDToSocketMap; | 34 class UDPSocket; |
| 35 |
| 36 typedef std::map<uint32, UDPSocket*> IDToSocketMap; |
23 IDToSocketMap* g_id_to_socket = NULL; | 37 IDToSocketMap* g_id_to_socket = NULL; |
24 | 38 |
25 class UDPSocket : public UDPSocketPrivateImpl { | 39 void AbortCallback(PP_CompletionCallback callback) { |
| 40 if (callback.func) |
| 41 PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED); |
| 42 } |
| 43 |
| 44 class UDPSocket : public PPB_UDPSocket_Private_API, |
| 45 public Resource { |
26 public: | 46 public: |
27 UDPSocket(const HostResource& resource, uint32 socket_id); | 47 UDPSocket(const HostResource& resource, uint32 socket_id); |
28 virtual ~UDPSocket(); | 48 virtual ~UDPSocket(); |
29 | 49 |
30 virtual void SendBind(const PP_NetAddress_Private& addr) OVERRIDE; | 50 // ResourceObjectBase overrides. |
31 virtual void SendRecvFrom(int32_t num_bytes) OVERRIDE; | 51 virtual PPB_UDPSocket_Private_API* AsPPB_UDPSocket_Private_API() OVERRIDE; |
32 virtual void SendSendTo(const std::string& data, | 52 |
33 const PP_NetAddress_Private& addr) OVERRIDE; | 53 // PPB_UDPSocket_Private_API implementation. |
34 virtual void SendClose() OVERRIDE; | 54 virtual int32_t Bind(const PP_NetAddress_Private* addr, |
| 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); |
35 | 74 |
36 private: | 75 private: |
37 void SendToBrowser(IPC::Message* msg); | 76 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback); |
| 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_; |
38 | 95 |
39 DISALLOW_COPY_AND_ASSIGN(UDPSocket); | 96 DISALLOW_COPY_AND_ASSIGN(UDPSocket); |
40 }; | 97 }; |
41 | 98 |
42 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) | 99 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) |
43 : UDPSocketPrivateImpl(resource, socket_id) { | 100 : Resource(resource), |
| 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 |
44 if (!g_id_to_socket) | 114 if (!g_id_to_socket) |
45 g_id_to_socket = new IDToSocketMap(); | 115 g_id_to_socket = new IDToSocketMap(); |
46 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); | 116 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); |
47 (*g_id_to_socket)[socket_id] = this; | 117 (*g_id_to_socket)[socket_id] = this; |
48 } | 118 } |
49 | 119 |
50 UDPSocket::~UDPSocket() { | 120 UDPSocket::~UDPSocket() { |
51 Close(); | 121 Close(); |
52 } | 122 } |
53 | 123 |
54 void UDPSocket::SendBind(const PP_NetAddress_Private& addr) { | 124 PPB_UDPSocket_Private_API* UDPSocket::AsPPB_UDPSocket_Private_API() { |
55 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Bind(socket_id_, addr)); | 125 return this; |
56 } | 126 } |
57 | 127 |
58 void UDPSocket::SendRecvFrom(int32_t num_bytes) { | 128 int32_t UDPSocket::Bind(const PP_NetAddress_Private* addr, |
59 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_RecvFrom(socket_id_, num_bytes)); | 129 PP_CompletionCallback callback) { |
| 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; |
60 } | 143 } |
61 | 144 |
62 void UDPSocket::SendSendTo(const std::string& data, | 145 int32_t UDPSocket::RecvFrom(char* buffer, |
63 const PP_NetAddress_Private& addr) { | 146 int32_t num_bytes, |
64 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_SendTo(socket_id_, data, addr)); | 147 PP_CompletionCallback callback) { |
| 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; |
65 } | 164 } |
66 | 165 |
67 void UDPSocket::SendClose() { | 166 PP_Bool UDPSocket::GetRecvFromAddress(PP_NetAddress_Private* addr) { |
68 // After removed from the mapping, this object won't receive any notifications | 167 if (!addr) |
| 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 |
69 // from the proxy. | 206 // from the proxy. |
70 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); | 207 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); |
71 g_id_to_socket->erase(socket_id_); | 208 g_id_to_socket->erase(socket_id_); |
72 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Close(socket_id_)); | 209 |
| 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_); |
73 } | 217 } |
74 | 218 |
75 void UDPSocket::SendToBrowser(IPC::Message* msg) { | 219 void UDPSocket::OnBindCompleted(bool succeeded) { |
76 PluginDispatcher::GetForResource(this)->SendToBrowser(msg); | 220 if (!bind_callback_.func) { |
| 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); |
77 } | 230 } |
78 | 231 |
| 232 void UDPSocket::OnRecvFromCompleted(bool succeeded, |
| 233 const std::string& data, |
| 234 const PP_NetAddress_Private& addr) { |
| 235 if (!recvfrom_callback_.func || !read_buffer_) { |
| 236 NOTREACHED(); |
| 237 return; |
| 238 } |
| 239 |
| 240 if (succeeded) { |
| 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 } |
79 } // namespace | 276 } // namespace |
80 | 277 |
81 //------------------------------------------------------------------------------ | |
82 | |
83 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) | 278 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) |
84 : InterfaceProxy(dispatcher) { | 279 : InterfaceProxy(dispatcher) { |
85 } | 280 } |
86 | 281 |
87 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { | 282 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { |
88 } | 283 } |
89 | 284 |
90 // static | 285 // static |
91 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( | 286 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( |
92 PP_Instance instance) { | 287 PP_Instance instance) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 return; | 354 return; |
160 } | 355 } |
161 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | 356 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); |
162 if (iter == g_id_to_socket->end()) | 357 if (iter == g_id_to_socket->end()) |
163 return; | 358 return; |
164 iter->second->OnSendToCompleted(succeeded, bytes_written); | 359 iter->second->OnSendToCompleted(succeeded, bytes_written); |
165 } | 360 } |
166 | 361 |
167 } // namespace proxy | 362 } // namespace proxy |
168 } // namespace ppapi | 363 } // namespace ppapi |
OLD | NEW |