| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_client.h" | 5 #include "content/renderer/p2p/socket_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "content/common/p2p_messages.h" | 9 #include "content/common/p2p_messages.h" |
| 10 #include "content/public/renderer/p2p_socket_client_delegate.h" |
| 10 #include "content/renderer/p2p/socket_dispatcher.h" | 11 #include "content/renderer/p2p/socket_dispatcher.h" |
| 12 #include "content/renderer/render_thread_impl.h" |
| 11 #include "crypto/random.h" | 13 #include "crypto/random.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { | 17 uint64 GetUniqueId(uint32 random_socket_id, uint32 packet_id) { |
| 16 uint64 uid = random_socket_id; | 18 uint64 uid = random_socket_id; |
| 17 uid <<= 32; | 19 uid <<= 32; |
| 18 uid |= packet_id; | 20 uid |= packet_id; |
| 19 return uid; | 21 return uid; |
| 20 } | 22 } |
| 21 | 23 |
| 22 } // namespace | 24 } // namespace |
| 23 | 25 |
| 24 namespace content { | 26 namespace content { |
| 25 | 27 |
| 26 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) | 28 P2PSocketClient* CreateP2PSocket() { |
| 29 return new P2PSocketClientImpl( |
| 30 RenderThreadImpl::current()->p2p_socket_dispatcher()); |
| 31 } |
| 32 |
| 33 P2PSocketClientImpl::P2PSocketClientImpl(P2PSocketDispatcher* dispatcher) |
| 27 : dispatcher_(dispatcher), | 34 : dispatcher_(dispatcher), |
| 28 ipc_message_loop_(dispatcher->message_loop()), | 35 ipc_message_loop_(dispatcher->message_loop()), |
| 29 delegate_message_loop_(base::MessageLoopProxy::current()), | 36 delegate_message_loop_(base::MessageLoopProxy::current()), |
| 30 socket_id_(0), delegate_(NULL), | 37 socket_id_(0), delegate_(NULL), |
| 31 state_(STATE_UNINITIALIZED), | 38 state_(STATE_UNINITIALIZED), |
| 32 random_socket_id_(0), | 39 random_socket_id_(0), |
| 33 next_packet_id_(0) { | 40 next_packet_id_(0) { |
| 34 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); | 41 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); |
| 35 } | 42 } |
| 36 | 43 |
| 37 P2PSocketClient::~P2PSocketClient() { | 44 P2PSocketClientImpl::~P2PSocketClientImpl() { |
| 38 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 45 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| 39 } | 46 } |
| 40 | 47 |
| 41 void P2PSocketClient::Init( | 48 void P2PSocketClientImpl::Init( |
| 42 P2PSocketType type, | 49 P2PSocketType type, |
| 43 const net::IPEndPoint& local_address, | 50 const net::IPEndPoint& local_address, |
| 44 const net::IPEndPoint& remote_address, | 51 const net::IPEndPoint& remote_address, |
| 45 P2PSocketClient::Delegate* delegate) { | 52 P2PSocketClientDelegate* delegate) { |
| 46 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 53 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 47 // |delegate_| is only accessesed on |delegate_message_loop_|. | 54 // |delegate_| is only accessesed on |delegate_message_loop_|. |
| 48 delegate_ = delegate; | 55 delegate_ = delegate; |
| 49 | 56 |
| 50 ipc_message_loop_->PostTask( | 57 ipc_message_loop_->PostTask( |
| 51 FROM_HERE, base::Bind(&P2PSocketClient::DoInit, this, type, local_address, | 58 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, |
| 59 this, |
| 60 type, |
| 61 local_address, |
| 52 remote_address)); | 62 remote_address)); |
| 53 } | 63 } |
| 54 | 64 |
| 55 void P2PSocketClient::DoInit(P2PSocketType type, | 65 void P2PSocketClientImpl::DoInit(P2PSocketType type, |
| 56 const net::IPEndPoint& local_address, | 66 const net::IPEndPoint& local_address, |
| 57 const net::IPEndPoint& remote_address) { | 67 const net::IPEndPoint& remote_address) { |
| 58 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 68 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 59 DCHECK(delegate_); | 69 DCHECK(delegate_); |
| 60 state_ = STATE_OPENING; | 70 state_ = STATE_OPENING; |
| 61 socket_id_ = dispatcher_->RegisterClient(this); | 71 socket_id_ = dispatcher_->RegisterClient(this); |
| 62 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 72 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 63 type, socket_id_, local_address, remote_address)); | 73 type, socket_id_, local_address, remote_address)); |
| 64 } | 74 } |
| 65 | 75 |
| 66 void P2PSocketClient::SendWithDscp( | 76 void P2PSocketClientImpl::SendWithDscp( |
| 67 const net::IPEndPoint& address, | 77 const net::IPEndPoint& address, |
| 68 const std::vector<char>& data, | 78 const std::vector<char>& data, |
| 69 net::DiffServCodePoint dscp) { | 79 net::DiffServCodePoint dscp) { |
| 70 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 80 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
| 71 ipc_message_loop_->PostTask( | 81 ipc_message_loop_->PostTask( |
| 72 FROM_HERE, base::Bind( | 82 FROM_HERE, base::Bind( |
| 73 &P2PSocketClient::SendWithDscp, this, address, data, dscp)); | 83 &P2PSocketClientImpl::SendWithDscp, this, address, data, dscp)); |
| 74 return; | 84 return; |
| 75 } | 85 } |
| 76 | 86 |
| 77 // Can send data only when the socket is open. | 87 // Can send data only when the socket is open. |
| 78 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); | 88 DCHECK(state_ == STATE_OPEN || state_ == STATE_ERROR); |
| 79 if (state_ == STATE_OPEN) { | 89 if (state_ == STATE_OPEN) { |
| 80 uint64 unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); | 90 uint64 unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); |
| 81 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", unique_id); | 91 TRACE_EVENT_ASYNC_BEGIN0("p2p", "Send", unique_id); |
| 82 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data, | 92 dispatcher_->SendP2PMessage(new P2PHostMsg_Send(socket_id_, address, data, |
| 83 dscp, unique_id)); | 93 dscp, unique_id)); |
| 84 } | 94 } |
| 85 } | 95 } |
| 86 | 96 |
| 87 void P2PSocketClient::Send(const net::IPEndPoint& address, | 97 void P2PSocketClientImpl::Send(const net::IPEndPoint& address, |
| 88 const std::vector<char>& data) { | 98 const std::vector<char>& data) { |
| 89 SendWithDscp(address, data, net::DSCP_DEFAULT); | 99 SendWithDscp(address, data, net::DSCP_DEFAULT); |
| 90 } | 100 } |
| 91 | 101 |
| 92 void P2PSocketClient::Close() { | 102 void P2PSocketClientImpl::Close() { |
| 93 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 103 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 94 | 104 |
| 95 delegate_ = NULL; | 105 delegate_ = NULL; |
| 96 | 106 |
| 97 ipc_message_loop_->PostTask( | 107 ipc_message_loop_->PostTask( |
| 98 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); | 108 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoClose, this)); |
| 99 } | 109 } |
| 100 | 110 |
| 101 void P2PSocketClient::DoClose() { | 111 void P2PSocketClientImpl::DoClose() { |
| 102 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 112 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 103 if (dispatcher_) { | 113 if (dispatcher_) { |
| 104 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | 114 if (state_ == STATE_OPEN || state_ == STATE_OPENING || |
| 105 state_ == STATE_ERROR) { | 115 state_ == STATE_ERROR) { |
| 106 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); | 116 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); |
| 107 } | 117 } |
| 108 dispatcher_->UnregisterClient(socket_id_); | 118 dispatcher_->UnregisterClient(socket_id_); |
| 109 } | 119 } |
| 110 | 120 |
| 111 state_ = STATE_CLOSED; | 121 state_ = STATE_CLOSED; |
| 112 } | 122 } |
| 113 | 123 |
| 114 void P2PSocketClient::set_delegate(Delegate* delegate) { | 124 int P2PSocketClientImpl::GetSocketID() const { |
| 125 return socket_id_; |
| 126 } |
| 127 |
| 128 void P2PSocketClientImpl::SetDelegate(P2PSocketClientDelegate* delegate) { |
| 115 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 129 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 116 delegate_ = delegate; | 130 delegate_ = delegate; |
| 117 } | 131 } |
| 118 | 132 |
| 119 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { | 133 void P2PSocketClientImpl::OnSocketCreated(const net::IPEndPoint& address) { |
| 120 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 134 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 121 DCHECK_EQ(state_, STATE_OPENING); | 135 DCHECK_EQ(state_, STATE_OPENING); |
| 122 state_ = STATE_OPEN; | 136 state_ = STATE_OPEN; |
| 123 | 137 |
| 124 delegate_message_loop_->PostTask( | 138 delegate_message_loop_->PostTask( |
| 125 FROM_HERE, | 139 FROM_HERE, |
| 126 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); | 140 base::Bind(&P2PSocketClientImpl::DeliverOnSocketCreated, this, address)); |
| 127 } | 141 } |
| 128 | 142 |
| 129 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { | 143 void P2PSocketClientImpl::DeliverOnSocketCreated( |
| 144 const net::IPEndPoint& address) { |
| 130 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 145 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 131 if (delegate_) | 146 if (delegate_) |
| 132 delegate_->OnOpen(address); | 147 delegate_->OnOpen(address); |
| 133 } | 148 } |
| 134 | 149 |
| 135 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { | 150 void P2PSocketClientImpl::OnIncomingTcpConnection( |
| 151 const net::IPEndPoint& address) { |
| 136 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 152 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 137 DCHECK_EQ(state_, STATE_OPEN); | 153 DCHECK_EQ(state_, STATE_OPEN); |
| 138 | 154 |
| 139 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); | 155 scoped_refptr<P2PSocketClientImpl> new_client = |
| 156 new P2PSocketClientImpl(dispatcher_); |
| 140 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get()); | 157 new_client->socket_id_ = dispatcher_->RegisterClient(new_client.get()); |
| 141 new_client->state_ = STATE_OPEN; | 158 new_client->state_ = STATE_OPEN; |
| 142 new_client->delegate_message_loop_ = delegate_message_loop_; | 159 new_client->delegate_message_loop_ = delegate_message_loop_; |
| 143 | 160 |
| 144 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | 161 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( |
| 145 socket_id_, address, new_client->socket_id_)); | 162 socket_id_, address, new_client->socket_id_)); |
| 146 | 163 |
| 147 delegate_message_loop_->PostTask( | 164 delegate_message_loop_->PostTask( |
| 148 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, | 165 FROM_HERE, base::Bind( |
| 149 this, address, new_client)); | 166 &P2PSocketClientImpl::DeliverOnIncomingTcpConnection, |
| 167 this, address, new_client)); |
| 150 } | 168 } |
| 151 | 169 |
| 152 void P2PSocketClient::DeliverOnIncomingTcpConnection( | 170 void P2PSocketClientImpl::DeliverOnIncomingTcpConnection( |
| 153 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { | 171 const net::IPEndPoint& address, |
| 172 scoped_refptr<P2PSocketClient> new_client) { |
| 154 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 173 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 155 if (delegate_) { | 174 if (delegate_) { |
| 156 delegate_->OnIncomingTcpConnection(address, new_client.get()); | 175 delegate_->OnIncomingTcpConnection(address, new_client.get()); |
| 157 } else { | 176 } else { |
| 158 // Just close the socket if there is no delegate to accept it. | 177 // Just close the socket if there is no delegate to accept it. |
| 159 new_client->Close(); | 178 new_client->Close(); |
| 160 } | 179 } |
| 161 } | 180 } |
| 162 | 181 |
| 163 void P2PSocketClient::OnSendComplete() { | 182 void P2PSocketClientImpl::OnSendComplete() { |
| 164 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 183 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 165 | 184 |
| 166 delegate_message_loop_->PostTask( | 185 delegate_message_loop_->PostTask( |
| 167 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnSendComplete, this)); | 186 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnSendComplete, this)); |
| 168 } | 187 } |
| 169 | 188 |
| 170 void P2PSocketClient::DeliverOnSendComplete() { | 189 void P2PSocketClientImpl::DeliverOnSendComplete() { |
| 171 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 190 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 172 if (delegate_) | 191 if (delegate_) |
| 173 delegate_->OnSendComplete(); | 192 delegate_->OnSendComplete(); |
| 174 } | 193 } |
| 175 | 194 |
| 176 void P2PSocketClient::OnError() { | 195 void P2PSocketClientImpl::OnError() { |
| 177 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 196 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 178 state_ = STATE_ERROR; | 197 state_ = STATE_ERROR; |
| 179 | 198 |
| 180 delegate_message_loop_->PostTask( | 199 delegate_message_loop_->PostTask( |
| 181 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); | 200 FROM_HERE, base::Bind(&P2PSocketClientImpl::DeliverOnError, this)); |
| 182 } | 201 } |
| 183 | 202 |
| 184 void P2PSocketClient::DeliverOnError() { | 203 void P2PSocketClientImpl::DeliverOnError() { |
| 185 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 204 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 186 if (delegate_) | 205 if (delegate_) |
| 187 delegate_->OnError(); | 206 delegate_->OnError(); |
| 188 } | 207 } |
| 189 | 208 |
| 190 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, | 209 void P2PSocketClientImpl::OnDataReceived(const net::IPEndPoint& address, |
| 191 const std::vector<char>& data) { | 210 const std::vector<char>& data) { |
| 192 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 211 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 193 DCHECK_EQ(STATE_OPEN, state_); | 212 DCHECK_EQ(STATE_OPEN, state_); |
| 194 delegate_message_loop_->PostTask( | 213 delegate_message_loop_->PostTask( |
| 195 FROM_HERE, | 214 FROM_HERE, |
| 196 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); | 215 base::Bind(&P2PSocketClientImpl::DeliverOnDataReceived, |
| 216 this, |
| 217 address, |
| 218 data)); |
| 197 } | 219 } |
| 198 | 220 |
| 199 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, | 221 void P2PSocketClientImpl::DeliverOnDataReceived(const net::IPEndPoint& address, |
| 200 const std::vector<char>& data) { | 222 const std::vector<char>& data) { |
| 201 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 223 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 202 if (delegate_) | 224 if (delegate_) |
| 203 delegate_->OnDataReceived(address, data); | 225 delegate_->OnDataReceived(address, data); |
| 204 } | 226 } |
| 205 | 227 |
| 206 void P2PSocketClient::Detach() { | 228 void P2PSocketClientImpl::Detach() { |
| 207 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 229 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
| 208 dispatcher_ = NULL; | 230 dispatcher_ = NULL; |
| 209 OnError(); | 231 OnError(); |
| 210 } | 232 } |
| 211 | 233 |
| 212 } // namespace content | 234 } // namespace content |
| OLD | NEW |