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