| 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/renderer/p2p/socket_dispatcher.h" | 5 #include "content/renderer/p2p/socket_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | |
| 10 #include "content/child/child_process.h" | 9 #include "content/child/child_process.h" |
| 11 #include "content/common/p2p_messages.h" | 10 #include "content/common/p2p_messages.h" |
| 12 #include "content/renderer/p2p/host_address_request.h" | 11 #include "content/renderer/p2p/host_address_request.h" |
| 13 #include "content/renderer/p2p/network_list_observer.h" | 12 #include "content/renderer/p2p/network_list_observer.h" |
| 14 #include "content/renderer/p2p/socket_client_impl.h" | 13 #include "content/renderer/p2p/socket_client_impl.h" |
| 15 #include "content/renderer/render_view_impl.h" | 14 #include "content/renderer/render_view_impl.h" |
| 16 #include "ipc/ipc_sender.h" | 15 #include "ipc/ipc_sender.h" |
| 17 | 16 |
| 18 namespace content { | 17 namespace content { |
| 19 | 18 |
| 20 P2PSocketDispatcher::P2PSocketDispatcher( | 19 P2PSocketDispatcher::P2PSocketDispatcher( |
| 21 base::MessageLoopProxy* ipc_message_loop) | 20 base::SingleThreadTaskRunner* ipc_task_runner) |
| 22 : message_loop_(ipc_message_loop), | 21 : ipc_task_runner_(ipc_task_runner), |
| 23 network_notifications_started_(false), | 22 network_notifications_started_(false), |
| 24 network_list_observers_( | 23 network_list_observers_( |
| 25 new ObserverListThreadSafe<NetworkListObserver>()), | 24 new ObserverListThreadSafe<NetworkListObserver>()), |
| 26 sender_(NULL) { | 25 sender_(NULL) { |
| 27 } | 26 } |
| 28 | 27 |
| 29 P2PSocketDispatcher::~P2PSocketDispatcher() { | 28 P2PSocketDispatcher::~P2PSocketDispatcher() { |
| 30 network_list_observers_->AssertEmpty(); | 29 network_list_observers_->AssertEmpty(); |
| 31 for (IDMap<P2PSocketClientImpl>::iterator i(&clients_); !i.IsAtEnd(); | 30 for (IDMap<P2PSocketClientImpl>::iterator i(&clients_); !i.IsAtEnd(); |
| 32 i.Advance()) { | 31 i.Advance()) { |
| 33 i.GetCurrentValue()->Detach(); | 32 i.GetCurrentValue()->Detach(); |
| 34 } | 33 } |
| 35 } | 34 } |
| 36 | 35 |
| 37 void P2PSocketDispatcher::AddNetworkListObserver( | 36 void P2PSocketDispatcher::AddNetworkListObserver( |
| 38 NetworkListObserver* network_list_observer) { | 37 NetworkListObserver* network_list_observer) { |
| 39 network_list_observers_->AddObserver(network_list_observer); | 38 network_list_observers_->AddObserver(network_list_observer); |
| 40 network_notifications_started_ = true; | 39 network_notifications_started_ = true; |
| 41 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications()); | 40 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications()); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void P2PSocketDispatcher::RemoveNetworkListObserver( | 43 void P2PSocketDispatcher::RemoveNetworkListObserver( |
| 45 NetworkListObserver* network_list_observer) { | 44 NetworkListObserver* network_list_observer) { |
| 46 network_list_observers_->RemoveObserver(network_list_observer); | 45 network_list_observers_->RemoveObserver(network_list_observer); |
| 47 } | 46 } |
| 48 | 47 |
| 49 void P2PSocketDispatcher::Send(IPC::Message* message) { | 48 void P2PSocketDispatcher::Send(IPC::Message* message) { |
| 50 DCHECK(message_loop_->BelongsToCurrentThread()); | 49 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 51 if (!sender_) { | 50 if (!sender_) { |
| 52 DLOG(WARNING) << "P2PSocketDispatcher::Send() - Sender closed."; | 51 DLOG(WARNING) << "P2PSocketDispatcher::Send() - Sender closed."; |
| 53 delete message; | 52 delete message; |
| 54 return; | 53 return; |
| 55 } | 54 } |
| 56 | 55 |
| 57 sender_->Send(message); | 56 sender_->Send(message); |
| 58 } | 57 } |
| 59 | 58 |
| 60 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) { | 59 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 78 } | 77 } |
| 79 | 78 |
| 80 void P2PSocketDispatcher::OnFilterRemoved() { | 79 void P2PSocketDispatcher::OnFilterRemoved() { |
| 81 sender_ = NULL; | 80 sender_ = NULL; |
| 82 } | 81 } |
| 83 | 82 |
| 84 void P2PSocketDispatcher::OnChannelClosing() { | 83 void P2PSocketDispatcher::OnChannelClosing() { |
| 85 sender_ = NULL; | 84 sender_ = NULL; |
| 86 } | 85 } |
| 87 | 86 |
| 88 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { | 87 base::SingleThreadTaskRunner* P2PSocketDispatcher::task_runner() { |
| 89 return message_loop_.get(); | 88 return ipc_task_runner_.get(); |
| 90 } | 89 } |
| 91 | 90 |
| 92 int P2PSocketDispatcher::RegisterClient(P2PSocketClientImpl* client) { | 91 int P2PSocketDispatcher::RegisterClient(P2PSocketClientImpl* client) { |
| 93 DCHECK(message_loop_->BelongsToCurrentThread()); | 92 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 94 return clients_.Add(client); | 93 return clients_.Add(client); |
| 95 } | 94 } |
| 96 | 95 |
| 97 void P2PSocketDispatcher::UnregisterClient(int id) { | 96 void P2PSocketDispatcher::UnregisterClient(int id) { |
| 98 DCHECK(message_loop_->BelongsToCurrentThread()); | 97 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 99 clients_.Remove(id); | 98 clients_.Remove(id); |
| 100 } | 99 } |
| 101 | 100 |
| 102 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { | 101 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { |
| 103 if (!message_loop_->BelongsToCurrentThread()) { | 102 if (!ipc_task_runner_->BelongsToCurrentThread()) { |
| 104 message_loop_->PostTask(FROM_HERE, | 103 ipc_task_runner_->PostTask( |
| 105 base::Bind(&P2PSocketDispatcher::Send, | 104 FROM_HERE, base::Bind(&P2PSocketDispatcher::Send, this, msg)); |
| 106 this, msg)); | |
| 107 return; | 105 return; |
| 108 } | 106 } |
| 109 Send(msg); | 107 Send(msg); |
| 110 } | 108 } |
| 111 | 109 |
| 112 int P2PSocketDispatcher::RegisterHostAddressRequest( | 110 int P2PSocketDispatcher::RegisterHostAddressRequest( |
| 113 P2PAsyncAddressResolver* request) { | 111 P2PAsyncAddressResolver* request) { |
| 114 DCHECK(message_loop_->BelongsToCurrentThread()); | 112 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 115 return host_address_requests_.Add(request); | 113 return host_address_requests_.Add(request); |
| 116 } | 114 } |
| 117 | 115 |
| 118 void P2PSocketDispatcher::UnregisterHostAddressRequest(int id) { | 116 void P2PSocketDispatcher::UnregisterHostAddressRequest(int id) { |
| 119 DCHECK(message_loop_->BelongsToCurrentThread()); | 117 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 120 host_address_requests_.Remove(id); | 118 host_address_requests_.Remove(id); |
| 121 } | 119 } |
| 122 | 120 |
| 123 void P2PSocketDispatcher::OnNetworkListChanged( | 121 void P2PSocketDispatcher::OnNetworkListChanged( |
| 124 const net::NetworkInterfaceList& networks) { | 122 const net::NetworkInterfaceList& networks) { |
| 125 network_list_observers_->Notify( | 123 network_list_observers_->Notify( |
| 126 FROM_HERE, &NetworkListObserver::OnNetworkListChanged, networks); | 124 FROM_HERE, &NetworkListObserver::OnNetworkListChanged, networks); |
| 127 } | 125 } |
| 128 | 126 |
| 129 void P2PSocketDispatcher::OnGetHostAddressResult( | 127 void P2PSocketDispatcher::OnGetHostAddressResult( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 // hasn't processed the close message by the time it sends the | 187 // hasn't processed the close message by the time it sends the |
| 190 // message to the renderer. | 188 // message to the renderer. |
| 191 DVLOG(1) << "Received P2P message for socket that doesn't exist."; | 189 DVLOG(1) << "Received P2P message for socket that doesn't exist."; |
| 192 return NULL; | 190 return NULL; |
| 193 } | 191 } |
| 194 | 192 |
| 195 return client; | 193 return client; |
| 196 } | 194 } |
| 197 | 195 |
| 198 } // namespace content | 196 } // namespace content |
| OLD | NEW |