| 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 Disconnect(); |
| 40 Disconnect(); | |
| 41 } | |
| 42 } | 62 } |
| 43 | 63 |
| 44 void TCPSocket::Connect(const std::string& address, | 64 void TCPSocket::Connect(const std::string& address, |
| 45 int port, | 65 int port, |
| 46 const CompletionCallback& callback) { | 66 const CompletionCallback& callback) { |
| 47 DCHECK(!callback.is_null()); | 67 DCHECK(!callback.is_null()); |
| 48 | 68 |
| 49 if (!connect_callback_.is_null()) { | 69 if (socket_mode_ == SERVER || !connect_callback_.is_null()) { |
| 50 callback.Run(net::ERR_CONNECTION_FAILED); | 70 callback.Run(net::ERR_CONNECTION_FAILED); |
| 51 return; | 71 return; |
| 52 } | 72 } |
| 73 DCHECK(!server_socket_.get()); |
| 74 socket_mode_ = CLIENT; |
| 53 connect_callback_ = callback; | 75 connect_callback_ = callback; |
| 54 | 76 |
| 55 int result = net::ERR_CONNECTION_FAILED; | 77 int result = net::ERR_CONNECTION_FAILED; |
| 56 do { | 78 do { |
| 57 if (is_connected_) | 79 if (is_connected_) |
| 58 break; | 80 break; |
| 59 | 81 |
| 60 net::AddressList address_list; | 82 net::AddressList address_list; |
| 61 if (!StringAndPortToAddressList(address, port, &address_list)) { | 83 if (!StringAndPortToAddressList(address, port, &address_list)) { |
| 62 result = net::ERR_ADDRESS_INVALID; | 84 result = net::ERR_ADDRESS_INVALID; |
| 63 break; | 85 break; |
| 64 } | 86 } |
| 65 | 87 |
| 66 socket_.reset(new net::TCPClientSocket(address_list, NULL, | 88 socket_.reset(new net::TCPClientSocket(address_list, NULL, |
| 67 net::NetLog::Source())); | 89 net::NetLog::Source())); |
| 90 |
| 68 connect_callback_ = callback; | 91 connect_callback_ = callback; |
| 69 result = socket_->Connect(base::Bind( | 92 result = socket_->Connect(base::Bind( |
| 70 &TCPSocket::OnConnectComplete, base::Unretained(this))); | 93 &TCPSocket::OnConnectComplete, base::Unretained(this))); |
| 71 } while (false); | 94 } while (false); |
| 72 | 95 |
| 73 if (result != net::ERR_IO_PENDING) | 96 if (result != net::ERR_IO_PENDING) |
| 74 OnConnectComplete(result); | 97 OnConnectComplete(result); |
| 75 } | 98 } |
| 76 | 99 |
| 77 void TCPSocket::Disconnect() { | 100 void TCPSocket::Disconnect() { |
| 78 is_connected_ = false; | 101 is_connected_ = false; |
| 79 socket_->Disconnect(); | 102 if (socket_.get()) |
| 103 socket_->Disconnect(); |
| 104 server_socket_.reset(NULL); |
| 80 } | 105 } |
| 81 | 106 |
| 82 int TCPSocket::Bind(const std::string& address, int port) { | 107 int TCPSocket::Bind(const std::string& address, int port) { |
| 83 // TODO(penghuang): Supports bind for tcp? | |
| 84 return net::ERR_FAILED; | 108 return net::ERR_FAILED; |
| 85 } | 109 } |
| 86 | 110 |
| 87 void TCPSocket::Read(int count, | 111 void TCPSocket::Read(int count, |
| 88 const ReadCompletionCallback& callback) { | 112 const ReadCompletionCallback& callback) { |
| 89 DCHECK(!callback.is_null()); | 113 DCHECK(!callback.is_null()); |
| 90 | 114 |
| 115 if (socket_mode_ != CLIENT) { |
| 116 callback.Run(net::ERR_FAILED, NULL); |
| 117 return; |
| 118 } |
| 119 |
| 91 if (!read_callback_.is_null()) { | 120 if (!read_callback_.is_null()) { |
| 92 callback.Run(net::ERR_IO_PENDING, NULL); | 121 callback.Run(net::ERR_IO_PENDING, NULL); |
| 93 return; | 122 return; |
| 94 } else { | 123 } else { |
| 95 read_callback_ = callback; | 124 read_callback_ = callback; |
| 96 } | 125 } |
| 97 | 126 |
| 98 int result = net::ERR_FAILED; | 127 int result = net::ERR_FAILED; |
| 99 scoped_refptr<net::IOBuffer> io_buffer; | 128 scoped_refptr<net::IOBuffer> io_buffer; |
| 100 do { | 129 do { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return false; | 165 return false; |
| 137 return socket_->SetKeepAlive(enable, delay); | 166 return socket_->SetKeepAlive(enable, delay); |
| 138 } | 167 } |
| 139 | 168 |
| 140 bool TCPSocket::SetNoDelay(bool no_delay) { | 169 bool TCPSocket::SetNoDelay(bool no_delay) { |
| 141 if (!socket_.get()) | 170 if (!socket_.get()) |
| 142 return false; | 171 return false; |
| 143 return socket_->SetNoDelay(no_delay); | 172 return socket_->SetNoDelay(no_delay); |
| 144 } | 173 } |
| 145 | 174 |
| 175 int TCPSocket::Listen(const std::string& address, int port, int backlog, |
| 176 std::string* error_msg) { |
| 177 if (socket_mode_ == CLIENT) { |
| 178 *error_msg = kTCPSocketTypeInvalidError; |
| 179 return net::ERR_NOT_IMPLEMENTED; |
| 180 } |
| 181 DCHECK(!socket_.get()); |
| 182 socket_mode_ = SERVER; |
| 183 |
| 184 scoped_ptr<net::IPEndPoint> bind_address(new net::IPEndPoint()); |
| 185 if (!StringAndPortToIPEndPoint(address, port, bind_address.get())) |
| 186 return net::ERR_INVALID_ARGUMENT; |
| 187 |
| 188 if (server_socket_.get()) { |
| 189 *error_msg = kSocketRebindError; |
| 190 return net::ERR_ADDRESS_IN_USE; |
| 191 } |
| 192 server_socket_.reset(new net::TCPServerSocket(NULL, |
| 193 net::NetLog::Source())); |
| 194 server_socket_->AllowAddressReuse(); |
| 195 int result = server_socket_->Listen(*bind_address, backlog); |
| 196 if (result) |
| 197 *error_msg = kSocketListenError; |
| 198 return result; |
| 199 } |
| 200 |
| 201 void TCPSocket::Accept(const AcceptCompletionCallback &callback) { |
| 202 if (socket_mode_ != SERVER || !server_socket_.get()) { |
| 203 callback.Run(net::ERR_FAILED, NULL); |
| 204 return; |
| 205 } |
| 206 |
| 207 // Limits to only 1 blocked accept call. |
| 208 if (!accept_callback_.is_null()) { |
| 209 callback.Run(net::ERR_FAILED, NULL); |
| 210 return; |
| 211 } |
| 212 |
| 213 int result = server_socket_->Accept(&accept_socket_, base::Bind( |
| 214 &TCPSocket::OnAccept, base::Unretained(this))); |
| 215 if (result == net::ERR_IO_PENDING) { |
| 216 accept_callback_ = callback; |
| 217 } else if (result == net::OK) { |
| 218 accept_callback_ = callback; |
| 219 this->OnAccept(result); |
| 220 } else { |
| 221 callback.Run(result, NULL); |
| 222 } |
| 223 } |
| 224 |
| 146 bool TCPSocket::GetPeerAddress(net::IPEndPoint* address) { | 225 bool TCPSocket::GetPeerAddress(net::IPEndPoint* address) { |
| 147 if (!socket_.get()) | 226 if (!socket_.get()) |
| 148 return false; | 227 return false; |
| 149 return !socket_->GetPeerAddress(address); | 228 return !socket_->GetPeerAddress(address); |
| 150 } | 229 } |
| 151 | 230 |
| 152 bool TCPSocket::GetLocalAddress(net::IPEndPoint* address) { | 231 bool TCPSocket::GetLocalAddress(net::IPEndPoint* address) { |
| 153 if (!socket_.get()) | 232 if (socket_.get()) { |
| 233 return !socket_->GetLocalAddress(address); |
| 234 } else if (server_socket_.get()) { |
| 235 return !server_socket_->GetLocalAddress(address); |
| 236 } else { |
| 154 return false; | 237 return false; |
| 155 return !socket_->GetLocalAddress(address); | 238 } |
| 156 } | 239 } |
| 157 | 240 |
| 158 Socket::SocketType TCPSocket::GetSocketType() const { | 241 Socket::SocketType TCPSocket::GetSocketType() const { |
| 159 return Socket::TYPE_TCP; | 242 return Socket::TYPE_TCP; |
| 160 } | 243 } |
| 161 | 244 |
| 162 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer, | 245 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer, |
| 163 int io_buffer_size, | 246 int io_buffer_size, |
| 164 const net::CompletionCallback& callback) { | 247 const net::CompletionCallback& callback) { |
| 165 if (!socket_.get() || !socket_->IsConnected()) | 248 if (socket_mode_ != CLIENT) |
| 249 return net::ERR_FAILED; |
| 250 else if (!socket_.get() || !socket_->IsConnected()) |
| 166 return net::ERR_SOCKET_NOT_CONNECTED; | 251 return net::ERR_SOCKET_NOT_CONNECTED; |
| 167 else | 252 else |
| 168 return socket_->Write(io_buffer, io_buffer_size, callback); | 253 return socket_->Write(io_buffer, io_buffer_size, callback); |
| 169 } | 254 } |
| 170 | 255 |
| 171 void TCPSocket::OnConnectComplete(int result) { | 256 void TCPSocket::OnConnectComplete(int result) { |
| 172 DCHECK(!connect_callback_.is_null()); | 257 DCHECK(!connect_callback_.is_null()); |
| 173 DCHECK(!is_connected_); | 258 DCHECK(!is_connected_); |
| 174 is_connected_ = result == net::OK; | 259 is_connected_ = result == net::OK; |
| 175 connect_callback_.Run(result); | 260 connect_callback_.Run(result); |
| 176 connect_callback_.Reset(); | 261 connect_callback_.Reset(); |
| 177 } | 262 } |
| 178 | 263 |
| 179 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | 264 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, |
| 180 int result) { | 265 int result) { |
| 181 DCHECK(!read_callback_.is_null()); | 266 DCHECK(!read_callback_.is_null()); |
| 182 read_callback_.Run(result, io_buffer); | 267 read_callback_.Run(result, io_buffer); |
| 183 read_callback_.Reset(); | 268 read_callback_.Reset(); |
| 184 } | 269 } |
| 185 | 270 |
| 271 void TCPSocket::OnAccept(int result) { |
| 272 DCHECK(!accept_callback_.is_null()); |
| 273 if (result == net::OK) { |
| 274 DCHECK(accept_socket_.get() != NULL); |
| 275 |
| 276 accept_callback_.Run( |
| 277 result, static_cast<net::TCPClientSocket*>(accept_socket_.release())); |
| 278 } else { |
| 279 accept_callback_.Run(result, NULL); |
| 280 } |
| 281 accept_callback_.Reset(); |
| 282 } |
| 283 |
| 186 } // namespace extensions | 284 } // namespace extensions |
| OLD | NEW |