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" |
12 | 13 |
13 using extensions::ResumableTCPSocket; | 14 using extensions::ResumableTCPSocket; |
14 using extensions::api::sockets_tcp::SocketInfo; | 15 using extensions::api::sockets_tcp::SocketInfo; |
15 using extensions::api::sockets_tcp::SocketProperties; | 16 using extensions::api::sockets_tcp::SocketProperties; |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char kSocketNotFoundError[] = "Socket not found"; | 20 const char kSocketNotFoundError[] = "Socket not found"; |
20 const char kPermissionError[] = "Does not have permission"; | 21 const char kPermissionError[] = "Does not have permission"; |
22 const char kSecureSocketTypeError[] = | |
23 "Only TCP sockets are supported for TLS."; | |
rpaquay
2013/12/16 20:18:46
This message should not be needed (use DCHEK inste
lally
2013/12/16 22:20:07
Ah, fixed. I was actually trying to stop already-
| |
24 const char kSocketNotConnectedError[] = "Socket not connected"; | |
21 | 25 |
22 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, | 26 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, |
23 ResumableTCPSocket* socket) { | 27 ResumableTCPSocket* socket) { |
24 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | 28 linked_ptr<SocketInfo> socket_info(new SocketInfo()); |
25 // This represents what we know about the socket, and does not call through | 29 // This represents what we know about the socket, and does not call through |
26 // to the system. | 30 // to the system. |
27 socket_info->socket_id = socket_id; | 31 socket_info->socket_id = socket_id; |
28 if (!socket->name().empty()) { | 32 if (!socket->name().empty()) { |
29 socket_info->name.reset(new std::string(socket->name())); | 33 socket_info->name.reset(new std::string(socket->name())); |
30 } | 34 } |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 int socket_id = *it; | 437 int socket_id = *it; |
434 ResumableTCPSocket* socket = GetTcpSocket(socket_id); | 438 ResumableTCPSocket* socket = GetTcpSocket(socket_id); |
435 if (socket) { | 439 if (socket) { |
436 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 440 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
437 } | 441 } |
438 } | 442 } |
439 } | 443 } |
440 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); | 444 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); |
441 } | 445 } |
442 | 446 |
447 SocketsTcpSecureFunction::SocketsTcpSecureFunction() {} | |
448 SocketsTcpSecureFunction::~SocketsTcpSecureFunction() {} | |
449 | |
450 bool SocketsTcpSecureFunction::Prepare() { | |
451 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
452 params_ = api::socket::Secure::Params::Create(*args_); | |
453 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
454 url_request_getter_ = GetProfile()->GetRequestContext(); | |
455 return true; | |
456 } | |
457 | |
458 // Override the regular implementation, which would call AsyncWorkCompleted | |
459 // immediately after Work(). | |
460 void SocketsTcpSecureFunction::AsyncWorkStart() { | |
461 int result = net::ERR_INVALID_ARGUMENT; | |
462 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
463 | |
464 Socket* socket = GetSocket(params_->socket_id); | |
465 Profile* profile = GetProfile(); | |
466 DCHECK(profile); | |
467 | |
468 if (!socket) { | |
469 SetResult(new base::FundamentalValue(result)); | |
470 error_ = kSocketNotFoundError; | |
471 AsyncWorkCompleted(); | |
472 return; | |
473 } | |
474 | |
475 TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket); | |
476 if (socket->GetSocketType() != Socket::TYPE_TCP | |
rpaquay
2013/12/16 20:18:46
The check for TYPE_TCP should be DCHECK, because i
lally
2013/12/16 22:20:07
I've added some comments and clarified this part.
| |
477 || tcp_socket->ClientStream() == NULL) { | |
478 SetResult(new base::FundamentalValue(result)); | |
479 error_ = kSecureSocketTypeError; | |
480 AsyncWorkCompleted(); | |
481 return; | |
482 } | |
483 | |
484 if (!socket->IsConnected()) { | |
485 SetResult(new base::FundamentalValue(result)); | |
486 error_ = kSocketNotConnectedError; | |
487 AsyncWorkCompleted(); | |
488 return; | |
489 } | |
490 | |
491 TLSSocket::SecureTCPSocket( | |
492 socket, profile, url_request_getter_, extension_id(), | |
493 params_->options.get(), | |
494 base::Bind(&SocketsTcpSecureFunction::TlsConnectDone, | |
495 this)); | |
496 } | |
497 | |
498 void SocketsTcpSecureFunction::TlsConnectDone(TLSSocket* sock, int result) { | |
499 SetResult(new base::FundamentalValue(result)); | |
rpaquay
2013/12/16 20:18:46
This call is not needed if you always use "results
lally
2013/12/16 22:20:07
Ok, done. Thanks.
| |
500 if (sock) { | |
501 SetSocket(params_->socket_id, sock); | |
502 } else { | |
503 RemoveSocket(params_->socket_id); | |
504 } | |
505 | |
506 if (result != net::OK) { | |
507 error_ = net::ErrorToString(result); | |
508 results_ = api::socket::Secure::Results::Create(result); | |
rpaquay
2013/12/16 20:18:46
1) move this out of the if block
2) RHS should be
lally
2013/12/16 22:20:07
Done.
| |
509 } | |
510 AsyncWorkCompleted(); | |
511 } | |
512 | |
443 } // namespace api | 513 } // namespace api |
444 } // namespace extensions | 514 } // namespace extensions |
OLD | NEW |