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/ipc_socket_factory.h" | 5 #include "content/renderer/p2p/ipc_socket_factory.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 IS_ERROR, | 66 IS_ERROR, |
67 }; | 67 }; |
68 | 68 |
69 void InitAcceptedTcp(P2PSocketClient* client, | 69 void InitAcceptedTcp(P2PSocketClient* client, |
70 const talk_base::SocketAddress& local_address, | 70 const talk_base::SocketAddress& local_address, |
71 const talk_base::SocketAddress& remote_address); | 71 const talk_base::SocketAddress& remote_address); |
72 | 72 |
73 P2PSocketType type_; | 73 P2PSocketType type_; |
74 | 74 |
75 // Message loop on which this socket was created and being used. | 75 // Message loop on which this socket was created and being used. |
76 MessageLoop* message_loop_; | 76 base::MessageLoop* message_loop_; |
77 | 77 |
78 // Corresponding P2P socket client. | 78 // Corresponding P2P socket client. |
79 scoped_refptr<P2PSocketClient> client_; | 79 scoped_refptr<P2PSocketClient> client_; |
80 | 80 |
81 // Local address is allocated by the browser process, and the | 81 // Local address is allocated by the browser process, and the |
82 // renderer side doesn't know the address until it receives OnOpen() | 82 // renderer side doesn't know the address until it receives OnOpen() |
83 // event from the browser. | 83 // event from the browser. |
84 talk_base::SocketAddress local_address_; | 84 talk_base::SocketAddress local_address_; |
85 | 85 |
86 // Remote address for client TCP connections. | 86 // Remote address for client TCP connections. |
(...skipping 11 matching lines...) Expand all Loading... |
98 bool writable_signal_expected_; | 98 bool writable_signal_expected_; |
99 | 99 |
100 // Current error code. Valid when state_ == IS_ERROR. | 100 // Current error code. Valid when state_ == IS_ERROR. |
101 int error_; | 101 int error_; |
102 | 102 |
103 DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket); | 103 DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket); |
104 }; | 104 }; |
105 | 105 |
106 IpcPacketSocket::IpcPacketSocket() | 106 IpcPacketSocket::IpcPacketSocket() |
107 : type_(P2P_SOCKET_UDP), | 107 : type_(P2P_SOCKET_UDP), |
108 message_loop_(MessageLoop::current()), | 108 message_loop_(base::MessageLoop::current()), |
109 state_(IS_UNINITIALIZED), | 109 state_(IS_UNINITIALIZED), |
110 send_packets_pending_(0), | 110 send_packets_pending_(0), |
111 writable_signal_expected_(false), | 111 writable_signal_expected_(false), |
112 error_(0) { | 112 error_(0) {} |
113 } | |
114 | 113 |
115 IpcPacketSocket::~IpcPacketSocket() { | 114 IpcPacketSocket::~IpcPacketSocket() { |
116 if (state_ == IS_OPENING || state_ == IS_OPEN || | 115 if (state_ == IS_OPENING || state_ == IS_OPEN || |
117 state_ == IS_ERROR) { | 116 state_ == IS_ERROR) { |
118 Close(); | 117 Close(); |
119 } | 118 } |
120 } | 119 } |
121 | 120 |
122 bool IpcPacketSocket::Init(P2PSocketType type, P2PSocketClient* client, | 121 bool IpcPacketSocket::Init(P2PSocketType type, P2PSocketClient* client, |
123 const talk_base::SocketAddress& local_address, | 122 const talk_base::SocketAddress& local_address, |
124 const talk_base::SocketAddress& remote_address) { | 123 const talk_base::SocketAddress& remote_address) { |
125 DCHECK_EQ(MessageLoop::current(), message_loop_); | 124 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
126 DCHECK_EQ(state_, IS_UNINITIALIZED); | 125 DCHECK_EQ(state_, IS_UNINITIALIZED); |
127 | 126 |
128 type_ = type; | 127 type_ = type; |
129 client_ = client; | 128 client_ = client; |
130 local_address_ = local_address; | 129 local_address_ = local_address; |
131 remote_address_ = remote_address; | 130 remote_address_ = remote_address; |
132 state_ = IS_OPENING; | 131 state_ = IS_OPENING; |
133 | 132 |
134 net::IPEndPoint local_endpoint; | 133 net::IPEndPoint local_endpoint; |
135 if (!jingle_glue::SocketAddressToIPEndPoint(local_address, &local_endpoint)) { | 134 if (!jingle_glue::SocketAddressToIPEndPoint(local_address, &local_endpoint)) { |
136 return false; | 135 return false; |
137 } | 136 } |
138 | 137 |
139 net::IPEndPoint remote_endpoint; | 138 net::IPEndPoint remote_endpoint; |
140 if (!jingle_glue::SocketAddressToIPEndPoint( | 139 if (!jingle_glue::SocketAddressToIPEndPoint( |
141 remote_address, &remote_endpoint)) { | 140 remote_address, &remote_endpoint)) { |
142 return false; | 141 return false; |
143 } | 142 } |
144 | 143 |
145 client_->Init(type, local_endpoint, remote_endpoint, this); | 144 client_->Init(type, local_endpoint, remote_endpoint, this); |
146 | 145 |
147 return true; | 146 return true; |
148 } | 147 } |
149 | 148 |
150 void IpcPacketSocket::InitAcceptedTcp( | 149 void IpcPacketSocket::InitAcceptedTcp( |
151 P2PSocketClient* client, | 150 P2PSocketClient* client, |
152 const talk_base::SocketAddress& local_address, | 151 const talk_base::SocketAddress& local_address, |
153 const talk_base::SocketAddress& remote_address) { | 152 const talk_base::SocketAddress& remote_address) { |
154 DCHECK_EQ(MessageLoop::current(), message_loop_); | 153 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
155 DCHECK_EQ(state_, IS_UNINITIALIZED); | 154 DCHECK_EQ(state_, IS_UNINITIALIZED); |
156 | 155 |
157 client_ = client; | 156 client_ = client; |
158 local_address_ = local_address; | 157 local_address_ = local_address; |
159 remote_address_ = remote_address; | 158 remote_address_ = remote_address; |
160 state_ = IS_OPEN; | 159 state_ = IS_OPEN; |
161 client_->set_delegate(this); | 160 client_->set_delegate(this); |
162 } | 161 } |
163 | 162 |
164 // talk_base::AsyncPacketSocket interface. | 163 // talk_base::AsyncPacketSocket interface. |
165 talk_base::SocketAddress IpcPacketSocket::GetLocalAddress() const { | 164 talk_base::SocketAddress IpcPacketSocket::GetLocalAddress() const { |
166 DCHECK_EQ(MessageLoop::current(), message_loop_); | 165 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
167 return local_address_; | 166 return local_address_; |
168 } | 167 } |
169 | 168 |
170 talk_base::SocketAddress IpcPacketSocket::GetRemoteAddress() const { | 169 talk_base::SocketAddress IpcPacketSocket::GetRemoteAddress() const { |
171 DCHECK_EQ(MessageLoop::current(), message_loop_); | 170 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
172 return remote_address_; | 171 return remote_address_; |
173 } | 172 } |
174 | 173 |
175 int IpcPacketSocket::Send(const void *data, size_t data_size) { | 174 int IpcPacketSocket::Send(const void *data, size_t data_size) { |
176 DCHECK_EQ(MessageLoop::current(), message_loop_); | 175 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
177 return SendTo(data, data_size, remote_address_); | 176 return SendTo(data, data_size, remote_address_); |
178 } | 177 } |
179 | 178 |
180 int IpcPacketSocket::SendTo(const void *data, size_t data_size, | 179 int IpcPacketSocket::SendTo(const void *data, size_t data_size, |
181 const talk_base::SocketAddress& address) { | 180 const talk_base::SocketAddress& address) { |
182 DCHECK_EQ(MessageLoop::current(), message_loop_); | 181 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
183 | 182 |
184 switch (state_) { | 183 switch (state_) { |
185 case IS_UNINITIALIZED: | 184 case IS_UNINITIALIZED: |
186 NOTREACHED(); | 185 NOTREACHED(); |
187 return EWOULDBLOCK; | 186 return EWOULDBLOCK; |
188 case IS_OPENING: | 187 case IS_OPENING: |
189 return EWOULDBLOCK; | 188 return EWOULDBLOCK; |
190 case IS_CLOSED: | 189 case IS_CLOSED: |
191 return ENOTCONN; | 190 return ENOTCONN; |
192 case IS_ERROR: | 191 case IS_ERROR: |
(...skipping 21 matching lines...) Expand all Loading... |
214 } | 213 } |
215 | 214 |
216 ++send_packets_pending_; | 215 ++send_packets_pending_; |
217 client_->Send(address_chrome, data_vector); | 216 client_->Send(address_chrome, data_vector); |
218 | 217 |
219 // Fake successful send. The caller ignores result anyway. | 218 // Fake successful send. The caller ignores result anyway. |
220 return data_size; | 219 return data_size; |
221 } | 220 } |
222 | 221 |
223 int IpcPacketSocket::Close() { | 222 int IpcPacketSocket::Close() { |
224 DCHECK_EQ(MessageLoop::current(), message_loop_); | 223 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
225 | 224 |
226 client_->Close(); | 225 client_->Close(); |
227 state_ = IS_CLOSED; | 226 state_ = IS_CLOSED; |
228 | 227 |
229 return 0; | 228 return 0; |
230 } | 229 } |
231 | 230 |
232 talk_base::AsyncPacketSocket::State IpcPacketSocket::GetState() const { | 231 talk_base::AsyncPacketSocket::State IpcPacketSocket::GetState() const { |
233 DCHECK_EQ(MessageLoop::current(), message_loop_); | 232 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
234 | 233 |
235 switch (state_) { | 234 switch (state_) { |
236 case IS_UNINITIALIZED: | 235 case IS_UNINITIALIZED: |
237 NOTREACHED(); | 236 NOTREACHED(); |
238 return STATE_CLOSED; | 237 return STATE_CLOSED; |
239 | 238 |
240 case IS_OPENING: | 239 case IS_OPENING: |
241 return STATE_BINDING; | 240 return STATE_BINDING; |
242 | 241 |
243 case IS_OPEN: | 242 case IS_OPEN: |
(...skipping 16 matching lines...) Expand all Loading... |
260 // We don't support socket options for IPC sockets. | 259 // We don't support socket options for IPC sockets. |
261 return -1; | 260 return -1; |
262 } | 261 } |
263 | 262 |
264 int IpcPacketSocket::SetOption(talk_base::Socket::Option opt, int value) { | 263 int IpcPacketSocket::SetOption(talk_base::Socket::Option opt, int value) { |
265 // We don't support socket options for IPC sockets. | 264 // We don't support socket options for IPC sockets. |
266 return -1; | 265 return -1; |
267 } | 266 } |
268 | 267 |
269 int IpcPacketSocket::GetError() const { | 268 int IpcPacketSocket::GetError() const { |
270 DCHECK_EQ(MessageLoop::current(), message_loop_); | 269 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
271 return error_; | 270 return error_; |
272 } | 271 } |
273 | 272 |
274 void IpcPacketSocket::SetError(int error) { | 273 void IpcPacketSocket::SetError(int error) { |
275 DCHECK_EQ(MessageLoop::current(), message_loop_); | 274 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
276 error_ = error; | 275 error_ = error; |
277 } | 276 } |
278 | 277 |
279 void IpcPacketSocket::OnOpen(const net::IPEndPoint& address) { | 278 void IpcPacketSocket::OnOpen(const net::IPEndPoint& address) { |
280 DCHECK_EQ(MessageLoop::current(), message_loop_); | 279 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
281 | 280 |
282 if (!jingle_glue::IPEndPointToSocketAddress(address, &local_address_)) { | 281 if (!jingle_glue::IPEndPointToSocketAddress(address, &local_address_)) { |
283 // Always expect correct IPv4 address to be allocated. | 282 // Always expect correct IPv4 address to be allocated. |
284 NOTREACHED(); | 283 NOTREACHED(); |
285 OnError(); | 284 OnError(); |
286 return; | 285 return; |
287 } | 286 } |
288 | 287 |
289 state_ = IS_OPEN; | 288 state_ = IS_OPEN; |
290 | 289 |
291 SignalAddressReady(this, local_address_); | 290 SignalAddressReady(this, local_address_); |
292 if (type_ == P2P_SOCKET_TCP_CLIENT) | 291 if (type_ == P2P_SOCKET_TCP_CLIENT) |
293 SignalConnect(this); | 292 SignalConnect(this); |
294 } | 293 } |
295 | 294 |
296 void IpcPacketSocket::OnIncomingTcpConnection( | 295 void IpcPacketSocket::OnIncomingTcpConnection( |
297 const net::IPEndPoint& address, | 296 const net::IPEndPoint& address, |
298 P2PSocketClient* client) { | 297 P2PSocketClient* client) { |
299 DCHECK_EQ(MessageLoop::current(), message_loop_); | 298 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
300 | 299 |
301 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); | 300 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); |
302 | 301 |
303 talk_base::SocketAddress remote_address; | 302 talk_base::SocketAddress remote_address; |
304 if (!jingle_glue::IPEndPointToSocketAddress(address, &remote_address)) { | 303 if (!jingle_glue::IPEndPointToSocketAddress(address, &remote_address)) { |
305 // Always expect correct IPv4 address to be allocated. | 304 // Always expect correct IPv4 address to be allocated. |
306 NOTREACHED(); | 305 NOTREACHED(); |
307 } | 306 } |
308 socket->InitAcceptedTcp(client, local_address_, remote_address); | 307 socket->InitAcceptedTcp(client, local_address_, remote_address); |
309 SignalNewConnection(this, socket.release()); | 308 SignalNewConnection(this, socket.release()); |
310 } | 309 } |
311 | 310 |
312 void IpcPacketSocket::OnSendComplete() { | 311 void IpcPacketSocket::OnSendComplete() { |
313 DCHECK_EQ(MessageLoop::current(), message_loop_); | 312 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
314 | 313 |
315 --send_packets_pending_; | 314 --send_packets_pending_; |
316 DCHECK_GE(send_packets_pending_, 0); | 315 DCHECK_GE(send_packets_pending_, 0); |
317 | 316 |
318 if (writable_signal_expected_ && | 317 if (writable_signal_expected_ && |
319 send_packets_pending_ <= kWritableSignalThreshold) { | 318 send_packets_pending_ <= kWritableSignalThreshold) { |
320 SignalReadyToSend(this); | 319 SignalReadyToSend(this); |
321 writable_signal_expected_ = false; | 320 writable_signal_expected_ = false; |
322 } | 321 } |
323 } | 322 } |
324 | 323 |
325 void IpcPacketSocket::OnError() { | 324 void IpcPacketSocket::OnError() { |
326 DCHECK_EQ(MessageLoop::current(), message_loop_); | 325 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
327 state_ = IS_ERROR; | 326 state_ = IS_ERROR; |
328 error_ = ECONNABORTED; | 327 error_ = ECONNABORTED; |
329 } | 328 } |
330 | 329 |
331 void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address, | 330 void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address, |
332 const std::vector<char>& data) { | 331 const std::vector<char>& data) { |
333 DCHECK_EQ(MessageLoop::current(), message_loop_); | 332 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
334 | 333 |
335 talk_base::SocketAddress address_lj; | 334 talk_base::SocketAddress address_lj; |
336 if (!jingle_glue::IPEndPointToSocketAddress(address, &address_lj)) { | 335 if (!jingle_glue::IPEndPointToSocketAddress(address, &address_lj)) { |
337 // We should always be able to convert address here because we | 336 // We should always be able to convert address here because we |
338 // don't expect IPv6 address on IPv4 connections. | 337 // don't expect IPv6 address on IPv4 connections. |
339 NOTREACHED(); | 338 NOTREACHED(); |
340 return; | 339 return; |
341 } | 340 } |
342 | 341 |
343 SignalReadPacket(this, &data[0], data.size(), address_lj); | 342 SignalReadPacket(this, &data[0], data.size(), address_lj); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 talk_base::SocketAddress crome_address; | 395 talk_base::SocketAddress crome_address; |
397 P2PSocketClient* socket_client = new P2PSocketClient(socket_dispatcher_); | 396 P2PSocketClient* socket_client = new P2PSocketClient(socket_dispatcher_); |
398 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); | 397 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); |
399 if (!socket->Init(P2P_SOCKET_TCP_CLIENT, socket_client, local_address, | 398 if (!socket->Init(P2P_SOCKET_TCP_CLIENT, socket_client, local_address, |
400 remote_address)) | 399 remote_address)) |
401 return NULL; | 400 return NULL; |
402 return socket.release(); | 401 return socket.release(); |
403 } | 402 } |
404 | 403 |
405 } // namespace content | 404 } // namespace content |
OLD | NEW |