Adopt absl::string_view in rtc_base/ (straightforward cases) Bug: webrtc:13579 Change-Id: I240db6285abb22652242bc0b2ebe9844ec4a45f0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/258723 Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org> Commit-Queue: Ali Tofigh <alito@webrtc.org> Cr-Commit-Position: refs/heads/main@{#36561}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index bf39ebe..c30340a 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn
@@ -703,6 +703,7 @@ ":win32", ] } + absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] } rtc_library("async_resolver_interface") {
diff --git a/rtc_base/async_resolver.cc b/rtc_base/async_resolver.cc index ad1598f..07d77b4 100644 --- a/rtc_base/async_resolver.cc +++ b/rtc_base/async_resolver.cc
@@ -14,6 +14,7 @@ #include <string> #include <utility> +#include "absl/strings/string_view.h" #include "api/ref_counted_base.h" #include "rtc_base/synchronization/mutex.h" #include "rtc_base/thread_annotations.h" @@ -66,7 +67,7 @@ } // namespace #endif -int ResolveHostname(const std::string& hostname, +int ResolveHostname(absl::string_view hostname, int family, std::vector<IPAddress>* addresses) { #ifdef __native_client__ @@ -99,7 +100,8 @@ // https://android.googlesource.com/platform/bionic/+/ // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657 hints.ai_flags = AI_ADDRCONFIG; - int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result); + int ret = + getaddrinfo(std::string(hostname).c_str(), nullptr, &hints, &result); if (ret != 0) { return ret; } @@ -151,8 +153,7 @@ [this, addr, caller_task_queue = webrtc::TaskQueueBase::Current(), state = state_] { std::vector<IPAddress> addresses; - int error = - ResolveHostname(addr.hostname().c_str(), addr.family(), &addresses); + int error = ResolveHostname(addr.hostname(), addr.family(), &addresses); webrtc::MutexLock lock(&state->mutex); if (state->status == State::Status::kLive) { caller_task_queue->PostTask(webrtc::ToQueuedTask(
diff --git a/rtc_base/event_tracer.cc b/rtc_base/event_tracer.cc index 1a2b41e..e14079e 100644 --- a/rtc_base/event_tracer.cc +++ b/rtc_base/event_tracer.cc
@@ -17,6 +17,7 @@ #include <string> #include <vector> +#include "absl/strings/string_view.h" #include "api/sequence_checker.h" #include "rtc_base/atomic_ops.h" #include "rtc_base/checks.h" @@ -367,11 +368,11 @@ } } -bool StartInternalCapture(const char* filename) { +bool StartInternalCapture(absl::string_view filename) { if (!g_event_logger) return false; - FILE* file = fopen(filename, "w"); + FILE* file = fopen(std::string(filename).c_str(), "w"); if (!file) { RTC_LOG(LS_ERROR) << "Failed to open trace file '" << filename << "' for writing.";
diff --git a/rtc_base/event_tracer.h b/rtc_base/event_tracer.h index 68aaf0d..77c2b35 100644 --- a/rtc_base/event_tracer.h +++ b/rtc_base/event_tracer.h
@@ -28,6 +28,7 @@ #include <stdio.h> +#include "absl/strings/string_view.h" #include "rtc_base/system/rtc_export.h" namespace webrtc { @@ -73,7 +74,7 @@ namespace tracing { // Set up internal event tracer. RTC_EXPORT void SetupInternalTracer(); -RTC_EXPORT bool StartInternalCapture(const char* filename); +RTC_EXPORT bool StartInternalCapture(absl::string_view filename); RTC_EXPORT void StartInternalCaptureToFile(FILE* file); RTC_EXPORT void StopInternalCapture(); // Make sure we run this, this will tear down the internal tracing.
diff --git a/rtc_base/experiments/BUILD.gn b/rtc_base/experiments/BUILD.gn index 3f720d5..fffd5a0 100644 --- a/rtc_base/experiments/BUILD.gn +++ b/rtc_base/experiments/BUILD.gn
@@ -19,7 +19,10 @@ "../../api:field_trials_view", "../../api/transport:field_trial_based_config", ] - absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ] + absl_deps = [ + "//third_party/abseil-cpp/absl/strings:strings", + "//third_party/abseil-cpp/absl/types:optional", + ] } rtc_library("field_trial_parser") {
diff --git a/rtc_base/experiments/alr_experiment.cc b/rtc_base/experiments/alr_experiment.cc index 36bf67d..f5d36f6 100644 --- a/rtc_base/experiments/alr_experiment.cc +++ b/rtc_base/experiments/alr_experiment.cc
@@ -15,6 +15,7 @@ #include <string> +#include "absl/strings/string_view.h" #include "api/transport/field_trial_based_config.h" #include "rtc_base/logging.h" @@ -39,7 +40,7 @@ } absl::optional<AlrExperimentSettings> -AlrExperimentSettings::CreateFromFieldTrial(const char* experiment_name) { +AlrExperimentSettings::CreateFromFieldTrial(absl::string_view experiment_name) { return AlrExperimentSettings::CreateFromFieldTrial(FieldTrialBasedConfig(), experiment_name); } @@ -47,7 +48,7 @@ absl::optional<AlrExperimentSettings> AlrExperimentSettings::CreateFromFieldTrial( const FieldTrialsView& key_value_config, - const char* experiment_name) { + absl::string_view experiment_name) { absl::optional<AlrExperimentSettings> ret; std::string group_name = key_value_config.Lookup(experiment_name);
diff --git a/rtc_base/experiments/alr_experiment.h b/rtc_base/experiments/alr_experiment.h index bf6aa34..048fd90 100644 --- a/rtc_base/experiments/alr_experiment.h +++ b/rtc_base/experiments/alr_experiment.h
@@ -13,6 +13,7 @@ #include <stdint.h> +#include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "api/field_trials_view.h" @@ -32,10 +33,10 @@ static const char kScreenshareProbingBweExperimentName[]; static const char kStrictPacingAndProbingExperimentName[]; static absl::optional<AlrExperimentSettings> CreateFromFieldTrial( - const char* experiment_name); + absl::string_view experiment_name); static absl::optional<AlrExperimentSettings> CreateFromFieldTrial( const FieldTrialsView& key_value_config, - const char* experiment_name); + absl::string_view experiment_name); static bool MaxOneFieldTrialEnabled(); static bool MaxOneFieldTrialEnabled(const FieldTrialsView& key_value_config);
diff --git a/rtc_base/file_rotating_stream_unittest.cc b/rtc_base/file_rotating_stream_unittest.cc index ee03d3f..1d1e5b6 100644 --- a/rtc_base/file_rotating_stream_unittest.cc +++ b/rtc_base/file_rotating_stream_unittest.cc
@@ -79,28 +79,28 @@ // Checks that the stream reads in the expected contents and then returns an // end of stream result. - void VerifyStreamRead(const char* expected_contents, - const size_t expected_length, + void VerifyStreamRead(absl::string_view expected_contents, absl::string_view dir_path, - const char* file_prefix) { + absl::string_view file_prefix) { + size_t expected_length = expected_contents.size(); FileRotatingStreamReader reader(dir_path, file_prefix); EXPECT_EQ(reader.GetSize(), expected_length); std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); memset(buffer.get(), 0, expected_length); EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length)); - EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); + EXPECT_EQ(0, + memcmp(expected_contents.data(), buffer.get(), expected_length)); } - void VerifyFileContents(const char* expected_contents, - const size_t expected_length, + void VerifyFileContents(absl::string_view expected_contents, absl::string_view file_path) { + size_t expected_length = expected_contents.size(); std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length + 1]); - webrtc::FileWrapper f = - webrtc::FileWrapper::OpenReadOnly(std::string(file_path)); + webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(file_path); ASSERT_TRUE(f.is_open()); size_t size_read = f.Read(buffer.get(), expected_length + 1); EXPECT_EQ(size_read, expected_length); - EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), + EXPECT_EQ(0, memcmp(expected_contents.data(), buffer.get(), std::min(expected_length, size_read))); } @@ -151,8 +151,7 @@ WriteAndFlush(message.c_str(), message.size()); // Since the max log size is 2, we will be causing rotation. Read from the // next file. - VerifyFileContents(message.c_str(), message.size(), - stream_->GetFilePath(1)); + VerifyFileContents(message, stream_->GetFilePath(1)); } // Check that exactly three files exist. for (size_t i = 0; i < arraysize(messages); ++i) { @@ -167,8 +166,7 @@ // Reopen for read. std::string expected_contents("bbccd"); - VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), - dir_path_, kFilePrefix); + VerifyStreamRead(expected_contents, dir_path_, kFilePrefix); } // Tests that a write operation (with dir name without delimiter) followed by a @@ -191,7 +189,7 @@ // Reopen for read. std::string expected_contents("bbccd"); - VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), + VerifyStreamRead(expected_contents, dir_path_ + std::string(webrtc::test::kPathDelimiter), kFilePrefix); } @@ -215,8 +213,8 @@ // Reopen for read. std::string expected_contents("bbccd"); - VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), - dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix); + VerifyStreamRead(expected_contents, dir_path_.substr(0, dir_path_.size() - 1), + kFilePrefix); } // Tests that writing data greater than the total capacity of the files @@ -230,11 +228,9 @@ std::string message("foobarbaz"); WriteAndFlush(message.c_str(), message.size()); std::string expected_file_contents("z"); - VerifyFileContents(expected_file_contents.c_str(), - expected_file_contents.size(), stream_->GetFilePath(0)); + VerifyFileContents(expected_file_contents, stream_->GetFilePath(0)); std::string expected_stream_contents("arbaz"); - VerifyStreamRead(expected_stream_contents.c_str(), - expected_stream_contents.size(), dir_path_, kFilePrefix); + VerifyStreamRead(expected_stream_contents, dir_path_, kFilePrefix); } // Tests that the returned file paths have the right folder and prefix. @@ -286,15 +282,16 @@ // Checks that the stream reads in the expected contents and then returns an // end of stream result. - void VerifyStreamRead(const char* expected_contents, - const size_t expected_length, + void VerifyStreamRead(absl::string_view expected_contents, absl::string_view dir_path) { + size_t expected_length = expected_contents.size(); CallSessionFileRotatingStreamReader reader(dir_path); EXPECT_EQ(reader.GetSize(), expected_length); std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); memset(buffer.get(), 0, expected_length); EXPECT_EQ(expected_length, reader.ReadAll(buffer.get(), expected_length)); - EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); + EXPECT_EQ(0, + memcmp(expected_contents.data(), buffer.get(), expected_length)); } std::unique_ptr<CallSessionFileRotatingStream> stream_; @@ -310,8 +307,7 @@ std::string message("abcde"); WriteAndFlush(message.c_str(), message.size()); std::string expected_contents("abe"); - VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), - dir_path_); + VerifyStreamRead(expected_contents, dir_path_); } // Tests that writing and reading to a stream with capacity lesser than 4MB @@ -323,8 +319,7 @@ std::string message("123456789"); WriteAndFlush(message.c_str(), message.size()); std::string expected_contents("1234789"); - VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), - dir_path_); + VerifyStreamRead(expected_contents, dir_path_); } // Tests that writing and reading to a stream with capacity greater than 4MB
diff --git a/rtc_base/http_common.cc b/rtc_base/http_common.cc index 5ac222f..2968cf1 100644 --- a/rtc_base/http_common.cc +++ b/rtc_base/http_common.cc
@@ -120,14 +120,14 @@ typedef std::pair<std::string, std::string> HttpAttribute; typedef std::vector<HttpAttribute> HttpAttributeList; -inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) { - if (pos >= len) +inline bool IsEndOfAttributeName(size_t pos, absl::string_view data) { + if (pos >= data.size()) return true; if (isspace(static_cast<unsigned char>(data[pos]))) return true; // The reason for this complexity is that some attributes may contain trailing // equal signs (like base64 tokens in Negotiate auth headers) - if ((pos + 1 < len) && (data[pos] == '=') && + if ((pos + 1 < data.size()) && (data[pos] == '=') && !isspace(static_cast<unsigned char>(data[pos + 1])) && (data[pos + 1] != '=')) { return true; @@ -135,10 +135,10 @@ return false; } -void HttpParseAttributes(const char* data, - size_t len, +void HttpParseAttributes(absl::string_view data, HttpAttributeList& attributes) { size_t pos = 0; + const size_t len = data.size(); while (true) { // Skip leading whitespace while ((pos < len) && isspace(static_cast<unsigned char>(data[pos]))) { @@ -151,12 +151,12 @@ // Find end of attribute name size_t start = pos; - while (!IsEndOfAttributeName(pos, len, data)) { + while (!IsEndOfAttributeName(pos, data)) { ++pos; } HttpAttribute attribute; - attribute.first.assign(data + start, data + pos); + attribute.first.assign(data.data() + start, data.data() + pos); // Attribute has value? if ((pos < len) && (data[pos] == '=')) { @@ -250,8 +250,7 @@ } // anonymous namespace -HttpAuthResult HttpAuthenticate(const char* challenge, - size_t len, +HttpAuthResult HttpAuthenticate(absl::string_view challenge, const SocketAddress& server, absl::string_view method, absl::string_view uri, @@ -261,7 +260,7 @@ std::string& response, std::string& auth_method) { HttpAttributeList args; - HttpParseAttributes(challenge, len, args); + HttpParseAttributes(challenge, args); HttpHasNthAttribute(args, 0, &auth_method, nullptr); if (context && (context->auth_method != auth_method))
diff --git a/rtc_base/http_common.h b/rtc_base/http_common.h index d287bd5..06e42c6 100644 --- a/rtc_base/http_common.h +++ b/rtc_base/http_common.h
@@ -36,8 +36,7 @@ // Start by passing a null pointer, then pass the same pointer each additional // call. When the authentication attempt is finished, delete the context. // TODO(bugs.webrtc.org/8905): Change "response" to "ZeroOnFreeBuffer". -HttpAuthResult HttpAuthenticate(const char* challenge, - size_t len, +HttpAuthResult HttpAuthenticate(absl::string_view challenge, const SocketAddress& server, absl::string_view method, absl::string_view uri,
diff --git a/rtc_base/ip_address.cc b/rtc_base/ip_address.cc index 5ebb402..d544b61 100644 --- a/rtc_base/ip_address.cc +++ b/rtc_base/ip_address.cc
@@ -283,10 +283,9 @@ return false; } in_addr addr; - const std::string str_copy = std::string(str); - if (rtc::inet_pton(AF_INET, str_copy.c_str(), &addr) == 0) { + if (rtc::inet_pton(AF_INET, str, &addr) == 0) { in6_addr addr6; - if (rtc::inet_pton(AF_INET6, str_copy.c_str(), &addr6) == 0) { + if (rtc::inet_pton(AF_INET6, str, &addr6) == 0) { *out = IPAddress(); return false; }
diff --git a/rtc_base/log_sinks.cc b/rtc_base/log_sinks.cc index b237e87..e8e1d40 100644 --- a/rtc_base/log_sinks.cc +++ b/rtc_base/log_sinks.cc
@@ -20,8 +20,8 @@ namespace rtc { -FileRotatingLogSink::FileRotatingLogSink(const std::string& log_dir_path, - const std::string& log_prefix, +FileRotatingLogSink::FileRotatingLogSink(absl::string_view log_dir_path, + absl::string_view log_prefix, size_t max_log_size, size_t num_log_files) : FileRotatingLogSink(new FileRotatingStream(log_dir_path, @@ -69,7 +69,7 @@ } CallSessionFileRotatingLogSink::CallSessionFileRotatingLogSink( - const std::string& log_dir_path, + absl::string_view log_dir_path, size_t max_total_log_size) : FileRotatingLogSink( new CallSessionFileRotatingStream(log_dir_path, max_total_log_size)) {
diff --git a/rtc_base/log_sinks.h b/rtc_base/log_sinks.h index da339a5..54f7750 100644 --- a/rtc_base/log_sinks.h +++ b/rtc_base/log_sinks.h
@@ -28,8 +28,8 @@ public: // `num_log_files` must be greater than 1 and `max_log_size` must be greater // than 0. - FileRotatingLogSink(const std::string& log_dir_path, - const std::string& log_prefix, + FileRotatingLogSink(absl::string_view log_dir_path, + absl::string_view log_prefix, size_t max_log_size, size_t num_log_files); ~FileRotatingLogSink() override; @@ -62,7 +62,7 @@ // Init() must be called before adding this sink. class CallSessionFileRotatingLogSink : public FileRotatingLogSink { public: - CallSessionFileRotatingLogSink(const std::string& log_dir_path, + CallSessionFileRotatingLogSink(absl::string_view log_dir_path, size_t max_total_log_size); ~CallSessionFileRotatingLogSink() override;
diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc index 102c1d1..b93474d 100644 --- a/rtc_base/logging.cc +++ b/rtc_base/logging.cc
@@ -188,7 +188,7 @@ LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev, - const std::string& tag) + absl::string_view tag) : LogMessage(file, line, sev) { print_stream_ << tag << ": "; } @@ -296,7 +296,7 @@ UpdateMinLogSeverity(); } -void LogMessage::ConfigureLogging(const char* params) { +void LogMessage::ConfigureLogging(absl::string_view params) { LoggingSeverity current_level = LS_VERBOSE; LoggingSeverity debug_level = GetLogToDebug(); @@ -355,13 +355,14 @@ } #if defined(WEBRTC_ANDROID) -void LogMessage::OutputToDebug(const std::string& str, +void LogMessage::OutputToDebug(absl::string_view str, LoggingSeverity severity, const char* tag) { #else -void LogMessage::OutputToDebug(const std::string& str, +void LogMessage::OutputToDebug(absl::string_view str, LoggingSeverity severity) { #endif + std::string str_str = std::string(str); bool log_to_stderr = log_to_stderr_; #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG) // On the Mac, all stderr output goes to the Console log and causes clutter. @@ -386,7 +387,7 @@ #if defined(WEBRTC_WIN) // Always log to the debugger. // Perhaps stderr should be controlled by a preference, as on Mac? - OutputDebugStringA(str.c_str()); + OutputDebugStringA(str_str.c_str()); if (log_to_stderr) { // This handles dynamically allocated consoles, too. if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) { @@ -426,14 +427,14 @@ int idx = 0; const int max_lines = size / kMaxLogLineSize + 1; if (max_lines == 1) { - __android_log_print(prio, tag, "%.*s", size, str.c_str()); + __android_log_print(prio, tag, "%.*s", size, str_str.c_str()); } else { while (size > 0) { const int len = std::min(size, kMaxLogLineSize); // Use the size of the string in the format (str may have \0 in the // middle). __android_log_print(prio, tag, "[%d/%d] %.*s", line + 1, max_lines, len, - str.c_str() + idx); + str_str.c_str() + idx); idx += len; size -= len; ++line; @@ -441,7 +442,7 @@ } #endif // WEBRTC_ANDROID if (log_to_stderr) { - fprintf(stderr, "%s", str.c_str()); + fprintf(stderr, "%s", str_str.c_str()); fflush(stderr); } }
diff --git a/rtc_base/logging.h b/rtc_base/logging.h index ee4d410..a1339aa 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h
@@ -439,7 +439,7 @@ LogMessage(const char* file, int line, LoggingSeverity sev, - const std::string& tag); + absl::string_view tag); ~LogMessage(); LogMessage(const LogMessage&) = delete; @@ -485,7 +485,7 @@ static int GetMinLogSeverity(); // Parses the provided parameter stream to configure the options above. // Useful for configuring logging from the command line. - static void ConfigureLogging(const char* params); + static void ConfigureLogging(absl::string_view params); // Checks the current global debug severity and if the `streams_` collection // is empty. If `severity` is smaller than the global severity and if the // `streams_` collection is empty, the LogMessage will be considered a noop @@ -516,7 +516,7 @@ LogMessage(const char* file, int line, LoggingSeverity sev, - const std::string& tag) {} + absl::string_view tag) {} ~LogMessage() = default; inline void AddTag(const char* tag) {} @@ -534,7 +534,7 @@ inline static void RemoveLogToStream(LogSink* stream) {} inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; } inline static int GetMinLogSeverity() { return 0; } - inline static void ConfigureLogging(const char* params) {} + inline static void ConfigureLogging(absl::string_view params) {} static constexpr bool IsNoop(LoggingSeverity severity) { return true; } template <LoggingSeverity S> static constexpr bool IsNoop() { @@ -551,11 +551,11 @@ // These write out the actual log messages. #if defined(WEBRTC_ANDROID) - static void OutputToDebug(const std::string& msg, + static void OutputToDebug(absl::string_view msg, LoggingSeverity severity, const char* tag); #else - static void OutputToDebug(const std::string& msg, LoggingSeverity severity); + static void OutputToDebug(absl::string_view msg, LoggingSeverity severity); #endif // defined(WEBRTC_ANDROID) // Called from the dtor (or from a test) to append optional extra error @@ -592,11 +592,11 @@ // Next methods do nothing; no one will call these functions. inline static void UpdateMinLogSeverity() {} #if defined(WEBRTC_ANDROID) - inline static void OutputToDebug(const std::string& msg, + inline static void OutputToDebug(absl::string_view msg, LoggingSeverity severity, const char* tag) {} #else - inline static void OutputToDebug(const std::string& msg, + inline static void OutputToDebug(absl::string_view msg, LoggingSeverity severity) {} #endif // defined(WEBRTC_ANDROID) inline void FinishPrintStream() {}
diff --git a/rtc_base/net_helpers.cc b/rtc_base/net_helpers.cc index f521f0f..f092989 100644 --- a/rtc_base/net_helpers.cc +++ b/rtc_base/net_helpers.cc
@@ -11,6 +11,9 @@ #include "rtc_base/net_helpers.h" #include <memory> +#include <string> + +#include "absl/strings/string_view.h" #if defined(WEBRTC_WIN) #include <ws2spi.h> @@ -37,11 +40,12 @@ #endif } -int inet_pton(int af, const char* src, void* dst) { +int inet_pton(int af, absl::string_view src, void* dst) { + std::string src_str = std::string(src); #if defined(WEBRTC_WIN) - return win32_inet_pton(af, src, dst); + return win32_inet_pton(af, src_str.c_str(), dst); #else - return ::inet_pton(af, src, dst); + return ::inet_pton(af, src_str.c_str(), dst); #endif }
diff --git a/rtc_base/net_helpers.h b/rtc_base/net_helpers.h index 4ed8478..631c634 100644 --- a/rtc_base/net_helpers.h +++ b/rtc_base/net_helpers.h
@@ -19,12 +19,14 @@ #include "rtc_base/win32.h" #endif +#include "absl/strings/string_view.h" + namespace rtc { // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid // the windows-native versions of these. const char* inet_ntop(int af, const void* src, char* dst, socklen_t size); -int inet_pton(int af, const char* src, void* dst); +int inet_pton(int af, absl::string_view src, void* dst); bool HasIPv4Enabled(); bool HasIPv6Enabled();
diff --git a/rtc_base/network.cc b/rtc_base/network.cc index 0369ccc..a7b9197 100644 --- a/rtc_base/network.cc +++ b/rtc_base/network.cc
@@ -216,7 +216,7 @@ // result of the downstream network filtering, see e.g. // BasicPortAllocatorSession::GetNetworks when // PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is turned on. -AdapterType GetAdapterTypeFromName(const char* network_name) { +AdapterType GetAdapterTypeFromName(absl::string_view network_name) { if (MatchTypeNameWithIndexPattern(network_name, "lo")) { // Note that we have a more robust way to determine if a network interface // is a loopback interface by checking the flag IFF_LOOPBACK in ifa_flags of
diff --git a/rtc_base/network.h b/rtc_base/network.h index 04c91cc..715982c 100644 --- a/rtc_base/network.h +++ b/rtc_base/network.h
@@ -63,7 +63,7 @@ // Utility function that attempts to determine an adapter type by an interface // name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other // mechanisms fail to determine the type. -RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name); +RTC_EXPORT AdapterType GetAdapterTypeFromName(absl::string_view network_name); class DefaultLocalAddressProvider { public:
diff --git a/rtc_base/openssl_adapter.cc b/rtc_base/openssl_adapter.cc index dc9aefa..d5c51ff 100644 --- a/rtc_base/openssl_adapter.cc +++ b/rtc_base/openssl_adapter.cc
@@ -252,11 +252,11 @@ role_ = role; } -int OpenSSLAdapter::StartSSL(const char* hostname) { +int OpenSSLAdapter::StartSSL(absl::string_view hostname) { if (state_ != SSL_NONE) return -1; - ssl_host_name_ = hostname; + ssl_host_name_.assign(hostname.data(), hostname.size()); if (GetSocket()->GetState() != Socket::CS_CONNECTED) { state_ = SSL_WAIT; @@ -422,7 +422,7 @@ return 0; } -void OpenSSLAdapter::Error(const char* context, int err, bool signal) { +void OpenSSLAdapter::Error(absl::string_view context, int err, bool signal) { RTC_LOG(LS_WARNING) << "OpenSSLAdapter::Error(" << context << ", " << err << ")"; state_ = SSL_ERROR;
diff --git a/rtc_base/openssl_adapter.h b/rtc_base/openssl_adapter.h index 942d1fe..d5e8b9e 100644 --- a/rtc_base/openssl_adapter.h +++ b/rtc_base/openssl_adapter.h
@@ -61,7 +61,7 @@ void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override; void SetIdentity(std::unique_ptr<SSLIdentity> identity) override; void SetRole(SSLRole role) override; - int StartSSL(const char* hostname) override; + int StartSSL(absl::string_view hostname) override; int Send(const void* pv, size_t cb) override; int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; int Recv(void* pv, size_t cb, int64_t* timestamp) override; @@ -110,7 +110,7 @@ int BeginSSL(); int ContinueSSL(); - void Error(const char* context, int err, bool signal = true); + void Error(absl::string_view context, int err, bool signal = true); void Cleanup(); // Return value and arguments have the same meanings as for Send; `error` is
diff --git a/rtc_base/openssl_stream_adapter.cc b/rtc_base/openssl_stream_adapter.cc index 90870cd..94b97d7 100644 --- a/rtc_base/openssl_stream_adapter.cc +++ b/rtc_base/openssl_stream_adapter.cc
@@ -956,7 +956,7 @@ return 0; } -void OpenSSLStreamAdapter::Error(const char* context, +void OpenSSLStreamAdapter::Error(absl::string_view context, int err, uint8_t alert, bool signal) {
diff --git a/rtc_base/openssl_stream_adapter.h b/rtc_base/openssl_stream_adapter.h index de20ba9..288ed84 100644 --- a/rtc_base/openssl_stream_adapter.h +++ b/rtc_base/openssl_stream_adapter.h
@@ -173,7 +173,7 @@ // `alert` indicates an alert description (one of the SSL_AD constants) to // send to the remote endpoint when closing the association. If 0, a normal // shutdown will be performed. - void Error(const char* context, int err, uint8_t alert, bool signal); + void Error(absl::string_view context, int err, uint8_t alert, bool signal); void Cleanup(uint8_t alert); // Flush the input buffers by reading left bytes (for DTLS)
diff --git a/rtc_base/socket_adapters.cc b/rtc_base/socket_adapters.cc index bc7d2d8..4ec93ae 100644 --- a/rtc_base/socket_adapters.cc +++ b/rtc_base/socket_adapters.cc
@@ -411,8 +411,9 @@ } else if ((state_ == PS_AUTHENTICATE) && absl::StartsWithIgnoreCase(data, "Proxy-Authenticate:")) { std::string response, auth_method; - switch (HttpAuthenticate(data + 19, len - 19, proxy_, "CONNECT", "/", user_, - pass_, context_, response, auth_method)) { + switch (HttpAuthenticate(absl::string_view(data + 19, len - 19), proxy_, + "CONNECT", "/", user_, pass_, context_, response, + auth_method)) { case HAR_IGNORE: RTC_LOG(LS_VERBOSE) << "Ignoring Proxy-Authenticate: " << auth_method; if (!unknown_mechanisms_.empty())
diff --git a/rtc_base/ssl_adapter.h b/rtc_base/ssl_adapter.h index 8f98141..4b8b9c7 100644 --- a/rtc_base/ssl_adapter.h +++ b/rtc_base/ssl_adapter.h
@@ -14,6 +14,7 @@ #include <string> #include <vector> +#include "absl/strings/string_view.h" #include "rtc_base/async_socket.h" #include "rtc_base/ssl_certificate.h" #include "rtc_base/ssl_identity.h" @@ -89,7 +90,7 @@ // StartSSL returns 0 if successful. // If StartSSL is called while the socket is closed or connecting, the SSL // negotiation will begin as soon as the socket connects. - virtual int StartSSL(const char* hostname) = 0; + virtual int StartSSL(absl::string_view hostname) = 0; // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume // a previous SSL session, which results in an abbreviated handshake.
diff --git a/rtc_base/ssl_adapter_unittest.cc b/rtc_base/ssl_adapter_unittest.cc index 430343e..cd63249 100644 --- a/rtc_base/ssl_adapter_unittest.cc +++ b/rtc_base/ssl_adapter_unittest.cc
@@ -110,7 +110,7 @@ RTC_LOG(LS_INFO) << "Starting " << GetSSLProtocolName(ssl_mode_) << " handshake with " << hostname; - if (ssl_adapter_->StartSSL(std::string(hostname).c_str()) != 0) { + if (ssl_adapter_->StartSSL(hostname) != 0) { return -1; } }
diff --git a/rtc_base/ssl_stream_adapter_unittest.cc b/rtc_base/ssl_stream_adapter_unittest.cc index 262eeef..f26166f 100644 --- a/rtc_base/ssl_stream_adapter_unittest.cc +++ b/rtc_base/ssl_stream_adapter_unittest.cc
@@ -685,7 +685,7 @@ return server_ssl_->GetSslVersion(); } - bool ExportKeyingMaterial(const char* label, + bool ExportKeyingMaterial(absl::string_view label, const unsigned char* context, size_t context_len, bool use_context,
diff --git a/rtc_base/system/BUILD.gn b/rtc_base/system/BUILD.gn index 87b2d9d..a34a151 100644 --- a/rtc_base/system/BUILD.gn +++ b/rtc_base/system/BUILD.gn
@@ -30,6 +30,7 @@ "..:criticalsection", "..:safe_conversions", ] + absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] } if (rtc_include_tests) {
diff --git a/rtc_base/system/file_wrapper.cc b/rtc_base/system/file_wrapper.cc index 3e49315..1979e6f 100644 --- a/rtc_base/system/file_wrapper.cc +++ b/rtc_base/system/file_wrapper.cc
@@ -9,10 +9,12 @@ */ #include "rtc_base/system/file_wrapper.h" -#include "rtc_base/numerics/safe_conversions.h" #include <cerrno> +#include "absl/strings/string_view.h" +#include "rtc_base/numerics/safe_conversions.h" + #ifdef _WIN32 #include <Windows.h> #else @@ -23,14 +25,17 @@ namespace webrtc { namespace { -FILE* FileOpen(const char* file_name_utf8, bool read_only, int* error) { +FILE* FileOpen(absl::string_view file_name_utf8, bool read_only, int* error) { + RTC_CHECK_EQ(file_name_utf8.find_first_of('\0'), absl::string_view::npos) + << "Invalid filename, containing NUL character"; + std::string file_name = std::string(file_name_utf8); #if defined(_WIN32) - int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0); + int len = MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, nullptr, 0); std::wstring wstr(len, 0); - MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len); + MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, &wstr[0], len); FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb"); #else - FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb"); + FILE* file = fopen(file_name.c_str(), read_only ? "rb" : "wb"); #endif if (!file && error) { *error = errno; @@ -38,36 +43,19 @@ return file; } -const char* GetCstrCheckNoEmbeddedNul(const std::string& s) { - const char* p = s.c_str(); - RTC_CHECK_EQ(strlen(p), s.size()) - << "Invalid filename, containing NUL character"; - return p; -} } // namespace // static -FileWrapper FileWrapper::OpenReadOnly(const char* file_name_utf8) { +FileWrapper FileWrapper::OpenReadOnly(absl::string_view file_name_utf8) { return FileWrapper(FileOpen(file_name_utf8, true, nullptr)); } // static -FileWrapper FileWrapper::OpenReadOnly(const std::string& file_name_utf8) { - return OpenReadOnly(GetCstrCheckNoEmbeddedNul(file_name_utf8)); -} - -// static -FileWrapper FileWrapper::OpenWriteOnly(const char* file_name_utf8, +FileWrapper FileWrapper::OpenWriteOnly(absl::string_view file_name_utf8, int* error /*=nullptr*/) { return FileWrapper(FileOpen(file_name_utf8, false, error)); } -// static -FileWrapper FileWrapper::OpenWriteOnly(const std::string& file_name_utf8, - int* error /*=nullptr*/) { - return OpenWriteOnly(GetCstrCheckNoEmbeddedNul(file_name_utf8), error); -} - FileWrapper::FileWrapper(FileWrapper&& other) { operator=(std::move(other)); }
diff --git a/rtc_base/system/file_wrapper.h b/rtc_base/system/file_wrapper.h index b55b0b9..5e1e3d6 100644 --- a/rtc_base/system/file_wrapper.h +++ b/rtc_base/system/file_wrapper.h
@@ -16,6 +16,8 @@ #include <string> +#include "absl/strings/string_view.h" + // Implementation that can read (exclusive) or write from/to a file. namespace webrtc { @@ -34,11 +36,8 @@ // returned object to check if the open operation was successful. On failure, // and if `error` is non-null, the system errno value is stored at |*error|. // The file is closed by the destructor. - static FileWrapper OpenReadOnly(const char* file_name_utf8); - static FileWrapper OpenReadOnly(const std::string& file_name_utf8); - static FileWrapper OpenWriteOnly(const char* file_name_utf8, - int* error = nullptr); - static FileWrapper OpenWriteOnly(const std::string& file_name_utf8, + static FileWrapper OpenReadOnly(absl::string_view file_name_utf8); + static FileWrapper OpenWriteOnly(absl::string_view file_name_utf8, int* error = nullptr); FileWrapper() = default;