1 // Copyright (c) 2013 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 <vector> 6 7 #include "ppapi/c/extensions/dev/ppb_ext_socket_dev.h" 8 #include "ppapi/shared_impl/tracked_callback.h" 9 #include "ppapi/thunk/enter.h" 10 #include "ppapi/thunk/extensions_common_api.h" 11 #include "ppapi/thunk/thunk.h" 12 13 namespace ppapi { 14 namespace thunk { 15 16 namespace { 17 18 int32_t Create(PP_Instance instance, 19 PP_Ext_Socket_SocketType_Dev type, 20 PP_Ext_Socket_CreateOptions_Dev options, 21 PP_Ext_Socket_CreateInfo_Dev* create_info, 22 PP_CompletionCallback callback) { 23 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 24 if (enter.failed()) 25 return enter.retval(); 26 27 std::vector<PP_Var> input_args; 28 std::vector<PP_Var*> output_args; 29 input_args.push_back(type); 30 input_args.push_back(options); 31 output_args.push_back(create_info); 32 return enter.SetResult(enter.functions()->CallBrowser( 33 "socket.create", input_args, output_args, enter.callback())); 34 } 35 36 void Destroy(PP_Instance instance, PP_Var socket_id) { 37 EnterInstanceAPI<ExtensionsCommon_API> enter(instance); 38 if (enter.failed()) 39 return; 40 41 std::vector<PP_Var> args; 42 args.push_back(socket_id); 43 enter.functions()->PostBrowser("socket.destroy", args); 44 } 45 46 int32_t Connect(PP_Instance instance, 47 PP_Var socket_id, 48 PP_Var hostname, 49 PP_Var port, 50 PP_Var* result, 51 PP_CompletionCallback callback) { 52 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 53 if (enter.failed()) 54 return enter.retval(); 55 56 std::vector<PP_Var> input_args; 57 std::vector<PP_Var*> output_args; 58 input_args.push_back(socket_id); 59 input_args.push_back(hostname); 60 input_args.push_back(port); 61 output_args.push_back(result); 62 return enter.SetResult(enter.functions()->CallBrowser( 63 "socket.connect", input_args, output_args, enter.callback())); 64 } 65 66 int32_t Bind(PP_Instance instance, 67 PP_Var socket_id, 68 PP_Var address, 69 PP_Var port, 70 PP_Var* result, 71 PP_CompletionCallback callback) { 72 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 73 if (enter.failed()) 74 return enter.retval(); 75 76 std::vector<PP_Var> input_args; 77 std::vector<PP_Var*> output_args; 78 input_args.push_back(socket_id); 79 input_args.push_back(address); 80 input_args.push_back(port); 81 output_args.push_back(result); 82 return enter.SetResult(enter.functions()->CallBrowser( 83 "socket.bind", input_args, output_args, enter.callback())); 84 } 85 86 void Disconnect(PP_Instance instance, PP_Var socket_id) { 87 EnterInstanceAPI<ExtensionsCommon_API> enter(instance); 88 if (enter.failed()) 89 return; 90 91 std::vector<PP_Var> args; 92 args.push_back(socket_id); 93 enter.functions()->PostBrowser("socket.disconnect", args); 94 } 95 96 int32_t Read(PP_Instance instance, 97 PP_Var socket_id, 98 PP_Var buffer_size, 99 PP_Ext_Socket_ReadInfo_Dev* read_info, 100 PP_CompletionCallback callback) { 101 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 102 if (enter.failed()) 103 return enter.retval(); 104 105 std::vector<PP_Var> input_args; 106 std::vector<PP_Var*> output_args; 107 input_args.push_back(socket_id); 108 input_args.push_back(buffer_size); 109 output_args.push_back(read_info); 110 return enter.SetResult(enter.functions()->CallBrowser( 111 "socket.read", input_args, output_args, enter.callback())); 112 } 113 114 int32_t Write(PP_Instance instance, 115 PP_Var socket_id, 116 PP_Var data, 117 PP_Ext_Socket_WriteInfo_Dev* write_info, 118 PP_CompletionCallback callback) { 119 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 120 if (enter.failed()) 121 return enter.retval(); 122 123 std::vector<PP_Var> input_args; 124 std::vector<PP_Var*> output_args; 125 input_args.push_back(socket_id); 126 input_args.push_back(data); 127 output_args.push_back(write_info); 128 return enter.SetResult(enter.functions()->CallBrowser( 129 "socket.write", input_args, output_args, enter.callback())); 130 } 131 132 int32_t RecvFrom(PP_Instance instance, 133 PP_Var socket_id, 134 PP_Var buffer_size, 135 PP_Ext_Socket_RecvFromInfo_Dev* recv_from_info, 136 PP_CompletionCallback callback) { 137 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 138 if (enter.failed()) 139 return enter.retval(); 140 141 std::vector<PP_Var> input_args; 142 std::vector<PP_Var*> output_args; 143 input_args.push_back(socket_id); 144 input_args.push_back(buffer_size); 145 output_args.push_back(recv_from_info); 146 return enter.SetResult(enter.functions()->CallBrowser( 147 "socket.recvFrom", input_args, output_args, enter.callback())); 148 } 149 150 int32_t SendTo(PP_Instance instance, 151 PP_Var socket_id, 152 PP_Var data, 153 PP_Var address, 154 PP_Var port, 155 PP_Ext_Socket_WriteInfo_Dev* write_info, 156 PP_CompletionCallback callback) { 157 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 158 if (enter.failed()) 159 return enter.retval(); 160 161 std::vector<PP_Var> input_args; 162 std::vector<PP_Var*> output_args; 163 input_args.push_back(socket_id); 164 input_args.push_back(data); 165 input_args.push_back(address); 166 input_args.push_back(port); 167 output_args.push_back(write_info); 168 return enter.SetResult(enter.functions()->CallBrowser( 169 "socket.sendTo", input_args, output_args, enter.callback())); 170 } 171 172 int32_t Listen(PP_Instance instance, 173 PP_Var socket_id, 174 PP_Var address, 175 PP_Var port, 176 PP_Var backlog, 177 PP_Var* result, 178 PP_CompletionCallback callback) { 179 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 180 if (enter.failed()) 181 return enter.retval(); 182 183 std::vector<PP_Var> input_args; 184 std::vector<PP_Var*> output_args; 185 input_args.push_back(socket_id); 186 input_args.push_back(address); 187 input_args.push_back(port); 188 input_args.push_back(backlog); 189 output_args.push_back(result); 190 return enter.SetResult(enter.functions()->CallBrowser( 191 "socket.listen", input_args, output_args, enter.callback())); 192 } 193 194 int32_t Accept(PP_Instance instance, 195 PP_Var socket_id, 196 PP_Ext_Socket_AcceptInfo_Dev* accept_info, 197 PP_CompletionCallback callback) { 198 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 199 if (enter.failed()) 200 return enter.retval(); 201 202 std::vector<PP_Var> input_args; 203 std::vector<PP_Var*> output_args; 204 input_args.push_back(socket_id); 205 output_args.push_back(accept_info); 206 return enter.SetResult(enter.functions()->CallBrowser( 207 "socket.accept", input_args, output_args, enter.callback())); 208 } 209 210 int32_t SetKeepAlive(PP_Instance instance, 211 PP_Var socket_id, 212 PP_Var enable, 213 PP_Var delay, 214 PP_Var* result, 215 PP_CompletionCallback callback) { 216 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 217 if (enter.failed()) 218 return enter.retval(); 219 220 std::vector<PP_Var> input_args; 221 std::vector<PP_Var*> output_args; 222 input_args.push_back(socket_id); 223 input_args.push_back(enable); 224 input_args.push_back(delay); 225 output_args.push_back(result); 226 return enter.SetResult(enter.functions()->CallBrowser( 227 "socket.setKeepAlive", input_args, output_args, enter.callback())); 228 } 229 230 int32_t SetNoDelay(PP_Instance instance, 231 PP_Var socket_id, 232 PP_Var no_delay, 233 PP_Var* result, 234 PP_CompletionCallback callback) { 235 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 236 if (enter.failed()) 237 return enter.retval(); 238 239 std::vector<PP_Var> input_args; 240 std::vector<PP_Var*> output_args; 241 input_args.push_back(socket_id); 242 input_args.push_back(no_delay); 243 output_args.push_back(result); 244 return enter.SetResult(enter.functions()->CallBrowser( 245 "socket.setNoDelay", input_args, output_args, enter.callback())); 246 } 247 248 int32_t GetInfo(PP_Instance instance, 249 PP_Var socket_id, 250 PP_Ext_Socket_SocketInfo_Dev* result, 251 PP_CompletionCallback callback) { 252 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 253 if (enter.failed()) 254 return enter.retval(); 255 256 std::vector<PP_Var> input_args; 257 std::vector<PP_Var*> output_args; 258 input_args.push_back(socket_id); 259 output_args.push_back(result); 260 return enter.SetResult(enter.functions()->CallBrowser( 261 "socket.getInfo", input_args, output_args, enter.callback())); 262 } 263 264 int32_t GetNetworkList(PP_Instance instance, 265 PP_Ext_Socket_NetworkInterface_Dev_Array* result, 266 PP_CompletionCallback callback) { 267 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 268 if (enter.failed()) 269 return enter.retval(); 270 271 std::vector<PP_Var> input_args; 272 std::vector<PP_Var*> output_args; 273 output_args.push_back(result); 274 return enter.SetResult(enter.functions()->CallBrowser( 275 "socket.getNetworkList", input_args, output_args, enter.callback())); 276 } 277 278 int32_t JoinGroup(PP_Instance instance, 279 PP_Var socket_id, 280 PP_Var address, 281 PP_Var* result, 282 PP_CompletionCallback callback) { 283 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 284 if (enter.failed()) 285 return enter.retval(); 286 287 std::vector<PP_Var> input_args; 288 std::vector<PP_Var*> output_args; 289 input_args.push_back(socket_id); 290 input_args.push_back(address); 291 output_args.push_back(result); 292 return enter.SetResult(enter.functions()->CallBrowser( 293 "socket.joinGroup", input_args, output_args, enter.callback())); 294 } 295 296 int32_t LeaveGroup(PP_Instance instance, 297 PP_Var socket_id, 298 PP_Var address, 299 PP_Var* result, 300 PP_CompletionCallback callback) { 301 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 302 if (enter.failed()) 303 return enter.retval(); 304 305 std::vector<PP_Var> input_args; 306 std::vector<PP_Var*> output_args; 307 input_args.push_back(socket_id); 308 input_args.push_back(address); 309 output_args.push_back(result); 310 return enter.SetResult(enter.functions()->CallBrowser( 311 "socket.leaveGroup", input_args, output_args, enter.callback())); 312 } 313 314 int32_t SetMulticastTimeToLive(PP_Instance instance, 315 PP_Var socket_id, 316 PP_Var ttl, 317 PP_Var* result, 318 PP_CompletionCallback callback) { 319 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 320 if (enter.failed()) 321 return enter.retval(); 322 323 std::vector<PP_Var> input_args; 324 std::vector<PP_Var*> output_args; 325 input_args.push_back(socket_id); 326 input_args.push_back(ttl); 327 output_args.push_back(result); 328 return enter.SetResult(enter.functions()->CallBrowser( 329 "socket.setMulticastTimeToLive", input_args, output_args, 330 enter.callback())); 331 } 332 333 int32_t SetMulticastLoopbackMode(PP_Instance instance, 334 PP_Var socket_id, 335 PP_Var enabled, 336 PP_Var* result, 337 PP_CompletionCallback callback) { 338 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 339 if (enter.failed()) 340 return enter.retval(); 341 342 std::vector<PP_Var> input_args; 343 std::vector<PP_Var*> output_args; 344 input_args.push_back(socket_id); 345 input_args.push_back(enabled); 346 output_args.push_back(result); 347 return enter.SetResult(enter.functions()->CallBrowser( 348 "socket.setMulticastLoopbackMode", input_args, output_args, 349 enter.callback())); 350 } 351 352 int32_t GetJoinedGroups(PP_Instance instance, 353 PP_Var socket_id, 354 PP_Var* groups, 355 PP_CompletionCallback callback) { 356 EnterInstanceAPI<ExtensionsCommon_API> enter(instance, callback); 357 if (enter.failed()) 358 return enter.retval(); 359 360 std::vector<PP_Var> input_args; 361 std::vector<PP_Var*> output_args; 362 input_args.push_back(socket_id); 363 output_args.push_back(groups); 364 return enter.SetResult(enter.functions()->CallBrowser( 365 "socket.getJoinedGroups", input_args, output_args, enter.callback())); 366 } 367 368 const PPB_Ext_Socket_Dev_0_1 g_ppb_ext_socket_dev_0_1_thunk = { 369 &Create, 370 &Destroy, 371 &Connect, 372 &Bind, 373 &Disconnect, 374 &Read, 375 &Write, 376 &RecvFrom, 377 &SendTo, 378 &Listen, 379 &Accept, 380 &SetKeepAlive, 381 &SetNoDelay, 382 &GetInfo, 383 &GetNetworkList 384 }; 385 386 const PPB_Ext_Socket_Dev_0_2 g_ppb_ext_socket_dev_0_2_thunk = { 387 &Create, 388 &Destroy, 389 &Connect, 390 &Bind, 391 &Disconnect, 392 &Read, 393 &Write, 394 &RecvFrom, 395 &SendTo, 396 &Listen, 397 &Accept, 398 &SetKeepAlive, 399 &SetNoDelay, 400 &GetInfo, 401 &GetNetworkList, 402 &JoinGroup, 403 &LeaveGroup, 404 &SetMulticastTimeToLive, 405 &SetMulticastLoopbackMode, 406 &GetJoinedGroups 407 }; 408 } // namespace 409 410 const PPB_Ext_Socket_Dev_0_1* GetPPB_Ext_Socket_Dev_0_1_Thunk() { 411 return &g_ppb_ext_socket_dev_0_1_thunk; 412 } 413 414 const PPB_Ext_Socket_Dev_0_2* GetPPB_Ext_Socket_Dev_0_2_Thunk() { 415 return &g_ppb_ext_socket_dev_0_2_thunk; 416 } 417 418 } // namespace thunk 419 } // namespace ppapi 420