| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/udp_socket.h" | 5 #include "ppapi/cpp/udp_socket.h" |
| 6 | 6 |
| 7 #include "ppapi/c/pp_errors.h" | 7 #include "ppapi/c/pp_errors.h" |
| 8 #include "ppapi/cpp/completion_callback.h" | 8 #include "ppapi/cpp/completion_callback.h" |
| 9 #include "ppapi/cpp/instance_handle.h" | 9 #include "ppapi/cpp/instance_handle.h" |
| 10 #include "ppapi/cpp/module_impl.h" | 10 #include "ppapi/cpp/module_impl.h" |
| 11 #include "ppapi/cpp/var.h" | 11 #include "ppapi/cpp/var.h" |
| 12 | 12 |
| 13 namespace pp { | 13 namespace pp { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 template <> const char* interface_name<PPB_UDPSocket_1_0>() { | 17 template <> const char* interface_name<PPB_UDPSocket_1_0>() { |
| 18 return PPB_UDPSOCKET_INTERFACE_1_0; | 18 return PPB_UDPSOCKET_INTERFACE_1_0; |
| 19 } | 19 } |
| 20 | 20 |
| 21 template <> const char* interface_name<PPB_UDPSocket_1_1>() { | 21 template <> const char* interface_name<PPB_UDPSocket_1_1>() { |
| 22 return PPB_UDPSOCKET_INTERFACE_1_1; | 22 return PPB_UDPSOCKET_INTERFACE_1_1; |
| 23 } | 23 } |
| 24 | 24 |
| 25 template <> const char* interface_name<PPB_UDPSocket_1_2>() { |
| 26 return PPB_UDPSOCKET_INTERFACE_1_2; |
| 27 } |
| 28 |
| 25 } // namespace | 29 } // namespace |
| 26 | 30 |
| 27 UDPSocket::UDPSocket() { | 31 UDPSocket::UDPSocket() { |
| 28 } | 32 } |
| 29 | 33 |
| 30 UDPSocket::UDPSocket(const InstanceHandle& instance) { | 34 UDPSocket::UDPSocket(const InstanceHandle& instance) { |
| 31 if (has_interface<PPB_UDPSocket_1_1>()) { | 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>()) { |
| 32 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_1>()->Create( | 39 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_1>()->Create( |
| 33 instance.pp_instance())); | 40 instance.pp_instance())); |
| 34 } else if (has_interface<PPB_UDPSocket_1_0>()) { | 41 } else if (has_interface<PPB_UDPSocket_1_0>()) { |
| 35 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_0>()->Create( | 42 PassRefFromConstructor(get_interface<PPB_UDPSocket_1_0>()->Create( |
| 36 instance.pp_instance())); | 43 instance.pp_instance())); |
| 37 } | 44 } |
| 38 } | 45 } |
| 39 | 46 |
| 40 UDPSocket::UDPSocket(PassRef, PP_Resource resource) | 47 UDPSocket::UDPSocket(PassRef, PP_Resource resource) |
| 41 : Resource(PASS_REF, resource) { | 48 : Resource(PASS_REF, resource) { |
| 42 } | 49 } |
| 43 | 50 |
| 44 UDPSocket::UDPSocket(const UDPSocket& other) : Resource(other) { | 51 UDPSocket::UDPSocket(const UDPSocket& other) : Resource(other) { |
| 45 } | 52 } |
| 46 | 53 |
| 47 UDPSocket::~UDPSocket() { | 54 UDPSocket::~UDPSocket() { |
| 48 } | 55 } |
| 49 | 56 |
| 50 UDPSocket& UDPSocket::operator=(const UDPSocket& other) { | 57 UDPSocket& UDPSocket::operator=(const UDPSocket& other) { |
| 51 Resource::operator=(other); | 58 Resource::operator=(other); |
| 52 return *this; | 59 return *this; |
| 53 } | 60 } |
| 54 | 61 |
| 55 // static | 62 // static |
| 56 bool UDPSocket::IsAvailable() { | 63 bool UDPSocket::IsAvailable() { |
| 57 return has_interface<PPB_UDPSocket_1_1>() || | 64 return has_interface<PPB_UDPSocket_1_2>() || |
| 65 has_interface<PPB_UDPSocket_1_1>() || |
| 58 has_interface<PPB_UDPSocket_1_0>(); | 66 has_interface<PPB_UDPSocket_1_0>(); |
| 59 } | 67 } |
| 60 | 68 |
| 61 int32_t UDPSocket::Bind(const NetAddress& addr, | 69 int32_t UDPSocket::Bind(const NetAddress& addr, |
| 62 const CompletionCallback& callback) { | 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()); |
| 74 } |
| 63 if (has_interface<PPB_UDPSocket_1_1>()) { | 75 if (has_interface<PPB_UDPSocket_1_1>()) { |
| 64 return get_interface<PPB_UDPSocket_1_1>()->Bind( | 76 return get_interface<PPB_UDPSocket_1_1>()->Bind( |
| 65 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); | 77 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 66 } | 78 } |
| 67 if (has_interface<PPB_UDPSocket_1_0>()) { | 79 if (has_interface<PPB_UDPSocket_1_0>()) { |
| 68 return get_interface<PPB_UDPSocket_1_0>()->Bind( | 80 return get_interface<PPB_UDPSocket_1_0>()->Bind( |
| 69 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); | 81 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 70 } | 82 } |
| 71 return callback.MayForce(PP_ERROR_NOINTERFACE); | 83 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 72 } | 84 } |
| 73 | 85 |
| 74 NetAddress UDPSocket::GetBoundAddress() { | 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())); |
| 91 } |
| 75 if (has_interface<PPB_UDPSocket_1_1>()) { | 92 if (has_interface<PPB_UDPSocket_1_1>()) { |
| 76 return NetAddress( | 93 return NetAddress( |
| 77 PASS_REF, | 94 PASS_REF, |
| 78 get_interface<PPB_UDPSocket_1_1>()->GetBoundAddress(pp_resource())); | 95 get_interface<PPB_UDPSocket_1_1>()->GetBoundAddress(pp_resource())); |
| 79 } | 96 } |
| 80 if (has_interface<PPB_UDPSocket_1_0>()) { | 97 if (has_interface<PPB_UDPSocket_1_0>()) { |
| 81 return NetAddress( | 98 return NetAddress( |
| 82 PASS_REF, | 99 PASS_REF, |
| 83 get_interface<PPB_UDPSocket_1_0>()->GetBoundAddress(pp_resource())); | 100 get_interface<PPB_UDPSocket_1_0>()->GetBoundAddress(pp_resource())); |
| 84 } | 101 } |
| 85 return NetAddress(); | 102 return NetAddress(); |
| 86 } | 103 } |
| 87 | 104 |
| 88 int32_t UDPSocket::RecvFrom( | 105 int32_t UDPSocket::RecvFrom( |
| 89 char* buffer, | 106 char* buffer, |
| 90 int32_t num_bytes, | 107 int32_t num_bytes, |
| 91 const CompletionCallbackWithOutput<NetAddress>& callback) { | 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()); |
| 113 } |
| 92 if (has_interface<PPB_UDPSocket_1_1>()) { | 114 if (has_interface<PPB_UDPSocket_1_1>()) { |
| 93 return get_interface<PPB_UDPSocket_1_1>()->RecvFrom( | 115 return get_interface<PPB_UDPSocket_1_1>()->RecvFrom( |
| 94 pp_resource(), buffer, num_bytes, callback.output(), | 116 pp_resource(), buffer, num_bytes, callback.output(), |
| 95 callback.pp_completion_callback()); | 117 callback.pp_completion_callback()); |
| 96 } | 118 } |
| 97 if (has_interface<PPB_UDPSocket_1_0>()) { | 119 if (has_interface<PPB_UDPSocket_1_0>()) { |
| 98 return get_interface<PPB_UDPSocket_1_0>()->RecvFrom( | 120 return get_interface<PPB_UDPSocket_1_0>()->RecvFrom( |
| 99 pp_resource(), buffer, num_bytes, callback.output(), | 121 pp_resource(), buffer, num_bytes, callback.output(), |
| 100 callback.pp_completion_callback()); | 122 callback.pp_completion_callback()); |
| 101 } | 123 } |
| 102 return callback.MayForce(PP_ERROR_NOINTERFACE); | 124 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 103 } | 125 } |
| 104 | 126 |
| 105 int32_t UDPSocket::SendTo(const char* buffer, | 127 int32_t UDPSocket::SendTo(const char* buffer, |
| 106 int32_t num_bytes, | 128 int32_t num_bytes, |
| 107 const NetAddress& addr, | 129 const NetAddress& addr, |
| 108 const CompletionCallback& callback) { | 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()); |
| 135 } |
| 109 if (has_interface<PPB_UDPSocket_1_1>()) { | 136 if (has_interface<PPB_UDPSocket_1_1>()) { |
| 110 return get_interface<PPB_UDPSocket_1_1>()->SendTo( | 137 return get_interface<PPB_UDPSocket_1_1>()->SendTo( |
| 111 pp_resource(), buffer, num_bytes, addr.pp_resource(), | 138 pp_resource(), buffer, num_bytes, addr.pp_resource(), |
| 112 callback.pp_completion_callback()); | 139 callback.pp_completion_callback()); |
| 113 } | 140 } |
| 114 if (has_interface<PPB_UDPSocket_1_0>()) { | 141 if (has_interface<PPB_UDPSocket_1_0>()) { |
| 115 return get_interface<PPB_UDPSocket_1_0>()->SendTo( | 142 return get_interface<PPB_UDPSocket_1_0>()->SendTo( |
| 116 pp_resource(), buffer, num_bytes, addr.pp_resource(), | 143 pp_resource(), buffer, num_bytes, addr.pp_resource(), |
| 117 callback.pp_completion_callback()); | 144 callback.pp_completion_callback()); |
| 118 } | 145 } |
| 119 return callback.MayForce(PP_ERROR_NOINTERFACE); | 146 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 120 } | 147 } |
| 121 | 148 |
| 122 void UDPSocket::Close() { | 149 void UDPSocket::Close() { |
| 150 if (has_interface<PPB_UDPSocket_1_2>()) |
| 151 return get_interface<PPB_UDPSocket_1_2>()->Close(pp_resource()); |
| 123 if (has_interface<PPB_UDPSocket_1_1>()) | 152 if (has_interface<PPB_UDPSocket_1_1>()) |
| 124 return get_interface<PPB_UDPSocket_1_1>()->Close(pp_resource()); | 153 return get_interface<PPB_UDPSocket_1_1>()->Close(pp_resource()); |
| 125 if (has_interface<PPB_UDPSocket_1_0>()) | 154 if (has_interface<PPB_UDPSocket_1_0>()) |
| 126 return get_interface<PPB_UDPSocket_1_0>()->Close(pp_resource()); | 155 return get_interface<PPB_UDPSocket_1_0>()->Close(pp_resource()); |
| 127 } | 156 } |
| 128 | 157 |
| 129 int32_t UDPSocket::SetOption(PP_UDPSocket_Option name, | 158 int32_t UDPSocket::SetOption(PP_UDPSocket_Option name, |
| 130 const Var& value, | 159 const Var& value, |
| 131 const CompletionCallback& callback) { | 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()); |
| 164 } |
| 132 if (has_interface<PPB_UDPSocket_1_1>()) { | 165 if (has_interface<PPB_UDPSocket_1_1>()) { |
| 133 return get_interface<PPB_UDPSocket_1_1>()->SetOption( | 166 return get_interface<PPB_UDPSocket_1_1>()->SetOption( |
| 134 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); | 167 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 135 } | 168 } |
| 136 if (has_interface<PPB_UDPSocket_1_0>()) { | 169 if (has_interface<PPB_UDPSocket_1_0>()) { |
| 137 return get_interface<PPB_UDPSocket_1_0>()->SetOption( | 170 return get_interface<PPB_UDPSocket_1_0>()->SetOption( |
| 138 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); | 171 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 139 } | 172 } |
| 140 return callback.MayForce(PP_ERROR_NOINTERFACE); | 173 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 141 } | 174 } |
| 142 | 175 |
| 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()); |
| 181 } |
| 182 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 183 } |
| 184 |
| 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()); |
| 190 } |
| 191 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 192 } |
| 193 |
| 143 } // namespace pp | 194 } // namespace pp |
| OLD | NEW |