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