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..69adcf323d07f87bd55329be59973916adf5f58a 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" |
| @@ -24,6 +25,7 @@ |
| #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_getter.h" |
| namespace extensions { |
| @@ -46,6 +48,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 +81,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()); |
| } |
| @@ -203,6 +213,8 @@ void SocketConnectFunction::AsyncWorkStart() { |
| return; |
| } |
| + socket_->set_hostname(hostname_); |
| + |
| SocketPermissionRequest::OperationType operation_type; |
| switch (socket_->GetSocketType()) { |
| case Socket::TYPE_TCP: |
| @@ -896,4 +908,71 @@ 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() { |
| + 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 neither UDP nor already TLS. Also make |
| + // sure that it's a client stream. |
| + 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; |
| + } |
| + |
| + Profile* profile = GetProfile(); |
|
Ryan Sleevi
2014/02/04 22:28:14
I'm quite surprised this is safe. GetProfile() was
lally
2014/02/11 22:05:35
This isn't the normal GetProfile(). It's implemen
|
| + DCHECK(profile); |
| + |
| + TLSSocket::UpgradeSocketToTLS( |
| + socket, profile, url_request_getter_, extension_id(), |
| + params_->options.get(), base::Bind(&SocketSecureFunction::TlsConnectDone, |
| + this)); |
| +} |
| + |
| +void SocketSecureFunction::TlsConnectDone( |
| + scoped_ptr<TLSSocket> socket, int result) { |
| + // |socket| can only be non-null if |result| == net::OK. |
| + DCHECK(result == net::OK || socket == NULL); |
| + |
| + if (socket && result == net::OK) { |
| + SetSocket(params_->socket_id, socket.release()); |
| + } else { |
| + RemoveSocket(params_->socket_id); |
| + error_ = net::ErrorToString(result); |
| + } |
| + |
| + results_ = api::socket::Secure::Results::Create(result); |
| + url_request_getter_->Release(); |
|
Ryan Sleevi
2014/02/04 22:28:14
Why are you manually releasing here? Won't this ca
lally
2014/02/11 22:05:35
Changed. Now passing ownership to UpgradeSocketTo
|
| + AsyncWorkCompleted(); |
| +} |
| + |
| } // namespace extensions |