| 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_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "content/common/child_process.h" |
| 10 #include "content/common/p2p_messages.h" | 11 #include "content/common/p2p_messages.h" |
| 11 #include "content/renderer/p2p/host_address_request.h" | 12 #include "content/renderer/p2p/host_address_request.h" |
| 12 #include "content/renderer/p2p/socket_client.h" | 13 #include "content/renderer/p2p/socket_client.h" |
| 13 #include "content/renderer/render_view_impl.h" | 14 #include "content/renderer/render_view_impl.h" |
| 14 #include "webkit/glue/network_list_observer.h" | 15 #include "webkit/glue/network_list_observer.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 class P2PSocketDispatcher::AsyncMessageSender | 19 P2PSocketDispatcher::P2PSocketDispatcher( |
| 19 : public base::RefCountedThreadSafe<AsyncMessageSender> { | 20 base::MessageLoopProxy* ipc_message_loop) |
| 20 public: | 21 : message_loop_(ipc_message_loop), |
| 21 explicit AsyncMessageSender(IPC::Sender* message_sender) | |
| 22 : message_loop_(base::MessageLoopProxy::current()), | |
| 23 message_sender_(message_sender) { | |
| 24 } | |
| 25 | |
| 26 void Detach() { | |
| 27 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 28 message_sender_ = NULL; | |
| 29 } | |
| 30 | |
| 31 void Send(IPC::Message* msg) { | |
| 32 message_loop_->PostTask(FROM_HERE, | |
| 33 base::Bind(&AsyncMessageSender::DoSend, this, msg)); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 friend class base::RefCountedThreadSafe<AsyncMessageSender>; | |
| 38 ~AsyncMessageSender() {} | |
| 39 | |
| 40 void DoSend(IPC::Message* msg) { | |
| 41 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 42 if (message_sender_) | |
| 43 message_sender_->Send(msg); | |
| 44 } | |
| 45 | |
| 46 scoped_refptr<base::MessageLoopProxy> message_loop_; | |
| 47 IPC::Sender* message_sender_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(AsyncMessageSender); | |
| 50 }; | |
| 51 | |
| 52 P2PSocketDispatcher::P2PSocketDispatcher(RenderViewImpl* render_view) | |
| 53 : content::RenderViewObserver(render_view), | |
| 54 message_loop_(base::MessageLoopProxy::current()), | |
| 55 network_notifications_started_(false), | 22 network_notifications_started_(false), |
| 56 network_list_observers_( | 23 network_list_observers_( |
| 57 new ObserverListThreadSafe<webkit_glue::NetworkListObserver>()), | 24 new ObserverListThreadSafe<webkit_glue::NetworkListObserver>()), |
| 58 async_message_sender_(new AsyncMessageSender(this)) { | 25 channel_(NULL) { |
| 59 } | 26 } |
| 60 | 27 |
| 61 P2PSocketDispatcher::~P2PSocketDispatcher() { | 28 P2PSocketDispatcher::~P2PSocketDispatcher() { |
| 62 FOR_EACH_OBSERVER(P2PSocketDispatcherDestructionObserver, | |
| 63 destruction_observer_, OnSocketDispatcherDestroyed()); | |
| 64 network_list_observers_->AssertEmpty(); | 29 network_list_observers_->AssertEmpty(); |
| 65 if (network_notifications_started_) | |
| 66 Send(new P2PHostMsg_StopNetworkNotifications(routing_id())); | |
| 67 for (IDMap<P2PSocketClient>::iterator i(&clients_); !i.IsAtEnd(); | 30 for (IDMap<P2PSocketClient>::iterator i(&clients_); !i.IsAtEnd(); |
| 68 i.Advance()) { | 31 i.Advance()) { |
| 69 i.GetCurrentValue()->Detach(); | 32 i.GetCurrentValue()->Detach(); |
| 70 } | 33 } |
| 71 async_message_sender_->Detach(); | |
| 72 } | 34 } |
| 73 | 35 |
| 74 void P2PSocketDispatcher::AddNetworkListObserver( | 36 void P2PSocketDispatcher::AddNetworkListObserver( |
| 75 webkit_glue::NetworkListObserver* network_list_observer) { | 37 webkit_glue::NetworkListObserver* network_list_observer) { |
| 76 network_list_observers_->AddObserver(network_list_observer); | 38 network_list_observers_->AddObserver(network_list_observer); |
| 77 network_notifications_started_ = true; | 39 network_notifications_started_ = true; |
| 78 async_message_sender_->Send( | 40 SendP2PMessage(new P2PHostMsg_StartNetworkNotifications()); |
| 79 new P2PHostMsg_StartNetworkNotifications(routing_id())); | |
| 80 } | 41 } |
| 81 | 42 |
| 82 void P2PSocketDispatcher::RemoveNetworkListObserver( | 43 void P2PSocketDispatcher::RemoveNetworkListObserver( |
| 83 webkit_glue::NetworkListObserver* network_list_observer) { | 44 webkit_glue::NetworkListObserver* network_list_observer) { |
| 84 network_list_observers_->RemoveObserver(network_list_observer); | 45 network_list_observers_->RemoveObserver(network_list_observer); |
| 85 } | 46 } |
| 86 | 47 |
| 87 void P2PSocketDispatcher::AddDestructionObserver( | 48 void P2PSocketDispatcher::Send(IPC::Message* message) { |
| 88 P2PSocketDispatcherDestructionObserver* observer) { | 49 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 89 destruction_observer_.AddObserver(observer); | 50 if (!channel_) { |
| 90 } | 51 DLOG(WARNING) << "P2PSocketDispatcher::Send() - Channel closed."; |
| 52 delete message; |
| 53 return; |
| 54 } |
| 91 | 55 |
| 92 void P2PSocketDispatcher::RemoveDestructionObserver( | 56 channel_->Send(message); |
| 93 P2PSocketDispatcherDestructionObserver* observer) { | |
| 94 destruction_observer_.RemoveObserver(observer); | |
| 95 } | 57 } |
| 96 | 58 |
| 97 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) { | 59 bool P2PSocketDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 98 bool handled = true; | 60 bool handled = true; |
| 99 IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher, message) | 61 IPC_BEGIN_MESSAGE_MAP(P2PSocketDispatcher, message) |
| 100 IPC_MESSAGE_HANDLER(P2PMsg_NetworkListChanged, OnNetworkListChanged) | 62 IPC_MESSAGE_HANDLER(P2PMsg_NetworkListChanged, OnNetworkListChanged) |
| 101 IPC_MESSAGE_HANDLER(P2PMsg_GetHostAddressResult, OnGetHostAddressResult) | 63 IPC_MESSAGE_HANDLER(P2PMsg_GetHostAddressResult, OnGetHostAddressResult) |
| 102 IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated, OnSocketCreated) | 64 IPC_MESSAGE_HANDLER(P2PMsg_OnSocketCreated, OnSocketCreated) |
| 103 IPC_MESSAGE_HANDLER(P2PMsg_OnIncomingTcpConnection, OnIncomingTcpConnection) | 65 IPC_MESSAGE_HANDLER(P2PMsg_OnIncomingTcpConnection, OnIncomingTcpConnection) |
| 104 IPC_MESSAGE_HANDLER(P2PMsg_OnError, OnError) | 66 IPC_MESSAGE_HANDLER(P2PMsg_OnError, OnError) |
| 105 IPC_MESSAGE_HANDLER(P2PMsg_OnDataReceived, OnDataReceived) | 67 IPC_MESSAGE_HANDLER(P2PMsg_OnDataReceived, OnDataReceived) |
| 106 IPC_MESSAGE_UNHANDLED(handled = false) | 68 IPC_MESSAGE_UNHANDLED(handled = false) |
| 107 IPC_END_MESSAGE_MAP() | 69 IPC_END_MESSAGE_MAP() |
| 108 return handled; | 70 return handled; |
| 109 } | 71 } |
| 110 | 72 |
| 73 void P2PSocketDispatcher::OnFilterAdded(IPC::Channel* channel) { |
| 74 DVLOG(1) << "P2PSocketDispatcher::OnFilterAdded()"; |
| 75 channel_ = channel; |
| 76 } |
| 77 |
| 78 void P2PSocketDispatcher::OnFilterRemoved() { |
| 79 channel_ = NULL; |
| 80 } |
| 81 |
| 82 void P2PSocketDispatcher::OnChannelClosing() { |
| 83 channel_ = NULL; |
| 84 } |
| 85 |
| 111 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { | 86 base::MessageLoopProxy* P2PSocketDispatcher::message_loop() { |
| 112 return message_loop_; | 87 return message_loop_; |
| 113 } | 88 } |
| 114 | 89 |
| 115 int P2PSocketDispatcher::RegisterClient(P2PSocketClient* client) { | 90 int P2PSocketDispatcher::RegisterClient(P2PSocketClient* client) { |
| 91 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 116 return clients_.Add(client); | 92 return clients_.Add(client); |
| 117 } | 93 } |
| 118 | 94 |
| 119 void P2PSocketDispatcher::UnregisterClient(int id) { | 95 void P2PSocketDispatcher::UnregisterClient(int id) { |
| 96 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 120 clients_.Remove(id); | 97 clients_.Remove(id); |
| 121 } | 98 } |
| 122 | 99 |
| 123 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { | 100 void P2PSocketDispatcher::SendP2PMessage(IPC::Message* msg) { |
| 124 msg->set_routing_id(routing_id()); | 101 if (!message_loop_->BelongsToCurrentThread()) { |
| 102 message_loop_->PostTask(FROM_HERE, |
| 103 base::Bind(&P2PSocketDispatcher::Send, |
| 104 this, msg)); |
| 105 return; |
| 106 } |
| 125 Send(msg); | 107 Send(msg); |
| 126 } | 108 } |
| 127 | 109 |
| 128 int P2PSocketDispatcher::RegisterHostAddressRequest( | 110 int P2PSocketDispatcher::RegisterHostAddressRequest( |
| 129 P2PHostAddressRequest* request) { | 111 P2PHostAddressRequest* request) { |
| 112 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 130 return host_address_requests_.Add(request); | 113 return host_address_requests_.Add(request); |
| 131 } | 114 } |
| 132 | 115 |
| 133 void P2PSocketDispatcher::UnregisterHostAddressRequest(int id) { | 116 void P2PSocketDispatcher::UnregisterHostAddressRequest(int id) { |
| 117 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 134 host_address_requests_.Remove(id); | 118 host_address_requests_.Remove(id); |
| 135 } | 119 } |
| 136 | 120 |
| 137 void P2PSocketDispatcher::OnNetworkListChanged( | 121 void P2PSocketDispatcher::OnNetworkListChanged( |
| 138 const net::NetworkInterfaceList& networks) { | 122 const net::NetworkInterfaceList& networks) { |
| 139 network_list_observers_->Notify( | 123 network_list_observers_->Notify( |
| 140 &webkit_glue::NetworkListObserver::OnNetworkListChanged, networks); | 124 &webkit_glue::NetworkListObserver::OnNetworkListChanged, networks); |
| 141 } | 125 } |
| 142 | 126 |
| 143 void P2PSocketDispatcher::OnGetHostAddressResult( | 127 void P2PSocketDispatcher::OnGetHostAddressResult( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 // hasn't processed the close message by the time it sends the | 175 // hasn't processed the close message by the time it sends the |
| 192 // message to the renderer. | 176 // message to the renderer. |
| 193 VLOG(1) << "Received P2P message for socket that doesn't exist."; | 177 VLOG(1) << "Received P2P message for socket that doesn't exist."; |
| 194 return NULL; | 178 return NULL; |
| 195 } | 179 } |
| 196 | 180 |
| 197 return client; | 181 return client; |
| 198 } | 182 } |
| 199 | 183 |
| 200 } // namespace content | 184 } // namespace content |
| OLD | NEW |