1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_NIX_XDG_UTIL_H_ 6 #define BASE_NIX_XDG_UTIL_H_ 7 8 // XDG refers to http://en.wikipedia.org/wiki/Freedesktop.org . 9 // This file contains utilities found across free desktop environments. 10 11 #include <optional> 12 #include <string> 13 #include <vector> 14 15 #include "base/base_export.h" 16 #include "base/functional/callback.h" 17 18 namespace base { 19 20 class CommandLine; 21 class Environment; 22 class FilePath; 23 struct LaunchOptions; 24 25 namespace nix { 26 27 enum DesktopEnvironment { 28 DESKTOP_ENVIRONMENT_OTHER = 0, 29 DESKTOP_ENVIRONMENT_CINNAMON = 1, 30 DESKTOP_ENVIRONMENT_DEEPIN = 2, 31 DESKTOP_ENVIRONMENT_GNOME = 3, 32 // KDE{3,4,5,6} are sufficiently different that we count 33 // them as different desktop environments here. 34 DESKTOP_ENVIRONMENT_KDE3 = 4, 35 DESKTOP_ENVIRONMENT_KDE4 = 5, 36 DESKTOP_ENVIRONMENT_KDE5 = 6, 37 DESKTOP_ENVIRONMENT_KDE6 = 12, 38 DESKTOP_ENVIRONMENT_PANTHEON = 7, 39 DESKTOP_ENVIRONMENT_UKUI = 8, 40 DESKTOP_ENVIRONMENT_UNITY = 9, 41 DESKTOP_ENVIRONMENT_XFCE = 10, 42 DESKTOP_ENVIRONMENT_LXQT = 11, 43 }; 44 45 // Values based on valid types indicated in: 46 // https://www.freedesktop.org/software/systemd/man/pam_systemd.html; though 47 // "Unset" and "Other" are provided by us to distinguish between the potentially 48 // valid "Unspecified" and other cases where we may not be able to find the 49 // value. 50 enum class SessionType { 51 kUnset = 0, 52 kOther = 1, 53 kUnspecified = 2, 54 kTty = 3, 55 kX11 = 4, 56 kWayland = 5, 57 kMir = 6, 58 }; 59 60 using XdgActivationTokenCallback = base::OnceCallback<void(std::string token)>; 61 using XdgActivationTokenCreator = 62 base::RepeatingCallback<void(XdgActivationTokenCallback callback)>; 63 using XdgActivationLaunchOptionsCallback = 64 base::OnceCallback<void(LaunchOptions)>; 65 66 // The default XDG config directory name. 67 BASE_EXPORT extern const char kDotConfigDir[]; 68 69 // The XDG config directory environment variable. 70 BASE_EXPORT extern const char kXdgConfigHomeEnvVar[]; 71 72 // The XDG current desktop environment variable. 73 BASE_EXPORT extern const char kXdgCurrentDesktopEnvVar[]; 74 75 // The XDG session type environment variable. 76 BASE_EXPORT extern const char kXdgSessionTypeEnvVar[]; 77 78 // The XDG activation token environment variable. 79 BASE_EXPORT extern const char kXdgActivationTokenEnvVar[]; 80 81 // Internally used to communicate the activation token between a newly launched 82 // process and an existing browser process. 83 BASE_EXPORT extern const char kXdgActivationTokenSwitch[]; 84 85 // Utility function for getting XDG directories. 86 // |env_name| is the name of an environment variable that we want to use to get 87 // a directory path. |fallback_dir| is the directory relative to $HOME that we 88 // use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL. 89 // Examples of |env_name| are XDG_CONFIG_HOME and XDG_DATA_HOME. 90 BASE_EXPORT FilePath GetXDGDirectory(Environment* env, 91 const char* env_name, 92 const char* fallback_dir); 93 94 // Wrapper around xdg_user_dir_lookup() from src/base/third_party/xdg-user-dirs 95 // This looks up "well known" user directories like the desktop and music 96 // folder. Examples of |dir_name| are DESKTOP and MUSIC. 97 BASE_EXPORT FilePath GetXDGUserDirectory(const char* dir_name, 98 const char* fallback_dir); 99 100 // Get the path to write user-specific application data files to, as specified 101 // in the XDG Base Directory Specification: 102 // http://standards.freedesktop.org/basedir-spec/latest/ 103 BASE_EXPORT FilePath GetXDGDataWriteLocation(Environment* env); 104 105 // Get the list of paths to search for application data files, in order of 106 // preference, as specified in the XDG Base Directory Specification: 107 // http://standards.freedesktop.org/basedir-spec/latest/ 108 // Called on the FILE thread. 109 BASE_EXPORT std::vector<FilePath> GetXDGDataSearchLocations(Environment* env); 110 111 // Return an entry from the DesktopEnvironment enum with a best guess 112 // of which desktop environment we're using. We use this to know when 113 // to attempt to use preferences from the desktop environment -- 114 // proxy settings, password manager, etc. 115 BASE_EXPORT DesktopEnvironment GetDesktopEnvironment(Environment* env); 116 117 // Return a string representation of the given desktop environment. 118 // May return NULL in the case of DESKTOP_ENVIRONMENT_OTHER. 119 BASE_EXPORT const char* GetDesktopEnvironmentName(DesktopEnvironment env); 120 // Convenience wrapper that calls GetDesktopEnvironment() first. 121 BASE_EXPORT const char* GetDesktopEnvironmentName(Environment* env); 122 123 // Return an entry from the SessionType enum with a best guess 124 // of which session type we're using. 125 BASE_EXPORT SessionType GetSessionType(Environment& env); 126 127 // Sets the global activation token from the environment and returns it if it 128 // exists, and removes it from the environment to prevent it from leaking into 129 // child processes. 130 BASE_EXPORT std::optional<std::string> ExtractXdgActivationTokenFromEnv( 131 Environment& env); 132 133 // Sets the global activation token from the command line if it exists and 134 // removes it from the command line. 135 BASE_EXPORT void ExtractXdgActivationTokenFromCmdLine( 136 base::CommandLine& cmd_line); 137 138 // Transfers ownership of the currently set global activation token if set. 139 BASE_EXPORT std::optional<std::string> TakeXdgActivationToken(); 140 141 // Sets the global token creator. 142 BASE_EXPORT void SetXdgActivationTokenCreator( 143 XdgActivationTokenCreator token_creator); 144 145 // Tries to create an xdg-activation token and invokes the `callback` with 146 // `LaunchOptions` containing the token if available, or empty `LaunchOptions`. 147 BASE_EXPORT void CreateLaunchOptionsWithXdgActivation( 148 XdgActivationLaunchOptionsCallback callback); 149 150 // Returns a request path as specified in v0.9 of xdg-desktop-portal: 151 // https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Request.html 152 BASE_EXPORT 153 std::string XdgDesktopPortalRequestPath(const std::string& sender, 154 const std::string& token); 155 156 // Returns a session path as specified in v0.9 of xdg-desktop-portal: 157 // https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Session.html 158 BASE_EXPORT 159 std::string XdgDesktopPortalSessionPath(const std::string& sender, 160 const std::string& token); 161 162 } // namespace nix 163 } // namespace base 164 165 #endif // BASE_NIX_XDG_UTIL_H_ 166