OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h" | 5 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "ppapi/proxy/plugin_dispatcher.h" | 10 #include "ppapi/proxy/plugin_dispatcher.h" |
11 #include "ppapi/proxy/plugin_globals.h" | 11 #include "ppapi/proxy/plugin_globals.h" |
12 #include "ppapi/proxy/plugin_proxy_delegate.h" | 12 #include "ppapi/proxy/plugin_proxy_delegate.h" |
13 #include "ppapi/proxy/plugin_resource_tracker.h" | 13 #include "ppapi/proxy/plugin_resource_tracker.h" |
14 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/shared_impl/private/tcp_socket_private_impl.h" | 15 #include "ppapi/shared_impl/private/tcp_socket_private_impl.h" |
16 #include "ppapi/shared_impl/resource.h" | 16 #include "ppapi/shared_impl/resource.h" |
17 #include "ppapi/thunk/thunk.h" | 17 #include "ppapi/thunk/thunk.h" |
18 | 18 |
19 namespace ppapi { | 19 namespace ppapi { |
20 namespace proxy { | 20 namespace proxy { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 typedef std::map<uint32, TCPSocketPrivateImpl*> IDToSocketMap; | 24 typedef std::map<uint32, TCPSocketPrivateImpl*> IDToSocketMap; |
25 IDToSocketMap* g_id_to_socket = NULL; | 25 IDToSocketMap* g_id_to_socket = NULL; |
26 | 26 |
27 class TCPSocket : public TCPSocketPrivateImpl { | 27 class TCPSocket : public TCPSocketPrivateImpl { |
28 public: | 28 public: |
| 29 // C-tor for new sockets. |
29 TCPSocket(const HostResource& resource, uint32 socket_id); | 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); |
30 virtual ~TCPSocket(); | 36 virtual ~TCPSocket(); |
31 | 37 |
32 virtual void SendConnect(const std::string& host, uint16_t port) OVERRIDE; | 38 virtual void SendConnect(const std::string& host, uint16_t port) OVERRIDE; |
33 virtual void SendConnectWithNetAddress( | 39 virtual void SendConnectWithNetAddress( |
34 const PP_NetAddress_Private& addr) OVERRIDE; | 40 const PP_NetAddress_Private& addr) OVERRIDE; |
35 virtual void SendSSLHandshake(const std::string& server_name, | 41 virtual void SendSSLHandshake(const std::string& server_name, |
36 uint16_t server_port) OVERRIDE; | 42 uint16_t server_port) OVERRIDE; |
37 virtual void SendRead(int32_t bytes_to_read) OVERRIDE; | 43 virtual void SendRead(int32_t bytes_to_read) OVERRIDE; |
38 virtual void SendWrite(const std::string& buffer) OVERRIDE; | 44 virtual void SendWrite(const std::string& buffer) OVERRIDE; |
39 virtual void SendDisconnect() OVERRIDE; | 45 virtual void SendDisconnect() OVERRIDE; |
40 | 46 |
41 private: | 47 private: |
42 void SendToBrowser(IPC::Message* msg); | 48 void SendToBrowser(IPC::Message* msg); |
43 | 49 |
44 DISALLOW_COPY_AND_ASSIGN(TCPSocket); | 50 DISALLOW_COPY_AND_ASSIGN(TCPSocket); |
45 }; | 51 }; |
46 | 52 |
47 TCPSocket::TCPSocket(const HostResource& resource, uint32 socket_id) | 53 TCPSocket::TCPSocket(const HostResource& resource, uint32 socket_id) |
48 : TCPSocketPrivateImpl(resource, socket_id) { | 54 : TCPSocketPrivateImpl(resource, socket_id) { |
49 if (!g_id_to_socket) | 55 if (!g_id_to_socket) |
50 g_id_to_socket = new IDToSocketMap(); | 56 g_id_to_socket = new IDToSocketMap(); |
51 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); | 57 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); |
52 (*g_id_to_socket)[socket_id] = this; | 58 (*g_id_to_socket)[socket_id] = this; |
53 } | 59 } |
54 | 60 |
| 61 TCPSocket::TCPSocket(const HostResource& resource, |
| 62 uint32 socket_id, |
| 63 const PP_NetAddress_Private& local_addr, |
| 64 const PP_NetAddress_Private& remote_addr) |
| 65 : TCPSocketPrivateImpl(resource, socket_id) { |
| 66 if (!g_id_to_socket) |
| 67 g_id_to_socket = new IDToSocketMap(); |
| 68 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); |
| 69 |
| 70 connection_state_ = CONNECTED; |
| 71 local_addr_ = local_addr; |
| 72 remote_addr_ = remote_addr; |
| 73 |
| 74 (*g_id_to_socket)[socket_id] = this; |
| 75 } |
| 76 |
55 TCPSocket::~TCPSocket() { | 77 TCPSocket::~TCPSocket() { |
56 Disconnect(); | 78 Disconnect(); |
57 } | 79 } |
58 | 80 |
59 void TCPSocket::SendConnect(const std::string& host, uint16_t port) { | 81 void TCPSocket::SendConnect(const std::string& host, uint16_t port) { |
60 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Connect( | 82 SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Connect( |
61 API_ID_PPB_TCPSOCKET_PRIVATE, socket_id_, host, port)); | 83 API_ID_PPB_TCPSOCKET_PRIVATE, socket_id_, host, port)); |
62 } | 84 } |
63 | 85 |
64 void TCPSocket::SendConnectWithNetAddress(const PP_NetAddress_Private& addr) { | 86 void TCPSocket::SendConnectWithNetAddress(const PP_NetAddress_Private& addr) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( | 136 PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( |
115 new PpapiHostMsg_PPBTCPSocket_Create( | 137 new PpapiHostMsg_PPBTCPSocket_Create( |
116 API_ID_PPB_TCPSOCKET_PRIVATE, dispatcher->plugin_dispatcher_id(), | 138 API_ID_PPB_TCPSOCKET_PRIVATE, dispatcher->plugin_dispatcher_id(), |
117 &socket_id)); | 139 &socket_id)); |
118 if (socket_id == 0) | 140 if (socket_id == 0) |
119 return 0; | 141 return 0; |
120 return (new TCPSocket(HostResource::MakeInstanceOnly(instance), | 142 return (new TCPSocket(HostResource::MakeInstanceOnly(instance), |
121 socket_id))->GetReference(); | 143 socket_id))->GetReference(); |
122 } | 144 } |
123 | 145 |
| 146 // static |
| 147 PP_Resource PPB_TCPSocket_Private_Proxy::CreateProxyResourceForConnectedSocket( |
| 148 PP_Instance instance, |
| 149 uint32 socket_id, |
| 150 const PP_NetAddress_Private& local_addr, |
| 151 const PP_NetAddress_Private& remote_addr) { |
| 152 return (new TCPSocket(HostResource::MakeInstanceOnly(instance), |
| 153 socket_id, |
| 154 local_addr, |
| 155 remote_addr))->GetReference(); |
| 156 } |
| 157 |
124 bool PPB_TCPSocket_Private_Proxy::OnMessageReceived(const IPC::Message& msg) { | 158 bool PPB_TCPSocket_Private_Proxy::OnMessageReceived(const IPC::Message& msg) { |
125 bool handled = true; | 159 bool handled = true; |
126 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Proxy, msg) | 160 IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Private_Proxy, msg) |
127 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK, | 161 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK, |
128 OnMsgConnectACK) | 162 OnMsgConnectACK) |
129 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SSLHandshakeACK, | 163 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SSLHandshakeACK, |
130 OnMsgSSLHandshakeACK) | 164 OnMsgSSLHandshakeACK) |
131 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK, OnMsgReadACK) | 165 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK, OnMsgReadACK) |
132 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK, OnMsgWriteACK) | 166 IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK, OnMsgWriteACK) |
133 IPC_MESSAGE_UNHANDLED(handled = false) | 167 IPC_MESSAGE_UNHANDLED(handled = false) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 return; | 224 return; |
191 } | 225 } |
192 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | 226 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); |
193 if (iter == g_id_to_socket->end()) | 227 if (iter == g_id_to_socket->end()) |
194 return; | 228 return; |
195 iter->second->OnWriteCompleted(succeeded, bytes_written); | 229 iter->second->OnWriteCompleted(succeeded, bytes_written); |
196 } | 230 } |
197 | 231 |
198 } // namespace proxy | 232 } // namespace proxy |
199 } // namespace ppapi | 233 } // namespace ppapi |
OLD | NEW |