| 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 "base/memory/ptr_util.h" |
| 10 #include "extensions/browser/api/api_resource.h" | 11 #include "extensions/browser/api/api_resource.h" |
| 11 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
| 12 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "net/base/rand_callback.h" | 15 #include "net/base/rand_callback.h" |
| 15 #include "net/socket/tcp_client_socket.h" | 16 #include "net/socket/tcp_client_socket.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 19 const char kTCPSocketTypeInvalidError[] = | 20 const char kTCPSocketTypeInvalidError[] = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 38 // static | 39 // static |
| 39 template <> | 40 template <> |
| 40 BrowserContextKeyedAPIFactory<ApiResourceManager<ResumableTCPServerSocket> >* | 41 BrowserContextKeyedAPIFactory<ApiResourceManager<ResumableTCPServerSocket> >* |
| 41 ApiResourceManager<ResumableTCPServerSocket>::GetFactoryInstance() { | 42 ApiResourceManager<ResumableTCPServerSocket>::GetFactoryInstance() { |
| 42 return g_server_factory.Pointer(); | 43 return g_server_factory.Pointer(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 TCPSocket::TCPSocket(const std::string& owner_extension_id) | 46 TCPSocket::TCPSocket(const std::string& owner_extension_id) |
| 46 : Socket(owner_extension_id), socket_mode_(UNKNOWN) {} | 47 : Socket(owner_extension_id), socket_mode_(UNKNOWN) {} |
| 47 | 48 |
| 48 TCPSocket::TCPSocket(scoped_ptr<net::TCPClientSocket> tcp_client_socket, | 49 TCPSocket::TCPSocket(std::unique_ptr<net::TCPClientSocket> tcp_client_socket, |
| 49 const std::string& owner_extension_id, | 50 const std::string& owner_extension_id, |
| 50 bool is_connected) | 51 bool is_connected) |
| 51 : Socket(owner_extension_id), | 52 : Socket(owner_extension_id), |
| 52 socket_(std::move(tcp_client_socket)), | 53 socket_(std::move(tcp_client_socket)), |
| 53 socket_mode_(CLIENT) { | 54 socket_mode_(CLIENT) { |
| 54 this->is_connected_ = is_connected; | 55 this->is_connected_ = is_connected; |
| 55 } | 56 } |
| 56 | 57 |
| 57 TCPSocket::TCPSocket(scoped_ptr<net::TCPServerSocket> tcp_server_socket, | 58 TCPSocket::TCPSocket(std::unique_ptr<net::TCPServerSocket> tcp_server_socket, |
| 58 const std::string& owner_extension_id) | 59 const std::string& owner_extension_id) |
| 59 : Socket(owner_extension_id), | 60 : Socket(owner_extension_id), |
| 60 server_socket_(std::move(tcp_server_socket)), | 61 server_socket_(std::move(tcp_server_socket)), |
| 61 socket_mode_(SERVER) {} | 62 socket_mode_(SERVER) {} |
| 62 | 63 |
| 63 // static | 64 // static |
| 64 TCPSocket* TCPSocket::CreateSocketForTesting( | 65 TCPSocket* TCPSocket::CreateSocketForTesting( |
| 65 scoped_ptr<net::TCPClientSocket> tcp_client_socket, | 66 std::unique_ptr<net::TCPClientSocket> tcp_client_socket, |
| 66 const std::string& owner_extension_id, | 67 const std::string& owner_extension_id, |
| 67 bool is_connected) { | 68 bool is_connected) { |
| 68 return new TCPSocket(std::move(tcp_client_socket), owner_extension_id, | 69 return new TCPSocket(std::move(tcp_client_socket), owner_extension_id, |
| 69 is_connected); | 70 is_connected); |
| 70 } | 71 } |
| 71 | 72 |
| 72 // static | 73 // static |
| 73 TCPSocket* TCPSocket::CreateServerSocketForTesting( | 74 TCPSocket* TCPSocket::CreateServerSocketForTesting( |
| 74 scoped_ptr<net::TCPServerSocket> tcp_server_socket, | 75 std::unique_ptr<net::TCPServerSocket> tcp_server_socket, |
| 75 const std::string& owner_extension_id) { | 76 const std::string& owner_extension_id) { |
| 76 return new TCPSocket(std::move(tcp_server_socket), owner_extension_id); | 77 return new TCPSocket(std::move(tcp_server_socket), owner_extension_id); |
| 77 } | 78 } |
| 78 | 79 |
| 79 TCPSocket::~TCPSocket() { Disconnect(); } | 80 TCPSocket::~TCPSocket() { Disconnect(); } |
| 80 | 81 |
| 81 void TCPSocket::Connect(const net::AddressList& address, | 82 void TCPSocket::Connect(const net::AddressList& address, |
| 82 const CompletionCallback& callback) { | 83 const CompletionCallback& callback) { |
| 83 DCHECK(!callback.is_null()); | 84 DCHECK(!callback.is_null()); |
| 84 | 85 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 int result) { | 293 int result) { |
| 293 DCHECK(!read_callback_.is_null()); | 294 DCHECK(!read_callback_.is_null()); |
| 294 read_callback_.Run(result, io_buffer); | 295 read_callback_.Run(result, io_buffer); |
| 295 read_callback_.Reset(); | 296 read_callback_.Reset(); |
| 296 } | 297 } |
| 297 | 298 |
| 298 void TCPSocket::OnAccept(int result) { | 299 void TCPSocket::OnAccept(int result) { |
| 299 DCHECK(!accept_callback_.is_null()); | 300 DCHECK(!accept_callback_.is_null()); |
| 300 if (result == net::OK && accept_socket_.get()) { | 301 if (result == net::OK && accept_socket_.get()) { |
| 301 accept_callback_.Run(result, | 302 accept_callback_.Run(result, |
| 302 make_scoped_ptr(static_cast<net::TCPClientSocket*>( | 303 base::WrapUnique(static_cast<net::TCPClientSocket*>( |
| 303 accept_socket_.release()))); | 304 accept_socket_.release()))); |
| 304 } else { | 305 } else { |
| 305 accept_callback_.Run(result, NULL); | 306 accept_callback_.Run(result, NULL); |
| 306 } | 307 } |
| 307 accept_callback_.Reset(); | 308 accept_callback_.Reset(); |
| 308 } | 309 } |
| 309 | 310 |
| 310 void TCPSocket::Release() { | 311 void TCPSocket::Release() { |
| 311 // Release() is only invoked when the underlying sockets are taken (via | 312 // Release() is only invoked when the underlying sockets are taken (via |
| 312 // ClientStream()) by TLSSocket. TLSSocket only supports CLIENT-mode | 313 // ClientStream()) by TLSSocket. TLSSocket only supports CLIENT-mode |
| (...skipping 24 matching lines...) Expand all Loading... |
| 337 return !read_callback_.is_null(); | 338 return !read_callback_.is_null(); |
| 338 } | 339 } |
| 339 | 340 |
| 340 ResumableTCPSocket::ResumableTCPSocket(const std::string& owner_extension_id) | 341 ResumableTCPSocket::ResumableTCPSocket(const std::string& owner_extension_id) |
| 341 : TCPSocket(owner_extension_id), | 342 : TCPSocket(owner_extension_id), |
| 342 persistent_(false), | 343 persistent_(false), |
| 343 buffer_size_(0), | 344 buffer_size_(0), |
| 344 paused_(false) {} | 345 paused_(false) {} |
| 345 | 346 |
| 346 ResumableTCPSocket::ResumableTCPSocket( | 347 ResumableTCPSocket::ResumableTCPSocket( |
| 347 scoped_ptr<net::TCPClientSocket> tcp_client_socket, | 348 std::unique_ptr<net::TCPClientSocket> tcp_client_socket, |
| 348 const std::string& owner_extension_id, | 349 const std::string& owner_extension_id, |
| 349 bool is_connected) | 350 bool is_connected) |
| 350 : TCPSocket(std::move(tcp_client_socket), owner_extension_id, is_connected), | 351 : TCPSocket(std::move(tcp_client_socket), owner_extension_id, is_connected), |
| 351 persistent_(false), | 352 persistent_(false), |
| 352 buffer_size_(0), | 353 buffer_size_(0), |
| 353 paused_(false) {} | 354 paused_(false) {} |
| 354 | 355 |
| 355 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } | 356 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } |
| 356 | 357 |
| 357 ResumableTCPServerSocket::ResumableTCPServerSocket( | 358 ResumableTCPServerSocket::ResumableTCPServerSocket( |
| 358 const std::string& owner_extension_id) | 359 const std::string& owner_extension_id) |
| 359 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} | 360 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} |
| 360 | 361 |
| 361 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } | 362 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } |
| 362 | 363 |
| 363 } // namespace extensions | 364 } // namespace extensions |
| OLD | NEW |