| 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 "base/memory/scoped_ptr.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 PPB_HostResolver_Shared::PPB_HostResolver_Shared(PP_Instance instance) | |
| 19 : Resource(OBJECT_IS_IMPL, instance), | |
| 20 host_resolver_id_(GenerateHostResolverID()) { | |
| 21 } | |
| 22 | |
| 23 PPB_HostResolver_Shared::PPB_HostResolver_Shared( | |
| 24 const HostResource& resource) | |
| 25 : Resource(OBJECT_IS_PROXY, resource), | |
| 26 host_resolver_id_(GenerateHostResolverID()) { | |
| 27 } | |
| 28 | |
| 29 PPB_HostResolver_Shared::~PPB_HostResolver_Shared() { | |
| 30 } | |
| 31 | |
| 32 thunk::PPB_HostResolver_Private_API* | |
| 33 PPB_HostResolver_Shared::AsPPB_HostResolver_Private_API() { | |
| 34 return this; | |
| 35 } | |
| 36 | |
| 37 int32_t PPB_HostResolver_Shared::Resolve( | |
| 38 const char* host, | |
| 39 uint16_t port, | |
| 40 const PP_HostResolver_Private_Hint* hint, | |
| 41 scoped_refptr<TrackedCallback> callback) { | |
| 42 if (!host) | |
| 43 return PP_ERROR_BADARGUMENT; | |
| 44 if (ResolveInProgress()) | |
| 45 return PP_ERROR_INPROGRESS; | |
| 46 | |
| 47 resolve_callback_ = callback; | |
| 48 | |
| 49 HostPortPair host_port; | |
| 50 host_port.host = host; | |
| 51 host_port.port = port; | |
| 52 | |
| 53 SendResolve(host_port, hint); | |
| 54 return PP_OK_COMPLETIONPENDING; | |
| 55 } | |
| 56 | |
| 57 PP_Var PPB_HostResolver_Shared::GetCanonicalName() { | |
| 58 return StringVar::StringToPPVar(canonical_name_); | |
| 59 } | |
| 60 | |
| 61 uint32_t PPB_HostResolver_Shared::GetSize() { | |
| 62 if (ResolveInProgress()) | |
| 63 return 0; | |
| 64 return static_cast<uint32_t>(net_address_list_.size()); | |
| 65 } | |
| 66 | |
| 67 bool PPB_HostResolver_Shared::GetNetAddress(uint32 index, | |
| 68 PP_NetAddress_Private* address) { | |
| 69 if (ResolveInProgress() || index >= GetSize()) | |
| 70 return false; | |
| 71 *address = net_address_list_[index]; | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 void PPB_HostResolver_Shared::OnResolveCompleted( | |
| 76 bool succeeded, | |
| 77 const std::string& canonical_name, | |
| 78 const std::vector<PP_NetAddress_Private>& net_address_list) { | |
| 79 if (succeeded) { | |
| 80 canonical_name_ = canonical_name; | |
| 81 net_address_list_ = net_address_list; | |
| 82 } else { | |
| 83 canonical_name_.clear(); | |
| 84 net_address_list_.clear(); | |
| 85 } | |
| 86 | |
| 87 resolve_callback_->Run(succeeded ? PP_OK : PP_ERROR_FAILED); | |
| 88 } | |
| 89 | |
| 90 uint32 PPB_HostResolver_Shared::GenerateHostResolverID() { | |
| 91 static uint32 host_resolver_id = 0; | |
| 92 return host_resolver_id++; | |
| 93 } | |
| 94 | |
| 95 bool PPB_HostResolver_Shared::ResolveInProgress() const { | |
| 96 return TrackedCallback::IsPending(resolve_callback_); | |
| 97 } | |
| 98 | |
| 99 } // namespace ppapi | |
| OLD | NEW |