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"
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"
24 typedef std::map
<uint32
, TCPSocketPrivateImpl
*> IDToSocketMap
;
25 IDToSocketMap
* g_id_to_socket
= NULL
;
27 class TCPSocket
: public TCPSocketPrivateImpl
{
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
,
34 const PP_NetAddress_Private
& local_addr
,
35 const PP_NetAddress_Private
& remote_addr
);
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
,
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
,
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
) {
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
,
68 const PP_NetAddress_Private
& local_addr
,
69 const PP_NetAddress_Private
& remote_addr
)
70 : TCPSocketPrivateImpl(resource
, socket_id
) {
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() {
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
,
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
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
) {
123 new PpapiHostMsg_PPBTCPSocket_SetBoolOption(socket_id_
, name
, value
));
126 void TCPSocket::SendToBrowser(IPC::Message
* msg
) {
127 PluginGlobals::Get()->GetBrowserSender()->Send(msg
);
132 //------------------------------------------------------------------------------
134 PPB_TCPSocket_Private_Proxy::PPB_TCPSocket_Private_Proxy(Dispatcher
* dispatcher
)
135 : InterfaceProxy(dispatcher
) {
138 PPB_TCPSocket_Private_Proxy::~PPB_TCPSocket_Private_Proxy() {
142 PP_Resource
PPB_TCPSocket_Private_Proxy::CreateProxyResource(
143 PP_Instance instance
) {
144 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
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(),
155 return (new TCPSocket(HostResource::MakeInstanceOnly(instance
),
156 socket_id
))->GetReference();
160 PP_Resource
PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket(
161 PP_Instance instance
,
163 const PP_NetAddress_Private
& local_addr
,
164 const PP_NetAddress_Private
& remote_addr
) {
165 return (new TCPSocket(HostResource::MakeInstanceOnly(instance
),
168 remote_addr
))->GetReference();
171 bool PPB_TCPSocket_Private_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
173 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Proxy
, msg
)
174 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK
,
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()
187 void PPB_TCPSocket_Private_Proxy::OnMsgConnectACK(
188 uint32
/* plugin_dispatcher_id */,
191 const PP_NetAddress_Private
& local_addr
,
192 const PP_NetAddress_Private
& remote_addr
) {
193 if (!g_id_to_socket
) {
197 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
198 if (iter
== g_id_to_socket
->end())
200 iter
->second
->OnConnectCompleted(succeeded
, local_addr
, remote_addr
);
203 void PPB_TCPSocket_Private_Proxy::OnMsgSSLHandshakeACK(
204 uint32
/* plugin_dispatcher_id */,
207 const PPB_X509Certificate_Fields
& certificate_fields
) {
208 if (!g_id_to_socket
) {
212 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
213 if (iter
== g_id_to_socket
->end())
215 iter
->second
->OnSSLHandshakeCompleted(succeeded
, certificate_fields
);
218 void PPB_TCPSocket_Private_Proxy::OnMsgReadACK(
219 uint32
/* plugin_dispatcher_id */,
222 const std::string
& data
) {
223 if (!g_id_to_socket
) {
227 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
228 if (iter
== g_id_to_socket
->end())
230 iter
->second
->OnReadCompleted(succeeded
, data
);
233 void PPB_TCPSocket_Private_Proxy::OnMsgWriteACK(
234 uint32
/* plugin_dispatcher_id */,
237 int32_t bytes_written
) {
238 if (!g_id_to_socket
) {
242 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
243 if (iter
== g_id_to_socket
->end())
245 iter
->second
->OnWriteCompleted(succeeded
, bytes_written
);
248 void PPB_TCPSocket_Private_Proxy::OnMsgSetBoolOptionACK(
249 uint32
/* plugin_dispatcher_id */,
252 if (!g_id_to_socket
) {
256 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
257 if (iter
== g_id_to_socket
->end())
259 iter
->second
->OnSetOptionCompleted(succeeded
);