Prepare to move packet_socket_factory to api/.

I gave up on removing proxy_info, user_agent and tcp_options. I don't
think it's feasible to remove them without removing all the proxy code.
The assumption that you can set the proxy and user agent long after
you have created the factory is entrenched in unit tests and the code
itself. So is the ability to set tcp opts depending on protocol or
endpoint properties.

It may be easier to untangle proxy stuff from the factory later,
when it becomes a more first-class citizen and isn't passed via
the allocator.

Requires https://chromium-review.googlesource.com/c/chromium/src/+/1778870
to land first.

Bug: webrtc:7447
Change-Id: Ib496e2bb689ea415e9f8ec1dfedff13a83fa4a8a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/150799
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29091}
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 5b9a5d5..bc80b75 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -50,7 +50,6 @@
     "base/p2p_constants.h",
     "base/p2p_transport_channel.cc",
     "base/p2p_transport_channel.h",
-    "base/packet_socket_factory.cc",
     "base/packet_socket_factory.h",
     "base/packet_transport_interface.h",
     "base/packet_transport_internal.cc",
@@ -93,6 +92,7 @@
 
   deps = [
     "../api:libjingle_peerconnection_api",
+    "../api:packet_socket_factory",
     "../api:scoped_refptr",
     "../api/rtc_event_log",
     "../api/transport:enums",
@@ -164,6 +164,7 @@
       ":p2p_server_utils",
       ":rtc_p2p",
       "../api:libjingle_peerconnection_api",
+      "../api:packet_socket_factory",
       "../rtc_base",
       "../rtc_base:gunit_helpers",
       "../rtc_base:rtc_base_approved",
diff --git a/p2p/base/basic_packet_socket_factory.cc b/p2p/base/basic_packet_socket_factory.cc
index 3204092..1476939 100644
--- a/p2p/base/basic_packet_socket_factory.cc
+++ b/p2p/base/basic_packet_socket_factory.cc
@@ -188,25 +188,6 @@
   return tcp_socket;
 }
 
-AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
-    const SocketAddress& local_address,
-    const SocketAddress& remote_address,
-    const ProxyInfo& proxy_info,
-    const std::string& user_agent,
-    int opts) {
-  PacketSocketTcpOptions tcp_options;
-  tcp_options.opts = opts;
-  return CreateClientTcpSocket(local_address, remote_address, proxy_info,
-                               user_agent, tcp_options);
-}
-
-AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
-    const SocketAddress& local_address,
-    const SocketAddress& remote_address) {
-  return CreateClientTcpSocket(local_address, remote_address, ProxyInfo(), "",
-                               PacketSocketTcpOptions());
-}
-
 AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
   return new AsyncResolver();
 }
diff --git a/p2p/base/basic_packet_socket_factory.h b/p2p/base/basic_packet_socket_factory.h
index ba6c59d..337efca 100644
--- a/p2p/base/basic_packet_socket_factory.h
+++ b/p2p/base/basic_packet_socket_factory.h
@@ -13,7 +13,7 @@
 
 #include <string>
 
