OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/socket/udp_socket.h" | 5 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "chrome/browser/extensions/api/api_resource.h" | 9 #include "chrome/browser/extensions/api/api_resource.h" |
8 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
9 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
10 #include "net/udp/datagram_socket.h" | 12 #include "net/udp/datagram_socket.h" |
11 #include "net/udp/udp_client_socket.h" | 13 #include "net/udp/udp_client_socket.h" |
12 | 14 |
13 namespace extensions { | 15 namespace extensions { |
14 | 16 |
15 UDPSocket::UDPSocket(const std::string& owner_extension_id) | 17 UDPSocket::UDPSocket(const std::string& owner_extension_id) |
16 : Socket(owner_extension_id), | 18 : Socket(owner_extension_id), |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 recv_from_callback_.Run(result, io_buffer, ip, port); | 217 recv_from_callback_.Run(result, io_buffer, ip, port); |
216 recv_from_callback_.Reset(); | 218 recv_from_callback_.Reset(); |
217 } | 219 } |
218 | 220 |
219 void UDPSocket::OnSendToComplete(int result) { | 221 void UDPSocket::OnSendToComplete(int result) { |
220 DCHECK(!send_to_callback_.is_null()); | 222 DCHECK(!send_to_callback_.is_null()); |
221 send_to_callback_.Run(result); | 223 send_to_callback_.Run(result); |
222 send_to_callback_.Reset(); | 224 send_to_callback_.Reset(); |
223 } | 225 } |
224 | 226 |
| 227 int UDPSocket::JoinGroup(const std::string& address) { |
| 228 net::IPAddressNumber ip; |
| 229 if (!net::ParseIPLiteralToNumber(address, &ip)) |
| 230 return net::ERR_ADDRESS_INVALID; |
| 231 |
| 232 std::string normalized_address = net::IPAddressToString(ip); |
| 233 std::vector<std::string>::iterator find_result = |
| 234 std::find(multicast_groups_.begin(), |
| 235 multicast_groups_.end(), |
| 236 normalized_address); |
| 237 if (find_result != multicast_groups_.end()) |
| 238 return net::ERR_ADDRESS_INVALID; |
| 239 |
| 240 int rv = socket_.JoinGroup(ip); |
| 241 if (rv == 0) |
| 242 multicast_groups_.push_back(normalized_address); |
| 243 return rv; |
| 244 } |
| 245 |
| 246 int UDPSocket::LeaveGroup(const std::string& address) { |
| 247 net::IPAddressNumber ip; |
| 248 if (!net::ParseIPLiteralToNumber(address, &ip)) |
| 249 return net::ERR_ADDRESS_INVALID; |
| 250 |
| 251 std::string normalized_address = net::IPAddressToString(ip); |
| 252 std::vector<std::string>::iterator find_result = |
| 253 std::find(multicast_groups_.begin(), |
| 254 multicast_groups_.end(), |
| 255 normalized_address); |
| 256 if (find_result == multicast_groups_.end()) |
| 257 return net::ERR_ADDRESS_INVALID; |
| 258 |
| 259 int rv = socket_.LeaveGroup(ip); |
| 260 if (rv == 0) |
| 261 multicast_groups_.erase(find_result); |
| 262 return rv; |
| 263 } |
| 264 |
| 265 int UDPSocket::SetMulticastTimeToLive(int ttl) { |
| 266 return socket_.SetMulticastTimeToLive(ttl); |
| 267 } |
| 268 |
| 269 int UDPSocket::SetMulticastLoopbackMode(bool loopback) { |
| 270 return socket_.SetMulticastLoopbackMode(loopback); |
| 271 } |
| 272 |
| 273 const std::vector<std::string>& UDPSocket::GetJoinedGroups() const { |
| 274 return multicast_groups_; |
| 275 } |
| 276 |
225 } // namespace extensions | 277 } // namespace extensions |
OLD | NEW |