Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: ppapi/proxy/ppb_flash_udp_socket_proxy.cc

Issue 7745005: Initial work for UDP Pepper API (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: add some error case logic, remove remaining warnings... Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ppapi/proxy/ppb_flash_udp_socket_proxy.h"
6
7 #include <algorithm>
8 #include <cstring>
9 #include <map>
10
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/task.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/proxy/plugin_dispatcher.h"
17 #include "ppapi/proxy/plugin_resource_tracker.h"
18 #include "ppapi/proxy/ppapi_messages.h"
19 #include "ppapi/shared_impl/resource.h"
20 #include "ppapi/thunk/ppb_flash_udp_socket_api.h"
21 #include "ppapi/thunk/thunk.h"
22 /*
23 #include "base/logging.h"
24 #include "base/memory/linked_ptr.h"
25 #include "base/message_loop.h"
26 #include "base/scoped_ptr.h"
27 #include "base/task.h"
28 #include "ppapi/c/pp_errors.h"
29 #include "ppapi/proxy/plugin_dispatcher.h"
30 //#include "ppapi/proxy/plugin_resource.h"
31 #include "ppapi/proxy/plugin_resource_tracker.h"
32 #include "ppapi/proxy/ppapi_messages.h"
33 #include "ppapi/thunk/ppb_flash_udp_socket_api.h"
34 #include "ppapi/thunk/thunk.h"
35 */
36 using ppapi::thunk::PPB_Flash_UDPSocket_API;
37
38 namespace ppapi {
39 namespace proxy {
40
41 const int32_t kFlashUDPSocketMaxReadSize = 1024 * 1024;
42 const int32_t kFlashUDPSocketMaxWriteSize = 1024 * 1024;
43
44 class FlashUDPSocket;
45
46 namespace {
47
48 typedef std::map<uint32, FlashUDPSocket*> IDToSocketMap;
49 IDToSocketMap* g_id_to_socket = NULL;
50
51 class AbortCallbackTask : public Task {
52 public:
53 explicit AbortCallbackTask(PP_CompletionCallback callback)
54 : callback_(callback) {}
55 virtual ~AbortCallbackTask() {}
56 virtual void Run() {
57 if (callback_.func)
58 PP_RunCompletionCallback(&callback_, PP_ERROR_ABORTED);
59 }
60
61 private:
62 PP_CompletionCallback callback_;
63 };
64
65 InterfaceProxy* CreateFlashUDPSocketProxy(Dispatcher* dispatcher,
66 const void* target_interface) {
67 return new PPB_Flash_UDPSocket_Proxy(dispatcher, target_interface);
68 }
69
70 } // namespace
71
72 class FlashUDPSocket : public PPB_Flash_UDPSocket_API,
73 public Resource {
74 public:
75 FlashUDPSocket(const HostResource& resource,
76 uint32 socket_id,
77 int32_t family);
78 virtual ~FlashUDPSocket();
79
80 // ResourceObjectBase overrides.
81 virtual PPB_Flash_UDPSocket_API* AsPPB_Flash_UDPSocket_API() OVERRIDE;
82
83 // PPB_Flash_UDPSocket_API implementation.
84
85 virtual int32_t Bind(const PP_Flash_NetAddress* addr,
86 PP_CompletionCallback callback) OVERRIDE;
87 virtual int32_t RecvFrom(char* buffer,
88 int32_t num_bytes,
89 const PP_Flash_NetAddress* addr,
90 PP_CompletionCallback callback) OVERRIDE;
91 virtual PP_Bool GetRecvFromAddress(PP_Flash_NetAddress* addr) OVERRIDE;
92
93 virtual int32_t SendTo(const char* buffer,
94 int32_t num_bytes,
95 const PP_Flash_NetAddress* addr,
96 PP_CompletionCallback callback) OVERRIDE;
97 virtual void Disconnect() OVERRIDE;
98
99 // Notifications from the proxy.
100 void OnReadPending(bool succeeded);
101 void OnRecvFromCompleted(bool succeeded,
102 const std::string& data,
103 const PP_Flash_NetAddress& addr);
104 void OnSendToCompleted(bool succeeded,
105 int32_t bytes_written);
106
107 private:
108 enum ConnectionState {
109 // Before a connection is successfully established (including a connect
110 // request is pending or a previous connect request failed).
111 BEFORE_CONNECT,
112 CONNECTED,
113 DISCONNECTED
114 };
115
116 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback);
117
118 PluginDispatcher* GetDispatcher() const {
119 return PluginDispatcher::GetForResource(this);
120 }
121
122 uint32 socket_id_;
123 int32_t family_;
124
125 ConnectionState connection_state_;
126
127 PP_CompletionCallback read_pending_callback_;
128 PP_CompletionCallback recvfrom_callback_;
129 PP_CompletionCallback sendto_callback_;
130
131 char* read_buffer_;
132 int32_t bytes_to_read_;
133
134 PP_Flash_NetAddress recvfrom_addr_;
135
136 DISALLOW_COPY_AND_ASSIGN(FlashUDPSocket);
137 };
138
139 FlashUDPSocket::FlashUDPSocket(const HostResource& resource,
140 uint32 socket_id,
141 int32_t family)
142 : Resource(resource),
143 socket_id_(socket_id),
144 family_(family),
145 connection_state_(BEFORE_CONNECT),
146 read_pending_callback_(PP_BlockUntilComplete()),
147 recvfrom_callback_(PP_BlockUntilComplete()),
148 sendto_callback_(PP_BlockUntilComplete()),
149 read_buffer_(NULL),
150 bytes_to_read_(-1) {
151 DCHECK_NE(socket_id != 0);
152
153 recvfrom_addr_.size = 0;
154 memset(recvfrom_addr_.data, 0, sizeof(recvfrom_addr_.data));
155
156 if (!g_id_to_socket)
157 g_id_to_socket = new IDToSocketMap();
158 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
159 (*g_id_to_socket)[socket_id] = this;
160 }
161
162 FlashUDPSocket::~FlashUDPSocket() {
163 Disconnect();
164 }
165
166 PPB_Flash_UDPSocket_API* FlashUDPSocket::AsPPB_Flash_UDPSocket_API() {
167 return this;
168 }
169
170 int32_t FlashUDPSocket::Bind(const PP_Flash_NetAddress* addr,
171 PP_CompletionCallback callback) {
172 read_pending_callback_ = callback;
173
174 scoped_ptr<IPC::Message> msg_deletor(
175 new PpapiHostMsg_PPBFlashUDPSocket_Bind(socket_id_, *addr));
176
177 GetDispatcher()->SendToBrowser(msg_deletor.release());
178
179 return 0;
180 }
181
182 int32_t FlashUDPSocket::RecvFrom(char* buffer,
183 int32_t num_bytes,
184 const PP_Flash_NetAddress* addr,
185 PP_CompletionCallback callback) {
186
187 if (!buffer || num_bytes <= 0 || !callback.func)
188 return PP_ERROR_BADARGUMENT;
189
190 if ( recvfrom_callback_.func )
191 return PP_ERROR_INPROGRESS;
192
193 read_buffer_ = buffer;
194 bytes_to_read_ = std::min(num_bytes, kFlashUDPSocketMaxReadSize);
195 recvfrom_callback_ = callback;
196
197 // Send the request, the browser will call us back via RecvFromACK.
198 GetDispatcher()->SendToBrowser(
199 new PpapiHostMsg_PPBFlashUDPSocket_RecvFrom(
200 socket_id_, num_bytes, *addr));
201 return PP_OK_COMPLETIONPENDING;
202 }
203
204 PP_Bool FlashUDPSocket::GetRecvFromAddress(PP_Flash_NetAddress* addr) {
205 if (!addr)
206 return PP_FALSE;
207
208 *addr = recvfrom_addr_;
209 return PP_TRUE;
210 }
211
212 int32_t FlashUDPSocket::SendTo(const char* buffer,
213 int32_t num_bytes,
214 const PP_Flash_NetAddress* addr,
215 PP_CompletionCallback callback) {
216 if (!callback.func)
217 return PP_ERROR_BADARGUMENT;
218
219 sendto_callback_ = callback;
220
221 // Send the request, the browser will call us back via SendToACK.
222 GetDispatcher()->SendToBrowser(
223 new PpapiHostMsg_PPBFlashUDPSocket_SendTo(
224 socket_id_, std::string(buffer, num_bytes), *addr));
225 return PP_OK_COMPLETIONPENDING;
226 }
227
228 void FlashUDPSocket::Disconnect() {
229 if (connection_state_ == DISCONNECTED)
230 return;
231
232 connection_state_ = DISCONNECTED;
233
234 // After removed from the mapping, this object won't receive any notfications
235 // from the proxy.
236 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
237 g_id_to_socket->erase(socket_id_);
238
239 GetDispatcher()->SendToBrowser(
240 new PpapiHostMsg_PPBFlashUDPSocket_Disconnect(socket_id_));
241 socket_id_ = 0;
242
243 PostAbortAndClearIfNecessary(&read_pending_callback_);
244 PostAbortAndClearIfNecessary(&recvfrom_callback_);
245 PostAbortAndClearIfNecessary(&sendto_callback_);
246 }
247
248 void FlashUDPSocket::OnReadPending(bool succeeded) {
249 if (!read_pending_callback_.func) {
250 NOTREACHED();
251 return;
252 }
253
254 PP_RunAndClearCompletionCallback(
255 &read_pending_callback_, succeeded);
256 }
257
258 void FlashUDPSocket::OnRecvFromCompleted(bool succeeded,
259 const std::string& data,
260 const PP_Flash_NetAddress& addr) {
261 if (!recvfrom_callback_.func || !read_buffer_) {
262 NOTREACHED();
263 return;
264 }
265
266 if (succeeded) {
267 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
268 if (!data.empty())
269 memcpy(read_buffer_, data.c_str(), data.size());
270 }
271 read_buffer_ = NULL;
272 bytes_to_read_ = -1;
273 recvfrom_addr_ = addr;
274
275 PP_RunAndClearCompletionCallback(
276 &recvfrom_callback_,
277 succeeded ? static_cast<int32_t>(data.size()) :
278 static_cast<int32_t>(PP_ERROR_FAILED));
279 }
280
281 void FlashUDPSocket::OnSendToCompleted(bool succeeded,
282 int32_t bytes_written) {
283 PP_RunAndClearCompletionCallback(
284 &sendto_callback_,
285 succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
286 }
287
288 void FlashUDPSocket::PostAbortAndClearIfNecessary(
289 PP_CompletionCallback* callback) {
290 DCHECK(callback);
291
292 if (callback->func) {
293 MessageLoop::current()->PostTask(FROM_HERE,
294 new AbortCallbackTask(*callback));
295 *callback = PP_BlockUntilComplete();
296 }
297 }
298
299 PPB_Flash_UDPSocket_Proxy::PPB_Flash_UDPSocket_Proxy(
300 Dispatcher* dispatcher,
301 const void* target_interface)
302 : InterfaceProxy(dispatcher, target_interface) {
303 }
304
305 PPB_Flash_UDPSocket_Proxy::~PPB_Flash_UDPSocket_Proxy() {
306 }
307
308 // static
309 const InterfaceProxy::Info* PPB_Flash_UDPSocket_Proxy::GetInfo() {
310 static const Info info = {
311 ::ppapi::thunk::GetPPB_Flash_UDPSocket_Thunk(),
312 PPB_FLASH_UDPSOCKET_INTERFACE,
313 INTERFACE_ID_PPB_FLASH_UDPSOCKET,
314 false,
315 &CreateFlashUDPSocketProxy,
316 };
317 return &info;
318 }
319
320 // static
321 PP_Resource PPB_Flash_UDPSocket_Proxy::CreateProxyResource(
322 PP_Instance instance,
323 int32_t family) {
324 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
325 if (!dispatcher)
326 return 0;
327
328 uint32 socket_id = 0;
329 dispatcher->SendToBrowser(new PpapiHostMsg_PPBFlashUDPSocket_Create(
330 INTERFACE_ID_PPB_FLASH_UDPSOCKET, dispatcher->plugin_dispatcher_id(),
331 family, &socket_id));
332 if (socket_id == 0)
333 return 0;
334
335 return (new FlashUDPSocket(HostResource::MakeInstanceOnly(instance),
336 socket_id, family))->GetReference();
337 }
338
339 bool PPB_Flash_UDPSocket_Proxy::OnMessageReceived(const IPC::Message& msg) {
340 bool handled = true;
341 IPC_BEGIN_MESSAGE_MAP(PPB_Flash_UDPSocket_Proxy, msg)
342 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashUDPSocket_ReadPendingACK,
343 OnReadPendingACK)
344 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashUDPSocket_RecvFromACK,
345 OnMsgRecvFromACK)
346 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashUDPSocket_SendToACK,
347 OnMsgSendToACK)
348 IPC_MESSAGE_UNHANDLED(handled = false)
349 IPC_END_MESSAGE_MAP()
350 return handled;
351 }
352 void PPB_Flash_UDPSocket_Proxy::OnReadPendingACK(
353 uint32 /* plugin_dispatcher_id */,
354 uint32 socket_id,
355 bool succeeded ) {
356 if (!g_id_to_socket) {
357 NOTREACHED();
358 return;
359 }
360 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
361 if (iter == g_id_to_socket->end())
362 return;
363 iter->second->OnReadPending(succeeded);
364 }
365
366 void PPB_Flash_UDPSocket_Proxy::OnMsgRecvFromACK(
367 uint32 /* plugin_dispatcher_id */,
368 uint32 socket_id,
369 bool succeeded,
370 const std::string& data,
371 const PP_Flash_NetAddress& addr) {
372 if (!g_id_to_socket) {
373 NOTREACHED();
374 return;
375 }
376 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
377 if (iter == g_id_to_socket->end())
378 return;
379 iter->second->OnRecvFromCompleted(succeeded, data, addr);
380 }
381
382 void PPB_Flash_UDPSocket_Proxy::OnMsgSendToACK(
383 uint32 /* plugin_dispatcher_id */,
384 uint32 socket_id,
385 bool succeeded,
386 int32_t bytes_written){
387 if (!g_id_to_socket) {
388 NOTREACHED();
389 return;
390 }
391 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
392 if (iter == g_id_to_socket->end())
393 return;
394 iter->second->OnSendToCompleted(succeeded, bytes_written);
395 }
396
397 } // namespace proxy
398 } // namespace ppapi
399
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698