Use Persistent::Reset.
[chromium-blink-merge.git] / ppapi / proxy / ppb_tcp_socket_private_proxy.cc
blob7f7fb7896a566786b478f9ad7e993a0e59f7e279
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.
5 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h"
7 #include <map>
9 #include "base/logging.h"
10 #include "ppapi/proxy/plugin_dispatcher.h"
11 #include "ppapi/proxy/plugin_globals.h"
12 #include "ppapi/proxy/plugin_resource_tracker.h"
13 #include "ppapi/proxy/ppapi_messages.h"
14 #include "ppapi/shared_impl/private/ppb_x509_certificate_private_shared.h"
15 #include "ppapi/shared_impl/private/tcp_socket_private_impl.h"
16 #include "ppapi/shared_impl/resource.h"
17 #include "ppapi/thunk/thunk.h"
19 namespace ppapi {
20 namespace proxy {
22 namespace {
24 typedef std::map<uint32, TCPSocketPrivateImpl*> IDToSocketMap;
25 IDToSocketMap* g_id_to_socket = NULL;
27 class TCPSocket : public TCPSocketPrivateImpl {
28 public:
29 // C-tor for new sockets.
30 TCPSocket(const HostResource& resource, uint32 socket_id);
31 // C-tor for already connected sockets.
32 TCPSocket(const HostResource& resource,
33 uint32 socket_id,
34 const PP_NetAddress_Private& local_addr,
35 const PP_NetAddress_Private& remote_addr);
36 virtual ~TCPSocket();
38 virtual void SendConnect(const std::string& host, uint16_t port) OVERRIDE;
39 virtual void SendConnectWithNetAddress(
40 const PP_NetAddress_Private& addr) OVERRIDE;
41 virtual void SendSSLHandshake(
42 const std::string& server_name,
43 uint16_t server_port,
44 const std::vector<std::vector<char> >& trusted_certs,
45 const std::vector<std::vector<char> >& untrusted_certs) OVERRIDE;
46 virtual void SendRead(int32_t bytes_to_read) OVERRIDE;
47 virtual void SendWrite(const std::string& buffer) OVERRIDE;
48 virtual void SendDisconnect() OVERRIDE;
49 virtual void SendSetBoolOption(PP_TCPSocketOption_Private name,
50 bool value) OVERRIDE;
52 private:
53 void SendToBrowser(IPC::Message* msg);
55 DISALLOW_COPY_AND_ASSIGN(TCPSocket);
58 TCPSocket::TCPSocket(const HostResource& resource, uint32 socket_id)
59 : TCPSocketPrivateImpl(resource, socket_id) {
60 if (!g_id_to_socket)
61 g_id_to_socket = new IDToSocketMap();
62 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
63 (*g_id_to_socket)[socket_id] = this;
66 TCPSocket::TCPSocket(const HostResource& resource,
67 uint32 socket_id,
68 const PP_NetAddress_Private& local_addr,
69 const PP_NetAddress_Private& remote_addr)
70 : TCPSocketPrivateImpl(resource, socket_id) {
71 if (!g_id_to_socket)
72 g_id_to_socket = new IDToSocketMap();
73 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
75 connection_state_ = CONNECTED;
76 local_addr_ = local_addr;
77 remote_addr_ = remote_addr;
79 (*g_id_to_socket)[socket_id] = this;
82 TCPSocket::~TCPSocket() {
83 Disconnect();
86 void TCPSocket::SendConnect(const std::string& host, uint16_t port) {
87 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Connect(
88 API_ID_PPB_TCPSOCKET_PRIVATE, socket_id_, host, port));
91 void TCPSocket::SendConnectWithNetAddress(const PP_NetAddress_Private& addr) {
92 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress(
93 API_ID_PPB_TCPSOCKET_PRIVATE, socket_id_, addr));
96 void TCPSocket::SendSSLHandshake(
97 const std::string& server_name,
98 uint16_t server_port,
99 const std::vector<std::vector<char> >& trusted_certs,
100 const std::vector<std::vector<char> >& untrusted_certs) {
101 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_SSLHandshake(
102 socket_id_, server_name, server_port, trusted_certs, untrusted_certs));
105 void TCPSocket::SendRead(int32_t bytes_to_read) {
106 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Read(socket_id_, bytes_to_read));
109 void TCPSocket::SendWrite(const std::string& buffer) {
110 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Write(socket_id_, buffer));
113 void TCPSocket::SendDisconnect() {
114 // After removed from the mapping, this object won't receive any notifications
115 // from the proxy.
116 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
117 g_id_to_socket->erase(socket_id_);
118 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_));
121 void TCPSocket::SendSetBoolOption(PP_TCPSocketOption_Private name, bool value) {
122 SendToBrowser(
123 new PpapiHostMsg_PPBTCPSocket_SetBoolOption(socket_id_, name, value));
126 void TCPSocket::SendToBrowser(IPC::Message* msg) {
127 PluginGlobals::Get()->GetBrowserSender()->Send(msg);
130 } // namespace
132 //------------------------------------------------------------------------------
134 PPB_TCPSocket_Private_Proxy::PPB_TCPSocket_Private_Proxy(Dispatcher* dispatcher)
135 : InterfaceProxy(dispatcher) {
138 PPB_TCPSocket_Private_Proxy::~PPB_TCPSocket_Private_Proxy() {
141 // static
142 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResource(
143 PP_Instance instance) {
144 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
145 if (!dispatcher)
146 return 0;
148 uint32 socket_id = 0;
149 PluginGlobals::Get()->GetBrowserSender()->Send(
150 new PpapiHostMsg_PPBTCPSocket_Create(
151 API_ID_PPB_TCPSOCKET_PRIVATE, dispatcher->plugin_dispatcher_id(),
152 &socket_id));
153 if (socket_id == 0)
154 return 0;
155 return (new TCPSocket(HostResource::MakeInstanceOnly(instance),
156 socket_id))->GetReference();
159 // static
160 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket(
161 PP_Instance instance,
162 uint32 socket_id,
163 const PP_NetAddress_Private& local_addr,
164 const PP_NetAddress_Private& remote_addr) {
165 return (new TCPSocket(HostResource::MakeInstanceOnly(instance),
166 socket_id,
167 local_addr,
168 remote_addr))->GetReference();
171 bool PPB_TCPSocket_Private_Proxy::OnMessageReceived(const IPC::Message& msg) {
172 bool handled = true;
173 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Proxy, msg)
174 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK,
175 OnMsgConnectACK)
176 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SSLHandshakeACK,
177 OnMsgSSLHandshakeACK)
178 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK, OnMsgReadACK)
179 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK, OnMsgWriteACK)
180 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SetBoolOptionACK,
181 OnMsgSetBoolOptionACK)
182 IPC_MESSAGE_UNHANDLED(handled = false)
183 IPC_END_MESSAGE_MAP()
184 return handled;
187 void PPB_TCPSocket_Private_Proxy::OnMsgConnectACK(
188 uint32 /* plugin_dispatcher_id */,
189 uint32 socket_id,
190 bool succeeded,
191 const PP_NetAddress_Private& local_addr,
192 const PP_NetAddress_Private& remote_addr) {
193 if (!g_id_to_socket) {
194 NOTREACHED();
195 return;
197 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
198 if (iter == g_id_to_socket->end())
199 return;
200 iter->second->OnConnectCompleted(succeeded, local_addr, remote_addr);
203 void PPB_TCPSocket_Private_Proxy::OnMsgSSLHandshakeACK(
204 uint32 /* plugin_dispatcher_id */,
205 uint32 socket_id,
206 bool succeeded,
207 const PPB_X509Certificate_Fields& certificate_fields) {
208 if (!g_id_to_socket) {
209 NOTREACHED();
210 return;
212 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
213 if (iter == g_id_to_socket->end())
214 return;
215 iter->second->OnSSLHandshakeCompleted(succeeded, certificate_fields);
218 void PPB_TCPSocket_Private_Proxy::OnMsgReadACK(
219 uint32 /* plugin_dispatcher_id */,
220 uint32 socket_id,
221 bool succeeded,
222 const std::string& data) {
223 if (!g_id_to_socket) {
224 NOTREACHED();
225 return;
227 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
228 if (iter == g_id_to_socket->end())
229 return;
230 iter->second->OnReadCompleted(succeeded, data);
233 void PPB_TCPSocket_Private_Proxy::OnMsgWriteACK(
234 uint32 /* plugin_dispatcher_id */,
235 uint32 socket_id,
236 bool succeeded,
237 int32_t bytes_written) {
238 if (!g_id_to_socket) {
239 NOTREACHED();
240 return;
242 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
243 if (iter == g_id_to_socket->end())
244 return;
245 iter->second->OnWriteCompleted(succeeded, bytes_written);
248 void PPB_TCPSocket_Private_Proxy::OnMsgSetBoolOptionACK(
249 uint32 /* plugin_dispatcher_id */,
250 uint32 socket_id,
251 bool succeeded) {
252 if (!g_id_to_socket) {
253 NOTREACHED();
254 return;
256 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
257 if (iter == g_id_to_socket->end())
258 return;
259 iter->second->OnSetOptionCompleted(succeeded);
262 } // namespace proxy
263 } // namespace ppapi