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.
5 #include "ppapi/cpp/private/net_address_private.h"
7 #include "ppapi/c/pp_bool.h"
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/module_impl.h"
10 #include "ppapi/cpp/var.h"
16 template <> const char* interface_name
<PPB_NetAddress_Private_1_1
>() {
17 return PPB_NETADDRESS_PRIVATE_INTERFACE_1_1
;
20 template <> const char* interface_name
<PPB_NetAddress_Private_1_0
>() {
21 return PPB_NETADDRESS_PRIVATE_INTERFACE_1_0
;
24 template <> const char* interface_name
<PPB_NetAddress_Private_0_1
>() {
25 return PPB_NETADDRESS_PRIVATE_INTERFACE_0_1
;
31 bool NetAddressPrivate::IsAvailable() {
32 return has_interface
<PPB_NetAddress_Private_1_1
>() ||
33 has_interface
<PPB_NetAddress_Private_1_0
>() ||
34 has_interface
<PPB_NetAddress_Private_0_1
>();
38 bool NetAddressPrivate::AreEqual(const PP_NetAddress_Private
& addr1
,
39 const PP_NetAddress_Private
& addr2
) {
40 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
41 return !!get_interface
<PPB_NetAddress_Private_1_1
>()->AreEqual(&addr1
,
44 if (has_interface
<PPB_NetAddress_Private_1_0
>()) {
45 return !!get_interface
<PPB_NetAddress_Private_1_0
>()->AreEqual(&addr1
,
48 if (has_interface
<PPB_NetAddress_Private_0_1
>()) {
49 return !!get_interface
<PPB_NetAddress_Private_0_1
>()->AreEqual(&addr1
,
56 bool NetAddressPrivate::AreHostsEqual(const PP_NetAddress_Private
& addr1
,
57 const PP_NetAddress_Private
& addr2
) {
58 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
59 return !!get_interface
<PPB_NetAddress_Private_1_1
>()->AreHostsEqual(&addr1
,
62 if (has_interface
<PPB_NetAddress_Private_1_0
>()) {
63 return !!get_interface
<PPB_NetAddress_Private_1_0
>()->AreHostsEqual(&addr1
,
66 if (has_interface
<PPB_NetAddress_Private_0_1
>()) {
67 return !!get_interface
<PPB_NetAddress_Private_0_1
>()->AreHostsEqual(&addr1
,
74 std::string
NetAddressPrivate::Describe(const PP_NetAddress_Private
& addr
,
76 Module
* module
= Module::Get();
80 PP_Var result_pp_var
= PP_MakeUndefined();
81 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
82 result_pp_var
= get_interface
<PPB_NetAddress_Private_1_1
>()->Describe(
85 PP_FromBool(include_port
));
86 } else if (has_interface
<PPB_NetAddress_Private_1_0
>()) {
87 result_pp_var
= get_interface
<PPB_NetAddress_Private_1_0
>()->Describe(
90 PP_FromBool(include_port
));
91 } else if (has_interface
<PPB_NetAddress_Private_0_1
>()) {
92 result_pp_var
= get_interface
<PPB_NetAddress_Private_0_1
>()->Describe(
95 PP_FromBool(include_port
));
98 Var
result(PASS_REF
, result_pp_var
);
99 return result
.is_string() ? result
.AsString() : std::string();
103 bool NetAddressPrivate::ReplacePort(const PP_NetAddress_Private
& addr_in
,
105 PP_NetAddress_Private
* addr_out
) {
106 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
107 return !!get_interface
<PPB_NetAddress_Private_1_1
>()->ReplacePort(&addr_in
,
111 if (has_interface
<PPB_NetAddress_Private_1_0
>()) {
112 return !!get_interface
<PPB_NetAddress_Private_1_0
>()->ReplacePort(&addr_in
,
116 if (has_interface
<PPB_NetAddress_Private_0_1
>()) {
117 return !!get_interface
<PPB_NetAddress_Private_0_1
>()->ReplacePort(&addr_in
,
125 bool NetAddressPrivate::GetAnyAddress(bool is_ipv6
,
126 PP_NetAddress_Private
* addr
) {
127 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
128 get_interface
<PPB_NetAddress_Private_1_1
>()->GetAnyAddress(
129 PP_FromBool(is_ipv6
),
132 } else if (has_interface
<PPB_NetAddress_Private_1_0
>()) {
133 get_interface
<PPB_NetAddress_Private_1_0
>()->GetAnyAddress(
134 PP_FromBool(is_ipv6
),
137 } else if (has_interface
<PPB_NetAddress_Private_0_1
>()) {
138 get_interface
<PPB_NetAddress_Private_0_1
>()->GetAnyAddress(
139 PP_FromBool(is_ipv6
),
147 PP_NetAddressFamily_Private
NetAddressPrivate::GetFamily(
148 const PP_NetAddress_Private
& addr
) {
149 if (has_interface
<PPB_NetAddress_Private_1_1
>())
150 return get_interface
<PPB_NetAddress_Private_1_1
>()->GetFamily(&addr
);
151 if (has_interface
<PPB_NetAddress_Private_1_0
>())
152 return get_interface
<PPB_NetAddress_Private_1_0
>()->GetFamily(&addr
);
153 return PP_NETADDRESSFAMILY_PRIVATE_UNSPECIFIED
;
157 uint16_t NetAddressPrivate::GetPort(const PP_NetAddress_Private
& addr
) {
158 if (has_interface
<PPB_NetAddress_Private_1_1
>())
159 return get_interface
<PPB_NetAddress_Private_1_1
>()->GetPort(&addr
);
160 if (has_interface
<PPB_NetAddress_Private_1_0
>())
161 return get_interface
<PPB_NetAddress_Private_1_0
>()->GetPort(&addr
);
166 bool NetAddressPrivate::GetAddress(const PP_NetAddress_Private
& addr
,
168 uint16_t address_size
) {
169 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
170 return PP_ToBool(get_interface
<PPB_NetAddress_Private_1_1
>()->GetAddress(
175 if (has_interface
<PPB_NetAddress_Private_1_0
>()) {
176 return PP_ToBool(get_interface
<PPB_NetAddress_Private_1_0
>()->GetAddress(
185 uint32_t NetAddressPrivate::GetScopeID(const PP_NetAddress_Private
& addr
) {
186 if (has_interface
<PPB_NetAddress_Private_1_1
>())
187 return get_interface
<PPB_NetAddress_Private_1_1
>()->GetScopeID(&addr
);
192 bool NetAddressPrivate::CreateFromIPv4Address(
195 struct PP_NetAddress_Private
* addr_out
) {
196 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
197 get_interface
<PPB_NetAddress_Private_1_1
>()->CreateFromIPv4Address(
205 bool NetAddressPrivate::CreateFromIPv6Address(
206 const uint8_t ip
[16],
209 struct PP_NetAddress_Private
* addr_out
) {
210 if (has_interface
<PPB_NetAddress_Private_1_1
>()) {
211 get_interface
<PPB_NetAddress_Private_1_1
>()->CreateFromIPv6Address(
212 ip
, scope_id
, port
, addr_out
);