Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / p2p / socket_dispatcher.cc
blobe9816f47e3c0f5a4fa6170296f92b2c82f7b3784
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/renderer/p2p/socket_dispatcher.h"
7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h"
9 #include "content/child/child_process.h"
10 #include "content/common/p2p_messages.h"
11 #include "content/renderer/p2p/host_address_request.h"
12 #include "content/renderer/p2p/network_list_observer.h"
13 #include "content/renderer/p2p/socket_client_impl.h"
14 #include "content/renderer/render_view_impl.h"
15 #include "ipc/ipc_sender.h"
17 namespace content {
19 P2PSocketDispatcher::P2PSocketDispatcher(
20 base::SingleThreadTaskRunner* ipc_task_runner)
21 : ipc_task_runner_(ipc_task_runner),
22 network_notifications_started_(false),
23 network_list_observers_(
24 new base::ObserverListThreadSafe<NetworkListObserver>()),
25 sender_(NULL) {
28 P2PSocketDispatcher::~P2PSocketDispatcher() {
29 network_list_observers_->AssertEmpty();
30 for (IDMap<P2PSocketClientImpl>::iterator i(&clients_); !i.IsAtEnd();
31 i.Advance()) {
32 i.GetCurrentValue()->Detach();
36 void P2PSocketDispatcher::AddNetworkListObserver(
37 NetworkListObserver* network_list_observer) {
38 network_list_observers_->AddObserver(network_list_observer);
39 network_notifications_started_ = true;
40 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications());
43 void P2PSocketDispatcher::RemoveNetworkListObserver(
44 NetworkListObserver* network_list_observer) {
45 network_list_observers_->RemoveObserver(network_list_observer);
48 void P2PSocketDispatcher::Send(IPC::Message* message) {
49 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
50 if (!sender_) {
51 DLOG(WARNING) << "P2PSocketDispatcher::Send() - Sender closed.";
52 delete message;
53 return;
56 sender_->Send(message);
59 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) {
60 bool handled = true;
61 IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher, message)
62 IPC_MESSAGE_HANDLER(P2PMsg_NetworkListChanged, OnNetworkListChanged)
63 IPC_MESSAGE_HANDLER(P2PMsg_GetHostAddressResult, OnGetHostAddressResult)
64 IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated, OnSocketCreated)
65 IPC_MESSAGE_HANDLER(P2PMsg_OnIncomingTcpConnection, OnIncomingTcpConnection)
66 IPC_MESSAGE_HANDLER(P2PMsg_OnSendComplete, OnSendComplete)
67 IPC_MESSAGE_HANDLER(P2PMsg_OnError, OnError)
68 IPC_MESSAGE_HANDLER(P2PMsg_OnDataReceived, OnDataReceived)
69 IPC_MESSAGE_UNHANDLED(handled = false)
70 IPC_END_MESSAGE_MAP()
71 return handled;
74 void P2PSocketDispatcher::OnFilterAdded(IPC::Sender* sender) {
75 DVLOG(1) << "P2PSocketDispatcher::OnFilterAdded()";
76 sender_ = sender;
79 void P2PSocketDispatcher::OnFilterRemoved() {
80 sender_ = NULL;
83 void P2PSocketDispatcher::OnChannelConnected(int32 peer_id) {
84 connected_ = true;
87 void P2PSocketDispatcher::OnChannelClosing() {
88 sender_ = NULL;
89 connected_ = false;
92 base::SingleThreadTaskRunner* P2PSocketDispatcher::task_runner() {
93 return ipc_task_runner_.get();
96 int P2PSocketDispatcher::RegisterClient(P2PSocketClientImpl* client) {
97 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
98 return clients_.Add(client);
101 void P2PSocketDispatcher::UnregisterClient(int id) {
102 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
103 clients_.Remove(id);
106 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) {
107 if (!ipc_task_runner_->BelongsToCurrentThread()) {
108 ipc_task_runner_->PostTask(
109 FROM_HERE, base::Bind(&P2PSocketDispatcher::Send, this, msg));
110 return;
112 Send(msg);
115 int P2PSocketDispatcher::RegisterHostAddressRequest(
116 P2PAsyncAddressResolver* request) {
117 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
118 return host_address_requests_.Add(request);
121 void P2PSocketDispatcher::UnregisterHostAddressRequest(int id) {
122 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
123 host_address_requests_.Remove(id);
126 void P2PSocketDispatcher::OnNetworkListChanged(
127 const net::NetworkInterfaceList& networks) {
128 network_list_observers_->Notify(
129 FROM_HERE, &NetworkListObserver::OnNetworkListChanged, networks);
132 void P2PSocketDispatcher::OnGetHostAddressResult(
133 int32 request_id,
134 const net::IPAddressList& addresses) {
135 P2PAsyncAddressResolver* request = host_address_requests_.Lookup(request_id);
136 if (!request) {
137 DVLOG(1) << "Received P2P message for socket that doesn't exist.";
138 return;
141 request->OnResponse(addresses);
144 void P2PSocketDispatcher::OnSocketCreated(
145 int socket_id,
146 const net::IPEndPoint& local_address,
147 const net::IPEndPoint& remote_address) {
148 P2PSocketClientImpl* client = GetClient(socket_id);
149 if (client) {
150 client->OnSocketCreated(local_address, remote_address);
154 void P2PSocketDispatcher::OnIncomingTcpConnection(
155 int socket_id, const net::IPEndPoint& address) {
156 P2PSocketClientImpl* client = GetClient(socket_id);
157 if (client) {
158 client->OnIncomingTcpConnection(address);
162 void P2PSocketDispatcher::OnSendComplete(
163 int socket_id,
164 const P2PSendPacketMetrics& send_metrics) {
165 P2PSocketClientImpl* client = GetClient(socket_id);
166 if (client) {
167 client->OnSendComplete(send_metrics);
171 void P2PSocketDispatcher::OnError(int socket_id) {
172 P2PSocketClientImpl* client = GetClient(socket_id);
173 if (client) {
174 client->OnError();
178 void P2PSocketDispatcher::OnDataReceived(
179 int socket_id, const net::IPEndPoint& address,
180 const std::vector<char>& data,
181 const base::TimeTicks& timestamp) {
182 P2PSocketClientImpl* client = GetClient(socket_id);
183 if (client) {
184 client->OnDataReceived(address, data, timestamp);
188 P2PSocketClientImpl* P2PSocketDispatcher::GetClient(int socket_id) {
189 P2PSocketClientImpl* client = clients_.Lookup(socket_id);
190 if (client == NULL) {
191 // This may happen if the socket was closed, but the browser side
192 // hasn't processed the close message by the time it sends the
193 // message to the renderer.
194 DVLOG(1) << "Received P2P message for socket that doesn't exist.";
195 return NULL;
198 return client;
201 } // namespace content