OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/sockets_tcp/sockets_tcp_api.h" | 5 #include "chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 7 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 8 #include "chrome/browser/extensions/api/socket/tls_socket.h" |
8 #include "chrome/browser/extensions/api/sockets_tcp/tcp_socket_event_dispatcher.
h" | 9 #include "chrome/browser/extensions/api/sockets_tcp/tcp_socket_event_dispatcher.
h" |
9 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" | 10 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" |
10 #include "content/public/common/socket_permission_request.h" | 11 #include "content/public/common/socket_permission_request.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/url_request/url_request_context_getter.h" |
12 | 14 |
13 using extensions::ResumableTCPSocket; | 15 using extensions::ResumableTCPSocket; |
14 using extensions::api::sockets_tcp::SocketInfo; | 16 using extensions::api::sockets_tcp::SocketInfo; |
15 using extensions::api::sockets_tcp::SocketProperties; | 17 using extensions::api::sockets_tcp::SocketProperties; |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 const char kSocketNotFoundError[] = "Socket not found"; | 21 const char kSocketNotFoundError[] = "Socket not found"; |
20 const char kPermissionError[] = "Does not have permission"; | 22 const char kPermissionError[] = "Does not have permission"; |
| 23 const char kInvalidSocketStateError[] = |
| 24 "Socket must be a connected client TCP socket."; |
| 25 const char kSocketNotConnectedError[] = "Socket not connected"; |
21 | 26 |
22 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, | 27 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, |
23 ResumableTCPSocket* socket) { | 28 ResumableTCPSocket* socket) { |
24 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | 29 linked_ptr<SocketInfo> socket_info(new SocketInfo()); |
25 // This represents what we know about the socket, and does not call through | 30 // This represents what we know about the socket, and does not call through |
26 // to the system. | 31 // to the system. |
27 socket_info->socket_id = socket_id; | 32 socket_info->socket_id = socket_id; |
28 if (!socket->name().empty()) { | 33 if (!socket->name().empty()) { |
29 socket_info->name.reset(new std::string(socket->name())); | 34 socket_info->name.reset(new std::string(socket->name())); |
30 } | 35 } |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 } | 257 } |
253 | 258 |
254 void SocketsTcpConnectFunction::AsyncWorkStart() { | 259 void SocketsTcpConnectFunction::AsyncWorkStart() { |
255 ResumableTCPSocket* socket = GetTcpSocket(params_->socket_id); | 260 ResumableTCPSocket* socket = GetTcpSocket(params_->socket_id); |
256 if (!socket) { | 261 if (!socket) { |
257 error_ = kSocketNotFoundError; | 262 error_ = kSocketNotFoundError; |
258 AsyncWorkCompleted(); | 263 AsyncWorkCompleted(); |
259 return; | 264 return; |
260 } | 265 } |
261 | 266 |
| 267 socket->set_hostname(params_->peer_address); |
| 268 |
262 content::SocketPermissionRequest param( | 269 content::SocketPermissionRequest param( |
263 SocketPermissionRequest::TCP_CONNECT, | 270 SocketPermissionRequest::TCP_CONNECT, |
264 params_->peer_address, | 271 params_->peer_address, |
265 params_->peer_port); | 272 params_->peer_port); |
266 if (!SocketsManifestData::CheckRequest(GetExtension(), param)) { | 273 if (!SocketsManifestData::CheckRequest(GetExtension(), param)) { |
267 error_ = kPermissionError; | 274 error_ = kPermissionError; |
268 AsyncWorkCompleted(); | 275 AsyncWorkCompleted(); |
269 return; | 276 return; |
270 } | 277 } |
271 | 278 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 int socket_id = *it; | 440 int socket_id = *it; |
434 ResumableTCPSocket* socket = GetTcpSocket(socket_id); | 441 ResumableTCPSocket* socket = GetTcpSocket(socket_id); |
435 if (socket) { | 442 if (socket) { |
436 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 443 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
437 } | 444 } |
438 } | 445 } |
439 } | 446 } |
440 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); | 447 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); |
441 } | 448 } |
442 | 449 |
| 450 SocketsTcpSecureFunction::SocketsTcpSecureFunction() {} |
| 451 SocketsTcpSecureFunction::~SocketsTcpSecureFunction() {} |
| 452 |
| 453 bool SocketsTcpSecureFunction::Prepare() { |
| 454 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 455 params_ = api::socket::Secure::Params::Create(*args_); |
| 456 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 457 url_request_getter_ = GetProfile()->GetRequestContext(); |
| 458 return true; |
| 459 } |
| 460 |
| 461 // Override the regular implementation, which would call AsyncWorkCompleted |
| 462 // immediately after Work(). |
| 463 void SocketsTcpSecureFunction::AsyncWorkStart() { |
| 464 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 465 |
| 466 ResumableTCPSocket* socket(GetTcpSocket(params_->socket_id)); |
| 467 if (!socket) { |
| 468 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 469 error_ = kSocketNotFoundError; |
| 470 AsyncWorkCompleted(); |
| 471 return; |
| 472 } |
| 473 |
| 474 paused_ = socket->paused(); |
| 475 persistent_ = socket->persistent(); |
| 476 |
| 477 // Make sure it's a connected TCP client socket. Error out if it's already |
| 478 // secure()'d. |
| 479 if (socket->GetSocketType() == Socket::TYPE_TLS || |
| 480 socket->ClientStream() == NULL) { |
| 481 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 482 error_ = kInvalidSocketStateError; |
| 483 AsyncWorkCompleted(); |
| 484 return; |
| 485 } |
| 486 |
| 487 if (!socket->IsConnected()) { |
| 488 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 489 error_ = kSocketNotConnectedError; |
| 490 AsyncWorkCompleted(); |
| 491 return; |
| 492 } |
| 493 |
| 494 Profile* profile = GetProfile(); |
| 495 DCHECK(profile); |
| 496 |
| 497 TLSSocket::UpgradeSocketToTLS( |
| 498 socket, profile, url_request_getter_, extension_id(), |
| 499 params_->options.get(), |
| 500 base::Bind(&SocketsTcpSecureFunction::TlsConnectDone, |
| 501 this)); |
| 502 } |
| 503 |
| 504 void SocketsTcpSecureFunction::TlsConnectDone(TLSSocket* socket, int result) { |
| 505 // socket can only be non-null if result == net::OK. |
| 506 DCHECK(result == net::OK || socket == NULL); |
| 507 |
| 508 if (socket && result == net::OK) { |
| 509 socket->set_persistent(persistent_); |
| 510 socket->set_paused(paused_); |
| 511 SetSocket(params_->socket_id, socket); |
| 512 } else { |
| 513 RemoveSocket(params_->socket_id); |
| 514 error_ = net::ErrorToString(result); |
| 515 } |
| 516 |
| 517 results_ = api::sockets_tcp::Secure::Results::Create(result); |
| 518 url_request_getter_->Release(); |
| 519 AsyncWorkCompleted(); |
| 520 } |
| 521 |
443 } // namespace api | 522 } // namespace api |
444 } // namespace extensions | 523 } // namespace extensions |
OLD | NEW |