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/proxy/ppb_host_resolver_private_proxy.h" | |
6 | |
7 #include <cstddef> | |
8 #include <map> | |
9 | |
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" | |
15 | |
16 namespace ppapi { | |
17 namespace proxy { | |
18 | |
19 namespace { | |
20 | |
21 typedef std::map<uint32, PPB_HostResolver_Shared*> IDToHostResolverMap; | |
22 IDToHostResolverMap* g_id_to_host_resolver = NULL; | |
23 | |
24 class HostResolver : public PPB_HostResolver_Shared { | |
25 public: | |
26 HostResolver(const HostResource& resource, | |
27 uint32 plugin_dispatcher_id); | |
28 virtual ~HostResolver(); | |
29 | |
30 virtual void SendResolve(const HostPortPair& host_port, | |
31 const PP_HostResolver_Private_Hint* hint) OVERRIDE; | |
32 | |
33 private: | |
34 void SendToBrowser(IPC::Message* msg); | |
35 | |
36 const uint32 plugin_dispatcher_id_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(HostResolver); | |
39 }; | |
40 | |
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; | |
50 } | |
51 | |
52 HostResolver::~HostResolver() { | |
53 g_id_to_host_resolver->erase(host_resolver_id_); | |
54 } | |
55 | |
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_, | |
61 host_resolver_id_, | |
62 host_port, | |
63 *hint)); | |
64 } | |
65 | |
66 void HostResolver::SendToBrowser(IPC::Message* msg) { | |
67 PluginGlobals::Get()->GetBrowserSender()->Send(msg); | |
68 } | |
69 | |
70 } // namespace | |
71 | |
72 //------------------------------------------------------------------------------ | |
73 | |
74 PPB_HostResolver_Private_Proxy::PPB_HostResolver_Private_Proxy( | |
75 Dispatcher* dispatcher) : InterfaceProxy(dispatcher) { | |
76 } | |
77 | |
78 PPB_HostResolver_Private_Proxy::~PPB_HostResolver_Private_Proxy() { | |
79 } | |
80 | |
81 // static | |
82 PP_Resource PPB_HostResolver_Private_Proxy::CreateProxyResource( | |
83 PP_Instance instance) { | |
84 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
85 if (!dispatcher) | |
86 return 0; | |
87 | |
88 HostResolver* host_resolver = | |
89 new HostResolver(HostResource::MakeInstanceOnly(instance), | |
90 dispatcher->plugin_dispatcher_id()); | |
91 return host_resolver->GetReference(); | |
92 } | |
93 | |
94 bool PPB_HostResolver_Private_Proxy::OnMessageReceived( | |
95 const IPC::Message& msg) { | |
96 bool handled = true; | |
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() | |
101 return handled; | |
102 } | |
103 | |
104 void PPB_HostResolver_Private_Proxy::OnMsgResolveACK( | |
105 uint32 plugin_dispatcher_id, | |
106 uint32 host_resolver_id, | |
107 bool succeeded, | |
108 const std::string& canonical_name, | |
109 const std::vector<PP_NetAddress_Private>& net_address_list) { | |
110 if (!g_id_to_host_resolver) { | |
111 NOTREACHED(); | |
112 return; | |
113 } | |
114 IDToHostResolverMap::iterator it = | |
115 g_id_to_host_resolver->find(host_resolver_id); | |
116 if (it == g_id_to_host_resolver->end()) | |
117 return; | |
118 it->second->OnResolveCompleted(succeeded, canonical_name, net_address_list); | |
119 } | |
120 | |
121 } // namespace proxy | |
122 } // namespace ppapi | |
OLD | NEW |