| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/socket/tcp_socket.h" | 5 #include "extensions/browser/api/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "extensions/browser/api/api_resource.h" | 10 #include "extensions/browser/api/api_resource.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 if (is_connected_) | 95 if (is_connected_) |
| 96 break; | 96 break; |
| 97 | 97 |
| 98 net::AddressList address_list; | 98 net::AddressList address_list; |
| 99 if (!StringAndPortToAddressList(address, port, &address_list)) { | 99 if (!StringAndPortToAddressList(address, port, &address_list)) { |
| 100 result = net::ERR_ADDRESS_INVALID; | 100 result = net::ERR_ADDRESS_INVALID; |
| 101 break; | 101 break; |
| 102 } | 102 } |
| 103 | 103 |
| 104 socket_.reset( | 104 socket_.reset( |
| 105 new net::TCPClientSocket(address_list, NULL, net::NetLog::Source())); | 105 new net::TCPClientSocket(address_list, nullptr, net::NetLog::Source())); |
| 106 | 106 |
| 107 connect_callback_ = callback; | 107 connect_callback_ = callback; |
| 108 result = socket_->Connect( | 108 result = socket_->Connect( |
| 109 base::Bind(&TCPSocket::OnConnectComplete, base::Unretained(this))); | 109 base::Bind(&TCPSocket::OnConnectComplete, base::Unretained(this))); |
| 110 } while (false); | 110 } while (false); |
| 111 | 111 |
| 112 if (result != net::ERR_IO_PENDING) | 112 if (result != net::ERR_IO_PENDING) |
| 113 OnConnectComplete(result); | 113 OnConnectComplete(result); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void TCPSocket::Disconnect() { | 116 void TCPSocket::Disconnect() { |
| 117 is_connected_ = false; | 117 is_connected_ = false; |
| 118 if (socket_.get()) | 118 if (socket_.get()) |
| 119 socket_->Disconnect(); | 119 socket_->Disconnect(); |
| 120 server_socket_.reset(NULL); | 120 server_socket_.reset(nullptr); |
| 121 connect_callback_.Reset(); | 121 connect_callback_.Reset(); |
| 122 read_callback_.Reset(); | 122 read_callback_.Reset(); |
| 123 accept_callback_.Reset(); | 123 accept_callback_.Reset(); |
| 124 accept_socket_.reset(NULL); | 124 accept_socket_.reset(nullptr); |
| 125 } | 125 } |
| 126 | 126 |
| 127 int TCPSocket::Bind(const std::string& address, int port) { | 127 int TCPSocket::Bind(const std::string& address, int port) { |
| 128 return net::ERR_FAILED; | 128 return net::ERR_FAILED; |
| 129 } | 129 } |
| 130 | 130 |
| 131 void TCPSocket::Read(int count, const ReadCompletionCallback& callback) { | 131 void TCPSocket::Read(int count, const ReadCompletionCallback& callback) { |
| 132 DCHECK(!callback.is_null()); | 132 DCHECK(!callback.is_null()); |
| 133 | 133 |
| 134 if (socket_mode_ != CLIENT) { | 134 if (socket_mode_ != CLIENT) { |
| 135 callback.Run(net::ERR_FAILED, NULL); | 135 callback.Run(net::ERR_FAILED, nullptr); |
| 136 return; | 136 return; |
| 137 } | 137 } |
| 138 | 138 |
| 139 if (!read_callback_.is_null()) { | 139 if (!read_callback_.is_null()) { |
| 140 callback.Run(net::ERR_IO_PENDING, NULL); | 140 callback.Run(net::ERR_IO_PENDING, nullptr); |
| 141 return; | 141 return; |
| 142 } | 142 } |
| 143 | 143 |
| 144 if (count < 0) { | 144 if (count < 0) { |
| 145 callback.Run(net::ERR_INVALID_ARGUMENT, NULL); | 145 callback.Run(net::ERR_INVALID_ARGUMENT, nullptr); |
| 146 return; | 146 return; |
| 147 } | 147 } |
| 148 | 148 |
| 149 if (!socket_.get() || !IsConnected()) { | 149 if (!socket_.get() || !IsConnected()) { |
| 150 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL); | 150 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, nullptr); |
| 151 return; | 151 return; |
| 152 } | 152 } |
| 153 | 153 |
| 154 read_callback_ = callback; | 154 read_callback_ = callback; |
| 155 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count); | 155 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count); |
| 156 int result = socket_->Read( | 156 int result = socket_->Read( |
| 157 io_buffer.get(), | 157 io_buffer.get(), |
| 158 count, | 158 count, |
| 159 base::Bind( | 159 base::Bind( |
| 160 &TCPSocket::OnReadComplete, base::Unretained(this), io_buffer)); | 160 &TCPSocket::OnReadComplete, base::Unretained(this), io_buffer)); |
| 161 | 161 |
| 162 if (result != net::ERR_IO_PENDING) | 162 if (result != net::ERR_IO_PENDING) |
| 163 OnReadComplete(io_buffer, result); | 163 OnReadComplete(io_buffer, result); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void TCPSocket::RecvFrom(int count, | 166 void TCPSocket::RecvFrom(int count, |
| 167 const RecvFromCompletionCallback& callback) { | 167 const RecvFromCompletionCallback& callback) { |
| 168 callback.Run(net::ERR_FAILED, NULL, NULL, 0); | 168 callback.Run(net::ERR_FAILED, nullptr, nullptr, 0); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 171 void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 172 int byte_count, | 172 int byte_count, |
| 173 const std::string& address, | 173 const std::string& address, |
| 174 int port, | 174 int port, |
| 175 const CompletionCallback& callback) { | 175 const CompletionCallback& callback) { |
| 176 callback.Run(net::ERR_FAILED); | 176 callback.Run(net::ERR_FAILED); |
| 177 } | 177 } |
| 178 | 178 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 193 int backlog, | 193 int backlog, |
| 194 std::string* error_msg) { | 194 std::string* error_msg) { |
| 195 if (socket_mode_ == CLIENT) { | 195 if (socket_mode_ == CLIENT) { |
| 196 *error_msg = kTCPSocketTypeInvalidError; | 196 *error_msg = kTCPSocketTypeInvalidError; |
| 197 return net::ERR_NOT_IMPLEMENTED; | 197 return net::ERR_NOT_IMPLEMENTED; |
| 198 } | 198 } |
| 199 DCHECK(!socket_.get()); | 199 DCHECK(!socket_.get()); |
| 200 socket_mode_ = SERVER; | 200 socket_mode_ = SERVER; |
| 201 | 201 |
| 202 if (!server_socket_.get()) { | 202 if (!server_socket_.get()) { |
| 203 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source())); | 203 server_socket_.reset( |
| 204 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
| 204 } | 205 } |
| 205 | 206 |
| 206 int result = server_socket_->ListenWithAddressAndPort(address, port, backlog); | 207 int result = server_socket_->ListenWithAddressAndPort(address, port, backlog); |
| 207 if (result) | 208 if (result) |
| 208 *error_msg = kSocketListenError; | 209 *error_msg = kSocketListenError; |
| 209 return result; | 210 return result; |
| 210 } | 211 } |
| 211 | 212 |
| 212 void TCPSocket::Accept(const AcceptCompletionCallback& callback) { | 213 void TCPSocket::Accept(const AcceptCompletionCallback& callback) { |
| 213 if (socket_mode_ != SERVER || !server_socket_.get()) { | 214 if (socket_mode_ != SERVER || !server_socket_.get()) { |
| 214 callback.Run(net::ERR_FAILED, NULL); | 215 callback.Run(net::ERR_FAILED, nullptr); |
| 215 return; | 216 return; |
| 216 } | 217 } |
| 217 | 218 |
| 218 // Limits to only 1 blocked accept call. | 219 // Limits to only 1 blocked accept call. |
| 219 if (!accept_callback_.is_null()) { | 220 if (!accept_callback_.is_null()) { |
| 220 callback.Run(net::ERR_FAILED, NULL); | 221 callback.Run(net::ERR_FAILED, nullptr); |
| 221 return; | 222 return; |
| 222 } | 223 } |
| 223 | 224 |
| 224 int result = server_socket_->Accept( | 225 int result = server_socket_->Accept( |
| 225 &accept_socket_, | 226 &accept_socket_, |
| 226 base::Bind(&TCPSocket::OnAccept, base::Unretained(this))); | 227 base::Bind(&TCPSocket::OnAccept, base::Unretained(this))); |
| 227 if (result == net::ERR_IO_PENDING) { | 228 if (result == net::ERR_IO_PENDING) { |
| 228 accept_callback_ = callback; | 229 accept_callback_ = callback; |
| 229 } else if (result == net::OK) { | 230 } else if (result == net::OK) { |
| 230 accept_callback_ = callback; | 231 accept_callback_ = callback; |
| 231 this->OnAccept(result); | 232 this->OnAccept(result); |
| 232 } else { | 233 } else { |
| 233 callback.Run(result, NULL); | 234 callback.Run(result, nullptr); |
| 234 } | 235 } |
| 235 } | 236 } |
| 236 | 237 |
| 237 bool TCPSocket::IsConnected() { | 238 bool TCPSocket::IsConnected() { |
| 238 RefreshConnectionStatus(); | 239 RefreshConnectionStatus(); |
| 239 return is_connected_; | 240 return is_connected_; |
| 240 } | 241 } |
| 241 | 242 |
| 242 bool TCPSocket::GetPeerAddress(net::IPEndPoint* address) { | 243 bool TCPSocket::GetPeerAddress(net::IPEndPoint* address) { |
| 243 if (!socket_.get()) | 244 if (!socket_.get()) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 read_callback_.Run(result, io_buffer); | 293 read_callback_.Run(result, io_buffer); |
| 293 read_callback_.Reset(); | 294 read_callback_.Reset(); |
| 294 } | 295 } |
| 295 | 296 |
| 296 void TCPSocket::OnAccept(int result) { | 297 void TCPSocket::OnAccept(int result) { |
| 297 DCHECK(!accept_callback_.is_null()); | 298 DCHECK(!accept_callback_.is_null()); |
| 298 if (result == net::OK && accept_socket_.get()) { | 299 if (result == net::OK && accept_socket_.get()) { |
| 299 accept_callback_.Run( | 300 accept_callback_.Run( |
| 300 result, static_cast<net::TCPClientSocket*>(accept_socket_.release())); | 301 result, static_cast<net::TCPClientSocket*>(accept_socket_.release())); |
| 301 } else { | 302 } else { |
| 302 accept_callback_.Run(result, NULL); | 303 accept_callback_.Run(result, nullptr); |
| 303 } | 304 } |
| 304 accept_callback_.Reset(); | 305 accept_callback_.Reset(); |
| 305 } | 306 } |
| 306 | 307 |
| 307 void TCPSocket::Release() { | 308 void TCPSocket::Release() { |
| 308 // Release() is only invoked when the underlying sockets are taken (via | 309 // Release() is only invoked when the underlying sockets are taken (via |
| 309 // ClientStream()) by TLSSocket. TLSSocket only supports CLIENT-mode | 310 // ClientStream()) by TLSSocket. TLSSocket only supports CLIENT-mode |
| 310 // sockets. | 311 // sockets. |
| 311 DCHECK(!server_socket_.release() && !accept_socket_.release() && | 312 DCHECK(!server_socket_.release() && !accept_socket_.release() && |
| 312 socket_mode_ == CLIENT) | 313 socket_mode_ == CLIENT) |
| 313 << "Called in server mode."; | 314 << "Called in server mode."; |
| 314 | 315 |
| 315 // Release() doesn't disconnect the underlying sockets, but it does | 316 // Release() doesn't disconnect the underlying sockets, but it does |
| 316 // disconnect them from this TCPSocket. | 317 // disconnect them from this TCPSocket. |
| 317 is_connected_ = false; | 318 is_connected_ = false; |
| 318 | 319 |
| 319 connect_callback_.Reset(); | 320 connect_callback_.Reset(); |
| 320 read_callback_.Reset(); | 321 read_callback_.Reset(); |
| 321 accept_callback_.Reset(); | 322 accept_callback_.Reset(); |
| 322 | 323 |
| 323 DCHECK(socket_.get()) << "Called on null client socket."; | 324 DCHECK(socket_.get()) << "Called on null client socket."; |
| 324 ignore_result(socket_.release()); | 325 ignore_result(socket_.release()); |
| 325 } | 326 } |
| 326 | 327 |
| 327 net::TCPClientSocket* TCPSocket::ClientStream() { | 328 net::TCPClientSocket* TCPSocket::ClientStream() { |
| 328 if (socket_mode_ != CLIENT || GetSocketType() != TYPE_TCP) | 329 if (socket_mode_ != CLIENT || GetSocketType() != TYPE_TCP) |
| 329 return NULL; | 330 return nullptr; |
| 330 return socket_.get(); | 331 return socket_.get(); |
| 331 } | 332 } |
| 332 | 333 |
| 333 bool TCPSocket::HasPendingRead() const { | 334 bool TCPSocket::HasPendingRead() const { |
| 334 return !read_callback_.is_null(); | 335 return !read_callback_.is_null(); |
| 335 } | 336 } |
| 336 | 337 |
| 337 ResumableTCPSocket::ResumableTCPSocket(const std::string& owner_extension_id) | 338 ResumableTCPSocket::ResumableTCPSocket(const std::string& owner_extension_id) |
| 338 : TCPSocket(owner_extension_id), | 339 : TCPSocket(owner_extension_id), |
| 339 persistent_(false), | 340 persistent_(false), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 350 | 351 |
| 351 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } | 352 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } |
| 352 | 353 |
| 353 ResumableTCPServerSocket::ResumableTCPServerSocket( | 354 ResumableTCPServerSocket::ResumableTCPServerSocket( |
| 354 const std::string& owner_extension_id) | 355 const std::string& owner_extension_id) |
| 355 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} | 356 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} |
| 356 | 357 |
| 357 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } | 358 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } |
| 358 | 359 |
| 359 } // namespace extensions | 360 } // namespace extensions |
| OLD | NEW |