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

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

Issue 10917167: Refactor the P2PSocketDispatcher to be created on the RenderThread instead of the RenderView. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed unnecessary destruction callback. Removed remaining routing_id. Created 8 years, 3 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) 2012 The Chromium Authors. All rights reserved. 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 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/bind.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "content/browser/renderer_host/p2p/socket_host.h" 9 #include "content/browser/renderer_host/p2p/socket_host.h"
10 #include "content/public/browser/resource_context.h" 10 #include "content/public/browser/resource_context.h"
11 #include "content/common/p2p_messages.h" 11 #include "content/common/p2p_messages.h"
12 #include "net/base/address_list.h" 12 #include "net/base/address_list.h"
13 #include "net/base/completion_callback.h" 13 #include "net/base/completion_callback.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "net/base/net_log.h" 15 #include "net/base/net_log.h"
16 #include "net/base/single_request_host_resolver.h" 16 #include "net/base/single_request_host_resolver.h"
17 #include "net/base/sys_addrinfo.h" 17 #include "net/base/sys_addrinfo.h"
18 18
19 using content::BrowserMessageFilter; 19 using content::BrowserMessageFilter;
20 using content::BrowserThread; 20 using content::BrowserThread;
21 21
22 namespace content { 22 namespace content {
23 23
24 class P2PSocketDispatcherHost::DnsRequest { 24 class P2PSocketDispatcherHost::DnsRequest {
25 public: 25 public:
26 typedef base::Callback<void(const net::IPAddressNumber&)> DoneCallback; 26 typedef base::Callback<void(const net::IPAddressNumber&)> DoneCallback;
27 27
28 DnsRequest(int32 routing_id, int32 request_id, 28 DnsRequest(int32 request_id,
29 net::HostResolver* host_resolver) 29 net::HostResolver* host_resolver)
30 : routing_id_(routing_id), 30 : request_id_(request_id),
31 request_id_(request_id),
32 resolver_(host_resolver) { 31 resolver_(host_resolver) {
33 } 32 }
34 33
35 void Resolve(const std::string& host_name, 34 void Resolve(const std::string& host_name,
36 const DoneCallback& done_callback) { 35 const DoneCallback& done_callback) {
37 DCHECK(!done_callback.is_null()); 36 DCHECK(!done_callback.is_null());
38 37
39 host_name_ = host_name; 38 host_name_ = host_name;
40 done_callback_ = done_callback; 39 done_callback_ = done_callback;
41 40
(...skipping 11 matching lines...) Expand all
53 net::HostResolver::RequestInfo info(net::HostPortPair(host_name_, 0)); 52 net::HostResolver::RequestInfo info(net::HostPortPair(host_name_, 0));
54 int result = resolver_.Resolve( 53 int result = resolver_.Resolve(
55 info, &addresses_, 54 info, &addresses_,
56 base::Bind(&P2PSocketDispatcherHost::DnsRequest::OnDone, 55 base::Bind(&P2PSocketDispatcherHost::DnsRequest::OnDone,
57 base::Unretained(this)), 56 base::Unretained(this)),
58 net::BoundNetLog()); 57 net::BoundNetLog());
59 if (result != net::ERR_IO_PENDING) 58 if (result != net::ERR_IO_PENDING)
60 OnDone(result); 59 OnDone(result);
61 } 60 }
62 61
63 int32 routing_id() { return routing_id_; }
64 int32 request_id() { return request_id_; } 62 int32 request_id() { return request_id_; }
65 63
66 private: 64 private:
67 void OnDone(int result) { 65 void OnDone(int result) {
68 if (result != net::OK) { 66 if (result != net::OK) {
69 LOG(ERROR) << "Failed to resolve address for " << host_name_ 67 LOG(ERROR) << "Failed to resolve address for " << host_name_
70 << ", errorcode: " << result; 68 << ", errorcode: " << result;
71 done_callback_.Run(net::IPAddressNumber()); 69 done_callback_.Run(net::IPAddressNumber());
72 return; 70 return;
73 } 71 }
74 72
75 // TODO(szym): Redundant check. http://crbug.com/126211 73 // TODO(szym): Redundant check. http://crbug.com/126211
76 if (addresses_.empty()) { 74 if (addresses_.empty()) {
77 LOG(ERROR) << "Received 0 addresses when trying to resolve address for " 75 LOG(ERROR) << "Received 0 addresses when trying to resolve address for "
78 << host_name_; 76 << host_name_;
79 done_callback_.Run(net::IPAddressNumber()); 77 done_callback_.Run(net::IPAddressNumber());
80 return; 78 return;
81 } 79 }
82 80
83 done_callback_.Run(addresses_.front().address()); 81 done_callback_.Run(addresses_.front().address());
84 } 82 }
85 83
86 int32 routing_id_;
87 int32 request_id_; 84 int32 request_id_;
88 net::AddressList addresses_; 85 net::AddressList addresses_;
89 86
90 std::string host_name_; 87 std::string host_name_;
91 net::SingleRequestHostResolver resolver_; 88 net::SingleRequestHostResolver resolver_;
92 89
93 DoneCallback done_callback_; 90 DoneCallback done_callback_;
94 }; 91 };
95 92
96 P2PSocketDispatcherHost::P2PSocketDispatcherHost( 93 P2PSocketDispatcherHost::P2PSocketDispatcherHost(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 138 }
142 139
143 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() { 140 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() {
144 DCHECK(sockets_.empty()); 141 DCHECK(sockets_.empty());
145 DCHECK(dns_requests_.empty()); 142 DCHECK(dns_requests_.empty());
146 143
147 if (monitoring_networks_) 144 if (monitoring_networks_)
148 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); 145 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
149 } 146 }
150 147
151 P2PSocketHost* P2PSocketDispatcherHost::LookupSocket( 148 P2PSocketHost* P2PSocketDispatcherHost::LookupSocket(int socket_id) {
152 int32 routing_id, int socket_id) { 149 SocketsMap::iterator it = sockets_.find(socket_id);
153 SocketsMap::iterator it = sockets_.find(
154 ExtendedSocketId(routing_id, socket_id));
155 if (it == sockets_.end()) 150 if (it == sockets_.end())
tommi (sloooow) - chröme 2012/09/11 12:09:09 change this to: return (it == sockets_.end()) ? N
perkj_chrome 2012/09/11 14:27:30 Done.
156 return NULL; 151 return NULL;
157 else 152 else
158 return it->second; 153 return it->second;
159 } 154 }
160 155
161 void P2PSocketDispatcherHost::OnStartNetworkNotifications( 156 void P2PSocketDispatcherHost::OnStartNetworkNotifications(
162 const IPC::Message& msg) { 157 const IPC::Message& msg) {
163 if (!monitoring_networks_) { 158 if (!monitoring_networks_) {
164 net::NetworkChangeNotifier::AddIPAddressObserver(this); 159 net::NetworkChangeNotifier::AddIPAddressObserver(this);
165 monitoring_networks_ = true; 160 monitoring_networks_ = true;
166 } 161 }
167 162
168 notifications_routing_ids_.insert(msg.routing_id());
169
170 BrowserThread::PostTask( 163 BrowserThread::PostTask(
171 BrowserThread::FILE, FROM_HERE, base::Bind( 164 BrowserThread::FILE, FROM_HERE, base::Bind(
172 &P2PSocketDispatcherHost::DoGetNetworkList, this)); 165 &P2PSocketDispatcherHost::DoGetNetworkList, this));
173 } 166 }
174 167
175 void P2PSocketDispatcherHost::OnStopNetworkNotifications( 168 void P2PSocketDispatcherHost::OnStopNetworkNotifications(
176 const IPC::Message& msg) { 169 const IPC::Message& msg) {
177 notifications_routing_ids_.erase(msg.routing_id()); 170 if (monitoring_networks_) {
171 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
172 monitoring_networks_ = false;
173 }
178 } 174 }
179 175
180 void P2PSocketDispatcherHost::OnGetHostAddress(const IPC::Message& msg, 176 void P2PSocketDispatcherHost::OnGetHostAddress(const std::string& host_name,
181 const std::string& host_name,
182 int32 request_id) { 177 int32 request_id) {
183 DnsRequest* request = new DnsRequest( 178 DnsRequest* request = new DnsRequest(request_id,
184 msg.routing_id(), request_id, resource_context_->GetHostResolver()); 179 resource_context_->GetHostResolver());
185 dns_requests_.insert(request); 180 dns_requests_.insert(request);
186 request->Resolve(host_name, base::Bind( 181 request->Resolve(host_name, base::Bind(
187 &P2PSocketDispatcherHost::OnAddressResolved, 182 &P2PSocketDispatcherHost::OnAddressResolved,
188 base::Unretained(this), request)); 183 base::Unretained(this), request));
189 } 184 }
190 185
191 void P2PSocketDispatcherHost::OnCreateSocket( 186 void P2PSocketDispatcherHost::OnCreateSocket(
192 const IPC::Message& msg, P2PSocketType type, int socket_id, 187 P2PSocketType type, int socket_id,
193 const net::IPEndPoint& local_address, 188 const net::IPEndPoint& local_address,
194 const net::IPEndPoint& remote_address) { 189 const net::IPEndPoint& remote_address) {
195 if (LookupSocket(msg.routing_id(), socket_id)) { 190 if (LookupSocket(socket_id)) {
196 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket " 191 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
197 "that already exists."; 192 "that already exists.";
198 return; 193 return;
199 } 194 }
200 195
201 scoped_ptr<P2PSocketHost> socket( 196 scoped_ptr<P2PSocketHost> socket(
202 P2PSocketHost::Create(this, msg.routing_id(), socket_id, type)); 197 P2PSocketHost::Create(this, socket_id, type));
203 198
204 if (!socket.get()) { 199 if (!socket.get()) {
205 Send(new P2PMsg_OnError(msg.routing_id(), socket_id)); 200 Send(new P2PMsg_OnError(socket_id));
206 return; 201 return;
207 } 202 }
208 203
209 if (socket->Init(local_address, remote_address)) { 204 if (socket->Init(local_address, remote_address)) {
210 sockets_.insert(std::pair<ExtendedSocketId, P2PSocketHost*>( 205 sockets_.insert(SocketPair(socket_id, socket.release()));
tommi (sloooow) - chröme 2012/09/11 12:09:09 without using SocketPair, this should just be: soc
perkj_chrome 2012/09/11 14:27:30 Done.
211 ExtendedSocketId(msg.routing_id(), socket_id), socket.release()));
212 } 206 }
213 } 207 }
214 208
215 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection( 209 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection(
216 const IPC::Message& msg, int listen_socket_id, 210 int listen_socket_id, const net::IPEndPoint& remote_address,
217 const net::IPEndPoint& remote_address, int connected_socket_id) { 211 int connected_socket_id) {
218 P2PSocketHost* socket = LookupSocket(msg.routing_id(), listen_socket_id); 212 P2PSocketHost* socket = LookupSocket(listen_socket_id);
219 if (!socket) { 213 if (!socket) {
220 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection " 214 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection "
221 "for invalid socket_id."; 215 "for invalid socket_id.";
222 return; 216 return;
223 } 217 }
224 P2PSocketHost* accepted_connection = 218 P2PSocketHost* accepted_connection =
225 socket->AcceptIncomingTcpConnection(remote_address, connected_socket_id); 219 socket->AcceptIncomingTcpConnection(remote_address, connected_socket_id);
226 if (accepted_connection) { 220 if (accepted_connection) {
227 sockets_.insert(std::pair<ExtendedSocketId, P2PSocketHost*>( 221 sockets_.insert(SocketPair(connected_socket_id, accepted_connection));
tommi (sloooow) - chröme 2012/09/11 12:09:09 sockets_[connected_socket_id] = accepted_connectio
perkj_chrome 2012/09/11 14:27:30 Done.
228 ExtendedSocketId(msg.routing_id(), connected_socket_id),
229 accepted_connection));
230 } 222 }
231 } 223 }
232 224
233 void P2PSocketDispatcherHost::OnSend(const IPC::Message& msg, int socket_id, 225 void P2PSocketDispatcherHost::OnSend(int socket_id,
234 const net::IPEndPoint& socket_address, 226 const net::IPEndPoint& socket_address,
235 const std::vector<char>& data) { 227 const std::vector<char>& data) {
236 P2PSocketHost* socket = LookupSocket(msg.routing_id(), socket_id); 228 P2PSocketHost* socket = LookupSocket(socket_id);
237 if (!socket) { 229 if (!socket) {
238 LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id."; 230 LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id.";
239 return; 231 return;
240 } 232 }
241 socket->Send(socket_address, data); 233 socket->Send(socket_address, data);
242 } 234 }
243 235
244 void P2PSocketDispatcherHost::OnDestroySocket(const IPC::Message& msg, 236 void P2PSocketDispatcherHost::OnDestroySocket(int socket_id) {
245 int socket_id) { 237 SocketsMap::iterator it = sockets_.find(socket_id);
246 SocketsMap::iterator it = sockets_.find(
247 ExtendedSocketId(msg.routing_id(), socket_id));
248 if (it != sockets_.end()) { 238 if (it != sockets_.end()) {
249 delete it->second; 239 delete it->second;
250 sockets_.erase(it); 240 sockets_.erase(it);
251 } else { 241 } else {
252 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id."; 242 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id.";
253 } 243 }
254 } 244 }
255 245
256 void P2PSocketDispatcherHost::DoGetNetworkList() { 246 void P2PSocketDispatcherHost::DoGetNetworkList() {
257 net::NetworkInterfaceList list; 247 net::NetworkInterfaceList list;
258 net::GetNetworkList(&list); 248 net::GetNetworkList(&list);
259 BrowserThread::PostTask( 249 BrowserThread::PostTask(
260 BrowserThread::IO, FROM_HERE, base::Bind( 250 BrowserThread::IO, FROM_HERE, base::Bind(
261 &P2PSocketDispatcherHost::SendNetworkList, this, list)); 251 &P2PSocketDispatcherHost::SendNetworkList, this, list));
262 } 252 }
263 253
264 void P2PSocketDispatcherHost::SendNetworkList( 254 void P2PSocketDispatcherHost::SendNetworkList(
265 const net::NetworkInterfaceList& list) { 255 const net::NetworkInterfaceList& list) {
266 for (std::set<int>::iterator it = notifications_routing_ids_.begin(); 256 Send(new P2PMsg_NetworkListChanged(list));
267 it != notifications_routing_ids_.end(); ++it) {
268 Send(new P2PMsg_NetworkListChanged(*it, list));
269 }
270 } 257 }
271 258
272 void P2PSocketDispatcherHost::OnAddressResolved( 259 void P2PSocketDispatcherHost::OnAddressResolved(
273 DnsRequest* request, 260 DnsRequest* request,
274 const net::IPAddressNumber& result) { 261 const net::IPAddressNumber& result) {
275 Send(new P2PMsg_GetHostAddressResult( 262 Send(new P2PMsg_GetHostAddressResult(request->request_id(), result));
276 request->routing_id(), request->request_id(), result));
277 263
278 dns_requests_.erase(request); 264 dns_requests_.erase(request);
279 delete request; 265 delete request;
280 } 266 }
281 267
282 } // namespace content 268 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698