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" | 9 #include "base/bind.h" |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
15 #include "base/task.h" | 11 #include "base/task.h" |
16 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
17 #include "ppapi/proxy/plugin_dispatcher.h" | 13 #include "ppapi/proxy/plugin_dispatcher.h" |
18 #include "ppapi/proxy/plugin_resource_tracker.h" | 14 #include "ppapi/proxy/plugin_resource_tracker.h" |
19 #include "ppapi/proxy/ppapi_messages.h" | 15 #include "ppapi/proxy/ppapi_messages.h" |
20 #include "ppapi/shared_impl/resource.h" | 16 #include "ppapi/shared_impl/resource.h" |
21 #include "ppapi/thunk/ppb_udp_socket_private_api.h" | 17 #include "ppapi/shared_impl/udp_socket_impl.h" |
22 #include "ppapi/thunk/thunk.h" | 18 #include "ppapi/thunk/thunk.h" |
23 | 19 |
24 using ppapi::thunk::PPB_UDPSocket_Private_API; | |
25 | |
26 namespace ppapi { | 20 namespace ppapi { |
27 namespace proxy { | 21 namespace proxy { |
28 | 22 |
29 const int32_t kUDPSocketMaxReadSize = 1024 * 1024; | |
30 const int32_t kUDPSocketMaxWriteSize = 1024 * 1024; | |
31 | |
32 namespace { | 23 namespace { |
33 | 24 |
34 class UDPSocket; | 25 typedef std::map<uint32, UDPSocketImpl*> IDToSocketMap; |
35 | |
36 typedef std::map<uint32, UDPSocket*> IDToSocketMap; | |
37 IDToSocketMap* g_id_to_socket = NULL; | 26 IDToSocketMap* g_id_to_socket = NULL; |
38 | 27 |
39 void AbortCallback(PP_CompletionCallback callback) { | 28 void AbortCallback(PP_CompletionCallback callback) { |
40 if (callback.func) | 29 PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED); |
41 PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED); | |
42 } | 30 } |
43 | 31 |
44 class UDPSocket : public PPB_UDPSocket_Private_API, | 32 class UDPSocket : public UDPSocketImpl { |
45 public Resource { | |
46 public: | 33 public: |
47 UDPSocket(const HostResource& resource, uint32 socket_id); | 34 UDPSocket(const HostResource& resource, uint32 socket_id); |
48 virtual ~UDPSocket(); | 35 virtual ~UDPSocket(); |
49 | 36 |
50 // ResourceObjectBase overrides. | 37 virtual void SendBind(const PP_NetAddress_Private& addr) OVERRIDE; |
51 virtual PPB_UDPSocket_Private_API* AsPPB_UDPSocket_Private_API() OVERRIDE; | 38 virtual void SendRecvFrom(int32_t num_bytes) OVERRIDE; |
52 | 39 virtual void SendSendTo(const std::string& data, |
53 // PPB_UDPSocket_Private_API implementation. | 40 const PP_NetAddress_Private& addr) OVERRIDE; |
54 virtual int32_t Bind(const PP_NetAddress_Private* addr, | 41 virtual void SendClose() OVERRIDE; |
55 PP_CompletionCallback callback) OVERRIDE; | 42 virtual void PostAbort(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 | 43 |
75 private: | 44 private: |
76 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback); | 45 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 | 46 |
96 DISALLOW_COPY_AND_ASSIGN(UDPSocket); | 47 DISALLOW_COPY_AND_ASSIGN(UDPSocket); |
97 }; | 48 }; |
98 | 49 |
99 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) | 50 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) |
100 : Resource(resource), | 51 : UDPSocketImpl(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) | 52 if (!g_id_to_socket) |
115 g_id_to_socket = new IDToSocketMap(); | 53 g_id_to_socket = new IDToSocketMap(); |
116 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); | 54 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); |
117 (*g_id_to_socket)[socket_id] = this; | 55 (*g_id_to_socket)[socket_id] = this; |
118 } | 56 } |
119 | 57 |
120 UDPSocket::~UDPSocket() { | 58 UDPSocket::~UDPSocket() { |
121 Close(); | 59 Close(); |
122 } | 60 } |
123 | 61 |
124 PPB_UDPSocket_Private_API* UDPSocket::AsPPB_UDPSocket_Private_API() { | 62 void UDPSocket::SendBind(const PP_NetAddress_Private& addr) { |
125 return this; | 63 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Bind(socket_id_, addr)); |
126 } | 64 } |
127 | 65 |
128 int32_t UDPSocket::Bind(const PP_NetAddress_Private* addr, | 66 void UDPSocket::SendRecvFrom(int32_t num_bytes) { |
129 PP_CompletionCallback callback) { | 67 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 } | 68 } |
144 | 69 |
145 int32_t UDPSocket::RecvFrom(char* buffer, | 70 void UDPSocket::SendSendTo(const std::string& data, |
146 int32_t num_bytes, | 71 const PP_NetAddress_Private& addr) { |
147 PP_CompletionCallback callback) { | 72 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 } | 73 } |
165 | 74 |
166 PP_Bool UDPSocket::GetRecvFromAddress(PP_NetAddress_Private* addr) { | 75 void UDPSocket::SendClose() { |
167 if (!addr) | 76 // 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. | 77 // from the proxy. |
207 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); | 78 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); |
208 g_id_to_socket->erase(socket_id_); | 79 g_id_to_socket->erase(socket_id_); |
209 | 80 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 } | 81 } |
218 | 82 |
219 void UDPSocket::OnBindCompleted(bool succeeded) { | 83 void UDPSocket::PostAbort(PP_CompletionCallback callback) { |
220 if (!bind_callback_.func) { | 84 MessageLoop::current()->PostTask(FROM_HERE, |
221 NOTREACHED(); | 85 base::Bind(&AbortCallback, callback)); |
222 return; | |
223 } | |
224 | |
225 if (succeeded) | |
226 binded_ = true; | |
227 | |
228 PP_RunAndClearCompletionCallback(&bind_callback_, | |
229 succeeded ? PP_OK : PP_ERROR_FAILED); | |
230 } | 86 } |
231 | 87 |
232 void UDPSocket::OnRecvFromCompleted(bool succeeded, | 88 void UDPSocket::SendToBrowser(IPC::Message* msg) { |
233 const std::string& data, | 89 PluginDispatcher::GetForResource(this)->SendToBrowser(msg); |
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 } | 90 } |
254 | 91 |
255 void UDPSocket::OnSendToCompleted(bool succeeded, int32_t bytes_written) { | 92 } // namespace |
256 if (!sendto_callback_.func) { | |
257 NOTREACHED(); | |
258 return; | |
259 } | |
260 | 93 |
261 PP_RunAndClearCompletionCallback( | 94 //------------------------------------------------------------------------------ |
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 | 95 |
278 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) | 96 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) |
279 : InterfaceProxy(dispatcher) { | 97 : InterfaceProxy(dispatcher) { |
280 } | 98 } |
281 | 99 |
282 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { | 100 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { |
283 } | 101 } |
284 | 102 |
285 // static | 103 // static |
286 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( | 104 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 return; | 172 return; |
355 } | 173 } |
356 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | 174 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); |
357 if (iter == g_id_to_socket->end()) | 175 if (iter == g_id_to_socket->end()) |
358 return; | 176 return; |
359 iter->second->OnSendToCompleted(succeeded, bytes_written); | 177 iter->second->OnSendToCompleted(succeeded, bytes_written); |
360 } | 178 } |
361 | 179 |
362 } // namespace proxy | 180 } // namespace proxy |
363 } // namespace ppapi | 181 } // namespace ppapi |
OLD | NEW |