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 "chrome/browser/extensions/api/socket/tcp_socket.h" | 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/api_resource.h" | 7 #include "chrome/browser/extensions/api/api_resource.h" |
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
10 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/rand_callback.h" | 12 #include "net/base/rand_callback.h" |
13 #include "net/socket/tcp_client_socket.h" | 13 #include "net/socket/tcp_client_socket.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
| 17 const char kTCPSocketTypeInvalidError[] = |
| 18 "Cannot call both connect and listen on the same socket."; |
| 19 const char kSocketListenError[] = "Could not listen on the specified port."; |
| 20 const char kSocketRebindError[] = "Rebinding a listening socket not supported"; |
| 21 |
17 TCPSocket::TCPSocket(const std::string& owner_extension_id, | 22 TCPSocket::TCPSocket(const std::string& owner_extension_id, |
18 ApiResourceEventNotifier* event_notifier) | 23 ApiResourceEventNotifier* event_notifier) |
19 : Socket(owner_extension_id, event_notifier) { | 24 : Socket(owner_extension_id, event_notifier), |
| 25 socket_mode_(UNKNOWN) { |
20 } | 26 } |
21 | 27 |
22 // For testing. | |
23 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, | 28 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, |
24 const std::string& owner_extension_id, | 29 const std::string& owner_extension_id, |
25 ApiResourceEventNotifier* event_notifier) | 30 ApiResourceEventNotifier* event_notifier, |
| 31 bool is_connected) |
26 : Socket(owner_extension_id, event_notifier), | 32 : Socket(owner_extension_id, event_notifier), |
27 socket_(tcp_client_socket) { | 33 socket_(tcp_client_socket), |
| 34 socket_mode_(CLIENT) { |
| 35 this->is_connected_ = is_connected; |
| 36 } |
| 37 |
| 38 TCPSocket::TCPSocket(net::TCPServerSocket* tcp_server_socket, |
| 39 const std::string& owner_extension_id) |
| 40 : Socket(owner_extension_id, NULL), |
| 41 server_socket_(tcp_server_socket), |
| 42 socket_mode_(SERVER) { |
28 } | 43 } |
29 | 44 |
30 // static | 45 // static |
31 TCPSocket* TCPSocket::CreateSocketForTesting( | 46 TCPSocket* TCPSocket::CreateSocketForTesting( |
32 net::TCPClientSocket* tcp_client_socket, | 47 net::TCPClientSocket* tcp_client_socket, |
33 const std::string& owner_extension_id, | 48 const std::string& owner_extension_id, |
34 ApiResourceEventNotifier* event_notifier) { | 49 ApiResourceEventNotifier* event_notifier) { |
35 return new TCPSocket(tcp_client_socket, owner_extension_id, event_notifier); | 50 return new TCPSocket(tcp_client_socket, owner_extension_id, event_notifier); |
36 } | 51 } |
37 | 52 |
| 53 // static |
| 54 TCPSocket* TCPSocket::CreateServerSocketForTesting( |
| 55 net::TCPServerSocket* tcp_server_socket, |
| 56 const std::string& owner_extension_id) { |
| 57 return new TCPSocket(tcp_server_socket, owner_extension_id); |
| 58 } |
| 59 |
38 TCPSocket::~TCPSocket() { | 60 TCPSocket::~TCPSocket() { |
39 if (is_connected_) { | 61 if (is_connected_) { |
40 Disconnect(); | 62 Disconnect(); |
41 } | 63 } |
| 64 server_socket_.reset(NULL); |
42 } | 65 } |
43 | 66 |
44 void TCPSocket::Connect(const std::string& address, | 67 void TCPSocket::Connect(const std::string& address, |
45 int port, | 68 int port, |
46 const CompletionCallback& callback) { | 69 const CompletionCallback& callback) { |
47 DCHECK(!callback.is_null()); | 70 DCHECK(!callback.is_null()); |
48 | 71 |
49 if (!connect_callback_.is_null()) { | 72 if (socket_mode_ == SERVER || !connect_callback_.is_null()) { |
50 callback.Run(net::ERR_CONNECTION_FAILED); | 73 callback.Run(net::ERR_CONNECTION_FAILED); |
51 return; | 74 return; |
52 } | 75 } |
| 76 DCHECK(!server_socket_.get()); |
| 77 socket_mode_ = CLIENT; |
53 connect_callback_ = callback; | 78 connect_callback_ = callback; |
54 | 79 |
55 int result = net::ERR_CONNECTION_FAILED; | 80 int result = net::ERR_CONNECTION_FAILED; |
56 do { | 81 do { |
57 if (is_connected_) | 82 if (is_connected_) |
58 break; | 83 break; |
59 | 84 |
60 net::AddressList address_list; | 85 net::AddressList address_list; |
61 if (!StringAndPortToAddressList(address, port, &address_list)) { | 86 if (!StringAndPortToAddressList(address, port, &address_list)) { |
62 result = net::ERR_ADDRESS_INVALID; | 87 result = net::ERR_ADDRESS_INVALID; |
63 break; | 88 break; |
64 } | 89 } |
65 | 90 |
66 socket_.reset(new net::TCPClientSocket(address_list, NULL, | 91 socket_.reset(new net::TCPClientSocket(address_list, NULL, |
67 net::NetLog::Source())); | 92 net::NetLog::Source())); |
| 93 |
| 94 if (bind_address_.get()) { |
| 95 if (socket_->Bind(*bind_address_) != net::OK) { |
| 96 socket_.reset(); |
| 97 OnConnectComplete(net::ERR_ADDRESS_IN_USE); |
| 98 return; |
| 99 }; |
| 100 } |
| 101 |
68 connect_callback_ = callback; | 102 connect_callback_ = callback; |
69 result = socket_->Connect(base::Bind( | 103 result = socket_->Connect(base::Bind( |
70 &TCPSocket::OnConnectComplete, base::Unretained(this))); | 104 &TCPSocket::OnConnectComplete, base::Unretained(this))); |
71 } while (false); | 105 } while (false); |
72 | 106 |
73 if (result != net::ERR_IO_PENDING) | 107 if (result != net::ERR_IO_PENDING) |
74 OnConnectComplete(result); | 108 OnConnectComplete(result); |
75 } | 109 } |
76 | 110 |
77 void TCPSocket::Disconnect() { | 111 void TCPSocket::Disconnect() { |
78 is_connected_ = false; | 112 is_connected_ = false; |
79 socket_->Disconnect(); | 113 socket_->Disconnect(); |
80 } | 114 } |
81 | 115 |
82 int TCPSocket::Bind(const std::string& address, int port) { | 116 int TCPSocket::Bind(const std::string& address, int port) { |
83 // TODO(penghuang): Supports bind for tcp? | 117 if (bind_address_.get() || server_socket_.get() || socket_.get()) { |
84 return net::ERR_FAILED; | 118 return net::ERR_FAILED; |
| 119 } |
| 120 |
| 121 scoped_ptr<net::IPEndPoint> ip_end_point(new net::IPEndPoint()); |
| 122 if (!StringAndPortToIPEndPoint(address, port, ip_end_point.get())) |
| 123 return net::ERR_INVALID_ARGUMENT; |
| 124 |
| 125 bind_address_.swap(ip_end_point); |
| 126 return net::OK; |
85 } | 127 } |
86 | 128 |
87 void TCPSocket::Read(int count, | 129 void TCPSocket::Read(int count, |
88 const ReadCompletionCallback& callback) { | 130 const ReadCompletionCallback& callback) { |
89 DCHECK(!callback.is_null()); | 131 DCHECK(!callback.is_null()); |
90 | 132 |
| 133 if (socket_mode_ != CLIENT) { |
| 134 callback.Run(net::ERR_FAILED, NULL); |
| 135 return; |
| 136 } |
| 137 |
91 if (!read_callback_.is_null()) { | 138 if (!read_callback_.is_null()) { |
92 callback.Run(net::ERR_IO_PENDING, NULL); | 139 callback.Run(net::ERR_IO_PENDING, NULL); |
93 return; | 140 return; |
94 } else { | 141 } else { |
95 read_callback_ = callback; | 142 read_callback_ = callback; |
96 } | 143 } |
97 | 144 |
98 int result = net::ERR_FAILED; | 145 int result = net::ERR_FAILED; |
99 scoped_refptr<net::IOBuffer> io_buffer; | 146 scoped_refptr<net::IOBuffer> io_buffer; |
100 do { | 147 do { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 return false; | 183 return false; |
137 return socket_->SetKeepAlive(enable, delay); | 184 return socket_->SetKeepAlive(enable, delay); |
138 } | 185 } |
139 | 186 |
140 bool TCPSocket::SetNoDelay(bool no_delay) { | 187 bool TCPSocket::SetNoDelay(bool no_delay) { |
141 if (!socket_.get()) | 188 if (!socket_.get()) |
142 return false; | 189 return false; |
143 return socket_->SetNoDelay(no_delay); | 190 return socket_->SetNoDelay(no_delay); |
144 } | 191 } |
145 | 192 |
| 193 int TCPSocket::Listen(const std::string& address, int port, int backlog, |
| 194 std::string* error_msg) { |
| 195 if (socket_mode_ == CLIENT) { |
| 196 *error_msg = kTCPSocketTypeInvalidError; |
| 197 return net::ERR_NOT_IMPLEMENTED; |
| 198 } |
| 199 DCHECK(!socket_.get()); |
| 200 socket_mode_ = SERVER; |
| 201 |
| 202 scoped_ptr<net::IPEndPoint> bind_address(new net::IPEndPoint()); |
| 203 if (!StringAndPortToIPEndPoint(address, port, bind_address.get())) |
| 204 return net::ERR_INVALID_ARGUMENT; |
| 205 |
| 206 if (server_socket_.get()) { |
| 207 *error_msg = kSocketRebindError; |
| 208 return net::ERR_ADDRESS_IN_USE; |
| 209 } |
| 210 server_socket_.reset(new net::TCPServerSocket(NULL, |
| 211 net::NetLog::Source())); |
| 212 server_socket_->AllowAddressReuse(); |
| 213 int result = server_socket_->Listen(*bind_address, backlog); |
| 214 if (result) { |
| 215 *error_msg = kSocketListenError; |
| 216 } |
| 217 return result; |
| 218 } |
| 219 |
| 220 void TCPSocket::Accept(const AcceptCompletionCallback &callback) { |
| 221 if (socket_mode_ != SERVER || !server_socket_.get()) { |
| 222 callback.Run(net::ERR_FAILED, NULL); |
| 223 return; |
| 224 } |
| 225 |
| 226 // Limits to only 1 blocked accept call. |
| 227 if (!accept_callback_.is_null()) { |
| 228 callback.Run(net::ERR_FAILED, NULL); |
| 229 return; |
| 230 } |
| 231 |
| 232 int result = server_socket_->Accept(&accept_socket_, base::Bind( |
| 233 &TCPSocket::OnAccept, base::Unretained(this))); |
| 234 if (result == net::ERR_IO_PENDING) { |
| 235 accept_callback_ = callback; |
| 236 return; |
| 237 } else if (result == net::OK) { |
| 238 accept_callback_ = callback; |
| 239 this->OnAccept(result); |
| 240 return; |
| 241 } else { |
| 242 callback.Run(result, NULL); |
| 243 return; |
| 244 } |
| 245 } |
| 246 |
146 bool TCPSocket::GetPeerAddress(net::IPEndPoint* address) { | 247 bool TCPSocket::GetPeerAddress(net::IPEndPoint* address) { |
147 if (!socket_.get()) | 248 if (!socket_.get()) |
148 return false; | 249 return false; |
149 return !socket_->GetPeerAddress(address); | 250 return !socket_->GetPeerAddress(address); |
150 } | 251 } |
151 | 252 |
152 bool TCPSocket::GetLocalAddress(net::IPEndPoint* address) { | 253 bool TCPSocket::GetLocalAddress(net::IPEndPoint* address) { |
153 if (!socket_.get()) | 254 if (socket_.get()) { |
| 255 return !socket_->GetLocalAddress(address); |
| 256 } else if (server_socket_.get()) { |
| 257 return !server_socket_->GetLocalAddress(address); |
| 258 } else { |
154 return false; | 259 return false; |
155 return !socket_->GetLocalAddress(address); | 260 } |
156 } | 261 } |
157 | 262 |
158 Socket::SocketType TCPSocket::GetSocketType() const { | 263 Socket::SocketType TCPSocket::GetSocketType() const { |
159 return Socket::TYPE_TCP; | 264 return Socket::TYPE_TCP; |
160 } | 265 } |
161 | 266 |
162 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer, | 267 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer, |
163 int io_buffer_size, | 268 int io_buffer_size, |
164 const net::CompletionCallback& callback) { | 269 const net::CompletionCallback& callback) { |
165 if (!socket_.get() || !socket_->IsConnected()) | 270 if (socket_mode_ != CLIENT) |
| 271 return net::ERR_FAILED; |
| 272 else if (!socket_.get() || !socket_->IsConnected()) |
166 return net::ERR_SOCKET_NOT_CONNECTED; | 273 return net::ERR_SOCKET_NOT_CONNECTED; |
167 else | 274 else |
168 return socket_->Write(io_buffer, io_buffer_size, callback); | 275 return socket_->Write(io_buffer, io_buffer_size, callback); |
169 } | 276 } |
170 | 277 |
171 void TCPSocket::OnConnectComplete(int result) { | 278 void TCPSocket::OnConnectComplete(int result) { |
172 DCHECK(!connect_callback_.is_null()); | 279 DCHECK(!connect_callback_.is_null()); |
173 DCHECK(!is_connected_); | 280 DCHECK(!is_connected_); |
174 is_connected_ = result == net::OK; | 281 is_connected_ = result == net::OK; |
175 connect_callback_.Run(result); | 282 connect_callback_.Run(result); |
176 connect_callback_.Reset(); | 283 connect_callback_.Reset(); |
177 } | 284 } |
178 | 285 |
179 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | 286 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, |
180 int result) { | 287 int result) { |
181 DCHECK(!read_callback_.is_null()); | 288 DCHECK(!read_callback_.is_null()); |
182 read_callback_.Run(result, io_buffer); | 289 read_callback_.Run(result, io_buffer); |
183 read_callback_.Reset(); | 290 read_callback_.Reset(); |
184 } | 291 } |
185 | 292 |
| 293 void TCPSocket::OnAccept(int result) { |
| 294 DCHECK(!accept_callback_.is_null()); |
| 295 if (result == net::OK) { |
| 296 DCHECK(accept_socket_.get() != NULL); |
| 297 |
| 298 accept_callback_.Run( |
| 299 result, static_cast<net::TCPClientSocket*>(accept_socket_.release())); |
| 300 } else { |
| 301 accept_callback_.Run(result, NULL); |
| 302 } |
| 303 accept_callback_.Reset(); |
| 304 } |
| 305 |
186 } // namespace extensions | 306 } // namespace extensions |
OLD | NEW |