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 NetworkList* CreateNetworkListFromAddrInfo(const addrinfo* ai) { | |
19 scoped_ptr<NetworkList> network_list(new NetworkList()); | |
20 PP_NetAddress_Private address; | |
21 | |
22 while (ai != NULL) { | |
23 if (!NetAddressPrivateImpl::SockaddrToNetAddress( | |
24 ai->ai_addr, | |
yzshen1
2012/03/06 08:07:46
nit: when calling a method/function, the style gui
ygorshenin1
2012/03/06 10:38:18
Done.
| |
25 ai->ai_addrlen, | |
26 &address)) { | |
27 return NULL; | |
28 } | |
29 network_list->push_back(address); | |
30 ai = ai->ai_next; | |
31 } | |
32 | |
33 return network_list.release(); | |
34 } | |
35 | |
36 PPB_HostResolver_Shared::PPB_HostResolver_Shared(PP_Instance instance) | |
37 : Resource(OBJECT_IS_IMPL, instance), | |
38 host_resolver_id_(GenerateHostResolverID()) { | |
39 } | |
40 | |
41 PPB_HostResolver_Shared::PPB_HostResolver_Shared( | |
42 const HostResource& resource) | |
43 : Resource(OBJECT_IS_PROXY, resource), | |
44 host_resolver_id_(GenerateHostResolverID()) { | |
45 } | |
46 | |
47 PPB_HostResolver_Shared::~PPB_HostResolver_Shared() { | |
48 } | |
49 | |
50 thunk::PPB_HostResolver_Private_API* | |
51 PPB_HostResolver_Shared::AsPPB_HostResolver_Private_API() { | |
52 return this; | |
53 } | |
54 | |
55 int32_t PPB_HostResolver_Shared::Resolve( | |
56 const char* host, | |
57 uint16_t port, | |
58 const PP_HostResolver_Private_Hint* hint, | |
59 PP_CompletionCallback callback) { | |
60 if (!host) | |
61 return PP_ERROR_BADARGUMENT; | |
62 if (!callback.func) | |
63 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
64 if (ResolveInProgress()) | |
65 return PP_ERROR_INPROGRESS; | |
66 | |
67 resolve_callback_ = new TrackedCallback(this, callback); | |
68 | |
69 HostPortPair host_port; | |
70 host_port.host = host; | |
71 host_port.port = port; | |
72 | |
73 SendResolve(host_port, hint); | |
74 return PP_OK_COMPLETIONPENDING; | |
75 } | |
76 | |
77 PP_Var PPB_HostResolver_Shared::GetCanonicalName() { | |
78 return StringVar::StringToPPVar(canonical_name_); | |
79 } | |
80 | |
81 uint32_t PPB_HostResolver_Shared::GetSize() { | |
82 if (ResolveInProgress()) | |
83 return 0; | |
84 return static_cast<uint32_t>(network_list_.size()); | |
85 } | |
86 | |
87 bool PPB_HostResolver_Shared::GetNetAddress(uint32 index, | |
88 PP_NetAddress_Private* address) { | |
89 if (ResolveInProgress() || index >= network_list_.size()) | |
90 return false; | |
91 *address = network_list_[index]; | |
92 return true; | |
93 } | |
94 | |
95 void PPB_HostResolver_Shared::OnResolveCompleted( | |
96 bool succeeded, | |
97 const std::string& canonical_name, | |
98 const NetworkList& network_list) { | |
99 if (succeeded) { | |
100 canonical_name_ = canonical_name; | |
101 network_list_ = network_list; | |
102 } else { | |
103 canonical_name_.clear(); | |
104 network_list_.clear(); | |
105 } | |
106 | |
107 TrackedCallback::ClearAndRun(&resolve_callback_, | |
108 succeeded ? PP_OK : PP_ERROR_FAILED); | |
109 } | |
110 | |
111 uint32 PPB_HostResolver_Shared::GenerateHostResolverID() { | |
112 static uint32 host_resolver_id = 0; | |
113 return host_resolver_id++; | |
114 } | |
115 | |
116 bool PPB_HostResolver_Shared::ResolveInProgress() const { | |
117 return TrackedCallback::IsPending(resolve_callback_); | |
118 } | |
119 | |
120 } // namespace ppapi | |
OLD | NEW |