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

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

Issue 8804006: Reland 8688002: PPB_TCPSocket_Private/PPB_UDPSocket_Private are exposed to Browser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix destruction issue 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 | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/ppb_tcp_socket_private_proxy.h ('k') | ppapi/proxy/ppb_udp_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
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_tcp_socket_private_proxy.h" 5 #include "ppapi/proxy/ppb_tcp_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"
12 #include "base/logging.h" 9 #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"
17 #include "ppapi/proxy/plugin_dispatcher.h" 10 #include "ppapi/proxy/plugin_dispatcher.h"
18 #include "ppapi/proxy/plugin_resource_tracker.h" 11 #include "ppapi/proxy/plugin_resource_tracker.h"
19 #include "ppapi/proxy/ppapi_messages.h" 12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/shared_impl/private/tcp_socket_private_impl.h"
20 #include "ppapi/shared_impl/resource.h" 14 #include "ppapi/shared_impl/resource.h"
21 #include "ppapi/thunk/ppb_tcp_socket_private_api.h"
22 #include "ppapi/thunk/thunk.h" 15 #include "ppapi/thunk/thunk.h"
23 16
24 using ppapi::thunk::PPB_TCPSocket_Private_API;
25
26 namespace ppapi { 17 namespace ppapi {
27 namespace proxy { 18 namespace proxy {
28 19
29 const int32_t kTCPSocketMaxReadSize = 1024 * 1024;
30 const int32_t kTCPSocketMaxWriteSize = 1024 * 1024;
31
32 class TCPSocket;
33
34 namespace { 20 namespace {
35 21
36 typedef std::map<uint32, TCPSocket*> IDToSocketMap; 22 typedef std::map<uint32, TCPSocketPrivateImpl*> IDToSocketMap;
37 IDToSocketMap* g_id_to_socket = NULL; 23 IDToSocketMap* g_id_to_socket = NULL;
38 24
39 void AbortCallback(PP_CompletionCallback callback) { 25 class TCPSocket : public TCPSocketPrivateImpl {
40 if (callback.func)
41 PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED);
42 }
43
44 } // namespace
45
46 class TCPSocket : public PPB_TCPSocket_Private_API,
47 public Resource {
48 public: 26 public:
49 TCPSocket(const HostResource& resource, uint32 socket_id); 27 TCPSocket(const HostResource& resource, uint32 socket_id);
50 virtual ~TCPSocket(); 28 virtual ~TCPSocket();
51 29
52 // Resource overrides. 30 virtual void SendConnect(const std::string& host, uint16_t port) OVERRIDE;
53 virtual PPB_TCPSocket_Private_API* AsPPB_TCPSocket_Private_API() OVERRIDE; 31 virtual void SendConnectWithNetAddress(
54 32 const PP_NetAddress_Private& addr) OVERRIDE;
55 // PPB_TCPSocket_Private_API implementation. 33 virtual void SendSSLHandshake(const std::string& server_name,
56 virtual int32_t Connect(const char* host, 34 uint16_t server_port) OVERRIDE;
57 uint16_t port, 35 virtual void SendRead(int32_t bytes_to_read) OVERRIDE;
58 PP_CompletionCallback callback) OVERRIDE; 36 virtual void SendWrite(const std::string& buffer) OVERRIDE;
59 virtual int32_t ConnectWithNetAddress( 37 virtual void SendDisconnect() OVERRIDE;
60 const PP_NetAddress_Private* addr,
61 PP_CompletionCallback callback) OVERRIDE;
62 virtual PP_Bool GetLocalAddress(PP_NetAddress_Private* local_addr) OVERRIDE;
63 virtual PP_Bool GetRemoteAddress(PP_NetAddress_Private* remote_addr) OVERRIDE;
64 virtual int32_t SSLHandshake(const char* server_name,
65 uint16_t server_port,
66 PP_CompletionCallback callback) OVERRIDE;
67 virtual int32_t Read(char* buffer,
68 int32_t bytes_to_read,
69 PP_CompletionCallback callback) OVERRIDE;
70 virtual int32_t Write(const char* buffer,
71 int32_t bytes_to_write,
72 PP_CompletionCallback callback) OVERRIDE;
73 virtual void Disconnect() OVERRIDE;
74
75 // Notifications from the proxy.
76 void OnConnectCompleted(bool succeeded,
77 const PP_NetAddress_Private& local_addr,
78 const PP_NetAddress_Private& remote_addr);
79 void OnSSLHandshakeCompleted(bool succeeded);
80 void OnReadCompleted(bool succeeded, const std::string& data);
81 void OnWriteCompleted(bool succeeded, int32_t bytes_written);
82 38
83 private: 39 private:
84 enum ConnectionState { 40 void SendToBrowser(IPC::Message* msg);
85 // Before a connection is successfully established (including a connect
86 // request is pending or a previous connect request failed).
87 BEFORE_CONNECT,
88 // A connection has been successfully established (including a request of
89 // initiating SSL is pending).
90 CONNECTED,
91 // An SSL connection has been successfully established.
92 SSL_CONNECTED,
93 // The connection has been ended.
94 DISCONNECTED
95 };
96
97 bool IsConnected() const;
98
99 PluginDispatcher* GetDispatcher() const {
100 return PluginDispatcher::GetForResource(this);
101 }
102
103 // Backend for both Connect() and ConnectWithNetAddress(). To keep things
104 // generic, the message is passed in (on error, it's deleted).
105 int32_t ConnectWithMessage(IPC::Message* msg,
106 PP_CompletionCallback callback);
107
108 void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback);
109
110 uint32 socket_id_;
111 ConnectionState connection_state_;
112
113 PP_CompletionCallback connect_callback_;
114 PP_CompletionCallback ssl_handshake_callback_;
115 PP_CompletionCallback read_callback_;
116 PP_CompletionCallback write_callback_;
117
118 char* read_buffer_;
119 int32_t bytes_to_read_;
120
121 PP_NetAddress_Private local_addr_;
122 PP_NetAddress_Private remote_addr_;
123 41
124 DISALLOW_COPY_AND_ASSIGN(TCPSocket); 42 DISALLOW_COPY_AND_ASSIGN(TCPSocket);
125 }; 43 };
126 44
127 TCPSocket::TCPSocket(const HostResource& resource, uint32 socket_id) 45 TCPSocket::TCPSocket(const HostResource& resource, uint32 socket_id)
128 : Resource(resource), 46 : TCPSocketPrivateImpl(resource, socket_id) {
129 socket_id_(socket_id),
130 connection_state_(BEFORE_CONNECT),
131 connect_callback_(PP_BlockUntilComplete()),
132 ssl_handshake_callback_(PP_BlockUntilComplete()),
133 read_callback_(PP_BlockUntilComplete()),
134 write_callback_(PP_BlockUntilComplete()),
135 read_buffer_(NULL),
136 bytes_to_read_(-1) {
137 DCHECK(socket_id != 0);
138
139 local_addr_.size = 0;
140 memset(local_addr_.data, 0, sizeof(local_addr_.data));
141 remote_addr_.size = 0;
142 memset(remote_addr_.data, 0, sizeof(remote_addr_.data));
143
144 if (!g_id_to_socket) 47 if (!g_id_to_socket)
145 g_id_to_socket = new IDToSocketMap(); 48 g_id_to_socket = new IDToSocketMap();
146 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); 49 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
147 (*g_id_to_socket)[socket_id] = this; 50 (*g_id_to_socket)[socket_id] = this;
148 } 51 }
149 52
150 TCPSocket::~TCPSocket() { 53 TCPSocket::~TCPSocket() {
151 Disconnect(); 54 Disconnect();
152 } 55 }
153 56
154 PPB_TCPSocket_Private_API* TCPSocket::AsPPB_TCPSocket_Private_API() { 57 void TCPSocket::SendConnect(const std::string& host, uint16_t port) {
155 return this; 58 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Connect(socket_id_, host, port));
156 } 59 }
157 60
158 int32_t TCPSocket::Connect(const char* host, 61 void TCPSocket::SendConnectWithNetAddress(const PP_NetAddress_Private& addr) {
159 uint16_t port, 62 SendToBrowser(
160 PP_CompletionCallback callback) { 63 new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress(socket_id_, addr));
161 if (!host)
162 return PP_ERROR_BADARGUMENT;
163
164 return ConnectWithMessage(
165 new PpapiHostMsg_PPBTCPSocket_Connect(socket_id_, host, port),
166 callback);
167 } 64 }
168 65
169 int32_t TCPSocket::ConnectWithNetAddress( 66 void TCPSocket::SendSSLHandshake(const std::string& server_name,
170 const PP_NetAddress_Private* addr, 67 uint16_t server_port) {
171 PP_CompletionCallback callback) { 68 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_SSLHandshake(
172 if (!addr) 69 socket_id_, server_name, server_port));
173 return PP_ERROR_BADARGUMENT;
174
175 return ConnectWithMessage(
176 new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress(
177 socket_id_, *addr),
178 callback);
179 } 70 }
180 71
181 PP_Bool TCPSocket::GetLocalAddress(PP_NetAddress_Private* local_addr) { 72 void TCPSocket::SendRead(int32_t bytes_to_read) {
182 if (!IsConnected() || !local_addr) 73 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Read(socket_id_, bytes_to_read));
183 return PP_FALSE;
184
185 *local_addr = local_addr_;
186 return PP_TRUE;
187 } 74 }
188 75
189 PP_Bool TCPSocket::GetRemoteAddress(PP_NetAddress_Private* remote_addr) { 76 void TCPSocket::SendWrite(const std::string& buffer) {
190 if (!IsConnected() || !remote_addr) 77 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Write(socket_id_, buffer));
191 return PP_FALSE;
192
193 *remote_addr = remote_addr_;
194 return PP_TRUE;
195 } 78 }
196 79
197 int32_t TCPSocket::SSLHandshake(const char* server_name, 80 void TCPSocket::SendDisconnect() {
198 uint16_t server_port,
199 PP_CompletionCallback callback) {
200 if (!server_name)
201 return PP_ERROR_BADARGUMENT;
202 if (!callback.func)
203 return PP_ERROR_BLOCKS_MAIN_THREAD;
204
205 if (connection_state_ != CONNECTED)
206 return PP_ERROR_FAILED;
207 if (ssl_handshake_callback_.func || read_callback_.func ||
208 write_callback_.func)
209 return PP_ERROR_INPROGRESS;
210
211 ssl_handshake_callback_ = callback;
212
213 // Send the request, the browser will call us back via SSLHandshakeACK.
214 GetDispatcher()->SendToBrowser(
215 new PpapiHostMsg_PPBTCPSocket_SSLHandshake(
216 socket_id_, std::string(server_name), server_port));
217 return PP_OK_COMPLETIONPENDING;
218 }
219
220 int32_t TCPSocket::Read(char* buffer,
221 int32_t bytes_to_read,
222 PP_CompletionCallback callback) {
223 if (!buffer || bytes_to_read <= 0)
224 return PP_ERROR_BADARGUMENT;
225 if (!callback.func)
226 return PP_ERROR_BLOCKS_MAIN_THREAD;
227
228 if (!IsConnected())
229 return PP_ERROR_FAILED;
230 if (read_callback_.func || ssl_handshake_callback_.func)
231 return PP_ERROR_INPROGRESS;
232
233 read_buffer_ = buffer;
234 bytes_to_read_ = std::min(bytes_to_read, kTCPSocketMaxReadSize);
235 read_callback_ = callback;
236
237 // Send the request, the browser will call us back via ReadACK.
238 GetDispatcher()->SendToBrowser(
239 new PpapiHostMsg_PPBTCPSocket_Read(socket_id_, bytes_to_read_));
240 return PP_OK_COMPLETIONPENDING;
241 }
242
243 int32_t TCPSocket::Write(const char* buffer,
244 int32_t bytes_to_write,
245 PP_CompletionCallback callback) {
246 if (!buffer || bytes_to_write <= 0)
247 return PP_ERROR_BADARGUMENT;
248 if (!callback.func)
249 return PP_ERROR_BLOCKS_MAIN_THREAD;
250
251 if (!IsConnected())
252 return PP_ERROR_FAILED;
253 if (write_callback_.func || ssl_handshake_callback_.func)
254 return PP_ERROR_INPROGRESS;
255
256 if (bytes_to_write > kTCPSocketMaxWriteSize)
257 bytes_to_write = kTCPSocketMaxWriteSize;
258
259 write_callback_ = callback;
260
261 // Send the request, the browser will call us back via WriteACK.
262 GetDispatcher()->SendToBrowser(
263 new PpapiHostMsg_PPBTCPSocket_Write(
264 socket_id_, std::string(buffer, bytes_to_write)));
265 return PP_OK_COMPLETIONPENDING;
266 }
267
268 void TCPSocket::Disconnect() {
269 if (connection_state_ == DISCONNECTED)
270 return;
271
272 connection_state_ = DISCONNECTED;
273 // After removed from the mapping, this object won't receive any notifications 81 // After removed from the mapping, this object won't receive any notifications
274 // from the proxy. 82 // from the proxy.
275 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); 83 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
276 g_id_to_socket->erase(socket_id_); 84 g_id_to_socket->erase(socket_id_);
277 85 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_));
278 GetDispatcher()->SendToBrowser(
279 new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_));
280 socket_id_ = 0;
281
282 PostAbortAndClearIfNecessary(&connect_callback_);
283 PostAbortAndClearIfNecessary(&ssl_handshake_callback_);
284 PostAbortAndClearIfNecessary(&read_callback_);
285 PostAbortAndClearIfNecessary(&write_callback_);
286 read_buffer_ = NULL;
287 bytes_to_read_ = -1;
288 } 86 }
289 87
290 void TCPSocket::OnConnectCompleted( 88 void TCPSocket::SendToBrowser(IPC::Message* msg) {
291 bool succeeded, 89 PluginDispatcher::GetForResource(this)->SendToBrowser(msg);
292 const PP_NetAddress_Private& local_addr,
293 const PP_NetAddress_Private& remote_addr) {
294 if (connection_state_ != BEFORE_CONNECT || !connect_callback_.func) {
295 NOTREACHED();
296 return;
297 }
298
299 if (succeeded) {
300 local_addr_ = local_addr;
301 remote_addr_ = remote_addr;
302 connection_state_ = CONNECTED;
303 }
304 PP_RunAndClearCompletionCallback(&connect_callback_,
305 succeeded ? PP_OK : PP_ERROR_FAILED);
306 } 90 }
307 91
308 void TCPSocket::OnSSLHandshakeCompleted(bool succeeded) { 92 } // namespace
309 if (connection_state_ != CONNECTED || !ssl_handshake_callback_.func) {
310 NOTREACHED();
311 return;
312 }
313 93
314 if (succeeded) { 94 //------------------------------------------------------------------------------
315 connection_state_ = SSL_CONNECTED;
316 PP_RunAndClearCompletionCallback(&ssl_handshake_callback_, PP_OK);
317 } else {
318 PP_RunAndClearCompletionCallback(&ssl_handshake_callback_, PP_ERROR_FAILED);
319 Disconnect();
320 }
321 }
322
323 void TCPSocket::OnReadCompleted(bool succeeded, const std::string& data) {
324 if (!read_callback_.func || !read_buffer_) {
325 NOTREACHED();
326 return;
327 }
328
329 if (succeeded) {
330 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
331 if (!data.empty())
332 memcpy(read_buffer_, data.c_str(), data.size());
333 }
334 read_buffer_ = NULL;
335 bytes_to_read_ = -1;
336
337 PP_RunAndClearCompletionCallback(
338 &read_callback_,
339 succeeded ? static_cast<int32_t>(data.size()) :
340 static_cast<int32_t>(PP_ERROR_FAILED));
341 }
342
343 void TCPSocket::OnWriteCompleted(bool succeeded, int32_t bytes_written) {
344 if (!write_callback_.func || (succeeded && bytes_written < 0)) {
345 NOTREACHED();
346 return;
347 }
348
349 PP_RunAndClearCompletionCallback(
350 &write_callback_,
351 succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED));
352 }
353
354 bool TCPSocket::IsConnected() const {
355 return connection_state_ == CONNECTED || connection_state_ == SSL_CONNECTED;
356 }
357
358 int32_t TCPSocket::ConnectWithMessage(IPC::Message* msg,
359 PP_CompletionCallback callback) {
360 scoped_ptr<IPC::Message> msg_deletor(msg);
361 if (!callback.func)
362 return PP_ERROR_BLOCKS_MAIN_THREAD;
363 if (connection_state_ != BEFORE_CONNECT)
364 return PP_ERROR_FAILED;
365 if (connect_callback_.func)
366 return PP_ERROR_INPROGRESS; // Can only have one pending request.
367
368 connect_callback_ = callback;
369 // Send the request, the browser will call us back via ConnectACK.
370 GetDispatcher()->SendToBrowser(msg_deletor.release());
371 return PP_OK_COMPLETIONPENDING;
372 }
373
374 void TCPSocket::PostAbortAndClearIfNecessary(
375 PP_CompletionCallback* callback) {
376 DCHECK(callback);
377
378 if (callback->func) {
379 MessageLoop::current()->PostTask(
380 FROM_HERE, base::Bind(&AbortCallback, *callback));
381 *callback = PP_BlockUntilComplete();
382 }
383 }
384 95
385 PPB_TCPSocket_Private_Proxy::PPB_TCPSocket_Private_Proxy(Dispatcher* dispatcher) 96 PPB_TCPSocket_Private_Proxy::PPB_TCPSocket_Private_Proxy(Dispatcher* dispatcher)
386 : InterfaceProxy(dispatcher) { 97 : InterfaceProxy(dispatcher) {
387 } 98 }
388 99
389 PPB_TCPSocket_Private_Proxy::~PPB_TCPSocket_Private_Proxy() { 100 PPB_TCPSocket_Private_Proxy::~PPB_TCPSocket_Private_Proxy() {
390 } 101 }
391 102
392 // static 103 // static
393 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResource( 104 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResource(
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 return; 186 return;
476 } 187 }
477 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); 188 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
478 if (iter == g_id_to_socket->end()) 189 if (iter == g_id_to_socket->end())
479 return; 190 return;
480 iter->second->OnWriteCompleted(succeeded, bytes_written); 191 iter->second->OnWriteCompleted(succeeded, bytes_written);
481 } 192 }
482 193
483 } // namespace proxy 194 } // namespace proxy
484 } // namespace ppapi 195 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/ppb_tcp_socket_private_proxy.h ('k') | ppapi/proxy/ppb_udp_socket_private_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698