Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / client / plugin / pepper_address_resolver.cc
blobb39e4d7532ae4245ce633ced788e8c4b8947ecd8
1 // Copyright 2014 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 "remoting/client/plugin/pepper_address_resolver.h"
7 #include "base/logging.h"
8 #include "ppapi/cpp/net_address.h"
9 #include "remoting/client/plugin/pepper_util.h"
11 namespace remoting {
13 PepperAddressResolver::PepperAddressResolver(const pp::InstanceHandle& instance)
14 : resolver_(instance),
15 error_(0),
16 callback_factory_(this) {
19 PepperAddressResolver::~PepperAddressResolver() {
22 void PepperAddressResolver::Start(const rtc::SocketAddress& address) {
23 DCHECK(!address.hostname().empty());
25 PP_HostResolver_Hint hint;
26 hint.flags = 0;
27 hint.family = PP_NETADDRESS_FAMILY_UNSPECIFIED;
28 pp::CompletionCallback callback =
29 callback_factory_.NewCallback(&PepperAddressResolver::OnResolved);
30 int result = resolver_.Resolve(
31 address.hostname().c_str(), address.port(), hint, callback);
32 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING);
35 bool PepperAddressResolver::GetResolvedAddress(int family,
36 rtc::SocketAddress* addr) const {
37 rtc::SocketAddress result;
38 switch (family) {
39 case AF_INET:
40 result = ipv4_address_;
41 break;
42 case AF_INET6:
43 result = ipv6_address_;
44 break;
47 if (result.IsNil())
48 return false;
50 *addr = result;
51 return true;
54 int PepperAddressResolver::GetError() const {
55 return error_;
58 void PepperAddressResolver::Destroy(bool wait) {
59 delete this;
62 void PepperAddressResolver::OnResolved(int32_t result) {
63 if (result < 0) {
64 error_ = EAI_FAIL;
65 SignalDone(this);
66 return;
69 int count = resolver_.GetNetAddressCount();
70 for (int i = 0; i < count; ++i) {
71 pp::NetAddress address = resolver_.GetNetAddress(i);
72 if (address.GetFamily() == PP_NETADDRESS_FAMILY_IPV4 &&
73 ipv4_address_.IsNil()) {
74 PpNetAddressToSocketAddress(address, &ipv4_address_);
75 } else if (address.GetFamily() == PP_NETADDRESS_FAMILY_IPV6 &&
76 ipv6_address_.IsNil()) {
77 PpNetAddressToSocketAddress(address, &ipv6_address_);
81 SignalDone(this);
84 } // namespace remoting