| 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 "content/renderer/p2p/ipc_socket_factory.h" | 5 #include "content/renderer/p2p/ipc_socket_factory.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <list> | 10 #include <list> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/sequence_checker.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 19 #include "base/threading/non_thread_safe.h" | |
| 20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 22 #include "content/renderer/media/webrtc_logging.h" | 22 #include "content/renderer/media/webrtc_logging.h" |
| 23 #include "content/renderer/p2p/host_address_request.h" | 23 #include "content/renderer/p2p/host_address_request.h" |
| 24 #include "content/renderer/p2p/socket_client_delegate.h" | 24 #include "content/renderer/p2p/socket_client_delegate.h" |
| 25 #include "content/renderer/p2p/socket_client_impl.h" | 25 #include "content/renderer/p2p/socket_client_impl.h" |
| 26 #include "content/renderer/p2p/socket_dispatcher.h" | 26 #include "content/renderer/p2p/socket_dispatcher.h" |
| 27 #include "jingle/glue/utils.h" | 27 #include "jingle/glue/utils.h" |
| 28 #include "net/base/ip_address.h" | 28 #include "net/base/ip_address.h" |
| 29 #include "third_party/webrtc/base/asyncpacketsocket.h" | 29 #include "third_party/webrtc/base/asyncpacketsocket.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 size_t total_packets_; | 207 size_t total_packets_; |
| 208 | 208 |
| 209 DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket); | 209 DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket); |
| 210 }; | 210 }; |
| 211 | 211 |
| 212 // Simple wrapper around P2PAsyncAddressResolver. The main purpose of this | 212 // Simple wrapper around P2PAsyncAddressResolver. The main purpose of this |
| 213 // class is to send SignalDone, after OnDone callback from | 213 // class is to send SignalDone, after OnDone callback from |
| 214 // P2PAsyncAddressResolver. Libjingle sig slots are not thread safe. In case | 214 // P2PAsyncAddressResolver. Libjingle sig slots are not thread safe. In case |
| 215 // of MT sig slots clients must call disconnect. This class is to make sure | 215 // of MT sig slots clients must call disconnect. This class is to make sure |
| 216 // we destruct from the same thread on which is created. | 216 // we destruct from the same thread on which is created. |
| 217 class AsyncAddressResolverImpl : public base::NonThreadSafe, | 217 class AsyncAddressResolverImpl : public rtc::AsyncResolverInterface { |
| 218 public rtc::AsyncResolverInterface { | |
| 219 public: | 218 public: |
| 220 AsyncAddressResolverImpl(P2PSocketDispatcher* dispatcher); | 219 AsyncAddressResolverImpl(P2PSocketDispatcher* dispatcher); |
| 221 ~AsyncAddressResolverImpl() override; | 220 ~AsyncAddressResolverImpl() override; |
| 222 | 221 |
| 223 // rtc::AsyncResolverInterface interface. | 222 // rtc::AsyncResolverInterface interface. |
| 224 void Start(const rtc::SocketAddress& addr) override; | 223 void Start(const rtc::SocketAddress& addr) override; |
| 225 bool GetResolvedAddress(int family, rtc::SocketAddress* addr) const override; | 224 bool GetResolvedAddress(int family, rtc::SocketAddress* addr) const override; |
| 226 int GetError() const override; | 225 int GetError() const override; |
| 227 void Destroy(bool wait) override; | 226 void Destroy(bool wait) override; |
| 228 | 227 |
| 229 private: | 228 private: |
| 230 virtual void OnAddressResolved(const net::IPAddressList& addresses); | 229 virtual void OnAddressResolved(const net::IPAddressList& addresses); |
| 231 | 230 |
| 232 scoped_refptr<P2PAsyncAddressResolver> resolver_; | 231 scoped_refptr<P2PAsyncAddressResolver> resolver_; |
| 232 |
| 233 SEQUENCE_CHECKER(sequence_checker_); |
| 234 |
| 233 int port_; // Port number in |addr| from Start() method. | 235 int port_; // Port number in |addr| from Start() method. |
| 234 std::vector<rtc::IPAddress> addresses_; // Resolved addresses. | 236 std::vector<rtc::IPAddress> addresses_; // Resolved addresses. |
| 235 }; | 237 }; |
| 236 | 238 |
| 237 IpcPacketSocket::IpcPacketSocket() | 239 IpcPacketSocket::IpcPacketSocket() |
| 238 : type_(P2P_SOCKET_UDP), | 240 : type_(P2P_SOCKET_UDP), |
| 239 state_(IS_UNINITIALIZED), | 241 state_(IS_UNINITIALIZED), |
| 240 send_bytes_available_(kMaximumInFlightBytes), | 242 send_bytes_available_(kMaximumInFlightBytes), |
| 241 writable_signal_expected_(false), | 243 writable_signal_expected_(false), |
| 242 error_(0), | 244 error_(0), |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 SignalReadPacket(this, &data[0], data.size(), address_lj, | 672 SignalReadPacket(this, &data[0], data.size(), address_lj, |
| 671 packet_time); | 673 packet_time); |
| 672 } | 674 } |
| 673 | 675 |
| 674 AsyncAddressResolverImpl::AsyncAddressResolverImpl( | 676 AsyncAddressResolverImpl::AsyncAddressResolverImpl( |
| 675 P2PSocketDispatcher* dispatcher) | 677 P2PSocketDispatcher* dispatcher) |
| 676 : resolver_(new P2PAsyncAddressResolver(dispatcher)) { | 678 : resolver_(new P2PAsyncAddressResolver(dispatcher)) { |
| 677 } | 679 } |
| 678 | 680 |
| 679 AsyncAddressResolverImpl::~AsyncAddressResolverImpl() { | 681 AsyncAddressResolverImpl::~AsyncAddressResolverImpl() { |
| 682 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 680 } | 683 } |
| 681 | 684 |
| 682 void AsyncAddressResolverImpl::Start(const rtc::SocketAddress& addr) { | 685 void AsyncAddressResolverImpl::Start(const rtc::SocketAddress& addr) { |
| 683 DCHECK(CalledOnValidThread()); | 686 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 684 // Copy port number from |addr|. |port_| must be copied | 687 // Copy port number from |addr|. |port_| must be copied |
| 685 // when resolved address is returned in GetResolvedAddress. | 688 // when resolved address is returned in GetResolvedAddress. |
| 686 port_ = addr.port(); | 689 port_ = addr.port(); |
| 687 | 690 |
| 688 resolver_->Start(addr, base::Bind( | 691 resolver_->Start(addr, base::Bind( |
| 689 &AsyncAddressResolverImpl::OnAddressResolved, | 692 &AsyncAddressResolverImpl::OnAddressResolved, |
| 690 base::Unretained(this))); | 693 base::Unretained(this))); |
| 691 } | 694 } |
| 692 | 695 |
| 693 bool AsyncAddressResolverImpl::GetResolvedAddress( | 696 bool AsyncAddressResolverImpl::GetResolvedAddress( |
| 694 int family, rtc::SocketAddress* addr) const { | 697 int family, rtc::SocketAddress* addr) const { |
| 695 DCHECK(CalledOnValidThread()); | 698 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 696 | 699 |
| 697 if (addresses_.empty()) | 700 if (addresses_.empty()) |
| 698 return false; | 701 return false; |
| 699 | 702 |
| 700 for (size_t i = 0; i < addresses_.size(); ++i) { | 703 for (size_t i = 0; i < addresses_.size(); ++i) { |
| 701 if (family == addresses_[i].family()) { | 704 if (family == addresses_[i].family()) { |
| 702 addr->SetResolvedIP(addresses_[i]); | 705 addr->SetResolvedIP(addresses_[i]); |
| 703 addr->SetPort(port_); | 706 addr->SetPort(port_); |
| 704 return true; | 707 return true; |
| 705 } | 708 } |
| 706 } | 709 } |
| 707 return false; | 710 return false; |
| 708 } | 711 } |
| 709 | 712 |
| 710 int AsyncAddressResolverImpl::GetError() const { | 713 int AsyncAddressResolverImpl::GetError() const { |
| 711 DCHECK(CalledOnValidThread()); | 714 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 712 return addresses_.empty() ? -1 : 0; | 715 return addresses_.empty() ? -1 : 0; |
| 713 } | 716 } |
| 714 | 717 |
| 715 void AsyncAddressResolverImpl::Destroy(bool wait) { | 718 void AsyncAddressResolverImpl::Destroy(bool wait) { |
| 716 DCHECK(CalledOnValidThread()); | 719 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 717 resolver_->Cancel(); | 720 resolver_->Cancel(); |
| 718 // Libjingle doesn't need this object any more and it's not going to delete | 721 // Libjingle doesn't need this object any more and it's not going to delete |
| 719 // it explicitly. | 722 // it explicitly. |
| 720 delete this; | 723 delete this; |
| 721 } | 724 } |
| 722 | 725 |
| 723 void AsyncAddressResolverImpl::OnAddressResolved( | 726 void AsyncAddressResolverImpl::OnAddressResolved( |
| 724 const net::IPAddressList& addresses) { | 727 const net::IPAddressList& addresses) { |
| 725 DCHECK(CalledOnValidThread()); | 728 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 726 for (size_t i = 0; i < addresses.size(); ++i) { | 729 for (size_t i = 0; i < addresses.size(); ++i) { |
| 727 rtc::SocketAddress socket_address; | 730 rtc::SocketAddress socket_address; |
| 728 if (!jingle_glue::IPEndPointToSocketAddress( | 731 if (!jingle_glue::IPEndPointToSocketAddress( |
| 729 net::IPEndPoint(addresses[i], 0), &socket_address)) { | 732 net::IPEndPoint(addresses[i], 0), &socket_address)) { |
| 730 NOTREACHED(); | 733 NOTREACHED(); |
| 731 } | 734 } |
| 732 addresses_.push_back(socket_address.ipaddr()); | 735 addresses_.push_back(socket_address.ipaddr()); |
| 733 } | 736 } |
| 734 SignalDone(this); | 737 SignalDone(this); |
| 735 } | 738 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 } | 807 } |
| 805 | 808 |
| 806 rtc::AsyncResolverInterface* | 809 rtc::AsyncResolverInterface* |
| 807 IpcPacketSocketFactory::CreateAsyncResolver() { | 810 IpcPacketSocketFactory::CreateAsyncResolver() { |
| 808 std::unique_ptr<AsyncAddressResolverImpl> resolver( | 811 std::unique_ptr<AsyncAddressResolverImpl> resolver( |
| 809 new AsyncAddressResolverImpl(socket_dispatcher_)); | 812 new AsyncAddressResolverImpl(socket_dispatcher_)); |
| 810 return resolver.release(); | 813 return resolver.release(); |
| 811 } | 814 } |
| 812 | 815 |
| 813 } // namespace content | 816 } // namespace content |
| OLD | NEW |