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