diff --git a/p2p/base/port.cc b/p2p/base/port.cc index 3bd09dc024..59c344bacd 100644 --- a/p2p/base/port.cc +++ b/p2p/base/port.cc @@ -80,13 +80,21 @@ const char* ProtoToString(ProtocolType proto) { return PROTO_NAMES[proto]; } -bool StringToProto(const char* value, ProtocolType* proto) { +absl::optional StringToProto(absl::string_view proto_name) { for (size_t i = 0; i <= PROTO_LAST; ++i) { - if (absl::EqualsIgnoreCase(PROTO_NAMES[i], value)) { - *proto = static_cast(i); - return true; + if (absl::EqualsIgnoreCase(PROTO_NAMES[i], proto_name)) { + return static_cast(i); } } + return absl::nullopt; +} + +bool StringToProto(const char* value, ProtocolType* proto) { + if (absl::optional type = StringToProto(value); + type.has_value()) { + *proto = *type; + return true; + } return false; } diff --git a/p2p/base/port.h b/p2p/base/port.h index b1dab5e92b..65560fe196 100644 --- a/p2p/base/port.h +++ b/p2p/base/port.h @@ -18,6 +18,7 @@ #include #include +#include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "api/candidate.h" #include "api/field_trials_view.h" @@ -125,7 +126,10 @@ class CandidateStats { typedef std::vector CandidateStatsList; const char* ProtoToString(ProtocolType proto); -bool StringToProto(const char* value, ProtocolType* proto); +absl::optional StringToProto(absl::string_view proto_name); + +// TODO(bugs.webrtc.org/13579): Delete once downstream usage is updated. +[[deprecated]] bool StringToProto(const char* value, ProtocolType* proto); struct ProtocolAddress { rtc::SocketAddress address; diff --git a/pc/ice_server_parsing.cc b/pc/ice_server_parsing.cc index cb4145be1a..73cb8a0f68 100644 --- a/pc/ice_server_parsing.cc +++ b/pc/ice_server_parsing.cc @@ -183,12 +183,15 @@ static RTCErrorType ParseIceServerUrl( RTC_LOG(LS_WARNING) << "Transport parameter missing value."; return RTCErrorType::SYNTAX_ERROR; } - if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) || - (turn_transport_type != cricket::PROTO_UDP && - turn_transport_type != cricket::PROTO_TCP)) { + + absl::optional proto = + cricket::StringToProto(tokens[1]); + if (!proto || + (*proto != cricket::PROTO_UDP && *proto != cricket::PROTO_TCP)) { RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp."; return RTCErrorType::SYNTAX_ERROR; } + turn_transport_type = *proto; } std::string hoststring; diff --git a/pc/webrtc_sdp.cc b/pc/webrtc_sdp.cc index 4ca1636e54..a62b6f7ffa 100644 --- a/pc/webrtc_sdp.cc +++ b/pc/webrtc_sdp.cc @@ -1112,12 +1112,13 @@ bool ParseCandidate(absl::string_view message, } SocketAddress address(connection_address, port); - cricket::ProtocolType protocol; - if (!StringToProto(transport.c_str(), &protocol)) { + absl::optional protocol = + cricket::StringToProto(transport); + if (!protocol) { return ParseFailed(first_line, "Unsupported transport type.", error); } bool tcp_protocol = false; - switch (protocol) { + switch (*protocol) { // Supported protocols. case cricket::PROTO_UDP: break; @@ -1225,7 +1226,7 @@ bool ParseCandidate(absl::string_view message, } } - *candidate = Candidate(component_id, cricket::ProtoToString(protocol), + *candidate = Candidate(component_id, cricket::ProtoToString(*protocol), address, priority, username, password, candidate_type, generation, foundation, network_id, network_cost); candidate->set_related_address(related_address);