summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_host_resolver_private_proxy.cc
blob: da73c09ea04b28c63266434c1515e31efe15cd36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/ppb_host_resolver_private_proxy.h"

#include <cstddef>
#include <map>

#include "base/logging.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/plugin_proxy_delegate.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/host_resource.h"

namespace ppapi {
namespace proxy {

namespace {

typedef std::map<uint32, PPB_HostResolver_Shared*> IDToHostResolverMap;
IDToHostResolverMap* g_id_to_host_resolver = NULL;

class HostResolver : public PPB_HostResolver_Shared {
 public:
  HostResolver(const HostResource& resource,
               uint32 plugin_dispatcher_id);
  virtual ~HostResolver();

  virtual void SendResolve(const HostPortPair& host_port,
                           const PP_HostResolver_Private_Hint* hint) OVERRIDE;

 private:
  void SendToBrowser(IPC::Message* msg);

  const uint32 plugin_dispatcher_id_;

  DISALLOW_COPY_AND_ASSIGN(HostResolver);
};

HostResolver::HostResolver(const HostResource& resource,
                           uint32 plugin_dispatcher_id)
    : PPB_HostResolver_Shared(resource),
      plugin_dispatcher_id_(plugin_dispatcher_id) {
  if (!g_id_to_host_resolver)
    g_id_to_host_resolver = new IDToHostResolverMap();
  DCHECK(g_id_to_host_resolver->find(host_resolver_id_) ==
         g_id_to_host_resolver->end());
  (*g_id_to_host_resolver)[host_resolver_id_] = this;
}

HostResolver::~HostResolver() {
  g_id_to_host_resolver->erase(host_resolver_id_);
}

void HostResolver::SendResolve(const HostPortPair& host_port,
                               const PP_HostResolver_Private_Hint* hint) {
  SendToBrowser(new PpapiHostMsg_PPBHostResolver_Resolve(
      API_ID_PPB_HOSTRESOLVER_PRIVATE,
      plugin_dispatcher_id_,
      host_resolver_id_,
      host_port,
      *hint));
}

void HostResolver::SendToBrowser(IPC::Message* msg) {
  PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser(msg);
}

}  // namespace

//------------------------------------------------------------------------------

PPB_HostResolver_Private_Proxy::PPB_HostResolver_Private_Proxy(
    Dispatcher* dispatcher) : InterfaceProxy(dispatcher) {
}

PPB_HostResolver_Private_Proxy::~PPB_HostResolver_Private_Proxy() {
}

// static
PP_Resource PPB_HostResolver_Private_Proxy::CreateProxyResource(
    PP_Instance instance) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return 0;

  HostResolver* host_resolver =
      new HostResolver(HostResource::MakeInstanceOnly(instance),
                       dispatcher->plugin_dispatcher_id());
  return host_resolver->GetReference();
}

bool PPB_HostResolver_Private_Proxy::OnMessageReceived(
    const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_HostResolver_Private_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBHostResolver_ResolveACK, OnMsgResolveACK)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void PPB_HostResolver_Private_Proxy::OnMsgResolveACK(
    uint32 plugin_dispatcher_id,
    uint32 host_resolver_id,
    bool succeeded,
    const std::string& canonical_name,
    const std::vector<PP_NetAddress_Private>& net_address_list) {
  if (!g_id_to_host_resolver) {
    NOTREACHED();
    return;
  }
  IDToHostResolverMap::iterator it =
      g_id_to_host_resolver->find(host_resolver_id);
  if (it == g_id_to_host_resolver->end())
    return;
  it->second->OnResolveCompleted(succeeded, canonical_name, net_address_list);
}

}  // namespace proxy
}  // namespace ppapi