Change Port::Type() to IceCandidateType

Bug: webrtc:15846
Change-Id: Ibda55129f13d22ac84a730ba54d915c81a90cde9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/340041
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41891}
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index 07b6d2e..241b423 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -1410,7 +1410,7 @@
   }
 
   if (ice_field_trials_.skip_relay_to_non_relay_connections) {
-    IceCandidateType port_type = PortTypeToIceCandidateType(port->Type());
+    IceCandidateType port_type = port->Type();
     if ((port_type != remote_candidate.type()) &&
         (port_type == IceCandidateType::kRelay ||
          remote_candidate.is_relay())) {
@@ -1758,7 +1758,7 @@
           .local = CreateRouteEndpointFromCandidate(
               /* local= */ true, conn->local_candidate(),
               /* uses_turn= */
-              conn->port()->Type() == RELAY_PORT_TYPE),
+              conn->port()->Type() == IceCandidateType::kRelay),
           .remote = CreateRouteEndpointFromCandidate(
               /* local= */ false, conn->remote_candidate(),
               /* uses_turn= */ conn->remote_candidate().is_relay()),
diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc
index e689ab0..261c582 100644
--- a/p2p/base/p2p_transport_channel_unittest.cc
+++ b/p2p/base/p2p_transport_channel_unittest.cc
@@ -5031,8 +5031,8 @@
   P2PTransportChannel& ch =
       StartTransportChannel(true, max_strong_interval, &field_trials_);
   EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout);
-  EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
-  EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
+  EXPECT_EQ(ch.ports()[0]->Type(), IceCandidateType::kHost);
+  EXPECT_EQ(ch.ports()[1]->Type(), IceCandidateType::kRelay);
 
   ch.AddRemoteCandidate(
       CreateUdpCandidate(IceCandidateType::kRelay, "1.1.1.1", 1, 1));
@@ -5092,8 +5092,8 @@
        TestRelayRelayFirstWhenEverythingPinged) {
   P2PTransportChannel& ch = StartTransportChannel(true, 500, &field_trials_);
   EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout);
-  EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
-  EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
+  EXPECT_EQ(ch.ports()[0]->Type(), IceCandidateType::kHost);
+  EXPECT_EQ(ch.ports()[1]->Type(), IceCandidateType::kRelay);
 
   ch.AddRemoteCandidate(
       CreateUdpCandidate(IceCandidateType::kHost, "1.1.1.1", 1, 1));
@@ -5130,8 +5130,8 @@
        TestNoStarvationOnNonRelayConnection) {
   P2PTransportChannel& ch = StartTransportChannel(true, 500, &field_trials_);
   EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout);
-  EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
-  EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
+  EXPECT_EQ(ch.ports()[0]->Type(), IceCandidateType::kHost);
+  EXPECT_EQ(ch.ports()[1]->Type(), IceCandidateType::kRelay);
 
   ch.AddRemoteCandidate(
       CreateUdpCandidate(IceCandidateType::kRelay, "1.1.1.1", 1, 1));
@@ -5173,8 +5173,8 @@
       "WebRTC-IceFieldTrials/skip_relay_to_non_relay_connections:true/");
   P2PTransportChannel& ch = StartTransportChannel(true, 500, &field_trials);
   EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout);
