Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 13 matching lines...) Expand all Loading... | |
| 110 dns_requests_.clear(); | 107 dns_requests_.clear(); |
| 111 } | 108 } |
| 112 | 109 |
| 113 void P2PSocketDispatcherHost::OnDestruct() const { | 110 void P2PSocketDispatcherHost::OnDestruct() const { |
| 114 BrowserThread::DeleteOnIOThread::Destruct(this); | 111 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 115 } | 112 } |
| 116 | 113 |
| 117 bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message, | 114 bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message, |
| 118 bool* message_was_ok) { | 115 bool* message_was_ok) { |
| 119 bool handled = true; | 116 bool handled = true; |
| 120 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost, message, *message_was_ok) | 117 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost, message, *message_was_ok) |
|
tommi (sloooow) - chröme
2012/09/11 09:04:17
As discussed I think it would make the class a lit
| |
| 121 IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications, | 118 IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications, |
| 122 OnStartNetworkNotifications) | 119 OnStartNetworkNotifications) |
| 123 IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications, | 120 IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications, |
| 124 OnStopNetworkNotifications) | 121 OnStopNetworkNotifications) |
| 125 IPC_MESSAGE_HANDLER(P2PHostMsg_GetHostAddress, OnGetHostAddress) | 122 IPC_MESSAGE_HANDLER(P2PHostMsg_GetHostAddress, OnGetHostAddress) |
| 126 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket) | 123 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket) |
| 127 IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection, | 124 IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection, |
| 128 OnAcceptIncomingTcpConnection) | 125 OnAcceptIncomingTcpConnection) |
| 129 IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend) | 126 IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend) |
| 130 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket) | 127 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket) |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 158 return it->second; | 155 return it->second; |
| 159 } | 156 } |
| 160 | 157 |
| 161 void P2PSocketDispatcherHost::OnStartNetworkNotifications( | 158 void P2PSocketDispatcherHost::OnStartNetworkNotifications( |
| 162 const IPC::Message& msg) { | 159 const IPC::Message& msg) { |
| 163 if (!monitoring_networks_) { | 160 if (!monitoring_networks_) { |
| 164 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 161 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 165 monitoring_networks_ = true; | 162 monitoring_networks_ = true; |
| 166 } | 163 } |
| 167 | 164 |
| 168 notifications_routing_ids_.insert(msg.routing_id()); | |
| 169 | |
| 170 BrowserThread::PostTask( | 165 BrowserThread::PostTask( |
| 171 BrowserThread::FILE, FROM_HERE, base::Bind( | 166 BrowserThread::FILE, FROM_HERE, base::Bind( |
| 172 &P2PSocketDispatcherHost::DoGetNetworkList, this)); | 167 &P2PSocketDispatcherHost::DoGetNetworkList, this)); |
| 173 } | 168 } |
| 174 | 169 |
| 175 void P2PSocketDispatcherHost::OnStopNetworkNotifications( | 170 void P2PSocketDispatcherHost::OnStopNetworkNotifications( |
| 176 const IPC::Message& msg) { | 171 const IPC::Message& msg) { |
| 177 notifications_routing_ids_.erase(msg.routing_id()); | 172 if (monitoring_networks_) { |
| 173 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | |
| 174 monitoring_networks_ = false; | |
| 175 } | |
| 178 } | 176 } |
| 179 | 177 |
| 180 void P2PSocketDispatcherHost::OnGetHostAddress(const IPC::Message& msg, | 178 void P2PSocketDispatcherHost::OnGetHostAddress(const IPC::Message& msg, |
| 181 const std::string& host_name, | 179 const std::string& host_name, |
| 182 int32 request_id) { | 180 int32 request_id) { |
| 183 DnsRequest* request = new DnsRequest( | 181 DnsRequest* request = new DnsRequest(request_id, |
| 184 msg.routing_id(), request_id, resource_context_->GetHostResolver()); | 182 resource_context_->GetHostResolver()); |
| 185 dns_requests_.insert(request); | 183 dns_requests_.insert(request); |
| 186 request->Resolve(host_name, base::Bind( | 184 request->Resolve(host_name, base::Bind( |
| 187 &P2PSocketDispatcherHost::OnAddressResolved, | 185 &P2PSocketDispatcherHost::OnAddressResolved, |
| 188 base::Unretained(this), request)); | 186 base::Unretained(this), request)); |
| 189 } | 187 } |
| 190 | 188 |
| 191 void P2PSocketDispatcherHost::OnCreateSocket( | 189 void P2PSocketDispatcherHost::OnCreateSocket( |
| 192 const IPC::Message& msg, P2PSocketType type, int socket_id, | 190 const IPC::Message& msg, P2PSocketType type, int socket_id, |
| 193 const net::IPEndPoint& local_address, | 191 const net::IPEndPoint& local_address, |
| 194 const net::IPEndPoint& remote_address) { | 192 const net::IPEndPoint& remote_address) { |
| 195 if (LookupSocket(msg.routing_id(), socket_id)) { | 193 if (LookupSocket(msg.routing_id(), socket_id)) { |
| 196 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket " | 194 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket " |
| 197 "that already exists."; | 195 "that already exists."; |
| 198 return; | 196 return; |
| 199 } | 197 } |
| 200 | 198 |
| 201 scoped_ptr<P2PSocketHost> socket( | 199 scoped_ptr<P2PSocketHost> socket( |
| 202 P2PSocketHost::Create(this, msg.routing_id(), socket_id, type)); | 200 P2PSocketHost::Create(this, socket_id, type)); |
| 203 | 201 |
| 204 if (!socket.get()) { | 202 if (!socket.get()) { |
| 205 Send(new P2PMsg_OnError(msg.routing_id(), socket_id)); | 203 Send(new P2PMsg_OnError(socket_id)); |
| 206 return; | 204 return; |
| 207 } | 205 } |
| 208 | 206 |
| 209 if (socket->Init(local_address, remote_address)) { | 207 if (socket->Init(local_address, remote_address)) { |
| 210 sockets_.insert(std::pair<ExtendedSocketId, P2PSocketHost*>( | 208 sockets_.insert(std::pair<ExtendedSocketId, P2PSocketHost*>( |
| 211 ExtendedSocketId(msg.routing_id(), socket_id), socket.release())); | 209 ExtendedSocketId(msg.routing_id(), socket_id), socket.release())); |
| 212 } | 210 } |
| 213 } | 211 } |
| 214 | 212 |
| 215 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection( | 213 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 void P2PSocketDispatcherHost::DoGetNetworkList() { | 254 void P2PSocketDispatcherHost::DoGetNetworkList() { |
| 257 net::NetworkInterfaceList list; | 255 net::NetworkInterfaceList list; |
| 258 net::GetNetworkList(&list); | 256 net::GetNetworkList(&list); |
| 259 BrowserThread::PostTask( | 257 BrowserThread::PostTask( |
| 260 BrowserThread::IO, FROM_HERE, base::Bind( | 258 BrowserThread::IO, FROM_HERE, base::Bind( |
| 261 &P2PSocketDispatcherHost::SendNetworkList, this, list)); | 259 &P2PSocketDispatcherHost::SendNetworkList, this, list)); |
| 262 } | 260 } |
| 263 | 261 |
| 264 void P2PSocketDispatcherHost::SendNetworkList( | 262 void P2PSocketDispatcherHost::SendNetworkList( |
| 265 const net::NetworkInterfaceList& list) { | 263 const net::NetworkInterfaceList& list) { |
| 266 for (std::set<int>::iterator it = notifications_routing_ids_.begin(); | 264 Send(new P2PMsg_NetworkListChanged(list)); |
| 267 it != notifications_routing_ids_.end(); ++it) { | |
| 268 Send(new P2PMsg_NetworkListChanged(*it, list)); | |
| 269 } | |
| 270 } | 265 } |
| 271 | 266 |
| 272 void P2PSocketDispatcherHost::OnAddressResolved( | 267 void P2PSocketDispatcherHost::OnAddressResolved( |
| 273 DnsRequest* request, | 268 DnsRequest* request, |
| 274 const net::IPAddressNumber& result) { | 269 const net::IPAddressNumber& result) { |
| 275 Send(new P2PMsg_GetHostAddressResult( | 270 Send(new P2PMsg_GetHostAddressResult(request->request_id(), result)); |
| 276 request->routing_id(), request->request_id(), result)); | |
| 277 | 271 |
| 278 dns_requests_.erase(request); | 272 dns_requests_.erase(request); |
| 279 delete request; | 273 delete request; |
| 280 } | 274 } |
| 281 | 275 |
| 282 } // namespace content | 276 } // namespace content |
| OLD | NEW |