Repository: electron/electron (language: cpp)

Issue:
[Bug]: app_id not set when running Electron with native Wayland
### Preflight Checklist

- [X] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [X] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

18.0.1

### What operating system are you using?

Other Linux

### Operating System Version

Arch Linux

### What arch are you using?

x64

### Last Known Working Electron version

18.0.0

### Expected Behavior

The `app_id` window property should be set to the same value as `class` under X11.

### Actual Behavior

`app_id` is an empty string when running Electron under Wayland.

### Testcase Gist URL

_No response_

### Additional Information

While most apps would crash prior to #33355, the default page if you run `electron --ozone-platform=wayland` would still run and properly set `app_id`.


Discussion / hints:
/cc @p2004a
I will try to reproduce and bisect the issue in the next couple of days.
@chrhasse I have problem with reproducing this.

Using test app from https://www.electronjs.org/docs/latest/tutorial/quick-start

When I run electron version 18.0.0 under X11 I see that the `WM_CLASS` is set as application name `my-electron-app`. When I run on wayland I get:

```
$ WAYLAND_DEBUG=1 ELECTRON_ENABLE_LOGGING=true ~/Downloads/electron-v18.0.0-linux-x64/electron --ozone-platform=wayland test_app |& grep 'app_id'
[2507759.298]  -> xdg_toplevel@46.set_app_id("")
```

So the `app_id` is not set under wayland already under version 18.0.0 (I've not tested anything earlier then that).
Sorry, I think I must have made a mistake in my testing. I can't easily test older builds from this repository as they crash due to #32436 (for my system on Arch Linux with Sway 1.7), so I've been testing using my distro's packages. I had built 18.0.0 by modifying the PKGBUILD for 17.2.0, but I deleted the PKGBUILD and the package after my initial testing, and when I rebuilt it using a modified 18.0.1 PKGBUILD for convenience's sake, it wasn't setting the app_id. I'm not sure if I tested the wrong version initially or if it has to do with the package maintainer's patches (which can be found [here](https://github.com/archlinux/svntogit-community/tree/packages/electron/trunk)). I can rebuild with a modified 17.2.0 PKGBUILD if I needed, but compilation takes me 2 and a half hours so I'd rather avoid it. 

At any rate, if I downgrade to 17.2.0 from the Arch repositories and run:

```
$ WAYLAND_DEBUG=1 ELECTRON_ENABLE_LOGGING=true electron --ozone-platform=wayland |& grep 'app_id' 
[2615031.990]  -> xdg_toplevel@45.set_app_id("Electron")
```

The only version I can test from the GitHub releases is 13.6.9 which also properly sets app_id:

```
$ WAYLAND_DEBUG=1 ELECTRON_ENABLE_LOGGING=true ./electron --enable-features=UseOzonePlatform --ozone-platform=wayland |& grep 'app_id'
[2926405.757]  -> xdg_toplevel@35.set_app_id("Electron")
```
Thanks, so the regression isn't between 18.0.0 and 18.0.1 but between 17 and 18 branches and is not related to my fixes.

I did `git bisect` on the main branch and 28ada6ea8bd2abeca8812e6d284c1f5f3974286b is the first commit where `app_id` is not set, introducing this regression.
Hi @p2004a, first of all thanks for your work.
Do you have any update on this issue? I start having applications without the name:
![image](https://user-images.githubusercontent.com/20546217/170245572-89e16676-a17a-44f4-b2c9-f5cb8a5374c6.png)
The one in the screenshot is Mattermost 5.1.0 which use Electron to v18.0.3.

Thanks ENx
@enx1998 I'm not currently actively working on it, I've only verified that this is not caused by my changes.

Fix patch:
diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc
index f33a3705cf031..ebdb6d97610fc 100644
--- a/shell/browser/native_window_views.cc
+++ b/shell/browser/native_window_views.cc
@@ -51,6 +51,7 @@
 #include "shell/browser/ui/views/client_frame_view_linux.h"
 #include "shell/browser/ui/views/frameless_view.h"
 #include "shell/browser/ui/views/native_frame_view.h"
+#include "shell/common/platform_util.h"
 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
 #include "ui/views/window/native_frame_view.h"
 
@@ -271,6 +272,8 @@ NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options,
   // Set WM_CLASS.
   params.wm_class_name = base::ToLowerASCII(name);
   params.wm_class_class = name;
+  // Set Wayland application ID.
+  params.wayland_app_id = platform_util::GetXdgAppId();
 
   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 07d7135788de1..3f7a2fa24f887 100644
--- a/shell/browser/notifications/linux/libnotify_notification.cc
+++ b/shell/browser/notifications/linux/libnotify_notification.cc
@@ -9,7 +9,6 @@
 
 #include "base/files/file_enumerator.h"
 #include "base/logging.h"
-#include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "shell/browser/notifications/notification_delegate.h"
 #include "shell/browser/ui/gtk_util.h"
@@ -138,13 +137,8 @@ void LibnotifyNotification::Show(const NotificationOptions& options) {
 
   // Send the desktop name to identify the application
   // The desktop-entry is the part before the .desktop
-  std::string desktop_id;
-  if (platform_util::GetDesktopName(&desktop_id)) {
-    const std::string suffix{".desktop"};
-    if (base::EndsWith(desktop_id, suffix,
-                       base::CompareCase::INSENSITIVE_ASCII)) {
-      desktop_id.resize(desktop_id.size() - suffix.size());
-    }
+  std::string desktop_id = platform_util::GetXdgAppId();
+  if (!desktop_id.empty()) {
     libnotify_loader_.notify_notification_set_hint_string(
         notification_, "desktop-entry", desktop_id.c_str());
   }
diff --git a/shell/common/platform_util.h b/shell/common/platform_util.h
index ba8f9652dba29..085c7e3a7d926 100644
--- a/shell/common/platform_util.h
+++ b/shell/common/platform_util.h
@@ -56,6 +56,10 @@ bool SetLoginItemEnabled(bool enabled);
 // Returns a success flag.
 // Unlike libgtkui, does *not* use "chromium-browser.desktop" as a fallback.
 bool GetDesktopName(std::string* setme);
+
+// The XDG application ID must match the name of the desktop entry file without
+// the .desktop extension.
+std::string GetXdgAppId();
 #endif
 
 }  // namespace platform_util
diff --git a/shell/common/platform_util_linux.cc b/shell/common/platform_util_linux.cc
index ef8d8f8f0de01..2018c6d8864bf 100644
--- a/shell/common/platform_util_linux.cc
+++ b/shell/common/platform_util_linux.cc
@@ -20,6 +20,7 @@
 #include "base/posix/eintr_wrapper.h"
 #include "base/process/kill.h"
 #include "base/process/launch.h"
+#include "base/strings/string_util.h"
 #include "base/threading/thread_restrictions.h"
 #include "components/dbus/thread_linux/dbus_thread_linux.h"
 #include "content/public/browser/browser_thread.h"
@@ -403,4 +404,18 @@ bool GetDesktopName(std::string* setme) {
   return base::Environment::Create()->GetVar("CHROME_DESKTOP", setme);
 }
 
+std::string GetXdgAppId() {
+  std::string desktop_file_name;
+  if (GetDesktopName(&desktop_file_name)) {
+    const std::string 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;
+}
+
 }  // namespace platform_util


Test patch: