Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / cpp / udp_socket.cc
blob19d2894775cec42fbbc1cb54e53d5358ddfd00e0
1 // Copyright 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.
5 #include "ppapi/cpp/udp_socket.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/cpp/completion_callback.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/module_impl.h"
11 #include "ppapi/cpp/var.h"
13 namespace pp {
15 namespace {
17 template <> const char* interface_name<PPB_UDPSocket_1_0>() {
18 return PPB_UDPSOCKET_INTERFACE_1_0;
21 template <> const char* interface_name<PPB_UDPSocket_1_1>() {
22 return PPB_UDPSOCKET_INTERFACE_1_1;
25 template <> const char* interface_name<PPB_UDPSocket_1_2>() {
26 return PPB_UDPSOCKET_INTERFACE_1_2;
29 } // namespace
31 UDPSocket::UDPSocket() {
34 UDPSocket::UDPSocket(const InstanceHandle& instance) {
35 if (has_interface<PPB_UDPSocket_1_2>()) {
36 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_2>()->Create(
37 instance.pp_instance()));
38 } else if (has_interface<PPB_UDPSocket_1_1>()) {
39 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_1>()->Create(
40 instance.pp_instance()));
41 } else if (has_interface<PPB_UDPSocket_1_0>()) {
42 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_0>()->Create(
43 instance.pp_instance()));
47 UDPSocket::UDPSocket(PassRef, PP_Resource resource)
48 : Resource(PASS_REF, resource) {
51 UDPSocket::UDPSocket(const UDPSocket& other) : Resource(other) {
54 UDPSocket::~UDPSocket() {
57 UDPSocket& UDPSocket::operator=(const UDPSocket& other) {
58 Resource::operator=(other);
59 return *this;
62 // static
63 bool UDPSocket::IsAvailable() {
64 return has_interface<PPB_UDPSocket_1_2>() ||
65 has_interface<PPB_UDPSocket_1_1>() ||
66 has_interface<PPB_UDPSocket_1_0>();
69 int32_t UDPSocket::Bind(const NetAddress& addr,
70 const CompletionCallback& callback) {
71 if (has_interface<PPB_UDPSocket_1_2>()) {
72 return get_interface<PPB_UDPSocket_1_2>()->Bind(
73 pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
75 if (has_interface<PPB_UDPSocket_1_1>()) {
76 return get_interface<PPB_UDPSocket_1_1>()->Bind(
77 pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
79 if (has_interface<PPB_UDPSocket_1_0>()) {
80 return get_interface<PPB_UDPSocket_1_0>()->Bind(
81 pp_resource(), addr.pp_resource(), callback.pp_completion_callback());
83 return callback.MayForce(PP_ERROR_NOINTERFACE);
86 NetAddress UDPSocket::GetBoundAddress() {
87 if (has_interface<PPB_UDPSocket_1_2>()) {
88 return NetAddress(
89 PASS_REF,
90 get_interface<PPB_UDPSocket_1_2>()->GetBoundAddress(pp_resource()));
92 if (has_interface<PPB_UDPSocket_1_1>()) {
93 return NetAddress(
94 PASS_REF,
95 get_interface<PPB_UDPSocket_1_1>()->GetBoundAddress(pp_resource()));
97 if (has_interface<PPB_UDPSocket_1_0>()) {
98 return NetAddress(
99 PASS_REF,
100 get_interface<PPB_UDPSocket_1_0>()->GetBoundAddress(pp_resource()));
102 return NetAddress();
105 int32_t UDPSocket::RecvFrom(
106 char* buffer,
107 int32_t num_bytes,
108 const CompletionCallbackWithOutput<NetAddress>& callback) {
109 if (has_interface<PPB_UDPSocket_1_2>()) {
110 return get_interface<PPB_UDPSocket_1_2>()->RecvFrom(
111 pp_resource(), buffer, num_bytes, callback.output(),
112 callback.pp_completion_callback());
114 if (has_interface<PPB_UDPSocket_1_1>()) {
115 return get_interface<PPB_UDPSocket_1_1>()->RecvFrom(
116 pp_resource(), buffer, num_bytes, callback.output(),
117 callback.pp_completion_callback());
119 if (has_interface<PPB_UDPSocket_1_0>()) {
120 return get_interface<PPB_UDPSocket_1_0>()->RecvFrom(
121 pp_resource(), buffer, num_bytes, callback.output(),
122 callback.pp_completion_callback());
124 return callback.MayForce(PP_ERROR_NOINTERFACE);
127 int32_t UDPSocket::SendTo(const char* buffer,
128 int32_t num_bytes,
129 const NetAddress& addr,
130 const CompletionCallback& callback) {
131 if (has_interface<PPB_UDPSocket_1_2>()) {
132 return get_interface<PPB_UDPSocket_1_2>()->SendTo(
133 pp_resource(), buffer, num_bytes, addr.pp_resource(),
134 callback.pp_completion_callback());
136 if (has_interface<PPB_UDPSocket_1_1>()) {
137 return get_interface<PPB_UDPSocket_1_1>()->SendTo(
138 pp_resource(), buffer, num_bytes, addr.pp_resource(),
139 callback.pp_completion_callback());
141 if (has_interface<PPB_UDPSocket_1_0>()) {
142 return get_interface<PPB_UDPSocket_1_0>()->SendTo(
143 pp_resource(), buffer, num_bytes, addr.pp_resource(),
144 callback.pp_completion_callback());
146 return callback.MayForce(PP_ERROR_NOINTERFACE);
149 void UDPSocket::Close() {
150 if (has_interface<PPB_UDPSocket_1_2>())
151 return get_interface<PPB_UDPSocket_1_2>()->Close(pp_resource());
152 if (has_interface<PPB_UDPSocket_1_1>())
153 return get_interface<PPB_UDPSocket_1_1>()->Close(pp_resource());
154 if (has_interface<PPB_UDPSocket_1_0>())
155 return get_interface<PPB_UDPSocket_1_0>()->Close(pp_resource());
158 int32_t UDPSocket::SetOption(PP_UDPSocket_Option name,
159 const Var& value,
160 const CompletionCallback& callback) {
161 if (has_interface<PPB_UDPSocket_1_2>()) {
162 return get_interface<PPB_UDPSocket_1_2>()->SetOption(
163 pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
165 if (has_interface<PPB_UDPSocket_1_1>()) {
166 return get_interface<PPB_UDPSocket_1_1>()->SetOption(
167 pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
169 if (has_interface<PPB_UDPSocket_1_0>()) {
170 return get_interface<PPB_UDPSocket_1_0>()->SetOption(
171 pp_resource(), name, value.pp_var(), callback.pp_completion_callback());
173 return callback.MayForce(PP_ERROR_NOINTERFACE);
176 int32_t UDPSocket::JoinGroup(const NetAddress& group,
177 const CompletionCallback callback) {
178 if (has_interface<PPB_UDPSocket_1_2>()) {
179 return get_interface<PPB_UDPSocket_1_2>()->JoinGroup(
180 pp_resource(), group.pp_resource(), callback.pp_completion_callback());
182 return callback.MayForce(PP_ERROR_NOINTERFACE);
185 int32_t UDPSocket::LeaveGroup(const NetAddress& group,
186 const CompletionCallback callback) {
187 if (has_interface<PPB_UDPSocket_1_2>()) {
188 return get_interface<PPB_UDPSocket_1_2>()->LeaveGroup(
189 pp_resource(), group.pp_resource(), callback.pp_completion_callback());
191 return callback.MayForce(PP_ERROR_NOINTERFACE);
194 } // namespace pp