Refactor android test results logging.
[chromium-blink-merge.git] / ppapi / proxy / ppb_tcp_socket_private_proxy.cc
blob7f11b28ff813281a0329a49bb2dd40e19c21327d
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;
50 private:
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) {
58 if (!g_id_to_socket)
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,
65 uint32 socket_id,
66 const PP_NetAddress_Private& local_addr,
67 const PP_NetAddress_Private& remote_addr)
68 : TCPSocketPrivateImpl(resource, socket_id) {
69 if (!g_id_to_socket)
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() {
81 Disconnect();
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,
96 uint16_t server_port,
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
113 // from the proxy.
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);
123 } // namespace
125 //------------------------------------------------------------------------------
127 PPB_TCPSocket_Private_Proxy::PPB_TCPSocket_Private_Proxy(Dispatcher* dispatcher)
128 : InterfaceProxy(dispatcher) {
131 PPB_TCPSocket_Private_Proxy::~PPB_TCPSocket_Private_Proxy() {
134 // static
135 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResource(
136 PP_Instance instance) {
137 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
138 if (!dispatcher)
139 return 0;
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(),
145 &socket_id));
146 if (socket_id == 0)
147 return 0;
148 return (new TCPSocket(HostResource::MakeInstanceOnly(instance),
149 socket_id))->GetReference();
152 // static
153 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket(
154 PP_Instance instance,
155 uint32 socket_id,
156 const PP_NetAddress_Private& local_addr,
157 const PP_NetAddress_Private& remote_addr) {
158 return (new TCPSocket(HostResource::MakeInstanceOnly(instance),
159 socket_id,
160 local_addr,
161 remote_addr))->GetReference();
164 bool PPB_TCPSocket_Private_Proxy::OnMessageReceived(const IPC::Message& msg) {
165 bool handled = true;
166 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Proxy, msg)
167 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK,
168 OnMsgConnectACK)
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()
175 return handled;
178 void PPB_TCPSocket_Private_Proxy::OnMsgConnectACK(
179 uint32 /* plugin_dispatcher_id */,
180 uint32 socket_id,
181 bool succeeded,
182 const PP_NetAddress_Private& local_addr,
183 const PP_NetAddress_Private& remote_addr) {
184 if (!g_id_to_socket) {
185 NOTREACHED();
186 return;
188 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
189 if (iter == g_id_to_socket->end())
190 return;
191 iter->second->OnConnectCompleted(succeeded, local_addr, remote_addr);
194 void PPB_TCPSocket_Private_Proxy::OnMsgSSLHandshakeACK(
195 uint32 /* plugin_dispatcher_id */,
196 uint32 socket_id,
197 bool succeeded,
198 const PPB_X509Certificate_Fields& certificate_fields) {
199 if (!g_id_to_socket) {
200 NOTREACHED();
201 return;
203 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
204 if (iter == g_id_to_socket->end())
205 return;
206 iter->second->OnSSLHandshakeCompleted(succeeded, certificate_fields);
209 void PPB_TCPSocket_Private_Proxy::OnMsgReadACK(
210 uint32 /* plugin_dispatcher_id */,
211 uint32 socket_id,
212 bool succeeded,
213 const std::string& data) {
214 if (!g_id_to_socket) {
215 NOTREACHED();
216 return;
218 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
219 if (iter == g_id_to_socket->end())
220 return;
221 iter->second->OnReadCompleted(succeeded, data);
224 void PPB_TCPSocket_Private_Proxy::OnMsgWriteACK(
225 uint32 /* plugin_dispatcher_id */,
226 uint32 socket_id,
227 bool succeeded,
228 int32_t bytes_written) {
229 if (!g_id_to_socket) {
230 NOTREACHED();
231 return;
233 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
234 if (iter == g_id_to_socket->end())
235 return;
236 iter->second->OnWriteCompleted(succeeded, bytes_written);
239 } // namespace proxy
240 } // namespace ppapi