OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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_tcp_server_socket_private_proxy.h" | |
6 | |
7 #include <cstddef> | |
8 | |
9 #include "base/logging.h" | |
10 #include "ppapi/c/pp_errors.h" | |
11 #include "ppapi/proxy/plugin_dispatcher.h" | |
12 #include "ppapi/proxy/plugin_globals.h" | |
13 #include "ppapi/proxy/plugin_resource_tracker.h" | |
14 #include "ppapi/proxy/ppapi_messages.h" | |
15 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h" | |
16 #include "ppapi/shared_impl/private/ppb_tcp_server_socket_shared.h" | |
17 #include "ppapi/shared_impl/resource.h" | |
18 #include "ppapi/thunk/enter.h" | |
19 #include "ppapi/thunk/thunk.h" | |
20 | |
21 namespace ppapi { | |
22 namespace proxy { | |
23 | |
24 namespace { | |
25 | |
26 class TCPServerSocket : public PPB_TCPServerSocket_Shared { | |
27 public: | |
28 TCPServerSocket(const HostResource& resource, uint32 plugin_dispatcher_id); | |
29 virtual ~TCPServerSocket(); | |
30 | |
31 virtual void OnAcceptCompleted( | |
32 bool succeeded, | |
33 uint32 tcp_socket_id, | |
34 const PP_NetAddress_Private& local_addr, | |
35 const PP_NetAddress_Private& remote_addr) OVERRIDE; | |
36 | |
37 virtual void SendListen(const PP_NetAddress_Private& addr, | |
38 int32_t backlog) OVERRIDE; | |
39 virtual void SendAccept() OVERRIDE; | |
40 virtual void SendStopListening() OVERRIDE; | |
41 | |
42 private: | |
43 void SendToBrowser(IPC::Message* msg); | |
44 | |
45 uint32 plugin_dispatcher_id_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(TCPServerSocket); | |
48 }; | |
49 | |
50 TCPServerSocket::TCPServerSocket(const HostResource& resource, | |
51 uint32 plugin_dispatcher_id) | |
52 : PPB_TCPServerSocket_Shared(resource), | |
53 plugin_dispatcher_id_(plugin_dispatcher_id) { | |
54 } | |
55 | |
56 TCPServerSocket::~TCPServerSocket() { | |
57 StopListening(); | |
58 } | |
59 | |
60 void TCPServerSocket::OnAcceptCompleted( | |
61 bool succeeded, | |
62 uint32 accepted_socket_id, | |
63 const PP_NetAddress_Private& local_addr, | |
64 const PP_NetAddress_Private& remote_addr) { | |
65 if (!TrackedCallback::IsPending(accept_callback_) || !tcp_socket_buffer_) { | |
66 NOTREACHED(); | |
67 return; | |
68 } | |
69 | |
70 if (succeeded) { | |
71 *tcp_socket_buffer_ = | |
72 PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket( | |
73 pp_instance(), | |
74 accepted_socket_id, | |
75 local_addr, | |
76 remote_addr); | |
77 } | |
78 tcp_socket_buffer_ = NULL; | |
79 | |
80 accept_callback_->Run(succeeded ? PP_OK : PP_ERROR_FAILED); | |
81 } | |
82 | |
83 void TCPServerSocket::SendListen(const PP_NetAddress_Private& addr, | |
84 int32_t backlog) { | |
85 SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Listen( | |
86 API_ID_PPB_TCPSERVERSOCKET_PRIVATE, | |
yzshen1
2013/07/19 23:51:58
The definition of this ID in api_id.h is not neede
ygorshenin1
2013/07/29 14:03:57
Done.
| |
87 plugin_dispatcher_id_, | |
88 pp_resource(), | |
89 addr, | |
90 backlog)); | |
91 } | |
92 | |
93 void TCPServerSocket::SendAccept() { | |
94 SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Accept( | |
95 API_ID_PPB_TCPSOCKET_PRIVATE, socket_id_)); | |
96 } | |
97 | |
98 void TCPServerSocket::SendStopListening() { | |
99 if (socket_id_ != 0) { | |
100 SendToBrowser(new PpapiHostMsg_PPBTCPServerSocket_Destroy(socket_id_)); | |
101 | |
102 PluginDispatcher* dispatcher = | |
103 PluginDispatcher::GetForInstance(host_resource().instance()); | |
104 if (dispatcher) { | |
105 InterfaceProxy* proxy = | |
106 dispatcher->GetInterfaceProxy(API_ID_PPB_TCPSERVERSOCKET_PRIVATE); | |
107 PPB_TCPServerSocket_Private_Proxy* server_socket_proxy = | |
108 static_cast<PPB_TCPServerSocket_Private_Proxy*>(proxy); | |
109 server_socket_proxy->ObjectDestroyed(socket_id_); | |
110 } | |
111 } | |
112 } | |
113 | |
114 void TCPServerSocket::SendToBrowser(IPC::Message* msg) { | |
115 PluginGlobals::Get()->GetBrowserSender()->Send(msg); | |
116 } | |
117 | |
118 } // namespace | |
119 | |
120 //------------------------------------------------------------------------------ | |
121 | |
122 PPB_TCPServerSocket_Private_Proxy::PPB_TCPServerSocket_Private_Proxy( | |
123 Dispatcher* dispatcher) | |
124 : InterfaceProxy(dispatcher) { | |
125 } | |
126 | |
127 PPB_TCPServerSocket_Private_Proxy::~PPB_TCPServerSocket_Private_Proxy() { | |
128 } | |
129 | |
130 PP_Resource PPB_TCPServerSocket_Private_Proxy::CreateProxyResource( | |
131 PP_Instance instance) { | |
132 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
133 if (!dispatcher) | |
134 return 0; | |
135 | |
136 TCPServerSocket* server_socket = | |
137 new TCPServerSocket(HostResource::MakeInstanceOnly(instance), | |
138 dispatcher->plugin_dispatcher_id()); | |
139 return server_socket->GetReference(); | |
140 } | |
141 | |
142 void PPB_TCPServerSocket_Private_Proxy::ObjectDestroyed(uint32 socket_id) { | |
143 id_to_server_socket_.erase(socket_id); | |
144 } | |
145 | |
146 bool PPB_TCPServerSocket_Private_Proxy::OnMessageReceived( | |
147 const IPC::Message& msg) { | |
148 bool handled = true; | |
149 IPC_BEGIN_MESSAGE_MAP(PPB_TCPServerSocket_Private_Proxy, msg) | |
150 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPServerSocket_ListenACK, OnMsgListenACK) | |
151 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPServerSocket_AcceptACK, OnMsgAcceptACK) | |
152 IPC_MESSAGE_UNHANDLED(handled = false) | |
153 IPC_END_MESSAGE_MAP() | |
154 return handled; | |
155 } | |
156 | |
157 void PPB_TCPServerSocket_Private_Proxy::OnMsgListenACK( | |
158 uint32 plugin_dispatcher_id, | |
159 PP_Resource socket_resource, | |
160 uint32 socket_id, | |
161 const PP_NetAddress_Private& local_addr, | |
162 int32_t status) { | |
163 thunk::EnterResourceNoLock<thunk::PPB_TCPServerSocket_Private_API> | |
164 enter(socket_resource, true); | |
165 if (enter.succeeded()) { | |
166 PPB_TCPServerSocket_Shared* server_socket = | |
167 static_cast<PPB_TCPServerSocket_Shared*>(enter.object()); | |
168 if (status == PP_OK) | |
169 id_to_server_socket_[socket_id] = server_socket; | |
170 server_socket->OnListenCompleted(socket_id, local_addr, status); | |
171 } else if (socket_id != 0 && status == PP_OK) { | |
172 IPC::Message* msg = | |
173 new PpapiHostMsg_PPBTCPServerSocket_Destroy(socket_id); | |
174 PluginGlobals::Get()->GetBrowserSender()->Send(msg); | |
175 } | |
176 } | |
177 | |
178 void PPB_TCPServerSocket_Private_Proxy::OnMsgAcceptACK( | |
179 uint32 plugin_dispatcher_id, | |
180 uint32 server_socket_id, | |
181 uint32 accepted_socket_id, | |
182 const PP_NetAddress_Private& local_addr, | |
183 const PP_NetAddress_Private& remote_addr) { | |
184 IDToServerSocketMap::iterator it = | |
185 id_to_server_socket_.find(server_socket_id); | |
186 if (it != id_to_server_socket_.end()) { | |
187 bool succeeded = (accepted_socket_id != 0); | |
188 it->second->OnAcceptCompleted(succeeded, | |
189 accepted_socket_id, | |
190 local_addr, | |
191 remote_addr); | |
192 } else if (accepted_socket_id != 0) { | |
193 PluginGlobals::Get()->GetBrowserSender()->Send( | |
194 new PpapiHostMsg_PPBTCPSocket_Disconnect(accepted_socket_id)); | |
195 } | |
196 } | |
197 | |
198 } // namespace proxy | |
199 } // namespace ppapi | |
OLD | NEW |