| 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..666e1afbda1dce7e8c9317efd57e6b15b10ee42d 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 client 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::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());
|
| }
|
| @@ -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,76 @@ 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();
|
| + DCHECK(profile);
|
| +
|
| + scoped_refptr<net::SSLConfigService> config_service(
|
| + profile->GetSSLConfigService());
|
| +
|
| + TLSSocket::UpgradeSocketToTLS(
|
| + socket,
|
| + config_service,
|
| + 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) {
|
| + ReplaceSocket(params_->socket_id, socket.release());
|
| + } else {
|
| + RemoveSocket(params_->socket_id);
|
| + error_ = net::ErrorToString(result);
|
| + }
|
| +
|
| + results_ = api::socket::Secure::Results::Create(result);
|
| + AsyncWorkCompleted();
|
| +}
|
| +
|
| } // namespace extensions
|
|
|