Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_dispatcher_host.cc
blobbe63c0217bb4cd277713dac1d04d688d5404be26
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_dispatcher_host.h"
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "content/browser/renderer_host/p2p/socket_host.h"
10 #include "content/common/p2p_messages.h"
11 #include "content/public/browser/resource_context.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/sys_addrinfo.h"
17 #include "net/dns/single_request_host_resolver.h"
18 #include "net/url_request/url_request_context_getter.h"
20 using content::BrowserMessageFilter;
21 using content::BrowserThread;
23 namespace content {
25 const size_t kMaximumPacketSize = 32768;
27 class P2PSocketDispatcherHost::DnsRequest {
28 public:
29 typedef base::Callback<void(const net::IPAddressList&)> DoneCallback;
31 DnsRequest(int32 request_id, net::HostResolver* host_resolver)
32 : request_id_(request_id),
33 resolver_(host_resolver) {
36 void Resolve(const std::string& host_name,
37 const DoneCallback& done_callback) {
38 DCHECK(!done_callback.is_null());
40 host_name_ = host_name;
41 done_callback_ = done_callback;
43 // Return an error if it's an empty string.
44 if (host_name_.empty()) {
45 net::IPAddressList address_list;
46 done_callback_.Run(address_list);
47 return;
50 // Add period at the end to make sure that we only resolve
51 // fully-qualified names.
52 if (host_name_.at(host_name_.size() - 1) != '.')
53 host_name_ = host_name_ + '.';
55 net::HostResolver::RequestInfo info(net::HostPortPair(host_name_, 0));
56 int result = resolver_.Resolve(
57 info,
58 net::DEFAULT_PRIORITY,
59 &addresses_,
60 base::Bind(&P2PSocketDispatcherHost::DnsRequest::OnDone,
61 base::Unretained(this)),
62 net::BoundNetLog());
63 if (result != net::ERR_IO_PENDING)
64 OnDone(result);
67 int32 request_id() { return request_id_; }
69 private:
70 void OnDone(int result) {
71 net::IPAddressList list;
72 if (result != net::OK) {
73 LOG(ERROR) << "Failed to resolve address for " << host_name_
74 << ", errorcode: " << result;
75 done_callback_.Run(list);
76 return;
79 DCHECK(!addresses_.empty());
80 for (net::AddressList::iterator iter = addresses_.begin();
81 iter != addresses_.end(); ++iter) {
82 list.push_back(iter->address());
84 done_callback_.Run(list);
87 int32 request_id_;
88 net::AddressList addresses_;
90 std::string host_name_;
91 net::SingleRequestHostResolver resolver_;
93 DoneCallback done_callback_;
96 P2PSocketDispatcherHost::P2PSocketDispatcherHost(
97 content::ResourceContext* resource_context,
98 net::URLRequestContextGetter* url_context)
99 : BrowserMessageFilter(P2PMsgStart),
100 resource_context_(resource_context),
101 url_context_(url_context),
102 monitoring_networks_(false) {
105 void P2PSocketDispatcherHost::OnChannelClosing() {
106 // Since the IPC channel is gone, close pending connections.
107 STLDeleteContainerPairSecondPointers(sockets_.begin(), sockets_.end());
108 sockets_.clear();
110 STLDeleteContainerPointers(dns_requests_.begin(), dns_requests_.end());
111 dns_requests_.clear();
113 if (monitoring_networks_) {
114 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
115 monitoring_networks_ = false;
119 void P2PSocketDispatcherHost::OnDestruct() const {
120 BrowserThread::DeleteOnIOThread::Destruct(this);
123 bool P2PSocketDispatcherHost::OnMessageReceived(const IPC::Message& message,
124 bool* message_was_ok) {
125 bool handled = true;
126 IPC_BEGIN_MESSAGE_MAP_EX(P2PSocketDispatcherHost, message, *message_was_ok)
127 IPC_MESSAGE_HANDLER(P2PHostMsg_StartNetworkNotifications,
128 OnStartNetworkNotifications)
129 IPC_MESSAGE_HANDLER(P2PHostMsg_StopNetworkNotifications,
130 OnStopNetworkNotifications)
131 IPC_MESSAGE_HANDLER(P2PHostMsg_GetHostAddress, OnGetHostAddress)
132 IPC_MESSAGE_HANDLER(P2PHostMsg_CreateSocket, OnCreateSocket)
133 IPC_MESSAGE_HANDLER(P2PHostMsg_AcceptIncomingTcpConnection,
134 OnAcceptIncomingTcpConnection)
135 IPC_MESSAGE_HANDLER(P2PHostMsg_Send, OnSend)
136 IPC_MESSAGE_HANDLER(P2PHostMsg_SetOption, OnSetOption)
137 IPC_MESSAGE_HANDLER(P2PHostMsg_DestroySocket, OnDestroySocket)
138 IPC_MESSAGE_UNHANDLED(handled = false)
139 IPC_END_MESSAGE_MAP_EX()
140 return handled;
143 void P2PSocketDispatcherHost::OnIPAddressChanged() {
144 // Notify the renderer about changes to list of network interfaces.
145 BrowserThread::PostTask(
146 BrowserThread::FILE, FROM_HERE, base::Bind(
147 &P2PSocketDispatcherHost::DoGetNetworkList, this));
150 P2PSocketDispatcherHost::~P2PSocketDispatcherHost() {
151 DCHECK(sockets_.empty());
152 DCHECK(dns_requests_.empty());
154 if (monitoring_networks_)
155 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
158 P2PSocketHost* P2PSocketDispatcherHost::LookupSocket(int socket_id) {
159 SocketsMap::iterator it = sockets_.find(socket_id);
160 return (it == sockets_.end()) ? NULL : it->second;
163 void P2PSocketDispatcherHost::OnStartNetworkNotifications(
164 const IPC::Message& msg) {
165 if (!monitoring_networks_) {
166 net::NetworkChangeNotifier::AddIPAddressObserver(this);
167 monitoring_networks_ = true;
170 BrowserThread::PostTask(
171 BrowserThread::FILE, FROM_HERE, base::Bind(
172 &P2PSocketDispatcherHost::DoGetNetworkList, this));
175 void P2PSocketDispatcherHost::OnStopNetworkNotifications(
176 const IPC::Message& msg) {
177 if (monitoring_networks_) {
178 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
179 monitoring_networks_ = false;
183 void P2PSocketDispatcherHost::OnGetHostAddress(const std::string& host_name,
184 int32 request_id) {
185 DnsRequest* request = new DnsRequest(request_id,
186 resource_context_->GetHostResolver());
187 dns_requests_.insert(request);
188 request->Resolve(host_name, base::Bind(
189 &P2PSocketDispatcherHost::OnAddressResolved,
190 base::Unretained(this), request));
193 void P2PSocketDispatcherHost::OnCreateSocket(
194 P2PSocketType type, int socket_id,
195 const net::IPEndPoint& local_address,
196 const P2PHostAndIPEndPoint& remote_address) {
197 if (LookupSocket(socket_id)) {
198 LOG(ERROR) << "Received P2PHostMsg_CreateSocket for socket "
199 "that already exists.";
200 return;
203 scoped_ptr<P2PSocketHost> socket(P2PSocketHost::Create(
204 this, socket_id, type, url_context_.get(), &throttler_));
206 if (!socket) {
207 Send(new P2PMsg_OnError(socket_id));
208 return;
211 if (socket->Init(local_address, remote_address)) {
212 sockets_[socket_id] = socket.release();
216 void P2PSocketDispatcherHost::OnAcceptIncomingTcpConnection(
217 int listen_socket_id, const net::IPEndPoint& remote_address,
218 int connected_socket_id) {
219 P2PSocketHost* socket = LookupSocket(listen_socket_id);
220 if (!socket) {
221 LOG(ERROR) << "Received P2PHostMsg_AcceptIncomingTcpConnection "
222 "for invalid socket_id.";
223 return;
225 P2PSocketHost* accepted_connection =
226 socket->AcceptIncomingTcpConnection(remote_address, connected_socket_id);
227 if (accepted_connection) {
228 sockets_[connected_socket_id] = accepted_connection;
232 void P2PSocketDispatcherHost::OnSend(int socket_id,
233 const net::IPEndPoint& socket_address,
234 const std::vector<char>& data,
235 const talk_base::PacketOptions& options,
236 uint64 packet_id) {
237 P2PSocketHost* socket = LookupSocket(socket_id);
238 if (!socket) {
239 LOG(ERROR) << "Received P2PHostMsg_Send for invalid socket_id.";
240 return;
243 if (data.size() > kMaximumPacketSize) {
244 LOG(ERROR) << "Received P2PHostMsg_Send with a packet that is too big: "
245 << data.size();
246 Send(new P2PMsg_OnError(socket_id));
247 delete socket;
248 sockets_.erase(socket_id);
249 return;
252 socket->Send(socket_address, data, options, packet_id);
255 void P2PSocketDispatcherHost::OnSetOption(int socket_id,
256 P2PSocketOption option,
257 int value) {
258 P2PSocketHost* socket = LookupSocket(socket_id);
259 if (!socket) {
260 LOG(ERROR) << "Received P2PHostMsg_SetOption for invalid socket_id.";
261 return;
264 socket->SetOption(option, value);
267 void P2PSocketDispatcherHost::OnDestroySocket(int socket_id) {
268 SocketsMap::iterator it = sockets_.find(socket_id);
269 if (it != sockets_.end()) {
270 delete it->second;
271 sockets_.erase(it);
272 } else {
273 LOG(ERROR) << "Received P2PHostMsg_DestroySocket for invalid socket_id.";
277 void P2PSocketDispatcherHost::DoGetNetworkList() {
278 net::NetworkInterfaceList list;
279 net::GetNetworkList(&list, net::EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES |
280 net::INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE);
281 BrowserThread::PostTask(
282 BrowserThread::IO, FROM_HERE, base::Bind(
283 &P2PSocketDispatcherHost::SendNetworkList, this, list));
286 void P2PSocketDispatcherHost::SendNetworkList(
287 const net::NetworkInterfaceList& list) {
288 Send(new P2PMsg_NetworkListChanged(list));
291 void P2PSocketDispatcherHost::OnAddressResolved(
292 DnsRequest* request,
293 const net::IPAddressList& addresses) {
294 Send(new P2PMsg_GetHostAddressResult(request->request_id(), addresses));
296 dns_requests_.erase(request);
297 delete request;
300 } // namespace content