1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/proxy/ppb_tcp_socket_proxy.h"
#include <map>
#include "base/logging.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/tcp_socket_shared.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_net_address_api.h"
#include "ppapi/thunk/ppb_tcp_socket_api.h"
#include "ppapi/thunk/thunk.h"
namespace ppapi {
namespace proxy {
namespace {
typedef thunk::EnterResourceNoLock<thunk::PPB_NetAddress_API>
EnterNetAddressNoLock;
typedef std::map<uint32, TCPSocketShared*> IDToSocketMap;
IDToSocketMap* g_id_to_socket = NULL;
class TCPSocket : public thunk::PPB_TCPSocket_API,
public Resource,
public TCPSocketShared {
public:
TCPSocket(const HostResource& resource, uint32 socket_id);
virtual ~TCPSocket();
// Resource overrides.
virtual thunk::PPB_TCPSocket_API* AsPPB_TCPSocket_API() OVERRIDE;
// thunk::PPB_TCPSocket_API implementation.
virtual int32_t Connect(PP_Resource addr,
scoped_refptr<TrackedCallback> callback) OVERRIDE;
virtual PP_Resource GetLocalAddress() OVERRIDE;
virtual PP_Resource GetRemoteAddress() OVERRIDE;
virtual int32_t Read(char* buffer,
int32_t bytes_to_read,
scoped_refptr<TrackedCallback> callback) OVERRIDE;
virtual int32_t Write(const char* buffer,
int32_t bytes_to_write,
scoped_refptr<TrackedCallback> callback) OVERRIDE;
virtual void Close() OVERRIDE;
virtual int32_t SetOption(PP_TCPSocket_Option_Dev name,
const PP_Var& value,
scoped_refptr<TrackedCallback> callback) OVERRIDE;
// TCPSocketShared implementation.
virtual void SendConnect(const std::string& host, uint16_t port) OVERRIDE;
virtual void SendConnectWithNetAddress(
const PP_NetAddress_Private& addr) OVERRIDE;
virtual void SendSSLHandshake(
const std::string& server_name,
uint16_t server_port,
const std::vector<std::vector<char> >& trusted_certs,
const std::vector<std::vector<char> >& untrusted_certs) OVERRIDE;
virtual void SendRead(int32_t bytes_to_read) OVERRIDE;
virtual void SendWrite(const std::string& buffer) OVERRIDE;
virtual void SendDisconnect() OVERRIDE;
virtual void SendSetBoolOption(PP_TCPSocketOption_Private name,
bool value) OVERRIDE;
virtual Resource* GetOwnerResource() OVERRIDE;
private:
void SendToBrowser(IPC::Message* msg);
DISALLOW_COPY_AND_ASSIGN(TCPSocket);
};
TCPSocket::TCPSocket(const HostResource& resource, uint32 socket_id)
: Resource(OBJECT_IS_PROXY, resource),
TCPSocketShared(OBJECT_IS_PROXY, socket_id) {
if (!g_id_to_socket)
g_id_to_socket = new IDToSocketMap();
DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end());
(*g_id_to_socket)[socket_id] = this;
}
TCPSocket::~TCPSocket() {
DisconnectImpl();
}
thunk::PPB_TCPSocket_API* TCPSocket::AsPPB_TCPSocket_API() {
return this;
}
int32_t TCPSocket::Connect(PP_Resource addr,
scoped_refptr<TrackedCallback> callback) {
EnterNetAddressNoLock enter(addr, true);
if (enter.failed())
return PP_ERROR_BADARGUMENT;
return ConnectWithNetAddressImpl(&enter.object()->GetNetAddressPrivate(),
callback);
}
PP_Resource TCPSocket::GetLocalAddress() {
PP_NetAddress_Private addr_private;
if (!GetLocalAddressImpl(&addr_private))
return 0;
thunk::EnterResourceCreationNoLock enter(pp_instance());
if (enter.failed())
return 0;
return enter.functions()->CreateNetAddressFromNetAddressPrivate(
pp_instance(), addr_private);
}
PP_Resource TCPSocket::GetRemoteAddress() {
PP_NetAddress_Private addr_private;
if (!GetRemoteAddressImpl(&addr_private))
return 0;
thunk::EnterResourceCreationNoLock enter(pp_instance());
if (enter.failed())
return 0;
return enter.functions()->CreateNetAddressFromNetAddressPrivate(
pp_instance(), addr_private);
}
int32_t TCPSocket::Read(char* buffer,
int32_t bytes_to_read,
scoped_refptr<TrackedCallback> callback) {
return ReadImpl(buffer, bytes_to_read, callback);
}
int32_t TCPSocket::Write(const char* buffer,
int32_t bytes_to_write,
scoped_refptr<TrackedCallback> callback) {
return WriteImpl(buffer, bytes_to_write, callback);
}
void TCPSocket::Close() {
DisconnectImpl();
}
int32_t TCPSocket::SetOption(PP_TCPSocket_Option_Dev name,
const PP_Var& value,
scoped_refptr<TrackedCallback> callback) {
// TODO(yzshen): Add support for other options.
if (name == PP_TCPSOCKET_OPTION_NO_DELAY)
return SetOptionImpl(PP_TCPSOCKETOPTION_NO_DELAY, value, callback);
return PP_ERROR_NOTSUPPORTED;
}
void TCPSocket::SendConnect(const std::string& host, uint16_t port) {
NOTREACHED();
}
void TCPSocket::SendConnectWithNetAddress(const PP_NetAddress_Private& addr) {
SendToBrowser(new PpapiHostMsg_PPBTCPSocket_ConnectWithNetAddress(
API_ID_PPB_TCPSOCKET, socket_id_, addr));
}
void TCPSocket::SendSSLHandshake(
const std::string& server_name,
uint16_t server_port,
const std::vector<std::vector<char> >& trusted_certs,
const std::vector<std::vector<char> >& untrusted_certs) {
NOTREACHED();
}
void TCPSocket::SendRead(int32_t bytes_to_read) {
SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Read(socket_id_, bytes_to_read));
}
void TCPSocket::SendWrite(const std::string& buffer) {
SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Write(socket_id_, buffer));
}
void TCPSocket::SendDisconnect() {
// After removed from the mapping, this object won't receive any notifications
// from the proxy.
DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end());
g_id_to_socket->erase(socket_id_);
SendToBrowser(new PpapiHostMsg_PPBTCPSocket_Disconnect(socket_id_));
}
void TCPSocket::SendSetBoolOption(PP_TCPSocketOption_Private name, bool value) {
SendToBrowser(
new PpapiHostMsg_PPBTCPSocket_SetBoolOption(socket_id_, name, value));
}
Resource* TCPSocket::GetOwnerResource() {
return this;
}
void TCPSocket::SendToBrowser(IPC::Message* msg) {
PluginGlobals::Get()->GetBrowserSender()->Send(msg);
}
} // namespace
//------------------------------------------------------------------------------
PPB_TCPSocket_Proxy::PPB_TCPSocket_Proxy(Dispatcher* dispatcher)
: InterfaceProxy(dispatcher) {
}
PPB_TCPSocket_Proxy::~PPB_TCPSocket_Proxy() {
}
// static
PP_Resource PPB_TCPSocket_Proxy::CreateProxyResource(PP_Instance instance) {
PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
if (!dispatcher)
return 0;
uint32 socket_id = 0;
PluginGlobals::Get()->GetBrowserSender()->Send(
new PpapiHostMsg_PPBTCPSocket_Create(
API_ID_PPB_TCPSOCKET, dispatcher->plugin_dispatcher_id(),
&socket_id));
if (socket_id == 0)
return 0;
return (new TCPSocket(HostResource::MakeInstanceOnly(instance),
socket_id))->GetReference();
}
bool PPB_TCPSocket_Proxy::OnMessageReceived(const IPC::Message& msg) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PPB_TCPSocket_Proxy, msg)
IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ConnectACK,
OnMsgConnectACK)
IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_ReadACK, OnMsgReadACK)
IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_WriteACK, OnMsgWriteACK)
IPC_MESSAGE_HANDLER(PpapiMsg_PPBTCPSocket_SetBoolOptionACK,
OnMsgSetBoolOptionACK)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PPB_TCPSocket_Proxy::OnMsgConnectACK(
uint32 /* plugin_dispatcher_id */,
uint32 socket_id,
bool succeeded,
const PP_NetAddress_Private& local_addr,
const PP_NetAddress_Private& remote_addr) {
if (!g_id_to_socket) {
NOTREACHED();
return;
}
IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
if (iter == g_id_to_socket->end())
return;
iter->second->OnConnectCompleted(succeeded, local_addr, remote_addr);
}
void PPB_TCPSocket_Proxy::OnMsgReadACK(uint32 /* plugin_dispatcher_id */,
uint32 socket_id,
bool succeeded,
const std::string& data) {
if (!g_id_to_socket) {
NOTREACHED();
return;
}
IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
if (iter == g_id_to_socket->end())
return;
iter->second->OnReadCompleted(succeeded, data);
}
void PPB_TCPSocket_Proxy::OnMsgWriteACK(uint32 /* plugin_dispatcher_id */,
uint32 socket_id,
bool succeeded,
int32_t bytes_written) {
if (!g_id_to_socket) {
NOTREACHED();
return;
}
IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
if (iter == g_id_to_socket->end())
return;
iter->second->OnWriteCompleted(succeeded, bytes_written);
}
void PPB_TCPSocket_Proxy::OnMsgSetBoolOptionACK(
uint32 /* plugin_dispatcher_id */,
uint32 socket_id,
bool succeeded) {
if (!g_id_to_socket) {
NOTREACHED();
return;
}
IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id);
if (iter == g_id_to_socket->end())
return;
iter->second->OnSetOptionCompleted(succeeded);
}
} // namespace proxy
} // namespace ppapi
|