Chromium Code Reviews| Index: chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.cc |
| diff --git a/chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.cc b/chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.cc |
| index fbf02dc0a1e0561a3a3e7ed3c3f0399a2e7e4974..b83478ac91881c8a908909c554e6342b41c0f4cd 100644 |
| --- a/chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.cc |
| +++ b/chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.cc |
| @@ -5,10 +5,12 @@ |
| #include "chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h" |
| #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| +#include "chrome/browser/extensions/api/socket/tls_socket.h" |
| #include "chrome/browser/extensions/api/sockets_tcp/tcp_socket_event_dispatcher.h" |
| #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" |
| #include "content/public/common/socket_permission_request.h" |
| #include "net/base/net_errors.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| using extensions::ResumableTCPSocket; |
| using extensions::api::sockets_tcp::SocketInfo; |
| @@ -18,6 +20,9 @@ namespace { |
| const char kSocketNotFoundError[] = "Socket not found"; |
| const char kPermissionError[] = "Does not have permission"; |
| +const char kInvalidSocketStateError[] = |
| + "Socket must be a connected client TCP socket."; |
| +const char kSocketNotConnectedError[] = "Socket not connected"; |
| linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, |
| ResumableTCPSocket* socket) { |
| @@ -440,5 +445,74 @@ void SocketsTcpGetSocketsFunction::Work() { |
| results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); |
| } |
| +SocketsTcpSecureFunction::SocketsTcpSecureFunction() {} |
| +SocketsTcpSecureFunction::~SocketsTcpSecureFunction() {} |
| + |
| +bool SocketsTcpSecureFunction::Prepare() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + params_ = api::socket::Secure::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + url_request_getter_ = GetProfile()->GetRequestContext(); |
| + return true; |
| +} |
| + |
| +// Override the regular implementation, which would call AsyncWorkCompleted |
| +// immediately after Work(). |
| +void SocketsTcpSecureFunction::AsyncWorkStart() { |
| + int result = net::ERR_INVALID_ARGUMENT; |
|
Ryan Sleevi
2013/12/17 00:37:40
style: Previous comments about using |reuslt| here
lally
2014/01/09 18:47:16
Done.
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + |
| + ResumableTCPSocket* socket = GetTcpSocket(params_->socket_id); |
| + Profile* profile = GetProfile(); |
| + DCHECK(profile); |
| + |
| + if (!socket) { |
| + SetResult(new base::FundamentalValue(result)); |
| + error_ = kSocketNotFoundError; |
| + AsyncWorkCompleted(); |
| + return; |
| + } |
| + |
| + // Make sure it's a connected TCP client socket. Error out if it's already |
| + // secure()'d. |
| + if (socket->GetSocketType() == Socket::TYPE_TLS |
| + || !socket->IsConnected() |
| + || socket->ClientStream() == NULL) { |
|
Ryan Sleevi
2013/12/17 00:37:40
style: Conditionals on preceding line.
lally
2014/01/09 18:47:16
Done.
|
| + SetResult(new base::FundamentalValue(result)); |
| + error_ = kInvalidSocketStateError; |
| + AsyncWorkCompleted(); |
| + return; |
| + } |
| + |
| + if (!socket->IsConnected()) { |
| + SetResult(new base::FundamentalValue(result)); |
| + error_ = kSocketNotConnectedError; |
| + AsyncWorkCompleted(); |
| + return; |
| + } |
| + |
| + TLSSocket::SecureTCPSocket( |
| + socket, profile, url_request_getter_, extension_id(), |
| + params_->options.get(), |
| + base::Bind(&SocketsTcpSecureFunction::TlsConnectDone, |
| + this)); |
| +} |
| + |
| +void SocketsTcpSecureFunction::TlsConnectDone(TLSSocket* sock, int result) { |
| + if (sock) { |
| + SetSocket(params_->socket_id, sock); |
| + } else { |
| + RemoveSocket(params_->socket_id); |
| + } |
| + |
| + url_request_getter_->Release(); |
| + |
| + results_ = api::sockets_tcp::Secure::Results::Create(result); |
| + if (result != net::OK) { |
| + error_ = net::ErrorToString(result); |
| + } |
| + AsyncWorkCompleted(); |
| +} |
| + |
| } // namespace api |
| } // namespace extensions |