| Index: ppapi/shared_impl/private/ppb_host_resolver_shared.cc
|
| diff --git a/ppapi/shared_impl/private/ppb_host_resolver_shared.cc b/ppapi/shared_impl/private/ppb_host_resolver_shared.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a88c96ed390ee41b3540676754d62f876720b47d
|
| --- /dev/null
|
| +++ b/ppapi/shared_impl/private/ppb_host_resolver_shared.cc
|
| @@ -0,0 +1,121 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ppapi/shared_impl/private/ppb_host_resolver_shared.h"
|
| +
|
| +#include <cstddef>
|
| +#include <cstring>
|
| +
|
| +#include "net/base/sys_addrinfo.h"
|
| +#include "ppapi/c/pp_errors.h"
|
| +#include "ppapi/shared_impl/private/net_address_private_impl.h"
|
| +#include "ppapi/shared_impl/var.h"
|
| +#include "ppapi/thunk/thunk.h"
|
| +
|
| +namespace ppapi {
|
| +
|
| +NetworkList* CreateNetworkListFromAddrInfo(const addrinfo* ai) {
|
| + scoped_ptr<NetworkList> network_list(new NetworkList());
|
| +
|
| + while (ai != NULL) {
|
| + network_list->push_back(NameAndAddressPair());
|
| + if (ai->ai_canonname)
|
| + network_list->back().canonical_name = ai->ai_canonname;
|
| + if (!NetAddressPrivateImpl::SockaddrToNetAddress(
|
| + ai->ai_addr,
|
| + ai->ai_addrlen,
|
| + &network_list->back().address)) {
|
| + return NULL;
|
| + }
|
| + ai = ai->ai_next;
|
| + }
|
| +
|
| + return network_list.release();
|
| +}
|
| +
|
| +PPB_HostResolver_Shared::PPB_HostResolver_Shared(PP_Instance instance)
|
| + : Resource(OBJECT_IS_IMPL, instance),
|
| + host_resolver_id_(GenerateHostResolverID()) {
|
| +}
|
| +
|
| +PPB_HostResolver_Shared::PPB_HostResolver_Shared(
|
| + const HostResource& resource)
|
| + : Resource(OBJECT_IS_PROXY, resource),
|
| + host_resolver_id_(GenerateHostResolverID()) {
|
| +}
|
| +
|
| +PPB_HostResolver_Shared::~PPB_HostResolver_Shared() {
|
| +}
|
| +
|
| +thunk::PPB_HostResolver_Private_API*
|
| +PPB_HostResolver_Shared::AsPPB_HostResolver_Private_API() {
|
| + return this;
|
| +}
|
| +
|
| +int32_t PPB_HostResolver_Shared::Resolve(
|
| + const char* host,
|
| + uint16_t port,
|
| + const PP_HostResolver_Private_Hint* hint,
|
| + PP_CompletionCallback callback) {
|
| + if (!host)
|
| + return PP_ERROR_BADARGUMENT;
|
| + if (!callback.func)
|
| + return PP_ERROR_BLOCKS_MAIN_THREAD;
|
| + if (ResolveInProgress())
|
| + return PP_ERROR_INPROGRESS;
|
| +
|
| + resolve_callback_ = new TrackedCallback(this, callback);
|
| +
|
| + HostPortPair host_port;
|
| + host_port.host = host;
|
| + host_port.port = port;
|
| +
|
| + SendResolve(host_port, hint);
|
| + return PP_OK_COMPLETIONPENDING;
|
| +}
|
| +
|
| +uint32_t PPB_HostResolver_Shared::GetSize() {
|
| + if (ResolveInProgress())
|
| + return 0;
|
| + return static_cast<uint32_t>(network_list_.size());
|
| +}
|
| +
|
| +bool PPB_HostResolver_Shared::GetItem(uint32 index,
|
| + PP_Var* canonical_name,
|
| + PP_NetAddress_Private* address) {
|
| + if (ResolveInProgress() || index >= network_list_.size())
|
| + return false;
|
| +
|
| + if (canonical_name) {
|
| + *canonical_name =
|
| + StringVar::StringToPPVar(network_list_[index].canonical_name);
|
| + }
|
| + if (address)
|
| + *address = network_list_[index].address;
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void PPB_HostResolver_Shared::OnResolveCompleted(
|
| + bool succeeded,
|
| + const NetworkList& network_list) {
|
| + if (succeeded)
|
| + network_list_ = network_list;
|
| + else
|
| + network_list_.clear();
|
| +
|
| + TrackedCallback::ClearAndRun(&resolve_callback_,
|
| + succeeded ? PP_OK : PP_ERROR_FAILED);
|
| +}
|
| +
|
| +uint32 PPB_HostResolver_Shared::GenerateHostResolverID() {
|
| + static uint32 host_resolver_id = 0;
|
| + return host_resolver_id++;
|
| +}
|
| +
|
| +bool PPB_HostResolver_Shared::ResolveInProgress() const {
|
| + return TrackedCallback::IsPending(resolve_callback_);
|
| +}
|
| +
|
| +} // namespace ppapi
|
|
|