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