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.
5 #include "ppapi/proxy/ppb_host_resolver_private_proxy.h"
10 #include "base/logging.h"
11 #include "ppapi/proxy/plugin_dispatcher.h"
12 #include "ppapi/proxy/plugin_globals.h"
13 #include "ppapi/proxy/ppapi_messages.h"
14 #include "ppapi/shared_impl/host_resource.h"
21 typedef std::map
<uint32
, PPB_HostResolver_Shared
*> IDToHostResolverMap
;
22 IDToHostResolverMap
* g_id_to_host_resolver
= NULL
;
24 class HostResolver
: public PPB_HostResolver_Shared
{
26 HostResolver(const HostResource
& resource
,
27 uint32 plugin_dispatcher_id
);
28 virtual ~HostResolver();
30 virtual void SendResolve(const HostPortPair
& host_port
,
31 const PP_HostResolver_Private_Hint
* hint
) OVERRIDE
;
34 void SendToBrowser(IPC::Message
* msg
);
36 const uint32 plugin_dispatcher_id_
;
38 DISALLOW_COPY_AND_ASSIGN(HostResolver
);
41 HostResolver::HostResolver(const HostResource
& resource
,
42 uint32 plugin_dispatcher_id
)
43 : PPB_HostResolver_Shared(resource
),
44 plugin_dispatcher_id_(plugin_dispatcher_id
) {
45 if (!g_id_to_host_resolver
)
46 g_id_to_host_resolver
= new IDToHostResolverMap();
47 DCHECK(g_id_to_host_resolver
->find(host_resolver_id_
) ==
48 g_id_to_host_resolver
->end());
49 (*g_id_to_host_resolver
)[host_resolver_id_
] = this;
52 HostResolver::~HostResolver() {
53 g_id_to_host_resolver
->erase(host_resolver_id_
);
56 void HostResolver::SendResolve(const HostPortPair
& host_port
,
57 const PP_HostResolver_Private_Hint
* hint
) {
58 SendToBrowser(new PpapiHostMsg_PPBHostResolver_Resolve(
59 API_ID_PPB_HOSTRESOLVER_PRIVATE
,
60 plugin_dispatcher_id_
,
66 void HostResolver::SendToBrowser(IPC::Message
* msg
) {
67 PluginGlobals::Get()->GetBrowserSender()->Send(msg
);
72 //------------------------------------------------------------------------------
74 PPB_HostResolver_Private_Proxy::PPB_HostResolver_Private_Proxy(
75 Dispatcher
* dispatcher
) : InterfaceProxy(dispatcher
) {
78 PPB_HostResolver_Private_Proxy::~PPB_HostResolver_Private_Proxy() {
82 PP_Resource
PPB_HostResolver_Private_Proxy::CreateProxyResource(
83 PP_Instance instance
) {
84 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
88 HostResolver
* host_resolver
=
89 new HostResolver(HostResource::MakeInstanceOnly(instance
),
90 dispatcher
->plugin_dispatcher_id());
91 return host_resolver
->GetReference();
94 bool PPB_HostResolver_Private_Proxy::OnMessageReceived(
95 const IPC::Message
& msg
) {
97 IPC_BEGIN_MESSAGE_MAP(PPB_HostResolver_Private_Proxy
, msg
)
98 IPC_MESSAGE_HANDLER(PpapiMsg_PPBHostResolver_ResolveACK
, OnMsgResolveACK
)
99 IPC_MESSAGE_UNHANDLED(handled
= false)
100 IPC_END_MESSAGE_MAP()
104 void PPB_HostResolver_Private_Proxy::OnMsgResolveACK(
105 uint32 plugin_dispatcher_id
,
106 uint32 host_resolver_id
,
108 const std::string
& canonical_name
,
109 const std::vector
<PP_NetAddress_Private
>& net_address_list
) {
110 if (!g_id_to_host_resolver
) {
114 IDToHostResolverMap::iterator it
=
115 g_id_to_host_resolver
->find(host_resolver_id
);
116 if (it
== g_id_to_host_resolver
->end())
118 it
->second
->OnResolveCompleted(succeeded
, canonical_name
, net_address_list
);