Index: extensions/browser/api/socket/socket_api.cc |
diff --git a/extensions/browser/api/socket/socket_api.cc b/extensions/browser/api/socket/socket_api.cc |
index 1c41ae598ebca93fcf2940797912e533bba74be8..6c6a48ac64957ff346858116a42db714bdfef1e0 100644 |
--- a/extensions/browser/api/socket/socket_api.cc |
+++ b/extensions/browser/api/socket/socket_api.cc |
@@ -13,6 +13,7 @@ |
#include "extensions/browser/api/dns/host_resolver_wrapper.h" |
#include "extensions/browser/api/socket/socket.h" |
#include "extensions/browser/api/socket/tcp_socket.h" |
+#include "extensions/browser/api/socket/tls_socket.h" |
#include "extensions/browser/api/socket/udp_socket.h" |
#include "extensions/browser/extension_system.h" |
#include "extensions/common/extension.h" |
@@ -24,6 +25,8 @@ |
#include "net/base/net_errors.h" |
#include "net/base/net_log.h" |
#include "net/base/net_util.h" |
+#include "net/url_request/url_request_context.h" |
+#include "net/url_request/url_request_context_getter.h" |
namespace extensions { |
@@ -43,6 +46,8 @@ const char kNetworkListError[] = "Network lookup failed or unsupported"; |
const char kTCPSocketBindError[] = |
"TCP socket does not support bind. For TCP server please use listen."; |
const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; |
+const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; |
+const char kSocketNotConnectedError[] = "Socket not connected"; |
const char kWildcardAddress[] = "*"; |
const int kWildcardPort = 0; |
@@ -71,6 +76,11 @@ Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { |
return manager_->Get(extension_->id(), api_resource_id); |
} |
+void SocketAsyncApiFunction::ReplaceSocket(int api_resource_id, |
+ Socket* socket) { |
+ manager_->Replace(extension_->id(), api_resource_id, socket); |
+} |
+ |
base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
return manager_->GetResourceIds(extension_->id()); |
} |
@@ -194,6 +204,8 @@ void SocketConnectFunction::AsyncWorkStart() { |
return; |
} |
+ socket_->set_hostname(hostname_); |
+ |
SocketPermissionRequest::OperationType operation_type; |
switch (socket_->GetSocketType()) { |
case Socket::TYPE_TCP: |
@@ -887,4 +899,76 @@ void SocketGetJoinedGroupsFunction::Work() { |
SetResult(values); |
} |
+SocketSecureFunction::SocketSecureFunction() { |
+} |
+ |
+SocketSecureFunction::~SocketSecureFunction() { |
+} |
+ |
+bool SocketSecureFunction::Prepare() { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ params_ = core_api::socket::Secure::Params::Create(*args_); |
+ EXTENSION_FUNCTION_VALIDATE(params_.get()); |
+ url_request_getter_ = browser_context()->GetRequestContext(); |
+ return true; |
+} |
+ |
+// Override the regular implementation, which would call AsyncWorkCompleted |
+// immediately after Work(). |
+void SocketSecureFunction::AsyncWorkStart() { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
+ |
+ Socket* socket = GetSocket(params_->socket_id); |
+ if (!socket) { |
+ SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
+ error_ = kSocketNotFoundError; |
+ AsyncWorkCompleted(); |
+ return; |
+ } |
+ |
+ // Make sure that the socket is a TCP client socket. |
+ if (socket->GetSocketType() != Socket::TYPE_TCP || |
+ static_cast<TCPSocket*>(socket)->ClientStream() == NULL) { |
+ SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
+ error_ = kSecureSocketTypeError; |
+ AsyncWorkCompleted(); |
+ return; |
+ } |
+ |
+ if (!socket->IsConnected()) { |
+ SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
+ error_ = kSocketNotConnectedError; |
+ AsyncWorkCompleted(); |
+ return; |
+ } |
+ |
+ net::URLRequestContext* url_request_context = |
+ url_request_getter_->GetURLRequestContext(); |
+ |
+ TLSSocket::UpgradeSocketToTLS( |
+ socket, |
+ url_request_context->ssl_config_service(), |
+ url_request_context->cert_verifier(), |
+ url_request_context->transport_security_state(), |
+ extension_id(), |
+ params_->options.get(), |
+ base::Bind(&SocketSecureFunction::TlsConnectDone, this)); |
+} |
+ |
+void SocketSecureFunction::TlsConnectDone(scoped_ptr<TLSSocket> socket, |
+ int result) { |
+ // if an error occurred, socket MUST be NULL. |
+ DCHECK(result == net::OK || socket == NULL); |
+ |
+ if (socket && result == net::OK) { |
+ ReplaceSocket(params_->socket_id, socket.release()); |
+ } else { |
+ RemoveSocket(params_->socket_id); |
+ error_ = net::ErrorToString(result); |
+ } |
+ |
+ results_ = core_api::socket::Secure::Results::Create(result); |
+ AsyncWorkCompleted(); |
+} |
+ |
} // namespace extensions |