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_server_socket_private_resource.h"
7 #include "ppapi/proxy/ppapi_messages.h"
8 #include "ppapi/proxy/tcp_socket_private_resource.h"
13 TCPServerSocketPrivateResource::TCPServerSocketPrivateResource(
14 Connection connection
,
16 : PluginResource(connection
, instance
),
17 state_(STATE_BEFORE_LISTENING
),
19 SendCreate(BROWSER
, PpapiHostMsg_TCPServerSocket_CreatePrivate());
22 TCPServerSocketPrivateResource::~TCPServerSocketPrivateResource() {
25 thunk::PPB_TCPServerSocket_Private_API
*
26 TCPServerSocketPrivateResource::AsPPB_TCPServerSocket_Private_API() {
30 int32_t TCPServerSocketPrivateResource::Listen(
31 const PP_NetAddress_Private
* addr
,
33 scoped_refptr
<TrackedCallback
> callback
) {
35 return PP_ERROR_BADARGUMENT
;
36 if (state_
!= STATE_BEFORE_LISTENING
)
37 return PP_ERROR_FAILED
;
38 if (TrackedCallback::IsPending(listen_callback_
))
39 return PP_ERROR_INPROGRESS
;
41 listen_callback_
= callback
;
43 // Send the request, the browser will call us back via ListenACK
44 Call
<PpapiPluginMsg_TCPServerSocket_ListenReply
>(
46 PpapiHostMsg_TCPServerSocket_Listen(*addr
, backlog
),
47 base::Bind(&TCPServerSocketPrivateResource::OnPluginMsgListenReply
,
48 base::Unretained(this)));
49 return PP_OK_COMPLETIONPENDING
;
52 int32_t TCPServerSocketPrivateResource::Accept(
53 PP_Resource
* tcp_socket
,
54 scoped_refptr
<TrackedCallback
> callback
) {
56 return PP_ERROR_BADARGUMENT
;
57 if (state_
!= STATE_LISTENING
)
58 return PP_ERROR_FAILED
;
59 if (TrackedCallback::IsPending(accept_callback_
))
60 return PP_ERROR_INPROGRESS
;
62 accept_callback_
= callback
;
64 Call
<PpapiPluginMsg_TCPServerSocket_AcceptReply
>(
66 PpapiHostMsg_TCPServerSocket_Accept(),
67 base::Bind(&TCPServerSocketPrivateResource::OnPluginMsgAcceptReply
,
68 base::Unretained(this), tcp_socket
));
69 return PP_OK_COMPLETIONPENDING
;
72 int32_t TCPServerSocketPrivateResource::GetLocalAddress(
73 PP_NetAddress_Private
* addr
) {
75 return PP_ERROR_BADARGUMENT
;
76 if (state_
!= STATE_LISTENING
)
77 return PP_ERROR_FAILED
;
82 void TCPServerSocketPrivateResource::StopListening() {
83 if (state_
== STATE_CLOSED
)
85 state_
= STATE_CLOSED
;
86 Post(BROWSER
, PpapiHostMsg_TCPServerSocket_StopListening());
87 if (TrackedCallback::IsPending(listen_callback_
))
88 listen_callback_
->PostAbort();
89 if (TrackedCallback::IsPending(accept_callback_
))
90 accept_callback_
->PostAbort();
93 void TCPServerSocketPrivateResource::OnPluginMsgListenReply(
94 const ResourceMessageReplyParams
& params
,
95 const PP_NetAddress_Private
& local_addr
) {
96 if (state_
!= STATE_BEFORE_LISTENING
||
97 !TrackedCallback::IsPending(listen_callback_
)) {
100 if (params
.result() == PP_OK
) {
101 local_addr_
= local_addr
;
102 state_
= STATE_LISTENING
;
104 listen_callback_
->Run(params
.result());
107 void TCPServerSocketPrivateResource::OnPluginMsgAcceptReply(
108 PP_Resource
* tcp_socket
,
109 const ResourceMessageReplyParams
& params
,
110 int pending_resource_id
,
111 const PP_NetAddress_Private
& local_addr
,
112 const PP_NetAddress_Private
& remote_addr
) {
114 if (state_
!= STATE_LISTENING
||
115 !TrackedCallback::IsPending(accept_callback_
)) {
118 if (params
.result() == PP_OK
) {
119 *tcp_socket
= (new TCPSocketPrivateResource(connection(), pp_instance(),
122 remote_addr
))->GetReference();
124 accept_callback_
->Run(params
.result());