OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "remoting/client/ipc_host_resolver.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "content/renderer/p2p/host_address_request.h" | |
9 #include "content/renderer/p2p/socket_dispatcher.h" | |
10 #include "net/base/ip_endpoint.h" | |
11 #include "jingle/glue/utils.h" | |
12 | |
13 namespace remoting { | |
14 | |
15 class IpcHostResolver : public HostResolver { | |
16 public: | |
17 IpcHostResolver(content::P2PSocketDispatcher* socket_dispatcher) | |
18 : socket_dispatcher_(socket_dispatcher), | |
19 port_(0) { | |
20 } | |
21 | |
22 virtual ~IpcHostResolver() { | |
23 if (request_) | |
24 request_->Cancel(); | |
25 } | |
26 | |
27 virtual void Resolve(const talk_base::SocketAddress& address) OVERRIDE { | |
28 if (address.IsUnresolved()) { | |
29 port_ = address.port(); | |
30 request_ = new content::P2PHostAddressRequest(socket_dispatcher_); | |
31 request_->Request(address.hostname(), base::Bind( | |
32 &IpcHostResolver::OnDone, base::Unretained(this))); | |
33 } else { | |
34 SignalDone(this, address); | |
35 } | |
36 } | |
37 | |
38 private: | |
39 void OnDone(const net::IPAddressNumber& address) { | |
40 talk_base::SocketAddress socket_address; | |
41 if (address.empty() || | |
42 !jingle_glue::IPEndPointToSocketAddress( | |
43 net::IPEndPoint(address, port_), &socket_address)) { | |
44 // Return nil address if the request has failed. | |
45 SignalDone(this, talk_base::SocketAddress()); | |
46 return; | |
47 } | |
48 | |
49 request_ = NULL; | |
50 SignalDone(this, socket_address); | |
51 } | |
52 | |
53 content::P2PSocketDispatcher* socket_dispatcher_; | |
54 scoped_refptr<content::P2PHostAddressRequest> request_; | |
55 uint16 port_; | |
56 }; | |
57 | |
58 IpcHostResolverFactory::IpcHostResolverFactory( | |
59 content::P2PSocketDispatcher* socket_dispatcher) | |
60 : socket_dispatcher_(socket_dispatcher) { | |
61 } | |
62 | |
63 IpcHostResolverFactory::~IpcHostResolverFactory() { | |
64 } | |
65 | |
66 HostResolver* IpcHostResolverFactory::CreateHostResolver() { | |
67 return new IpcHostResolver(socket_dispatcher_); | |
68 } | |
69 | |
70 } // namespace remoting | |
OLD | NEW |