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
;
51 void SendToBrowser(IPC::Message
* msg
);
53 DISALLOW_COPY_AND_ASSIGN(TCPSocket
);
56 TCPSocket::TCPSocket(const HostResource
& resource
, uint32 socket_id
)
57 : TCPSocketPrivateImpl(resource
, socket_id
) {
59 g_id_to_socket
= new IDToSocketMap();
60 DCHECK(g_id_to_socket
->find(socket_id
) == g_id_to_socket
->end());
61 (*g_id_to_socket
)[socket_id
] = this;
64 TCPSocket::TCPSocket(const HostResource
& resource
,
66 const PP_NetAddress_Private
& local_addr
,
67 const PP_NetAddress_Private
& remote_addr
)
68 : TCPSocketPrivateImpl(resource
, socket_id
) {
70 g_id_to_socket
= new IDToSocketMap();
71 DCHECK(g_id_to_socket
->find(socket_id
) == g_id_to_socket
->end());
73 connection_state_
= CONNECTED
;
74 local_addr_
= local_addr
;
75 remote_addr_
= remote_addr
;
77 (*g_id_to_socket
)[socket_id
] = this;
80 TCPSocket::~TCPSocket() {
84 void TCPSocket::SendConnect(const std::string
& host
, uint16_t port
) {
85 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Connect(
86 API_ID_PPB_TCPSOCKET_PRIVATE
, socket_id_
, host
, port
));
89 void TCPSocket::SendConnectWithNetAddress(const PP_NetAddress_Private
& addr
) {
90 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress(
91 API_ID_PPB_TCPSOCKET_PRIVATE
, socket_id_
, addr
));
94 void TCPSocket::SendSSLHandshake(
95 const std::string
& server_name
,
97 const std::vector
<std::vector
<char> >& trusted_certs
,
98 const std::vector
<std::vector
<char> >& untrusted_certs
) {
99 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_SSLHandshake(
100 socket_id_
, server_name
, server_port
, trusted_certs
, untrusted_certs
));
103 void TCPSocket::SendRead(int32_t bytes_to_read
) {
104 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Read(socket_id_
, bytes_to_read
));
107 void TCPSocket::SendWrite(const std::string
& buffer
) {
108 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Write(socket_id_
, buffer
));
111 void TCPSocket::SendDisconnect() {
112 // After removed from the mapping, this object won't receive any notifications
114 DCHECK(g_id_to_socket
->find(socket_id_
) != g_id_to_socket
->end());
115 g_id_to_socket
->erase(socket_id_
);
116 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_
));
119 void TCPSocket::SendToBrowser(IPC::Message
* msg
) {
120 PluginGlobals::Get()->GetBrowserSender()->Send(msg
);
125 //------------------------------------------------------------------------------
127 PPB_TCPSocket_Private_Proxy::PPB_TCPSocket_Private_Proxy(Dispatcher
* dispatcher
)
128 : InterfaceProxy(dispatcher
) {
131 PPB_TCPSocket_Private_Proxy::~PPB_TCPSocket_Private_Proxy() {
135 PP_Resource
PPB_TCPSocket_Private_Proxy::CreateProxyResource(
136 PP_Instance instance
) {
137 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
141 uint32 socket_id
= 0;
142 PluginGlobals::Get()->GetBrowserSender()->Send(
143 new PpapiHostMsg_PPBTCPSocket_Create(
144 API_ID_PPB_TCPSOCKET_PRIVATE
, dispatcher
->plugin_dispatcher_id(),
148 return (new TCPSocket(HostResource::MakeInstanceOnly(instance
),
149 socket_id
))->GetReference();
153 PP_Resource
PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket(
154 PP_Instance instance
,
156 const PP_NetAddress_Private
& local_addr
,
157 const PP_NetAddress_Private
& remote_addr
) {
158 return (new TCPSocket(HostResource::MakeInstanceOnly(instance
),
161 remote_addr
))->GetReference();
164 bool PPB_TCPSocket_Private_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
166 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Proxy
, msg
)
167 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK
,
169 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SSLHandshakeACK
,
170 OnMsgSSLHandshakeACK
)
171 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK
, OnMsgReadACK
)
172 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK
, OnMsgWriteACK
)
173 IPC_MESSAGE_UNHANDLED(handled
= false)
174 IPC_END_MESSAGE_MAP()
178 void PPB_TCPSocket_Private_Proxy::OnMsgConnectACK(
179 uint32
/* plugin_dispatcher_id */,
182 const PP_NetAddress_Private
& local_addr
,
183 const PP_NetAddress_Private
& remote_addr
) {
184 if (!g_id_to_socket
) {
188 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
189 if (iter
== g_id_to_socket
->end())
191 iter
->second
->OnConnectCompleted(succeeded
, local_addr
, remote_addr
);
194 void PPB_TCPSocket_Private_Proxy::OnMsgSSLHandshakeACK(
195 uint32
/* plugin_dispatcher_id */,
198 const PPB_X509Certificate_Fields
& certificate_fields
) {
199 if (!g_id_to_socket
) {
203 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
204 if (iter
== g_id_to_socket
->end())
206 iter
->second
->OnSSLHandshakeCompleted(succeeded
, certificate_fields
);
209 void PPB_TCPSocket_Private_Proxy::OnMsgReadACK(
210 uint32
/* plugin_dispatcher_id */,
213 const std::string
& data
) {
214 if (!g_id_to_socket
) {
218 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
219 if (iter
== g_id_to_socket
->end())
221 iter
->second
->OnReadCompleted(succeeded
, data
);
224 void PPB_TCPSocket_Private_Proxy::OnMsgWriteACK(
225 uint32
/* plugin_dispatcher_id */,
228 int32_t bytes_written
) {
229 if (!g_id_to_socket
) {
233 IDToSocketMap::iterator iter
= g_id_to_socket
->find(socket_id
);
234 if (iter
== g_id_to_socket
->end())
236 iter
->second
->OnWriteCompleted(succeeded
, bytes_written
);