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

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

Issue 8506016: Remove 'Flash' from TCP/UDP Pepper interfaces. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: resolved last comments Created 9 years, 1 month 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
« no previous file with comments | « ppapi/proxy/ppb_flash_udp_socket_proxy.h ('k') | ppapi/proxy/ppb_tcp_socket_private_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 using ppapi::thunk::PPB_Flash_UDPSocket_API;
24
25 namespace ppapi {
26 namespace proxy {
27
28 const int32_t kFlashUDPSocketMaxReadSize = 1024 * 1024;
29 const int32_t kFlashUDPSocketMaxWriteSize = 1024 * 1024;
30
31 namespace {
32
33 class FlashUDPSocket;
34
35 typedef std::map<uint32, FlashUDPSocket*> IDToSocketMap;
36 IDToSocketMap* g_id_to_socket = NULL;
37
38 class AbortCallbackTask : public Task {
39 public:
40 explicit AbortCallbackTask(PP_CompletionCallback callback)
41 : callback_(callback) {}
42 virtual ~AbortCallbackTask() {}
43 virtual void Run() {
44 if (callback_.func)
45 PP_RunCompletionCallback(&callback_, PP_ERROR_ABORTED);
46 }
47
48 private:
49 PP_CompletionCallback callback_;
50 };
51
52 class FlashUDPSocket : public PPB_Flash_UDPSocket_API,
53 public Resource {
54 public:
55 FlashUDPSocket(const HostResource& resource, uint32 socket_id);
56 virtual ~FlashUDPSocket();
57
58 // ResourceObjectBase overrides.
59 virtual PPB_Flash_UDPSocket_API* AsPPB_Flash_UDPSocket_API() OVERRIDE;
60
61 // PPB_Flash_UDPSocket_API implementation.
62 virtual int32_t Bind(const PP_NetAddress_Private* addr,
63 PP_CompletionCallback callback) OVERRIDE;
64 virtual int32_t RecvFrom(char* buffer,
65 int32_t num_bytes,
66 PP_CompletionCallback callback) OVERRIDE;
67 virtual PP_Bool GetRecvFromAddress(PP_NetAddress_Private* addr) OVERRIDE;
68
69 virtual int32_t SendTo(const char* buffer,
70 int32_t num_bytes,
71 const PP_NetAddress_Private* addr,
72 PP_CompletionCallback callback) OVERRIDE;
73 virtual void Close() OVERRIDE;
74
75 // Notifications from the proxy.
76 void OnBindCompleted(bool succeeded);
77 void OnRecvFromCompleted(bool succeeded,
78 const std::string& data,
79 const PP_NetAddress_Private& addr);
80 void OnSendToCompleted(bool succeeded,
81 int32_t bytes_written);
82
83 private:
84 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback);
85
86 PluginDispatcher* GetDispatcher() const {
87 return PluginDispatcher::GetForResource(this);
88 }
89
90 uint32 socket_id_;
91
92 bool binded_;
93 bool closed_;
94
95 PP_CompletionCallback bind_callback_;
96 PP_CompletionCallback recvfrom_callback_;
97 PP_CompletionCallback sendto_callback_;
98
99 char* read_buffer_;
100 int32_t bytes_to_read_;
101
102 PP_NetAddress_Private recvfrom_addr_;
103
104 DISALLOW_COPY_AND_ASSIGN(FlashUDPSocket);
105 };
106
107 FlashUDPSocket::FlashUDPSocket(const HostResource& resource,
108 uint32 socket_id)
109 : Resource(resource),
110 socket_id_(socket_id),
111 binded_(false),
112 closed_(false),
113 bind_callback_(PP_BlockUntilComplete()),
114 recvfrom_callback_(PP_BlockUntilComplete()),
115 sendto_callback_(PP_BlockUntilComplete()),
116 read_buffer_(NULL),
117 bytes_to_read_(-1) {
118 DCHECK(socket_id != 0);
119
120 recvfrom_addr_.size = 0;
121 memset(recvfrom_addr_.data, 0, sizeof(recvfrom_addr_.data));
122
123 if (!g_id_to_socket)
124 g_id_to_socket = new IDToSocketMap();
125 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
126 (*g_id_to_socket)[socket_id] = this;
127 }
128
129 FlashUDPSocket::~FlashUDPSocket() {
130 Close();
131 }
132
133 PPB_Flash_UDPSocket_API* FlashUDPSocket::AsPPB_Flash_UDPSocket_API() {
134 return this;
135 }
136
137 int32_t FlashUDPSocket::Bind(const PP_NetAddress_Private* addr,
138 PP_CompletionCallback callback) {
139 if (!addr || !callback.func)
140 return PP_ERROR_BADARGUMENT;
141 if (binded_ || closed_)
142 return PP_ERROR_FAILED;
143 if (bind_callback_.func)
144 return PP_ERROR_INPROGRESS;
145
146 bind_callback_ = callback;
147
148 GetDispatcher()->SendToBrowser(
149 new PpapiHostMsg_PPBFlashUDPSocket_Bind(socket_id_, *addr));
150
151 return PP_OK_COMPLETIONPENDING;
152 }
153
154 int32_t FlashUDPSocket::RecvFrom(char* buffer,
155 int32_t num_bytes,
156 PP_CompletionCallback callback) {
157 if (!buffer || num_bytes <= 0 || !callback.func)
158 return PP_ERROR_BADARGUMENT;
159 if (!binded_)
160 return PP_ERROR_FAILED;
161 if (recvfrom_callback_.func)
162 return PP_ERROR_INPROGRESS;
163
164 read_buffer_ = buffer;
165 bytes_to_read_ = std::min(num_bytes, kFlashUDPSocketMaxReadSize);
166 recvfrom_callback_ = callback;
167
168 // Send the request, the browser will call us back via RecvFromACK.
169 GetDispatcher()->SendToBrowser(
170 new PpapiHostMsg_PPBFlashUDPSocket_RecvFrom(
171 socket_id_, num_bytes));
172 return PP_OK_COMPLETIONPENDING;
173 }
174
175 PP_Bool FlashUDPSocket::GetRecvFromAddress(PP_NetAddress_Private* addr) {
176 if (!addr)
177 return PP_FALSE;
178
179 *addr = recvfrom_addr_;
180 return PP_TRUE;
181 }
182
183 int32_t FlashUDPSocket::SendTo(const char* buffer,
184 int32_t num_bytes,
185 const PP_NetAddress_Private* addr,
186 PP_CompletionCallback callback) {
187 if (!buffer || num_bytes <= 0 || !addr || !callback.func)
188 return PP_ERROR_BADARGUMENT;
189 if (!binded_)
190 return PP_ERROR_FAILED;
191 if (sendto_callback_.func)
192 return PP_ERROR_INPROGRESS;
193
194 if (num_bytes > kFlashUDPSocketMaxWriteSize)
195 num_bytes = kFlashUDPSocketMaxWriteSize;
196
197 sendto_callback_ = callback;
198
199 // Send the request, the browser will call us back via SendToACK.
200 GetDispatcher()->SendToBrowser(
201 new PpapiHostMsg_PPBFlashUDPSocket_SendTo(
202 socket_id_, std::string(buffer, num_bytes), *addr));
203
204 return PP_OK_COMPLETIONPENDING;
205 }
206
207 void FlashUDPSocket::Close() {
208 if(closed_)
209 return;
210
211 binded_ = false;
212 closed_ = true;
213
214 // After removed from the mapping, this object won't receive any notfications
215 // from the proxy.
216 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
217 g_id_to_socket->erase(socket_id_);
218
219 GetDispatcher()->SendToBrowser(
220 new PpapiHostMsg_PPBFlashUDPSocket_Close(socket_id_));
221 socket_id_ = 0;
222
223 PostAbortAndClearIfNecessary(&bind_callback_);
224 PostAbortAndClearIfNecessary(&recvfrom_callback_);
225 PostAbortAndClearIfNecessary(&sendto_callback_);
226 }
227
228 void FlashUDPSocket::OnBindCompleted(bool succeeded) {
229 if (!bind_callback_.func) {
230 NOTREACHED();
231 return;
232 }
233
234 if (succeeded)
235 binded_ = true;
236
237 PP_RunAndClearCompletionCallback(&bind_callback_,
238 succeeded ? PP_OK : PP_ERROR_FAILED);
239 }
240
241 void FlashUDPSocket::OnRecvFromCompleted(bool succeeded,
242 const std::string& data,
243 const PP_NetAddress_Private& addr) {
244 if (!recvfrom_callback_.func || !read_buffer_) {
245 NOTREACHED();
246 return;
247 }
248
249 if (succeeded) {
250 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
251 if (!data.empty())
252 memcpy(read_buffer_, data.c_str(), data.size());
253 }
254 read_buffer_ = NULL;
255 bytes_to_read_ = -1;
256 recvfrom_addr_ = addr;
257
258 PP_RunAndClearCompletionCallback(
259 &recvfrom_callback_,
260 succeeded ? static_cast<int32_t>(data.size()) :
261 static_cast<int32_t>(PP_ERROR_FAILED));
262 }
263
264 void FlashUDPSocket::OnSendToCompleted(bool succeeded,
265 int32_t bytes_written) {
266 if (!sendto_callback_.func) {
267 NOTREACHED();
268 return;
269 }
270
271 PP_RunAndClearCompletionCallback(
272 &sendto_callback_,
273 succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
274 }
275
276 void FlashUDPSocket::PostAbortAndClearIfNecessary(
277 PP_CompletionCallback* callback) {
278 DCHECK(callback);
279
280 if (callback->func) {
281 MessageLoop::current()->PostTask(FROM_HERE,
282 new AbortCallbackTask(*callback));
283 *callback = PP_BlockUntilComplete();
284 }
285 }
286 } // namespace
287
288 PPB_Flash_UDPSocket_Proxy::PPB_Flash_UDPSocket_Proxy(Dispatcher* dispatcher)
289 : InterfaceProxy(dispatcher) {
290 }
291
292 PPB_Flash_UDPSocket_Proxy::~PPB_Flash_UDPSocket_Proxy() {
293 }
294
295 // static
296 PP_Resource PPB_Flash_UDPSocket_Proxy::CreateProxyResource(
297 PP_Instance instance) {
298 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
299 if (!dispatcher)
300 return 0;
301
302 uint32 socket_id = 0;
303 dispatcher->SendToBrowser(new PpapiHostMsg_PPBFlashUDPSocket_Create(
304 API_ID_PPB_FLASH_UDPSOCKET, dispatcher->plugin_dispatcher_id(),
305 &socket_id));
306 if (socket_id == 0)
307 return 0;
308
309 return (new FlashUDPSocket(HostResource::MakeInstanceOnly(instance),
310 socket_id))->GetReference();
311 }
312
313 bool PPB_Flash_UDPSocket_Proxy::OnMessageReceived(const IPC::Message& msg) {
314 bool handled = true;
315 IPC_BEGIN_MESSAGE_MAP(PPB_Flash_UDPSocket_Proxy, msg)
316 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashUDPSocket_BindACK,
317 OnMsgBindACK)
318 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashUDPSocket_RecvFromACK,
319 OnMsgRecvFromACK)
320 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFlashUDPSocket_SendToACK,
321 OnMsgSendToACK)
322 IPC_MESSAGE_UNHANDLED(handled = false)
323 IPC_END_MESSAGE_MAP()
324 return handled;
325 }
326
327 void PPB_Flash_UDPSocket_Proxy::OnMsgBindACK(
328 uint32 /* plugin_dispatcher_id */,
329 uint32 socket_id,
330 bool succeeded) {
331 if (!g_id_to_socket) {
332 NOTREACHED();
333 return;
334 }
335 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
336 if (iter == g_id_to_socket->end())
337 return;
338 iter->second->OnBindCompleted(succeeded);
339 }
340
341 void PPB_Flash_UDPSocket_Proxy::OnMsgRecvFromACK(
342 uint32 /* plugin_dispatcher_id */,
343 uint32 socket_id,
344 bool succeeded,
345 const std::string& data,
346 const PP_NetAddress_Private& addr) {
347 if (!g_id_to_socket) {
348 NOTREACHED();
349 return;
350 }
351 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
352 if (iter == g_id_to_socket->end())
353 return;
354 iter->second->OnRecvFromCompleted(succeeded, data, addr);
355 }
356
357 void PPB_Flash_UDPSocket_Proxy::OnMsgSendToACK(
358 uint32 /* plugin_dispatcher_id */,
359 uint32 socket_id,
360 bool succeeded,
361 int32_t bytes_written) {
362 if (!g_id_to_socket) {
363 NOTREACHED();
364 return;
365 }
366 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
367 if (iter == g_id_to_socket->end())
368 return;
369 iter->second->OnSendToCompleted(succeeded, bytes_written);
370 }
371
372 } // namespace proxy
373 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/ppb_flash_udp_socket_proxy.h ('k') | ppapi/proxy/ppb_tcp_socket_private_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698