refactor: have GetXdgAppId() return a std::optional<std::string> (#49318)

* refactor: GetXdgAppId() now returns std::optional<std::string>

* refactor: slightly simplify app.setDesktopName() ts calls

* refactor: add better documentation for dekstop-entry in XDG notifications
This commit is contained in:
Charles Kerr
2026-01-07 13:30:09 -06:00
committed by GitHub
parent a89b2cd9bc
commit b5a7d81c7d
6 changed files with 32 additions and 33 deletions

View File

@@ -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) {

View File

@@ -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

View File

@@ -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;

View File

@@ -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<std::string> 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(

View File

@@ -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<std::string> 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<std::string> GetXdgAppId();
#endif
} // namespace platform_util

View File

@@ -413,18 +413,16 @@ std::optional<std::string> GetDesktopName() {
return base::Environment::Create()->GetVar("CHROME_DESKTOP");
}
std::string GetXdgAppId() {
if (std::optional<std::string> 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<std::string> 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