1 // Copyright 2013 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 "ppapi/proxy/host_resolver_resource_base.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/proxy/net_address_resource.h"
9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/shared_impl/tracked_callback.h"
11 #include "ppapi/shared_impl/var.h"
16 HostResolverResourceBase::HostResolverResourceBase(Connection connection
,
18 : PluginResource(connection
, instance
),
19 allow_get_results_(false) {
20 SendCreate(BROWSER
, PpapiHostMsg_HostResolverPrivate_Create());
23 HostResolverResourceBase::~HostResolverResourceBase() {
26 int32_t HostResolverResourceBase::ResolveImpl(
29 const PP_HostResolver_Private_Hint
* hint
,
30 scoped_refptr
<TrackedCallback
> callback
) {
31 allow_get_results_
= false;
33 return PP_ERROR_BADARGUMENT
;
34 if (ResolveInProgress())
35 return PP_ERROR_INPROGRESS
;
37 resolve_callback_
= callback
;
39 HostPortPair host_port
;
40 host_port
.host
= host
;
41 host_port
.port
= port
;
43 SendResolve(host_port
, hint
);
44 return PP_OK_COMPLETIONPENDING
;
47 PP_Var
HostResolverResourceBase::GetCanonicalNameImpl() {
48 if (!allow_get_results_
)
49 return PP_MakeUndefined();
51 return StringVar::StringToPPVar(canonical_name_
);
54 uint32_t HostResolverResourceBase::GetSizeImpl() {
55 if (!allow_get_results_
)
57 return static_cast<uint32_t>(net_address_list_
.size());
60 scoped_refptr
<NetAddressResource
> HostResolverResourceBase::GetNetAddressImpl(
62 if (!allow_get_results_
|| index
>= GetSizeImpl())
63 return scoped_refptr
<NetAddressResource
>();
65 return net_address_list_
[index
];
68 void HostResolverResourceBase::OnPluginMsgResolveReply(
69 const ResourceMessageReplyParams
& params
,
70 const std::string
& canonical_name
,
71 const std::vector
<PP_NetAddress_Private
>& net_address_list
) {
72 if (params
.result() == PP_OK
) {
73 allow_get_results_
= true;
74 canonical_name_
= canonical_name
;
76 net_address_list_
.clear();
77 for (std::vector
<PP_NetAddress_Private
>::const_iterator iter
=
78 net_address_list
.begin();
79 iter
!= net_address_list
.end();
81 net_address_list_
.push_back(
82 new NetAddressResource(connection(), pp_instance(), *iter
));
85 canonical_name_
.clear();
86 net_address_list_
.clear();
88 resolve_callback_
->Run(params
.result());
91 void HostResolverResourceBase::SendResolve(
92 const HostPortPair
& host_port
,
93 const PP_HostResolver_Private_Hint
* hint
) {
94 PpapiHostMsg_HostResolverPrivate_Resolve
msg(host_port
, *hint
);
95 Call
<PpapiPluginMsg_HostResolverPrivate_ResolveReply
>(
98 base::Bind(&HostResolverResourceBase::OnPluginMsgResolveReply
,
99 base::Unretained(this)));
102 bool HostResolverResourceBase::ResolveInProgress() const {
103 return TrackedCallback::IsPending(resolve_callback_
);