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 TCPSocket::TCPSocket(const std::string& address, int port, | 17 TCPSocket::TCPSocket(APIResourceEventNotifier* event_notifier) |
18 APIResourceEventNotifier* event_notifier) | 18 : Socket(event_notifier) { |
19 : Socket(address, port, event_notifier) { | |
20 net::IPAddressNumber ip_number; | |
21 if (net::ParseIPLiteralToNumber(address, &ip_number)) { | |
22 net::AddressList address_list = | |
23 net::AddressList::CreateFromIPAddress(ip_number, port); | |
24 socket_.reset(new net::TCPClientSocket(address_list, NULL, | |
25 net::NetLog::Source())); | |
26 } | |
27 } | 19 } |
28 | 20 |
29 // For testing. | 21 // For testing. |
30 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, | 22 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, |
31 const std::string& address, int port, | |
32 APIResourceEventNotifier* event_notifier) | 23 APIResourceEventNotifier* event_notifier) |
33 : Socket(address, port, event_notifier), | 24 : Socket(event_notifier), |
34 socket_(tcp_client_socket) { | 25 socket_(tcp_client_socket) { |
35 } | 26 } |
36 | 27 |
37 // static | 28 // static |
38 TCPSocket* TCPSocket::CreateSocketForTesting( | 29 TCPSocket* TCPSocket::CreateSocketForTesting( |
39 net::TCPClientSocket* tcp_client_socket, | 30 net::TCPClientSocket* tcp_client_socket, |
40 const std::string& address, int port, | |
41 APIResourceEventNotifier* event_notifier) { | 31 APIResourceEventNotifier* event_notifier) { |
42 return new TCPSocket(tcp_client_socket, address, port, event_notifier); | 32 return new TCPSocket(tcp_client_socket, event_notifier); |
43 } | 33 } |
44 | 34 |
45 TCPSocket::~TCPSocket() { | 35 TCPSocket::~TCPSocket() { |
46 if (is_connected_) { | 36 if (is_connected_) { |
47 Disconnect(); | 37 Disconnect(); |
48 } | 38 } |
49 } | 39 } |
50 | 40 |
51 bool TCPSocket::IsValid() { | 41 int TCPSocket::Connect(const std::string& address, int port) { |
52 return socket_ != NULL; | 42 if (is_connected_) |
53 } | 43 return net::ERR_CONNECTION_FAILED; |
54 | 44 |
55 net::Socket* TCPSocket::socket() { | 45 net::AddressList address_list; |
56 return socket_.get(); | 46 if (!StringAndPortToAddressList(address, port, &address_list)) |
57 } | 47 return net::ERR_INVALID_ARGUMENT; |
58 | 48 |
59 int TCPSocket::Connect() { | 49 socket_.reset(new net::TCPClientSocket(address_list, NULL, |
| 50 net::NetLog::Source())); |
| 51 |
60 int result = socket_->Connect(base::Bind( | 52 int result = socket_->Connect(base::Bind( |
61 &TCPSocket::OnConnect, base::Unretained(this))); | 53 &TCPSocket::OnConnect, base::Unretained(this))); |
62 is_connected_ = result == net::OK; | 54 if (result == net::OK) { |
| 55 is_connected_ = true; |
| 56 } |
63 return result; | 57 return result; |
64 } | 58 } |
65 | 59 |
66 void TCPSocket::Disconnect() { | 60 void TCPSocket::Disconnect() { |
67 is_connected_ = false; | 61 is_connected_ = false; |
68 socket_->Disconnect(); | 62 socket_->Disconnect(); |
69 } | 63 } |
70 | 64 |
| 65 int TCPSocket::Bind(const std::string& address, int port) { |
| 66 // TODO(penghuang): Supports bind for tcp? |
| 67 return net::ERR_FAILED; |
| 68 } |
| 69 |
| 70 int TCPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) { |
| 71 return socket_->Read( |
| 72 io_buffer.get(), |
| 73 io_buffer_len, |
| 74 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, |
| 75 static_cast<net::IPEndPoint*>(NULL))); |
| 76 } |
| 77 |
| 78 int TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { |
| 79 return socket_->Write( |
| 80 io_buffer.get(), byte_count, |
| 81 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
| 82 } |
| 83 |
| 84 int TCPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, |
| 85 int io_buffer_len, |
| 86 net::IPEndPoint* address) { |
| 87 return net::ERR_FAILED; |
| 88 } |
| 89 |
| 90 int TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 91 int byte_count, |
| 92 const std::string& address, |
| 93 int port) { |
| 94 return net::ERR_FAILED; |
| 95 } |
| 96 |
71 void TCPSocket::OnConnect(int result) { | 97 void TCPSocket::OnConnect(int result) { |
72 is_connected_ = result == net::OK; | 98 is_connected_ = result == net::OK; |
73 event_notifier()->OnConnectComplete(result); | 99 event_notifier()->OnConnectComplete(result); |
74 } | 100 } |
75 | 101 |
76 } // namespace extensions | 102 } // namespace extensions |
OLD | NEW |