From dc0911bd37d7b9d3599a5e6433339b764312e446 Mon Sep 17 00:00:00 2001 From: Ali Tofigh Date: Wed, 17 Aug 2022 21:52:37 +0200 Subject: [PATCH] Adopt absl::string_view in modules/desktop_capture/ Bug: webrtc:13579 Change-Id: I9fbc3d6df2dedb7eac908e1af38300d2b42142c8 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/272000 Commit-Queue: Ali Tofigh Reviewed-by: Alexander Cooper Cr-Commit-Position: refs/heads/main@{#37817} --- modules/desktop_capture/BUILD.gn | 1 + .../linux/wayland/xdg_desktop_portal_utils.cc | 53 +++++++++++-------- .../linux/wayland/xdg_desktop_portal_utils.h | 17 +++--- .../linux/x11/shared_x_display.cc | 7 +-- .../linux/x11/shared_x_display.h | 4 +- .../full_screen_mac_application_handler.cc | 16 +++--- .../screen_drawer_lock_posix.cc | 5 +- .../screen_drawer_lock_posix.h | 3 +- 8 files changed, 62 insertions(+), 44 deletions(-) diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn index fba4538656..689d659116 100644 --- a/modules/desktop_capture/BUILD.gn +++ b/modules/desktop_capture/BUILD.gn @@ -209,6 +209,7 @@ if (rtc_include_tests) { "../../rtc_base:logging", "../../system_wrappers", ] + absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] if (is_posix || is_fuchsia) { sources += [ diff --git a/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.cc b/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.cc index 4fdf54abce..4a74b9f188 100644 --- a/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.cc +++ b/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.cc @@ -9,6 +9,9 @@ */ #include "modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.h" +#include + +#include "absl/strings/string_view.h" #include "modules/desktop_capture/linux/wayland/scoped_glib.h" #include "rtc_base/logging.h" @@ -30,7 +33,7 @@ std::string RequestResponseToString(RequestResponse request) { } } -std::string PrepareSignalHandle(const char* token, +std::string PrepareSignalHandle(absl::string_view token, GDBusConnection* connection) { Scoped sender( g_strdup(g_dbus_connection_get_unique_name(connection) + 1)); @@ -39,33 +42,36 @@ std::string PrepareSignalHandle(const char* token, sender.get()[i] = '_'; } } - const char* handle = g_strconcat(kDesktopRequestObjectPath, "/", sender.get(), - "/", token, /*end of varargs*/ nullptr); + const char* handle = + g_strconcat(kDesktopRequestObjectPath, "/", sender.get(), "/", + std::string(token).c_str(), /*end of varargs*/ nullptr); return handle; } -uint32_t SetupRequestResponseSignal(const char* object_path, +uint32_t SetupRequestResponseSignal(absl::string_view object_path, const GDBusSignalCallback callback, gpointer user_data, GDBusConnection* connection) { return g_dbus_connection_signal_subscribe( connection, kDesktopBusName, kRequestInterfaceName, "Response", - object_path, /*arg0=*/nullptr, G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE, - callback, user_data, /*user_data_free_func=*/nullptr); + std::string(object_path).c_str(), /*arg0=*/nullptr, + G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE, callback, user_data, + /*user_data_free_func=*/nullptr); } -void RequestSessionProxy(const char* interface_name, +void RequestSessionProxy(absl::string_view interface_name, const ProxyRequestCallback proxy_request_callback, GCancellable* cancellable, gpointer user_data) { g_dbus_proxy_new_for_bus( G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, /*info=*/nullptr, - kDesktopBusName, kDesktopObjectPath, interface_name, cancellable, + kDesktopBusName, kDesktopObjectPath, std::string(interface_name).c_str(), + cancellable, reinterpret_cast(proxy_request_callback), user_data); } void SetupSessionRequestHandlers( - const std::string& portal_prefix, + absl::string_view portal_prefix, const SessionRequestCallback session_request_callback, const SessionRequestResponseSignalHandler request_response_signale_handler, GDBusConnection* connection, @@ -78,13 +84,15 @@ void SetupSessionRequestHandlers( Scoped variant_string; g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT); - variant_string = g_strdup_printf("%s_session%d", portal_prefix.c_str(), - g_random_int_range(0, G_MAXINT)); + variant_string = + g_strdup_printf("%.*s_session%d", static_cast(portal_prefix.size()), + portal_prefix.data(), g_random_int_range(0, G_MAXINT)); g_variant_builder_add(&builder, "{sv}", "session_handle_token", g_variant_new_string(variant_string.get())); - variant_string = g_strdup_printf("%s_%d", portal_prefix.c_str(), - g_random_int_range(0, G_MAXINT)); + variant_string = + g_strdup_printf("%.*s_%d", static_cast(portal_prefix.size()), + portal_prefix.data(), g_random_int_range(0, G_MAXINT)); g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(variant_string.get())); @@ -102,8 +110,8 @@ void SetupSessionRequestHandlers( } void StartSessionRequest( - const std::string& prefix, - const std::string session_handle, + absl::string_view prefix, + absl::string_view session_handle, const StartRequestResponseSignalHandler signal_handler, const SessionStartRequestedHandler session_started_handler, GDBusProxy* proxy, @@ -117,7 +125,8 @@ void StartSessionRequest( g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT); variant_string = - g_strdup_printf("%s%d", prefix.c_str(), g_random_int_range(0, G_MAXINT)); + g_strdup_printf("%.*s%d", static_cast(prefix.size()), prefix.data(), + g_random_int_range(0, G_MAXINT)); g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(variant_string.get())); @@ -131,21 +140,21 @@ void StartSessionRequest( RTC_LOG(LS_INFO) << "Starting the portal session."; g_dbus_proxy_call( proxy, "Start", - g_variant_new("(osa{sv})", session_handle.c_str(), parent_window, - &builder), + g_variant_new("(osa{sv})", std::string(session_handle).c_str(), + parent_window, &builder), G_DBUS_CALL_FLAGS_NONE, /*timeout=*/-1, cancellable, reinterpret_cast(session_started_handler), user_data); } -void TearDownSession(std::string session_handle, +void TearDownSession(absl::string_view session_handle, GDBusProxy* proxy, GCancellable* cancellable, GDBusConnection* connection) { if (!session_handle.empty()) { - Scoped message( - g_dbus_message_new_method_call(kDesktopBusName, session_handle.c_str(), - kSessionInterfaceName, "Close")); + Scoped message(g_dbus_message_new_method_call( + kDesktopBusName, std::string(session_handle).c_str(), + kSessionInterfaceName, "Close")); if (message.get()) { Scoped error; g_dbus_connection_send_message(connection, message.get(), diff --git a/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.h b/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.h index 7f1ef0b8d5..edd046639b 100644 --- a/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.h +++ b/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.h @@ -17,6 +17,7 @@ #include #include +#include "absl/strings/string_view.h" #include "modules/desktop_capture/linux/wayland/portal_request_response.h" #include "modules/desktop_capture/linux/wayland/scoped_glib.h" #include "modules/desktop_capture/linux/wayland/xdg_session_details.h" @@ -43,7 +44,6 @@ using SessionRequestResponseSignalHandler = void (*)(GDBusConnection*, const char*, GVariant*, gpointer); -using SessionRequestResponseSignalCallback = void (*)(std::string); using StartRequestResponseSignalHandler = void (*)(GDBusConnection*, const char*, const char*, @@ -59,22 +59,23 @@ std::string RequestResponseToString(RequestResponse request); // Returns a string path for signal handle based on the provided connection and // token. -std::string PrepareSignalHandle(const char* token, GDBusConnection* connection); +std::string PrepareSignalHandle(absl::string_view token, + GDBusConnection* connection); // Sets up the callback to execute when a response signal is received for the // given object. -uint32_t SetupRequestResponseSignal(const char* object_path, +uint32_t SetupRequestResponseSignal(absl::string_view object_path, const GDBusSignalCallback callback, gpointer user_data, GDBusConnection* connection); -void RequestSessionProxy(const char* interface_name, +void RequestSessionProxy(absl::string_view interface_name, const ProxyRequestCallback proxy_request_callback, GCancellable* cancellable, gpointer user_data); void SetupSessionRequestHandlers( - const std::string& portal_prefix, + absl::string_view portal_prefix, const SessionRequestCallback session_request_callback, const SessionRequestResponseSignalHandler request_response_signale_handler, GDBusConnection* connection, @@ -85,8 +86,8 @@ void SetupSessionRequestHandlers( gpointer user_data); void StartSessionRequest( - const std::string& prefix, - const std::string session_handle, + absl::string_view prefix, + absl::string_view session_handle, const StartRequestResponseSignalHandler signal_handler, const SessionStartRequestedHandler session_started_handler, GDBusProxy* proxy, @@ -97,7 +98,7 @@ void StartSessionRequest( gpointer user_data); // Tears down the portal session and cleans up related objects. -void TearDownSession(std::string session_handle, +void TearDownSession(absl::string_view session_handle, GDBusProxy* proxy, GCancellable* cancellable, GDBusConnection* connection); diff --git a/modules/desktop_capture/linux/x11/shared_x_display.cc b/modules/desktop_capture/linux/x11/shared_x_display.cc index ad2e04374d..b7849508b0 100644 --- a/modules/desktop_capture/linux/x11/shared_x_display.cc +++ b/modules/desktop_capture/linux/x11/shared_x_display.cc @@ -15,6 +15,7 @@ #include +#include "absl/strings/string_view.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" @@ -31,9 +32,9 @@ SharedXDisplay::~SharedXDisplay() { // static rtc::scoped_refptr SharedXDisplay::Create( - const std::string& display_name) { - Display* display = - XOpenDisplay(display_name.empty() ? NULL : display_name.c_str()); + absl::string_view display_name) { + Display* display = XOpenDisplay( + display_name.empty() ? NULL : std::string(display_name).c_str()); if (!display) { RTC_LOG(LS_ERROR) << "Unable to open display"; return nullptr; diff --git a/modules/desktop_capture/linux/x11/shared_x_display.h b/modules/desktop_capture/linux/x11/shared_x_display.h index 6405c36a2d..084da80167 100644 --- a/modules/desktop_capture/linux/x11/shared_x_display.h +++ b/modules/desktop_capture/linux/x11/shared_x_display.h @@ -12,9 +12,9 @@ #define MODULES_DESKTOP_CAPTURE_LINUX_X11_SHARED_X_DISPLAY_H_ #include -#include #include +#include "absl/strings/string_view.h" #include "api/ref_counted_base.h" #include "api/scoped_refptr.h" #include "rtc_base/system/rtc_export.h" @@ -42,7 +42,7 @@ class RTC_EXPORT SharedXDisplay // connection failed. Equivalent to CreateDefault() when `display_name` is // empty. static rtc::scoped_refptr Create( - const std::string& display_name); + absl::string_view display_name); // Creates X11 Display connection for the default display (e.g. specified in // DISPLAY). NULL is returned if X11 connection failed. diff --git a/modules/desktop_capture/mac/full_screen_mac_application_handler.cc b/modules/desktop_capture/mac/full_screen_mac_application_handler.cc index 6eab0c753e..45cd3223d2 100644 --- a/modules/desktop_capture/mac/full_screen_mac_application_handler.cc +++ b/modules/desktop_capture/mac/full_screen_mac_application_handler.cc @@ -9,11 +9,15 @@ */ #include "modules/desktop_capture/mac/full_screen_mac_application_handler.h" + #include + #include #include #include + #include "absl/strings/match.h" +#include "absl/strings/string_view.h" #include "api/function_view.h" #include "modules/desktop_capture/mac/window_list_utils.h" @@ -52,7 +56,7 @@ static constexpr const char* kPowerPointSlideShowTitles[] = { class FullScreenMacApplicationHandler : public FullScreenApplicationHandler { public: using TitlePredicate = - std::function; + std::function; FullScreenMacApplicationHandler(DesktopCapturer::SourceId sourceId, TitlePredicate title_predicate, @@ -134,14 +138,14 @@ class FullScreenMacApplicationHandler : public FullScreenApplicationHandler { mutable DesktopCapturer::SourceList cache_sources_; }; -bool equal_title_predicate(const std::string& original_title, - const std::string& title) { +bool equal_title_predicate(absl::string_view original_title, + absl::string_view title) { return original_title == title; } -bool slide_show_title_predicate(const std::string& original_title, - const std::string& title) { - if (title.find(original_title) == std::string::npos) +bool slide_show_title_predicate(absl::string_view original_title, + absl::string_view title) { + if (title.find(original_title) == absl::string_view::npos) return false; for (const char* pp_slide_title : kPowerPointSlideShowTitles) { diff --git a/modules/desktop_capture/screen_drawer_lock_posix.cc b/modules/desktop_capture/screen_drawer_lock_posix.cc index bfee619fdb..28cb501fe7 100644 --- a/modules/desktop_capture/screen_drawer_lock_posix.cc +++ b/modules/desktop_capture/screen_drawer_lock_posix.cc @@ -13,6 +13,7 @@ #include #include +#include "absl/strings/string_view.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" @@ -51,8 +52,8 @@ ScreenDrawerLockPosix::~ScreenDrawerLockPosix() { } // static -void ScreenDrawerLockPosix::Unlink(const char* name) { - sem_unlink(name); +void ScreenDrawerLockPosix::Unlink(absl::string_view name) { + sem_unlink(std::string(name).c_str()); } } // namespace webrtc diff --git a/modules/desktop_capture/screen_drawer_lock_posix.h b/modules/desktop_capture/screen_drawer_lock_posix.h index 1d5adf2c57..13899b2d75 100644 --- a/modules/desktop_capture/screen_drawer_lock_posix.h +++ b/modules/desktop_capture/screen_drawer_lock_posix.h @@ -13,6 +13,7 @@ #include +#include "absl/strings/string_view.h" #include "modules/desktop_capture/screen_drawer.h" namespace webrtc { @@ -27,7 +28,7 @@ class ScreenDrawerLockPosix final : public ScreenDrawerLock { // Unlinks the named semaphore actively. This will remove the sem_t object in // the system and allow others to create a different sem_t object with the // same/ name. - static void Unlink(const char* name); + static void Unlink(absl::string_view name); private: sem_t* semaphore_;