[email protected] | 669b283 | 2012-02-21 23:37:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 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" |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 10 | #include "ppapi/cpp/instance_handle.h" |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 11 | #include "ppapi/cpp/module.h" |
| 12 | #include "ppapi/cpp/module_impl.h" |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 13 | #include "ppapi/cpp/var.h" |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 14 | |
| 15 | namespace pp { |
| 16 | |
| 17 | namespace { |
| 18 | |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 19 | template <> const char* interface_name<PPB_UDPSocket_Private_0_4>() { |
| 20 | return PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4; |
| 21 | } |
| 22 | |
[email protected] | d8b0735 | 2012-04-26 09:55:18 | [diff] [blame] | 23 | template <> const char* interface_name<PPB_UDPSocket_Private_0_3>() { |
| 24 | return PPB_UDPSOCKET_PRIVATE_INTERFACE_0_3; |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 29 | UDPSocketPrivate::UDPSocketPrivate(const InstanceHandle& instance) { |
[email protected] | 38602098 | 2013-03-29 19:11:18 | [diff] [blame] | 30 | if (has_interface<PPB_UDPSocket_Private_0_4>()) { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 31 | PassRefFromConstructor(get_interface<PPB_UDPSocket_Private_0_4>()->Create( |
| 32 | instance.pp_instance())); |
| 33 | } else if (has_interface<PPB_UDPSocket_Private_0_3>()) { |
[email protected] | d8b0735 | 2012-04-26 09:55:18 | [diff] [blame] | 34 | PassRefFromConstructor(get_interface<PPB_UDPSocket_Private_0_3>()->Create( |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 35 | instance.pp_instance())); |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
[email protected] | d8b0735 | 2012-04-26 09:55:18 | [diff] [blame] | 39 | // static |
| 40 | bool UDPSocketPrivate::IsAvailable() { |
[email protected] | 38602098 | 2013-03-29 19:11:18 | [diff] [blame] | 41 | return has_interface<PPB_UDPSocket_Private_0_4>() || |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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; |
[email protected] | d8b0735 | 2012-04-26 09:55:18 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 54 | int32_t UDPSocketPrivate::Bind(const PP_NetAddress_Private* addr, |
| 55 | const CompletionCallback& callback) { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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); |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 669b283 | 2012-02-21 23:37:12 | [diff] [blame] | 67 | bool UDPSocketPrivate::GetBoundAddress(PP_NetAddress_Private* addr) { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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; |
[email protected] | 669b283 | 2012-02-21 23:37:12 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 83 | int32_t UDPSocketPrivate::RecvFrom(char* buffer, |
| 84 | int32_t num_bytes, |
| 85 | const CompletionCallback& callback) { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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); |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool UDPSocketPrivate::GetRecvFromAddress(PP_NetAddress_Private* addr) { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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; |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 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) { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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); |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 669b283 | 2012-02-21 23:37:12 | [diff] [blame] | 130 | void UDPSocketPrivate::Close() { |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 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()); |
[email protected] | 669b283 | 2012-02-21 23:37:12 | [diff] [blame] | 135 | } |
[email protected] | 0addda9 | 2012-08-31 08:55:28 | [diff] [blame] | 136 | |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 137 | } // namespace pp |