Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "ppapi/cpp/extensions/dev/socket_dev.h" | |
| 6 | |
| 7 #include "ppapi/cpp/completion_callback.h" | |
| 8 #include "ppapi/cpp/extensions/optional.h" | |
| 9 #include "ppapi/cpp/extensions/to_var_converter.h" | |
| 10 #include "ppapi/cpp/logging.h" | |
| 11 #include "ppapi/cpp/module_impl.h" | |
| 12 | |
| 13 namespace pp { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 template <> const char* interface_name<PPB_Ext_Socket_Dev_0_1>() { | |
| 18 return PPB_EXT_SOCKET_DEV_INTERFACE_0_1; | |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 namespace ext { | |
| 24 namespace socket { | |
| 25 | |
| 26 const char* const SocketType_Dev::kTcp = "tcp"; | |
| 27 const char* const SocketType_Dev::kUdp = "udp"; | |
| 28 | |
| 29 SocketType_Dev::SocketType_Dev() : value(NONE) { | |
| 30 } | |
| 31 | |
| 32 SocketType_Dev::SocketType_Dev(ValueType in_value) : value(in_value) { | |
| 33 } | |
| 34 | |
| 35 SocketType_Dev::~SocketType_Dev() { | |
| 36 } | |
| 37 | |
| 38 bool SocketType_Dev::Populate(const PP_Var& var_value) { | |
| 39 if (var_value.type != PP_VARTYPE_STRING) | |
| 40 return false; | |
| 41 | |
| 42 std::string string_value = Var(var_value).AsString(); | |
| 43 if (string_value == kTcp) { | |
| 44 value = TCP; | |
| 45 } else if (string_value == kUdp) { | |
| 46 value = UDP; | |
| 47 } else { | |
| 48 value = NONE; | |
| 49 return false; | |
| 50 } | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 Var SocketType_Dev::CreateVar() const { | |
| 55 switch (value) { | |
| 56 case TCP: | |
| 57 return Var(kTcp); | |
| 58 case UDP: | |
| 59 return Var(kUdp); | |
| 60 default: | |
| 61 PP_NOTREACHED(); | |
| 62 return Var(std::string()); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 const char* const CreateInfo_Dev::kSocketId = "socketId"; | |
| 67 | |
| 68 CreateInfo_Dev::CreateInfo_Dev() | |
| 69 : socket_id(kSocketId) { | |
| 70 } | |
| 71 | |
| 72 CreateInfo_Dev::~CreateInfo_Dev() { | |
| 73 } | |
| 74 | |
| 75 bool CreateInfo_Dev::Populate(const PP_Ext_Socket_CreateInfo_Dev& value) { | |
| 76 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 77 return false; | |
| 78 | |
| 79 VarDictionary_Dev dict(value); | |
| 80 bool result = socket_id.Populate(dict); | |
| 81 | |
| 82 return result; | |
| 83 } | |
| 84 | |
| 85 Var CreateInfo_Dev::CreateVar() const { | |
| 86 VarDictionary_Dev dict; | |
| 87 | |
| 88 bool result = socket_id.AddTo(&dict); | |
| 89 // Suppress unused variable warnings. | |
| 90 static_cast<void>(result); | |
| 91 PP_DCHECK(result); | |
| 92 | |
| 93 return dict; | |
| 94 } | |
| 95 | |
| 96 const char* const AcceptInfo_Dev::kResultCode = "resultCode"; | |
| 97 const char* const AcceptInfo_Dev::kSocketId = "socketId"; | |
| 98 | |
| 99 AcceptInfo_Dev::AcceptInfo_Dev() | |
| 100 : result_code(kResultCode), | |
| 101 socket_id(kSocketId) { | |
| 102 } | |
| 103 | |
| 104 AcceptInfo_Dev::~AcceptInfo_Dev() { | |
| 105 } | |
| 106 | |
| 107 bool AcceptInfo_Dev::Populate(const PP_Ext_Socket_AcceptInfo_Dev& value) { | |
| 108 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 109 return false; | |
| 110 | |
| 111 VarDictionary_Dev dict(value); | |
| 112 bool result = result_code.Populate(dict); | |
| 113 result = socket_id.Populate(dict) && result; | |
| 114 | |
| 115 return result; | |
| 116 } | |
| 117 | |
| 118 Var AcceptInfo_Dev::CreateVar() const { | |
| 119 VarDictionary_Dev dict; | |
| 120 | |
| 121 bool result = result_code.AddTo(&dict); | |
| 122 result = socket_id.MayAddTo(&dict) && result; | |
| 123 PP_DCHECK(result); | |
| 124 | |
| 125 return dict; | |
| 126 } | |
| 127 | |
| 128 const char* const ReadInfo_Dev::kResultCode = "resultCode"; | |
| 129 const char* const ReadInfo_Dev::kData = "data"; | |
| 130 | |
| 131 ReadInfo_Dev::ReadInfo_Dev() | |
| 132 : result_code(kResultCode), | |
| 133 data(kData) { | |
| 134 } | |
| 135 | |
| 136 ReadInfo_Dev::~ReadInfo_Dev() { | |
| 137 } | |
| 138 | |
| 139 bool ReadInfo_Dev::Populate(const PP_Ext_Socket_ReadInfo_Dev& value) { | |
| 140 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 141 return false; | |
| 142 | |
| 143 VarDictionary_Dev dict(value); | |
| 144 bool result = result_code.Populate(dict); | |
| 145 result = data.Populate(dict) && result; | |
| 146 | |
| 147 return result; | |
| 148 } | |
| 149 | |
| 150 Var ReadInfo_Dev::CreateVar() const { | |
| 151 VarDictionary_Dev dict; | |
| 152 | |
| 153 bool result = result_code.AddTo(&dict); | |
| 154 result = data.AddTo(&dict) && result; | |
| 155 PP_DCHECK(result); | |
| 156 | |
| 157 return dict; | |
| 158 } | |
| 159 | |
| 160 const char* const WriteInfo_Dev::kBytesWritten = "bytesWritten"; | |
| 161 | |
| 162 WriteInfo_Dev::WriteInfo_Dev() | |
| 163 : bytes_written(kBytesWritten) { | |
| 164 } | |
| 165 | |
| 166 WriteInfo_Dev::~WriteInfo_Dev() { | |
| 167 } | |
| 168 | |
| 169 bool WriteInfo_Dev::Populate(const PP_Ext_Socket_WriteInfo_Dev& value) { | |
| 170 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 171 return false; | |
| 172 | |
| 173 VarDictionary_Dev dict(value); | |
| 174 bool result = bytes_written.Populate(dict); | |
| 175 | |
| 176 return result; | |
| 177 } | |
| 178 | |
| 179 Var WriteInfo_Dev::CreateVar() const { | |
| 180 VarDictionary_Dev dict; | |
| 181 | |
| 182 bool result = bytes_written.AddTo(&dict); | |
| 183 // Suppress unused variable warnings. | |
| 184 static_cast<void>(result); | |
| 185 PP_DCHECK(result); | |
| 186 | |
| 187 return dict; | |
| 188 } | |
| 189 | |
| 190 const char* const RecvFromInfo_Dev::kResultCode = "result_code"; | |
|
raymes
2013/04/08 01:59:48
should this be "resultCode" ?
yzshen1
2013/04/08 16:31:08
Good catch! :)
On 2013/04/08 01:59:48, raymes wro
| |
| 191 const char* const RecvFromInfo_Dev::kData = "data"; | |
| 192 const char* const RecvFromInfo_Dev::kAddress = "address"; | |
| 193 const char* const RecvFromInfo_Dev::kPort = "port"; | |
| 194 | |
| 195 RecvFromInfo_Dev::RecvFromInfo_Dev() | |
| 196 : result_code(kResultCode), | |
| 197 data(kData), | |
| 198 address(kAddress), | |
| 199 port(kPort) { | |
| 200 } | |
| 201 | |
| 202 RecvFromInfo_Dev::~RecvFromInfo_Dev() { | |
| 203 } | |
| 204 | |
| 205 bool RecvFromInfo_Dev::Populate(const PP_Ext_Socket_RecvFromInfo_Dev& value) { | |
| 206 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 207 return false; | |
| 208 | |
| 209 VarDictionary_Dev dict(value); | |
| 210 bool result = result_code.Populate(dict); | |
| 211 result = data.Populate(dict) && result; | |
| 212 result = address.Populate(dict) && result; | |
| 213 result = port.Populate(dict) && result; | |
| 214 | |
| 215 return result; | |
| 216 } | |
| 217 | |
| 218 Var RecvFromInfo_Dev::CreateVar() const { | |
| 219 VarDictionary_Dev dict; | |
| 220 | |
| 221 bool result = result_code.AddTo(&dict); | |
| 222 result = data.AddTo(&dict) && result; | |
| 223 result = address.AddTo(&dict) && result; | |
| 224 result = port.AddTo(&dict) && result; | |
| 225 PP_DCHECK(result); | |
| 226 | |
| 227 return dict; | |
| 228 } | |
| 229 | |
| 230 const char* const SocketInfo_Dev::kSocketType = "socketType"; | |
| 231 const char* const SocketInfo_Dev::kConnected = "connected"; | |
| 232 const char* const SocketInfo_Dev::kPeerAddress = "peerAddress"; | |
| 233 const char* const SocketInfo_Dev::kPeerPort = "peerPort"; | |
| 234 const char* const SocketInfo_Dev::kLocalAddress = "localAddress"; | |
| 235 const char* const SocketInfo_Dev::kLocalPort = "localPort"; | |
| 236 | |
| 237 SocketInfo_Dev::SocketInfo_Dev() | |
| 238 : socket_type(kSocketType), | |
| 239 connected(kConnected), | |
| 240 peer_address(kPeerAddress), | |
| 241 peer_port(kPeerPort), | |
| 242 local_address(kLocalAddress), | |
| 243 local_port(kLocalPort) { | |
| 244 } | |
| 245 | |
| 246 SocketInfo_Dev::~SocketInfo_Dev() { | |
| 247 } | |
| 248 | |
| 249 bool SocketInfo_Dev::Populate(const PP_Ext_Socket_SocketInfo_Dev& value) { | |
| 250 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 251 return false; | |
| 252 | |
| 253 VarDictionary_Dev dict(value); | |
| 254 bool result = socket_type.Populate(dict); | |
| 255 result = connected.Populate(dict) && result; | |
| 256 result = peer_address.Populate(dict) && result; | |
| 257 result = peer_port.Populate(dict) && result; | |
| 258 result = local_address.Populate(dict) && result; | |
| 259 result = local_port.Populate(dict) && result; | |
| 260 | |
| 261 return result; | |
| 262 } | |
| 263 | |
| 264 Var SocketInfo_Dev::CreateVar() const { | |
| 265 VarDictionary_Dev dict; | |
| 266 | |
| 267 bool result = socket_type.AddTo(&dict); | |
| 268 result = connected.AddTo(&dict) && result; | |
| 269 result = peer_address.MayAddTo(&dict) && result; | |
| 270 result = peer_port.MayAddTo(&dict) && result; | |
| 271 result = local_address.MayAddTo(&dict) && result; | |
| 272 result = local_port.MayAddTo(&dict) && result; | |
| 273 PP_DCHECK(result); | |
| 274 | |
| 275 return dict; | |
| 276 } | |
| 277 | |
| 278 const char* const NetworkInterface_Dev::kName = "name"; | |
| 279 const char* const NetworkInterface_Dev::kAddress = "address"; | |
| 280 | |
| 281 NetworkInterface_Dev::NetworkInterface_Dev() | |
| 282 : name(kName), | |
| 283 address(kAddress) { | |
| 284 } | |
| 285 | |
| 286 NetworkInterface_Dev::~NetworkInterface_Dev() { | |
| 287 } | |
| 288 | |
| 289 bool NetworkInterface_Dev::Populate( | |
| 290 const PP_Ext_Socket_NetworkInterface_Dev& value) { | |
| 291 if (value.type != PP_VARTYPE_DICTIONARY) | |
| 292 return false; | |
| 293 | |
| 294 VarDictionary_Dev dict(value); | |
| 295 bool result = name.Populate(dict); | |
| 296 result = address.Populate(dict) && result; | |
| 297 | |
| 298 return result; | |
| 299 } | |
| 300 | |
| 301 Var NetworkInterface_Dev::CreateVar() const { | |
| 302 VarDictionary_Dev dict; | |
| 303 | |
| 304 bool result = name.AddTo(&dict); | |
| 305 result = address.AddTo(&dict) && result; | |
| 306 PP_DCHECK(result); | |
| 307 | |
| 308 return dict; | |
| 309 } | |
| 310 | |
| 311 Socket_Dev::Socket_Dev(const InstanceHandle& instance) : instance_(instance) { | |
| 312 } | |
| 313 | |
| 314 Socket_Dev::~Socket_Dev() { | |
| 315 } | |
| 316 | |
| 317 int32_t Socket_Dev::Create( | |
| 318 const SocketType_Dev& type, | |
| 319 const Optional<CreateOptions_Dev>& options, | |
| 320 const CompletionCallbackWithOutput<CreateInfo_Dev>& callback) { | |
| 321 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 322 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 323 | |
| 324 internal::ToVarConverter<SocketType_Dev> type_var(type); | |
| 325 internal::ToVarConverter<Optional<CreateOptions_Dev> > options_var(options); | |
| 326 | |
| 327 return get_interface<PPB_Ext_Socket_Dev_0_1>()->Create( | |
| 328 instance_.pp_instance(), | |
| 329 type_var.pp_var(), | |
| 330 options_var.pp_var(), | |
| 331 callback.output(), | |
| 332 callback.pp_completion_callback()); | |
| 333 } | |
| 334 | |
| 335 void Socket_Dev::Destroy(int32_t socket_id) { | |
| 336 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 337 return; | |
| 338 | |
| 339 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 340 | |
| 341 return get_interface<PPB_Ext_Socket_Dev_0_1>()->Destroy( | |
| 342 instance_.pp_instance(), | |
| 343 socket_id_var.pp_var()); | |
| 344 } | |
| 345 | |
| 346 void Socket_Dev::Disconnect(int32_t socket_id) { | |
| 347 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 348 return; | |
| 349 | |
| 350 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 351 | |
| 352 return get_interface<PPB_Ext_Socket_Dev_0_1>()->Disconnect( | |
| 353 instance_.pp_instance(), | |
| 354 socket_id_var.pp_var()); | |
| 355 } | |
| 356 | |
| 357 int32_t Socket_Dev::Read( | |
| 358 int32_t socket_id, | |
| 359 const Optional<int32_t>& buffer_size, | |
| 360 const CompletionCallbackWithOutput<ReadInfo_Dev>& callback) { | |
| 361 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 362 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 363 | |
| 364 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 365 internal::ToVarConverter<Optional<int32_t> > buffer_size_var(buffer_size); | |
| 366 | |
| 367 return get_interface<PPB_Ext_Socket_Dev_0_1>()->Read( | |
| 368 instance_.pp_instance(), | |
| 369 socket_id_var.pp_var(), | |
| 370 buffer_size_var.pp_var(), | |
| 371 callback.output(), | |
| 372 callback.pp_completion_callback()); | |
| 373 } | |
| 374 | |
| 375 int32_t Socket_Dev::Write( | |
| 376 int32_t socket_id, | |
| 377 const Var& data, | |
| 378 const CompletionCallbackWithOutput<WriteInfo_Dev>& callback) { | |
| 379 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 380 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 381 | |
| 382 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 383 internal::ToVarConverter<Var> data_var(data); | |
| 384 | |
| 385 return get_interface<PPB_Ext_Socket_Dev_0_1>()->Write( | |
| 386 instance_.pp_instance(), | |
| 387 socket_id_var.pp_var(), | |
| 388 data_var.pp_var(), | |
| 389 callback.output(), | |
| 390 callback.pp_completion_callback()); | |
| 391 } | |
| 392 | |
| 393 int32_t Socket_Dev::RecvFrom( | |
| 394 int32_t socket_id, | |
| 395 const Optional<int32_t>& buffer_size, | |
| 396 const CompletionCallbackWithOutput<RecvFromInfo_Dev>& callback) { | |
| 397 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 398 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 399 | |
| 400 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 401 internal::ToVarConverter<Optional<int32_t> > buffer_size_var(buffer_size); | |
| 402 | |
| 403 return get_interface<PPB_Ext_Socket_Dev_0_1>()->RecvFrom( | |
| 404 instance_.pp_instance(), | |
| 405 socket_id_var.pp_var(), | |
| 406 buffer_size_var.pp_var(), | |
| 407 callback.output(), | |
| 408 callback.pp_completion_callback()); | |
| 409 } | |
| 410 | |
| 411 int32_t Socket_Dev::SendTo( | |
| 412 int32_t socket_id, | |
| 413 const Var& data, | |
| 414 const std::string& address, | |
| 415 int32_t port, | |
| 416 const CompletionCallbackWithOutput<WriteInfo_Dev>& callback) { | |
| 417 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 418 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 419 | |
| 420 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 421 internal::ToVarConverter<Var> data_var(data); | |
| 422 internal::ToVarConverter<std::string> address_var(address); | |
| 423 internal::ToVarConverter<int32_t> port_var(port); | |
| 424 | |
| 425 return get_interface<PPB_Ext_Socket_Dev_0_1>()->SendTo( | |
| 426 instance_.pp_instance(), | |
| 427 socket_id_var.pp_var(), | |
| 428 data_var.pp_var(), | |
| 429 address_var.pp_var(), | |
| 430 port_var.pp_var(), | |
| 431 callback.output(), | |
| 432 callback.pp_completion_callback()); | |
| 433 } | |
| 434 | |
| 435 int32_t Socket_Dev::Accept( | |
| 436 int32_t socket_id, | |
| 437 const CompletionCallbackWithOutput<AcceptInfo_Dev>& callback) { | |
| 438 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 439 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 440 | |
| 441 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 442 | |
| 443 return get_interface<PPB_Ext_Socket_Dev_0_1>()->Accept( | |
| 444 instance_.pp_instance(), | |
| 445 socket_id_var.pp_var(), | |
| 446 callback.output(), | |
| 447 callback.pp_completion_callback()); | |
| 448 } | |
| 449 | |
| 450 int32_t Socket_Dev::GetInfo( | |
| 451 int32_t socket_id, | |
| 452 const CompletionCallbackWithOutput<SocketInfo_Dev>& callback) { | |
| 453 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 454 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 455 | |
| 456 internal::ToVarConverter<int32_t> socket_id_var(socket_id); | |
| 457 | |
| 458 return get_interface<PPB_Ext_Socket_Dev_0_1>()->GetInfo( | |
| 459 instance_.pp_instance(), | |
| 460 socket_id_var.pp_var(), | |
| 461 callback.output(), | |
| 462 callback.pp_completion_callback()); | |
| 463 } | |
| 464 | |
| 465 int32_t Socket_Dev::GetNetworkList( | |
| 466 const CompletionCallbackWithOutput<std::vector<NetworkInterface_Dev> >& | |
| 467 callback) { | |
| 468 if (!has_interface<PPB_Ext_Socket_Dev_0_1>()) | |
| 469 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 470 | |
| 471 return get_interface<PPB_Ext_Socket_Dev_0_1>()->GetNetworkList( | |
| 472 instance_.pp_instance(), | |
| 473 callback.output(), | |
| 474 callback.pp_completion_callback()); | |
| 475 } | |
| 476 | |
| 477 } // namespace socket | |
| 478 } // namespace ext | |
| 479 } // namespace pp | |
| OLD | NEW |