-  EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
-  EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
+  EXPECT_EQ(ch.ports()[0]->Type(), IceCandidateType::kHost);
+  EXPECT_EQ(ch.ports()[1]->Type(), IceCandidateType::kRelay);
 
   // Remote Relay candidate arrives.
   ch.AddRemoteCandidate(
@@ -5199,9 +5199,9 @@
 
   P2PTransportChannel& ch = StartTransportChannel(true, 500, &field_trials_);
   EXPECT_TRUE_WAIT(ch.ports().size() == 3, kDefaultTimeout);
-  EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
-  EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
-  EXPECT_EQ(ch.ports()[2]->Type(), RELAY_PORT_TYPE);
+  EXPECT_EQ(ch.ports()[0]->Type(), IceCandidateType::kHost);
+  EXPECT_EQ(ch.ports()[1]->Type(), IceCandidateType::kRelay);
+  EXPECT_EQ(ch.ports()[2]->Type(), IceCandidateType::kRelay);
 
   // Remote Relay candidate arrives.
   ch.AddRemoteCandidate(
diff --git a/p2p/base/port.cc b/p2p/base/port.cc
index 057ba74..4393671 100644
--- a/p2p/base/port.cc
+++ b/p2p/base/port.cc
@@ -176,12 +176,8 @@
   CancelPendingTasks();
 }
 
-const absl::string_view Port::Type() const {
-  if (type_ == webrtc::IceCandidateType::kHost)
-    return "local";
-  if (type_ == webrtc::IceCandidateType::kSrflx)
-    return "stun";
-  return webrtc::IceCandidateTypeToString(type_);
+IceCandidateType Port::Type() const {
+  return type_;
 }
 const rtc::Network* Port::Network() const {
   return network_;
diff --git a/p2p/base/port.h b/p2p/base/port.h
index 40e8679..ff7ddf9 100644
--- a/p2p/base/port.h
+++ b/p2p/base/port.h
@@ -241,7 +241,7 @@
   // uniquely identify subclasses. Whenever a new subclass of Port introduces a
   // conflict in the value of the 2-tuple, make sure that the implementation
   // that relies on this 2-tuple for RTTI is properly changed.
-  const absl::string_view Type() const override;
+  webrtc::IceCandidateType Type() const override;
   const rtc::Network* Network() const override;
 
   // Methods to set/get ICE role and tiebreaker values.
diff --git a/p2p/base/port_interface.h b/p2p/base/port_interface.h
index de68306..70b2085 100644
--- a/p2p/base/port_interface.h
+++ b/p2p/base/port_interface.h
@@ -52,8 +52,7 @@
  public:
   virtual ~PortInterface();
 
-  // TODO: bugs.webrtc.org/15846 - Change return type to IceCandidateType.
-  virtual const absl::string_view Type() const = 0;
+  virtual webrtc::IceCandidateType Type() const = 0;
   virtual const rtc::Network* Network() const = 0;
 
   // Methods to set/get ICE role and tiebreaker values.
diff --git a/p2p/base/stun_port_unittest.cc b/p2p/base/stun_port_unittest.cc
index 7bc151e..9646fab 100644
--- a/p2p/base/stun_port_unittest.cc
+++ b/p2p/base/stun_port_unittest.cc
@@ -35,6 +35,7 @@
 using ::testing::Return;
 using ::testing::ReturnPointee;
 using ::testing::SetArgPointee;
+using webrtc::IceCandidateType;
 
 static const SocketAddress kLocalAddr("127.0.0.1", 0);
 static const SocketAddress kIPv6LocalAddr("::1", 0);
@@ -261,14 +262,14 @@
 // Test that we can create a STUN port.
 TEST_F(StunPortTest, TestCreateStunPort) {
   CreateStunPort(kStunAddr1);
-  EXPECT_EQ("stun", port()->Type());
+  EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
   EXPECT_EQ(0U, port()->Candidates().size());
 }
 
 // Test that we can create a UDP port.
 TEST_F(StunPortTest, TestCreateUdpPort) {
   CreateSharedUdpPort(kStunAddr1, nullptr);
-  EXPECT_EQ("local", port()->Type());
+  EXPECT_EQ(IceCandidateType::kHost, port()->Type());
   EXPECT_EQ(0U, port()->Candidates().size());
 }
 
@@ -460,7 +461,7 @@
   stun_servers.insert(kStunAddr1);
   stun_servers.insert(kStunAddr2);
   CreateStunPort(stun_servers);
-  EXPECT_EQ("stun", port()->Type());
+  EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
   PrepareAddress();
   EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
   EXPECT_EQ(1U, port()->Candidates().size());
@@ -474,7 +475,7 @@
   stun_servers.insert(kStunAddr1);
   stun_servers.insert(kBadAddr);
   CreateStunPort(stun_servers);
-  EXPECT_EQ("stun", port()->Type());
+  EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
   PrepareAddress();
   EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
   EXPECT_EQ(1U, port()->Candidates().size());
@@ -495,7 +496,7 @@
   stun_servers.insert(kStunAddr1);
   stun_servers.insert(kStunAddr2);
   CreateStunPort(stun_servers);
-  EXPECT_EQ("stun", port()->Type());
+  EXPECT_EQ(IceCandidateType::kSrflx, port()->Type());
   PrepareAddress();
   EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
   EXPECT_EQ(2U, port()->Candidates().size());
diff --git a/p2p/base/turn_port_unittest.cc b/p2p/base/turn_port_unittest.cc
index eaf28e4..6915afa 100644
--- a/p2p/base/turn_port_unittest.cc
+++ b/p2p/base/turn_port_unittest.cc
@@ -865,7 +865,7 @@
 
 TEST_F(TurnPortTest, TestTurnPortType) {
   CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
-  EXPECT_EQ(cricket::RELAY_PORT_TYPE, turn_port_->Type());
+  EXPECT_EQ(IceCandidateType::kRelay, turn_port_->Type());
 }
 
 // Tests that the URL of the servers can be correctly reconstructed when
diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc
index cc38a66..a0b93be 100644
--- a/p2p/client/basic_port_allocator.cc
+++ b/p2p/client/basic_port_allocator.cc
@@ -42,6 +42,7 @@
 namespace cricket {
 namespace {
 using ::rtc::CreateRandomId;
+using ::webrtc::IceCandidateType;
 using ::webrtc::SafeTask;
 using ::webrtc::TimeDelta;
 
@@ -509,9 +510,10 @@
   for (PortInterface* port : ports) {
     // The port type and protocol can be used to identify different subclasses
     // of Port in the current implementation. Note that a TCPPort has the type
-    // LOCAL_PORT_TYPE but uses the protocol PROTO_TCP.
-    if (port->Type() == STUN_PORT_TYPE ||
-        (port->Type() == LOCAL_PORT_TYPE && port->GetProtocol() == PROTO_UDP)) {
+    // IceCandidateType::kHost but uses the protocol PROTO_TCP.
+    if (port->Type() == IceCandidateType::kSrflx ||
+        (port->Type() == IceCandidateType::kHost &&
+         port->GetProtocol() == PROTO_UDP)) {
       static_cast<UDPPort*>(port)->set_stun_keepalive_delay(
           stun_keepalive_interval);
     }
@@ -993,7 +995,7 @@
   if (CandidatePairable(c, port) && !data->has_pairable_candidate()) {
     data->set_has_pairable_candidate(true);
 
-    if (port->Type() == RELAY_PORT_TYPE) {
+    if (port->Type() == IceCandidateType::kRelay) {
       if (turn_port_prune_policy_ == webrtc::KEEP_FIRST_READY) {
         pruned = PruneNewlyPairableTurnPort(data);
       } else if (turn_port_prune_policy_ == webrtc::PRUNE_BASED_ON_PRIORITY) {
@@ -1041,7 +1043,7 @@
   Port* best_turn_port = nullptr;
   for (const PortData& data : ports_) {
     if (data.port()->Network()->name() == network_name &&
-        data.port()->Type() == RELAY_PORT_TYPE && data.ready() &&
+        data.port()->Type() == IceCandidateType::kRelay && data.ready() &&
         (!best_turn_port || ComparePort(data.port(), best_turn_port) > 0)) {
       best_turn_port = data.port();
     }
@@ -1052,7 +1054,8 @@
 bool BasicPortAllocatorSession::PruneNewlyPairableTurnPort(
     PortData* newly_pairable_port_data) {
   RTC_DCHECK_RUN_ON(network_thread_);
-  RTC_DCHECK(newly_pairable_port_data->port()->Type() == RELAY_PORT_TYPE);
+  RTC_DCHECK(newly_pairable_port_data->port()->Type() ==
+             IceCandidateType::kRelay);
   // If an existing turn port is ready on the same network, prune the newly
   // pairable port.
   const std::string& network_name =
@@ -1060,7 +1063,7 @@
 
   for (PortData& data : ports_) {
     if (data.port()->Network()->name() == network_name &&
-        data.port()->Type() == RELAY_PORT_TYPE && data.ready() &&
+        data.port()->Type() == IceCandidateType::kRelay && data.ready() &&
         &data != newly_pairable_port_data) {
       RTC_LOG(LS_INFO) << "Port pruned: "
                        << newly_pairable_port_data->port()->ToString();
@@ -1085,7 +1088,7 @@
   std::vector<PortData*> ports_to_prune;
   for (PortData& data : ports_) {
     if (data.port()->Network()->name() == network_name &&
-        data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() &&
+        data.port()->Type() == IceCandidateType::kRelay && !data.pruned() &&
         ComparePort(data.port(), best_turn_port) < 0) {
       pruned = true;
       if (data.port() != newly_pairable_turn_port) {
@@ -1354,7 +1357,8 @@
                      [this](const BasicPortAllocatorSession::PortData& p) {
                        return !p.pruned() && p.port()->Network() == network_ &&
                               p.port()->GetProtocol() == PROTO_UDP &&
-                              p.port()->Type() == LOCAL_PORT_TYPE && !p.error();
+                              p.port()->Type() == IceCandidateType::kHost &&
+                              !p.error();
                      })) {
     *flags |= PORTALLOCATOR_DISABLE_UDP;
   }
@@ -1364,7 +1368,8 @@
                      [this](const BasicPortAllocatorSession::PortData& p) {
                        return !p.pruned() && p.port()->Network() == network_ &&
                               p.port()->GetProtocol() == PROTO_TCP &&
-                              p.port()->Type() == LOCAL_PORT_TYPE && !p.error();
+                              p.port()->Type() == IceCandidateType::kHost &&
+                              !p.error();
                      })) {
     *flags |= PORTALLOCATOR_DISABLE_TCP;
   }
diff --git a/p2p/client/basic_port_allocator_unittest.cc b/p2p/client/basic_port_allocator_unittest.cc
index a8c0352..fa244c8 100644
--- a/p2p/client/basic_port_allocator_unittest.cc
+++ b/p2p/client/basic_port_allocator_unittest.cc
@@ -120,8 +120,8 @@
     int expected) {
   auto ready_ports = allocator_session->ReadyPorts();
   for (const auto* port : ready_ports) {
-    if (port->Type() == cricket::STUN_PORT_TYPE ||
-        (port->Type() == cricket::LOCAL_PORT_TYPE &&
+    if (port->Type() == IceCandidateType::kSrflx ||
+        (port->Type() == IceCandidateType::kHost &&
          port->GetProtocol() == cricket::PROTO_UDP)) {
       EXPECT_EQ(
           static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
@@ -314,7 +314,7 @@
   // Returns the number of ports that have matching type, protocol and
   // address.
   static int CountPorts(const std::vector<PortInterface*>& ports,
-                        absl::string_view type,
+                        IceCandidateType type,
                         ProtocolType protocol,
                         const SocketAddress& client_addr) {
     return absl::c_count_if(
@@ -613,10 +613,14 @@
     // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
     EXPECT_EQ(3U, session_->ReadyPorts().size());
     EXPECT_EQ(3U, ports_.size());
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
-    EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
-    EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
+    EXPECT_EQ(
+        1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP, kClientAddr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP,
+                            kClientIPv6Addr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kRelay, PROTO_UDP,
+                            kClientIPv6Addr));
+    EXPECT_EQ(0, CountPorts(ports_, IceCandidateType::kRelay, PROTO_UDP,
+                            kClientAddr));
 
     // Now that we remove candidates when a TURN port is pruned, there will be
     // exactly 3 candidates in both `candidates_` and `ready_candidates`.
@@ -656,12 +660,13 @@
     // found in `ready_ports`, and when it is pruned, it is not found in
     // `ready_ports`, so we only need to verify the content in one of them.
     EXPECT_EQ(2U, ports_.size());
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
+    EXPECT_EQ(
+        1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP, kClientAddr));
     int num_udp_ports = tcp_pruned ? 1 : 0;
-    EXPECT_EQ(num_udp_ports,
-              CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
-    EXPECT_EQ(1 - num_udp_ports,
-              CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
+    EXPECT_EQ(num_udp_ports, CountPorts(ports_, IceCandidateType::kRelay,
+                                        PROTO_UDP, kClientAddr));
+    EXPECT_EQ(1 - num_udp_ports, CountPorts(ports_, IceCandidateType::kRelay,
+                                            PROTO_TCP, kClientAddr));
 
     // Now that we remove candidates when a TURN port is pruned, `candidates_`
     // should only contains two candidates regardless whether the TCP TURN port
@@ -711,16 +716,26 @@
     // use.
     EXPECT_EQ(10U, session_->ReadyPorts().size());
     EXPECT_EQ(10U, ports_.size());
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
-    EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
-    EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
-    EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
+    EXPECT_EQ(
+        1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP, kClientAddr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP,
+                            kClientAddr2));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP,
+                            kClientIPv6Addr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_UDP,
+                            kClientIPv6Addr2));
+    EXPECT_EQ(
+        1, CountPorts(ports_, IceCandidateType::kHost, PROTO_TCP, kClientAddr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_TCP,
+                            kClientAddr2));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_TCP,
+                            kClientIPv6Addr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kHost, PROTO_TCP,
+                            kClientIPv6Addr2));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kRelay, PROTO_UDP,
+                            kClientIPv6Addr));
+    EXPECT_EQ(1, CountPorts(ports_, IceCandidateType::kRelay, PROTO_UDP,
+                            kClientIPv6Addr2));
 
     // Now that we remove candidates when TURN ports are pruned, there will be
     // exactly 10 candidates in `candidates_`.
@@ -1268,11 +1283,15 @@
   EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
                              kDefaultAllocationTimeout, fake_clock);
   EXPECT_EQ(4U, ports_.size());
-  EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
-  EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
+  EXPECT_EQ(1,
+            CountPorts(ports_, IceCandidateType::kSrflx, PROTO_UDP, kAnyAddr));
+  EXPECT_EQ(1,
+            CountPorts(ports_, IceCandidateType::kHost, PROTO_TCP, kAnyAddr));
   // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
-  EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
-  EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
+  EXPECT_EQ(1,
+            CountPorts(ports_, IceCandidateType::kRelay, PROTO_UDP, kAnyAddr));
+  EXPECT_EQ(1,
+            CountPorts(ports_, IceCandidateType::kRelay, PROTO_TCP, kAnyAddr));
   // The "any" address port should be in the signaled ready ports, but the host
   // candidate for it is useless and shouldn't be signaled. So we only have
   // STUN/TURN candidates.
@@ -2203,7 +2222,7 @@
   EXPECT_GT(initial_ports_size, ports.size());
   for (const PortInterface* port : ports) {
     // Expect only relay ports.
-    EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
+    EXPECT_EQ(IceCandidateType::kRelay, port->Type());
   }
   for (const Candidate& candidate : candidates) {
     // Expect only relay candidates now that the filter is applied.