summaryrefslogtreecommitdiffstats
path: root/net/socket/client_socket_factory.cc
blob: 947a2fad72e1072d35c55bdcacb308061ee8e767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket/client_socket_factory.h"

#include "base/singleton.h"
#include "build/build_config.h"
#include "net/socket/client_socket_handle.h"
#if defined(OS_WIN)
#include "net/socket/ssl_client_socket_win.h"
#elif defined(USE_OPENSSL)
#include "net/socket/ssl_client_socket_openssl.h"
#elif defined(USE_NSS)
#include "net/socket/ssl_client_socket_nss.h"
#elif defined(USE_OPENSSL) && defined(ANDROID)
#include "net/socket/ssl_client_socket_openssl.h"
#elif defined(OS_MACOSX)
#include "net/socket/ssl_client_socket_nss.h"
#endif
#include "net/socket/ssl_host_info.h"
#include "net/socket/tcp_client_socket.h"

namespace net {

class DnsRRResolver;

namespace {

SSLClientSocket* DefaultSSLClientSocketFactory(
    ClientSocketHandle* transport_socket,
    const std::string& hostname,
    const SSLConfig& ssl_config,
    SSLHostInfo* ssl_host_info,
    DnsRRResolver* dnsrr_resolver) {
  scoped_ptr<SSLHostInfo> shi(ssl_host_info);
#if defined(OS_WIN)
  return new SSLClientSocketWin(transport_socket, hostname, ssl_config);
#elif defined(USE_OPENSSL)
  return new SSLClientSocketOpenSSL(transport_socket, hostname, ssl_config);
#elif defined(USE_NSS)
  return new SSLClientSocketNSS(transport_socket, hostname, ssl_config,
                                shi.release(), dnsrr_resolver);
#elif defined(OS_MACOSX)
  return new SSLClientSocketNSS(transport_socket, hostname, ssl_config,
                                shi.release(), dnsrr_resolver);
#else
  NOTIMPLEMENTED();
  return NULL;
#endif
}

SSLClientSocketFactory g_ssl_factory = DefaultSSLClientSocketFactory;

class DefaultClientSocketFactory : public ClientSocketFactory {
 public:
  virtual ClientSocket* CreateTCPClientSocket(
      const AddressList& addresses,
      NetLog* net_log,
      const NetLog::Source& source) {
    return new TCPClientSocket(addresses, net_log, source);
  }

  virtual SSLClientSocket* CreateSSLClientSocket(
      ClientSocketHandle* transport_socket,
      const std::string& hostname,
      const SSLConfig& ssl_config,
      SSLHostInfo* ssl_host_info,
      DnsRRResolver* dnsrr_resolver) {
    return g_ssl_factory(transport_socket, hostname, ssl_config, ssl_host_info,
                         dnsrr_resolver);
  }
};

}  // namespace

// static
ClientSocketFactory* ClientSocketFactory::GetDefaultFactory() {
  return Singleton<DefaultClientSocketFactory>::get();
}

// static
void ClientSocketFactory::SetSSLClientSocketFactory(
    SSLClientSocketFactory factory) {
  g_ssl_factory = factory;
}

// Deprecated function (http://crbug.com/37810) that takes a ClientSocket.
SSLClientSocket* ClientSocketFactory::CreateSSLClientSocket(
    ClientSocket* transport_socket,
    const std::string& hostname,
    const SSLConfig& ssl_config,
    SSLHostInfo* ssl_host_info) {
  ClientSocketHandle* socket_handle = new ClientSocketHandle();
  socket_handle->set_socket(transport_socket);
  return CreateSSLClientSocket(socket_handle, hostname, ssl_config,
                               ssl_host_info, NULL /* DnsRRResolver */);
}

}  // namespace net