Correct the parsing of the XDG_CURRENT_DESKTOP The XDG_CURRENT_DESKTOP could contain multiple values in priority order separated by colon. Go through them in the loop and return on the first one that we recognize/support. The previous state was causing problems with Chrome on the GNOME Classic mode (used in Red Hat Enterprise Linux 7) where the users with multiple Google accounts set in Chrome couldn't switch between them - only one was shown in the account switcher in i.e. GMail. The reason for that was that the XDG_CURRENT_DESKTOP's value GNOME:GNOME-Classic was parsed as not as base::nix::DesktopEnvironment::DESKTOP_ENVIRONMENT_GNOME. base: :nix::DesktopEnvironment::DESKTOP_ENVIRONMENT_OTHER and Change-Id: I122f24fd1cf5a0f932c3fccd5220152a9944609d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1541077 Reviewed-by: Lei Zhang Reviewed-by: Thomas Anderson Commit-Queue: Tomáš Popela Cr-Commit-Position: refs/heads/master@{#645224} diff --git a/base/nix/xdg_util.cc b/base/nix/xdg_util.cc index 9ff4d88..f051080 100644 --- a/base/nix/xdg_util.cc +++ b/base/nix/xdg_util.cc @@ -11,6 +11,7 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" #include "base/path_service.h" +#include "base/strings/string_split.h" #include "base/strings/string_util.h" #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" @@ -57,35 +58,37 @@ // XDG_CURRENT_DESKTOP is the newest standard circa 2012. std::string xdg_current_desktop; if (env->GetVar("XDG_CURRENT_DESKTOP", &xdg_current_desktop)) { - // Not all desktop environments set this env var as of this writing. - if (base::StartsWith(xdg_current_desktop, "Unity", - base::CompareCase::SENSITIVE)) { - // gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity - // DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz - std::string desktop_session; - if (env->GetVar("DESKTOP_SESSION", &desktop_session) && - desktop_session.find("gnome-fallback") != std::string::npos) { - return DESKTOP_ENVIRONMENT_GNOME; - } - return DESKTOP_ENVIRONMENT_UNITY; - } - if (xdg_current_desktop == "GNOME") - return DESKTOP_ENVIRONMENT_GNOME; - if (xdg_current_desktop == "X-Cinnamon") - return DESKTOP_ENVIRONMENT_CINNAMON; - if (xdg_current_desktop == "KDE") { - std::string kde_session; - if (env->GetVar(kKDESessionEnvVar, &kde_session)) { - if (kde_session == "5") { - return DESKTOP_ENVIRONMENT_KDE5; + // It could have multiple values separated by colon in priority order. + for (const auto& value : SplitStringPiece( + xdg_current_desktop, ":", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY)) { + if (value == "Unity") { + // gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity + // DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz + std::string desktop_session; + if (env->GetVar("DESKTOP_SESSION", &desktop_session) && + desktop_session.find("gnome-fallback") != std::string::npos) { + return DESKTOP_ENVIRONMENT_GNOME; } + return DESKTOP_ENVIRONMENT_UNITY; } - return DESKTOP_ENVIRONMENT_KDE4; + if (value == "GNOME") + return DESKTOP_ENVIRONMENT_GNOME; + if (value == "X-Cinnamon") + return DESKTOP_ENVIRONMENT_CINNAMON; + if (value == "KDE") { + std::string kde_session; + if (env->GetVar(kKDESessionEnvVar, &kde_session)) { + if (kde_session == "5") { + return DESKTOP_ENVIRONMENT_KDE5; + } + } + return DESKTOP_ENVIRONMENT_KDE4; + } + if (value == "Pantheon") + return DESKTOP_ENVIRONMENT_PANTHEON; + if (value == "XFCE") + return DESKTOP_ENVIRONMENT_XFCE; } - if (xdg_current_desktop == "Pantheon") - return DESKTOP_ENVIRONMENT_PANTHEON; - if (xdg_current_desktop == "XFCE") - return DESKTOP_ENVIRONMENT_XFCE; } // DESKTOP_SESSION was what everyone used in 2010. diff --git a/base/nix/xdg_util_unittest.cc b/base/nix/xdg_util_unittest.cc index e195303..ad81836 100644 --- a/base/nix/xdg_util_unittest.cc +++ b/base/nix/xdg_util_unittest.cc @@ -34,6 +34,7 @@ const char* const kDesktopXFCE = "xfce"; const char* const kXdgDesktopCinnamon = "X-Cinnamon"; const char* const kXdgDesktopGNOME = "GNOME"; +const char* const kXdgDesktopGNOMEClassic = "GNOME:GNOME-Classic"; const char* const kXdgDesktopKDE = "KDE"; const char* const kXdgDesktopPantheon = "Pantheon"; const char* const kXdgDesktopUnity = "Unity"; @@ -110,6 +111,15 @@ EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter)); } +TEST(XDGUtilTest, GetXdgDesktopGnomeClassic) { + MockEnvironment getter; + EXPECT_CALL(getter, GetVar(_, _)).WillRepeatedly(Return(false)); + EXPECT_CALL(getter, GetVar(Eq(kXdgDesktop), _)) + .WillOnce(DoAll(SetArgPointee<1>(kXdgDesktopGNOMEClassic), Return(true))); + + EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter)); +} + TEST(XDGUtilTest, GetXdgDesktopGnomeFallback) { MockEnvironment getter; EXPECT_CALL(getter, GetVar(_, _)).WillRepeatedly(Return(false));