Repository: electron/electron (language: cpp)

Issue:
[Bug]: --ozone-platform-hint command line flag ignored
### 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.3

### What operating system are you using?

Other Linux

### Operating System Version

Arch Linux

### What arch are you using?

x64

### Last Known Working Electron version

_No response_

### Expected Behavior

Setting the `--ozone-platform-hint` flag to `auto` or `wayland` should make electron use wayland (assuming `--enable-features=UseOzonePlatform` is also set)

### Actual Behavior

The flag is ignored.

### Testcase Gist URL

_No response_

### Additional Information

Using `--ozone-platform=wayland` does work, but then it fails when not running wayland.


Discussion / hints:
Can reproduce this on Signal-desktop (electron 17.3.1), VSCode (electron 17.4.1).

Since this flag was added in chromium 97, Electron 17+ should support this flag.
As documented Electron doesn't automatically inherit all Chromium flags.  That flag for instance is supported inside `//chrome` **not** `//content` which means we don't support it by default.

https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/chrome_browser_main_extra_parts_linux.cc;l=123-135;drc=edab26bf11e993e5069d433e8c2186ad2dee012e;bpv=1;bpt=1

Someone would have to copy that code into the right place

Fix patch:
diff --git a/filenames.gni b/filenames.gni
index df58773b49c74..9fda05b41f983 100644
--- a/filenames.gni
+++ b/filenames.gni
@@ -23,6 +23,7 @@ filenames = {
 
   lib_sources_linux = [
     "shell/browser/browser_linux.cc",
+    "shell/browser/electron_browser_main_parts_linux.cc",
     "shell/browser/lib/power_observer_linux.cc",
     "shell/browser/lib/power_observer_linux.h",
     "shell/browser/linux/unity_service.cc",
diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc
index a8891a0a782da..13bd28b53443c 100644
--- a/shell/browser/electron_browser_main_parts.cc
+++ b/shell/browser/electron_browser_main_parts.cc
@@ -217,6 +217,7 @@ int ElectronBrowserMainParts::PreEarlyInitialization() {
   HandleSIGCHLD();
 #endif
 #if BUILDFLAG(IS_LINUX)
+  DetectOzonePlatform();
   ui::OzonePlatform::PreEarlyInitialization();
 #endif
 #if BUILDFLAG(IS_MAC)
diff --git a/shell/browser/electron_browser_main_parts.h b/shell/browser/electron_browser_main_parts.h
index 95ced64ed7c9e..3c45ed29bfbb7 100644
--- a/shell/browser/electron_browser_main_parts.h
+++ b/shell/browser/electron_browser_main_parts.h
@@ -122,6 +122,10 @@ class ElectronBrowserMainParts : public content::BrowserMainParts {
       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
 #endif
 
+#if BUILDFLAG(IS_LINUX)
+  void DetectOzonePlatform();
+#endif
+
 #if BUILDFLAG(IS_MAC)
   void FreeAppDelegate();
   void RegisterURLHandler();
diff --git a/shell/browser/electron_browser_main_parts_linux.cc b/shell/browser/electron_browser_main_parts_linux.cc
new file mode 100644
index 0000000000000..919cc085a2755
--- /dev/null
+++ b/shell/browser/electron_browser_main_parts_linux.cc
@@ -0,0 +1,134 @@
+// Copyright (c) 2022 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "shell/browser/electron_browser_main_parts.h"
+
+#include "base/command_line.h"
+#include "base/environment.h"
+#include "ui/ozone/public/ozone_switches.h"
+
+#if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/nix/xdg_util.h"
+#include "base/threading/thread_restrictions.h"
+#endif
+
+#if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+
+constexpr char kPlatformWayland[] = "wayland";
+
+bool HasWaylandDisplay(base::Environment* env) {
+  std::string wayland_display;
+  const bool has_wayland_display =
+      env->GetVar("WAYLAND_DISPLAY", &wayland_display) &&
+      !wayland_display.empty();
+  if (has_wayland_display)
+    return true;
+
+  std::string xdg_runtime_dir;
+  const bool has_xdg_runtime_dir =
+      env->GetVar("XDG_RUNTIME_DIR", &xdg_runtime_dir) &&
+      !xdg_runtime_dir.empty();
+  if (has_xdg_runtime_dir) {
+    auto wayland_server_pipe =
+        base::FilePath(xdg_runtime_dir).Append("wayland-0");
+    // Normally, this should happen exactly once, at the startup of the main
+    // process.
+    base::ScopedAllowBlocking allow_blocking;
+    return base::PathExists(wayland_server_pipe);
+  }
+
+  return false;
+}
+
+#endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+
+#if BUILDFLAG(OZONE_PLATFORM_X11)
+constexpr char kPlatformX11[] = "x11";
+#endif
+
+namespace electron {
+
+namespace {
+
+// Evaluates the environment and returns the effective platform name for the
+// given |ozone_platform_hint|.
+// For the "auto" value, returns "wayland" if the XDG session type is "wayland",
+// "x11" otherwise.
+// For the "wayland" value, checks if the Wayland server is available, and
+// returns "x11" if it is not.
+// See https://crbug.com/1246928.
+std::string MaybeFixPlatformName(const std::string& ozone_platform_hint) {
+#if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+  // Wayland is selected if both conditions below are true:
+  // 1. The user selected either 'wayland' or 'auto'.
+  // 2. The XDG session type is 'wayland', OR the user has selected 'wayland'
+  //    explicitly and a Wayland server is running.
+  // Otherwise, fall back to X11.
+  if (ozone_platform_hint == kPlatformWayland ||
+      ozone_platform_hint == "auto") {
+    auto env(base::Environment::Create());
+
+    std::string xdg_session_type;
+    const bool has_xdg_session_type =
+        env->GetVar(base::nix::kXdgSessionTypeEnvVar, &xdg_session_type) &&
+        !xdg_session_type.empty();
+
+    if ((has_xdg_session_type && xdg_session_type == "wayland") ||
+        (ozone_platform_hint == kPlatformWayland &&
+         HasWaylandDisplay(env.get()))) {
+      return kPlatformWayland;
+    }
+  }
+#endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+
+#if BUILDFLAG(OZONE_PLATFORM_X11)
+  if (ozone_platform_hint == kPlatformX11) {
+    return kPlatformX11;
+  }
+#if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+  if (ozone_platform_hint == kPlatformWayland ||
+      ozone_platform_hint == "auto") {
+    // We are here if:
+    // - The binary has both X11 and Wayland backends.
+    // - The user wanted Wayland but that did not work, otherwise it would have
+    //   been returned above.
+    if (ozone_platform_hint == kPlatformWayland) {
+      LOG(WARNING) << "No Wayland server is available. Falling back to X11.";
+    } else {
+      LOG(WARNING) << "This is not a Wayland session.  Falling back to X11. "
+                      "If you need to run Chrome on Wayland using some "
+                      "embedded compositor, e. g., Weston, please specify "
+                      "Wayland as your preferred Ozone platform, or use "
+                      "--ozone-platform=wayland.";
+    }
+    return kPlatformX11;
+  }
+#endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
+#endif  // BUILDFLAG(OZONE_PLATFORM_X11)
+
+  return ozone_platform_hint;
+}
+
+}  // namespace
+
+void ElectronBrowserMainParts::DetectOzonePlatform() {
+  auto* const command_line = base::CommandLine::ForCurrentProcess();
+  if (!command_line->HasSwitch(switches::kOzonePlatform)) {
+    const auto ozone_platform_hint =
+        command_line->GetSwitchValueASCII(switches::kOzonePlatformHint);
+    if (!ozone_platform_hint.empty()) {
+      command_line->AppendSwitchASCII(
+          switches::kOzonePlatform, MaybeFixPlatformName(ozone_platform_hint));
+    }
+  }
+
+  auto env = base::Environment::Create();
+  std::string desktop_startup_id;
+  if (env->GetVar("DESKTOP_STARTUP_ID", &desktop_startup_id))
+    command_line->AppendSwitchASCII("desktop-startup-id", desktop_startup_id);
+}
+
+}  // namespace electron


Test patch: