Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(557)

Side by Side Diff: content/browser/renderer_host/p2p/socket_dispatcher_host.cc

Issue 7599003: Add IPC for DNS host address resolution. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/p2p/socket_dispatcher_host.h" 5 #include "content/browser/renderer_host/p2p/socket_dispatcher_host.h"
6 6
7 #include "base/bind.h"
7 #include "base/stl_util.h" 8 #include "base/stl_util.h"
8 #include "content/browser/renderer_host/p2p/socket_host.h" 9 #include "content/browser/renderer_host/p2p/socket_host.h"
10 #include "content/browser/resource_context.h"
9 #include "content/common/p2p_messages.h" 11 #include "content/common/p2p_messages.h"
12 #include "net/base/address_list.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_log.h"
16 #include "net/base/single_request_host_resolver.h"
17 #include "net/base/sys_addrinfo.h"
10 18
11 P2PSocketDispatcherHost::P2PSocketDispatcherHost() 19 class P2PSocketDispatcherHost::DnsRequest {
12 : monitoring_networks_(false) { 20 public:
21 typedef base::Callback<void(const net::IPAddressNumber&)> DoneCallback;
22
23 DnsRequest(int32 routing_id, int32 request_id,
24 net::HostResolver* host_resolver)
25 : routing_id_(routing_id),
26 request_id_(request_id),
27 resolver_(host_resolver),
28 ALLOW_THIS_IN_INITIALIZER_LIST(completion_callback_(
29 this, &P2PSocketDispatcherHost::DnsRequest::OnDone)) {
30 }
31
32 void Resolve(const std::string& host_name,
33 const DoneCallback& done_callback) {
34 DCHECK(!done_callback.is_null());
35
36 host_name_ = host_name;
37 done_callback_ = done_callback;
38
39 // Return an error if it's an empty string.
40 if (host_name_.empty()) {
41 done_callback_.Run(net::IPAddressNumber());
42 return;
43 }
44
45 // Add period at the end to make sure that we only resolve
46 // fully-qualified names.
47 if (host_name_.at(host_name_.size() - 1) != '.')
48 host_name_ = host_name_ + '.';
49
50 net::HostResolver::RequestInfo info(net::HostPortPair(host_name_, 0));
51 resolver_.Resolve(info, &addresses_, &completion_callback_,
52 net::BoundNetLog());
53 }
54
55 int32 routing_id() { return routing_id_; }
56 int32 request_id() { return request_id_; }
57
58 private:
59 void OnDone(int result) {
60 if (result != net::OK) {
61 LOG(ERROR) << "Failed to resolve address for " << host_name_
62 << ", errorcode: " << result;
63 done_callback_.Run(net::IPAddressNumber());
64 return;
65 }
66
67 if (addresses_.head() == NULL) {
68 LOG(ERROR) << "Received 0 addresses when trying to resolve address for "
69 << host_name_;
70 done_callback_.Run(net::IPAddressNumber());
71 return;
72 }
73
74 net::IPEndPoint end_point;
75 if (!end_point.FromSockAddr(addresses_.head()->ai_addr,
76 addresses_.head()->ai_addrlen)) {
77 LOG(ERROR) << "Received invalid address for " << host_name_;
78 done_callback_.Run(net::IPAddressNumber());
79 return;
80 }
81
82 done_callback_.Run(end_point.address());
83 }
84
85 int32 routing_id_;
86 int32 request_id_;
87 net::AddressList addresses_;
88
89 std::string host_name_;
90 net::SingleRequestHostResolver resolver_;
91
92 DoneCallback done_callback_;
93
94 net::CompletionCallbackImpl<DnsRequest> completion_callback_;
95 };
96
97 P2PSocketDispatcherHost::P2PSocketDispatcherHost(
98 const content::ResourceContext* resource_context)
99 : resource_context_(resource_context),
100 monitoring_networks_(false) {
13 } 101 }
14 102
15 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() { 103 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() {
16 DCHECK(sockets_.empty()); 104 DCHECK(sockets_.empty());
105 DCHECK(dns_requests_.empty());
17 106
18 if (monitoring_networks_) 107 if (monitoring_networks_)
19 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); 108 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
20 } 109 }
21 110
22 void P2PSocketDispatcherHost::OnChannelClosing() { 111 void P2PSocketDispatcherHost::OnChannelClosing() {
23 BrowserMessageFilter::OnChannelClosing(); 112 BrowserMessageFilter::OnChannelClosing();
24 113
25 // Since the IPC channel is gone, close pending connections. 114 // Since the IPC channel is gone, close pending connections.
26 STLDeleteContainerPairSecondPointers(sockets_.begin(), sockets_.end()); 115 STLDeleteContainerPairSecondPointers(sockets_.begin(), sockets_.end());
27 sockets_.clear(); 116 sockets_.clear();
117
118 STLDeleteContainerPointers(dns_requests_.begin(), dns_requests_.end());
119 dns_requests_.clear();
28 } 120 }
29 121
30 void P2PSocketDispatcherHost::OnDestruct() const { 122 void P2PSocketDispatcherHost::OnDestruct() const {
31 BrowserThread::DeleteOnIOThread::Destruct(this); 123 BrowserThread::DeleteOnIOThread::Destruct(this);
32 } 124 }
33 125
34 bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message, 126 bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message,
35 bool* message_was_ok) { 127 bool* message_was_ok) {
36 bool handled = true; 128 bool handled = true;
37 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost, message, *message_was_ok) 129 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost, message, *message_was_ok)
38 IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications, 130 IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications,
39 OnStartNetworkNotifications) 131 OnStartNetworkNotifications)
40 IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications, 132 IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications,
41 OnStopNetworkNotifications) 133 OnStopNetworkNotifications)
134 IPC_MESSAGE_HANDLER(P2PHostMsg_GetHostAddress, OnGetHostAddress)
42 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket) 135 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket)
43 IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection, 136 IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection,
44 OnAcceptIncomingTcpConnection) 137 OnAcceptIncomingTcpConnection)
45 IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend) 138 IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend)
46 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket) 139 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket)
47 IPC_MESSAGE_UNHANDLED(handled = false) 140 IPC_MESSAGE_UNHANDLED(handled = false)
48 IPC_END_MESSAGE_MAP_EX() 141 IPC_END_MESSAGE_MAP_EX()
49 return handled; 142 return handled;
50 } 143 }
51 144
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 } 187 }
95 188
96 void P2PSocketDispatcherHost::SendNetworkList( 189 void P2PSocketDispatcherHost::SendNetworkList(
97 const net::NetworkInterfaceList& list) { 190 const net::NetworkInterfaceList& list) {
98 for (std::set<int>::iterator it = notifications_routing_ids_.begin(); 191 for (std::set<int>::iterator it = notifications_routing_ids_.begin();
99 it != notifications_routing_ids_.end(); ++it) { 192 it != notifications_routing_ids_.end(); ++it) {
100 Send(new P2PMsg_NetworkListChanged(*it, list)); 193 Send(new P2PMsg_NetworkListChanged(*it, list));
101 } 194 }
102 } 195 }
103 196
197 void P2PSocketDispatcherHost::OnGetHostAddress(const IPC::Message& msg,
198 const std::string& host_name,
199 int32 request_id) {
200 DnsRequest* request = new DnsRequest(
201 msg.routing_id(), request_id, resource_context_->host_resolver());
202 request->Resolve(host_name, base::Bind(
203 &P2PSocketDispatcherHost::OnAddressResolved,
204 base::Unretained(this), request));
205 dns_requests_.insert(request);
206 }
207
208 void P2PSocketDispatcherHost::OnAddressResolved(
209 DnsRequest* request,
210 const net::IPAddressNumber& result) {
211 Send(new P2PMsg_GetHostAddressResult(
212 request->routing_id(), request->request_id(), result));
213
214 dns_requests_.erase(request);
215 delete request;
216 }
217
104 void P2PSocketDispatcherHost::OnCreateSocket( 218 void P2PSocketDispatcherHost::OnCreateSocket(
105 const IPC::Message& msg, P2PSocketType type, int socket_id, 219 const IPC::Message& msg, P2PSocketType type, int socket_id,
106 const net::IPEndPoint& local_address, 220 const net::IPEndPoint& local_address,
107 const net::IPEndPoint& remote_address) { 221 const net::IPEndPoint& remote_address) {
108 if (LookupSocket(msg.routing_id(), socket_id)) { 222 if (LookupSocket(msg.routing_id(), socket_id)) {
109 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket " 223 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
110 "that already exists."; 224 "that already exists.";
111 return; 225 return;
112 } 226 }
113 227
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 int socket_id) { 272 int socket_id) {
159 SocketsMap::iterator it = sockets_.find( 273 SocketsMap::iterator it = sockets_.find(
160 ExtendedSocketId(msg.routing_id(), socket_id)); 274 ExtendedSocketId(msg.routing_id(), socket_id));
161 if (it != sockets_.end()) { 275 if (it != sockets_.end()) {
162 delete it->second; 276 delete it->second;
163 sockets_.erase(it); 277 sockets_.erase(it);
164 } else { 278 } else {
165 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id."; 279 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id.";
166 } 280 }
167 } 281 }
OLDNEW
« no previous file with comments | « content/browser/renderer_host/p2p/socket_dispatcher_host.h ('k') | content/common/p2p_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698