OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ppapi/proxy/ppb_udp_socket_private_proxy.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/logging.h" | |
10 #include "ppapi/c/private/ppb_udp_socket_private.h" | |
11 #include "ppapi/proxy/plugin_dispatcher.h" | |
12 #include "ppapi/proxy/plugin_globals.h" | |
13 #include "ppapi/proxy/plugin_resource_tracker.h" | |
14 #include "ppapi/proxy/ppapi_messages.h" | |
15 #include "ppapi/shared_impl/private/udp_socket_private_impl.h" | |
16 #include "ppapi/shared_impl/resource.h" | |
17 #include "ppapi/thunk/thunk.h" | |
18 | |
19 namespace ppapi { | |
20 namespace proxy { | |
21 | |
22 namespace { | |
23 | |
24 typedef std::map<uint32, UDPSocketPrivateImpl*> IDToSocketMap; | |
25 IDToSocketMap* g_id_to_socket = NULL; | |
26 | |
27 class UDPSocket : public UDPSocketPrivateImpl { | |
28 public: | |
29 UDPSocket(const HostResource& resource, uint32 socket_id); | |
30 virtual ~UDPSocket(); | |
31 | |
32 virtual void SendBoolSocketFeature(int32_t name, bool value) OVERRIDE; | |
33 virtual void SendBind(const PP_NetAddress_Private& addr) OVERRIDE; | |
34 virtual void SendRecvFrom(int32_t num_bytes) OVERRIDE; | |
35 virtual void SendSendTo(const std::string& data, | |
36 const PP_NetAddress_Private& addr) OVERRIDE; | |
37 virtual void SendClose() OVERRIDE; | |
38 | |
39 private: | |
40 void SendToBrowser(IPC::Message* msg); | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(UDPSocket); | |
43 }; | |
44 | |
45 UDPSocket::UDPSocket(const HostResource& resource, uint32 socket_id) | |
46 : UDPSocketPrivateImpl(resource, socket_id) { | |
47 if (!g_id_to_socket) | |
48 g_id_to_socket = new IDToSocketMap(); | |
49 DCHECK(g_id_to_socket->find(socket_id) == g_id_to_socket->end()); | |
50 (*g_id_to_socket)[socket_id] = this; | |
51 } | |
52 | |
53 UDPSocket::~UDPSocket() { | |
54 Close(); | |
55 } | |
56 | |
57 void UDPSocket::SendBoolSocketFeature(int32_t name, bool value) { | |
58 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_SetBoolSocketFeature( | |
59 API_ID_PPB_UDPSOCKET_PRIVATE, socket_id_, name, value)); | |
60 } | |
61 | |
62 void UDPSocket::SendBind(const PP_NetAddress_Private& addr) { | |
63 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Bind( | |
64 API_ID_PPB_UDPSOCKET_PRIVATE, socket_id_, addr)); | |
65 } | |
66 | |
67 void UDPSocket::SendRecvFrom(int32_t num_bytes) { | |
68 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_RecvFrom(socket_id_, num_bytes)); | |
69 } | |
70 | |
71 void UDPSocket::SendSendTo(const std::string& data, | |
72 const PP_NetAddress_Private& addr) { | |
73 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_SendTo( | |
74 API_ID_PPB_UDPSOCKET_PRIVATE, socket_id_, data, addr)); | |
75 } | |
76 | |
77 void UDPSocket::SendClose() { | |
78 // After removed from the mapping, this object won't receive any notifications | |
79 // from the proxy. | |
80 DCHECK(g_id_to_socket->find(socket_id_) != g_id_to_socket->end()); | |
81 g_id_to_socket->erase(socket_id_); | |
82 SendToBrowser(new PpapiHostMsg_PPBUDPSocket_Close(socket_id_)); | |
83 } | |
84 | |
85 void UDPSocket::SendToBrowser(IPC::Message* msg) { | |
86 PluginGlobals::Get()->GetBrowserSender()->Send(msg); | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 //------------------------------------------------------------------------------ | |
92 | |
93 PPB_UDPSocket_Private_Proxy::PPB_UDPSocket_Private_Proxy(Dispatcher* dispatcher) | |
94 : InterfaceProxy(dispatcher) { | |
95 } | |
96 | |
97 PPB_UDPSocket_Private_Proxy::~PPB_UDPSocket_Private_Proxy() { | |
98 } | |
99 | |
100 // static | |
101 PP_Resource PPB_UDPSocket_Private_Proxy::CreateProxyResource( | |
102 PP_Instance instance) { | |
103 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
104 if (!dispatcher) | |
105 return 0; | |
106 | |
107 uint32 socket_id = 0; | |
108 PluginGlobals::Get()->GetBrowserSender()->Send( | |
109 new PpapiHostMsg_PPBUDPSocket_Create( | |
110 API_ID_PPB_UDPSOCKET_PRIVATE, dispatcher->plugin_dispatcher_id(), | |
111 &socket_id)); | |
112 if (socket_id == 0) | |
113 return 0; | |
114 | |
115 return (new UDPSocket(HostResource::MakeInstanceOnly(instance), | |
116 socket_id))->GetReference(); | |
117 } | |
118 | |
119 bool PPB_UDPSocket_Private_Proxy::OnMessageReceived(const IPC::Message& msg) { | |
120 bool handled = true; | |
121 IPC_BEGIN_MESSAGE_MAP(PPB_UDPSocket_Private_Proxy, msg) | |
122 IPC_MESSAGE_HANDLER(PpapiMsg_PPBUDPSocket_BindACK, | |
123 OnMsgBindACK) | |
124 IPC_MESSAGE_HANDLER(PpapiMsg_PPBUDPSocket_RecvFromACK, | |
125 OnMsgRecvFromACK) | |
126 IPC_MESSAGE_HANDLER(PpapiMsg_PPBUDPSocket_SendToACK, | |
127 OnMsgSendToACK) | |
128 IPC_MESSAGE_UNHANDLED(handled = false) | |
129 IPC_END_MESSAGE_MAP() | |
130 return handled; | |
131 } | |
132 | |
133 void PPB_UDPSocket_Private_Proxy::OnMsgBindACK( | |
134 uint32 /* plugin_dispatcher_id */, | |
135 uint32 socket_id, | |
136 bool succeeded, | |
137 const PP_NetAddress_Private& bound_addr) { | |
138 if (!g_id_to_socket) { | |
139 NOTREACHED(); | |
140 return; | |
141 } | |
142 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | |
143 if (iter == g_id_to_socket->end()) | |
144 return; | |
145 iter->second->OnBindCompleted(succeeded, bound_addr); | |
146 } | |
147 | |
148 void PPB_UDPSocket_Private_Proxy::OnMsgRecvFromACK( | |
149 uint32 /* plugin_dispatcher_id */, | |
150 uint32 socket_id, | |
151 bool succeeded, | |
152 const std::string& data, | |
153 const PP_NetAddress_Private& addr) { | |
154 if (!g_id_to_socket) { | |
155 NOTREACHED(); | |
156 return; | |
157 } | |
158 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | |
159 if (iter == g_id_to_socket->end()) | |
160 return; | |
161 iter->second->OnRecvFromCompleted(succeeded, data, addr); | |
162 } | |
163 | |
164 void PPB_UDPSocket_Private_Proxy::OnMsgSendToACK( | |
165 uint32 /* plugin_dispatcher_id */, | |
166 uint32 socket_id, | |
167 bool succeeded, | |
168 int32_t bytes_written) { | |
169 if (!g_id_to_socket) { | |
170 NOTREACHED(); | |
171 return; | |
172 } | |
173 IDToSocketMap::iterator iter = g_id_to_socket->find(socket_id); | |
174 if (iter == g_id_to_socket->end()) | |
175 return; | |
176 iter->second->OnSendToCompleted(succeeded, bytes_written); | |
177 } | |
178 | |
179 } // namespace proxy | |
180 } // namespace ppapi | |
OLD | NEW |