Revert of Use ExtensionRegistryObserver instead of deprecated extension notification...
[chromium-blink-merge.git] / ppapi / cpp / private / net_address_private.cc
blob3a32827639afc9e961af9a38343319b76db01dc7
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"
12 namespace pp {
14 namespace {
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;
28 } // namespace
30 // static
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>();
37 // static
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,
42 &addr2);
44 if (has_interface<PPB_NetAddress_Private_1_0>()) {
45 return !!get_interface<PPB_NetAddress_Private_1_0>()->AreEqual(&addr1,
46 &addr2);
48 if (has_interface<PPB_NetAddress_Private_0_1>()) {
49 return !!get_interface<PPB_NetAddress_Private_0_1>()->AreEqual(&addr1,
50 &addr2);
52 return false;
55 // static
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,
60 &addr2);
62 if (has_interface<PPB_NetAddress_Private_1_0>()) {
63 return !!get_interface<PPB_NetAddress_Private_1_0>()->AreHostsEqual(&addr1,
64 &addr2);
66 if (has_interface<PPB_NetAddress_Private_0_1>()) {
67 return !!get_interface<PPB_NetAddress_Private_0_1>()->AreHostsEqual(&addr1,
68 &addr2);
70 return false;
73 // static
74 std::string NetAddressPrivate::Describe(const PP_NetAddress_Private& addr,
75 bool include_port) {
76 Module* module = Module::Get();
77 if (!module)
78 return std::string();
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(
83 module->pp_module(),
84 &addr,
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(
88 module->pp_module(),
89 &addr,
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(
93 module->pp_module(),
94 &addr,
95 PP_FromBool(include_port));
98 Var result(PASS_REF, result_pp_var);
99 return result.is_string() ? result.AsString() : std::string();
102 // static
103 bool NetAddressPrivate::ReplacePort(const PP_NetAddress_Private& addr_in,
104 uint16_t port,
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,
108 port,
109 addr_out);
111 if (has_interface<PPB_NetAddress_Private_1_0>()) {
112 return !!get_interface<PPB_NetAddress_Private_1_0>()->ReplacePort(&addr_in,
113 port,
114 addr_out);
116 if (has_interface<PPB_NetAddress_Private_0_1>()) {
117 return !!get_interface<PPB_NetAddress_Private_0_1>()->ReplacePort(&addr_in,
118 port,
119 addr_out);
121 return false;
124 // static
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),
130 addr);
131 return true;
132 } else if (has_interface<PPB_NetAddress_Private_1_0>()) {
133 get_interface<PPB_NetAddress_Private_1_0>()->GetAnyAddress(
134 PP_FromBool(is_ipv6),
135 addr);
136 return true;
137 } else if (has_interface<PPB_NetAddress_Private_0_1>()) {
138 get_interface<PPB_NetAddress_Private_0_1>()->GetAnyAddress(
139 PP_FromBool(is_ipv6),
140 addr);
141 return true;
143 return false;
146 // static
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;
156 // static
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);
162 return 0;
165 // static
166 bool NetAddressPrivate::GetAddress(const PP_NetAddress_Private& addr,
167 void* address,
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(
171 &addr,
172 address,
173 address_size));
175 if (has_interface<PPB_NetAddress_Private_1_0>()) {
176 return PP_ToBool(get_interface<PPB_NetAddress_Private_1_0>()->GetAddress(
177 &addr,
178 address,
179 address_size));
181 return false;
184 // static
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);
188 return 0;
191 // static
192 bool NetAddressPrivate::CreateFromIPv4Address(
193 const uint8_t ip[4],
194 uint16_t port,
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(
198 ip, port, addr_out);
199 return true;
201 return false;
204 // static
205 bool NetAddressPrivate::CreateFromIPv6Address(
206 const uint8_t ip[16],
207 uint32_t scope_id,
208 uint16_t port,
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);
213 return true;
215 return false;
218 } // namespace pp