Chromium Code Reviews| Index: chrome/browser/extensions/api/socket/socket_api.cc |
| diff --git a/chrome/browser/extensions/api/socket/socket_api.cc b/chrome/browser/extensions/api/socket/socket_api.cc |
| index da173c2952ae873f8068a4db783838efd5bf2225..a0a4738a9c365ced2b97631af37f1c2a37852328 100644 |
| --- a/chrome/browser/extensions/api/socket/socket_api.cc |
| +++ b/chrome/browser/extensions/api/socket/socket_api.cc |
| @@ -12,6 +12,7 @@ |
| #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| #include "chrome/browser/extensions/api/socket/socket.h" |
| #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| +#include "chrome/browser/extensions/api/socket/tls_socket.h" |
| #include "chrome/browser/extensions/api/socket/udp_socket.h" |
| #include "chrome/browser/extensions/extension_system.h" |
| #include "chrome/browser/io_thread.h" |
| @@ -46,6 +47,9 @@ const char kMulticastSocketTypeError[] = |
| "Only UDP socket supports multicast."; |
| const char kWildcardAddress[] = "*"; |
| const int kWildcardPort = 0; |
| +const char kSecureSocketTypeError[] = |
| + "Only TCP sockets are supported for TLS."; |
| +const char kSocketNotConnectedError[] = "Socket not connected"; |
| SocketAsyncApiFunction::SocketAsyncApiFunction() { |
| } |
| @@ -76,6 +80,11 @@ Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { |
| return manager_->Get(extension_->id(), api_resource_id); |
| } |
| +void SocketAsyncApiFunction::SetSocket(int api_resource_id, |
| + Socket* socket) { |
| + manager_->Set(extension_->id(), api_resource_id, socket); |
| +} |
| + |
| base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
| return manager_->GetResourceIds(extension_->id()); |
| } |
| @@ -239,6 +248,7 @@ void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
| } |
| void SocketConnectFunction::StartConnect() { |
| + socket_->set_hostname(hostname_); |
| socket_->Connect(resolved_address_, port_, |
| base::Bind(&SocketConnectFunction::OnConnect, this)); |
| } |
| @@ -896,4 +906,69 @@ void SocketGetJoinedGroupsFunction::Work() { |
| SetResult(values); |
| } |
| +SocketSecureFunction::SocketSecureFunction() {} |
| +SocketSecureFunction::~SocketSecureFunction() {} |
| + |
| +bool SocketSecureFunction::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 SocketSecureFunction::AsyncWorkStart() { |
| + int result = net::ERR_INVALID_ARGUMENT; |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + |
| + Socket* socket = GetSocket(params_->socket_id); |
| + Profile* profile = GetProfile(); |
|
rpaquay
2013/12/16 20:18:46
nit: move this closer to first usage.
lally
2013/12/16 22:20:07
Done.
|
| + DCHECK(profile); |
| + |
| + if (!socket) { |
| + SetResult(new base::FundamentalValue(result)); |
| + error_ = kSocketNotFoundError; |
| + AsyncWorkCompleted(); |
| + return; |
| + } |
| + |
| + TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket); |
| + if (socket->GetSocketType() != Socket::TYPE_TCP |
| + || tcp_socket->ClientStream() == NULL) { |
| + SetResult(new base::FundamentalValue(result)); |
| + error_ = kSecureSocketTypeError; |
| + 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(&SocketSecureFunction::TlsConnectDone, |
| + this)); |
| +} |
| + |
| +void SocketSecureFunction::TlsConnectDone(TLSSocket* sock, int result) { |
| + SetResult(new base::FundamentalValue(result)); |
|
rpaquay
2013/12/16 20:18:46
remove this line (see comment below)
lally
2013/12/16 22:20:07
Done.
|
| + if (sock) { |
| + SetSocket(params_->socket_id, sock); |
| + } else { |
| + RemoveSocket(params_->socket_id); |
| + } |
| + |
| + if (result != net::OK) { |
| + error_ = net::ErrorToString(result); |
| + results_ = api::socket::Secure::Results::Create(result); |
|
rpaquay
2013/12/16 20:18:46
move this out of the "if" block.
lally
2013/12/16 22:20:07
Done.
|
| + } |
| + AsyncWorkCompleted(); |
| +} |
| + |
| } // namespace extensions |