From b5a7d81c7dccb34ac53a2259f3497936146e7974 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 7 Jan 2026 13:30:09 -0600 Subject: [PATCH] refactor: have `GetXdgAppId()` return a `std::optional` (#49318) * refactor: GetXdgAppId() now returns std::optional * refactor: slightly simplify app.setDesktopName() ts calls * refactor: add better documentation for dekstop-entry in XDG notifications --- default_app/main.ts | 8 +++----- lib/browser/init.ts | 6 +----- shell/browser/native_window_views.cc | 4 +++- .../linux/libnotify_notification.cc | 14 +++++++------ shell/common/platform_util.h | 13 +++++++----- shell/common/platform_util_linux.cc | 20 +++++++++---------- 6 files changed, 32 insertions(+), 33 deletions(-) diff --git a/default_app/main.ts b/default_app/main.ts index 38fb3b665d..0ea8430700 100644 --- a/default_app/main.ts +++ b/default_app/main.ts @@ -110,11 +110,9 @@ async function loadApplicationPackage (packagePath: string) { } else if (packageJson.name) { app.name = packageJson.name; } - if (packageJson.desktopName) { - app.setDesktopName(packageJson.desktopName); - } else { - app.setDesktopName(`${app.name}.desktop`); - } + + app.setDesktopName(packageJson.desktopName || `${app.name}.desktop`); + // Set v8 flags, deliberately lazy load so that apps that do not use this // feature do not pay the price if (packageJson.v8Flags) { diff --git a/lib/browser/init.ts b/lib/browser/init.ts index 4a52cd0283..d8a007c185 100644 --- a/lib/browser/init.ts +++ b/lib/browser/init.ts @@ -126,11 +126,7 @@ if (packageJson.productName != null) { } // Set application's desktop name. -if (packageJson.desktopName != null) { - app.setDesktopName(packageJson.desktopName); -} else { - app.setDesktopName(`${app.name}.desktop`); -} +app.setDesktopName(packageJson.desktopName || `${app.name}.desktop`); // Set v8 flags, deliberately lazy load so that apps that do not use this // feature do not pay the price diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index 05e40ed631..1e37d646fa 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -290,7 +290,9 @@ NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options, params.wm_class_name = base::ToLowerASCII(name); params.wm_class_class = name; // Set Wayland application ID. - params.wayland_app_id = platform_util::GetXdgAppId(); + if (auto const app_id = platform_util::GetXdgAppId()) { + params.wayland_app_id = *app_id; + } auto* native_widget = new views::DesktopNativeWidgetAura(widget()); params.native_widget = native_widget; diff --git a/shell/browser/notifications/linux/libnotify_notification.cc b/shell/browser/notifications/linux/libnotify_notification.cc index 5364f1d9ed..ecdceeabb3 100644 --- a/shell/browser/notifications/linux/libnotify_notification.cc +++ b/shell/browser/notifications/linux/libnotify_notification.cc @@ -141,13 +141,15 @@ void LibnotifyNotification::Show(const NotificationOptions& options) { notification_, "x-canonical-append", g_variant_new_string("true")); } - // Send the desktop name to identify the application - // The desktop-entry is the part before the .desktop - std::string desktop_id = platform_util::GetXdgAppId(); - if (!desktop_id.empty()) { + // https://specifications.freedesktop.org/notification/latest-single/#id-1.9.7 + // This specifies the name of the desktop filename representing the + // calling program. This should be the same as the prefix used for the + // application's .desktop file. An example would be "rhythmbox" from + // "rhythmbox.desktop". This can be used by the daemon to retrieve the + // correct icon for the application, for logging purposes, etc. + if (const std::optional name = platform_util::GetXdgAppId()) { GetLibNotifyLoader().notify_notification_set_hint( - notification_, "desktop-entry", - g_variant_new_string(desktop_id.c_str())); + notification_, "desktop-entry", g_variant_new_string(name->c_str())); } GetLibNotifyLoader().notify_notification_set_hint( diff --git a/shell/common/platform_util.h b/shell/common/platform_util.h index ccca2f778b..51b4ac6ba7 100644 --- a/shell/common/platform_util.h +++ b/shell/common/platform_util.h @@ -61,13 +61,16 @@ bool SetLoginItemEnabled(const std::string& type, #endif #if BUILDFLAG(IS_LINUX) -// Returns a desktop name if available. -// Unlike libgtkui, does *not* use "chromium-browser.desktop" as a fallback. +// Returns a desktop name (e.g. 'myapp.desktop') if available. +// Unlike libgtkui, this does *not* use "chromium-browser.desktop" as a +// fallback. +// https://specifications.freedesktop.org/desktop-entry/latest/file-naming.html std::optional GetDesktopName(); -// The XDG application ID must match the name of the desktop entry file without -// the .desktop extension. -std::string GetXdgAppId(); +// Returns the app id (e.g. 'myapp') if available. +// This is equivalent to the basename of `GetDesktopName()`. +// https://developer.gnome.org/documentation/tutorials/application-id.html +std::optional GetXdgAppId(); #endif } // namespace platform_util diff --git a/shell/common/platform_util_linux.cc b/shell/common/platform_util_linux.cc index b5cedcff0a..a35aa15087 100644 --- a/shell/common/platform_util_linux.cc +++ b/shell/common/platform_util_linux.cc @@ -413,18 +413,16 @@ std::optional GetDesktopName() { return base::Environment::Create()->GetVar("CHROME_DESKTOP"); } -std::string GetXdgAppId() { - if (std::optional desktop_file_name = GetDesktopName()) { - constexpr std::string_view kDesktopExtension = ".desktop"; - if (base::EndsWith(*desktop_file_name, kDesktopExtension, - base::CompareCase::INSENSITIVE_ASCII)) { - desktop_file_name->resize(desktop_file_name->size() - - kDesktopExtension.size()); - } - return *desktop_file_name; - } +std::optional GetXdgAppId() { + auto name = GetDesktopName(); + if (!name) + return {}; - return ""; + // remove '.desktop' file suffix, if present + if (std::string_view suffix = ".desktop"; name->ends_with(suffix)) + name->resize(std::size(*name) - std::size(suffix)); + + return *name; } } // namespace platform_util