-#include "p2p/base/packet_socket_factory.h"
+#include "api/packet_socket_factory.h"
 
 namespace rtc {
 
@@ -37,14 +37,6 @@
                                            int opts) override;
   AsyncPacketSocket* CreateClientTcpSocket(
       const SocketAddress& local_address,
-      const SocketAddress& remote_address) override;
-  AsyncPacketSocket* CreateClientTcpSocket(const SocketAddress& local_address,
-                                           const SocketAddress& remote_address,
-                                           const ProxyInfo& proxy_info,
-                                           const std::string& user_agent,
-                                           int opts) override;
-  AsyncPacketSocket* CreateClientTcpSocket(
-      const SocketAddress& local_address,
       const SocketAddress& remote_address,
       const ProxyInfo& proxy_info,
       const std::string& user_agent,
diff --git a/p2p/base/packet_socket_factory.cc b/p2p/base/packet_socket_factory.cc
deleted file mode 100644
index 403dc26..0000000
--- a/p2p/base/packet_socket_factory.cc
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- *  Copyright 2017 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include "p2p/base/packet_socket_factory.h"
-
-#include <string>
-
-#include "rtc_base/checks.h"
-
-namespace rtc {
-
-PacketSocketTcpOptions::PacketSocketTcpOptions() = default;
-
-PacketSocketTcpOptions::~PacketSocketTcpOptions() = default;
-
-AsyncPacketSocket* PacketSocketFactory::CreateClientTcpSocket(
-    const SocketAddress& local_address,
-    const SocketAddress& remote_address,
-    const ProxyInfo& proxy_info,
-    const std::string& user_agent,
-    const PacketSocketTcpOptions& tcp_options) {
-  return CreateClientTcpSocket(local_address, remote_address, proxy_info,
-                               user_agent, tcp_options.opts);
-}
-
-AsyncPacketSocket* PacketSocketFactory::CreateClientTcpSocket(
-    const SocketAddress& local_address,
-    const SocketAddress& remote_address,
-    const ProxyInfo& proxy_info,
-    const std::string& user_agent,
-    int opts) {
-  RTC_NOTREACHED();
-  return nullptr;
-}
-
-AsyncPacketSocket* PacketSocketFactory::CreateClientTcpSocket(
-    const SocketAddress& local_address,
-    const SocketAddress& remote_address) {
-  RTC_NOTREACHED();
-  return nullptr;
-}
-
-}  // namespace rtc
diff --git a/p2p/base/packet_socket_factory.h b/p2p/base/packet_socket_factory.h
index 5c90d6d..139a778 100644
--- a/p2p/base/packet_socket_factory.h
+++ b/p2p/base/packet_socket_factory.h
@@ -8,90 +8,12 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+// TODO(bugs.webrtc.org/7447): Remove this file once downstream points to the
+// new location in api/.
+
 #ifndef P2P_BASE_PACKET_SOCKET_FACTORY_H_
 #define P2P_BASE_PACKET_SOCKET_FACTORY_H_
 
-#include <string>
-#include <vector>
-
-#include "rtc_base/proxy_info.h"
-#include "rtc_base/system/rtc_export.h"
-
-namespace rtc {
-
-class SSLCertificateVerifier;
-class AsyncPacketSocket;
-class AsyncResolverInterface;
-
-// TODO(bugs.webrtc.org/7447): move this to basic_packet_socket_factory.
-struct PacketSocketTcpOptions {
-  PacketSocketTcpOptions();
-  ~PacketSocketTcpOptions();
-
-  int opts = 0;
-  std::vector<std::string> tls_alpn_protocols;
-  std::vector<std::string> tls_elliptic_curves;
-  // An optional custom SSL certificate verifier that an API user can provide to
-  // inject their own certificate verification logic (not available to users
-  // outside of the WebRTC repo).
-  SSLCertificateVerifier* tls_cert_verifier = nullptr;
-};
-
-class RTC_EXPORT PacketSocketFactory {
- public:
-  enum Options {
-    OPT_STUN = 0x04,
-
-    // The TLS options below are mutually exclusive.
-    OPT_TLS = 0x02,           // Real and secure TLS.
-    OPT_TLS_FAKE = 0x01,      // Fake TLS with a dummy SSL handshake.
-    OPT_TLS_INSECURE = 0x08,  // Insecure TLS without certificate validation.
-
-    // Deprecated, use OPT_TLS_FAKE.
-    OPT_SSLTCP = OPT_TLS_FAKE,
-  };
-
-  PacketSocketFactory() {}
-  virtual ~PacketSocketFactory() = default;
-
-  virtual AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address,
-                                             uint16_t min_port,
-                                             uint16_t max_port) = 0;
-  virtual AsyncPacketSocket* CreateServerTcpSocket(
-      const SocketAddress& local_address,
-      uint16_t min_port,
-      uint16_t max_port,
-      int opts) = 0;
-
-  // TODO(bugs.webrtc.org/7447): This should be the only CreateClientTcpSocket
-  // implementation left; the two other are deprecated.
-  virtual AsyncPacketSocket* CreateClientTcpSocket(
-      const SocketAddress& local_address,
-      const SocketAddress& remote_address);
-
-  // TODO(bugs.webrtc.org/7447): Deprecated, about to be removed.
-  virtual AsyncPacketSocket* CreateClientTcpSocket(
-      const SocketAddress& local_address,
-      const SocketAddress& remote_address,
-      const ProxyInfo& proxy_info,
-      const std::string& user_agent,
-      int opts);
-
-  // TODO(bugs.webrtc.org/7447): Deprecated, about to be removed.
-  virtual AsyncPacketSocket* CreateClientTcpSocket(
-      const SocketAddress& local_address,
-      const SocketAddress& remote_address,
-      const ProxyInfo& proxy_info,
-      const std::string& user_agent,
-      const PacketSocketTcpOptions& tcp_options);
-
-  virtual AsyncResolverInterface* CreateAsyncResolver() = 0;
-
- private:
-  PacketSocketFactory(const PacketSocketFactory&) = delete;
-  PacketSocketFactory& operator=(const PacketSocketFactory&) = delete;
-};
-
-}  // namespace rtc
+#include "api/packet_socket_factory.h"
 
 #endif  // P2P_BASE_PACKET_SOCKET_FACTORY_H_
