Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / proxy / tcp_socket_resource_base.cc
blobfb4db5bee64a584dde7ec4697b7ead75d025d2be
1 // Copyright 2013 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.
5 #include "ppapi/proxy/tcp_socket_resource_base.h"
7 #include <cstring>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "ppapi/c/pp_bool.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/proxy/error_conversion.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/shared_impl/ppapi_globals.h"
16 #include "ppapi/shared_impl/private/ppb_x509_certificate_private_shared.h"
17 #include "ppapi/shared_impl/socket_option_data.h"
18 #include "ppapi/shared_impl/var.h"
19 #include "ppapi/shared_impl/var_tracker.h"
20 #include "ppapi/thunk/enter.h"
21 #include "ppapi/thunk/ppb_x509_certificate_private_api.h"
23 namespace ppapi {
24 namespace proxy {
26 const int32_t TCPSocketResourceBase::kMaxReadSize = 1024 * 1024;
27 const int32_t TCPSocketResourceBase::kMaxWriteSize = 1024 * 1024;
28 const int32_t TCPSocketResourceBase::kMaxSendBufferSize =
29 1024 * TCPSocketResourceBase::kMaxWriteSize;
30 const int32_t TCPSocketResourceBase::kMaxReceiveBufferSize =
31 1024 * TCPSocketResourceBase::kMaxReadSize;
33 TCPSocketResourceBase::TCPSocketResourceBase(Connection connection,
34 PP_Instance instance,
35 TCPSocketVersion version)
36 : PluginResource(connection, instance),
37 state_(TCPSocketState::INITIAL),
38 read_buffer_(NULL),
39 bytes_to_read_(-1),
40 accepted_tcp_socket_(NULL),
41 version_(version) {
42 local_addr_.size = 0;
43 memset(local_addr_.data, 0,
44 arraysize(local_addr_.data) * sizeof(*local_addr_.data));
45 remote_addr_.size = 0;
46 memset(remote_addr_.data, 0,
47 arraysize(remote_addr_.data) * sizeof(*remote_addr_.data));
50 TCPSocketResourceBase::TCPSocketResourceBase(
51 Connection connection,
52 PP_Instance instance,
53 TCPSocketVersion version,
54 const PP_NetAddress_Private& local_addr,
55 const PP_NetAddress_Private& remote_addr)
56 : PluginResource(connection, instance),
57 state_(TCPSocketState::CONNECTED),
58 read_buffer_(NULL),
59 bytes_to_read_(-1),
60 local_addr_(local_addr),
61 remote_addr_(remote_addr),
62 accepted_tcp_socket_(NULL),
63 version_(version) {
66 TCPSocketResourceBase::~TCPSocketResourceBase() {
67 CloseImpl();
70 int32_t TCPSocketResourceBase::BindImpl(
71 const PP_NetAddress_Private* addr,
72 scoped_refptr<TrackedCallback> callback) {
73 if (!addr)
74 return PP_ERROR_BADARGUMENT;
75 if (state_.IsPending(TCPSocketState::BIND))
76 return PP_ERROR_INPROGRESS;
77 if (!state_.IsValidTransition(TCPSocketState::BIND))
78 return PP_ERROR_FAILED;
80 bind_callback_ = callback;
81 state_.SetPendingTransition(TCPSocketState::BIND);
83 Call<PpapiPluginMsg_TCPSocket_BindReply>(
84 BROWSER,
85 PpapiHostMsg_TCPSocket_Bind(*addr),
86 base::Bind(&TCPSocketResourceBase::OnPluginMsgBindReply,
87 base::Unretained(this)),
88 callback);
89 return PP_OK_COMPLETIONPENDING;
92 int32_t TCPSocketResourceBase::ConnectImpl(
93 const char* host,
94 uint16_t port,
95 scoped_refptr<TrackedCallback> callback) {
96 if (!host)
97 return PP_ERROR_BADARGUMENT;
98 if (state_.IsPending(TCPSocketState::CONNECT))
99 return PP_ERROR_INPROGRESS;
100 if (!state_.IsValidTransition(TCPSocketState::CONNECT))
101 return PP_ERROR_FAILED;
103 connect_callback_ = callback;
104 state_.SetPendingTransition(TCPSocketState::CONNECT);
106 Call<PpapiPluginMsg_TCPSocket_ConnectReply>(
107 BROWSER,
108 PpapiHostMsg_TCPSocket_Connect(host, port),
109 base::Bind(&TCPSocketResourceBase::OnPluginMsgConnectReply,
110 base::Unretained(this)),
111 callback);
112 return PP_OK_COMPLETIONPENDING;
115 int32_t TCPSocketResourceBase::ConnectWithNetAddressImpl(
116 const PP_NetAddress_Private* addr,
117 scoped_refptr<TrackedCallback> callback) {
118 if (!addr)
119 return PP_ERROR_BADARGUMENT;
120 if (state_.IsPending(TCPSocketState::CONNECT))
121 return PP_ERROR_INPROGRESS;
122 if (!state_.IsValidTransition(TCPSocketState::CONNECT))
123 return PP_ERROR_FAILED;
125 connect_callback_ = callback;
126 state_.SetPendingTransition(TCPSocketState::CONNECT);
128 Call<PpapiPluginMsg_TCPSocket_ConnectReply>(
129 BROWSER,
130 PpapiHostMsg_TCPSocket_ConnectWithNetAddress(*addr),
131 base::Bind(&TCPSocketResourceBase::OnPluginMsgConnectReply,
132 base::Unretained(this)),
133 callback);
134 return PP_OK_COMPLETIONPENDING;
137 PP_Bool TCPSocketResourceBase::GetLocalAddressImpl(
138 PP_NetAddress_Private* local_addr) {
139 if (!state_.IsBound() || !local_addr)
140 return PP_FALSE;
141 *local_addr = local_addr_;
142 return PP_TRUE;
145 PP_Bool TCPSocketResourceBase::GetRemoteAddressImpl(
146 PP_NetAddress_Private* remote_addr) {
147 if (!state_.IsConnected() || !remote_addr)
148 return PP_FALSE;
149 *remote_addr = remote_addr_;
150 return PP_TRUE;
153 int32_t TCPSocketResourceBase::SSLHandshakeImpl(
154 const char* server_name,
155 uint16_t server_port,
156 scoped_refptr<TrackedCallback> callback) {
157 if (!server_name)
158 return PP_ERROR_BADARGUMENT;
160 if (state_.IsPending(TCPSocketState::SSL_CONNECT) ||
161 TrackedCallback::IsPending(read_callback_) ||
162 TrackedCallback::IsPending(write_callback_)) {
163 return PP_ERROR_INPROGRESS;
165 if (!state_.IsValidTransition(TCPSocketState::SSL_CONNECT))
166 return PP_ERROR_FAILED;
168 ssl_handshake_callback_ = callback;
169 state_.SetPendingTransition(TCPSocketState::SSL_CONNECT);
171 Call<PpapiPluginMsg_TCPSocket_SSLHandshakeReply>(
172 BROWSER,
173 PpapiHostMsg_TCPSocket_SSLHandshake(server_name,
174 server_port,
175 trusted_certificates_,
176 untrusted_certificates_),
177 base::Bind(&TCPSocketResourceBase::OnPluginMsgSSLHandshakeReply,
178 base::Unretained(this)),
179 callback);
180 return PP_OK_COMPLETIONPENDING;
183 PP_Resource TCPSocketResourceBase::GetServerCertificateImpl() {
184 if (!server_certificate_.get())
185 return 0;
186 return server_certificate_->GetReference();
189 PP_Bool TCPSocketResourceBase::AddChainBuildingCertificateImpl(
190 PP_Resource certificate,
191 PP_Bool trusted) {
192 // TODO(raymes): This is exposed in the private PPB_TCPSocket_Private
193 // interface for Flash but isn't currently implemented due to security
194 // implications. It is exposed so that it can be hooked up on the Flash side
195 // and if we decide to implement it we can do so without modifying the Flash
196 // codebase.
197 NOTIMPLEMENTED();
198 return PP_FALSE;
201 int32_t TCPSocketResourceBase::ReadImpl(
202 char* buffer,
203 int32_t bytes_to_read,
204 scoped_refptr<TrackedCallback> callback) {
205 if (!buffer || bytes_to_read <= 0)
206 return PP_ERROR_BADARGUMENT;
208 if (!state_.IsConnected())
209 return PP_ERROR_FAILED;
210 if (TrackedCallback::IsPending(read_callback_) ||
211 state_.IsPending(TCPSocketState::SSL_CONNECT))
212 return PP_ERROR_INPROGRESS;
213 read_buffer_ = buffer;
214 bytes_to_read_ = std::min(bytes_to_read, kMaxReadSize);
215 read_callback_ = callback;
217 Call<PpapiPluginMsg_TCPSocket_ReadReply>(
218 BROWSER,
219 PpapiHostMsg_TCPSocket_Read(bytes_to_read_),
220 base::Bind(&TCPSocketResourceBase::OnPluginMsgReadReply,
221 base::Unretained(this)),
222 callback);
223 return PP_OK_COMPLETIONPENDING;
226 int32_t TCPSocketResourceBase::WriteImpl(
227 const char* buffer,
228 int32_t bytes_to_write,
229 scoped_refptr<TrackedCallback> callback) {
230 if (!buffer || bytes_to_write <= 0)
231 return PP_ERROR_BADARGUMENT;
233 if (!state_.IsConnected())
234 return PP_ERROR_FAILED;
235 if (TrackedCallback::IsPending(write_callback_) ||
236 state_.IsPending(TCPSocketState::SSL_CONNECT))
237 return PP_ERROR_INPROGRESS;
239 if (bytes_to_write > kMaxWriteSize)
240 bytes_to_write = kMaxWriteSize;
242 write_callback_ = callback;
244 Call<PpapiPluginMsg_TCPSocket_WriteReply>(
245 BROWSER,
246 PpapiHostMsg_TCPSocket_Write(std::string(buffer, bytes_to_write)),
247 base::Bind(&TCPSocketResourceBase::OnPluginMsgWriteReply,
248 base::Unretained(this)),
249 callback);
250 return PP_OK_COMPLETIONPENDING;
253 int32_t TCPSocketResourceBase::ListenImpl(
254 int32_t backlog,
255 scoped_refptr<TrackedCallback> callback) {
256 if (backlog <= 0)
257 return PP_ERROR_BADARGUMENT;
258 if (state_.IsPending(TCPSocketState::LISTEN))
259 return PP_ERROR_INPROGRESS;
260 if (!state_.IsValidTransition(TCPSocketState::LISTEN))
261 return PP_ERROR_FAILED;
263 listen_callback_ = callback;
264 state_.SetPendingTransition(TCPSocketState::LISTEN);
266 Call<PpapiPluginMsg_TCPSocket_ListenReply>(
267 BROWSER,
268 PpapiHostMsg_TCPSocket_Listen(backlog),
269 base::Bind(&TCPSocketResourceBase::OnPluginMsgListenReply,
270 base::Unretained(this)),
271 callback);
272 return PP_OK_COMPLETIONPENDING;
275 int32_t TCPSocketResourceBase::AcceptImpl(
276 PP_Resource* accepted_tcp_socket,
277 scoped_refptr<TrackedCallback> callback) {
278 if (!accepted_tcp_socket)
279 return PP_ERROR_BADARGUMENT;
280 if (TrackedCallback::IsPending(accept_callback_))
281 return PP_ERROR_INPROGRESS;
282 if (state_.state() != TCPSocketState::LISTENING)
283 return PP_ERROR_FAILED;
285 accept_callback_ = callback;
286 accepted_tcp_socket_ = accepted_tcp_socket;
288 Call<PpapiPluginMsg_TCPSocket_AcceptReply>(
289 BROWSER,
290 PpapiHostMsg_TCPSocket_Accept(),
291 base::Bind(&TCPSocketResourceBase::OnPluginMsgAcceptReply,
292 base::Unretained(this)),
293 callback);
294 return PP_OK_COMPLETIONPENDING;
297 void TCPSocketResourceBase::CloseImpl() {
298 if (state_.state() == TCPSocketState::CLOSED)
299 return;
301 state_.DoTransition(TCPSocketState::CLOSE, true);
303 Post(BROWSER, PpapiHostMsg_TCPSocket_Close());
305 PostAbortIfNecessary(&bind_callback_);
306 PostAbortIfNecessary(&connect_callback_);
307 PostAbortIfNecessary(&ssl_handshake_callback_);
308 PostAbortIfNecessary(&read_callback_);
309 PostAbortIfNecessary(&write_callback_);
310 PostAbortIfNecessary(&listen_callback_);
311 PostAbortIfNecessary(&accept_callback_);
312 read_buffer_ = NULL;
313 bytes_to_read_ = -1;
314 server_certificate_ = NULL;
315 accepted_tcp_socket_ = NULL;
318 int32_t TCPSocketResourceBase::SetOptionImpl(
319 PP_TCPSocket_Option name,
320 const PP_Var& value,
321 bool check_connect_state,
322 scoped_refptr<TrackedCallback> callback) {
323 SocketOptionData option_data;
324 switch (name) {
325 case PP_TCPSOCKET_OPTION_NO_DELAY: {
326 if (check_connect_state && !state_.IsConnected())
327 return PP_ERROR_FAILED;
329 if (value.type != PP_VARTYPE_BOOL)
330 return PP_ERROR_BADARGUMENT;
331 option_data.SetBool(PP_ToBool(value.value.as_bool));
332 break;
334 case PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE:
335 case PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE: {
336 if (check_connect_state && !state_.IsConnected())
337 return PP_ERROR_FAILED;
339 if (value.type != PP_VARTYPE_INT32)
340 return PP_ERROR_BADARGUMENT;
341 option_data.SetInt32(value.value.as_int);
342 break;
344 default: {
345 NOTREACHED();
346 return PP_ERROR_BADARGUMENT;
350 set_option_callbacks_.push(callback);
352 Call<PpapiPluginMsg_TCPSocket_SetOptionReply>(
353 BROWSER,
354 PpapiHostMsg_TCPSocket_SetOption(name, option_data),
355 base::Bind(&TCPSocketResourceBase::OnPluginMsgSetOptionReply,
356 base::Unretained(this)),
357 callback);
358 return PP_OK_COMPLETIONPENDING;
361 void TCPSocketResourceBase::PostAbortIfNecessary(
362 scoped_refptr<TrackedCallback>* callback) {
363 if (TrackedCallback::IsPending(*callback))
364 (*callback)->PostAbort();
367 void TCPSocketResourceBase::OnPluginMsgBindReply(
368 const ResourceMessageReplyParams& params,
369 const PP_NetAddress_Private& local_addr) {
370 // It is possible that CloseImpl() has been called. We don't want to update
371 // class members in this case.
372 if (!state_.IsPending(TCPSocketState::BIND))
373 return;
375 DCHECK(TrackedCallback::IsPending(bind_callback_));
376 if (params.result() == PP_OK) {
377 local_addr_ = local_addr;
378 state_.CompletePendingTransition(true);
379 } else {
380 state_.CompletePendingTransition(false);
382 RunCallback(bind_callback_, params.result());
385 void TCPSocketResourceBase::OnPluginMsgConnectReply(
386 const ResourceMessageReplyParams& params,
387 const PP_NetAddress_Private& local_addr,
388 const PP_NetAddress_Private& remote_addr) {
389 // It is possible that CloseImpl() has been called. We don't want to update
390 // class members in this case.
391 if (!state_.IsPending(TCPSocketState::CONNECT))
392 return;
394 DCHECK(TrackedCallback::IsPending(connect_callback_));
395 if (params.result() == PP_OK) {
396 local_addr_ = local_addr;
397 remote_addr_ = remote_addr;
398 state_.CompletePendingTransition(true);
399 } else {
400 if (version_ == TCP_SOCKET_VERSION_1_1_OR_ABOVE) {
401 state_.CompletePendingTransition(false);
402 } else {
403 // In order to maintain backward compatibility, allow to connect the
404 // socket again.
405 state_ = TCPSocketState(TCPSocketState::INITIAL);
408 RunCallback(connect_callback_, params.result());
411 void TCPSocketResourceBase::OnPluginMsgSSLHandshakeReply(
412 const ResourceMessageReplyParams& params,
413 const PPB_X509Certificate_Fields& certificate_fields) {
414 // It is possible that CloseImpl() has been called. We don't want to
415 // update class members in this case.
416 if (!state_.IsPending(TCPSocketState::SSL_CONNECT))
417 return;
419 DCHECK(TrackedCallback::IsPending(ssl_handshake_callback_));
420 if (params.result() == PP_OK) {
421 state_.CompletePendingTransition(true);
422 server_certificate_ = new PPB_X509Certificate_Private_Shared(
423 OBJECT_IS_PROXY,
424 pp_instance(),
425 certificate_fields);
426 } else {
427 state_.CompletePendingTransition(false);
429 RunCallback(ssl_handshake_callback_, params.result());
432 void TCPSocketResourceBase::OnPluginMsgReadReply(
433 const ResourceMessageReplyParams& params,
434 const std::string& data) {
435 // It is possible that CloseImpl() has been called. We shouldn't access the
436 // buffer in that case. The user may have released it.
437 if (!state_.IsConnected() || !TrackedCallback::IsPending(read_callback_) ||
438 !read_buffer_) {
439 return;
442 const bool succeeded = params.result() == PP_OK;
443 if (succeeded) {
444 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_);
445 if (!data.empty())
446 memmove(read_buffer_, data.c_str(), data.size());
448 read_buffer_ = NULL;
449 bytes_to_read_ = -1;
451 RunCallback(read_callback_,
452 succeeded ? static_cast<int32_t>(data.size()) : params.result());
455 void TCPSocketResourceBase::OnPluginMsgWriteReply(
456 const ResourceMessageReplyParams& params) {
457 if (!state_.IsConnected() || !TrackedCallback::IsPending(write_callback_))
458 return;
459 RunCallback(write_callback_, params.result());
462 void TCPSocketResourceBase::OnPluginMsgListenReply(
463 const ResourceMessageReplyParams& params) {
464 if (!state_.IsPending(TCPSocketState::LISTEN))
465 return;
467 DCHECK(TrackedCallback::IsPending(listen_callback_));
468 state_.CompletePendingTransition(params.result() == PP_OK);
470 RunCallback(listen_callback_, params.result());
473 void TCPSocketResourceBase::OnPluginMsgAcceptReply(
474 const ResourceMessageReplyParams& params,
475 int pending_host_id,
476 const PP_NetAddress_Private& local_addr,
477 const PP_NetAddress_Private& remote_addr) {
478 // It is possible that CloseImpl() has been called. We shouldn't access the
479 // output parameter in that case. The user may have released it.
480 if (state_.state() != TCPSocketState::LISTENING ||
481 !TrackedCallback::IsPending(accept_callback_) || !accepted_tcp_socket_) {
482 return;
485 if (params.result() == PP_OK) {
486 *accepted_tcp_socket_ = CreateAcceptedSocket(pending_host_id, local_addr,
487 remote_addr);
489 accepted_tcp_socket_ = NULL;
490 RunCallback(accept_callback_, params.result());
493 void TCPSocketResourceBase::OnPluginMsgSetOptionReply(
494 const ResourceMessageReplyParams& params) {
495 if (set_option_callbacks_.empty()) {
496 NOTREACHED();
497 return;
499 scoped_refptr<TrackedCallback> callback = set_option_callbacks_.front();
500 set_option_callbacks_.pop();
501 if (TrackedCallback::IsPending(callback))
502 RunCallback(callback, params.result());
505 void TCPSocketResourceBase::RunCallback(scoped_refptr<TrackedCallback> callback,
506 int32_t pp_result) {
507 callback->Run(ConvertNetworkAPIErrorForCompatibility(
508 pp_result, version_ == TCP_SOCKET_VERSION_PRIVATE));
511 } // namespace ppapi
512 } // namespace proxy