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 "extensions/browser/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 "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/base/rand_callback.h" | 11 #include "net/base/rand_callback.h" |
12 #include "net/socket/tcp_client_socket.h" | 12 #include "net/socket/tcp_client_socket.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
(...skipping 17 matching lines...) Expand all Loading... |
33 LAZY_INSTANCE_INITIALIZER; | 33 LAZY_INSTANCE_INITIALIZER; |
34 | 34 |
35 // static | 35 // static |
36 template <> | 36 template <> |
37 BrowserContextKeyedAPIFactory<ApiResourceManager<ResumableTCPServerSocket> >* | 37 BrowserContextKeyedAPIFactory<ApiResourceManager<ResumableTCPServerSocket> >* |
38 ApiResourceManager<ResumableTCPServerSocket>::GetFactoryInstance() { | 38 ApiResourceManager<ResumableTCPServerSocket>::GetFactoryInstance() { |
39 return g_server_factory.Pointer(); | 39 return g_server_factory.Pointer(); |
40 } | 40 } |
41 | 41 |
42 TCPSocket::TCPSocket(const std::string& owner_extension_id) | 42 TCPSocket::TCPSocket(const std::string& owner_extension_id) |
43 : Socket(owner_extension_id), | 43 : Socket(owner_extension_id), socket_mode_(UNKNOWN) {} |
44 socket_mode_(UNKNOWN) { | |
45 } | |
46 | 44 |
47 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, | 45 TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket, |
48 const std::string& owner_extension_id, | 46 const std::string& owner_extension_id, |
49 bool is_connected) | 47 bool is_connected) |
50 : Socket(owner_extension_id), | 48 : Socket(owner_extension_id), |
51 socket_(tcp_client_socket), | 49 socket_(tcp_client_socket), |
52 socket_mode_(CLIENT) { | 50 socket_mode_(CLIENT) { |
53 this->is_connected_ = is_connected; | 51 this->is_connected_ = is_connected; |
54 } | 52 } |
55 | 53 |
56 TCPSocket::TCPSocket(net::TCPServerSocket* tcp_server_socket, | 54 TCPSocket::TCPSocket(net::TCPServerSocket* tcp_server_socket, |
57 const std::string& owner_extension_id) | 55 const std::string& owner_extension_id) |
58 : Socket(owner_extension_id), | 56 : Socket(owner_extension_id), |
59 server_socket_(tcp_server_socket), | 57 server_socket_(tcp_server_socket), |
60 socket_mode_(SERVER) { | 58 socket_mode_(SERVER) {} |
61 } | |
62 | 59 |
63 // static | 60 // static |
64 TCPSocket* TCPSocket::CreateSocketForTesting( | 61 TCPSocket* TCPSocket::CreateSocketForTesting( |
65 net::TCPClientSocket* tcp_client_socket, | 62 net::TCPClientSocket* tcp_client_socket, |
66 const std::string& owner_extension_id, | 63 const std::string& owner_extension_id, |
67 bool is_connected) { | 64 bool is_connected) { |
68 return new TCPSocket(tcp_client_socket, owner_extension_id, is_connected); | 65 return new TCPSocket(tcp_client_socket, owner_extension_id, is_connected); |
69 } | 66 } |
70 | 67 |
71 // static | 68 // static |
72 TCPSocket* TCPSocket::CreateServerSocketForTesting( | 69 TCPSocket* TCPSocket::CreateServerSocketForTesting( |
73 net::TCPServerSocket* tcp_server_socket, | 70 net::TCPServerSocket* tcp_server_socket, |
74 const std::string& owner_extension_id) { | 71 const std::string& owner_extension_id) { |
75 return new TCPSocket(tcp_server_socket, owner_extension_id); | 72 return new TCPSocket(tcp_server_socket, owner_extension_id); |
76 } | 73 } |
77 | 74 |
78 TCPSocket::~TCPSocket() { | 75 TCPSocket::~TCPSocket() { Disconnect(); } |
79 Disconnect(); | |
80 } | |
81 | 76 |
82 void TCPSocket::Connect(const std::string& address, | 77 void TCPSocket::Connect(const std::string& address, |
83 int port, | 78 int port, |
84 const CompletionCallback& callback) { | 79 const CompletionCallback& callback) { |
85 DCHECK(!callback.is_null()); | 80 DCHECK(!callback.is_null()); |
86 | 81 |
87 if (socket_mode_ == SERVER || !connect_callback_.is_null()) { | 82 if (socket_mode_ == SERVER || !connect_callback_.is_null()) { |
88 callback.Run(net::ERR_CONNECTION_FAILED); | 83 callback.Run(net::ERR_CONNECTION_FAILED); |
89 return; | 84 return; |
90 } | 85 } |
91 DCHECK(!server_socket_.get()); | 86 DCHECK(!server_socket_.get()); |
92 socket_mode_ = CLIENT; | 87 socket_mode_ = CLIENT; |
93 connect_callback_ = callback; | 88 connect_callback_ = callback; |
94 | 89 |
95 int result = net::ERR_CONNECTION_FAILED; | 90 int result = net::ERR_CONNECTION_FAILED; |
96 do { | 91 do { |
97 if (is_connected_) | 92 if (is_connected_) |
98 break; | 93 break; |
99 | 94 |
100 net::AddressList address_list; | 95 net::AddressList address_list; |
101 if (!StringAndPortToAddressList(address, port, &address_list)) { | 96 if (!StringAndPortToAddressList(address, port, &address_list)) { |
102 result = net::ERR_ADDRESS_INVALID; | 97 result = net::ERR_ADDRESS_INVALID; |
103 break; | 98 break; |
104 } | 99 } |
105 | 100 |
106 socket_.reset(new net::TCPClientSocket(address_list, NULL, | 101 socket_.reset( |
107 net::NetLog::Source())); | 102 new net::TCPClientSocket(address_list, NULL, net::NetLog::Source())); |
108 | 103 |
109 connect_callback_ = callback; | 104 connect_callback_ = callback; |
110 result = socket_->Connect(base::Bind( | 105 result = socket_->Connect( |
111 &TCPSocket::OnConnectComplete, base::Unretained(this))); | 106 base::Bind(&TCPSocket::OnConnectComplete, base::Unretained(this))); |
112 } while (false); | 107 } while (false); |
113 | 108 |
114 if (result != net::ERR_IO_PENDING) | 109 if (result != net::ERR_IO_PENDING) |
115 OnConnectComplete(result); | 110 OnConnectComplete(result); |
116 } | 111 } |
117 | 112 |
118 void TCPSocket::Disconnect() { | 113 void TCPSocket::Disconnect() { |
119 is_connected_ = false; | 114 is_connected_ = false; |
120 if (socket_.get()) | 115 if (socket_.get()) |
121 socket_->Disconnect(); | 116 socket_->Disconnect(); |
122 server_socket_.reset(NULL); | 117 server_socket_.reset(NULL); |
123 connect_callback_.Reset(); | 118 connect_callback_.Reset(); |
124 read_callback_.Reset(); | 119 read_callback_.Reset(); |
125 accept_callback_.Reset(); | 120 accept_callback_.Reset(); |
126 accept_socket_.reset(NULL); | 121 accept_socket_.reset(NULL); |
127 } | 122 } |
128 | 123 |
129 int TCPSocket::Bind(const std::string& address, int port) { | 124 int TCPSocket::Bind(const std::string& address, int port) { |
130 return net::ERR_FAILED; | 125 return net::ERR_FAILED; |
131 } | 126 } |
132 | 127 |
133 void TCPSocket::Read(int count, | 128 void TCPSocket::Read(int count, const ReadCompletionCallback& callback) { |
134 const ReadCompletionCallback& callback) { | |
135 DCHECK(!callback.is_null()); | 129 DCHECK(!callback.is_null()); |
136 | 130 |
137 if (socket_mode_ != CLIENT) { | 131 if (socket_mode_ != CLIENT) { |
138 callback.Run(net::ERR_FAILED, NULL); | 132 callback.Run(net::ERR_FAILED, NULL); |
139 return; | 133 return; |
140 } | 134 } |
141 | 135 |
142 if (!read_callback_.is_null()) { | 136 if (!read_callback_.is_null()) { |
143 callback.Run(net::ERR_IO_PENDING, NULL); | 137 callback.Run(net::ERR_IO_PENDING, NULL); |
144 return; | 138 return; |
145 } | 139 } |
146 | 140 |
147 if (count < 0) { | 141 if (count < 0) { |
148 callback.Run(net::ERR_INVALID_ARGUMENT, NULL); | 142 callback.Run(net::ERR_INVALID_ARGUMENT, NULL); |
149 return; | 143 return; |
150 } | 144 } |
151 | 145 |
152 if (!socket_.get() || !IsConnected()) { | 146 if (!socket_.get() || !IsConnected()) { |
153 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL); | 147 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL); |
154 return; | 148 return; |
155 } | 149 } |
156 | 150 |
157 read_callback_ = callback; | 151 read_callback_ = callback; |
158 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count); | 152 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count); |
159 int result = socket_->Read(io_buffer.get(), count, | 153 int result = socket_->Read( |
160 base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this), | 154 io_buffer.get(), |
161 io_buffer)); | 155 count, |
| 156 base::Bind( |
| 157 &TCPSocket::OnReadComplete, base::Unretained(this), io_buffer)); |
162 | 158 |
163 if (result != net::ERR_IO_PENDING) | 159 if (result != net::ERR_IO_PENDING) |
164 OnReadComplete(io_buffer, result); | 160 OnReadComplete(io_buffer, result); |
165 } | 161 } |
166 | 162 |
167 void TCPSocket::RecvFrom(int count, | 163 void TCPSocket::RecvFrom(int count, |
168 const RecvFromCompletionCallback& callback) { | 164 const RecvFromCompletionCallback& callback) { |
169 callback.Run(net::ERR_FAILED, NULL, NULL, 0); | 165 callback.Run(net::ERR_FAILED, NULL, NULL, 0); |
170 } | 166 } |
171 | 167 |
(...skipping 10 matching lines...) Expand all Loading... |
182 return false; | 178 return false; |
183 return socket_->SetKeepAlive(enable, delay); | 179 return socket_->SetKeepAlive(enable, delay); |
184 } | 180 } |
185 | 181 |
186 bool TCPSocket::SetNoDelay(bool no_delay) { | 182 bool TCPSocket::SetNoDelay(bool no_delay) { |
187 if (!socket_.get()) | 183 if (!socket_.get()) |
188 return false; | 184 return false; |
189 return socket_->SetNoDelay(no_delay); | 185 return socket_->SetNoDelay(no_delay); |
190 } | 186 } |
191 | 187 |
192 int TCPSocket::Listen(const std::string& address, int port, int backlog, | 188 int TCPSocket::Listen(const std::string& address, |
| 189 int port, |
| 190 int backlog, |
193 std::string* error_msg) { | 191 std::string* error_msg) { |
194 if (socket_mode_ == CLIENT) { | 192 if (socket_mode_ == CLIENT) { |
195 *error_msg = kTCPSocketTypeInvalidError; | 193 *error_msg = kTCPSocketTypeInvalidError; |
196 return net::ERR_NOT_IMPLEMENTED; | 194 return net::ERR_NOT_IMPLEMENTED; |
197 } | 195 } |
198 DCHECK(!socket_.get()); | 196 DCHECK(!socket_.get()); |
199 socket_mode_ = SERVER; | 197 socket_mode_ = SERVER; |
200 | 198 |
201 scoped_ptr<net::IPEndPoint> bind_address(new net::IPEndPoint()); | 199 scoped_ptr<net::IPEndPoint> bind_address(new net::IPEndPoint()); |
202 if (!StringAndPortToIPEndPoint(address, port, bind_address.get())) | 200 if (!StringAndPortToIPEndPoint(address, port, bind_address.get())) |
203 return net::ERR_INVALID_ARGUMENT; | 201 return net::ERR_INVALID_ARGUMENT; |
204 | 202 |
205 if (!server_socket_.get()) { | 203 if (!server_socket_.get()) { |
206 server_socket_.reset(new net::TCPServerSocket(NULL, | 204 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source())); |
207 net::NetLog::Source())); | |
208 } | 205 } |
209 int result = server_socket_->Listen(*bind_address, backlog); | 206 int result = server_socket_->Listen(*bind_address, backlog); |
210 if (result) | 207 if (result) |
211 *error_msg = kSocketListenError; | 208 *error_msg = kSocketListenError; |
212 return result; | 209 return result; |
213 } | 210 } |
214 | 211 |
215 void TCPSocket::Accept(const AcceptCompletionCallback &callback) { | 212 void TCPSocket::Accept(const AcceptCompletionCallback& callback) { |
216 if (socket_mode_ != SERVER || !server_socket_.get()) { | 213 if (socket_mode_ != SERVER || !server_socket_.get()) { |
217 callback.Run(net::ERR_FAILED, NULL); | 214 callback.Run(net::ERR_FAILED, NULL); |
218 return; | 215 return; |
219 } | 216 } |
220 | 217 |
221 // Limits to only 1 blocked accept call. | 218 // Limits to only 1 blocked accept call. |
222 if (!accept_callback_.is_null()) { | 219 if (!accept_callback_.is_null()) { |
223 callback.Run(net::ERR_FAILED, NULL); | 220 callback.Run(net::ERR_FAILED, NULL); |
224 return; | 221 return; |
225 } | 222 } |
226 | 223 |
227 int result = server_socket_->Accept(&accept_socket_, base::Bind( | 224 int result = server_socket_->Accept( |
228 &TCPSocket::OnAccept, base::Unretained(this))); | 225 &accept_socket_, |
| 226 base::Bind(&TCPSocket::OnAccept, base::Unretained(this))); |
229 if (result == net::ERR_IO_PENDING) { | 227 if (result == net::ERR_IO_PENDING) { |
230 accept_callback_ = callback; | 228 accept_callback_ = callback; |
231 } else if (result == net::OK) { | 229 } else if (result == net::OK) { |
232 accept_callback_ = callback; | 230 accept_callback_ = callback; |
233 this->OnAccept(result); | 231 this->OnAccept(result); |
234 } else { | 232 } else { |
235 callback.Run(result, NULL); | 233 callback.Run(result, NULL); |
236 } | 234 } |
237 } | 235 } |
238 | 236 |
(...skipping 11 matching lines...) Expand all Loading... |
250 bool TCPSocket::GetLocalAddress(net::IPEndPoint* address) { | 248 bool TCPSocket::GetLocalAddress(net::IPEndPoint* address) { |
251 if (socket_.get()) { | 249 if (socket_.get()) { |
252 return !socket_->GetLocalAddress(address); | 250 return !socket_->GetLocalAddress(address); |
253 } else if (server_socket_.get()) { | 251 } else if (server_socket_.get()) { |
254 return !server_socket_->GetLocalAddress(address); | 252 return !server_socket_->GetLocalAddress(address); |
255 } else { | 253 } else { |
256 return false; | 254 return false; |
257 } | 255 } |
258 } | 256 } |
259 | 257 |
260 Socket::SocketType TCPSocket::GetSocketType() const { | 258 Socket::SocketType TCPSocket::GetSocketType() const { return Socket::TYPE_TCP; } |
261 return Socket::TYPE_TCP; | |
262 } | |
263 | 259 |
264 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer, | 260 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer, |
265 int io_buffer_size, | 261 int io_buffer_size, |
266 const net::CompletionCallback& callback) { | 262 const net::CompletionCallback& callback) { |
267 if (socket_mode_ != CLIENT) | 263 if (socket_mode_ != CLIENT) |
268 return net::ERR_FAILED; | 264 return net::ERR_FAILED; |
269 else if (!socket_.get() || !IsConnected()) | 265 else if (!socket_.get() || !IsConnected()) |
270 return net::ERR_SOCKET_NOT_CONNECTED; | 266 return net::ERR_SOCKET_NOT_CONNECTED; |
271 else | 267 else |
272 return socket_->Write(io_buffer, io_buffer_size, callback); | 268 return socket_->Write(io_buffer, io_buffer_size, callback); |
273 } | 269 } |
274 | 270 |
275 void TCPSocket::RefreshConnectionStatus() { | 271 void TCPSocket::RefreshConnectionStatus() { |
276 if (!is_connected_) return; | 272 if (!is_connected_) |
277 if (server_socket_) return; | 273 return; |
| 274 if (server_socket_) |
| 275 return; |
278 if (!socket_->IsConnected()) { | 276 if (!socket_->IsConnected()) { |
279 Disconnect(); | 277 Disconnect(); |
280 } | 278 } |
281 } | 279 } |
282 | 280 |
283 void TCPSocket::OnConnectComplete(int result) { | 281 void TCPSocket::OnConnectComplete(int result) { |
284 DCHECK(!connect_callback_.is_null()); | 282 DCHECK(!connect_callback_.is_null()); |
285 DCHECK(!is_connected_); | 283 DCHECK(!is_connected_); |
286 is_connected_ = result == net::OK; | 284 is_connected_ = result == net::OK; |
287 connect_callback_.Run(result); | 285 connect_callback_.Run(result); |
(...skipping 15 matching lines...) Expand all Loading... |
303 } else { | 301 } else { |
304 accept_callback_.Run(result, NULL); | 302 accept_callback_.Run(result, NULL); |
305 } | 303 } |
306 accept_callback_.Reset(); | 304 accept_callback_.Reset(); |
307 } | 305 } |
308 | 306 |
309 ResumableTCPSocket::ResumableTCPSocket(const std::string& owner_extension_id) | 307 ResumableTCPSocket::ResumableTCPSocket(const std::string& owner_extension_id) |
310 : TCPSocket(owner_extension_id), | 308 : TCPSocket(owner_extension_id), |
311 persistent_(false), | 309 persistent_(false), |
312 buffer_size_(0), | 310 buffer_size_(0), |
313 paused_(false) { | 311 paused_(false) {} |
314 } | |
315 | 312 |
316 ResumableTCPSocket::ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, | 313 ResumableTCPSocket::ResumableTCPSocket(net::TCPClientSocket* tcp_client_socket, |
317 const std::string& owner_extension_id, | 314 const std::string& owner_extension_id, |
318 bool is_connected) | 315 bool is_connected) |
319 : TCPSocket(tcp_client_socket, owner_extension_id, is_connected), | 316 : TCPSocket(tcp_client_socket, owner_extension_id, is_connected), |
320 persistent_(false), | 317 persistent_(false), |
321 buffer_size_(0), | 318 buffer_size_(0), |
322 paused_(false) { | 319 paused_(false) {} |
323 } | |
324 | 320 |
325 bool ResumableTCPSocket::IsPersistent() const { | 321 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } |
326 return persistent(); | |
327 } | |
328 | 322 |
329 ResumableTCPServerSocket::ResumableTCPServerSocket( | 323 ResumableTCPServerSocket::ResumableTCPServerSocket( |
330 const std::string& owner_extension_id) | 324 const std::string& owner_extension_id) |
331 : TCPSocket(owner_extension_id), | 325 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} |
332 persistent_(false), | |
333 paused_(false) { | |
334 } | |
335 | 326 |
336 bool ResumableTCPServerSocket::IsPersistent() const { | 327 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } |
337 return persistent(); | |
338 } | |
339 | 328 |
340 } // namespace extensions | 329 } // namespace extensions |
OLD | NEW |