diff --git a/p2p/base/port_unittest.cc b/p2p/base/port_unittest.cc
index 4e32867..bef8426 100644
--- a/p2p/base/port_unittest.cc
+++ b/p2p/base/port_unittest.cc
@@ -1034,13 +1034,12 @@
     return result;
   }
 
-  // TODO(?): |proxy_info| and |user_agent| should be set
-  // per-factory and not when socket is created.
-  AsyncPacketSocket* CreateClientTcpSocket(const SocketAddress& local_address,
-                                           const SocketAddress& remote_address,
-                                           const rtc::ProxyInfo& proxy_info,
-                                           const std::string& user_agent,
-                                           int opts) override {
+  AsyncPacketSocket* CreateClientTcpSocket(
+      const SocketAddress& local_address,
+      const SocketAddress& remote_address,
+      const rtc::ProxyInfo& proxy_info,
+      const std::string& user_agent,
+      const rtc::PacketSocketTcpOptions& opts) override {
     EXPECT_TRUE(next_client_tcp_socket_ != NULL);
     AsyncPacketSocket* result = next_client_tcp_socket_;
     next_client_tcp_socket_ = NULL;
diff --git a/p2p/base/relay_port.cc b/p2p/base/relay_port.cc
index 662a44b..bb62ebb 100644
--- a/p2p/base/relay_port.cc
+++ b/p2p/base/relay_port.cc
@@ -519,9 +519,11 @@
     int opts = (ra->proto == PROTO_SSLTCP)
                    ? rtc::PacketSocketFactory::OPT_TLS_FAKE
                    : 0;
+    rtc::PacketSocketTcpOptions tcp_opts;
+    tcp_opts.opts = opts;
     socket = port_->socket_factory()->CreateClientTcpSocket(
         rtc::SocketAddress(port_->Network()->GetBestIP(), 0), ra->address,
-        port_->proxy(), port_->user_agent(), opts);
+        port_->proxy(), port_->user_agent(), tcp_opts);
   } else {
     RTC_LOG(LS_WARNING) << "Unknown protocol: " << ra->proto;
   }
diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc
index 2cc2c94..91b6e14 100644
--- a/p2p/base/tcp_port.cc
+++ b/p2p/base/tcp_port.cc
@@ -550,10 +550,12 @@
   int opts = (remote_candidate().protocol() == SSLTCP_PROTOCOL_NAME)
                  ? rtc::PacketSocketFactory::OPT_TLS_FAKE
                  : 0;
+  rtc::PacketSocketTcpOptions tcp_opts;
+  tcp_opts.opts = opts;
   socket_.reset(port()->socket_factory()->CreateClientTcpSocket(
       rtc::SocketAddress(port()->Network()->GetBestIP(), 0),
       remote_candidate().address(), port()->proxy(), port()->user_agent(),
-      opts));
+      tcp_opts));
   if (socket_) {
     RTC_LOG(LS_VERBOSE) << ToString() << ": Connecting from "
                         << socket_->GetLocalAddress().ToSensitiveString()