File: xdg_util.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 7,235 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_NIX_XDG_UTIL_H_
#define BASE_NIX_XDG_UTIL_H_

// XDG refers to http://en.wikipedia.org/wiki/Freedesktop.org .
// This file contains utilities found across free desktop environments.

#include <optional>
#include <string>
#include <vector>

#include "base/base_export.h"
#include "base/functional/callback.h"
#include "base/strings/cstring_view.h"

namespace base {

class CommandLine;
class Environment;
class FilePath;
struct LaunchOptions;

namespace nix {

enum DesktopEnvironment {
  DESKTOP_ENVIRONMENT_OTHER = 0,
  DESKTOP_ENVIRONMENT_CINNAMON = 1,
  DESKTOP_ENVIRONMENT_DEEPIN = 2,
  DESKTOP_ENVIRONMENT_GNOME = 3,
  // KDE{3,4,5,6} are sufficiently different that we count
  // them as different desktop environments here.
  DESKTOP_ENVIRONMENT_KDE3 = 4,
  DESKTOP_ENVIRONMENT_KDE4 = 5,
  DESKTOP_ENVIRONMENT_KDE5 = 6,
  DESKTOP_ENVIRONMENT_KDE6 = 12,
  DESKTOP_ENVIRONMENT_PANTHEON = 7,
  DESKTOP_ENVIRONMENT_UKUI = 8,
  DESKTOP_ENVIRONMENT_UNITY = 9,
  DESKTOP_ENVIRONMENT_XFCE = 10,
  DESKTOP_ENVIRONMENT_LXQT = 11,
  DESKTOP_ENVIRONMENT_COSMIC = 13,
};

// Values based on valid types indicated in:
// https://www.freedesktop.org/software/systemd/man/pam_systemd.html; though
// "Unset" and "Other" are provided by us to distinguish between the potentially
// valid "Unspecified" and other cases where we may not be able to find the
// value.
enum class SessionType {
  kUnset = 0,
  kOther = 1,
  kUnspecified = 2,
  kTty = 3,
  kX11 = 4,
  kWayland = 5,
  kMir = 6,
};

using XdgActivationTokenCallback = base::OnceCallback<void(std::string token)>;
using XdgActivationTokenCreator =
    base::RepeatingCallback<void(XdgActivationTokenCallback callback)>;
using XdgActivationLaunchOptionsCallback =
    base::OnceCallback<void(LaunchOptions)>;

// The default XDG config directory name.
inline constexpr char kDotConfigDir[] = ".config";

// The XDG config directory environment variable.
inline constexpr char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";

// The XDG current desktop environment variable.
inline constexpr char kXdgCurrentDesktopEnvVar[] = "XDG_CURRENT_DESKTOP";

// The XDG session type environment variable.
inline constexpr char kXdgSessionTypeEnvVar[] = "XDG_SESSION_TYPE";

// The XDG activation token environment variable.
inline constexpr char kXdgActivationTokenEnvVar[] = "XDG_ACTIVATION_TOKEN";

// Desktop startup ID environment variable.
inline constexpr char kDesktopStartupIdEnvVar[] = "DESKTOP_STARTUP_ID";

// Internally used to communicate the activation token between a newly launched
// process and an existing browser process.
inline constexpr char kXdgActivationTokenSwitch[] = "xdg-activation-token";

// Utility function for getting XDG directories.
// |env_name| is the name of an environment variable that we want to use to get
// a directory path. |fallback_dir| is the directory relative to $HOME that we
// use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL.
// Examples of |env_name| are XDG_CONFIG_HOME and XDG_DATA_HOME.
BASE_EXPORT FilePath GetXDGDirectory(Environment* env,
                                     cstring_view env_name,
                                     const char* fallback_dir);

// Wrapper around xdg_user_dir_lookup() from src/base/third_party/xdg-user-dirs
// This looks up "well known" user directories like the desktop and music
// folder. Examples of |dir_name| are DESKTOP and MUSIC.
BASE_EXPORT FilePath GetXDGUserDirectory(const char* dir_name,
                                         const char* fallback_dir);

// Get the path to write user-specific application data files to, as specified
// in the XDG Base Directory Specification:
// http://standards.freedesktop.org/basedir-spec/latest/
BASE_EXPORT FilePath GetXDGDataWriteLocation(Environment* env);

// Get the list of paths to search for application data files, in order of
// preference, as specified in the XDG Base Directory Specification:
// http://standards.freedesktop.org/basedir-spec/latest/
// Called on the FILE thread.
BASE_EXPORT std::vector<FilePath> GetXDGDataSearchLocations(Environment* env);

// Return an entry from the DesktopEnvironment enum with a best guess
// of which desktop environment we're using.  We use this to know when
// to attempt to use preferences from the desktop environment --
// proxy settings, password manager, etc.
BASE_EXPORT DesktopEnvironment GetDesktopEnvironment(Environment* env);

// Return a string representation of the given desktop environment.
// May return NULL in the case of DESKTOP_ENVIRONMENT_OTHER.
BASE_EXPORT const char* GetDesktopEnvironmentName(DesktopEnvironment env);
// Convenience wrapper that calls GetDesktopEnvironment() first.
BASE_EXPORT const char* GetDesktopEnvironmentName(Environment* env);

// Return an entry from the SessionType enum with a best guess
// of which session type we're using.
BASE_EXPORT SessionType GetSessionType(Environment& env);

// Sets the global activation token from the environment and returns it if it
// exists, and removes it from the environment to prevent it from leaking into
// child processes.
BASE_EXPORT std::optional<std::string> ExtractXdgActivationTokenFromEnv(
    Environment& env);

// Sets the global activation token from the command line if it exists and
// removes it from the command line.
BASE_EXPORT void ExtractXdgActivationTokenFromCmdLine(
    base::CommandLine& cmd_line);

// Sets the global activation token not received from the command line or as
// an environment variable, e.g. from notification D-BUS API.
BASE_EXPORT void SetActivationToken(std::string token);

// Transfers ownership of the currently set global activation token if set.
BASE_EXPORT std::optional<std::string> TakeXdgActivationToken();

// Sets the global token creator.
BASE_EXPORT void SetXdgActivationTokenCreator(
    XdgActivationTokenCreator token_creator);

// Tries to create an xdg-activation token and invokes the `callback` with
// `LaunchOptions` containing the token if available, or empty `LaunchOptions`.
// This must be called on UI thread.
BASE_EXPORT void CreateLaunchOptionsWithXdgActivation(
    XdgActivationLaunchOptionsCallback callback);

// Tries to create an xdg-activation token and invokes the `callback` with the
// token if available, or an empty string.
// This must be called on UI thread.
BASE_EXPORT void CreateXdgActivationToken(XdgActivationTokenCallback callback);

// Returns a request path as specified in v0.9 of xdg-desktop-portal:
// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Request.html
BASE_EXPORT
std::string XdgDesktopPortalRequestPath(const std::string& sender,
                                        const std::string& token);

// Returns a session path as specified in v0.9 of xdg-desktop-portal:
// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Session.html
BASE_EXPORT
std::string XdgDesktopPortalSessionPath(const std::string& sender,
                                        const std::string& token);

}  // namespace nix
}  // namespace base

#endif  // BASE_NIX_XDG_UTIL_H_