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

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

Issue 8688002: PPB_TCPSocket_Private/PPB_UDPSocket_Private are exposed to Browser (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Deleted NaCl tests. Created 9 years 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
OLDNEW
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> 7 #include <algorithm>
yzshen1 2011/11/28 20:59:16 You could remove those includes that are not neede
ygorshenin 2011/11/29 18:30:09 Done.
8 #include <cstring> 8 #include <cstring>
9 #include <map> 9 #include <map>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
yzshen1 2011/11/29 20:19:32 This is still needed, right?
ygorshenin 2011/11/30 11:50:54 No, this is not needed. Deleted.
yzshen1 2011/11/30 21:11:04 This is still needed, because DCHECK is used in th
ygorshenin 2011/12/01 10:58:31 Restored.
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/task.h" 14 #include "base/task.h"
15 #include "ppapi/c/pp_errors.h" 15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/proxy/plugin_dispatcher.h" 16 #include "ppapi/proxy/plugin_dispatcher.h"
17 #include "ppapi/proxy/plugin_resource_tracker.h" 17 #include "ppapi/proxy/plugin_resource_tracker.h"
18 #include "ppapi/proxy/ppapi_messages.h" 18 #include "ppapi/proxy/ppapi_messages.h"
19 #include "ppapi/shared_impl/resource.h" 19 #include "ppapi/shared_impl/resource.h"
20 #include "ppapi/thunk/ppb_udp_socket_private_api.h" 20 #include "ppapi/shared_impl/udp_socket_impl.h"
21 #include "ppapi/thunk/thunk.h" 21 #include "ppapi/thunk/thunk.h"
22 22
23 using ppapi::thunk::PPB_UDPSocket_Private_API;
24
25 namespace ppapi { 23 namespace ppapi {
26 namespace proxy { 24 namespace proxy {
27 25
28 const int32_t kUDPSocketMaxReadSize = 1024 * 1024;
29 const int32_t kUDPSocketMaxWriteSize = 1024 * 1024;
30
31 namespace { 26 namespace {
32 27
33 class UDPSocket; 28 typedef std::map<uint32, UDPSocketImpl*> IDToSocketMap;
34
35 typedef std::map<uint32, UDPSocket*> IDToSocketMap;
36 IDToSocketMap* g_id_to_socket = NULL; 29 IDToSocketMap* g_id_to_socket = NULL;
37 30
38 class AbortCallbackTask : public Task { 31 class AbortCallbackTask : public Task {
39 public: 32 public:
40 explicit AbortCallbackTask(PP_CompletionCallback callback) 33 explicit AbortCallbackTask(PP_CompletionCallback callback)
41 : callback_(callback) {} 34 : callback_(callback) {}
42 virtual ~AbortCallbackTask() {} 35 virtual ~AbortCallbackTask() {}
43 virtual void Run() { 36 virtual void Run() {
44 if (callback_.func) 37 if (callback_.func)
45 PP_RunCompletionCallback(&callback_, PP_ERROR_ABORTED); 38 PP_RunCompletionCallback(&callback_, PP_ERROR_ABORTED);
46 } 39 }
47 40
48 private: 41 private:
49 PP_CompletionCallback callback_; 42 PP_CompletionCallback callback_;
50 }; 43 };
51 44
52 class UDPSocket : public PPB_UDPSocket_Private_API, 45 class UDPSocket : public UDPSocketImpl {
53 public Resource {
54 public: 46 public:
55 UDPSocket(const HostResource& resource, uint32 socket_id); 47 UDPSocket(const HostResource& resource, uint32 socket_id);
56 virtual ~UDPSocket(); 48 virtual ~UDPSocket();
57 49
58 // ResourceObjectBase overrides. 50 virtual void SendBind(const PP_NetAddress_Private& addr) OVERRIDE;
59 virtual PPB_UDPSocket_Private_API* AsPPB_UDPSocket_Private_API() OVERRIDE; 51 virtual void SendRecvFrom(int32_t num_bytes) OVERRIDE;
60 52 virtual void SendSendTo(const std::string& data,
61 // PPB_UDPSocket_Private_API implementation. 53 const PP_NetAddress_Private& addr) OVERRIDE;
62 virtual int32_t Bind(const PP_NetAddress_Private* addr, 54 virtual void SendClose() OVERRIDE;
63 PP_CompletionCallback callback) OVERRIDE; 55 virtual void PostAbort(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 56
83 private: 57 private:
84 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback); 58 void SendToBrowser(IPC::Message* msg);
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 59
104 DISALLOW_COPY_AND_ASSIGN(UDPSocket); 60 DISALLOW_COPY_AND_ASSIGN(UDPSocket);
105 }; 61 };
106 62
107 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) 63 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id)
108 : Resource(resource), 64 : UDPSocketImpl(resource, socket_id) {
109 socket_id_(socket_id),
110 binded_(false),
111 closed_(false),
112 bind_callback_(PP_BlockUntilComplete()),
113 recvfrom_callback_(PP_BlockUntilComplete()),
114 sendto_callback_(PP_BlockUntilComplete()),
115 read_buffer_(NULL),
116 bytes_to_read_(-1) {
117 DCHECK(socket_id != 0);
118
119 recvfrom_addr_.size = 0;
120 memset(recvfrom_addr_.data, 0, sizeof(recvfrom_addr_.data));
121
122 if (!g_id_to_socket) 65 if (!g_id_to_socket)
123 g_id_to_socket = new IDToSocketMap(); 66 g_id_to_socket = new IDToSocketMap();
124 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); 67 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
125 (*g_id_to_socket)[socket_id] = this; 68 (*g_id_to_socket)[socket_id] = this;
126 } 69 }
127 70
128 UDPSocket::~UDPSocket() { 71 UDPSocket::~UDPSocket() {
129 Close(); 72 SendClose();
yzshen1 2011/11/28 20:59:16 Please use Close().
ygorshenin 2011/11/29 18:30:09 Done.
130 } 73 }
131 74
132 PPB_UDPSocket_Private_API* UDPSocket::AsPPB_UDPSocket_Private_API() { 75 void UDPSocket::SendToBrowser(IPC::Message* msg) {
yzshen1 2011/11/28 20:59:16 Please keep the definitions in the same order as t
ygorshenin 2011/11/29 18:30:09 Done.
133 return this; 76 PluginDispatcher::GetForResource(this)->SendToBrowser(msg);
134 } 77 }
135 78
136 int32_t UDPSocket::Bind(const PP_NetAddress_Private* addr, 79 void UDPSocket::SendBind(const PP_NetAddress_Private& addr) {
137 PP_CompletionCallback callback) { 80 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Bind(socket_id_, addr));
138 if (!addr || !callback.func)
139 return PP_ERROR_BADARGUMENT;
140 if (binded_ || closed_)
141 return PP_ERROR_FAILED;
142 if (bind_callback_.func)
143 return PP_ERROR_INPROGRESS;
144
145 bind_callback_ = callback;
146
147 GetDispatcher()->SendToBrowser(
148 new PpapiHostMsg_PPBUDPSocket_Bind(socket_id_, *addr));
149
150 return PP_OK_COMPLETIONPENDING;
151 } 81 }
152 82
153 int32_t UDPSocket::RecvFrom(char* buffer, 83 void UDPSocket::SendRecvFrom(int32_t num_bytes) {
154 int32_t num_bytes, 84 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_RecvFrom(socket_id_, num_bytes));
155 PP_CompletionCallback callback) {
156 if (!buffer || num_bytes <= 0 || !callback.func)
157 return PP_ERROR_BADARGUMENT;
158 if (!binded_)
159 return PP_ERROR_FAILED;
160 if (recvfrom_callback_.func)
161 return PP_ERROR_INPROGRESS;
162
163 read_buffer_ = buffer;
164 bytes_to_read_ = std::min(num_bytes, kUDPSocketMaxReadSize);
165 recvfrom_callback_ = callback;
166
167 // Send the request, the browser will call us back via RecvFromACK.
168 GetDispatcher()->SendToBrowser(
169 new PpapiHostMsg_PPBUDPSocket_RecvFrom(
170 socket_id_, num_bytes));
171 return PP_OK_COMPLETIONPENDING;
172 } 85 }
173 86
174 PP_Bool UDPSocket::GetRecvFromAddress(PP_NetAddress_Private* addr) { 87 void UDPSocket::SendSendTo(const std::string& data,
175 if (!addr) 88 const PP_NetAddress_Private& addr) {
176 return PP_FALSE; 89 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_SendTo(socket_id_, data, addr));
177
178 *addr = recvfrom_addr_;
179 return PP_TRUE;
180 } 90 }
181 91
182 int32_t UDPSocket::SendTo(const char* buffer, 92 void UDPSocket::SendClose() {
183 int32_t num_bytes, 93 // After removed from the mapping, this object won't receive any notifications
184 const PP_NetAddress_Private* addr,
185 PP_CompletionCallback callback) {
186 if (!buffer || num_bytes <= 0 || !addr || !callback.func)
187 return PP_ERROR_BADARGUMENT;
188 if (!binded_)
189 return PP_ERROR_FAILED;
190 if (sendto_callback_.func)
191 return PP_ERROR_INPROGRESS;
192
193 if (num_bytes > kUDPSocketMaxWriteSize)
194 num_bytes = kUDPSocketMaxWriteSize;
195
196 sendto_callback_ = callback;
197
198 // Send the request, the browser will call us back via SendToACK.
199 GetDispatcher()->SendToBrowser(
200 new PpapiHostMsg_PPBUDPSocket_SendTo(
201 socket_id_, std::string(buffer, num_bytes), *addr));
202
203 return PP_OK_COMPLETIONPENDING;
204 }
205
206 void UDPSocket::Close() {
207 if(closed_)
208 return;
209
210 binded_ = false;
211 closed_ = true;
212
213 // After removed from the mapping, this object won't receive any notfications
214 // from the proxy. 94 // from the proxy.
215 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); 95 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
216 g_id_to_socket->erase(socket_id_); 96 g_id_to_socket->erase(socket_id_);
217 97 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Close(socket_id_));
218 GetDispatcher()->SendToBrowser(
219 new PpapiHostMsg_PPBUDPSocket_Close(socket_id_));
220 socket_id_ = 0;
221
222 PostAbortAndClearIfNecessary(&bind_callback_);
223 PostAbortAndClearIfNecessary(&recvfrom_callback_);
224 PostAbortAndClearIfNecessary(&sendto_callback_);
225 } 98 }
226 99
227 void UDPSocket::OnBindCompleted(bool succeeded) { 100 void UDPSocket::PostAbort(PP_CompletionCallback callback) {
228 if (!bind_callback_.func) { 101 MessageLoop::current()->PostTask(FROM_HERE, new AbortCallbackTask(callback));
dmichael (off chromium) 2011/11/28 20:38:43 ditto, this should be changed to base::Bind (now o
ygorshenin 2011/11/29 18:30:09 Added TODO comment.
229 NOTREACHED();
230 return;
231 }
232
233 if (succeeded)
234 binded_ = true;
235
236 PP_RunAndClearCompletionCallback(&bind_callback_,
237 succeeded ? PP_OK : PP_ERROR_FAILED);
238 } 102 }
239 103
240 void UDPSocket::OnRecvFromCompleted(bool succeeded, 104 } // namespace
241 const std::string& data,
242 const PP_NetAddress_Private& addr) {
243 if (!recvfrom_callback_.func || !read_buffer_) {
244 NOTREACHED();
245 return;
246 }
247 105
248 if (succeeded) { 106 //------------------------------------------------------------------------------
249 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
250 if (!data.empty())
251 memcpy(read_buffer_, data.c_str(), data.size());
252 }
253 read_buffer_ = NULL;
254 bytes_to_read_ = -1;
255 recvfrom_addr_ = addr;
256
257 PP_RunAndClearCompletionCallback(
258 &recvfrom_callback_,
259 succeeded ? static_cast<int32_t>(data.size()) :
260 static_cast<int32_t>(PP_ERROR_FAILED));
261 }
262
263 void UDPSocket::OnSendToCompleted(bool succeeded, int32_t bytes_written) {
264 if (!sendto_callback_.func) {
265 NOTREACHED();
266 return;
267 }
268
269 PP_RunAndClearCompletionCallback(
270 &sendto_callback_,
271 succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
272 }
273
274 void UDPSocket::PostAbortAndClearIfNecessary(
275 PP_CompletionCallback* callback) {
276 DCHECK(callback);
277
278 if (callback->func) {
279 MessageLoop::current()->PostTask(FROM_HERE,
280 new AbortCallbackTask(*callback));
281 *callback = PP_BlockUntilComplete();
282 }
283 }
284 } // namespace
285 107
286 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) 108 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher)
287 : InterfaceProxy(dispatcher) { 109 : InterfaceProxy(dispatcher) {
288 } 110 }
289 111
290 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { 112 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() {
291 } 113 }
292 114
293 // static 115 // static
294 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( 116 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource(
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 return; 184 return;
363 } 185 }
364 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); 186 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
365 if (iter == g_id_to_socket->end()) 187 if (iter == g_id_to_socket->end())
366 return; 188 return;
367 iter->second->OnSendToCompleted(succeeded, bytes_written); 189 iter->second->OnSendToCompleted(succeeded, bytes_written);
368 } 190 }
369 191
370 } // namespace proxy 192 } // namespace proxy
371 } // namespace ppapi 193 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698