xref: /aosp_15_r20/external/cronet/base/nix/xdg_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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, const char* env_name,
91                                      const char* fallback_dir);
92 
93 // Wrapper around xdg_user_dir_lookup() from src/base/third_party/xdg-user-dirs
94 // This looks up "well known" user directories like the desktop and music
95 // folder. Examples of |dir_name| are DESKTOP and MUSIC.
96 BASE_EXPORT FilePath GetXDGUserDirectory(const char* dir_name,
97                                          const char* fallback_dir);
98 
99 // Get the path to write user-specific application data files to, as specified
100 // in the XDG Base Directory Specification:
101 // http://standards.freedesktop.org/basedir-spec/latest/
102 BASE_EXPORT FilePath GetXDGDataWriteLocation(Environment* env);
103 
104 // Get the list of paths to search for application data files, in order of
105 // preference, as specified in the XDG Base Directory Specification:
106 // http://standards.freedesktop.org/basedir-spec/latest/
107 // Called on the FILE thread.
108 BASE_EXPORT std::vector<FilePath> GetXDGDataSearchLocations(Environment* env);
109 
110 // Return an entry from the DesktopEnvironment enum with a best guess
111 // of which desktop environment we're using.  We use this to know when
112 // to attempt to use preferences from the desktop environment --
113 // proxy settings, password manager, etc.
114 BASE_EXPORT DesktopEnvironment GetDesktopEnvironment(Environment* env);
115 
116 // Return a string representation of the given desktop environment.
117 // May return NULL in the case of DESKTOP_ENVIRONMENT_OTHER.
118 BASE_EXPORT const char* GetDesktopEnvironmentName(DesktopEnvironment env);
119 // Convenience wrapper that calls GetDesktopEnvironment() first.
120 BASE_EXPORT const char* GetDesktopEnvironmentName(Environment* env);
121 
122 // Return an entry from the SessionType enum with a best guess
123 // of which session type we're using.
124 BASE_EXPORT SessionType GetSessionType(Environment& env);
125 
126 // Sets the global activation token from the environment and returns it if it
127 // exists, and removes it from the environment to prevent it from leaking into
128 // child processes.
129 BASE_EXPORT std::optional<std::string> ExtractXdgActivationTokenFromEnv(
130     Environment& env);
131 
132 // Sets the global activation token from the command line if it exists and
133 // removes it from the command line.
134 BASE_EXPORT void ExtractXdgActivationTokenFromCmdLine(
135     base::CommandLine& cmd_line);
136 
137 // Transfers ownership of the currently set global activation token if set.
138 BASE_EXPORT std::optional<std::string> TakeXdgActivationToken();
139 
140 // Sets the global token creator.
141 BASE_EXPORT void SetXdgActivationTokenCreator(
142     XdgActivationTokenCreator token_creator);
143 
144 // Tries to create an xdg-activation token and invokes the `callback` with
145 // `LaunchOptions` containing the token if available, or empty `LaunchOptions`.
146 BASE_EXPORT void CreateLaunchOptionsWithXdgActivation(
147     XdgActivationLaunchOptionsCallback callback);
148 
149 }  // namespace nix
150 }  // namespace base
151 
152 #endif  // BASE_NIX_XDG_UTIL_H_
153