OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/shared_impl/private/ppb_host_resolver_shared.h" |
| 6 |
| 7 #include <cstddef> |
| 8 #include <cstring> |
| 9 |
| 10 #include "net/base/sys_addrinfo.h" |
| 11 #include "ppapi/c/pp_errors.h" |
| 12 #include "ppapi/shared_impl/private/net_address_private_impl.h" |
| 13 #include "ppapi/shared_impl/var.h" |
| 14 #include "ppapi/thunk/thunk.h" |
| 15 |
| 16 namespace ppapi { |
| 17 |
| 18 NetAddressList* CreateNetAddressListFromAddrInfo(const addrinfo* ai) { |
| 19 scoped_ptr<NetAddressList> net_address_list(new NetAddressList()); |
| 20 PP_NetAddress_Private address; |
| 21 |
| 22 while (ai != NULL) { |
| 23 if (!NetAddressPrivateImpl::SockaddrToNetAddress( |
| 24 ai->ai_addr, ai->ai_addrlen, &address)) { |
| 25 return NULL; |
| 26 } |
| 27 net_address_list->push_back(address); |
| 28 ai = ai->ai_next; |
| 29 } |
| 30 |
| 31 return net_address_list.release(); |
| 32 } |
| 33 |
| 34 PPB_HostResolver_Shared::PPB_HostResolver_Shared(PP_Instance instance) |
| 35 : Resource(OBJECT_IS_IMPL, instance), |
| 36 host_resolver_id_(GenerateHostResolverID()) { |
| 37 } |
| 38 |
| 39 PPB_HostResolver_Shared::PPB_HostResolver_Shared( |
| 40 const HostResource& resource) |
| 41 : Resource(OBJECT_IS_PROXY, resource), |
| 42 host_resolver_id_(GenerateHostResolverID()) { |
| 43 } |
| 44 |
| 45 PPB_HostResolver_Shared::~PPB_HostResolver_Shared() { |
| 46 } |
| 47 |
| 48 thunk::PPB_HostResolver_Private_API* |
| 49 PPB_HostResolver_Shared::AsPPB_HostResolver_Private_API() { |
| 50 return this; |
| 51 } |
| 52 |
| 53 int32_t PPB_HostResolver_Shared::Resolve( |
| 54 const char* host, |
| 55 uint16_t port, |
| 56 const PP_HostResolver_Private_Hint* hint, |
| 57 PP_CompletionCallback callback) { |
| 58 if (!host) |
| 59 return PP_ERROR_BADARGUMENT; |
| 60 if (!callback.func) |
| 61 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 62 if (ResolveInProgress()) |
| 63 return PP_ERROR_INPROGRESS; |
| 64 |
| 65 resolve_callback_ = new TrackedCallback(this, callback); |
| 66 |
| 67 HostPortPair host_port; |
| 68 host_port.host = host; |
| 69 host_port.port = port; |
| 70 |
| 71 SendResolve(host_port, hint); |
| 72 return PP_OK_COMPLETIONPENDING; |
| 73 } |
| 74 |
| 75 PP_Var PPB_HostResolver_Shared::GetCanonicalName() { |
| 76 return StringVar::StringToPPVar(canonical_name_); |
| 77 } |
| 78 |
| 79 uint32_t PPB_HostResolver_Shared::GetSize() { |
| 80 if (ResolveInProgress()) |
| 81 return 0; |
| 82 return static_cast<uint32_t>(net_address_list_.size()); |
| 83 } |
| 84 |
| 85 bool PPB_HostResolver_Shared::GetNetAddress(uint32 index, |
| 86 PP_NetAddress_Private* address) { |
| 87 if (ResolveInProgress() || index >= GetSize()) |
| 88 return false; |
| 89 *address = net_address_list_[index]; |
| 90 return true; |
| 91 } |
| 92 |
| 93 void PPB_HostResolver_Shared::OnResolveCompleted( |
| 94 bool succeeded, |
| 95 const std::string& canonical_name, |
| 96 const NetAddressList& net_address_list) { |
| 97 if (succeeded) { |
| 98 canonical_name_ = canonical_name; |
| 99 net_address_list_ = net_address_list; |
| 100 } else { |
| 101 canonical_name_.clear(); |
| 102 net_address_list_.clear(); |
| 103 } |
| 104 |
| 105 TrackedCallback::ClearAndRun(&resolve_callback_, |
| 106 succeeded ? PP_OK : PP_ERROR_FAILED); |
| 107 } |
| 108 |
| 109 uint32 PPB_HostResolver_Shared::GenerateHostResolverID() { |
| 110 static uint32 host_resolver_id = 0; |
| 111 return host_resolver_id++; |
| 112 } |
| 113 |
| 114 bool PPB_HostResolver_Shared::ResolveInProgress() const { |
| 115 return TrackedCallback::IsPending(resolve_callback_); |
| 116 } |
| 117 |
| 118 } // namespace ppapi |
OLD | NEW |