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