Home | History | Annotate | Download | only in private
      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/cpp/private/udp_socket_private.h"
      6 
      7 #include "ppapi/c/pp_bool.h"
      8 #include "ppapi/c/pp_errors.h"
      9 #include "ppapi/cpp/completion_callback.h"
     10 #include "ppapi/cpp/instance_handle.h"
     11 #include "ppapi/cpp/module.h"
     12 #include "ppapi/cpp/module_impl.h"
     13 #include "ppapi/cpp/var.h"
     14 
     15 namespace pp {
     16 
     17 namespace {
     18 
     19 template <> const char* interface_name<PPB_UDPSocket_Private_0_4>() {
     20   return PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4;
     21 }
     22 
     23 template <> const char* interface_name<PPB_UDPSocket_Private_0_3>() {
     24   return PPB_UDPSOCKET_PRIVATE_INTERFACE_0_3;
     25 }
     26 
     27 }  // namespace
     28 
     29 UDPSocketPrivate::UDPSocketPrivate(const InstanceHandle& instance) {
     30   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
     31     PassRefFromConstructor(get_interface<PPB_UDPSocket_Private_0_4>()->Create(
     32         instance.pp_instance()));
     33   } else if (has_interface<PPB_UDPSocket_Private_0_3>()) {
     34     PassRefFromConstructor(get_interface<PPB_UDPSocket_Private_0_3>()->Create(
     35         instance.pp_instance()));
     36   }
     37 }
     38 
     39 // static
     40 bool UDPSocketPrivate::IsAvailable() {
     41   return has_interface<PPB_UDPSocket_Private_0_4>() ||
     42       has_interface<PPB_UDPSocket_Private_0_3>();
     43 }
     44 
     45 int32_t UDPSocketPrivate::SetSocketFeature(PP_UDPSocketFeature_Private name,
     46                                            const Var& value) {
     47   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
     48     return get_interface<PPB_UDPSocket_Private_0_4>()->SetSocketFeature(
     49         pp_resource(), name, value.pp_var());
     50   }
     51   return PP_ERROR_NOINTERFACE;
     52 }
     53 
     54 int32_t UDPSocketPrivate::Bind(const PP_NetAddress_Private* addr,
     55                                const CompletionCallback& callback) {
     56   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
     57     return get_interface<PPB_UDPSocket_Private_0_4>()->Bind(
     58         pp_resource(), addr, callback.pp_completion_callback());
     59   }
     60   if (has_interface<PPB_UDPSocket_Private_0_3>()) {
     61     return get_interface<PPB_UDPSocket_Private_0_3>()->Bind(
     62         pp_resource(), addr, callback.pp_completion_callback());
     63   }
     64   return callback.MayForce(PP_ERROR_NOINTERFACE);
     65 }
     66 
     67 bool UDPSocketPrivate::GetBoundAddress(PP_NetAddress_Private* addr) {
     68   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
     69     PP_Bool result =
     70         get_interface<PPB_UDPSocket_Private_0_4>()->GetBoundAddress(
     71             pp_resource(), addr);
     72     return PP_ToBool(result);
     73   }
     74   if (has_interface<PPB_UDPSocket_Private_0_3>()) {
     75     PP_Bool result =
     76         get_interface<PPB_UDPSocket_Private_0_3>()->GetBoundAddress(
     77             pp_resource(), addr);
     78     return PP_ToBool(result);
     79   }
     80   return false;
     81 }
     82 
     83 int32_t UDPSocketPrivate::RecvFrom(char* buffer,
     84                                    int32_t num_bytes,
     85                                    const CompletionCallback& callback) {
     86   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
     87     return get_interface<PPB_UDPSocket_Private_0_4>()->RecvFrom(
     88         pp_resource(), buffer, num_bytes, callback.pp_completion_callback());
     89   }
     90   if (has_interface<PPB_UDPSocket_Private_0_3>()) {
     91     return get_interface<PPB_UDPSocket_Private_0_3>()->RecvFrom(
     92         pp_resource(), buffer, num_bytes, callback.pp_completion_callback());
     93   }
     94   return callback.MayForce(PP_ERROR_NOINTERFACE);
     95 }
     96 
     97 bool UDPSocketPrivate::GetRecvFromAddress(PP_NetAddress_Private* addr) {
     98   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
     99     PP_Bool result =
    100         get_interface<PPB_UDPSocket_Private_0_4>()->GetRecvFromAddress(
    101             pp_resource(), addr);
    102     return PP_ToBool(result);
    103   }
    104   if (has_interface<PPB_UDPSocket_Private_0_3>()) {
    105     PP_Bool result =
    106         get_interface<PPB_UDPSocket_Private_0_3>()->GetRecvFromAddress(
    107             pp_resource(), addr);
    108     return PP_ToBool(result);
    109   }
    110   return false;
    111 }
    112 
    113 int32_t UDPSocketPrivate::SendTo(const char* buffer,
    114                                  int32_t num_bytes,
    115                                  const PP_NetAddress_Private* addr,
    116                                  const CompletionCallback& callback) {
    117   if (has_interface<PPB_UDPSocket_Private_0_4>()) {
    118     return get_interface<PPB_UDPSocket_Private_0_4>()->SendTo(
    119         pp_resource(), buffer, num_bytes, addr,
    120         callback.pp_completion_callback());
    121   }
    122   if (has_interface<PPB_UDPSocket_Private_0_3>()) {
    123     return get_interface<PPB_UDPSocket_Private_0_3>()->SendTo(
    124         pp_resource(), buffer, num_bytes, addr,
    125         callback.pp_completion_callback());
    126   }
    127   return callback.MayForce(PP_ERROR_NOINTERFACE);
    128 }
    129 
    130 void UDPSocketPrivate::Close() {
    131   if (has_interface<PPB_UDPSocket_Private_0_4>())
    132     return get_interface<PPB_UDPSocket_Private_0_4>()->Close(pp_resource());
    133   if (has_interface<PPB_UDPSocket_Private_0_3>())
    134     return get_interface<PPB_UDPSocket_Private_0_3>()->Close(pp_resource());
    135 }
    136 
    137 }  // namespace pp
    138