Repository: electron/electron
Pull request: fix: set `XDG_CURRENT_DESKTOP` env var back to original value before invoking xdg utils

#### Description of Change

<!--
Thank you for your Pull Request. Please provide a description above and review
the requirements below.

Contributors guide: https://github.com/electron/electron/blob/main/CONTRIBUTING.md
-->

This PR corrects the XDG_CURRENT_DESKTOP environment variable, with the goal of ensuring that `electron.shell.openExternal` and `electron.shell.openPath` actually honor the user-defined system defaults on Linux.

Currently, this variable is [overridden by Electron to `Unity`](https://www.electronjs.org/docs/latest/api/environment-variables#original_xdg_current_desktop) (the original value is preserved in the `ORIGINAL_XDG_CURRENT_DESKTOP` variable). This was introduced by https://github.com/electron/electron/pull/23512 to workaround https://github.com/electron/electron/issues/5050 and https://github.com/electron/electron/issues/9046, however it causes `xdg-open` and similar utilities which `electron.shell` relies on to no longer honor the system defaults if a user is running the GNOME desktop environment (I have only tested this on GNOME, but I assume this is valid for all non-Unity environments).

This change preserves the overridden environment variable value for use by Electron and Chromium. When a call needs to be made to the XDG utilities, the original value will be dynamically injected back into the environment variable set that is sent to the spawned XDG process.

cc @clavin @mlaurencin 

fixes #45129

#### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] PR description included and stakeholders cc'd
- [x] [PR release notes](https://github.com/electron/clerk/blob/main/README.md) describe the change in a way relevant to app developers, and are [capitalized, punctuated, and past tense](https://github.com/electron/clerk/blob/main/README.md#examples).

#### Release Notes

Notes: Fixed `electron.shell.openExternal` and `electron.shell.openPath` to honor user-defined system defaults on Linux


Changed files:
filename: shell/common/platform_util_linux.cc
patch: @@ -15,6 +15,7 @@
 
 #include "base/cancelable_callback.h"
 #include "base/containers/contains.h"
+#include "base/containers/map_util.h"
 #include "base/environment.h"
 #include "base/files/file_util.h"
 #include "base/files/scoped_file.h"
@@ -58,6 +59,8 @@ const char kFreedesktopPortalName[] = "org.freedesktop.portal.Desktop";
 const char kFreedesktopPortalPath[] = "/org/freedesktop/portal/desktop";
 const char kFreedesktopPortalOpenURI[] = "org.freedesktop.portal.OpenURI";
 
+const char kOriginalXdgCurrentDesktopEnvVar[] = "ORIGINAL_XDG_CURRENT_DESKTOP";
+
 const char kMethodOpenDirectory[] = "OpenDirectory";
 
 class ShowItemHelper {
@@ -282,6 +285,12 @@ bool XDGUtil(const std::vector<std::string>& argv,
     base::nix::CreateLaunchOptionsWithXdgActivation(base::BindOnce(
         [](base::RepeatingClosure quit_loop, base::LaunchOptions* options_out,
            base::LaunchOptions options) {
+          // Correct the XDG_CURRENT_DESKTOP environment variable before calling
+          // XDG, in case it was changed for compatibility.
+          if (const auto* orig = base::FindOrNull(
+                  options.environment, kOriginalXdgCurrentDesktopEnvVar))
+            options.environment.emplace(base::nix::kXdgCurrentDesktopEnvVar,
+                                        *orig);
           *options_out = std::move(options);
           std::move(quit_loop).Run();
         },